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