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