---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# SAP Digital Manufacturing Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and SAP Digital Manufacturing (Example)

metadata:
  name: SAP Digital Manufacturing Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  sapDmApiHost:
    description: Public API host of your SAP DM tenant
    type: string
    default: api.eu20.dmc.cloud.sap

  oauthTokenUrl:
    description: OAuth token endpoint from the service key of your SAP DM instance
    type: string
    default: https://example.authentication.eu20.hana.ondemand.com/oauth/token

  oauthClientId:
    description: OAuth client ID from the service key
    type: string

  oauthClientSecret:
    description: OAuth client secret from the service key
    type: string

  processDefinitionKey:
    description: Key of the SAP DM production process to start
    type: string
    default: REG_00000000-0000-0000-0000-000000000000

  topicRoot:
    description: Root of the MQTT topic hierarchy
    type: string
    default: enterprise

resources:
  # Connection to the SAP DM public API using OAuth 2.0 client credentials
  sapDmConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref sapDmApiHost
        port: 443
        oauthClientCredentials:
          auth_url: !ref oauthTokenUrl
          client_id: !ref oauthClientId
          client_secret: !ref oauthClientSecret

  # Starts one execution of a SAP DM production process per message
  processStartEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref sapDmConnection
      write:
        path: !sub '/pe/api/v1/process/processDefinitions/start?key=${processDefinitionKey}'

  processStartMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/process-data'
          publish:
            endpoint: !ref processStartEndpoint
          rules:
            - transform:
                expression: '{ "body": $ }'

  # Logs data collection values against an SFC
  dataCollectionLogEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref sapDmConnection
      write:
        path: /datacollection/v1/log

  dataCollectionLogMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/dc-values'
          publish:
            endpoint: !ref dataCollectionLogEndpoint
          rules:
            - transform:
                expression: '{ "body": $ }'

  # HTTP route that SAP DM production processes can call back
  sapDmResponseServer:
    type: Cybus::Server::Http
    properties:
      basePath: /sap-dm

  sapDmResponseRoute:
    type: Cybus::Node::Http
    properties:
      parent: !ref sapDmResponseServer
      method: POST
      route: /response

  responseMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref sapDmResponseRoute
          publish:
            topic: !sub '${topicRoot}/sap-dm/response'
