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