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