Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoUpdateNetworkInstanceRollback.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.onap.so.bpmn.infrastructure.scripts;
22
23 import groovy.xml.XmlUtil
24 import groovy.json.*
25
26 import org.onap.so.bpmn.common.scripts.AaiUtil
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.onap.so.bpmn.common.scripts.NetworkUtils
30 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
31 import org.onap.so.bpmn.common.scripts.VidUtils
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.bpmn.core.UrnPropertiesReader
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.onap.so.rest.APIResponse
36
37 import java.util.UUID;
38
39 import org.camunda.bpm.engine.delegate.BpmnError
40 import org.camunda.bpm.engine.delegate.DelegateExecution
41 import org.apache.commons.lang3.*
42 import org.apache.commons.codec.binary.Base64;
43 import org.springframework.web.util.UriUtils
44
45 import org.onap.so.logger.MessageEnum
46 import org.onap.so.logger.MsoLogger
47
48 /**
49  * This groovy class supports the <class>DoCreateNetworkInstance.bpmn</class> process.
50  *
51  */
52 public class DoUpdateNetworkInstanceRollback extends AbstractServiceTaskProcessor {
53         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoUpdateNetworkInstanceRollback.class);
54         
55         String Prefix="UPDNETIR_"
56         ExceptionUtil exceptionUtil = new ExceptionUtil()
57         JsonUtils jsonUtil = new JsonUtils()
58         VidUtils vidUtils = new VidUtils(this)
59         NetworkUtils networkUtils = new NetworkUtils()
60         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
61
62         def className = getClass().getSimpleName()
63
64         /**
65          * This method is executed during the preProcessRequest task of the <class>DoUpdateNetworkInstanceRollback.bpmn</class> process.
66          * @param execution
67          */
68         public InitializeProcessVariables(DelegateExecution execution){
69                 /* Initialize all the process variables in this block */
70
71                 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
72                 execution.setVariable(Prefix + "rollbackSDNCRequest", null)
73                 execution.setVariable(Prefix + "WorkflowException", null)
74
75                 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
76                 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
77                 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
78
79                 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
80                 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
81                 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
82
83                 execution.setVariable(Prefix + "Success", false)
84                 execution.setVariable(Prefix + "fullRollback", false)
85
86         }
87
88         // **************************************************
89         //     Pre or Prepare Request Section
90         // **************************************************
91         /**
92          * This method is executed during the preProcessRequest task of the <class>DoUpdateNetworkInstanceRollback.bpmn</class> process.
93          * @param execution
94          */
95         public void preProcessRequest (DelegateExecution execution) {
96
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
109                         // Partial Rollback
110                         Map<String, String> rollbackData = execution.getVariable("rollbackData")
111                         if (rollbackData != null && rollbackData instanceof Map) {
112
113                                         if(rollbackData.containsKey("rollbackNetworkRequest")) {
114                                                 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
115                                         }
116
117                                         if(rollbackData.containsKey("rollbackSDNCRequest")) {
118                                            rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
119                                         }
120                         }
121
122                         execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
123                         execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
124                         msoLogger.debug("'rollbackData': " + '\n' + execution.getVariable("rollbackData"))
125
126                         String sdncVersion = execution.getVariable("sdncVersion")
127                         msoLogger.debug("sdncVersion? : " + sdncVersion)
128
129                         // PO Authorization Info / headers Authorization=
130                         String basicAuthValuePO = UrnPropertiesReader.getVariable("mso.adapters.po.auth",execution)
131                         
132                         try {
133                                 def encodedString = utils.getBasicAuth(basicAuthValuePO, UrnPropertiesReader.getVariable("mso.msoKey", execution))
134                                 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
135                                 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
136
137                         } catch (IOException ex) {
138                                 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
139                                 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
140                                 msoLogger.debug(dataErrorMessage )
141                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
142                         }
143
144                         if (execution.getVariable("SavedWorkflowException1") != null) {
145                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
146                         } else {
147                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
148                         }
149                         msoLogger.debug("*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"))
150                         if(execution.getVariable(Prefix + "WorkflowException") != null) {
151                                 // called by: DoCreateNetworkInstance, partial rollback
152                                 execution.setVariable(Prefix + "fullRollback", false)
153
154                         } else {
155                            // called by: Macro - Full Rollback, WorkflowException = null
156                            execution.setVariable(Prefix + "fullRollback", true)
157
158                         }
159                         msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
160
161
162                 } catch (BpmnError e) {
163                 throw e;
164
165                 } catch (Exception ex) {
166                         sendSyncError(execution)
167                         // caught exception
168                         String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
169                         msoLogger.debug(exceptionMessage)
170                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
171
172                 }
173
174         }
175
176         public void validateRollbackResponses (DelegateExecution execution) {
177
178                 execution.setVariable("prefix",Prefix)
179
180                 msoLogger.trace("Inside validateRollbackResponses() of DoUpdateNetworkInstanceRollback ")
181
182                 try {
183                         // validate PO network rollback response
184                         String rollbackNetworkErrorMessages = ""
185                         String rollbackNetworkReturnCode = "200"
186                         if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
187                                 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
188                                 String rollbackNetworkResponse = execution.getVariable(Prefix + "rollbackNetworkResponse")
189                                 msoLogger.debug(" NetworkRollback Code - " + rollbackNetworkReturnCode)
190                                 msoLogger.debug(" NetworkRollback Response - " + rollbackNetworkResponse)
191                                 if (rollbackNetworkReturnCode != "200") {
192                                         rollbackNetworkErrorMessages = " + PO Network rollback failed. "
193                                 } else {
194                                         rollbackNetworkErrorMessages = " + PO Network rollback completed."
195                                 }
196                         }
197
198                         // validate SDNC rollback response
199                         String rollbackSdncErrorMessages = ""
200                         String rollbackSDNCReturnCode = "200"
201                         if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
202                                 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
203                                 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
204                                 String rollbackSDNCReturnInnerCode = ""
205                                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
206                                 rollbackSDNCResponse = rollbackSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
207                                 if (rollbackSDNCReturnCode == "200") {
208                                         if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
209                                                 rollbackSDNCReturnInnerCode = utils.getNodeText(rollbackSDNCResponse, "response-code")
210                                                 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
211                                                     rollbackSdncErrorMessages = " + SNDC changeassign rollback completed."
212                                                 } else {
213                                                         rollbackSdncErrorMessages = " + SDNC changeassign rollback failed. "
214                                                 }
215                                         } else {
216                                                   rollbackSdncErrorMessages = " + SNDC changeassign rollback completed."
217                                         }
218                                  } else {
219                                           rollbackSdncErrorMessages = " + SDNC changeassign rollback failed. "
220                              }
221                                 msoLogger.debug(" SDNC changeassign rollback Code - " + rollbackSDNCReturnCode)
222                                 msoLogger.debug(" SDNC changeassign rollback  Response - " + rollbackSDNCResponse)
223                         }
224
225                         String statusMessage = ""
226                         int errorCode = 7000
227                         msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
228                         if (execution.getVariable(Prefix + "fullRollback") == false) {
229                                 // original WorkflowException,
230                                 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException")
231                                 if (wfe != null) {
232                                    // rollback due to failure in DoCreate - Partial rollback
233                                    statusMessage = wfe.getErrorMessage()
234                                    errorCode = wfe.getErrorCode()
235                                 } else {
236                                    statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
237                                    errorCode = '7000'
238                                 }
239
240                                 // set if all rolledbacks are successful
241                                 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200") {
242                                         execution.setVariable("rolledBack", true)
243
244                                 } else {
245                                         execution.setVariable("rolledBack", false)
246
247                                 }
248
249                                 statusMessage = statusMessage + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
250                                 msoLogger.debug("Final DoUpdateNetworkInstanceRollback status message: " + statusMessage)
251                                 String processKey = getProcessKey(execution);
252                                 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
253                                 execution.setVariable("workflowException", exception);
254
255                         } else {
256                                 // rollback due to failures in Main flow (Macro) - Full rollback
257                                 // WorkflowException = null
258                                 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200") {
259                                         execution.setVariable("rollbackSuccessful", true)
260                                         execution.setVariable("rollbackError", false)
261                                 } else {
262                                         String exceptionMessage = "Network Update Rollback was not Successful. "
263                                         msoLogger.debug(exceptionMessage)
264                                         execution.setVariable("rollbackSuccessful", false)
265                                         execution.setVariable("rollbackError", true)
266                                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
267                                         throw new BpmnError("MSOWorkflowException")
268                                 }
269
270                         }
271
272
273                 } catch (Exception ex) {
274                         execution.setVariable("WorkflowException", null)
275                         String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
276                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
277                         msoLogger.debug(exceptionMessage)
278                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
279                         execution.setVariable("WorkflowException", execution.getVariable("WorkflowException"))
280
281                 }
282
283         }
284
285         // *******************************
286         //     Build Error Section
287         // *******************************
288
289
290
291         public void processJavaException(DelegateExecution execution){
292
293                 execution.setVariable("prefix",Prefix)
294
295                 try{
296                         msoLogger.debug("Caught a Java Exception in " + Prefix)
297                         msoLogger.debug("Started processJavaException Method")
298                         msoLogger.debug("Variables List: " + execution.getVariables())
299                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
300                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
301
302                 }catch(Exception e){
303                         msoLogger.debug("Caught Exception during processJavaException Method: " + e)
304                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
305                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
306                 }
307                 msoLogger.trace("Completed processJavaException Method in " + Prefix)
308         }
309
310 }