d4b853179cae22b740f75ded59bfaaffa3b45ba9
[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.delegate.DelegateExecution\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 (DelegateExecution execution) {\r
63                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
64                 execution.setVariable("prefix",Prefix)\r
65                 String msg = ""\r
66                 \r
67                 utils.log("INFO", " *** 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("INFO", "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("INFO", 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("INFO", 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.toString())\r
146                                 }\r
147                         }\r
148                         execution.setVariable("operationType", "DELETE") \r
149                         \r
150                         utils.log("DEBUG", "User Input Parameters map: " + userParams.toString(), isDebugEnabled)\r
151                         execution.setVariable("serviceInputParams", inputMap)\r
152 \r
153                 } catch (BpmnError e) {\r
154                         throw e;\r
155                 } catch (Exception ex){\r
156                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
157                         utils.log("INFO", msg, isDebugEnabled)\r
158                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
159                 }\r
160                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
161         }\r
162 \r
163         public void sendSyncResponse (DelegateExecution execution) {\r
164                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
165                 utils.log("INFO", " *** sendSyncResponse  *** ", isDebugEnabled)\r
166 \r
167                 try {\r
168                         String requestId = execution.getVariable("msoRequestId")\r
169                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
170 \r
171                         // RESTResponse (for API Handler (APIH) Reply Task)\r
172                         String syncResponse = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()\r
173                         utils.log("INFO", " sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse, isDebugEnabled)\r
174                         sendWorkflowResponse(execution, 202, syncResponse)\r
175 \r
176                 } catch (Exception ex) {\r
177                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()\r
178                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
179                 }\r
180                 utils.log("INFO"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)\r
181         }\r
182         \r
183         public void sendSyncError (DelegateExecution execution) {\r
184                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
185                 utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)\r
186 \r
187                 try {\r
188                         String errorMessage = ""\r
189                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {\r
190                                 WorkflowException wfe = execution.getVariable("WorkflowException")\r
191                                 errorMessage = wfe.getErrorMessage()\r
192                         } else {\r
193                                 errorMessage = "Sending Sync Error."\r
194                         }\r
195 \r
196                         String buildworkflowException =\r
197                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
198                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>\r
199                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
200                                    </aetgt:WorkflowException>"""\r
201 \r
202                         utils.logAudit(buildworkflowException)\r
203                         sendWorkflowResponse(execution, 500, buildworkflowException)\r
204 \r
205                 } catch (Exception ex) {\r
206                         utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)\r
207                 }\r
208 \r
209         }\r
210         \r
211         public void prepareCompletionRequest (DelegateExecution execution) {\r
212                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
213                 utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)\r
214 \r
215                 try {\r
216                         String requestId = execution.getVariable("msoRequestId")\r
217                         String source = execution.getVariable("source")\r
218                         String msoCompletionRequest =\r
219                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
220                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
221                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
222                                                         <request-id>${requestId}</request-id>\r
223                                                         <action>DELETE</action>\r
224                                                         <source>${source}</source>\r
225                                                 </request-info>\r
226                                                 <aetgt:status-message>E2E Service Instance was deleted successfully.</aetgt:status-message>\r
227                                                 <aetgt:mso-bpel-name>DeleteCustomE2EServiceInstance</aetgt:mso-bpel-name>\r
228                                         </aetgt:MsoCompletionRequest>"""\r
229 \r
230                         // Format Response\r
231                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)\r
232 \r
233                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)\r
234                         utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)\r
235 \r
236                 } catch (Exception ex) {\r
237                         String msg = " Exception in prepareCompletion:" + ex.getMessage()\r
238                         utils.log("INFO", msg, isDebugEnabled)\r
239                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
240                 }\r
241                 utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)\r
242         }\r
243         \r
244         public void prepareFalloutRequest(DelegateExecution execution){\r
245                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
246                 utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)\r
247 \r
248                 try {\r
249                         WorkflowException wfex = execution.getVariable("WorkflowException")\r
250                         utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)\r
251                         String requestId = execution.getVariable("msoRequestId")\r
252                         String source = execution.getVariable("source")\r
253                         String requestInfo =\r
254                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
255                                         <request-id>${requestId}</request-id>\r
256                                         <action>DELETE</action>\r
257                                         <source>${source}</source>\r
258                                    </request-info>"""\r
259 \r
260                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)\r
261                         execution.setVariable("falloutRequest", falloutRequest)\r
262                 } catch (Exception ex) {\r
263                         utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)\r
264                         String errorException = "  Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()\r
265                         String requestId = execution.getVariable("msoRequestId")\r
266                         String falloutRequest =\r
267                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
268                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
269                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
270                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
271                                               <request-id>${requestId}</request-id>\r
272                                               <action>DELETE</action>\r
273                                               <source>VID</source>\r
274                                            </request-info>\r
275                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
276                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>\r
277                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
278                                                 </aetgt:WorkflowException>\r
279                                         </aetgt:FalloutHandlerRequest>"""\r
280 \r
281                         execution.setVariable("falloutRequest", falloutRequest)\r
282                 }\r
283                 utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)\r
284         }\r
285         \r
286 \r
287         // *******************************\r
288         //     Build DB request Section\r
289         // *******************************\r
290         public void prepareDBRequest (DelegateExecution execution) {\r
291                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
292                 execution.setVariable("prefix", Prefix)\r
293 \r
294                 try {\r
295                         utils.log("INFO", " ***** Inside prepareDBRequest of DeleteCustomE2EServiceInstance ***** ", isDebugEnabled)\r
296 \r
297                         String requestId = execution.getVariable("DELSI_requestId")\r
298                         String statusMessage = "E2E Service Instance successfully deleted."\r
299 \r
300                         //TODO - verify the format for Service Instance Delete,\r
301                         String dbRequest =\r
302                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">\r
303                                                 <soapenv:Header/>\r
304                                                 <soapenv:Body>\r
305                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">\r
306                                                                 <requestId>${requestId}</requestId>\r
307                                                                 <lastModifiedBy>BPMN</lastModifiedBy>\r
308                                                                 <statusMessage>${statusMessage}</statusMessage>\r
309                                                                 <responseBody></responseBody>\r
310                                                                 <requestStatus>COMPLETED</requestStatus>\r
311                                                                 <progress>100</progress>\r
312                                                         </ns:updateInfraRequest>\r
313                                                    </soapenv:Body>\r
314                                            </soapenv:Envelope>"""\r
315 \r
316                    String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)\r
317                    execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)\r
318                    utils.logAudit(buildDeleteDBRequestAsString)\r
319 \r
320                 } catch (Exception ex) {\r
321                         // try error in method block\r
322                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()\r
323                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
324 \r
325                 }\r
326 \r
327          }\r
328 \r
329         // *******************************\r
330         //     Build Error Section\r
331         // *******************************\r
332         public void prepareDBRequestError (DelegateExecution execution) {\r
333                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
334                 execution.setVariable("prefix", Prefix)\r
335 \r
336                 utils.log("INFO", " ***** Inside prepareDBRequestError of DeleteCustomE2EServiceInstance ***** ", isDebugEnabled)\r
337 \r
338                 try {\r
339                         String requestId = execution.getVariable("DELSI_requestId")\r
340                         String statusMessage = ""\r
341                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {\r
342                                 WorkflowException wfe = execution.getVariable("WorkflowException")\r
343                                 statusMessage = wfe.getErrorMessage()\r
344 \r
345                         } else {\r
346                                 statusMessage = "Encountered Error during DeleteCustomE2EServiceInstance proccessing. "\r
347                         }\r
348 \r
349                         //TODO - verify the format for Service Instance Create,\r
350                         String dbRequest =\r
351                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">\r
352                                                 <soapenv:Header/>\r
353                                                 <soapenv:Body>\r
354                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">\r
355                                                                 <requestId>${requestId}</requestId>\r
356                                                                 <lastModifiedBy>BPMN</lastModifiedBy>\r
357                                                                 <statusMessage>${statusMessage}</statusMessage>\r
358                                                                 <responseBody></responseBody>\r
359                                                                 <requestStatus>FAILED</requestStatus>\r
360                                                         </ns:updateInfraRequest>\r
361                                                    </soapenv:Body>\r
362                                            </soapenv:Envelope>"""\r
363 \r
364                    String buildDBRequestAsString = utils.formatXml(dbRequest)\r
365                    execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)\r
366                    utils.logAudit(buildDBRequestAsString)\r
367 \r
368                 } catch (Exception ex) {\r
369                         // try error in method block\r
370                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()\r
371                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)\r
372 \r
373                 }\r
374 \r
375          }\r
376 \r
377         public void processJavaException(DelegateExecution execution) {\r
378                 //TODO:\r
379         }\r
380 }\r