---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# AWS IoT Core Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and AWS IoT Core (Example)

metadata:
  name: AWS IoT Core Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  awsIotEndpoint:
    description: Device data endpoint (iot:Data-ATS) of your AWS account
    type: string
    default: a1b2c3d4e5f6g7-ats.iot.eu-central-1.amazonaws.com

  clientId:
    description: MQTT client ID, must match the client ARN in the AWS IoT policy
    type: string
    default: connectware-shopfloor

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

definitions:
  # The Amazon root CA certificate as PEM file content (AmazonRootCA1.pem)
  caCert: |
    -----BEGIN CERTIFICATE-----
    -----END CERTIFICATE-----
  # The device certificate as PEM file content
  clientCert: |
    -----BEGIN CERTIFICATE-----
    -----END CERTIFICATE-----
  # The device private key as PEM file content
  clientPrivateKey: |
    -----BEGIN RSA PRIVATE KEY-----
    -----END RSA PRIVATE KEY-----

resources:
  # Mutual TLS connection to the AWS IoT Core message broker
  awsIotConnection:
    type: Cybus::Connection
    properties:
      protocol: Mqtt
      connection:
        host: !ref awsIotEndpoint
        port: 8883
        scheme: mqtts
        clientId: !ref clientId
        keepalive: 60
        mutualAuthentication: true
        caCert: !ref caCert
        clientCert: !ref clientCert
        clientPrivateKey: !ref clientPrivateKey

  # Publishes shop floor data from the ISA-95 topic hierarchy to AWS IoT Core
  telemetryMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/telemetry'
            qos: 1
          publish:
            connection: !ref awsIotConnection
            topic: dt/$site/$area/$line/$cell/telemetry
            qos: 1

  # Receives cloud-to-device commands from AWS IoT Core
  commandEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Mqtt
      connection: !ref awsIotConnection
      subscribe:
        topic: !sub 'cmd/${clientId}/commands'
        qos: 1

  commandMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref commandEndpoint
          publish:
            topic: !sub '${topicRoot}/cloud/aws-iot/commands'
