SO changes for Service Intent
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / ModifyServiceIntentInstance.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, Wipro Limited.
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.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
26 import org.onap.so.bpmn.common.scripts.ExceptionUtil
27 import org.onap.so.bpmn.common.scripts.RequestDBUtil
28 import org.onap.so.bpmn.core.json.JsonUtils
29 import org.onap.so.db.request.beans.ResourceOperationStatus
30 import org.slf4j.Logger
31 import org.slf4j.LoggerFactory
32
33 import static org.apache.commons.lang3.StringUtils.isBlank
34
35 class ModifyServiceIntentInstance extends AbstractServiceTaskProcessor {
36     String Prefix="MCLL_"
37     ExceptionUtil exceptionUtil = new ExceptionUtil()
38     JsonUtils jsonUtil = new JsonUtils()
39     RequestDBUtil requestDBUtil = new RequestDBUtil()
40     ServiceIntentUtils serviceIntentUtils = new ServiceIntentUtils()
41
42     private static final Logger logger = LoggerFactory.getLogger(ModifyServiceIntentInstance.class)
43
44     @Override
45     void preProcessRequest(DelegateExecution execution) {
46         logger.debug(Prefix + "preProcessRequest Start")
47         execution.setVariable("prefix", Prefix)
48         execution.setVariable("startTime", System.currentTimeMillis())
49         def msg
50         try {
51             // get request input
52             String subnetInstanceReq = execution.getVariable("bpmnRequest")
53             logger.debug(subnetInstanceReq)
54
55             serviceIntentUtils.setCommonExecutionVars(execution)
56
57             String serviceInstanceID = jsonUtil.getJsonValue(subnetInstanceReq, "serviceInstanceID")
58             if (isBlank(serviceInstanceID)) {
59                 msg = "Input serviceInstanceID is null"
60                 logger.debug(msg)
61                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
62             } else
63             {
64                 execution.setVariable("serviceInstanceID", serviceInstanceID)
65             }
66
67             String jobId = UUID.randomUUID().toString()
68             execution.setVariable("jobId", jobId)
69
70         } catch(BpmnError e) {
71             throw e
72         } catch(Exception ex) {
73             msg = "Exception in ModifyServiceIntentInstance.preProcessRequest " + ex.getMessage()
74             logger.debug(msg)
75             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
76         }
77         logger.debug(Prefix + "preProcessRequest Exit")
78     }
79
80
81     /**
82      * create operation status in request db
83      *
84      * Init the Operation Status
85      */
86     def prepareInitOperationStatus = { DelegateExecution execution ->
87         logger.debug(Prefix + "prepareInitOperationStatus Start")
88
89         String siId = execution.getVariable("serviceInstanceID")
90         String jobId = execution.getVariable("jobId")
91         String nsiId = siId
92         String modelUuid = serviceIntentUtils.getModelUuidFromServiceInstance(siId)
93         logger.debug("Generated new job for Service Instance serviceId:" + nsiId + "jobId:" + jobId)
94
95         ResourceOperationStatus initStatus = new ResourceOperationStatus()
96         initStatus.setServiceId(nsiId)
97         initStatus.setOperationId(jobId)
98         initStatus.setResourceTemplateUUID(modelUuid)
99         initStatus.setOperType("Modify")
100         requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
101
102         logger.debug(Prefix + "prepareInitOperationStatus Exit")
103     }
104
105
106
107     /**
108      * return sync response
109      */
110     def sendSyncResponse = { DelegateExecution execution ->
111         logger.debug(Prefix + "sendSyncResponse Start")
112         try {
113             String jobId = execution.getVariable("jobId")
114             String modifySyncResponse = """{"jobId": "${jobId}","status": "processing"}"""
115                                                 .trim().replaceAll(" ", "")
116             logger.debug("sendSyncResponse to APIH:" + "\n" + modifySyncResponse)
117             sendWorkflowResponse(execution, 202, modifySyncResponse)
118
119             execution.setVariable("sentSyncResponse", true)
120         } catch (Exception ex) {
121             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
122             logger.debug(msg)
123             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
124         }
125         logger.debug(Prefix + "sendSyncResponse Exit")
126     }
127
128 }