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