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