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