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