Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DeleteCustomE2EServiceInstance.groovy
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                         String operationId = jsonUtil.getJsonValue(siRequest, "operationId")\r
118                         execution.setVariable("operationId", operationId)\r
119                                         \r
120                         execution.setVariable("operationType", "DELETE")\r
121                 } catch (BpmnError e) {\r
122                         throw e;\r
123                 } catch (Exception ex){\r
124                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
125                         utils.log("INFO", msg, isDebugEnabled)\r
126                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
127                 }\r
128                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
129         }\r
130 \r
131         public void sendSyncResponse (DelegateExecution execution) {\r
132                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
133                 utils.log("INFO", " *** sendSyncResponse  *** ", isDebugEnabled)\r
134                 try {\r
135                         String operationId = execution.getVariable("operationId")\r
136                         String syncResponse = """{"operationId":"${operationId}"}""".trim()\r
137                         utils.log("INFO", " sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse, isDebugEnabled)\r
138                         sendWorkflowResponse(execution, 202, syncResponse)\r
139 \r
140                 } catch (Exception ex) {\r
141                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()\r
142                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
143                 }\r
144                 utils.log("INFO"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)\r
145         }\r
146         \r
147         public void sendSyncError (DelegateExecution execution) {\r
148                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
149                 utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)\r
150 \r
151                 try {\r
152                         String errorMessage = ""\r
153                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {\r
154                                 WorkflowException wfe = execution.getVariable("WorkflowException")\r
155                                 errorMessage = wfe.getErrorMessage()\r
156                         } else {\r
157                                 errorMessage = "Sending Sync Error."\r
158                         }\r
159 \r
160                         String buildworkflowException =\r
161                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
162                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>\r
163                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
164                                    </aetgt:WorkflowException>"""\r
165 \r
166                         utils.logAudit(buildworkflowException)\r
167                         sendWorkflowResponse(execution, 500, buildworkflowException)\r
168 \r
169                 } catch (Exception ex) {\r
170                         utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)\r
171                 }\r
172 \r
173         }\r
174         \r
175         public void prepareCompletionRequest (DelegateExecution execution) {\r
176                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
177                 utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)\r
178 \r
179                 try {\r
180                         String requestId = execution.getVariable("msoRequestId")\r
181                         String source = execution.getVariable("source")\r
182                         String msoCompletionRequest =\r
183                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
184                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
185                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
186                                                         <request-id>${requestId}</request-id>\r
187                                                         <action>DELETE</action>\r
188                                                         <source>${source}</source>\r
189                                                 </request-info>\r
190                                                 <aetgt:status-message>E2E Service Instance was deleted successfully.</aetgt:status-message>\r
191                                                 <aetgt:mso-bpel-name>DeleteCustomE2EServiceInstance</aetgt:mso-bpel-name>\r
192                                         </aetgt:MsoCompletionRequest>"""\r
193 \r
194                         // Format Response\r
195                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)\r
196 \r
197                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)\r
198                         utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)\r
199 \r
200                 } catch (Exception ex) {\r
201                         String msg = " Exception in prepareCompletion:" + ex.getMessage()\r
202                         utils.log("INFO", msg, isDebugEnabled)\r
203                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
204                 }\r
205                 utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)\r
206         }\r
207         \r
208         public void prepareFalloutRequest(DelegateExecution execution){\r
209                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
210                 utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)\r
211 \r
212                 try {\r
213                         WorkflowException wfex = execution.getVariable("WorkflowException")\r
214                         utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)\r
215                         String requestId = execution.getVariable("msoRequestId")\r
216                         String source = execution.getVariable("source")\r
217                         String requestInfo =\r
218                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
219                                         <request-id>${requestId}</request-id>\r
220                                         <action>DELETE</action>\r
221                                         <source>${source}</source>\r
222                                    </request-info>"""\r
223 \r
224                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)\r
225                         execution.setVariable("falloutRequest", falloutRequest)\r
226                 } catch (Exception ex) {\r
227                         utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)\r
228                         String errorException = "  Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()\r
229                         String requestId = execution.getVariable("msoRequestId")\r
230                         String falloutRequest =\r
231                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
232                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
233                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
234                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
235                                               <request-id>${requestId}</request-id>\r
236                                               <action>DELETE</action>\r
237                                               <source>VID</source>\r
238                                            </request-info>\r
239                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
240                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>\r
241                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
242                                                 </aetgt:WorkflowException>\r
243                                         </aetgt:FalloutHandlerRequest>"""\r
244 \r
245                         execution.setVariable("falloutRequest", falloutRequest)\r
246                 }\r
247                 utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)\r
248         }\r
249         \r
250 \r
251         // *******************************\r
252         //     Build DB request Section\r
253         // *******************************\r
254         public void prepareDBRequest (DelegateExecution execution) {\r
255                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
256                 execution.setVariable("prefix", Prefix)\r
257 \r
258                 try {\r
259                         utils.log("INFO", " ***** Inside prepareDBRequest of DeleteCustomE2EServiceInstance ***** ", isDebugEnabled)\r
260 \r
261                         String requestId = execution.getVariable("DELSI_requestId")\r
262                         String statusMessage = "E2E Service Instance successfully deleted."\r
263 \r
264                         //TODO - verify the format for Service Instance Delete,\r
265                         String dbRequest =\r
266                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">\r
267                                                 <soapenv:Header/>\r
268                                                 <soapenv:Body>\r
269                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">\r
270                                                                 <requestId>${requestId}</requestId>\r
271                                                                 <lastModifiedBy>BPMN</lastModifiedBy>\r
272                                                                 <statusMessage>${statusMessage}</statusMessage>\r
273                                                                 <responseBody></responseBody>\r
274                                                                 <requestStatus>COMPLETED</requestStatus>\r
275                                                                 <progress>100</progress>\r
276                                                         </ns:updateInfraRequest>\r
277                                                    </soapenv:Body>\r
278                                            </soapenv:Envelope>"""\r
279 \r
280                    String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)\r
281                    execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)\r
282                    utils.logAudit(buildDeleteDBRequestAsString)\r
283 \r
284                 } catch (Exception ex) {\r
285                         // try error in method block\r
286                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()\r
287                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
288 \r
289                 }\r
290 \r
291          }\r
292 \r
293         // *******************************\r
294         //     Build Error Section\r
295         // *******************************\r
296         public void prepareDBRequestError (DelegateExecution execution) {\r
297                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
298                 execution.setVariable("prefix", Prefix)\r
299 \r
300                 utils.log("INFO", " ***** Inside prepareDBRequestError of DeleteCustomE2EServiceInstance ***** ", isDebugEnabled)\r
301 \r
302                 try {\r
303                         String requestId = execution.getVariable("DELSI_requestId")\r
304                         String statusMessage = ""\r
305                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {\r
306                                 WorkflowException wfe = execution.getVariable("WorkflowException")\r
307                                 statusMessage = wfe.getErrorMessage()\r
308 \r
309                         } else {\r
310                                 statusMessage = "Encountered Error during DeleteCustomE2EServiceInstance proccessing. "\r
311                         }\r
312 \r
313                         //TODO - verify the format for Service Instance Create,\r
314                         String dbRequest =\r
315                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">\r
316                                                 <soapenv:Header/>\r
317                                                 <soapenv:Body>\r
318                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">\r
319                                                                 <requestId>${requestId}</requestId>\r
320                                                                 <lastModifiedBy>BPMN</lastModifiedBy>\r
321                                                                 <statusMessage>${statusMessage}</statusMessage>\r
322                                                                 <responseBody></responseBody>\r
323                                                                 <requestStatus>FAILED</requestStatus>\r
324                                                         </ns:updateInfraRequest>\r
325                                                    </soapenv:Body>\r
326                                            </soapenv:Envelope>"""\r
327 \r
328                    String buildDBRequestAsString = utils.formatXml(dbRequest)\r
329                    execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)\r
330                    utils.logAudit(buildDBRequestAsString)\r
331 \r
332                 } catch (Exception ex) {\r
333                         // try error in method block\r
334                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()\r
335                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)\r
336 \r
337                 }\r
338 \r
339          }\r
340 \r
341         public void processJavaException(DelegateExecution execution) {\r
342                 //TODO:\r
343         }\r
344 \r
345         /**\r
346          * Init the service Operation Status\r
347          */\r
348         public void prepareInitServiceOperationStatus(DelegateExecution execution){\r
349                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
350                 utils.log("DEBUG", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)\r
351                 try{\r
352                         String serviceId = execution.getVariable("serviceInstanceId")\r
353                         String operationId = execution.getVariable("operationId")\r
354                         String userId = ""\r
355                         String result = "processing"\r
356                         String progress = "0"\r
357                         String reason = ""\r
358                         String operationContent = "Prepare service creation"\r
359                         utils.log("DEBUG", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)\r
360                         serviceId = UriUtils.encode(serviceId,"UTF-8")\r
361 \r
362                         def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")\r
363                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)\r
364                         utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)\r
365 \r
366                         String payload =\r
367                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
368                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
369                         <soapenv:Header/>\r
370                         <soapenv:Body>\r
371                             <ns:updateServiceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">\r
372                             <serviceId>${serviceId}</serviceId>\r
373                             <operationId>${operationId}</operationId>\r
374                             <operationType>DELETE</operationType>\r
375                             <userId>${userId}</userId>\r
376                             <result>${result}</result>\r
377                             <operationContent>${operationContent}</operationContent>\r
378                             <progress>${progress}</progress>\r
379                             <reason>${reason}</reason>\r
380                         </ns:updateServiceOperationStatus>\r
381                     </soapenv:Body>\r
382                 </soapenv:Envelope>"""\r
383 \r
384                         payload = utils.formatXml(payload)\r
385                         execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)\r
386                         utils.log("DEBUG", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)\r
387 \r
388                 }catch(Exception e){\r
389                         utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)\r
390                         execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())\r
391                 }\r
392                 utils.log("DEBUG", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)\r
393         }\r
394 }\r