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