---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# SAP S/4HANA Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for exchanging data between Connectware and
  SAP S/4HANA via its OData REST APIs (Example)

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

parameters:
  s4ApiHost:
    description: API hostname of your SAP S/4HANA system
    type: string
    default: my300000-api.s4hana.cloud.sap

  commUserName:
    description: Name of the communication user
    type: string

  commUserPassword:
    description: Password of the communication user
    type: string

  productionPlant:
    description: Production plant used to filter production orders
    type: string
    default: '1010'

  pollInterval:
    description: Polling interval for production orders in milliseconds
    type: integer
    default: 60000

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

resources:
  # Connection to the SAP S/4HANA OData APIs using basic authentication
  s4Connection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref s4ApiHost
        port: 443
        auth:
          username: !ref commUserName
          password: !ref commUserPassword
        headers:
          Accept: application/json

  # Polls production orders of one plant from SAP S/4HANA
  productionOrderEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref s4Connection
      subscribe:
        path: /sap/opu/odata/sap/API_PRODUCTION_ORDER_2_SRV/A_ProductionOrder_2
        interval: !ref pollInterval
        query:
          $filter: !sub "ProductionPlant eq '${productionPlant}'"
          $top: '50'
          $format: json

  productionOrderMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref productionOrderEndpoint
          publish:
            topic: !sub '${topicRoot}/erp/s4hana/production-orders'
          rules:
            # Optional: unwraps the OData envelope so that the plain list
            # of production orders is published
            - transform:
                expression: 'value.d.results'

  # Posts production order confirmations to SAP S/4HANA
  confirmationEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref s4Connection
      write:
        path: /sap/opu/odata/sap/API_PROD_ORDER_CONFIRMATION_2_SRV/ProdnOrdConf2
        headers:
          # Only evaluated if the CSRF token check of the OData service is
          # deactivated, see the SAP S/4HANA Integration guide for details
          X-Requested-With: 'X'

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