[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DelServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 package org.openecomp.mso.bpmn.infrastructure.scripts;
21
22 import groovy.xml.XmlUtil
23 import groovy.json.*
24
25 import org.openecomp.mso.bpmn.core.json.JsonUtils
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
28 import org.openecomp.mso.bpmn.common.scripts.VidUtils
29 import org.openecomp.mso.bpmn.core.WorkflowException
30 import org.openecomp.mso.rest.APIResponse;
31
32 import java.util.UUID;
33 import javax.xml.parsers.DocumentBuilder
34 import javax.xml.parsers.DocumentBuilderFactory
35
36 import org.camunda.bpm.engine.delegate.BpmnError
37 import org.camunda.bpm.engine.runtime.Execution
38 import org.json.JSONObject;
39 import org.apache.commons.lang3.*
40 import org.apache.commons.codec.binary.Base64;
41 import org.w3c.dom.Document
42 import org.w3c.dom.Element
43 import org.w3c.dom.Node
44 import org.w3c.dom.NodeList
45 import org.xml.sax.InputSource
46 import static org.apache.commons.lang3.StringUtils.*;
47
48 import org.springframework.web.util.UriUtils;
49
50 /**
51  * This groovy class supports the <class>DelServiceInstance.bpmn</class> process.
52  *
53  */
54 public class DelServiceInstance extends AbstractServiceTaskProcessor {
55
56         String Prefix="DELSI_"
57         ExceptionUtil exceptionUtil = new ExceptionUtil()
58         JsonUtils jsonUtil = new JsonUtils()
59         VidUtils vidUtils = new VidUtils()
60         
61         public void preProcessRequest (Execution execution) {
62                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
63                 execution.setVariable("prefix",Prefix)
64                 String msg = ""
65                 
66                 utils.log("DEBUG", " *** preProcessRequest Request *** ", isDebugEnabled)
67
68                 try {
69                         // check for incoming json message/input
70                         String siRequest = execution.getVariable("bpmnRequest")
71                         utils.logAudit(siRequest)
72                         
73
74                         String requestId = execution.getVariable("mso-request-id")
75                         execution.setVariable("msoRequestId", requestId)
76                         utils.log("DEBUG", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)
77                         
78                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
79                         if (isBlank(serviceInstanceId)) {
80                                 msg = "Input serviceInstanceId' is null"
81                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
82                         }
83                 
84                         //String xmlRequestDetails = vidUtils.getJsonRequestDetailstoXml(siRequest)
85                         //execution.setVariable("requestDetails", xmlRequestDetails)
86                         
87                         //modelInfo
88                         String serviceModelInfo = jsonUtil.getJsonValue(siRequest, "requestDetails.modelInfo")
89                         if (isBlank(serviceModelInfo)) {
90                                 msg = "Input serviceModelInfo is null"
91                                 utils.log("DEBUG", msg, isDebugEnabled)
92                         } else
93                         {
94                                 execution.setVariable("serviceModelInfo", serviceModelInfo)
95                                 //utils.log("DEBUG", "modelInfo" + serviceModelInfo,  isDebugEnabled)
96                         }
97                         
98                         //requestInfo
99                         String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
100                         if (isBlank(productFamilyId))
101                         {
102                                 msg = "Input productFamilyId is null"
103                                 utils.log("DEBUG", msg, isDebugEnabled)
104                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
105                         } else {
106                                 execution.setVariable("productFamilyId", productFamilyId)
107                         }
108                         String source = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.source")
109                         execution.setVariable("source", source)
110                         
111                         //subscriberInfo
112                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "requestDetails.subscriberInfo.globalSubscriberId")
113                         if (isBlank(globalSubscriberId)) {
114                                 msg = "Input globalSubscriberId' is null"
115                                 utils.log("DEBUG", msg, isDebugEnabled)
116                         } else {
117                                 execution.setVariable("globalSubscriberId", globalSubscriberId)
118                         }
119                         
120                         //requestParameters
121                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.subscriptionServiceType")
122                         if (isBlank(subscriptionServiceType)) {
123                                 msg = "Input subscriptionServiceType is null"
124                                 utils.log("DEBUG", msg, isDebugEnabled)
125                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
126                         } else {
127                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
128                         }
129
130                 } catch (BpmnError e) {
131                         throw e;
132                 } catch (Exception ex){
133                         msg = "Exception in preProcessRequest " + ex.getMessage()
134                         utils.log("DEBUG", msg, isDebugEnabled)
135                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
136                 }
137                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
138         }
139
140         public void sendSyncResponse (Execution execution) {
141                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
142                 utils.log("DEBUG", " *** sendSyncResponse  *** ", isDebugEnabled)
143
144                 try {
145                         String requestId = execution.getVariable("msoRequestId")
146                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
147
148                         // RESTResponse (for API Handler (APIH) Reply Task)
149                         String syncResponse = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()
150                         utils.log("DEBUG", " sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse, isDebugEnabled)
151                         sendWorkflowResponse(execution, 202, syncResponse)
152
153                 } catch (Exception ex) {
154                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
155                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
156                 }
157                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
158         }
159         
160         public void sendSyncError (Execution execution) {
161                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
162                 utils.log("DEBUG", " *** sendSyncError *** ", isDebugEnabled)
163
164                 try {
165                         String errorMessage = ""
166                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
167                                 WorkflowException wfe = execution.getVariable("WorkflowException")
168                                 errorMessage = wfe.getErrorMessage()
169                         } else {
170                                 errorMessage = "Sending Sync Error."
171                         }
172
173                         String buildworkflowException =
174                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
175                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
176                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
177                                    </aetgt:WorkflowException>"""
178
179                         utils.logAudit(buildworkflowException)
180                         sendWorkflowResponse(execution, 500, buildworkflowException)
181
182                 } catch (Exception ex) {
183                         utils.log("DEBUG", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
184                 }
185
186         }
187         
188         public void prepareCompletionRequest (Execution execution) {
189                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
190                 utils.log("DEBUG", " *** prepareCompletion *** ", isDebugEnabled)
191
192                 try {
193                         String requestId = execution.getVariable("msoRequestId")
194                         String source = execution.getVariable("source")
195                         String msoCompletionRequest =
196                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
197                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">
198                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
199                                                         <request-id>${requestId}</request-id>
200                                                         <action>DELETE</action>
201                                                         <source>${source}</source>
202                                                 </request-info>
203                                                 <aetgt:status-message>Service Instance was deleted successfully.</aetgt:status-message>
204                                                 <aetgt:mso-bpel-name>DelServiceInstance</aetgt:mso-bpel-name>
205                                         </aetgt:MsoCompletionRequest>"""
206
207                         // Format Response
208                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
209
210                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
211                         utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
212
213                 } catch (Exception ex) {
214                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
215                         utils.log("DEBUG", msg, isDebugEnabled)
216                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
217                 }
218                 utils.log("DEBUG", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
219         }
220         
221         public void prepareFalloutRequest(Execution execution){
222                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
223                 utils.log("DEBUG", " *** prepareFalloutRequest *** ", isDebugEnabled)
224
225                 try {
226                         WorkflowException wfex = execution.getVariable("WorkflowException")
227                         utils.log("DEBUG", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
228                         String requestId = execution.getVariable("msoRequestId")
229                         String source = execution.getVariable("source")
230                         String requestInfo =
231                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
232                                         <request-id>${requestId}</request-id>
233                                         <action>DELETE</action>
234                                         <source>${source}</source>
235                                    </request-info>"""
236
237                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
238                         execution.setVariable("falloutRequest", falloutRequest)
239                 } catch (Exception ex) {
240                         utils.log("DEBUG", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
241                         String errorException = "  Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
242                         String requestId = execution.getVariable("msoRequestId")
243                         String falloutRequest =
244                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
245                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"
246                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">
247                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
248                                               <request-id>${requestId}</request-id>
249                                               <action>DELETE</action>
250                                               <source>VID</source>
251                                            </request-info>
252                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
253                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>
254                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
255                                                 </aetgt:WorkflowException>
256                                         </aetgt:FalloutHandlerRequest>"""
257
258                         execution.setVariable("falloutRequest", falloutRequest)
259                 }
260                 utils.log("DEBUG", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
261         }
262         
263
264         // *******************************
265         //     Build DB request Section
266         // *******************************
267         public void prepareDBRequest (Execution execution) {
268                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
269                 execution.setVariable("prefix", Prefix)
270
271                 try {
272                         utils.log("DEBUG", " ***** Inside prepareDBRequest of DeleteServiceInstance ***** ", isDebugEnabled)
273
274                         String requestId = execution.getVariable("DELSI_requestId")
275                         String statusMessage = "Service Instance successfully deleted."
276
277                         //TODO - verify the format for Service Instance Delete,
278                         String dbRequest =
279                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
280                                                 <soapenv:Header/>
281                                                 <soapenv:Body>
282                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">
283                                                                 <requestId>${requestId}</requestId>
284                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
285                                                                 <statusMessage>${statusMessage}</statusMessage>
286                                                                 <responseBody></responseBody>
287                                                                 <requestStatus>COMPLETED</requestStatus>
288                                                                 <progress>100</progress>
289                                                         </ns:updateInfraRequest>
290                                                    </soapenv:Body>
291                                            </soapenv:Envelope>"""
292
293                    String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)
294                    execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)
295                    utils.logAudit(buildDeleteDBRequestAsString)
296
297                 } catch (Exception ex) {
298                         // try error in method block
299                         String exceptionMessage = "Bpmn error encountered in DeleteServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()
300                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
301
302                 }
303
304          }
305
306         // *******************************
307         //     Build Error Section
308         // *******************************
309         public void prepareDBRequestError (Execution execution) {
310                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
311                 execution.setVariable("prefix", Prefix)
312
313                 utils.log("DEBUG", " ***** Inside prepareDBRequestError of DeleteServiceInstanceInfra ***** ", isDebugEnabled)
314
315                 try {
316                         String requestId = execution.getVariable("DELSI_requestId")
317                         String statusMessage = ""
318                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
319                                 WorkflowException wfe = execution.getVariable("WorkflowException")
320                                 statusMessage = wfe.getErrorMessage()
321
322                         } else {
323                                 statusMessage = "Encountered Error during DeleteServiceInstanceInfra proccessing. "
324                         }
325
326                         //TODO - verify the format for Service Instance Create,
327                         String dbRequest =
328                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
329                                                 <soapenv:Header/>
330                                                 <soapenv:Body>
331                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">
332                                                                 <requestId>${requestId}</requestId>
333                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
334                                                                 <statusMessage>${statusMessage}</statusMessage>
335                                                                 <responseBody></responseBody>
336                                                                 <requestStatus>FAILED</requestStatus>
337                                                         </ns:updateInfraRequest>
338                                                    </soapenv:Body>
339                                            </soapenv:Envelope>"""
340
341                    String buildDBRequestAsString = utils.formatXml(dbRequest)
342                    execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)
343                    utils.logAudit(buildDBRequestAsString)
344
345                 } catch (Exception ex) {
346                         // try error in method block
347                         String exceptionMessage = "Bpmn error encountered in DeleteServiceInstanceInfra flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()
348                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
349
350                 }
351
352          }
353 }