d218adc2797b3144f67c256e1389fc8f95257184
[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                         \r
86                         //requestInfo\r
87 //                      String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")\r
88 //                      if (isBlank(productFamilyId))\r
89 //                      {\r
90 //                              msg = "Input productFamilyId is null"\r
91 //                              utils.log("INFO", msg, isDebugEnabled)\r
92 //                              //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
93 //                      } else {\r
94 //                              execution.setVariable("productFamilyId", productFamilyId)\r
95 //                      }\r
96                         String source = jsonUtil.getJsonValue(siRequest, "source")\r
97                         execution.setVariable("source", source)\r
98                         \r
99                         //subscriberInfo\r
100                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "globalSubscriberId")\r
101                         if (isBlank(globalSubscriberId)) {\r
102                                 msg = "Input globalSubscriberId' is null"\r
103                                 utils.log("INFO", msg, isDebugEnabled)\r
104                         } else {\r
105                                 execution.setVariable("globalSubscriberId", globalSubscriberId)\r
106                         }\r
107                         \r
108                         //requestParameters\r
109                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "serviceType")\r
110                         if (isBlank(subscriptionServiceType)) {\r
111                                 msg = "Input subscriptionServiceType is null"\r
112                                 utils.log("DEBUG", msg, isDebugEnabled)\r
113                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
114                         } else {\r
115                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)\r
116                         }\r
117 \r
118                         execution.setVariable("operationType", "DELETE")\r
119                 } catch (BpmnError e) {\r
120                         throw e;\r
121                 } catch (Exception ex){\r
122                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
123                         utils.log("INFO", msg, isDebugEnabled)\r
124                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
125                 }\r
126                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
127         }\r
128 \r
129         public void sendSyncResponse (DelegateExecution execution) {\r
130                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
131                 utils.log("INFO", " *** sendSyncResponse  *** ", isDebugEnabled)\r
132 \r
133                 try {\r
134                         String requestId = execution.getVariable("msoRequestId")\r
135                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
136 \r
137                         // RESTResponse (for API Handler (APIH) Reply Task)\r
138                         String syncResponse = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()\r
139                         utils.log("INFO", " sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse, isDebugEnabled)\r
140                         sendWorkflowResponse(execution, 202, syncResponse)\r
141 \r
142                 } catch (Exception ex) {\r
143                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()\r
144                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
145                 }\r
146                 utils.log("INFO"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)\r
147         }\r
148         \r
149         public void sendSyncError (DelegateExecution execution) {\r
150                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
151                 utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)\r
152 \r
153                 try {\r
154                         String errorMessage = ""\r
155                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {\r
156                                 WorkflowException wfe = execution.getVariable("WorkflowException")\r
157                                 errorMessage = wfe.getErrorMessage()\r
158                         } else {\r
159                                 errorMessage = "Sending Sync Error."\r
160                         }\r
161 \r
162                         String buildworkflowException =\r
163                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
164                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>\r
165                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
166                                    </aetgt:WorkflowException>"""\r
167 \r
168                         utils.logAudit(buildworkflowException)\r
169                         sendWorkflowResponse(execution, 500, buildworkflowException)\r
170 \r
171                 } catch (Exception ex) {\r
172                         utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)\r
173                 }\r
174 \r
175         }\r
176         \r
177         public void prepareCompletionRequest (DelegateExecution execution) {\r
178                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
179                 utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)\r
180 \r
181                 try {\r
182                         String requestId = execution.getVariable("msoRequestId")\r
183                         String source = execution.getVariable("source")\r
184                         String msoCompletionRequest =\r
185                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
186                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
187                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
188                                                         <request-id>${requestId}</request-id>\r
189                                                         <action>DELETE</action>\r
190                                                         <source>${source}</source>\r
191                                                 </request-info>\r
192                                                 <aetgt:status-message>E2E Service Instance was deleted successfully.</aetgt:status-message>\r
193                                                 <aetgt:mso-bpel-name>DeleteCustomE2EServiceInstance</aetgt:mso-bpel-name>\r
194                                         </aetgt:MsoCompletionRequest>"""\r
195 \r
196                         // Format Response\r
197                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)\r
198 \r
199                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)\r
200                         utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)\r
201 \r
202                 } catch (Exception ex) {\r
203                         String msg = " Exception in prepareCompletion:" + ex.getMessage()\r
204                         utils.log("INFO", msg, isDebugEnabled)\r
205                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
206                 }\r
207                 utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)\r
208         }\r
209         \r
210         public void prepareFalloutRequest(DelegateExecution execution){\r
211                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
212                 utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)\r
213 \r
214                 try {\r
215                         WorkflowException wfex = execution.getVariable("WorkflowException")\r
216                         utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)\r
217                         String requestId = execution.getVariable("msoRequestId")\r
218                         String source = execution.getVariable("source")\r
219                         String requestInfo =\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 \r
226                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)\r
227                         execution.setVariable("falloutRequest", falloutRequest)\r
228                 } catch (Exception ex) {\r
229                         utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)\r
230                         String errorException = "  Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()\r
231                         String requestId = execution.getVariable("msoRequestId")\r
232                         String falloutRequest =\r
233                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
234                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
235                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
236                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
237                                               <request-id>${requestId}</request-id>\r
238                                               <action>DELETE</action>\r
239                                               <source>VID</source>\r
240                                            </request-info>\r
241                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
242                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>\r
243                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
244                                                 </aetgt:WorkflowException>\r
245                                         </aetgt:FalloutHandlerRequest>"""\r
246 \r
247                         execution.setVariable("falloutRequest", falloutRequest)\r
248                 }\r
249                 utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)\r
250         }\r
251         \r
252 \r
253         // *******************************\r
254         //     Build DB request Section\r
255         // *******************************\r
256         public void prepareDBRequest (DelegateExecution execution) {\r
257                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
258                 execution.setVariable("prefix", Prefix)\r
259 \r
260                 try {\r
261                         utils.log("INFO", " ***** Inside prepareDBRequest of DeleteCustomE2EServiceInstance ***** ", isDebugEnabled)\r
262 \r
263                         String requestId = execution.getVariable("DELSI_requestId")\r
264                         String statusMessage = "E2E Service Instance successfully deleted."\r
265 \r
266                         //TODO - verify the format for Service Instance Delete,\r
267                         String dbRequest =\r
268                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">\r
269                                                 <soapenv:Header/>\r
270                                                 <soapenv:Body>\r
271                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">\r
272                                                                 <requestId>${requestId}</requestId>\r
273                                                                 <lastModifiedBy>BPMN</lastModifiedBy>\r
274                                                                 <statusMessage>${statusMessage}</statusMessage>\r
275                                                                 <responseBody></responseBody>\r
276                                                                 <requestStatus>COMPLETED</requestStatus>\r
277                                                                 <progress>100</progress>\r
278                                                         </ns:updateInfraRequest>\r
279                                                    </soapenv:Body>\r
280                                            </soapenv:Envelope>"""\r
281 \r
282                    String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)\r
283                    execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)\r
284                    utils.logAudit(buildDeleteDBRequestAsString)\r
285 \r
286                 } catch (Exception ex) {\r
287                         // try error in method block\r
288                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()\r
289                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
290 \r
291                 }\r
292 \r
293          }\r
294 \r
295         // *******************************\r
296         //     Build Error Section\r
297         // *******************************\r
298         public void prepareDBRequestError (DelegateExecution execution) {\r
299                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
300                 execution.setVariable("prefix", Prefix)\r
301 \r
302                 utils.log("INFO", " ***** Inside prepareDBRequestError of DeleteCustomE2EServiceInstance ***** ", isDebugEnabled)\r
303 \r
304                 try {\r
305                         String requestId = execution.getVariable("DELSI_requestId")\r
306                         String statusMessage = ""\r
307                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {\r
308                                 WorkflowException wfe = execution.getVariable("WorkflowException")\r
309                                 statusMessage = wfe.getErrorMessage()\r
310 \r
311                         } else {\r
312                                 statusMessage = "Encountered Error during DeleteCustomE2EServiceInstance proccessing. "\r
313                         }\r
314 \r
315                         //TODO - verify the format for Service Instance Create,\r
316                         String dbRequest =\r
317                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">\r
318                                                 <soapenv:Header/>\r
319                                                 <soapenv:Body>\r
320                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">\r
321                                                                 <requestId>${requestId}</requestId>\r
322                                                                 <lastModifiedBy>BPMN</lastModifiedBy>\r
323                                                                 <statusMessage>${statusMessage}</statusMessage>\r
324                                                                 <responseBody></responseBody>\r
325                                                                 <requestStatus>FAILED</requestStatus>\r
326                                                         </ns:updateInfraRequest>\r
327                                                    </soapenv:Body>\r
328                                            </soapenv:Envelope>"""\r
329 \r
330                    String buildDBRequestAsString = utils.formatXml(dbRequest)\r
331                    execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)\r
332                    utils.logAudit(buildDBRequestAsString)\r
333 \r
334                 } catch (Exception ex) {\r
335                         // try error in method block\r
336                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()\r
337                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)\r
338 \r
339                 }\r
340 \r
341          }\r
342 \r
343         public void processJavaException(DelegateExecution execution) {\r
344                 //TODO:\r
345         }\r
346 }\r