Fixed issues in AN NSSMF for modify & Deallocate flow
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / ModifySliceSubnet.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 ModifySliceSubnet extends AbstractServiceTaskProcessor {
35     String Prefix="ModifySliceSubnet_"
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(ModifySliceSubnet.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 Info
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 servicename = jsonUtil.getJsonValue(subnetInstanceReq, "name")
79             execution.setVariable("servicename", servicename)
80
81             String nsiId = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties.nsiInfo.nsiId")
82             if (isBlank(nsiId)) {
83                 msg = "Input nsiId is null"
84                 logger.debug(msg)
85                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
86             } else
87             {
88                 execution.setVariable("nsiId", nsiId)
89             }
90
91             String networkType = jsonUtil.getJsonValue(subnetInstanceReq, "networkType")
92             if (isBlank(networkType)) {
93                 msg = "Input networkType is null"
94                 logger.debug(msg)
95                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
96             } else
97             {
98                 execution.setVariable("networkType", networkType.toUpperCase())
99             }
100
101             //requestParameters, subscriptionServiceType is 5G
102             String subscriptionServiceType = jsonUtil.getJsonValue(subnetInstanceReq, "subscriptionServiceType")
103             if (isBlank(subscriptionServiceType)) {
104                 msg = "Input subscriptionServiceType is null"
105                 logger.debug(msg)
106                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
107             } else {
108                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
109             }
110
111             String jobId = UUID.randomUUID().toString()
112             execution.setVariable("jobId", jobId)
113
114             String sliceParams = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties")
115             execution.setVariable("sliceParams", sliceParams)
116
117         } catch(BpmnError e) {
118             throw e
119         } catch(Exception ex) {
120             msg = "Exception in ModifySliceSubnet.preProcessRequest " + ex.getMessage()
121             logger.debug(msg)
122             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
123         }
124         logger.debug(Prefix + "preProcessRequest Exit")
125     }
126
127
128     /**
129      * create operation status in request db
130      *
131      * Init the Operation Status
132      */
133     def prepareInitOperationStatus = { DelegateExecution execution ->
134         logger.debug(Prefix + "prepareInitOperationStatus Start")
135
136         String nssiId = execution.getVariable("serviceInstanceID")
137         String jobId = execution.getVariable("jobId")
138         String nsiId = execution.getVariable("nsiId")
139         String modelUuid = anNssmfUtils.getModelUuid(execution, nssiId)
140         logger.debug("Generated new job for Service Instance serviceId:" + nsiId + "jobId:" + jobId)
141
142         ResourceOperationStatus initStatus = new ResourceOperationStatus()
143         initStatus.setServiceId(nsiId)
144         initStatus.setOperationId(jobId)
145         initStatus.setResourceTemplateUUID(modelUuid)
146         initStatus.setOperType("Modify")
147         requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
148
149         logger.debug(Prefix + "prepareInitOperationStatus Exit")
150     }
151
152
153
154     /**
155      * return sync response
156      */
157     def sendSyncResponse = { DelegateExecution execution ->
158         logger.debug(Prefix + "sendSyncResponse Start")
159         try {
160             String jobId = execution.getVariable("jobId")
161             String modifySyncResponse = """{"jobId": "${jobId}","status": "processing"}"""
162                                                 .trim().replaceAll(" ", "")
163             logger.debug("sendSyncResponse to APIH:" + "\n" + modifySyncResponse)
164             sendWorkflowResponse(execution, 202, modifySyncResponse)
165
166             execution.setVariable("sentSyncResponse", true)
167         } catch (Exception ex) {
168             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
169             logger.debug(msg)
170             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
171         }
172         logger.debug(Prefix + "sendSyncResponse Exit")
173     }
174
175 }