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

description: >
  Service commissioning file for creating ServiceNow incidents from shop
  floor alarms and polling assigned incidents through the Table API (Example)

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

parameters:
  serviceNowHost:
    description: Hostname of your ServiceNow instance
    type: string
    default: example.service-now.com

  serviceNowUsername:
    description: Username of the ServiceNow integration user
    type: string

  serviceNowPassword:
    description: Password of the ServiceNow integration user
    type: string

  callerId:
    description: sys_id of the ServiceNow user set as the caller of new incidents
    type: string
    default: '00000000000000000000000000000000'

  assignmentGroup:
    description: Name of the assignment group whose incidents are polled
    type: string
    default: Service Desk

  pollInterval:
    description: Polling interval for assigned incidents in milliseconds
    type: integer
    default: 30000

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

resources:
  # Connection to the ServiceNow instance using basic authentication
  serviceNowConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref serviceNowHost
        port: 443
        auth:
          username: !ref serviceNowUsername
          password: !ref serviceNowPassword

  # Creates one ServiceNow incident per alarm message
  incidentCreateEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref serviceNowConnection
      write:
        path: /api/now/table/incident

  # Builds the incident from the alarm payload and the topic levels
  incidentCreateMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/alarm'
          publish:
            endpoint: !ref incidentCreateEndpoint
          rules:
            - transform:
                expression: !sub >-
                  {
                    "body": {
                      "short_description": "Shop floor alarm: " & text,
                      "description": "Alarm " & code & " reported by cell "
                        & $context.vars.cell & " on line " & $context.vars.line
                        & " in area " & $context.vars.area & " at site "
                        & $context.vars.site & ".",
                      "urgency": $string(urgency),
                      "impact": $string(impact),
                      "caller_id": "${callerId}"
                    }
                  }

  # Polls the active incidents of the assignment group
  assignedIncidentsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref serviceNowConnection
      subscribe:
        path: /api/now/table/incident
        interval: !ref pollInterval
        query:
          sysparm_query: !sub 'assignment_group.name=${assignmentGroup}^active=true'
          sysparm_fields: number,short_description,state,priority,urgency,sys_updated_on,sys_id
          sysparm_display_value: 'true'
          sysparm_limit: '25'

  # Publishes the polled incidents to the MQTT broker
  assignedIncidentsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref assignedIncidentsEndpoint
          publish:
            topic: !sub '${topicRoot}/servicenow/incidents'
          rules:
            - transform:
                expression: 'value.result'
