Removed MsoLogger class
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / vcpe / scripts / DoCreateAllottedResourceTXCRollback.groovy
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.vcpe.scripts
24
25 import org.onap.aai.domain.yang.AllottedResource;
26 import org.onap.so.bpmn.common.scripts.*;
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.onap.so.bpmn.core.WorkflowException
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
31 import org.onap.so.logger.ErrorCode
32 import org.camunda.bpm.engine.delegate.BpmnError
33 import org.camunda.bpm.engine.delegate.DelegateExecution
34
35 import static org.apache.commons.lang3.StringUtils.*
36
37 import org.onap.so.logger.MessageEnum
38 import org.slf4j.Logger
39 import org.slf4j.LoggerFactory
40
41 /**
42  * This groovy class supports the <class>CreateAllottedResourceTXCRollback.bpmn</class> process.
43  *
44  * @author
45  *
46  * Inputs:
47  * @param - msoRequestId
48  * @param - isDebugLogEnabled
49  * @param - disableRollback - O
50  * @param - rollbackData
51  *
52  * Outputs:
53  * @param - rollbackError
54  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
55  *
56  */
57 public class DoCreateAllottedResourceTXCRollback extends AbstractServiceTaskProcessor{
58         private static final Logger logger = LoggerFactory.getLogger(DoCreateAllottedResourceTXCRollback.class);
59
60         String Prefix="DCARTXCRB_"
61         ExceptionUtil exceptionUtil = new ExceptionUtil()
62
63         public void preProcessRequest (DelegateExecution execution) {
64
65
66                 String msg = ""
67                 logger.trace("start preProcessRequest")
68                 execution.setVariable("prefix", Prefix)
69                 String rbType = "DCARTXC_"
70                 try {
71
72                         def rollbackData = execution.getVariable("rollbackData")
73                         logger.debug("RollbackData:" + rollbackData)
74
75                         if (rollbackData != null) {
76                                 if (rollbackData.hasType(rbType)) {
77
78                                         execution.setVariable("serviceInstanceId", rollbackData.get(rbType, "serviceInstanceId"))
79                                         execution.setVariable("parentServiceInstanceId", rollbackData.get(rbType, "parentServiceInstanceId"))
80                                         execution.setVariable("allottedResourceId", rollbackData.get("SERVICEINSTANCE", "allottedResourceId"))
81
82
83                                         def rollbackAAI = rollbackData.get(rbType, "rollbackAAI")
84                                         if ("true".equals(rollbackAAI))
85                                         {
86                                                 execution.setVariable("rollbackAAI",true)
87                                                 execution.setVariable("aaiARPath", rollbackData.get(rbType, "aaiARPath"))
88
89                                         }
90                                         def rollbackSDNC = rollbackData.get(rbType, "rollbackSDNCassign")
91                                         if ("true".equals(rollbackSDNC))
92                                         {
93                                                 execution.setVariable("rollbackSDNC", true)
94                                                 execution.setVariable("deactivateSdnc", rollbackData.get(rbType, "rollbackSDNCactivate"))
95                                                 execution.setVariable("deleteSdnc",  rollbackData.get(rbType, "rollbackSDNCcreate"))
96                                                 execution.setVariable("unassignSdnc", rollbackData.get(rbType, "rollbackSDNCassign"))
97
98                                                 logger.debug("sdncDeactivate:\n" + execution.getVariable("deactivateSdnc") )
99                                                 logger.debug("sdncDelete:\n" + execution.getVariable("deleteSdnc"))
100                                                 logger.debug("sdncUnassign:\n" + execution.getVariable("unassignSdnc"))
101
102                                                 execution.setVariable("sdncDeactivateRequest", rollbackData.get(rbType, "sdncActivateRollbackReq"))
103                                                 execution.setVariable("sdncDeleteRequest",  rollbackData.get(rbType, "sdncCreateRollbackReq"))
104                                                 execution.setVariable("sdncUnassignRequest", rollbackData.get(rbType, "sdncAssignRollbackReq"))
105                                         }
106
107                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackSDNC") != true)
108                                         {
109                                                 execution.setVariable("skipRollback", true)
110                                         }
111                                 }
112                                 else {
113                                         execution.setVariable("skipRollback", true)
114                                 }
115                         }
116                         else {
117                                 execution.setVariable("skipRollback", true)
118                         }
119                         if (execution.getVariable("disableRollback").equals("true" ))
120                         {
121                                 execution.setVariable("skipRollback", true)
122                         }
123
124                 }catch(BpmnError b){
125                         logger.debug("Rethrowing MSOWorkflowException")
126                         throw b
127                 } catch (Exception ex){
128                         msg = "Exception in preProcessRequest " + ex.getMessage()
129                         logger.debug(msg)
130                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
131                 }
132                 logger.trace("end preProcessRequest")
133         }
134
135         // aaiARPath set during query (existing AR)
136         public void updateAaiAROrchStatus(DelegateExecution execution, String status){
137
138                 String msg = null;
139                 logger.trace("start updateAaiAROrchStatus")
140                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
141                 String aaiARPath  = execution.getVariable("aaiARPath")
142                 logger.debug(" aaiARPath:" + aaiARPath)
143                 Optional<AllottedResource> ar = Optional.empty(); //need this for getting resourceVersion for delete
144                 if (!isBlank(aaiARPath))
145                 {
146                         ar = arUtils.getARbyLink(execution, aaiARPath, "")
147                 }
148                 if (!ar.isPresent())
149                 {
150                         msg = "AR not found in AAI at:" + aaiARPath
151                         logger.debug(msg)
152                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
153                 }
154                 String orchStatus = arUtils.updateAROrchStatus(execution, status, aaiARPath)
155                 logger.trace("end updateAaiAROrchStatus")
156         }
157
158         public void validateSDNCResp(DelegateExecution execution, String response, String method){
159
160
161                 logger.trace("start ValidateSDNCResponse Process")
162                 String msg = ""
163
164                 try {
165                         WorkflowException workflowException = execution.getVariable("WorkflowException")
166                         logger.debug("workflowException: " + workflowException)
167
168                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
169                         logger.debug("SDNCResponse: " + response)
170
171                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
172                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
173
174                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
175                                 logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + response)
176
177                         }else{
178
179                                 logger.debug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.")
180                                 throw new BpmnError("MSOWorkflowException")
181                         }
182                 } catch (BpmnError e) {
183                         if ("404".contentEquals(e.getErrorCode()))
184                         {
185                                 msg = "SDNC rollback " + method + " returned a 404. Proceding with rollback"
186                                 logger.debug(msg)
187                         }
188                         else {
189                                 throw e;
190                         }
191                 } catch(Exception ex) {
192                         msg = "Exception in validateSDNCResp. " + ex.getMessage()
193                         logger.debug(msg)
194                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
195                 }
196                 logger.trace("Exit ValidateSDNCResp Process")
197         }
198
199         public void deleteAaiAR(DelegateExecution execution){
200
201                 try{
202                         logger.trace("start deleteAaiAR")
203                         AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
204                         String arLink = execution.getVariable("aaiARPath")
205                         arUtils.deleteAR(execution, arLink)
206                 } catch (BpmnError e) {
207                         throw e;
208                 }catch(Exception ex){
209                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
210                                              "Exception Occurred Processing preProcessSDNCGetRequest.", "BPMN",
211                                              ErrorCode.UnknownError.getValue(), "Exception is:\n" + ex);
212                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + ex.getMessage())
213                 }
214                 logger.trace("end deleteAaiAR")
215         }
216
217         public void postProcessRequest(DelegateExecution execution) {
218
219                 logger.trace("start postProcessRequest")
220                 String msg = ""
221                 try {
222                         execution.setVariable("rollbackData", null)
223                         boolean skipRollback = execution.getVariable("skipRollback")
224                         if (skipRollback != true)
225                         {
226                                 execution.setVariable("rolledBack", true)
227                                 logger.debug("rolledBack")
228                         }
229                         logger.trace("end postProcessRequest")
230
231                 } catch (BpmnError e) {
232                         logger.debug(msg)
233                 } catch (Exception ex) {
234                         msg = "Exception in postProcessRequest. " + ex.getMessage()
235                         logger.debug(msg)
236                 }
237
238         }
239
240         public void processRollbackException(DelegateExecution execution){
241
242                 logger.trace("start processRollbackException")
243                 try{
244                         logger.debug("Caught an Exception in DoCreateAllottedResourceRollback")
245                         execution.setVariable("rollbackData", null)
246                         execution.setVariable("rolledBack", false)
247                         execution.setVariable("rollbackError", "Caught exception in AllottedResource Create Rollback")
248                         execution.setVariable("WorkflowException", null)
249
250                 }catch(BpmnError b){
251                         logger.debug("BPMN Error during processRollbackExceptions Method: ")
252                 }catch(Exception e){
253                         logger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
254                 }
255
256                 logger.trace("end processRollbackException")
257         }
258
259         public void processRollbackJavaException(DelegateExecution execution){
260
261                 logger.trace("start processRollbackJavaException")
262                 try{
263                         execution.setVariable("rollbackData", null)
264                         execution.setVariable("rolledBack", false)
265                         execution.setVariable("rollbackError", "Caught Java exception in AllottedResource Create Rollback")
266                         logger.debug("Caught Exception in processRollbackJavaException")
267
268                 }catch(Exception e){
269                         logger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
270                 }
271                 logger.trace("end processRollbackJavaException")
272         }
273
274 }