SO changes for Service Intent
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DeleteServiceIntentInstance.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 DeleteServiceIntentInstance extends AbstractServiceTaskProcessor {
36     String Prefix = "DCLL_"
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(DeleteServiceIntentInstance.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                 execution.setVariable("serviceInstanceID", serviceInstanceID)
64             }
65
66             String jobId = UUID.randomUUID().toString()
67             execution.setVariable("jobId", jobId)
68
69         } catch (BpmnError e) {
70             throw e
71         } catch (Exception ex) {
72             msg = "Exception in DeAllocateSliceSubnet.preProcessRequest " + ex.getMessage()
73             logger.debug(msg)
74             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
75         }
76         logger.debug(Prefix + "preProcessRequest Exit")
77     }
78
79
80     /**
81      * create operation status in request db
82      *
83      * Init the Operation Status
84      */
85     def prepareInitOperationStatus = { DelegateExecution execution ->
86         logger.debug(Prefix + "prepareInitOperationStatus Start")
87
88         String siId = execution.getVariable("serviceInstanceID")
89         String jobId = execution.getVariable("jobId")
90         String nsiId = siId
91         String modelUuid = serviceIntentUtils.getModelUuidFromServiceInstance(siId)
92         logger.debug("Generated new job for Service Instance serviceId:" + nsiId + " jobId:" + jobId)
93
94         ResourceOperationStatus initStatus = new ResourceOperationStatus()
95         initStatus.setServiceId(nsiId)
96         initStatus.setOperationId(jobId)
97         initStatus.setResourceTemplateUUID(modelUuid)
98         initStatus.setOperType("Delete")
99         requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
100
101         logger.debug(Prefix + "prepareInitOperationStatus Exit")
102     }
103
104
105     /**
106      * return sync response
107      */
108     def sendSyncResponse = { DelegateExecution execution ->
109         logger.debug(Prefix + "sendSyncResponse Start")
110         try {
111             String jobId = execution.getVariable("jobId")
112             String deAllocateSyncResponse = """{"jobId": "${jobId}","status": "processing"}""".trim().replaceAll(" ", "")
113
114             logger.debug("sendSyncResponse to APIH:" + "\n" + deAllocateSyncResponse)
115             sendWorkflowResponse(execution, 202, deAllocateSyncResponse)
116
117             execution.setVariable("sentSyncResponse", true)
118         } catch (Exception ex) {
119             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
120             logger.debug(msg)
121             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
122         }
123         logger.debug(Prefix + "sendSyncResponse Exit")
124     }
125
126 }
127