Integration

Federated AQL and Gateways

Set up national and regional gateways, query records across openEHR repositories, and handle routing, partial results, and follow-up requests.

AMCDR's gateways follow the draft Federation Tier with AQL 0.2 requirements N1–N27. Automated tests check the implementation; independent openEHR, IHE, privacy and security certification require separate assessment.

What the federation provides

An authorized application sends one patient-scoped AQL query to the National Federation Gateway. The Gateway resolves identity and record location outside AQL, asks eligible Regional Federation Agents to query their local AMCDR by local ehr_id, and returns one result. Clinical compositions remain in their controlling CDR.

ComponentResponsibilityClinical storage
National Federation GatewayStandard Query API facade, AMMPI resolution, locator and consent gate, fan-out, combined results, provenance, follow-up routing and central audit.None
Regional Federation AgentGateway authentication, signed clinician context, local policy/consent, EHR resolution, AMCDR connector and regional audit.None
AMMPI and record locatorCanonical patient identity and patient-to-node metadata.No clinical payloads
PCMNational policy, consent and signed decision context.Policy metadata only
AMCDR/openEHR CDREHRs, versions, compositions, contributions, templates and final source-side access decision.Authoritative clinical content

End-to-end query sequence

  1. The authenticated client calls GET|POST /openehr/v1/query/aql on the National Gateway.
  2. The Gateway treats e/ehr_status/subject/external_ref/id/value as a facade patient-resolution predicate only.
  3. AMMPI resolves the input to the canonical patient, and the locator supplies candidate nodes for an undirected query.
  4. National consent and policy remove denied nodes before local resolution or query.
  5. Each Regional Agent resolves the canonical patient through local AMCDR GET /openehr/v1/ehr?subject_id=....
  6. The Gateway sends standard AQL containing only e/ehr_id/value = '<local-ehr-id>'.
  7. The Agent validates signed clinician context, repeats local controls and calls local AMCDR POST /openehr/v1/query/aql.
  8. The Gateway combines rows, adds only selected provenance and applies global DISTINCT/ORDER.

Client request

POST /openehr/v1/query/aql
Authorization: Bearer <credential from the trusted identity boundary>
X-User-ID: clinician-123
X-User-Roles: doctor
X-Organisation: hospital-a
X-Facility: facility-a
X-Purpose-Of-Use: TREAT
X-Correlation-ID: 3fbc49a7-09b0-49ab-8890-09912e9faf80
Content-Type: application/json

{
  "q": "SELECT e/ehr_status/subject/external_ref/id/value AS patient_id, c/uid/value AS composition_id FROM EHR e CONTAINS COMPOSITION c WHERE e/ehr_status/subject/external_ref/id/value = $patient_id ORDER BY composition_id",
  "query_parameters": {"patient_id": "MPI-000123"}
}

The local CDR does not need the national ID in EHR_STATUS.subject. When the subject is selected, the Gateway re-injects the original resolution input into every row.

Directed queries and provenance

SELECT
  p/id AS endpoint_id,
  p/system_id AS system_id,
  p/organisation AS organisation,
  c/uid/value AS composition_id
FROM ENDPOINT p ["hospital-a", "hospital-b"]
CONTAINS EHR e
CONTAINS COMPOSITION c
WHERE e/ehr_status/subject/external_ref/id/value = 'MPI-000123'

FROM ORGANISATION p ["regional-health-authority-a"] expands an organisation to its registered endpoints. IDs in directives are stable registry identifiers, never URLs. Endpoint columns appear only when selected, and an alias collision is renamed deterministically.

Undirected aggregates are blocked because cross-node correctness cannot be assumed. An aggregate is permitted only when an endpoint- or organisation-directed query resolves to one endpoint.

Result and failure semantics

CaseBehavior
Normal result{q, columns, rows, meta}; no hidden endpoint columns.
Default duplicate handlingRows pass through. Use endpoint provenance, SELECT DISTINCT or explicit ?dedup=version.
Global orderingORDER BY runs over the combined result after fan-out and deduplication.
Patient found nowhereHTTP 200, empty rows and meta.resolution_status=not-found.
Some nodes failHTTP 200 when at least one succeeds; meta.partial=true and every endpoint outcome remains visible.
All targeted nodes time outHTTP 504 with the normal metadata envelope.
Invalid or unsafe facade AQLHTTP 400 before any node query.
Unknown directed endpointHTTP 404.

Endpoint statuses include active, offline, time-out, not-resolved, consent-denied and excluded. LIMIT/OFFSET remain node-local and are not stable cross-node pagination.

Follow-up reads and writes

The facade forwards governed composition, versioned-composition and contribution operations under /openehr/v1/ehr/{ehr_id}/.... It selects the owner by the creating_system_id in the OBJECT_VERSION_ID, then an explicit ?endpoint= or X-AMCDR-Endpoint-ID, then ask-all for reads only.

Versioned writes must route to one controlling CDR. New objects must name one endpoint. Ambiguous writes fail with HTTP 400, and openEHR UIDs are never changed.

Register and configure a node

1. Prepare AMCDR

2. Configure the Regional Agent

AMCDR_RFA_DEPLOYMENT_ENVIRONMENT=production
AMCDR_RFA_NODE_ID=hospital-a
AMCDR_RFA_GATEWAY_TOKEN=<random-agent-token>
AMCDR_RFA_CLIENT_CONTEXT_SECRET=<random-secret-at-least-32-bytes>
AMCDR_RFA_CLIENT_CONTEXT_REQUIRED=true
AMCDR_RFA_LOCAL_CDR_BASE_URL=https://amcdr-a.internal.example.org
AMCDR_RFA_ALLOW_RUNTIME_CONNECTOR_CONFIGURATION=false
AMCDR_RFA_AMCDR_API_KEY=<dedicated-expiring-amcdr-integration-key>
AMCDR_RFA_AMCDR_API_KEY_HEADER=X-API-Key
AMCDR_RFA_AMCDR_VERIFY_TLS=true
AMCDR_RFA_PCM_REQUIRED=true
AMCDR_RFA_AMMPI_REQUIRED=true

3. Configure the National Gateway

AMCDR_NFG_DEPLOYMENT_ENVIRONMENT=production
AMCDR_NFG_FEDERATION_TOKEN=<random-client-api-token>
AMCDR_NFG_REGIONAL_GATEWAY_TOKEN=<same-agent-token>
AMCDR_NFG_REGIONAL_CLIENT_CONTEXT_SECRET=<same-signed-context-secret>
AMCDR_NFG_REGIONAL_VERIFY_TLS=true
AMCDR_NFG_FEDERATION_AQL_ENABLED=true
AMCDR_NFG_PCM_REQUIRED=true
AMCDR_NFG_AMMPI_REQUIRED=true

Place the public National Gateway behind the approved OIDC, national-PKI, mTLS or service-mesh identity boundary. Both processes fail production startup when SQLite, short/development/placeholder credentials, unsafe PCM/AMMPI authentication, unsafe TLS, optional configured PCM/AMMPI or unsigned forwarded clinician context are detected.

4. Register the node

{
  "id": "hospital-a",
  "name": "Hospital A Regional Agent",
  "region": "central",
  "organisation": "regional-health-authority-a",
  "base_url": "https://regional-agent-a.example.org",
  "cdr_base_url": "https://amcdr-a.example.org/openehr/v1",
  "system_id": "amcdr-a.example.org",
  "creating_system_ids": ["legacy-cdr-a.example.org"],
  "connection_type_code": "openehr-rest-query",
  "certificate_fingerprint": "sha256:<fingerprint>",
  "supported_capabilities": ["federation-aql-node", "openehr-rest-query", "ips-2.0.1"],
  "status": "active"
}

Use the National Gateway Nodes UI or POST /federation/nodes. Each endpoint has exactly one managing organisation. Locator lifecycle events should include local_ehr_id when known.

Authentication and access checks

The configured identity provider or gateway authenticates the client. The National Gateway authenticates onward and signs a short-lived X-AMCDR-Client-Context containing clinician ID, roles, organisation, facility, purpose, correlation, audience and scope. The Regional Agent validates it and compares every field to the forwarded request before local resolution or query.

National consent determines which nodes may receive the query. Each Regional Agent checks local policy and consent, and AMCDR makes the final access decision.

Acceptance tests

TrackVerify
TransparencyA plain standard patient AQL returns single-CDR-shaped rows with no implicit endpoint fields.
Identity scopeNodes receive local ehr_id and never the facade subject predicate.
Directed routingEndpoint/organisation targeting, not-resolved members and single-node aggregate.
Partial resultsActive, not-resolved, consent-denied, timeout and upstream-error outcomes.
Combined rowsDefault duplicate pass-through, global DISTINCT/ORDER and version dedup.
OwnershipFollow-up read/write by creating-system ID, no UID mutation, ambiguous write rejection.
SecuritySigned context accepted; missing, expired, tampered or mismatched context rejected; source-side consent denial retained.
Identity lifecycleAMMPI merge/survivor resolution changes subsequent routing correctly.

Monitoring and troubleshooting

Scrape /metrics and monitor 4xx/5xx/504 rates, latency, active nodes, locator/audit volume, outbox growth, database health and broker lag. Alert on zero active nodes, sustained partial responses, signed-context failures and replay backlog.

SymptomCheck
401 at National GatewayClient credential and trusted identity boundary.
401/403 at Regional AgentGateway token; signed-context signature, issuer, audience, scope, expiry, clock and exact claim match.
not-foundAMMPI canonical identity and locator lifecycle ingestion.
not-resolvedLocal patient-to-EHR link and the AMCDR EHR lookup operation.
Node rejects queryConfirm an ehr_id predicate and no subject predicate reached the node.
Follow-up write rejectedControlling system/creating-system registry or explicit endpoint for a new object.
504Regional TLS/network, Agent health, AMCDR latency and timeout settings.

Standards and related guides