---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Oracle Fusion Cloud SCM Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and Oracle Fusion Cloud SCM (Example)

metadata:
  name: Oracle Fusion Cloud SCM Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  oracleFusionHost:
    description: Hostname of your Oracle Fusion Cloud instance
    type: string
    default: example.fa.us2.oraclecloud.com

  restApiVersion:
    description: Version of the Oracle Fusion Cloud REST API
    type: string
    default: 11.13.18.05

  integrationUsername:
    description: Username of the Oracle Fusion Cloud integration user
    type: string

  integrationPassword:
    description: Password of the Oracle Fusion Cloud integration user
    type: string

  organizationCode:
    description: Code of the manufacturing organization to poll work orders for
    type: string
    default: PLANT01

  pollInterval:
    description: Polling interval for work orders in milliseconds
    type: integer
    default: 60000

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

resources:
  # Connection to the Oracle Fusion Cloud REST APIs using basic authentication
  oracleFusionConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref oracleFusionHost
        port: 443
        prefix: !sub '/fscmRestApi/resources/${restApiVersion}'
        auth:
          username: !ref integrationUsername
          password: !ref integrationPassword

  # Reports work order operation transactions to Oracle Manufacturing
  operationTransactionsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref oracleFusionConnection
      write:
        path: /operationTransactions

  operationTransactionsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/work-order-transactions'
          publish:
            endpoint: !ref operationTransactionsEndpoint
          rules:
            - transform:
                expression: '{ "body": $ }'

  # Reports material transactions (component issues and returns)
  materialTransactionsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref oracleFusionConnection
      write:
        path: /materialTransactions

  materialTransactionsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/material-transactions'
          publish:
            endpoint: !ref materialTransactionsEndpoint
          rules:
            - transform:
                expression: '{ "body": $ }'

  # Polls released work orders from Oracle Manufacturing
  workOrdersEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref oracleFusionConnection
      subscribe:
        path: /workOrders
        interval: !ref pollInterval
        query:
          q: !sub "WorkOrderStatusCode='ORA_RELEASED';OrganizationCode='${organizationCode}'"
          onlyData: 'true'
          limit: '25'

  workOrdersMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref workOrdersEndpoint
          publish:
            topic: !sub '${topicRoot}/oracle-fusion/work-orders'
