2f9c8d299b5381d6bec7dbe5e886211c726c443d
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / CompleteMsoProcess.groovy
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 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 \r
23 import org.apache.commons.lang3.*\r
24 import org.camunda.bpm.engine.delegate.BpmnError\r
25 import org.camunda.bpm.engine.runtime.Execution\r
26 \r
27 \r
28 public class CompleteMsoProcess extends AbstractServiceTaskProcessor {\r
29 \r
30         String Prefix="CMSO_"\r
31         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
32 \r
33         // Complete MSO Request processing\r
34         public initializeProcessVariables(Execution execution){\r
35 \r
36                 def method = getClass().getSimpleName() + '.initializeProcessVariables(' +'execution=' + execution.getId() +')'\r
37                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
38                 logDebug('Entered ' + method, isDebugLogEnabled)\r
39                 try {\r
40 \r
41                         /* Initialize all the process request variables in this block */\r
42                         execution.setVariable("prefix",Prefix)\r
43                         execution.setVariable("CMSO_request_id","")\r
44                         execution.setVariable("CMSO_notification-url","")\r
45                         execution.setVariable("CMSO_mso-bpel-name","")\r
46                         execution.setVariable("CMSO_request_action","")\r
47                         execution.setVariable("CMSO_notification-url-Ok", false)\r
48                         execution.setVariable("CMSO_request_id-Ok", false)\r
49 \r
50                         //updateRequest Adapter process variables\r
51                         execution.setVariable("CMSO_updateRequestResponse", "")\r
52                         execution.setVariable("CMSO_updateRequestResponseCode", "")\r
53                         execution.setVariable("CMSO_updateFinalNotifyAckStatusFailedPayload", "")\r
54 \r
55                         //Set DB adapter variables here\r
56                         execution.setVariable("CMSO_updateDBStatusToSuccessPayload", "")\r
57                         execution.setVariable("CMSO_updateInfraRequestDBPayload", "")\r
58                         execution.setVariable("CMSO_setUpdateDBstatustoSuccessPayload", "")\r
59 \r
60                         //Auth variables\r
61                         execution.setVariable("BasicAuthHeaderValue","")\r
62 \r
63                         //Response variables\r
64                         execution.setVariable("CompletionHandlerResponse","")\r
65                         execution.setVariable("CMSO_ErrorResponse", null)\r
66                         execution.setVariable("CMSO_ResponseCode", "")\r
67 \r
68                         setSuccessIndicator(execution, false)\r
69 \r
70                 } catch (BpmnError e) {\r
71                         throw e;\r
72                 } catch (Exception e) {\r
73                         logError('Caught exception in ' + method, e)\r
74                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)\r
75                 }\r
76 \r
77         }\r
78 \r
79         public void preProcessRequest (Execution execution) {\r
80 \r
81                 initializeProcessVariables(execution)\r
82                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'\r
83                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
84 //              utils.log("DEBUG", "*** Started CompleteMsoProcess preProcessRequest Method ***", isDebugLogEnabled);\r
85                 logDebug('Entered ' + method, isDebugLogEnabled)\r
86                 try {\r
87                         def xml = execution.getVariable("CompleteMsoProcessRequest")\r
88 \r
89                         utils.logAudit("CompleteMsoProcess Request: " + xml)\r
90                         utils.log("DEBUG", "Incoming Request is: "+ xml, isDebugLogEnabled)\r
91 \r
92                         //mso-bpel-name from the incoming request\r
93                         def msoBpelName = utils.getNodeText1(xml,"mso-bpel-name")\r
94                         execution.setVariable("CMSO_mso-bpel-name",msoBpelName)\r
95 \r
96                         //Check the incoming request type\r
97                         //Incoming request can be ACTIVE_REQUESTS (request-information node) or  INFRA_ACTIVE_REQUESTS (request-info node)\r
98                         if (utils.nodeExists(xml, "request-information")) {\r
99                                 execution.setVariable("CMSO_request_id-Ok", true) // Incoming request is for ACTIVE_REQUESTS\r
100                         }\r
101 \r
102                         //Check notification-url for the incoming request type\r
103                         //ACTIVE_REQUESTS may have notificationurl node\r
104                         //INFRA_ACTIVE_REQUESTS notificationurl node does not exist\r
105                         def notificationurl = ""\r
106                         if (utils.nodeExists(xml, "notification-url")) {\r
107                                 notificationurl = utils.getNodeText(xml,"notification-url")\r
108                                 if(notificationurl != null && !notificationurl.isEmpty()) {\r
109                                         execution.setVariable("CMSO_notification-url-Ok", true)\r
110                                         execution.setVariable("CMSO_notification-url",notificationurl)\r
111                                 }\r
112                         }\r
113 \r
114                         //Check request_id for the incoming request type\r
115                         //For INFRA_ACTIVE_REQUESTS payload request-id IS optional (Not sure why this is option since req id is primary key ... also tried exe through SOAP UI to check if MSO code handles null like auto generated seq not it does not)\r
116                         //For ACTIVE_REQUESTS payload request-id is NOT optional\r
117                         def request_id = ""\r
118                         if (utils.nodeExists(xml, "request-id")) {\r
119                                 execution.setVariable("CMSO_request_id",utils.getNodeText(xml,"request-id"))\r
120                         }\r
121 \r
122 \r
123                         // INFRA_ACTIVE_REQUESTS         have "action" element ... mandatory\r
124                         // ACTIVE_REQUEST have "request-action" ... mandatory\r
125                         if (utils.nodeExists(xml, "request-action")) {\r
126                                 execution.setVariable("CMSO_request_action",utils.getNodeText(xml,"request-action"))\r
127                         } else if (utils.nodeExists(xml, "action")) {\r
128                                 execution.setVariable("CMSO_request_action",utils.getNodeText(xml,"action"))\r
129                         }\r
130 \r
131                         //Check source for the incoming request type\r
132                         //For INFRA_ACTIVE_REQUESTS payload source IS optional\r
133                         //For ACTIVE_REQUESTS payload source is NOT optional\r
134                         def source = ""\r
135                         if (utils.nodeExists(xml, "source")) {\r
136                                 execution.setVariable("CMSO_source",utils.getNodeText(xml,"source"))\r
137                         }\r
138 \r
139                         utils.log("DEBUG", "CMSO_notification-url-Ok --> " + execution.getVariable("CMSO_notification-url-Ok"), isDebugLogEnabled)\r
140                         utils.log("DEBUG", "CMSO_request_id-Ok --> " + execution.getVariable("CMSO_request_id-Ok"), isDebugLogEnabled)\r
141 \r
142                         // set the DHV/Service Instantiation values if specified in the request\r
143                         execution.setVariable("CMSO_is_srv_inst_req", String.valueOf("true".equals(utils.getNodeText1(xml, "is-srv-inst-req"))))\r
144                         utils.log("DEBUG", "CMSO_is_srv_inst_req --> " + execution.getVariable("CMSO_is_srv_inst_req"), isDebugLogEnabled)\r
145                         execution.setVariable("CMSO_is_json_content", String.valueOf("JSON".equals(utils.getNodeText1(xml, "resp-content-type"))))\r
146                         utils.log("DEBUG", "CMSO_is_json_content --> " + execution.getVariable("CMSO_is_json_content"), isDebugLogEnabled)\r
147                         execution.setVariable("CMSO_service_inst_id", utils.getNodeText1(xml, "service-instance-id"))\r
148                         utils.log("DEBUG", "CMSO_service_inst_id --> " + execution.getVariable("CMSO_service_inst_id"), isDebugLogEnabled)\r
149                         execution.setVariable("CMSO_start_time", utils.getNodeText1(xml, "start-time"))\r
150                         utils.log("DEBUG", "CMSO_start_time --> " + execution.getVariable("CMSO_start_time"), isDebugLogEnabled)\r
151                         // this variable is used by the camunda flow to set the Content-Type for the async response\r
152                         if (execution.getVariable("CMSO_is_srv_inst_req").equals("true") &&\r
153                                 execution.getVariable("CMSO_is_json_content").equals("true")) {\r
154                                 execution.setVariable("CMSO_content_type", "application/json")\r
155                         } else {\r
156                                 execution.setVariable("CMSO_content_type", "text/xml")\r
157                         }\r
158                         \r
159                         logDebug('Exited ' + method, isDebugLogEnabled)\r
160                 } catch (BpmnError e) {\r
161                         throw e;\r
162                 } catch (Exception e) {\r
163                         utils.log("DEBUG", "Exception Occured During PreProcessRequest: " + e, isDebugLogEnabled);\r
164                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in " + method)\r
165                 }\r
166 \r
167 //              utils.log("DEBUG", "*** Completed CompleteMsoProcess preProcessRequest Method ***", isDebugLogEnabled);\r
168         }\r
169 \r
170         public void postProcessResponse (Execution execution) {\r
171 \r
172                 def method = getClass().getSimpleName() + '.postProcessResponse(' +'execution=' + execution.getId() +')'\r
173                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
174                 logDebug('Entered ' + method, isDebugLogEnabled)\r
175 //              utils.log("DEBUG", "*** Started CompleteMsoProcess PostProcessRequest Method ***", isDebugLogEnabled);\r
176                 try {\r
177 \r
178                         def msoCompletionResponse = """\r
179                         <sdncadapterworkflow:MsoCompletionResponse xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">\r
180                            <sdncadapterworkflow:out>BPEL ${execution.getVariable("CMSO_mso-bpel-name")} completed</sdncadapterworkflow:out>\r
181                         </sdncadapterworkflow:MsoCompletionResponse>\r
182                         """.trim()\r
183 \r
184                         // Format Response\r
185                         def xmlMsoCompletionResponse = utils.formatXML(msoCompletionResponse)\r
186                         String buildMsoCompletionResponseAsString = xmlMsoCompletionResponse.drop(38).trim()\r
187                         // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead\r
188                         execution.setVariable("WorkflowResponse", buildMsoCompletionResponseAsString)\r
189                         utils.logAudit("CompleteMsoProcess Response: " + buildMsoCompletionResponseAsString)\r
190                         execution.setVariable("CompleteMsoProcessResponse", buildMsoCompletionResponseAsString)\r
191                         execution.setVariable("CMSO_ResponseCode", "200")\r
192 \r
193                         setSuccessIndicator(execution, true)\r
194 \r
195                         utils.log("DEBUG", "@@ CompleteMsoProcess Response @@ " + "\n" + execution.getVariable("CompleteMsoProcessResponse"), isDebugLogEnabled)\r
196 \r
197                         logDebug('Exited ' + method, isDebugLogEnabled)\r
198                 } catch (BpmnError e) {\r
199                         throw e;\r
200                 } catch (Exception e) {\r
201                         logError('Caught exception in ' + method, e)\r
202                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)\r
203                 }\r
204 //              utils.log("DEBUG", "*** Completed CompleteMsoProcess PostProcessRequest Method ***", isDebugLogEnabled);\r
205 \r
206         }\r
207 \r
208         public void updateDBStatusToSuccessPayload (Execution execution){\r
209                 def method = getClass().getSimpleName() + '.updateDBStatusToSuccessPayload(' +'execution=' + execution.getId() +')'\r
210                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
211                 logDebug('Entered ' + method, isDebugLogEnabled)\r
212                 \r
213                 try {\r
214                         // Catalog DB headers Authorization\r
215                         String basicAuthValueDB = execution.getVariable("URN_mso_adapters_db_auth")\r
216                         utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB, isDebugLogEnabled)\r
217                         \r
218                         def encodedString = utils.getBasicAuth(basicAuthValueDB, execution.getVariable("URN_mso_msoKey"))\r
219                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)\r
220                 } catch (IOException ex) {\r
221                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()\r
222                         utils.log("DEBUG", dataErrorMessage, isDebugLogEnabled)\r
223                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)\r
224                 }\r
225                 \r
226                 try {\r
227 \r
228                         String payload = """\r
229                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">\r
230                         <soapenv:Header/>\r
231                         <soapenv:Body>\r
232                            <req:updateStatus>\r
233                                   <requestId>${execution.getVariable("CMSO_request_id")}</requestId>\r
234                                   <lastModifiedBy>BPEL</lastModifiedBy>\r
235                                   <status>COMPLETED</status>\r
236                            </req:updateStatus>\r
237                         </soapenv:Body>\r
238                  </soapenv:Envelope>\r
239                 """\r
240                         execution.setVariable("CMSO_updateDBStatusToSuccessPayload", payload)\r
241                         utils.logAudit("updateDBStatusToSuccessPayload: " + payload)\r
242                         logDebug('Exited ' + method, isDebugLogEnabled)\r
243                         //println("CMSO_updateDBStatusToSuccessPayload --> " + execution.getVariable("CMSO_updateDBStatusToSuccessPayload"))\r
244 \r
245                 } catch (BpmnError e) {\r
246                         throw e;\r
247                 } catch (Exception e) {\r
248                         logError('Caught exception in ' + method, e)\r
249                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)\r
250                 }\r
251         }\r
252 \r
253         public void setUpdateDBstatustoSuccessPayload (Execution execution){\r
254 \r
255                 def method = getClass().getSimpleName() + '.setUpdateDBstatustoSuccessPayload(' +'execution=' + execution.getId() +')'\r
256                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
257                 logDebug('Entered ' + method, isDebugLogEnabled)\r
258                 \r
259                 try {\r
260                         String basicAuthValueDB = execution.getVariable("URN_mso_adapters_db_auth")\r
261                         utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB, isDebugLogEnabled)\r
262                         \r
263                         def encodedString = utils.getBasicAuth(basicAuthValueDB, execution.getVariable("URN_mso_msoKey"))\r
264                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)\r
265                 } catch (IOException ex) {\r
266                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()\r
267                         utils.log("DEBUG", dataErrorMessage, isDebugLogEnabled)\r
268                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)\r
269                 } \r
270                 \r
271                 try {\r
272
273                         def xml = execution.getVariable("CompleteMsoProcessRequest")\r
274 \r
275                         //Get statusMessage if exists\r
276                         def statusMessage\r
277                         if(utils.nodeExists(xml, "status-message")){\r
278                                 statusMessage = utils.getNodeText1(xml, "status-message")\r
279                         }else{\r
280                                 statusMessage = "Resource Completed Successfully"\r
281                         }\r
282 \r
283                         //Get instance Id if exist\r
284                         String idXml = ""\r
285                         if(utils.nodeExists(xml, "vnfId")){\r
286                                 idXml = utils.getNodeXml(xml, "vnfId")\r
287                         }else if(utils.nodeExists(xml, "networkId")){\r
288                                 idXml = utils.getNodeXml(xml, "networkId")\r
289                         }else if(utils.nodeExists(xml, "serviceInstanceId")){\r
290                                 idXml = utils.getNodeXml(xml, "serviceInstanceId")\r
291                         }else if(utils.nodeExists(xml, "vfModuleId")){\r
292                                 idXml = utils.getNodeXml(xml, "vfModuleId")\r
293                         }else if(utils.nodeExists(xml, "volumeGroupId")){\r
294                                 idXml = utils.getNodeXml(xml, "volumeGroupId")\r
295                         }else{\r
296                                 idXml = ""\r
297                         }\r
298                         idXml = utils.removeXmlPreamble(idXml)\r
299                         utils.log("DEBUG", "Incoming Instance Id Xml: " + idXml, isDebugLogEnabled)\r
300 \r
301                         String payload = """\r
302                                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">\r
303                                                    <soapenv:Header/>\r
304                                                    <soapenv:Body>\r
305                                                       <req:updateInfraRequest>\r
306                                                          <requestId>${execution.getVariable("CMSO_request_id")}</requestId>\r
307                                                          <lastModifiedBy>${execution.getVariable("CMSO_mso-bpel-name")}</lastModifiedBy>\r
308                                                          <statusMessage>${statusMessage}</statusMessage>\r
309                                                          <requestStatus>COMPLETE</requestStatus>\r
310                                                                  <progress>100</progress>\r
311                                                                  ${idXml}\r
312                                                       </req:updateInfraRequest>\r
313                                                    </soapenv:Body>\r
314                                                 </soapenv:Envelope>"""\r
315 \r
316                         execution.setVariable("CMSO_setUpdateDBstatustoSuccessPayload", payload)\r
317                         utils.log("DEBUG", "Outgoing Update Mso Request Payload is: " + payload, isDebugLogEnabled)\r
318                         utils.logAudit("setUpdateDBstatustoSuccessPayload: " + payload)\r
319 \r
320                 } catch (BpmnError e) {\r
321                         throw e;\r
322                 } catch (Exception e) {\r
323                         logError('Caught exception in ' + method, e)\r
324                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)\r
325                 }\r
326                 logDebug('Exited ' + method, isDebugLogEnabled)\r
327         }\r
328 \r
329         public void buildDataError (Execution execution, String message) {\r
330 \r
331                 def method = getClass().getSimpleName() + '.buildDataError(' +'execution=' + execution.getId() +')'\r
332                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
333                 logDebug('Entered ' + method, isDebugLogEnabled)\r
334                 try {\r
335 \r
336                         def msoCompletionResponse = """\r
337                         <sdncadapterworkflow:MsoCompletionResponse xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">\r
338                            <sdncadapterworkflow:out>BPEL ${execution.getVariable("CMSO_mso-bpel-name")} FAILED</sdncadapterworkflow:out>\r
339                         </sdncadapterworkflow:MsoCompletionResponse>\r
340                         """.trim()\r
341 \r
342                         // Format Response\r
343                         def xmlMsoCompletionResponse = utils.formatXml(msoCompletionResponse)\r
344                         String buildMsoCompletionResponseAsString = xmlMsoCompletionResponse.drop(38).trim()\r
345                         utils.logAudit("CompleteMsoProcess Response: " + buildMsoCompletionResponseAsString)\r
346                         execution.setVariable("CompleteMsoProcessResponse", buildMsoCompletionResponseAsString)\r
347                         utils.log("DEBUG", "@@ CompleteMsoProcess Response @@ " + "\n" + execution.getVariable("CompletionHandlerResponse"), isDebugLogEnabled)\r
348 \r
349                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, message)\r
350 \r
351                 } catch (BpmnError e) {\r
352                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugLogEnabled)\r
353                         throw e;\r
354                 } catch (Exception e) {\r
355                         logError('Caught exception in ' + method, e)\r
356                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)\r
357                 }\r
358 \r
359         }\r
360 \r
361 }\r