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