---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Azure Event Hubs Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and Azure Event Hubs through the Apache Kafka endpoint (Example)

metadata:
  name: Azure Event Hubs Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  eventHubsNamespace:
    description: Name of your Event Hubs namespace, without the domain suffix
    type: string
    default: example-namespace

  connectionString:
    description: Connection string of your Event Hubs namespace
    type: string

  telemetryEventHub:
    description: Name of the event hub that receives shop floor data
    type: string
    default: factory-telemetry

  commandsEventHub:
    description: Name of the event hub that Connectware consumes from
    type: string
    default: factory-commands

  consumerGroupId:
    description: Kafka consumer group ID used when consuming from Event Hubs
    type: string
    default: connectware

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

resources:
  # Producer connection to the Kafka endpoint of the Event Hubs namespace.
  # Configuring the sasl property makes the connector connect over TLS.
  eventHubsProducer:
    type: Cybus::Connection
    properties:
      protocol: Kafka
      connection:
        brokers:
          - !sub '${eventHubsNamespace}.servicebus.windows.net:9093'
        sasl:
          mechanism: plain
          username: '$ConnectionString'
          password: !ref connectionString

  # Writes messages to the telemetry event hub
  telemetryEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Kafka
      connection: !ref eventHubsProducer
      write:
        topic: !ref telemetryEventHub

  # Streams shop floor data from the MQTT topic hierarchy into the event hub.
  # The transform rule wraps each payload in the message format that the
  # Kafka connector expects.
  telemetryMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/telemetry'
          publish:
            endpoint: !ref telemetryEndpoint
          rules:
            - transform:
                expression: '{ "value": { "value": $string($) } }'

  # Consumer connection to the Kafka endpoint of the Event Hubs namespace.
  # A Kafka connection is either a producer or a consumer, so the consuming
  # direction needs its own connection resource.
  eventHubsConsumer:
    type: Cybus::Connection
    properties:
      protocol: Kafka
      connection:
        clientType: consumer
        groupId: !ref consumerGroupId
        brokers:
          - !sub '${eventHubsNamespace}.servicebus.windows.net:9093'
        sasl:
          mechanism: plain
          username: '$ConnectionString'
          password: !ref connectionString

  # Consumes new events from the commands event hub
  commandsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Kafka
      connection: !ref eventHubsConsumer
      subscribe:
        topic: !ref commandsEventHub
        fromBeginning: false

  # Publishes every consumed event to an MQTT topic
  commandsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref commandsEndpoint
          publish:
            topic: !sub '${topicRoot}/azure/event-hubs/commands'
