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