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

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and RabbitMQ through the RabbitMQ MQTT plugin (Example)

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

parameters:
  rabbitmqHost:
    description: Hostname or IP address of the RabbitMQ node
    type: string
    default: rabbitmq.example.com

  rabbitmqUsername:
    description: RabbitMQ user for the MQTT connection
    type: string
    default: connectware

  rabbitmqPassword:
    description: Password of the RabbitMQ user
    type: string

  clientId:
    description: MQTT client ID, determines the names of the subscription queues
    type: string
    default: connectware-shopfloor

resources:
  # Connection to the MQTT listener of the RabbitMQ MQTT plugin
  rabbitMqConnection:
    type: Cybus::Connection
    properties:
      protocol: Mqtt
      connection:
        host: !ref rabbitmqHost
        port: 1883
        scheme: mqtt
        username: !ref rabbitmqUsername
        password: !ref rabbitmqPassword
        clientId: !ref clientId
        keepalive: 60

  # Forwards shop floor telemetry to RabbitMQ. The MQTT plugin publishes each
  # message to the amq.topic exchange, with the topic as the routing key
  # (slashes translated to dots).
  telemetryMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: enterprise/+site/+area/+line/+cell/telemetry
            qos: 1
          publish:
            connection: !ref rabbitMqConnection
            topic: enterprise/$site/$area/$line/$cell/telemetry
            qos: 1

  # Receives messages that AMQP backends publish to the amq.topic exchange
  # with the routing key backend.erp.orders
  ordersEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Mqtt
      connection: !ref rabbitMqConnection
      subscribe:
        topic: backend/erp/orders
        qos: 1

  ordersMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref ordersEndpoint
          publish:
            topic: enterprise/backend/erp/orders
