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