fd6a4a1cdede410c3180e1735c0d02ef3daa582f
[so.git] /
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.onap.so.bpmn.infrastructure.scripts
22
23
24 import org.onap.so.bpmn.core.UrnPropertiesReader
25 import org.onap.so.bpmn.core.json.JsonUtils
26 import org.onap.so.client.HttpClient
27 import org.onap.so.client.HttpClientFactory
28 import org.onap.so.logger.MsoLogger
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.NetworkUtils
32 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
33 import org.onap.so.bpmn.common.scripts.VidUtils
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.utils.TargetEntity
36
37 import javax.ws.rs.core.Response
38 import org.camunda.bpm.engine.delegate.BpmnError
39 import org.camunda.bpm.engine.delegate.DelegateExecution
40
41 /**
42  * This groovy class supports the <class>DoCreateNetworkInstance.bpmn</class> process.
43  *
44  */
45 public class DoCreateNetworkInstanceRollback extends AbstractServiceTaskProcessor {
46         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateNetworkInstanceRollback.class);
47
48         String Prefix="CRENWKIR_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51         VidUtils vidUtils = new VidUtils(this)
52         NetworkUtils networkUtils = new NetworkUtils()
53         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
54
55         def className = getClass().getSimpleName()
56
57         /**
58          * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
59          * @param execution
60          */
61         public InitializeProcessVariables(DelegateExecution execution){
62                 /* Initialize all the process variables in this block */
63
64                 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
65                 execution.setVariable(Prefix + "rollbackSDNCRequest", null)
66                 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", null)
67                 execution.setVariable(Prefix + "WorkflowException", null)
68
69                 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
70                 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
71                 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
72
73                 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
74                 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
75                 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
76
77                 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", "")
78                 execution.setVariable(Prefix + "rollbackActivateSDNCResponse", "")
79                 execution.setVariable(Prefix + "rollbackActivateSDNCReturnCode", "")
80
81                 execution.setVariable(Prefix + "Success", false)
82                 execution.setVariable(Prefix + "fullRollback", false)
83                 execution.setVariable(Prefix + "networkId", "")
84                 execution.setVariable(Prefix + "urlRollbackPoNetwork", "")
85
86         }
87
88         // **************************************************
89         //     Pre or Prepare Request Section
90         // **************************************************
91         /**
92          * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
93          * @param execution
94          */
95         public void preProcessRequest (DelegateExecution execution) {
96                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
97                 execution.setVariable("prefix",Prefix)
98
99                 msoLogger.trace("Inside preProcessRequest() of " + className + ".groovy")
100
101                 try {
102                         // initialize flow variables
103                         InitializeProcessVariables(execution)
104
105                         // GET Incoming request/variables
106                         String rollbackNetworkRequest = null
107                         String rollbackSDNCRequest = null
108                         String rollbackActivateSDNCRequest = null
109
110                         // Partial Rollback
111                         Map<String, String> rollbackData = execution.getVariable("rollbackData")
112                         if (rollbackData != null && rollbackData instanceof Map) {
113
114                                         if(rollbackData.containsKey("rollbackSDNCRequest")) {
115                                            rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
116                                         }
117
118                                         if(rollbackData.containsKey("rollbackNetworkRequest")) {
119                                                 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
120                                         }
121
122                                         if(rollbackData.containsKey("rollbackActivateSDNCRequest")) {
123                                            rollbackActivateSDNCRequest = rollbackData["rollbackActivateSDNCRequest"]
124                                         }
125
126                         }
127
128                         execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
129                         execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
130                         execution.setVariable(Prefix + "rollbackActivateSDNCRequest", rollbackActivateSDNCRequest)
131                         msoLogger.debug("'rollbackData': " + '\n' + execution.getVariable("rollbackData"))
132
133                         String sdncVersion = execution.getVariable("sdncVersion")
134                         msoLogger.debug("sdncVersion? : " + sdncVersion)
135
136                         // PO Authorization Info / headers Authorization=
137                         String basicAuthValuePO = UrnPropertiesReader.getVariable("mso.adapters.po.auth",execution)
138                         try {
139                                 def encodedString = utils.getBasicAuth(basicAuthValuePO, UrnPropertiesReader.getVariable("mso.msoKey", execution))
140                                 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
141                                 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
142
143                         } catch (IOException ex) {
144                                 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
145                                 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
146                                 msoLogger.debug(dataErrorMessage )
147                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
148                         }
149
150                         if (execution.getVariable("SavedWorkflowException1") != null) {
151                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
152                         } else {
153                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
154                         }
155                         msoLogger.debug("*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"))
156                         if(execution.getVariable(Prefix + "WorkflowException") != null) {
157                                 // called by: DoCreateNetworkInstance, partial rollback
158                                 execution.setVariable(Prefix + "fullRollback", false)
159
160                         } else {
161                            // called by: Macro - Full Rollback, WorkflowException = null
162                            execution.setVariable(Prefix + "fullRollback", true)
163
164                         }
165                         msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
166
167
168                 } catch (BpmnError e) {
169                 throw e;
170
171                 } catch (Exception ex) {
172                         // caught exception
173                         String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
174                         msoLogger.debug(exceptionMessage)
175                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
176
177                 }
178
179         }
180
181         public void callPONetworkAdapter (DelegateExecution execution) {
182                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
183                 execution.setVariable("prefix",Prefix)
184
185                 msoLogger.trace("Inside callPONetworkAdapter() of " + className + "")
186
187                 try {
188                         String poUrl = UrnPropertiesReader.getVariable("mso.adapters.network.rest.endpoint",execution)
189                         String rollbackSDNCRequest = execution.getVariable(Prefix + "rollbackSDNCRequest")
190                         String networkId = utils.getNodeText(rollbackSDNCRequest, "network-id")
191
192                         String rollbackNetworkRequest  = execution.getVariable(Prefix + "rollbackNetworkRequest")
193
194                         String urlRollbackPoNetwork = poUrl+ "/" + networkId + "/rollback"
195                         msoLogger.debug("'urlRollbackPoNetwork': " + urlRollbackPoNetwork)
196                         execution.setVariable(Prefix + "urlRollbackPoNetwork", urlRollbackPoNetwork)
197
198                         URL url = new URL(urlRollbackPoNetwork)
199                         HttpClient httpClient = new HttpClientFactory().newXmlClient(url, TargetEntity.OPENSTACK_ADAPTER)
200                         httpClient.addAdditionalHeader("Authorization", execution.getVariable("BasicAuthHeaderValuePO"))
201                         Response response = httpClient.delete(rollbackNetworkRequest)
202
203                         execution.setVariable(Prefix + "rollbackNetworkReturnCode", response.getStatus())
204
205                         msoLogger.debug(" Network Adapter rollback responseCode: " + response.getStatus())
206
207
208                 } catch (Exception ex) {
209                         String exceptionMessage = "Exception Encountered in callPONetworkAdapter() of DoCreateNetworkInstanceRollback flow - " + ex.getMessage()
210                         msoLogger.debug(exceptionMessage)
211                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
212                 }
213
214         }
215
216
217         public void validateRollbackResponses (DelegateExecution execution) {
218                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
219                 execution.setVariable("prefix",Prefix)
220
221                 msoLogger.trace("Inside validateRollbackResponses() of DoCreateNetworkInstanceRollback")
222
223                 try {
224                         // validate PO network rollback response
225                         String rollbackNetworkErrorMessages = ""
226                         String rollbackNetworkReturnCode = "200"
227                         if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
228                                 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
229                                 msoLogger.debug(" NetworkRollback Code - " + rollbackNetworkReturnCode)
230                                 if (rollbackNetworkReturnCode != "200") {
231                                         rollbackNetworkErrorMessages = " + PO Network rollback failed. "
232                                 } else {
233                                         rollbackNetworkErrorMessages = " + PO Network rollback completed."
234                                 }
235                         }
236
237                         // validate SDNC rollback response
238                         String rollbackSdncErrorMessages = ""
239                         String rollbackSDNCReturnCode = "200"
240                         if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
241                                 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
242                                 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
243                                 String rollbackSDNCReturnInnerCode = ""
244                                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
245                                 rollbackSDNCResponse = rollbackSDNCResponse
246                                 rollbackSDNCResponse = rollbackSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
247                                 if (rollbackSDNCReturnCode == "200") {
248                                         if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
249                                                 rollbackSDNCReturnInnerCode = utils.getNodeText(rollbackSDNCResponse, "response-code")
250                                                 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
251                                                     rollbackSdncErrorMessages = " + SNDC assign rollback completed."
252                                                 } else {
253                                                         rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
254                                                 }
255                                         } else {
256                                                   rollbackSdncErrorMessages = " + SNDC assign rollback completed."
257                                         }
258                                  } else {
259                                           rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
260                              }
261                                 msoLogger.debug(" SDNC assign rollback Code - " + rollbackSDNCReturnCode)
262                                 msoLogger.debug(" SDNC assign rollback  Response - " + rollbackSDNCResponse)
263                         }
264
265                         // validate SDNC activate rollback response
266                         String rollbackActivateSdncErrorMessages = ""
267                         String rollbackActivateSDNCReturnCode = "200"
268                         if (execution.getVariable(Prefix + "rollbackActivateSDNCRequest") != null) {
269                                 rollbackActivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackActivateSDNCReturnCode")
270                                 String rollbackActivateSDNCResponse = execution.getVariable(Prefix + "rollbackActivateSDNCResponse")
271                                 String rollbackActivateSDNCReturnInnerCode = ""
272                                 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse
273                                 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
274                                 if (rollbackActivateSDNCReturnCode == "200") {
275                                         if (utils.nodeExists(rollbackActivateSDNCResponse, "response-code")) {
276                                                 rollbackActivateSDNCReturnInnerCode = utils.getNodeText(rollbackActivateSDNCResponse, "response-code")
277                                                 if (rollbackActivateSDNCReturnInnerCode == "200" || rollbackActivateSDNCReturnInnerCode == "" || rollbackActivateSDNCReturnInnerCode == "0") {
278                                                    rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
279                                                 } else {
280                                                         rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
281                                                 }
282                                         } else {
283                                                  rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
284                                         }
285                                 } else {
286                                           rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
287                                 }
288                                 msoLogger.debug(" SDNC activate rollback Code - " + rollbackActivateSDNCReturnCode)
289                                 msoLogger.debug(" SDNC activate rollback  Response - " + rollbackActivateSDNCResponse)
290                         }
291
292
293                         String statusMessage = ""
294                         int errorCode = 7000
295                         msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
296                         if (execution.getVariable(Prefix + "fullRollback") == false) {
297                                 // original WorkflowException,
298                                 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException")
299                                 if (wfe != null) {
300                                    // rollback due to failure in DoCreate - Partial rollback
301                                    statusMessage = wfe.getErrorMessage()
302                                    errorCode = wfe.getErrorCode()
303                                 } else {
304                                    statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
305                                    errorCode = '7000'
306                             }
307
308                                 // set if all rolledbacks are successful
309                                 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
310                                         execution.setVariable("rolledBack", true)
311                                         execution.setVariable("wasDeleted", true)
312
313                                 } else {
314                                         execution.setVariable("rolledBack", false)
315                                         execution.setVariable("wasDeleted", true)
316                                 }
317
318                                 statusMessage = statusMessage + rollbackActivateSdncErrorMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
319                                 msoLogger.debug("Final DoCreateNetworkInstanceRollback status message: " + statusMessage)
320                                 String processKey = getProcessKey(execution);
321                                 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
322                                 execution.setVariable("workflowException", exception);
323
324                         } else {
325                                 // rollback due to failures in Main flow (Macro) - Full rollback
326                                 // WorkflowException = null
327                             if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
328                                         execution.setVariable("rollbackSuccessful", true)
329                                         execution.setVariable("rollbackError", false)
330                             } else {
331                                     String exceptionMessage = "Network Create Rollback was not Successful. "
332                     msoLogger.debug(exceptionMessage)
333                                         execution.setVariable("rollbackSuccessful", false)
334                                     execution.setVariable("rollbackError", true)
335                                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
336                                         throw new BpmnError("MSOWorkflowException")
337                             }
338
339                         }
340
341
342                 } catch (Exception ex) {
343                         String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
344                         String exceptionMessage = " Bpmn error encountered in DoCreateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
345                         msoLogger.debug(exceptionMessage)
346                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
347
348                 }
349
350         }
351
352         // *******************************
353         //     Build Error Section
354         // *******************************
355
356
357
358         public void processJavaException(DelegateExecution execution){
359                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
360                 execution.setVariable("prefix",Prefix)
361
362                 try{
363                         msoLogger.debug("Caught a Java Exception in " + Prefix)
364                         msoLogger.debug("Started processJavaException Method")
365                         msoLogger.debug("Variables List: " + execution.getVariables())
366                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
367                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
368
369                 }catch(Exception e){
370                         msoLogger.debug("Caught Exception during processJavaException Method: " + e)
371                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
372                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
373                 }
374                 msoLogger.debug("Completed processJavaException Method in " + Prefix)
375         }
376
377 }