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

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

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

parameters:
  bootstrapServer:
    description: Bootstrap server of your Redpanda cluster
    type: string
    default: redpanda.example.com:9092

  saslUsername:
    description: Username of the SCRAM user
    type: string
    default: connectware

  saslPassword:
    description: Password of the SCRAM user
    type: string

  produceTopic:
    description: Redpanda topic that receives the shop floor data
    type: string
    default: factory.machine-data

  consumeTopic:
    description: Redpanda topic that Connectware consumes into MQTT
    type: string
    default: factory.commands

  consumerGroupId:
    description: Consumer group ID that Connectware uses when consuming
    type: string
    default: connectware

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

resources:
  # Producer connection to Redpanda using SASL/SCRAM over TLS
  # The mechanism must match the mechanism the user was created with
  # (scram-sha-256 or scram-sha-512)
  # Remove the sasl property to connect to an unsecured development
  # cluster over PLAINTEXT
  redpandaProducerConnection:
    type: Cybus::Connection
    properties:
      protocol: Kafka
      connection:
        brokers:
          - !ref bootstrapServer
        clientType: producer
        sasl:
          mechanism: scram-sha-256
          username: !ref saslUsername
          password: !ref saslPassword

  # Produces shop floor data to the Redpanda topic
  machineDataEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Kafka
      connection: !ref redpandaProducerConnection
      write:
        topic: !ref produceTopic

  machineDataMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/machine-data'
          publish:
            endpoint: !ref machineDataEndpoint
          rules:
            - transform:
                expression: '{ "value": [ { "value": $string($) } ] }'

  # Consumer connection to Redpanda
  # A Kafka connection is either a producer or a consumer, never both
  redpandaConsumerConnection:
    type: Cybus::Connection
    properties:
      protocol: Kafka
      connection:
        brokers:
          - !ref bootstrapServer
        clientType: consumer
        groupId: !ref consumerGroupId
        sasl:
          mechanism: scram-sha-256
          username: !ref saslUsername
          password: !ref saslPassword

  # Consumes the Redpanda topic into the MQTT topic hierarchy
  commandsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Kafka
      connection: !ref redpandaConsumerConnection
      subscribe:
        topic: !ref consumeTopic
        fromBeginning: false

  commandsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref commandsEndpoint
          publish:
            topic: !sub '${topicRoot}/redpanda/commands'
          rules:
            - transform:
                expression: 'value.message'
