cbeb1d3d69b6ff031e6ca7f0492ae835f548c149
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / HandlePNF.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 Huawei Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import org.apache.commons.lang3.StringUtils
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.so.bpmn.common.recipe.ResourceInput
26 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.onap.so.bpmn.common.scripts.MsoUtils
30 import org.onap.so.bpmn.core.UrnPropertiesReader
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames
33 import org.slf4j.Logger
34 import org.slf4j.LoggerFactory
35
36 public class HandlePNF extends AbstractServiceTaskProcessor{
37     private static final Logger logger = LoggerFactory.getLogger( HandlePNF.class);
38
39     ExceptionUtil exceptionUtil = new ExceptionUtil()
40     JsonUtils jsonUtil = new JsonUtils()
41     MsoUtils msoUtils = new MsoUtils()
42     String Prefix="CRESI_"
43
44     @Override
45     void preProcessRequest(DelegateExecution execution) {
46         logger.debug("Start preProcess for HandlePNF")
47         // set correlation ID
48         def resourceInput = execution.getVariable("resourceInput")
49         String serInput = jsonUtil.getJsonValue(resourceInput, "requestsInputs")
50         String correlationId = jsonUtil.getJsonValue(serInput, "service.parameters.requestInputs.ont_ont_pnf_name")
51         if (!StringUtils.isEmpty(correlationId)) {
52             execution.setVariable(ExecutionVariableNames.PNF_CORRELATION_ID, correlationId)
53             logger.debug("Found correlation id : " + correlationId)
54         } else {
55             logger.error("== correlation id is empty ==")
56             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "correlation id is not provided")
57         }
58         
59         String serviceInstanceID = jsonUtil.getJsonValue(resourceInput, ExecutionVariableNames.SERVICE_INSTANCE_ID)
60         if (!StringUtils.isEmpty(serviceInstanceID)) {
61             execution.setVariable(ExecutionVariableNames.SERVICE_INSTANCE_ID, serviceInstanceID)
62             logger.debug("found serviceInstanceID: "+serviceInstanceID)
63         } else {
64             logger.error("== serviceInstance ID is empty ==")
65             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "serviceInstance ID is not provided")
66         }
67
68         // next task will set the uuid
69         logger.debug("exit preProcess for HandlePNF")
70     }
71
72     void postProcessRequest(DelegateExecution execution) {
73         logger.debug("start postProcess for HandlePNF")
74         ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(execution.getVariable("resourceInput"), ResourceInput.class)
75         String operType = resourceInputObj.getOperationType()
76         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
77         String serviceInstanceId = resourceInputObj.getServiceInstanceId()
78         String operationId = resourceInputObj.getOperationId()
79         String progress = "100"
80         String status = "finished"
81         String statusDescription = "SDCN resource creation and activation completed"
82
83         String body = """
84                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
85                         xmlns:ns="http://org.onap.so/requestsdb">
86                         <soapenv:Header/>
87                 <soapenv:Body>
88                     <ns:updateResourceOperationStatus>
89                                <operType>${msoUtils.xmlEscape(operType)}</operType>
90                                <operationId>${msoUtils.xmlEscape(operationId)}</operationId>
91                                <progress>${msoUtils.xmlEscape(progress)}</progress>
92                                <resourceTemplateUUID>${msoUtils.xmlEscape(resourceCustomizationUuid)}</resourceTemplateUUID>
93                                <serviceId>${msoUtils.xmlEscape(serviceInstanceId)}</serviceId>
94                                <status>${msoUtils.xmlEscape(status)}</status>
95                                <statusDescription>${msoUtils.xmlEscape(statusDescription)}</statusDescription>
96                     </ns:updateResourceOperationStatus>
97                 </soapenv:Body>
98                 </soapenv:Envelope>""";
99         logger.debug("body: "+body)
100         setProgressUpdateVariables(execution, body)
101         logger.debug("exit postProcess for HandlePNF")
102     }
103
104     public void sendSyncResponse (DelegateExecution execution) {
105         logger.debug(" *** sendSyncResponse *** ")
106
107         try {
108             String operationStatus = "finished"
109             // RESTResponse for main flow
110             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
111             logger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
112             sendWorkflowResponse(execution, 202, resourceOperationResp)
113             execution.setVariable("sentSyncResponse", true)
114
115         } catch (Exception ex) {
116             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
117             logger.debug(msg)
118             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
119         }
120         logger.debug(" ***** Exit sendSyncResponse *****")
121     }
122
123     private void setProgressUpdateVariables(DelegateExecution execution, String body) {
124         def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
125         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
126         execution.setVariable("CVFMI_updateResOperStatusRequest", body)
127     }
128 }