|
| 1 | +# Engine integration guide |
| 2 | + |
| 3 | +This guide explains how to add a new engine to the OpenBinding gateway. It covers schema specialization, validation hooks, routing, and tests. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The gateway validates incoming instances in stages and then routes them to the selected engine. |
| 8 | + |
| 9 | +Validation stages: |
| 10 | +1. General schema |
| 11 | +2. Specialization schema |
| 12 | +3. General semantic rules |
| 13 | +4. Engine semantic rules |
| 14 | + |
| 15 | +Engines integrate through the gateway plugin interface and a specialization schema. |
| 16 | + |
| 17 | +## Required artifacts |
| 18 | + |
| 19 | +1. Engine plugin (gateway): implement `EngineValidationPlugin`. |
| 20 | +2. Specialization schema: `schemas/specializations/<engine-id>.schema.json`. |
| 21 | +3. Specialization model (recommended): `schemas/specializations/<engine-id>.schema.mermaid`. |
| 22 | +4. Engine URL in registry + env wiring. |
| 23 | +5. Tests for validation and transformation. |
| 24 | + |
| 25 | +## Step-by-step |
| 26 | + |
| 27 | +### 1) Add a specialization schema |
| 28 | + |
| 29 | +Create a specialization schema under `schemas/specializations/`. |
| 30 | + |
| 31 | +### 1.1) Add a specialization model (recommended) |
| 32 | + |
| 33 | +To enable visual exploration in the frontend **Schema Explorer** (`JSON | Model` tabs), add a Mermaid model next to your specialization schema: |
| 34 | + |
| 35 | +- Path: `schemas/specializations/<engine-id>.schema.mermaid` |
| 36 | +- Naming must match your engine id exactly (`<engine-id>`) |
| 37 | + |
| 38 | +The Mermaid model is optional, but strongly recommended for maintainability and onboarding. |
| 39 | + |
| 40 | +If the file is missing, the frontend will show **Model not available** while keeping JSON schema validation and all engine workflows fully operational. |
| 41 | + |
| 42 | +#### Good practices |
| 43 | + |
| 44 | +- Keep JSON and Mermaid aligned conceptually (same constraints/capabilities). |
| 45 | +- Keep node/edge labels stable and meaningful across versions. |
| 46 | +- Prefer modular Mermaid subgraphs for large models. |
| 47 | +- Update both files in the same PR when constraints change. |
| 48 | +- Avoid changing `<engine-id>` naming once released, to prevent schema/model mismatch. |
| 49 | + |
| 50 | + |
| 51 | +## Engine options defaults (Playground) |
| 52 | + |
| 53 | +The frontend Playground can prefill the `options` object depending on the selected engine. |
| 54 | +To support this, the gateway exposes engine-level defaults at: |
| 55 | + |
| 56 | +- `GET /v1/engines/{engine_id}/options/defaults` |
| 57 | + |
| 58 | +If the engine has no options, the endpoint returns an empty JSON object: `{}`. |
| 59 | + |
| 60 | +### How to define defaults |
| 61 | + |
| 62 | +Defaults are defined in the gateway engine plugin by implementing `get_default_options()`. |
| 63 | +Example: |
| 64 | + |
| 65 | +- Return `{}` if your engine does not accept any options. |
| 66 | +- Return a JSON object with the gateway defaults (e.g. `{ "iterations_count": 1000 }`) if your engine supports options. |
| 67 | + |
| 68 | +### 2) Implement the engine plugin |
| 69 | + |
| 70 | +Create a new plugin in `openbinding-gateway/src/openbinding_gateway/validation/engine_plugins/`: |
| 71 | + |
| 72 | +```python |
| 73 | +from typing import Any, Dict, List, Tuple |
| 74 | +import httpx |
| 75 | +from .base import EngineValidationPlugin |
| 76 | +from ...models.api import ValidationViolation |
| 77 | + |
| 78 | +class MyEnginePlugin(EngineValidationPlugin): |
| 79 | + async def check_engine_health(self, base_url: str, client: httpx.AsyncClient) -> bool: |
| 80 | + resp = await client.get(f"{base_url.rstrip('/')}/health") |
| 81 | + return resp.status_code == 200 |
| 82 | + |
| 83 | + def get_capabilities(self) -> Dict[str, Any]: |
| 84 | + return { |
| 85 | + "qos_features_supported": ["*"], |
| 86 | + "composition_nodes_supported": ["TASK", "SEQ"], |
| 87 | + "objective_types_supported": ["weighted_sum"], |
| 88 | + "constraints_supported": ["attribute_bound"], |
| 89 | + "schema_version": "v1", |
| 90 | + } |
| 91 | + |
| 92 | + def get_specialization_schema_path(self) -> str: |
| 93 | + # Uses SCHEMAS_DIR if available |
| 94 | + # e.g. /app/schemas/specializations/my-engine.schema.json |
| 95 | + ... |
| 96 | + |
| 97 | + def validate_semantics(self, instance: Dict[str, Any]) -> List[ValidationViolation]: |
| 98 | + violations: List[ValidationViolation] = [] |
| 99 | + # Add engine-specific invariants here |
| 100 | + return violations |
| 101 | + |
| 102 | + def transform_request(self, instance: Dict[str, Any], options: Dict[str, Any] = {}) -> Tuple[Dict[str, Any], List[str]]: |
| 103 | + # Map general instance to engine request payload |
| 104 | + return {"instance": instance, "options": options}, [] |
| 105 | + |
| 106 | + def transform_response(self, engine_response: Dict[str, Any], original_request: Dict[str, Any]) -> Dict[str, Any]: |
| 107 | + # Map engine response to gateway solution format |
| 108 | + return engine_response |
| 109 | +``` |
| 110 | + |
| 111 | +### 3) Register the plugin and URL |
| 112 | + |
| 113 | +Add the plugin to `EngineRegistry`: |
| 114 | + |
| 115 | +- File: `openbinding-gateway/src/openbinding_gateway/registry/engine.py` |
| 116 | +- Add env var for the engine URL (e.g. `ENGINE_MY_ENGINE_URL`). |
| 117 | +- Register the plugin in the initialization block. |
| 118 | + |
| 119 | +Example: |
| 120 | + |
| 121 | +```python |
| 122 | +from ..validation.engine_plugins.my_engine import MyEnginePlugin |
| 123 | + |
| 124 | +_engine_urls = { |
| 125 | + "my-engine": os.getenv("ENGINE_MY_ENGINE_URL", "http://engine-my:1234"), |
| 126 | +} |
| 127 | + |
| 128 | +EngineRegistry.register("my-engine", MyEnginePlugin()) |
| 129 | +``` |
| 130 | + |
| 131 | +### 4) Ensure schema endpoints work |
| 132 | + |
| 133 | +The gateway exposes: |
| 134 | + |
| 135 | +- `/v1/schemas/general` |
| 136 | +- `/v1/schemas/general/model` |
| 137 | +- `/v1/schemas/<engine-id>` |
| 138 | +- `/v1/schemas/<engine-id>/model` |
| 139 | + |
| 140 | +Your specialization schema must exist and be discoverable via `SCHEMAS_DIR`. |
| 141 | +Your specialization model should follow the same directory and naming convention to be discoverable by the `/model` endpoint. |
| 142 | + |
| 143 | +### 5) Add tests |
| 144 | + |
| 145 | +Recommended tests: |
| 146 | + |
| 147 | +- Schema and semantic validation: `openbinding-gateway/tests/test_validation_comprehensive.py` |
| 148 | +- Plugin request/response transformation: `openbinding-gateway/tests/test_plugin_transformation.py` |
| 149 | +- Integration tests via docker compose (if the engine is available) |
| 150 | + |
| 151 | +### 6) Wire docker compose (if needed) |
| 152 | + |
| 153 | +Add the engine service to `docker-compose.yml` and expose the engine URL to the gateway: |
| 154 | + |
| 155 | +```yaml |
| 156 | +environment: |
| 157 | + - ENGINE_MY_ENGINE_URL=http://engine-my:1234 |
| 158 | +``` |
| 159 | +
|
| 160 | +## Validation expectations |
| 161 | +
|
| 162 | +The gateway uses schema defaults and semantic checks before engine-specific validation. If your engine depends on implicit rules, enforce them in `validate_semantics`. |
| 163 | + |
| 164 | +Common checks: |
| 165 | + |
| 166 | +- Unsupported composition nodes |
| 167 | +- Unsupported constraint types or objective types |
| 168 | +- Missing candidates or missing QoS values |
| 169 | +- Attribute bounds on missing features |
| 170 | + |
| 171 | +## Troubleshooting |
| 172 | + |
| 173 | +- Check `/v1/engines` to confirm the engine is registered and reachable. |
| 174 | +- Use `/v1/analyze` for validation errors and warnings. |
| 175 | +- Ensure `SCHEMAS_DIR` resolves to the folder containing your specialization schema. |
| 176 | +- If the Model tab shows unavailable, confirm `<engine-id>.schema.mermaid` exists under `schemas/specializations/` and matches engine id naming exactly. |
0 commit comments