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