/root> #-------------------------------------------------------------------------------
/root> # ============LICENSE_START=======================================================
/root> #  Copyright (C) 2016-2018 Ericsson. All rights reserved.
/root> #  Modifications Copyright (C) 2019-2020,2024 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=ONAPvCPEPolicyModel
result: SUCCESS

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

/root> schema create name=SimpleLongType    flavour=Java schema=java.lang.Long
result: SUCCESS

/root> schema create name=SimpleBooleanType flavour=Java schema=java.lang.Boolean
result: SUCCESS

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

/root> 
/root> schema create name=VirtualControlLoopEventType flavour=Java schema=org.onap.policy.controlloop.VirtualControlLoopEvent
result: SUCCESS

/root> schema create name=VirtualControlLoopNotificationType flavour=Java schema=org.onap.policy.controlloop.VirtualControlLoopNotification
result: SUCCESS

/root> 
/root> schema create name=APPCLCMRequestType flavour=Java schema=org.onap.policy.appclcm.AppcLcmMessageWrapper
result: SUCCESS

/root> schema create name=APPCLCMResponseType flavour=Java schema=org.onap.policy.appclcm.AppcLcmMessageWrapper
result: SUCCESS

/root> 
/root> schema create name=GuardDecisionAttributesType flavour=Avro schema=LS
{
    "type": "record",
    "name": "GuardDecisionAttributes_Type",
    "namespace": "org.onap.policy.apex.onap.vcpe",
    "fields": [
        {
            "name": "actor",
            "type": "string"
        },
        {
            "name": "recipe",
            "type": "string"
        },
        {
            "name": "target",
            "type": "string"
        },
        {
            "name": "clname",
            "type": "string"
        }
    ]
}

LE
result: SUCCESS

/root> 
/root> schema create name=VCPEClosedLoopStatusType flavour=Avro schema=LS
{
    "type": "record",
    "name": "VCPEClosedLoopStatus",
    "fields": [
        {
            "name": "AAI",
            "type": {
                "type": "record",
                "name": "VCPE_AAI_Type",
                "namespace": "org.onap.policy.apex.onap.vcpe",
                "fields": [
                    {
                        "name": "genericVnfResourceVersion",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfVnfName",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfProvStatus",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfIsClosedLoopDisabled",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfOrchestrationStatus",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfVnfType",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfInMaint",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfServiceId",
                        "type": "string"
                    },
                    {
                        "name": "genericVnfVnfId",
                        "type": "string"
                    },
                    {
                        "name": "vserverIsClosedLoopDisabled",
                        "type": "string"
                    },
                    {
                        "name": "vserverName",
                        "type": "string"
                    },
                    {
                        "name": "vserverProvStatus",
                        "type": "string"
                    }
                ]
            }
        },
        {
            "name": "closedLoopAlarmStart",
            "type": "long"
        },
        {
            "name": "closedLoopAlarmEnd",
            "type": "long"
        },
        {
            "name": "closedLoopControlName",
            "type": "string"
        },
        {
            "name": "version",
            "type": "string"
        },
        {
            "name": "requestID",
            "type": "string"
        },
        {
            "name": "closedLoopEventClient",
            "type": "string"
        },
        {
            "name": "closedLoopEventStatus",
            "type": "string"
        },
        {
            "name": "target_type",
            "type": "string"
        },
        {
            "name": "target",
            "type": "string"
        },
        {
            "name": "from",
            "type": "string"
        },
        {
            "name": "policyScope",
            "type": "string"
        },
        {
            "name": "policyName",
            "type": "string"
        },
        {
            "name": "policyVersion",
            "type": "string"
        },
        {
            "name": "notification",
            "type": "string"
        },
        {
            "name": "notificationTime",
            "type": "long"
        },
        {
            "name": "message",
            "type": "string"
        }
    ]
}
LE
result: SUCCESS

/root> 
/root> album create name=VCPEClosedLoopStatusAlbum scope=policy writable=true schemaName=VCPEClosedLoopStatusType
result: SUCCESS

/root> 
/root> album create name=ControlLoopExecutionIDAlbum scope=policy writable=true schemaName=UUIDType
result: SUCCESS

/root> 
/root> album create name=RequestIDVNFIDAlbum scope=policy writable=true schemaName=UUIDType
result: SUCCESS

/root> 
/root> event create name=ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e version=1.0.2 nameSpace=org.onap.policy.apex.onap.vcpe source=DCAE target=APEX
result: SUCCESS

/root> event parameter create name=ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e parName=VirtualControlLoopEvent schemaName=VirtualControlLoopEventType
result: SUCCESS

/root> 
/root> event create name=VCPEStateUpdatedEvent nameSpace=org.onap.policy.apex.onap.vcpe source=APEX target=APEX
result: SUCCESS

/root> event parameter create name=VCPEStateUpdatedEvent parName=requestID schemaName=UUIDType
result: SUCCESS

/root> event parameter create name=VCPEStateUpdatedEvent parName=vnfID     schemaName=UUIDType
result: SUCCESS

/root> event parameter create name=VCPEStateUpdatedEvent parName=vnfName   schemaName=SimpleStringType optional=true
result: SUCCESS

/root> 
/root> event create name=GuardRequestEvent nameSpace=org.onap.policy.apex.onap.vcpe source=APEX target=APEX
result: SUCCESS

/root> 
/root> event parameter create name=GuardRequestEvent parName=decisionAttributes schemaName=GuardDecisionAttributesType
result: SUCCESS

/root> event parameter create name=GuardRequestEvent parName=onapName           schemaName=SimpleStringType
result: SUCCESS

/root> 
/root> event create name=GuardResponseEvent nameSpace=org.onap.policy.apex.onap.vcpe source=APEX target=APEX
result: SUCCESS

/root> 
/root> event parameter create name=GuardResponseEvent parName=decision  schemaName=SimpleStringType
result: SUCCESS

/root> event parameter create name=GuardResponseEvent parName=details   schemaName=SimpleStringType
result: SUCCESS

/root> 
/root> event create name=APPCRestartVNFRequestEvent version="2.0.0" nameSpace=org.onap.policy.apex.onap.vcpe source=APEX target=APPC
result: SUCCESS

/root> event parameter create name=APPCRestartVNFRequestEvent parName=APPCLCMRequestEvent schemaName=APPCLCMRequestType
result: SUCCESS

/root> 
/root> event create name=APPCRestartVNFResponseEvent version="2.0.0" nameSpace=org.onap.policy.apex.onap.vcpe source=APEX target=APPC
result: SUCCESS

/root> event parameter create name=APPCRestartVNFResponseEvent parName=APPCLCMResponseEvent schemaName=APPCLCMResponseType
result: SUCCESS

/root> 
/root> event create name=ControlLoopLogEvent nameSpace=org.onap.policy.apex.onap.vcpe source=APEX target=APEX
result: SUCCESS

/root> event parameter create name=ControlLoopLogEvent parName=VirtualControlLoopNotification schemaName=VirtualControlLoopNotificationType
result: SUCCESS

/root> 
/root> task create name=GetVCPEStateTask
result: SUCCESS

/root> 
/root> task inputfield create name=GetVCPEStateTask fieldName=VirtualControlLoopEvent schemaName=VirtualControlLoopEventType
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> 
/root> task outputfield create name=GetVCPEStateTask fieldName=requestID schemaName=UUIDType
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 outputfield create name=GetVCPEStateTask fieldName=vnfID     schemaName=UUIDType
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 outputfield create name=GetVCPEStateTask fieldName=vnfName   schemaName=SimpleStringType optional=true
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> 
/root> task contextref create name=GetVCPEStateTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> task contextref create name=GetVCPEStateTask albumName=RequestIDVNFIDAlbum
result: SUCCESS

/root> 
/root> task logic create name=GetVCPEStateTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * Note: The incoming closedloop message can be ONSET with both VNF-name and VNF-ID
 *       or ABATED with only VNF-name. So need to handle differently. For ABATED case,
 *       since we still keep the RequireIDVNFID context album, we can get it from there.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

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

var clEventType = org.onap.policy.controlloop.VirtualControlLoopEvent;
var longType = java.lang.Long;
var uuidType = java.util.UUID;

var clEvent = executor.inFields.get("VirtualControlLoopEvent");

executor.logger.info(clEvent.toString());
executor.logger.info(clEvent.getClosedLoopControlName());

var requestID = clEvent.getRequestId();
executor.logger.info("requestID = " + requestID);
var vnfID = null;
var vcpeClosedLoopStatus = null;

if (clEvent.getAai().get("generic-vnf.vnf-id") != null) {
   vnfID = uuidType.fromString(clEvent.getAai().get("generic-vnf.vnf-id"));
   executor.logger.info("vnfID = " + vnfID);
   vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(vnfID);

   if (vcpeClosedLoopStatus == null) {
      executor.logger.info("Creating context information for new vCPE VNF \"" + vnfID.toString() + "\"");

      vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").getSchemaHelper().createNewInstance();

      vcpeClosedLoopStatus.put("closedLoopControlName", clEvent.getClosedLoopControlName());
      vcpeClosedLoopStatus.put("closedLoopAlarmStart",  clEvent.getClosedLoopAlarmStart().toEpochMilli());
      vcpeClosedLoopStatus.put("closedLoopEventClient", clEvent.getClosedLoopEventClient());
      vcpeClosedLoopStatus.put("closedLoopEventStatus", clEvent.getClosedLoopEventStatus().toString());
      vcpeClosedLoopStatus.put("version",               clEvent.getVersion());
      vcpeClosedLoopStatus.put("requestID",             clEvent.getRequestId().toString());
      vcpeClosedLoopStatus.put("target_type",           clEvent.getTargetType().toString());
      vcpeClosedLoopStatus.put("target",                clEvent.getTarget());
      vcpeClosedLoopStatus.put("from",                  clEvent.getFrom());
      vcpeClosedLoopStatus.put("policyScope",           "vCPE");
      vcpeClosedLoopStatus.put("policyName",            "ONAPvCPEPolicyModel");
      vcpeClosedLoopStatus.put("policyVersion",         "0.0.1");
      vcpeClosedLoopStatus.put("notification",          "ACTIVE");
      vcpeClosedLoopStatus.put("notificationTime",      java.lang.System.currentTimeMillis());
      vcpeClosedLoopStatus.put("message",               "");

      var aaiInfo = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").getSchemaHelper().createNewSubInstance("VCPE_AAI_Type");

      aaiInfo.put("genericVnfResourceVersion",      clEvent.getAai().get("generic-vnf.resource-version"));
      aaiInfo.put("genericVnfVnfName",              clEvent.getAai().get("generic-vnf.vnf-name"));
      aaiInfo.put("genericVnfProvStatus",           clEvent.getAai().get("generic-vnf.prov-status"));
      aaiInfo.put("genericVnfIsClosedLoopDisabled", clEvent.getAai().get("generic-vnf.is-closed-loop-disabled"));
      aaiInfo.put("genericVnfOrchestrationStatus",  clEvent.getAai().get("generic-vnf.orchestration-status"));
      aaiInfo.put("genericVnfVnfType",              clEvent.getAai().get("generic-vnf.vnf-type"));
      aaiInfo.put("genericVnfInMaint",              clEvent.getAai().get("generic-vnf.in-maint"));
      aaiInfo.put("genericVnfServiceId",            clEvent.getAai().get("generic-vnf.service-id"));
      aaiInfo.put("genericVnfVnfId",                clEvent.getAai().get("generic-vnf.vnf-id"));
      aaiInfo.put("vserverIsClosedLoopDisabled",    clEvent.getAai().get("vserver.is-closed-loop-disabled"));
      aaiInfo.put("vserverProvStatus",              clEvent.getAai().get("vserver.prov-status"));
      aaiInfo.put("vserverName",                    clEvent.getAai().get("vserver.vserver-name"));

      vcpeClosedLoopStatus.put("AAI", aaiInfo);

      if (clEvent.getClosedLoopAlarmEnd() != null) {
         vcpeClosedLoopStatus.put("closedLoopAlarmEnd", clEvent.getClosedLoopAlarmEnd().toEpochMilli());
      } else {
         vcpeClosedLoopStatus.put("closedLoopAlarmEnd", java.lang.Long.valueOf(0));
      }

      executor.getContextAlbum("VCPEClosedLoopStatusAlbum").put(vnfID.toString(), vcpeClosedLoopStatus);

      executor.logger.info("Created context information for new vCPE VNF \"" + vnfID.toString() + "\"");
   }

   executor.outFields.put("requestID", requestID);
   executor.outFields.put("vnfID", vnfID);

   executor.logger.info(executor.outFields);
}
else {
    executor.logger.info("No vnf-id in VirtualControlLoopEvent, status:" + clEvent.getClosedLoopEventStatus().toString());
    var vnfName = clEvent.getAai().get("generic-vnf.vnf-name");
    executor.logger.info("No vnf-id in VirtualControlLoopEvent for " + vnfName);

    vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(vnfName.toString());

    if (vcpeClosedLoopStatus == null) {
        executor.logger.info("Creating context information for new vCPE VNF \"" + vnfName.toString() + "\"");

        vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").getSchemaHelper().createNewInstance();

        vcpeClosedLoopStatus.put("closedLoopControlName", clEvent.getClosedLoopControlName());
        vcpeClosedLoopStatus.put("closedLoopAlarmStart",  clEvent.getClosedLoopAlarmStart().toEpochMilli());
        vcpeClosedLoopStatus.put("closedLoopEventClient", clEvent.getClosedLoopEventClient());
        vcpeClosedLoopStatus.put("closedLoopEventStatus", clEvent.getClosedLoopEventStatus().toString());
        vcpeClosedLoopStatus.put("version",               clEvent.getVersion());
        vcpeClosedLoopStatus.put("requestID",             clEvent.getRequestId().toString());
        vcpeClosedLoopStatus.put("target_type",           clEvent.getTargetType().toString());
        vcpeClosedLoopStatus.put("target",                clEvent.getTarget());
        vcpeClosedLoopStatus.put("from",                  clEvent.getFrom());
        vcpeClosedLoopStatus.put("policyScope",           "vCPE");
        vcpeClosedLoopStatus.put("policyName",            "ONAPvCPEPolicyModel");
        vcpeClosedLoopStatus.put("policyVersion",         "0.0.1");
        vcpeClosedLoopStatus.put("notification",          "ACTIVE");
        vcpeClosedLoopStatus.put("notificationTime",      java.lang.System.currentTimeMillis());
        vcpeClosedLoopStatus.put("message",               "");

        var aaiInfo = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").getSchemaHelper().createNewSubInstance("VCPE_AAI_Type");

        aaiInfo.put("genericVnfVnfName", clEvent.getAai().get("generic-vnf.vnf-name"));
        vcpeClosedLoopStatus.put("AAI", aaiInfo);

        if (clEvent.getClosedLoopAlarmEnd() != null) {
            vcpeClosedLoopStatus.put("closedLoopAlarmEnd", clEvent.getClosedLoopAlarmEnd().toEpochMilli());
        } else {
            vcpeClosedLoopStatus.put("closedLoopAlarmEnd", java.lang.Long.valueOf(0));
        }

        executor.getContextAlbum("VCPEClosedLoopStatusAlbum").put(vnfName.toString(), vcpeClosedLoopStatus);

        executor.logger.info("Created context information for new vCPE VNF \"" + vnfName.toString() + "\"");
    }
    executor.outFields.put("requestID", requestID);
    executor.outFields.put("vnfName", vnfName);
    executor.logger.info(executor.outFields);
}

true;

LE
result: SUCCESS

/root> 
/root> task create name=AbatedTask
result: SUCCESS

/root> 
/root> task inputfield create name=AbatedTask fieldName=requestID schemaName=UUIDType
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 inputfield create name=AbatedTask fieldName=vnfID     schemaName=UUIDType
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 inputfield create name=AbatedTask fieldName=vnfName   schemaName=SimpleStringType optional=true
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> 
/root> task outputfield create name=AbatedTask fieldName=requestID schemaName=UUIDType
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 outputfield create name=AbatedTask fieldName=vnfID     schemaName=UUIDType
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 outputfield create name=AbatedTask fieldName=vnfName   schemaName=SimpleStringType optional=true
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> 
/root> task contextref create name=AbatedTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> 
/root> task logic create name=AbatedTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 vcpeClosedLoopStatus = null;
if( executor.inFields.get("vnfID") == null) {
   executor.logger.info("AbatedTask: vnfID is null");
   var vnfName = executor.inFields.get("vnfName");
   vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfName"));
} else {
   vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfID").toString());
}

vcpeClosedLoopStatus.put("notification",     "FINAL_SUCCESS");
vcpeClosedLoopStatus.put("notificationTime", java.lang.System.currentTimeMillis());
vcpeClosedLoopStatus.put("message",          "situation has been abated");

executor.logger.info(executor.outFields);

true;

LE
result: SUCCESS

/root> 
/root> task create name=GuardRequestTask
result: SUCCESS

/root> 
/root> task inputfield create name=GuardRequestTask fieldName=requestID schemaName=UUIDType
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 inputfield create name=GuardRequestTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task outputfield create name=GuardRequestTask fieldName=decisionAttributes schemaName=GuardDecisionAttributesType
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 outputfield create name=GuardRequestTask fieldName=onapName           schemaName=SimpleStringType
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> 
/root> task contextref create name=GuardRequestTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> task contextref create name=GuardRequestTask albumName=ControlLoopExecutionIDAlbum
result: SUCCESS

/root> 
/root> task logic create name=GuardRequestTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfID").toString());

var guardDecisionAttributes = executor.subject.getOutFieldSchemaHelper("decisionAttributes").createNewInstance();

guardDecisionAttributes.put("actor", "APPC");
guardDecisionAttributes.put("recipe", "Restart");
guardDecisionAttributes.put("target", executor.inFields.get("vnfID").toString());
guardDecisionAttributes.put("clname", "APEXvCPEImplementation");

executor.logger.info(guardDecisionAttributes);

executor.outFields.put("decisionAttributes", guardDecisionAttributes);
executor.outFields.put("onapName", "PDPD");

executor.getContextAlbum("ControlLoopExecutionIDAlbum").put(executor.executionId.toString(),
        executor.inFields.get("vnfID"));

executor.logger.info(executor.outFields);

true;

LE
result: SUCCESS

/root> 
/root> task create name=GuardResponseTask
result: SUCCESS

/root> 
/root> task inputfield create name=GuardResponseTask fieldName=decision  schemaName=SimpleStringType
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 inputfield create name=GuardResponseTask fieldName=details   schemaName=SimpleStringType
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> 
/root> task outputfield create name=GuardResponseTask fieldName=requestID schemaName=UUIDType
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 outputfield create name=GuardResponseTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task contextref create name=GuardResponseTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> task contextref create name=GuardResponseTask albumName=ControlLoopExecutionIDAlbum
result: SUCCESS

/root> 
/root> task logic create name=GuardResponseTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 vnfID = executor.getContextAlbum("ControlLoopExecutionIDAlbum").remove(executor.executionId.toString());

executor.logger.info("Continuing execution with VNF ID: " + vnfID);

var vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(vnfID.toString());
executor.logger.info(vcpeClosedLoopStatus);

var guardResult = executor.inFields.get("decision");
var returnValue = true;

if (guardResult == "PERMIT") {
    vcpeClosedLoopStatus.put("notification", "OPERATION: GUARD_PERMIT");
} else if (guardResult == "DENY") {
    vcpeClosedLoopStatus.put("notification", "OPERATION: GUARD_DENY");
} else {
    executor.message = "guard result must be either \"PERMIT\" or \"DENY\"";
    returnValue = false;
}

var uuidType = java.util.UUID;
var requestID = uuidType.fromString(vcpeClosedLoopStatus.get("requestID"));

executor.outFields.put("requestID", requestID);
executor.outFields.put("vnfID", vnfID);

executor.logger.info(executor.outFields);


returnValue;

LE
result: SUCCESS

/root> 
/root> task create name=DeniedTask
result: SUCCESS

/root> 
/root> task inputfield create name=DeniedTask fieldName=requestID schemaName=UUIDType
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 inputfield create name=DeniedTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task outputfield create name=DeniedTask fieldName=requestID schemaName=UUIDType
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 outputfield create name=DeniedTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task contextref create name=DeniedTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> 
/root> task logic create name=DeniedTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfID").toString());

vcpeClosedLoopStatus.put("notification",     "REJECTED");
vcpeClosedLoopStatus.put("notificationTime", java.lang.System.currentTimeMillis());

executor.logger.info(executor.outFields);

true;

LE
result: SUCCESS

/root> 
/root> task create name=APPCRestartVNFRequestTask
result: SUCCESS

/root> 
/root> task inputfield create name=APPCRestartVNFRequestTask fieldName=requestID schemaName=UUIDType
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 inputfield create name=APPCRestartVNFRequestTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task outputfield create name=APPCRestartVNFRequestTask fieldName=APPCLCMRequestEvent schemaName=APPCLCMRequestType
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> 
/root> task contextref create name=APPCRestartVNFRequestTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> task contextref create name=APPCRestartVNFRequestTask albumName=RequestIDVNFIDAlbum
result: SUCCESS

/root> 
/root> task logic create name=APPCRestartVNFRequestTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020,2024 Nordix Foundation.
 * ================================================================================
 * 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 appcRequest = new org.onap.policy.appclcm.AppcLcmMessageWrapper;
appcRequest.setBody(new org.onap.policy.appclcm.AppcLcmBody);
appcRequest.getBody().setInput(new org.onap.policy.appclcm.AppcLcmInput);
appcRequest.getBody().getInput().setCommonHeader(
        new org.onap.policy.appclcm.AppcLcmCommonHeader);

appcRequest.setVersion("2.0.0");
appcRequest.setRpcName("restart");
appcRequest.setCorrelationId(executor.inFields.get("requestID"));
appcRequest.setType("request");

var vcpeClosedLoopStatus = executor
        .getContextAlbum("VCPEClosedLoopStatusAlbum").get(
                executor.inFields.get("vnfID").toString());

appcRequest.getBody().getInput().getCommonHeader().setTimeStamp(java.time.Instant.now());
appcRequest.getBody().getInput().getCommonHeader().setApiVer("2.00");
appcRequest.getBody().getInput().getCommonHeader().setOriginatorId(
        executor.inFields.get("requestID").toString());
appcRequest.getBody().getInput().getCommonHeader().setRequestId(
        executor.inFields.get("requestID"));
appcRequest.getBody().getInput().getCommonHeader().setSubRequestId("1");
appcRequest.getBody().getInput().getCommonHeader().getFlags().put("ttl", "10000");
appcRequest.getBody().getInput().getCommonHeader().getFlags().put("force", "TRUE");
appcRequest.getBody().getInput().getCommonHeader().getFlags().put("mode", "EXCLUSIVE");

appcRequest.getBody().getInput().setAction("Restart");
appcRequest.getBody().getInput().setActionIdentifiers(new java.util.HashMap());
appcRequest.getBody().getInput().getActionIdentifiers().put("vnf-id",
        executor.inFields.get("vnfID").toString());

executor.getContextAlbum("RequestIDVNFIDAlbum").put(
        executor.inFields.get("requestID").toString(),
        executor.inFields.get("vnfID"));

vcpeClosedLoopStatus.put("notification", "OPERATION");
vcpeClosedLoopStatus.put("notificationTime", java.lang.System
        .currentTimeMillis());

executor.outFields.put("APPCLCMRequestEvent", appcRequest);

executor.logger.info(executor.outFields);

true;

LE
result: SUCCESS

/root> 
/root> task create name=APPCRestartVNFResponseTask
result: SUCCESS

/root> 
/root> task inputfield create name=APPCRestartVNFResponseTask fieldName=APPCLCMResponseEvent schemaName=APPCLCMResponseType
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> 
/root> task outputfield create name=APPCRestartVNFResponseTask fieldName=requestID schemaName=UUIDType
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 outputfield create name=APPCRestartVNFResponseTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task contextref create name=APPCRestartVNFResponseTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> task contextref create name=APPCRestartVNFResponseTask albumName=RequestIDVNFIDAlbum
result: SUCCESS

/root> 
/root> task logic create name=APPCRestartVNFResponseTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * APPC LCM Response code: 100 ACCEPTED
 *                         200 ERROR UNEXPECTED ERROR means failure
 *                         312 REJECTED DUPLICATE REQUEST
 *                         400 SUCCESS
 *
 * Note: Sometimes the corelationId has a -1 at the tail, need to get rid of it when present.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

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

var appcResponse = executor.inFields.get("APPCLCMResponseEvent");

var requestIDString = appcResponse.getCorrelationId().substr(0, 36);
executor.logger.info("requestIDString = " + requestIDString);
var vnfID = executor.getContextAlbum("RequestIDVNFIDAlbum")
        .get(requestIDString);
executor.logger.info("Size of RequestIDVNFIDAlbum = "
        + executor.getContextAlbum("RequestIDVNFIDAlbum").size());
executor.logger.info("vnfID = " + vnfID);

var returnValue = true;

if (vnfID != null) {
    var vcpeClosedLoopStatus = executor.getContextAlbum(
            "VCPEClosedLoopStatusAlbum").get(vnfID.toString());
    var requestId = java.util.UUID.fromString(vcpeClosedLoopStatus
            .get("requestID"));

    vcpeClosedLoopStatus.put("notificationTime", java.lang.System
            .currentTimeMillis());

    executor.logger.info("Got from APPC code: "
            + org.onap.policy.appclcm.AppcLcmResponseCode
                    .toResponseValue(appcResponse.getBody().getOutput()
                            .getStatus().getCode()));

    if (org.onap.policy.appclcm.AppcLcmResponseCode
            .toResponseValue(appcResponse.getBody().getOutput().getStatus()
                    .getCode()) == org.onap.policy.appclcm.AppcLcmResponseCode.SUCCESS) {
        vcpeClosedLoopStatus.put("notification", "OPERATION_SUCCESS");
        vcpeClosedLoopStatus.put("message", "vCPE restarted");
        executor.getContextAlbum("RequestIDVNFIDAlbum").remove(requestIDString);
    } else if (org.onap.policy.appclcm.AppcLcmResponseCode
            .toResponseValue(appcResponse.getBody().getOutput().getStatus()
                    .getCode()) == "ACCEPTED"
            || org.onap.policy.appclcm.AppcLcmResponseCode
                    .toResponseValue(appcResponse.getBody().getOutput()
                            .getStatus().getCode()) == "REJECT") {
        executor.logger
                .info("Got ACCEPTED 100 or REJECT 312, keep the context, wait for next response. Code is: "
                        + org.onap.policy.appclcm.AppcLcmResponseCode
                                .toResponseValue(appcResponse.getBody()
                                        .getOutput().getStatus().getCode()));
    } else {
        executor.getContextAlbum("RequestIDVNFIDAlbum").remove(requestIDString);
        vcpeClosedLoopStatus.put("notification", "OPERATION_FAILURE");
        vcpeClosedLoopStatus.put("message", "vCPE restart failed");
    }

    executor.outFields.put("requestID", requestId);
    executor.outFields.put("vnfID", vnfID);
} else {
    executor.message = "VNF ID not found in context album for request ID "
            + requestIDString;
    returnValue = false
}

executor.logger.info(executor.outFields);

returnValue;

LE
result: SUCCESS

/root> 
/root> task create name=ControlLoopLogTask
result: SUCCESS

/root> 
/root> task inputfield create name=ControlLoopLogTask fieldName=requestID schemaName=UUIDType
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 inputfield create name=ControlLoopLogTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task outputfield create name=ControlLoopLogTask fieldName=VirtualControlLoopNotification schemaName=VirtualControlLoopNotificationType
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> 
/root> task contextref create name=ControlLoopLogTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> 
/root> task logic create name=ControlLoopLogTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 vnfID = executor.inFields.get("vnfID");
if(vnfID == null) {
   vnfID = executor.inFields.get("vnfName");
}
executor.logger.info("vnfID=" + vnfID);

var vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(vnfID.toString());

executor.logger.info("Logging context information for VNF \"" + vnfID + "\"");

var clNotification = new org.onap.policy.controlloop.VirtualControlLoopNotification();

clNotification.setClosedLoopControlName(vcpeClosedLoopStatus.get("closedLoopControlName"));
clNotification.setClosedLoopAlarmStart(java.time.Instant.ofEpochMilli(vcpeClosedLoopStatus.get("closedLoopAlarmStart")));
clNotification.setClosedLoopAlarmEnd(java.time.Instant.ofEpochMilli(vcpeClosedLoopStatus.get("closedLoopAlarmEnd")));
clNotification.setClosedLoopEventClient(vcpeClosedLoopStatus.get("closedLoopEventClient"));
clNotification.setVersion(vcpeClosedLoopStatus.get("version"));
clNotification.setRequestId(java.util.UUID.fromString(vcpeClosedLoopStatus.get("requestID")));
clNotification.setTargetType(vcpeClosedLoopStatus.get("target_type"));
clNotification.setTarget(vcpeClosedLoopStatus.get("target"));
clNotification.setFrom(vcpeClosedLoopStatus.get("from"));
clNotification.setPolicyScope(vcpeClosedLoopStatus.get("policyScope"));
clNotification.setPolicyName(vcpeClosedLoopStatus.get("policyName"));
clNotification.setPolicyVersion(vcpeClosedLoopStatus.get("policyVersion"));
clNotification.setNotification(org.onap.policy.controlloop.ControlLoopNotificationType.toType(vcpeClosedLoopStatus.get("notification")));
clNotification.setMessage(vcpeClosedLoopStatus.get("message"));

var notificationInstant = java.time.Instant.ofEpochSecond(vcpeClosedLoopStatus.get("notificationTime"));
var notificationTime = java.time.ZonedDateTime.ofInstant(notificationInstant, java.time.ZoneOffset.UTC);
clNotification.setNotificationTime(notificationTime);

var aaiInfo = vcpeClosedLoopStatus.get("AAI");

clNotification.getAai().put("generic-vnf.resource-version",        aaiInfo.get("genericVnfResourceVersion"));
clNotification.getAai().put("generic-vnf.vnf-name",                aaiInfo.get("genericVnfVnfName"));
clNotification.getAai().put("generic-vnf.prov-status",             aaiInfo.get("genericVnfProvStatus"));
clNotification.getAai().put("generic-vnf.is-closed-loop-disabled", aaiInfo.get("genericVnfIsClosedLoopDisabled"));
clNotification.getAai().put("generic-vnf.orchestration-status",    aaiInfo.get("genericVnfOrchestrationStatus"));
clNotification.getAai().put("generic-vnf.vnf-type",                aaiInfo.get("genericVnfVnfType"));
clNotification.getAai().put("generic-vnf.in-maint",                aaiInfo.get("genericVnfInMaint"));
clNotification.getAai().put("generic-vnf.service-id",              aaiInfo.get("genericVnfServiceId"));

if(vnfID != null) {
   clNotification.getAai().put("generic-vnf.vnf-id",                  aaiInfo.get("genericVnfVnfId"));
}
executor.outFields.put("VirtualControlLoopNotification", clNotification);

executor.logger.info(executor.outFields);

true;

LE
result: SUCCESS

/root> 
/root> task create name=AAILookupTask
result: SUCCESS

/root> 
/root> task inputfield create name=AAILookupTask fieldName=requestID schemaName=UUIDType
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 inputfield create name=AAILookupTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task outputfield create name=AAILookupTask fieldName=requestID schemaName=UUIDType
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 outputfield create name=AAILookupTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task contextref create name=AAILookupTask albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> 
/root> task logic create name=AAILookupTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfID").toString());

executor.logger.info("Executing A&AI Lookup");
executor.logger.info(vcpeClosedLoopStatus);

var aaiInfo = vcpeClosedLoopStatus.get("AAI");
var returnValue = true;

if (aaiInfo.get("vserverName") == null) {
    executor.message = "the field vserver.vserver-name must exist in the onset control loop event";
    executor.logger.warn(executor.message);
    returnValue = false;
}
else if (aaiInfo.get("genericVnfVnfId") == null && aaiInfo.get("genericVnfVnfName") == null) {
    executor.message = "either the field generic-vnf.vnf-id or generic-vnf.vnf-name must exist"
        + " in the onset control loop event";
    executor.logger.warn(executor.message);
    returnValue = false;
}
else {
    var restManager = new org.onap.policy.rest.RestManager;
    var aaiManager = new org.onap.policy.aai.AaiManager(restManager);

    // We need to instantiate the type in order to trigger the static JAXB handling
    // in the AaiCqResponse class
    var aaiCqResponseType = org.onap.policy.aai.AaiCqResponse;

    var aaiResponse = aaiManager.getCustomQueryResponse(
            "http://localhost:54321/OnapVCpeSim/sim",
            "aai.username",
            "aai.password",
            executor.inFields.get("requestID"),
            vcpeClosedLoopStatus.get("AAI").get("vserverName")
    );

    var genericVnf;

    if (aaiInfo.get("genericVnfVnfId") != null) {
        genericVnf = aaiResponse.getGenericVnfByModelInvariantId(aaiInfo.get("genericVnfVnfId"));
    }
    else {
        genericVnf = aaiResponse.getGenericVnfByVnfName(aaiInfo.get("genericVnfVnfId"));
    }

    aaiInfo.put("genericVnfResourceVersion",      genericVnf.getResourceVersion());
    aaiInfo.put("genericVnfVnfName",              genericVnf.getVnfName());
    aaiInfo.put("genericVnfProvStatus",           genericVnf.getProvStatus());
    aaiInfo.put("genericVnfIsClosedLoopDisabled", genericVnf.isIsClosedLoopDisabled().toString());
    aaiInfo.put("genericVnfVnfType",              genericVnf.getVnfType());
    aaiInfo.put("genericVnfInMaint",              genericVnf.isInMaint().toString());
    aaiInfo.put("genericVnfServiceId",            genericVnf.getServiceId());
    aaiInfo.put("genericVnfVnfId",                genericVnf.getVnfId());
    aaiInfo.put("genericVnfOrchestrationStatus",
            genericVnf.getVfModules().getVfModule().get(0).getOrchestrationStatus());

    executor.outFields.put("requestID", executor.inFields.get("requestID"));
    executor.outFields.put("vnfID", executor.inFields.get("vnfID"));

    executor.logger.info(executor.outFields);
}

returnValue;

LE
result: SUCCESS

/root> 
/root> task create name=NoAAILookupTask
result: SUCCESS

/root> 
/root> task inputfield create name=NoAAILookupTask fieldName=requestID schemaName=UUIDType
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 inputfield create name=NoAAILookupTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task outputfield create name=NoAAILookupTask fieldName=requestID schemaName=UUIDType
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 outputfield create name=NoAAILookupTask fieldName=vnfID     schemaName=UUIDType
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> 
/root> task logic create name=NoAAILookupTask logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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);

executor.logger.info(executor.outFields);

true;

LE
result: SUCCESS

/root> 
/root> #
/root> # ReceiveEventPolicy
/root> #
/root> policy create name=ReceiveEventPolicy template=Freestyle firstState=GetVCPEState
result: SUCCESS

/root> 
/root> policy state create name=ReceiveEventPolicy stateName=AbatedState triggerName=VCPEStateUpdatedEvent defaultTaskName=ControlLoopLogTask
result: SUCCESS

/root> policy state output create name=ReceiveEventPolicy stateName=AbatedState outputName=AbatedOutput eventName=ControlLoopLogEvent
result: SUCCESS

/root> policy state taskref create name=ReceiveEventPolicy stateName=AbatedState taskName=ControlLoopLogTask outputType=DIRECT outputName=AbatedOutput
result: SUCCESS

/root> 
/root> policy state create name=ReceiveEventPolicy stateName=OnsetOrAbatedState triggerName=VCPEStateUpdatedEvent defaultTaskName=AbatedTask
result: SUCCESS

/root> policy state output create name=ReceiveEventPolicy stateName=OnsetOrAbatedState outputName=OnsetOutput eventName=GuardRequestEvent
result: SUCCESS

/root> policy state taskref create name=ReceiveEventPolicy stateName=OnsetOrAbatedState taskName=GuardRequestTask outputType=DIRECT outputName=OnsetOutput
result: SUCCESS

/root> policy state output create name=ReceiveEventPolicy stateName=OnsetOrAbatedState outputName=AbatedOutput eventName=VCPEStateUpdatedEvent nextState=AbatedState
result: SUCCESS

/root> policy state taskref create name=ReceiveEventPolicy stateName=OnsetOrAbatedState taskName=AbatedTask outputType=DIRECT outputName=AbatedOutput
result: SUCCESS

/root> 
/root> policy state contextref create name=ReceiveEventPolicy stateName=OnsetOrAbatedState albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> 
/root> policy state selecttasklogic create name=ReceiveEventPolicy stateName=OnsetOrAbatedState logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 returnValue = true;
var status = null;

if( executor.inFields.get("vnfID") == null) {
   executor.logger.info("OnsetOrAbatedStateTSL: vnfID is null");
   var vnfName = executor.inFields.get("vnfName");
   var vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfName"));
   status = vcpeClosedLoopStatus.get("closedLoopEventStatus").toString();
} else {
   var vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfID").toString());
   status = vcpeClosedLoopStatus.get("closedLoopEventStatus").toString();
}

if (status == "ONSET") {
    executor.subject.getTaskKey("GuardRequestTask").copyTo(executor.selectedTask);
} else if (status == "ABATED") {
    executor.subject.getTaskKey("AbatedTask").copyTo(executor.selectedTask);
    onsetFlag = executor.isFalse;
} else {
    executor.message = "closedLoopEventStatus is \"" + status + "\", it must be either \"ONSET\" or \"ABATED\"";
    returnValue = false;
}

executor.logger.info("ReceiveEventPolicyOnsetOrAbatedStateTSL State Selected Task:" + executor.selectedTask);

returnValue;

LE
result: SUCCESS

/root> 
/root> policy state create name=ReceiveEventPolicy stateName=RequestAAIState triggerName=VCPEStateUpdatedEvent defaultTaskName=AAILookupTask
result: SUCCESS

/root> policy state output create name=ReceiveEventPolicy stateName=RequestAAIState outputName=RequestAAIStateOutput eventName=VCPEStateUpdatedEvent nextState=OnsetOrAbatedState
result: SUCCESS

/root> policy state taskref create name=ReceiveEventPolicy stateName=RequestAAIState taskName=AAILookupTask outputType=DIRECT outputName=RequestAAIStateOutput
result: SUCCESS

/root> 
/root> policy state contextref create name=ReceiveEventPolicy stateName=RequestAAIState albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> 
/root> policy state create name=ReceiveEventPolicy stateName=GetVCPEState triggerName=ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e defaultTaskName=GetVCPEStateTask
result: SUCCESS

/root> policy state output create name=ReceiveEventPolicy stateName=GetVCPEState outputName=GetVCPEStateOutput eventName=VCPEStateUpdatedEvent nextState=RequestAAIState
result: SUCCESS

/root> policy state taskref create name=ReceiveEventPolicy stateName=GetVCPEState taskName=GetVCPEStateTask outputType=DIRECT outputName=GetVCPEStateOutput
result: SUCCESS

/root> 
/root> #
/root> # RestartAPPCRequestPolicy
/root> #
/root> policy create name=RestartAPPCRequestPolicy template=Freestyle firstState=PolicyGuardResponseState
result: SUCCESS

/root> 
/root> policy state create name=RestartAPPCRequestPolicy stateName=DeniedState triggerName=VCPEStateUpdatedEvent defaultTaskName=ControlLoopLogTask
result: SUCCESS

/root> policy state output create name=RestartAPPCRequestPolicy stateName=DeniedState outputName=DeniedOutput eventName=ControlLoopLogEvent
result: SUCCESS

/root> policy state taskref create name=RestartAPPCRequestPolicy stateName=DeniedState taskName=ControlLoopLogTask outputType=DIRECT outputName=DeniedOutput
result: SUCCESS

/root> 
/root> policy state create name=RestartAPPCRequestPolicy stateName=PermitOrDenyState triggerName=VCPEStateUpdatedEvent defaultTaskName=DeniedTask
result: SUCCESS

/root> policy state output create name=RestartAPPCRequestPolicy stateName=PermitOrDenyState outputName=PermitOutput eventName=APPCRestartVNFRequestEvent
result: SUCCESS

/root> policy state taskref create name=RestartAPPCRequestPolicy stateName=PermitOrDenyState taskName=APPCRestartVNFRequestTask outputType=DIRECT outputName=PermitOutput
result: SUCCESS

/root> policy state output create name=RestartAPPCRequestPolicy stateName=PermitOrDenyState outputName=DenyOutput eventName=VCPEStateUpdatedEvent nextState=DeniedState
result: SUCCESS

/root> policy state taskref create name=RestartAPPCRequestPolicy stateName=PermitOrDenyState taskName=DeniedTask outputType=DIRECT outputName=DenyOutput
result: SUCCESS

/root> 
/root> policy state create name=RestartAPPCRequestPolicy stateName=PolicyGuardResponseState triggerName=GuardResponseEvent defaultTaskName=GuardResponseTask
result: SUCCESS

/root> policy state output create name=RestartAPPCRequestPolicy stateName=PolicyGuardResponseState outputName=GuardResponseOutput eventName=VCPEStateUpdatedEvent nextState=PermitOrDenyState
result: SUCCESS

/root> policy state taskref create name=RestartAPPCRequestPolicy stateName=PolicyGuardResponseState taskName=GuardResponseTask outputType=DIRECT outputName=GuardResponseOutput
result: SUCCESS

/root> 
/root> policy state contextref create name=RestartAPPCRequestPolicy stateName=PermitOrDenyState albumName=VCPEClosedLoopStatusAlbum
result: SUCCESS

/root> 
/root> policy state selecttasklogic create name=RestartAPPCRequestPolicy stateName=PermitOrDenyState logicFlavour=JAVASCRIPT logic=LS
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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 vcpeClosedLoopStatus = executor.getContextAlbum("VCPEClosedLoopStatusAlbum").get(
        executor.inFields.get("vnfID").toString());

var guardResult = vcpeClosedLoopStatus.get("notification");

if (guardResult == "OPERATION: GUARD_PERMIT") {
    executor.subject.getTaskKey("APPCRestartVNFRequestTask").copyTo(executor.selectedTask);
} else {
    executor.subject.getTaskKey("DeniedTask").copyTo(executor.selectedTask);
}

executor.logger.info("RestartAPPCRequestPolicyPermitOrDenyTSL State Selected Task:" + executor.selectedTask);

true;

LE
result: SUCCESS

/root> 
/root> #
/root> # RestartAPPCResponsePolicy
/root> #
/root> policy create name=RestartAPPCResponsePolicy template=Freestyle firstState=RestartAPPCResponseState
result: SUCCESS

/root> 
/root> policy state create name=RestartAPPCResponsePolicy stateName=ResponseLogState triggerName=VCPEStateUpdatedEvent defaultTaskName=ControlLoopLogTask
result: SUCCESS

/root> policy state output create name=RestartAPPCResponsePolicy stateName=ResponseLogState outputName=ResponseLogOutput eventName=ControlLoopLogEvent
result: SUCCESS

/root> policy state taskref create name=RestartAPPCResponsePolicy stateName=ResponseLogState taskName=ControlLoopLogTask outputType=DIRECT outputName=ResponseLogOutput
result: SUCCESS

/root> 
/root> policy state create name=RestartAPPCResponsePolicy stateName=RestartAPPCResponseState triggerName=APPCRestartVNFResponseEvent defaultTaskName=APPCRestartVNFResponseTask
result: SUCCESS

/root> policy state output create name=RestartAPPCResponsePolicy stateName=RestartAPPCResponseState outputName=APPCRestartVNFResponseOutput eventName=VCPEStateUpdatedEvent nextState=ResponseLogState
result: SUCCESS

/root> policy state taskref create name=RestartAPPCResponsePolicy stateName=RestartAPPCResponseState taskName=APPCRestartVNFResponseTask outputType=DIRECT outputName=APPCRestartVNFResponseOutput
result: SUCCESS

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

/root> 
/root> /root> #-------------------------------------------------------------------------------
/root> # ============LICENSE_START=======================================================
/root> #  Copyright (C) 2016-2018 Ericsson. All rights reserved.
/root> #  Modifications Copyright (C) 2019-2020,2024 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=ONAPvCPEPolicyModel
result: SUCCESS

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

/root> schema create name=SimpleLongType    flavour=Java schema=java.lang.Long
result: SUCCESS

/root> schema create name=SimpleBooleanType flavour=Java schema=java.lang.Boolean
result: SUCCESS

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

/root> 
/root> schema create name=VirtualControlLoopEventType flavour=Java schema=org.onap.policy.controlloop.VirtualControlLoopEvent
result: SUCCESS

/root> schema create name=VirtualControlLoopNotificationType flavour=Java schema=org.onap.policy.controlloop.VirtualControlLoopNotification
result: SUCCESS

/root> 
/root> schema create name=APPCLCMRequestType flavour=Java schema=org.onap.policy.appclcm.AppcLcmMessageWrapper
result: SUCCESS

/root> schema create name=APPCLCMResponseType flavour=Java schema=org.onap.policy.appclcm.AppcLcmMessageWrapper
result: SUCCESS

/root> 
/root> schema create name=GuardDecisionAttributesType flavour=Avro schema=LS
file "/w/workspace/policy-apex-pdp-sonar/src/main/resources/schemas/definitive/GuardDecisionAttributesType.avsc" specified in Macro File Tag not found
LE
result: SUCCESS