Fix issues in NSMF terminate flow
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DeAllocateSliceSubnet.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 import static org.apache.commons.lang3.StringUtils.isBlank
33
34 class DeAllocateSliceSubnet extends AbstractServiceTaskProcessor {
35     String Prefix="DeAllocateSliceSubnet_"
36     ExceptionUtil exceptionUtil = new ExceptionUtil()
37     JsonUtils jsonUtil = new JsonUtils()
38     RequestDBUtil requestDBUtil = new RequestDBUtil()
39     AnNssmfUtils anNssmfUtils = new AnNssmfUtils()
40
41     private static final Logger logger = LoggerFactory.getLogger(DeAllocateSliceSubnet.class)
42
43      @Override
44     void preProcessRequest(DelegateExecution execution) {
45         logger.debug(Prefix + "preProcessRequest Start")
46         execution.setVariable("prefix", Prefix)
47         execution.setVariable("startTime", System.currentTimeMillis())
48         def msg
49         try {
50             // get request input
51             String subnetInstanceReq = execution.getVariable("bpmnRequest")
52             logger.debug(subnetInstanceReq)
53
54             String requestId = execution.getVariable("mso-request-id")
55             execution.setVariable("msoRequestId", requestId)
56             logger.debug("Input Request:" + subnetInstanceReq + " reqId:" + requestId)
57
58             //subscriberInfo
59             String globalSubscriberId = jsonUtil.getJsonValue(subnetInstanceReq, "globalSubscriberId")
60             if (isBlank(globalSubscriberId)) {
61                 msg = "Input globalSubscriberId' is null"
62                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
63             } else {
64                 execution.setVariable("globalSubscriberId", globalSubscriberId)
65             }
66
67             //NSSI ID
68             String serviceInstanceID = jsonUtil.getJsonValue(subnetInstanceReq, "serviceInstanceID")
69             if (isBlank(serviceInstanceID)) {
70                 msg = "Input serviceInstanceID is null"
71                 logger.debug(msg)
72                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
73             } else
74             {
75                 execution.setVariable("serviceInstanceID", serviceInstanceID)
76             }
77
78             String nsiId = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties.nsiId")
79             if (isBlank(nsiId)) {
80                 msg = "Input nsiId is null"
81                 logger.debug(msg)
82                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
83             } else
84             {
85                 execution.setVariable("nsiId", nsiId)
86             }
87
88             String networkType = jsonUtil.getJsonValue(subnetInstanceReq, "networkType")
89             if (isBlank(networkType)) {
90                 msg = "Input networkType is null"
91                 logger.debug(msg)
92                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
93             } else
94             {
95                 execution.setVariable("networkType", networkType.toUpperCase())
96             }
97
98             //requestParameters, subscriptionServiceType is 5G
99             String subscriptionServiceType = jsonUtil.getJsonValue(subnetInstanceReq, "subscriptionServiceType")
100             if (isBlank(subscriptionServiceType)) {
101                 msg = "Input subscriptionServiceType is null"
102                 logger.debug(msg)
103                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
104             } else {
105                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
106             }
107             
108             String jobId = UUID.randomUUID().toString()
109             execution.setVariable("jobId", jobId)
110
111             String sliceParams = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties")
112             execution.setVariable("sliceParams", sliceParams)
113
114         } catch(BpmnError e) {
115             throw e
116         } catch(Exception ex) {
117             msg = "Exception in DeAllocateSliceSubnet.preProcessRequest " + ex.getMessage()
118             logger.debug(msg)
119             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
120         }
121         logger.debug(Prefix + "preProcessRequest Exit")
122     }
123
124
125     /**
126      * create operation status in request db
127      *
128      * Init the Operation Status
129      */
130     def prepareInitOperationStatus = { DelegateExecution execution ->
131         logger.debug(Prefix + "prepareInitOperationStatus Start")
132
133         String nssiId = execution.getVariable("serviceInstanceID")
134         String jobId = execution.getVariable("jobId")
135         String nsiId = execution.getVariable("nsiId")
136         String modelUuid = anNssmfUtils.getModelUuid(execution, nssiId)
137         logger.debug("Generated new job for Service Instance serviceId:" + nsiId + " jobId:" + jobId)
138
139         ResourceOperationStatus initStatus = new ResourceOperationStatus()
140         initStatus.setServiceId(nsiId)
141         initStatus.setOperationId(jobId)
142         //initStatus.setResourceTemplateUUID(modelUuid)
143         initStatus.setResourceInstanceID(nssiId)
144         initStatus.setOperType("Deallocate")
145         requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
146
147         logger.debug(Prefix + "prepareInitOperationStatus Exit")
148     }
149
150
151
152     /**
153      * return sync response
154      */
155     def sendSyncResponse = { DelegateExecution execution ->
156         logger.debug(Prefix + "sendSyncResponse Start")
157         try {
158             String jobId = execution.getVariable("jobId")
159             String deAllocateSyncResponse = """{"jobId": "${jobId}","status": "processing"}""".trim().replaceAll(" ", "")
160
161             logger.debug("sendSyncResponse to APIH:" + "\n" + deAllocateSyncResponse)
162             sendWorkflowResponse(execution, 202, deAllocateSyncResponse)
163
164             execution.setVariable("sentSyncResponse", true)
165         } catch (Exception ex) {
166             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
167             logger.debug(msg)
168             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
169         }
170         logger.debug(Prefix + "sendSyncResponse Exit")
171     }
172
173 }
174