AT&T 1712 and 1802 release code
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / CompleteMsoProcess.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.common.scripts
22
23 import org.apache.commons.lang3.*
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.delegate.DelegateExecution\r
26
27
28 public class CompleteMsoProcess extends AbstractServiceTaskProcessor {
29
30         String Prefix="CMSO_"
31         ExceptionUtil exceptionUtil = new ExceptionUtil()
32
33         // Complete MSO Request processing
34         public initializeProcessVariables(DelegateExecution execution){\r
35
36                 def method = getClass().getSimpleName() + '.initializeProcessVariables(' +'execution=' + execution.getId() +')'
37                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
38                 logDebug('Entered ' + method, isDebugLogEnabled)
39                 try {
40
41                         /* Initialize all the process request variables in this block */
42                         execution.setVariable("prefix",Prefix)
43                         execution.setVariable("CMSO_request_id","")
44                         execution.setVariable("CMSO_notification-url","")
45                         execution.setVariable("CMSO_mso-bpel-name","")
46                         execution.setVariable("CMSO_request_action","")
47                         execution.setVariable("CMSO_notification-url-Ok", false)
48                         execution.setVariable("CMSO_request_id-Ok", false)
49
50                         //updateRequest Adapter process variables
51                         execution.setVariable("CMSO_updateRequestResponse", "")
52                         execution.setVariable("CMSO_updateRequestResponseCode", "")
53                         execution.setVariable("CMSO_updateFinalNotifyAckStatusFailedPayload", "")
54
55                         //Set DB adapter variables here
56                         execution.setVariable("CMSO_updateDBStatusToSuccessPayload", "")
57                         execution.setVariable("CMSO_updateInfraRequestDBPayload", "")
58                         execution.setVariable("CMSO_setUpdateDBstatustoSuccessPayload", "")
59
60                         //Auth variables
61                         execution.setVariable("BasicAuthHeaderValue","")
62
63                         //Response variables
64                         execution.setVariable("CompletionHandlerResponse","")
65                         execution.setVariable("CMSO_ErrorResponse", null)
66                         execution.setVariable("CMSO_ResponseCode", "")
67
68                         setSuccessIndicator(execution, false)
69
70                 } catch (BpmnError e) {
71                         throw e;
72                 } catch (Exception e) {
73                         logError('Caught exception in ' + method, e)
74                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
75                 }
76
77         }
78
79         public void preProcessRequest (DelegateExecution execution) {\r
80
81                 initializeProcessVariables(execution)
82                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
83                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
84 //              utils.log("DEBUG", "*** Started CompleteMsoProcess preProcessRequest Method ***", isDebugLogEnabled);
85                 logDebug('Entered ' + method, isDebugLogEnabled)
86
87                 setBasicDBAuthHeader(execution, isDebugLogEnabled)
88
89                 try {
90                         def xml = execution.getVariable("CompleteMsoProcessRequest")
91
92                         utils.logAudit("CompleteMsoProcess Request: " + xml)
93                         utils.log("DEBUG", "Incoming Request is: "+ xml, isDebugLogEnabled)
94
95                         //mso-bpel-name from the incoming request
96                         def msoBpelName = utils.getNodeText1(xml,"mso-bpel-name")
97                         execution.setVariable("CMSO_mso-bpel-name",msoBpelName)
98
99                         //Check the incoming request type
100                         //Incoming request can be ACTIVE_REQUESTS (request-information node) or  INFRA_ACTIVE_REQUESTS (request-info node)
101                         if (utils.nodeExists(xml, "request-information")) {
102                                 execution.setVariable("CMSO_request_id-Ok", true) // Incoming request is for ACTIVE_REQUESTS
103                         }
104
105                         //Check for rehome indicator
106                         def rehomeIndicator = utils.getNodeText1(xml,"rehomeDone")
107                         execution.setVariable("rehomeDone", rehomeIndicator)
108
109                         //Check notification-url for the incoming request type
110                         //ACTIVE_REQUESTS may have notificationurl node
111                         //INFRA_ACTIVE_REQUESTS notificationurl node does not exist
112                         def notificationurl = ""
113                         if (utils.nodeExists(xml, "notification-url")) {
114                                 notificationurl = utils.getNodeText(xml,"notification-url")
115                                 if(notificationurl != null && !notificationurl.isEmpty()) {
116                                         execution.setVariable("CMSO_notification-url-Ok", true)
117                                         execution.setVariable("CMSO_notification-url",notificationurl)
118                                 }
119                         }
120
121                         //Check request_id for the incoming request type
122                         //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)
123                         //For ACTIVE_REQUESTS payload request-id is NOT optional
124                         def request_id = ""
125                         if (utils.nodeExists(xml, "request-id")) {
126                                 execution.setVariable("CMSO_request_id",utils.getNodeText(xml,"request-id"))
127                         }
128
129
130                         // INFRA_ACTIVE_REQUESTS         have "action" element ... mandatory
131                         // ACTIVE_REQUEST have "request-action" ... mandatory
132                         if (utils.nodeExists(xml, "request-action")) {
133                                 execution.setVariable("CMSO_request_action",utils.getNodeText(xml,"request-action"))
134                         } else if (utils.nodeExists(xml, "action")) {
135                                 execution.setVariable("CMSO_request_action",utils.getNodeText(xml,"action"))
136                         }
137
138                         //Check source for the incoming request type
139                         //For INFRA_ACTIVE_REQUESTS payload source IS optional
140                         //For ACTIVE_REQUESTS payload source is NOT optional
141                         def source = ""
142                         if (utils.nodeExists(xml, "source")) {
143                                 execution.setVariable("CMSO_source",utils.getNodeText(xml,"source"))
144                         }
145
146                         utils.log("DEBUG", "CMSO_notification-url-Ok --> " + execution.getVariable("CMSO_notification-url-Ok"), isDebugLogEnabled)
147                         utils.log("DEBUG", "CMSO_request_id-Ok --> " + execution.getVariable("CMSO_request_id-Ok"), isDebugLogEnabled)
148
149                         // set the DHV/Service Instantiation values if specified in the request
150                         execution.setVariable("CMSO_is_srv_inst_req", String.valueOf("true".equals(utils.getNodeText1(xml, "is-srv-inst-req"))))
151                         utils.log("DEBUG", "CMSO_is_srv_inst_req --> " + execution.getVariable("CMSO_is_srv_inst_req"), isDebugLogEnabled)
152                         execution.setVariable("CMSO_is_json_content", String.valueOf("JSON".equals(utils.getNodeText1(xml, "resp-content-type"))))
153                         utils.log("DEBUG", "CMSO_is_json_content --> " + execution.getVariable("CMSO_is_json_content"), isDebugLogEnabled)
154                         execution.setVariable("CMSO_service_inst_id", utils.getNodeText1(xml, "service-instance-id"))
155                         utils.log("DEBUG", "CMSO_service_inst_id --> " + execution.getVariable("CMSO_service_inst_id"), isDebugLogEnabled)
156                         execution.setVariable("CMSO_start_time", utils.getNodeText1(xml, "start-time"))
157                         utils.log("DEBUG", "CMSO_start_time --> " + execution.getVariable("CMSO_start_time"), isDebugLogEnabled)
158                         // this variable is used by the camunda flow to set the Content-Type for the async response
159                         if (execution.getVariable("CMSO_is_srv_inst_req").equals("true") &&
160                                 execution.getVariable("CMSO_is_json_content").equals("true")) {
161                                 execution.setVariable("CMSO_content_type", "application/json")
162                         } else {
163                                 execution.setVariable("CMSO_content_type", "text/xml")
164                         }
165
166                         logDebug('Exited ' + method, isDebugLogEnabled)
167                 } catch (BpmnError e) {
168                         throw e;
169                 } catch (Exception e) {
170                         utils.log("DEBUG", "Exception Occured During PreProcessRequest: " + e, isDebugLogEnabled);
171                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
172                 }
173
174 //              utils.log("DEBUG", "*** Completed CompleteMsoProcess preProcessRequest Method ***", isDebugLogEnabled);
175         }
176
177         public void setUpdateDBstatustoSuccessPayload (DelegateExecution execution){\r
178
179                 def method = getClass().getSimpleName() + '.setUpdateDBstatustoSuccessPayload(' +'execution=' + execution.getId() +')'
180                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
181                 logDebug('Entered ' + method, isDebugLogEnabled)
182
183                 try {
184
185                         def xml = execution.getVariable("CompleteMsoProcessRequest")
186
187                         //Get statusMessage if exists
188                         def statusMessage
189                         if(utils.nodeExists(xml, "status-message")){
190                                 statusMessage = utils.getNodeText1(xml, "status-message")
191                         }else{
192                                 statusMessage = "Resource Completed Successfully"
193                         }
194
195                         //Get instance Id if exist
196                         String idXml = ""
197                         if(utils.nodeExists(xml, "vnfId")){
198                                 idXml = utils.getNodeXml(xml, "vnfId")
199                         }else if(utils.nodeExists(xml, "networkId")){
200                                 idXml = utils.getNodeXml(xml, "networkId")
201                         }else if(utils.nodeExists(xml, "configurationId")){
202                                 idXml = utils.getNodeXml(xml, "configurationId")
203                         }else if(utils.nodeExists(xml, "serviceInstanceId")){
204                                 idXml = utils.getNodeXml(xml, "serviceInstanceId")
205                         }else if(utils.nodeExists(xml, "vfModuleId")){
206                                 idXml = utils.getNodeXml(xml, "vfModuleId")
207                         }else if(utils.nodeExists(xml, "volumeGroupId")){
208                                 idXml = utils.getNodeXml(xml, "volumeGroupId")
209                         }else{
210                                 idXml = ""
211                         }
212                         idXml = utils.removeXmlPreamble(idXml)
213                         utils.log("DEBUG", "Incoming Instance Id Xml: " + idXml, isDebugLogEnabled)
214
215                         String payload = """
216                                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">
217                                                    <soapenv:Header/>
218                                                    <soapenv:Body>
219                                                       <req:updateInfraRequest>
220                                                          <requestId>${execution.getVariable("CMSO_request_id")}</requestId>
221                                                          <lastModifiedBy>${execution.getVariable("CMSO_mso-bpel-name")}</lastModifiedBy>
222                                                          <statusMessage>${statusMessage}</statusMessage>
223                                                          <requestStatus>COMPLETE</requestStatus>
224                                                                  <progress>100</progress>
225                                                                  ${idXml}
226                                                       </req:updateInfraRequest>
227                                                    </soapenv:Body>
228                                                 </soapenv:Envelope>"""
229
230                         execution.setVariable("CMSO_setUpdateDBstatustoSuccessPayload", payload)
231                         utils.log("DEBUG", "Outgoing Update Mso Request Payload is: " + payload, isDebugLogEnabled)
232                         utils.logAudit("setUpdateDBstatustoSuccessPayload: " + payload)
233
234                 } catch (BpmnError e) {
235                         throw e;
236                 } catch (Exception e) {
237                         logError('Caught exception in ' + method, e)
238                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
239                 }
240                 logDebug('Exited ' + method, isDebugLogEnabled)
241         }
242
243         public void buildDataError (DelegateExecution execution, String message) {\r
244
245                 def method = getClass().getSimpleName() + '.buildDataError(' +'execution=' + execution.getId() +')'
246                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
247                 logDebug('Entered ' + method, isDebugLogEnabled)
248                 try {
249
250                         def msoCompletionResponse = """
251                         <sdncadapterworkflow:MsoCompletionResponse xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">
252                            <sdncadapterworkflow:out>BPEL ${execution.getVariable("CMSO_mso-bpel-name")} FAILED</sdncadapterworkflow:out>
253                         </sdncadapterworkflow:MsoCompletionResponse>
254                         """.trim()
255
256                         // Format Response
257                         def xmlMsoCompletionResponse = utils.formatXml(msoCompletionResponse)
258                         String buildMsoCompletionResponseAsString = xmlMsoCompletionResponse.drop(38).trim()
259                         utils.logAudit("CompleteMsoProcess Response: " + buildMsoCompletionResponseAsString)
260                         execution.setVariable("CompleteMsoProcessResponse", buildMsoCompletionResponseAsString)
261                         utils.log("DEBUG", "@@ CompleteMsoProcess Response @@ " + "\n" + execution.getVariable("CompletionHandlerResponse"), isDebugLogEnabled)
262
263                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, message)
264
265                 } catch (BpmnError e) {
266                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugLogEnabled)
267                         throw e;
268                 } catch (Exception e) {
269                         logError('Caught exception in ' + method, e)
270                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
271                 }
272
273         }
274
275         public void postProcessResponse (DelegateExecution execution) {\r
276
277                                 def method = getClass().getSimpleName() + '.postProcessResponse(' +'execution=' + execution.getId() +')'
278                                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
279                                 logDebug('Entered ' + method, isDebugLogEnabled)
280                 //              utils.log("DEBUG", "*** Started CompleteMsoProcess PostProcessRequest Method ***", isDebugLogEnabled);
281                                 try {
282
283                                         def msoCompletionResponse = """
284                         <sdncadapterworkflow:MsoCompletionResponse xmlns:sdncadapterworkflow="http://ecomp.com/mso/workflow/schema/v1">
285                            <sdncadapterworkflow:out>BPEL ${execution.getVariable("CMSO_mso-bpel-name")} completed</sdncadapterworkflow:out>
286                         </sdncadapterworkflow:MsoCompletionResponse>
287                         """.trim()
288
289                                         // Format Response
290                                         def xmlMsoCompletionResponse = utils.formatXML(msoCompletionResponse)
291                                         String buildMsoCompletionResponseAsString = xmlMsoCompletionResponse.drop(38).trim()
292                                         // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
293                                         execution.setVariable("WorkflowResponse", buildMsoCompletionResponseAsString)
294                                         utils.logAudit("CompleteMsoProcess Response: " + buildMsoCompletionResponseAsString)
295                                         execution.setVariable("CompleteMsoProcessResponse", buildMsoCompletionResponseAsString)
296                                         execution.setVariable("CMSO_ResponseCode", "200")
297
298                                         setSuccessIndicator(execution, true)
299
300                                         utils.log("DEBUG", "@@ CompleteMsoProcess Response @@ " + "\n" + execution.getVariable("CompleteMsoProcessResponse"), isDebugLogEnabled)
301
302                                         logDebug('Exited ' + method, isDebugLogEnabled)
303                                 } catch (BpmnError e) {
304                                         throw e;
305                                 } catch (Exception e) {
306                                         logError('Caught exception in ' + method, e)
307                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
308                                 }
309                 //              utils.log("DEBUG", "*** Completed CompleteMsoProcess PostProcessRequest Method ***", isDebugLogEnabled);
310
311         }
312
313
314 }