313e2f4cb70a196917b90eb231d19fb8be5bc2f7
[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     //     Pre or Prepare Request Section
92     // **************************************************
93     /**
94      * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
95      * @param execution
96      */
97     public void preProcessRequest (DelegateExecution execution) {
98         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
99         execution.setVariable("prefix",Prefix)
100
101         logger.trace("Inside preProcessRequest() of " + className + ".groovy")
102
103         try {
104             // initialize flow variables
105             InitializeProcessVariables(execution)
106
107             // GET Incoming request/variables
108             String rollbackNetworkRequest = null
109             String rollbackSDNCRequest = null
110             String rollbackActivateSDNCRequest = null
111
112             // Partial Rollback
113             Map<String, String> rollbackData = execution.getVariable("rollbackData")
114             if (rollbackData != null && rollbackData instanceof Map) {
115
116                 if(rollbackData.containsKey("rollbackSDNCRequest")) {
117                     rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
118                 }
119
120                 if(rollbackData.containsKey("rollbackNetworkRequest")) {
121                     rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
122                 }
123
124                 if(rollbackData.containsKey("rollbackActivateSDNCRequest")) {
125                     rollbackActivateSDNCRequest = rollbackData["rollbackActivateSDNCRequest"]
126                 }
127
128             }
129
130             execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
131             execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
132             execution.setVariable(Prefix + "rollbackActivateSDNCRequest", rollbackActivateSDNCRequest)
133             logger.debug("'rollbackData': " + '\n' + execution.getVariable("rollbackData"))
134
135             String sdncVersion = execution.getVariable("sdncVersion")
136             logger.debug("sdncVersion? : " + sdncVersion)
137
138             // PO Authorization Info / headers Authorization=
139             String basicAuthValuePO = UrnPropertiesReader.getVariable("mso.adapters.po.auth",execution)
140             try {
141                 def encodedString = utils.getBasicAuth(basicAuthValuePO, UrnPropertiesReader.getVariable("mso.msoKey", execution))
142                 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
143                 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
144
145             } catch (IOException ex) {
146                 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
147                 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
148                 logger.debug(dataErrorMessage )
149                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
150             }
151
152             if (execution.getVariable("SavedWorkflowException1") != null) {
153                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
154             } else {
155                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
156             }
157             logger.debug("*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"))
158             if(execution.getVariable(Prefix + "WorkflowException") != null) {
159                 // called by: DoCreateNetworkInstance, partial rollback
160                 execution.setVariable(Prefix + "fullRollback", false)
161
162             } else {
163                 // called by: Macro - Full Rollback, WorkflowException = null
164                 execution.setVariable(Prefix + "fullRollback", true)
165
166             }
167             logger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
168
169
170         } catch (BpmnError e) {
171             throw e;
172
173         } catch (Exception ex) {
174             // caught exception
175             String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
176             logger.debug(exceptionMessage)
177             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
178
179         }
180
181     }
182
183     public void setNetworkAdapterResponseCode (DelegateExecution execution) {
184         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
185         execution.setVariable("prefix",Prefix)
186         try {
187
188             execution.setVariable(Prefix + "rollbackNetworkReturnCode", "200")
189
190         } catch (Exception ex) {
191             String exceptionMessage = "Exception Encountered in callPONetworkAdapter() of DoCreateNetworkInstanceRollback flow - " + ex.getMessage()
192             logger.debug(exceptionMessage)
193             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
194         }
195
196     }
197
198
199     public void validateRollbackResponses (DelegateExecution execution) {
200         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
201         execution.setVariable("prefix",Prefix)
202
203         logger.trace("Inside validateRollbackResponses() of DoCreateNetworkInstanceRollback")
204
205         try {
206             // validate PO network rollback response
207             String rollbackNetworkErrorMessages = ""
208             String rollbackNetworkReturnCode = "200"
209             if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
210                 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
211                 logger.debug(" NetworkRollback Code - " + rollbackNetworkReturnCode)
212                 if (rollbackNetworkReturnCode != "200") {
213                     rollbackNetworkErrorMessages = " + PO Network rollback failed. "
214                 } else {
215                     rollbackNetworkErrorMessages = " + PO Network rollback completed."
216                 }
217             }
218
219             // validate SDNC rollback response
220             String rollbackSdncErrorMessages = ""
221             String rollbackSDNCReturnCode = "200"
222             if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
223                 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
224                 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
225                 String rollbackSDNCReturnInnerCode = ""
226                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
227                 rollbackSDNCResponse = rollbackSDNCResponse
228                 rollbackSDNCResponse = rollbackSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
229                 if (rollbackSDNCReturnCode == "200") {
230                     if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
231                         rollbackSDNCReturnInnerCode = utils.getNodeText(rollbackSDNCResponse, "response-code")
232                         if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
233                             rollbackSdncErrorMessages = " + SNDC assign rollback completed."
234                         } else {
235                             rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
236                         }
237                     } else {
238                         rollbackSdncErrorMessages = " + SNDC assign rollback completed."
239                     }
240                 } else {
241                     rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
242                 }
243                 logger.debug(" SDNC assign rollback Code - " + rollbackSDNCReturnCode)
244                 logger.debug(" SDNC assign rollback  Response - " + rollbackSDNCResponse)
245             }
246
247             // validate SDNC activate rollback response
248             String rollbackActivateSdncErrorMessages = ""
249             String rollbackActivateSDNCReturnCode = "200"
250             if (execution.getVariable(Prefix + "rollbackActivateSDNCRequest") != null) {
251                 rollbackActivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackActivateSDNCReturnCode")
252                 String rollbackActivateSDNCResponse = execution.getVariable(Prefix + "rollbackActivateSDNCResponse")
253                 String rollbackActivateSDNCReturnInnerCode = ""
254                 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse
255                 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
256                 if (rollbackActivateSDNCReturnCode == "200") {
257                     if (utils.nodeExists(rollbackActivateSDNCResponse, "response-code")) {
258                         rollbackActivateSDNCReturnInnerCode = utils.getNodeText(rollbackActivateSDNCResponse, "response-code")
259                         if (rollbackActivateSDNCReturnInnerCode == "200" || rollbackActivateSDNCReturnInnerCode == "" || rollbackActivateSDNCReturnInnerCode == "0") {
260                             rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
261                         } else {
262                             rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
263                         }
264                     } else {
265                         rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
266                     }
267                 } else {
268                     rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
269                 }
270                 logger.debug(" SDNC activate rollback Code - " + rollbackActivateSDNCReturnCode)
271                 logger.debug(" SDNC activate rollback  Response - " + rollbackActivateSDNCResponse)
272             }
273
274
275             String statusMessage = ""
276             int errorCode = 7000
277             logger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
278             if (execution.getVariable(Prefix + "fullRollback") == false) {
279                 // original WorkflowException,
280                 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException")
281                 if (wfe != null) {
282                     // rollback due to failure in DoCreate - Partial rollback
283                     statusMessage = wfe.getErrorMessage()
284                     errorCode = wfe.getErrorCode()
285                 } else {
286                     statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
287                     errorCode = '7000'
288                 }
289
290                 // set if all rolledbacks are successful
291                 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
292                     execution.setVariable("rolledBack", true)
293                     execution.setVariable("wasDeleted", true)
294
295                 } else {
296                     execution.setVariable("rolledBack", false)
297                     execution.setVariable("wasDeleted", true)
298                 }
299
300                 statusMessage = statusMessage + rollbackActivateSdncErrorMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
301                 logger.debug("Final DoCreateNetworkInstanceRollback status message: " + statusMessage)
302                 String processKey = getProcessKey(execution);
303                 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
304                 execution.setVariable("workflowException", exception);
305
306             } else {
307                 // rollback due to failures in Main flow (Macro) - Full rollback
308                 // WorkflowException = null
309                 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
310                     execution.setVariable("rollbackSuccessful", true)
311                     execution.setVariable("rollbackError", false)
312                 } else {
313                     String exceptionMessage = "Network Create Rollback was not Successful. "
314                     logger.debug(exceptionMessage)
315                     execution.setVariable("rollbackSuccessful", false)
316                     execution.setVariable("rollbackError", true)
317                     exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
318                     throw new BpmnError("MSOWorkflowException")
319                 }
320
321             }
322
323
324         } catch (Exception ex) {
325             String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
326             String exceptionMessage = " Bpmn error encountered in DoCreateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
327             logger.debug(exceptionMessage)
328             exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
329
330         }
331
332     }
333
334     // *******************************
335     //     Build Error Section
336     // *******************************
337
338
339
340     public void processJavaException(DelegateExecution execution){
341         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
342         execution.setVariable("prefix",Prefix)
343
344         try{
345             logger.debug("Caught a Java Exception in " + Prefix)
346             logger.debug("Started processJavaException Method")
347             logger.debug("Variables List: " + execution.getVariables())
348             execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
349             exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
350
351         }catch(Exception e){
352             logger.debug("Caught Exception during processJavaException Method: " + e)
353             execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
354             exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
355         }
356         logger.debug("Completed processJavaException Method in " + Prefix)
357     }
358
359 }