/root> #-------------------------------------------------------------------------------
/root> # ============LICENSE_START=======================================================
/root> #  Copyright (C) 2022 Nordix Foundation.
/root> # ================================================================================
/root> # Licensed under the Apache License, Version 2.0 (the "License");
/root> # you may not use this file except in compliance with the License.
/root> # You may obtain a copy of the License at
/root> #
/root> #      http://www.apache.org/licenses/LICENSE-2.0
/root> #
/root> # Unless required by applicable law or agreed to in writing, software
/root> # distributed under the License is distributed on an "AS IS" BASIS,
/root> # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/root> # See the License for the specific language governing permissions and
/root> # limitations under the License.
/root> #
/root> # SPDX-License-Identifier: Apache-2.0
/root> # ============LICENSE_END=========================================================
/root> #-------------------------------------------------------------------------------
/root> 
/root> model create name=APEXacElementPolicy
result: SUCCESS

/root> 
/root> ##
/root> ## SCHEMAS
/root> ##
/root> 
/root> schema create name=SimpleStringType flavour=Java schema=java.lang.String
result: SUCCESS

/root> schema create name=UUIDType         flavour=Java schema=java.util.UUID
result: SUCCESS

/root> schema create name=SimpleIntType    flavour=Java schema=java.lang.Integer
result: SUCCESS

/root> 
/root> schema create name=ACEventType flavour=Json schema=LS
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "elementId": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "version": {
                    "type": "string"
                }
            },
            "required": [
                "name",
                "version"
            ]
        },
        "message": {
            "type": "string"
        },
        "messageType": {
            "type": "string"
        }
    },
    "required": [
        "elementId",
        "message",
        "messageType"
    ]
}

LE
result: SUCCESS

/root> 
/root> ##
/root> ## EVENTS
/root> ##
/root> 
/root> album create name=ACElementAlbum scope=policy writable=true schemaName=ACEventType
result: SUCCESS

/root> 
/root> # Incoming event
/root> event create name=AcElementEvent version=0.0.1 nameSpace=org.onap.policy.apex.ac.element source=Dmaap target=APEX toscaPolicyState=ENTRY
result: SUCCESS

/root> event parameter create name=AcElementEvent parName=DmaapResponseEvent schemaName=ACEventType
result: SUCCESS

/root> 
/root> # Forwarding event
/root> event create name=DmaapResponseStatusEvent nameSpace=org.onap.policy.apex.ac.element source=APEX target=Dmaap
result: SUCCESS

/root> event parameter create name=DmaapResponseStatusEvent parName=DmaapResponseStatusEvent schemaName=ACEventType
result: SUCCESS

/root> 
/root> # Log event
/root> event create name=LogEvent nameSpace=org.onap.policy.apex.ac.element source=APEX target=file
result: SUCCESS

/root> event parameter create name=LogEvent parName=final_status schemaName=SimpleStringType
result: SUCCESS

/root> event parameter create name=LogEvent parName=message schemaName=SimpleStringType
result: SUCCESS

/root> 
/root> ##
/root> ## TASKS
/root> ##
/root> 
/root> task create name=ForwardPayloadTask
result: SUCCESS

/root> task contextref create name=ForwardPayloadTask albumName=ACElementAlbum
result: SUCCESS

/root> task inputfield create name=ForwardPayloadTask fieldName=DmaapResponseEvent schemaName=ACEventType
result: SUCCESS
inputFields and outputFields are deprecated from Task definition and will be removed. Instead, inputEvent and outputEvents are automatically populated to Tasks based on State definition

/root> task logic create name=ForwardPayloadTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Nordix. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

executor.logger.info(executor.subject.id);
executor.logger.info(executor.inFields);

var msgResponse = executor.inFields.get('DmaapResponseEvent');
executor.logger.info('Task in progress with mesages: ' + msgResponse);

var elementId = msgResponse.get('elementId').get('name');

if (msgResponse.get('messageType') == 'STATUS' &&
    (elementId == 'onap.policy.clamp.ac.startertobridge'
    || elementId == 'onap.policy.clamp.ac.bridgetosink')) {

    var receiverId = '';
    if (elementId == 'onap.policy.clamp.ac.startertobridge') {
        receiverId = 'onap.policy.clamp.ac.bridge';
    } else {
        receiverId = 'onap.policy.clamp.ac.sink';
    }

    var elementIdResponse = new java.util.HashMap();
    elementIdResponse.put('name', receiverId);
    elementIdResponse.put('version', msgResponse.get('elementId').get('version'));

    var dmaapResponse = new java.util.HashMap();
    dmaapResponse.put('elementId', elementIdResponse);

    var message = msgResponse.get('message') + ' trace added from policy';
    dmaapResponse.put('message', message);
    dmaapResponse.put('messageType', 'STATUS');
    dmaapResponse.put('messageId', msgResponse.get('messageId'));
    dmaapResponse.put('timestamp', msgResponse.get('timestamp'));

    executor.logger.info('Sending forwarding Event to Ac element: ' + dmaapResponse);

    executor.outFields.put('DmaapResponseStatusEvent', dmaapResponse);
}

true;

LE
result: SUCCESS

/root> 
/root> 
/root> ##
/root> ## POLICIES
/root> ##
/root> 
/root> # Policy ReceiveEventPolicy
/root> policy create name=ReceiveEventPolicy template=Freestyle firstState=DecideForwardingState
result: SUCCESS

/root> 
/root> 
/root> # State DecideForwardingState
/root> policy state create name=ReceiveEventPolicy stateName=DecideForwardingState triggerName=AcElementEvent defaultTaskName=ForwardPayloadTask
result: SUCCESS

/root> policy state output create name=ReceiveEventPolicy stateName=DecideForwardingState outputName=CreateForwardPayload eventName=DmaapResponseStatusEvent nextState=NULL
result: SUCCESS

/root> policy state taskref create name=ReceiveEventPolicy stateName=DecideForwardingState taskName=ForwardPayloadTask outputType=DIRECT outputName=CreateForwardPayload
result: SUCCESS

/root> 
/root> 
/root> validate
result: SUCCESS
{
  "name": "APEXacElementPolicy",
  "version": "0.0.1"
}
***validation of model successful***

/root> 