14277301879ff7a08ba17d2e399a769462576b68
[so.git] /
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.so.logger.LoggingAnchor
26 import org.onap.aai.domain.yang.AllottedResource;
27 import org.onap.so.bpmn.common.scripts.*;
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.core.WorkflowException
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
32 import org.onap.so.logger.ErrorCode
33 import org.camunda.bpm.engine.delegate.BpmnError
34 import org.camunda.bpm.engine.delegate.DelegateExecution
35
36 import static org.apache.commons.lang3.StringUtils.*
37
38 import org.onap.so.logger.MessageEnum
39 import org.slf4j.Logger
40 import org.slf4j.LoggerFactory
41
42 /**
43  * This groovy class supports the <class>CreateAllottedResourceTXCRollback.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 DoCreateAllottedResourceTXCRollback extends AbstractServiceTaskProcessor{
59         private static final Logger logger = LoggerFactory.getLogger(DoCreateAllottedResourceTXCRollback.class);
60
61         String Prefix="DCARTXCRB_"
62         ExceptionUtil exceptionUtil = new ExceptionUtil()
63
64         public void preProcessRequest (DelegateExecution execution) {
65
66
67                 String msg = ""
68                 logger.trace("start preProcessRequest")
69                 execution.setVariable("prefix", Prefix)
70                 String rbType = "DCARTXC_"
71                 try {
72
73                         def rollbackData = execution.getVariable("rollbackData")
74                         logger.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                                                 logger.debug("sdncDeactivate:\n" + execution.getVariable("deactivateSdnc") )
100                                                 logger.debug("sdncDelete:\n" + execution.getVariable("deleteSdnc"))
101                                                 logger.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                         logger.debug("Rethrowing MSOWorkflowException")
127                         throw b
128                 } catch (Exception ex){
129                         msg = "Exception in preProcessRequest " + ex.getMessage()
130                         logger.debug(msg)
131                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
132                 }
133                 logger.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                 logger.trace("start updateAaiAROrchStatus")
141                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
142                 String aaiARPath  = execution.getVariable("aaiARPath")
143                 logger.debug(" aaiARPath:" + aaiARPath)
144                 Optional<AllottedResource> ar = Optional.empty(); //need this for getting resourceVersion for delete
145                 if (!isBlank(aaiARPath))
146                 {
147                         ar = arUtils.getARbyLink(execution, aaiARPath, "")
148                 }
149                 if (!ar.isPresent())
150                 {
151                         msg = "AR not found in AAI at:" + aaiARPath
152                         logger.debug(msg)
153                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
154                 }
155                 String orchStatus = arUtils.updateAROrchStatus(execution, status, aaiARPath)
156                 logger.trace("end updateAaiAROrchStatus")
157         }
158
159         public void validateSDNCResp(DelegateExecution execution, String response, String method){
160
161
162                 logger.trace("start ValidateSDNCResponse Process")
163                 String msg = ""
164
165                 try {
166                         WorkflowException workflowException = execution.getVariable("WorkflowException")
167                         logger.debug("workflowException: " + workflowException)
168
169                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
170                         logger.debug("SDNCResponse: " + response)
171
172                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
173                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
174
175                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
176                                 logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + response)
177
178                         }else{
179
180                                 logger.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                                 logger.debug(msg)
188                         }
189                         else {
190                                 throw e;
191                         }
192                 } catch(Exception ex) {
193                         msg = "Exception in validateSDNCResp. " + ex.getMessage()
194                         logger.debug(msg)
195                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
196                 }
197                 logger.trace("Exit ValidateSDNCResp Process")
198         }
199
200         public void deleteAaiAR(DelegateExecution execution){
201
202                 try{
203                         logger.trace("start deleteAaiAR")
204                         AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
205                         String arLink = execution.getVariable("aaiARPath")
206                         arUtils.deleteAR(execution, arLink)
207                 } catch (BpmnError e) {
208                         throw e;
209                 }catch(Exception ex){
210                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
211                                              "Exception Occurred Processing preProcessSDNCGetRequest.", "BPMN",
212                                              ErrorCode.UnknownError.getValue(), "Exception is:\n" + ex);
213                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + ex.getMessage())
214                 }
215                 logger.trace("end deleteAaiAR")
216         }
217
218         public void postProcessRequest(DelegateExecution execution) {
219
220                 logger.trace("start postProcessRequest")
221                 String msg = ""
222                 try {
223                         execution.setVariable("rollbackData", null)
224                         boolean skipRollback = execution.getVariable("skipRollback")
225                         if (skipRollback != true)
226                         {
227                                 execution.setVariable("rolledBack", true)
228                                 logger.debug("rolledBack")
229                         }
230                         logger.trace("end postProcessRequest")
231
232                 } catch (BpmnError e) {
233                         logger.debug(msg)
234                 } catch (Exception ex) {
235                         msg = "Exception in postProcessRequest. " + ex.getMessage()
236                         logger.debug(msg)
237                 }
238
239         }
240
241         public void processRollbackException(DelegateExecution execution){
242
243                 logger.trace("start processRollbackException")
244                 try{
245                         logger.debug("Caught an Exception in DoCreateAllottedResourceRollback")
246                         execution.setVariable("rollbackData", null)
247                         execution.setVariable("rolledBack", false)
248                         execution.setVariable("rollbackError", "Caught exception in AllottedResource Create Rollback")
249                         execution.setVariable("WorkflowException", null)
250
251                 }catch(BpmnError b){
252                         logger.debug("BPMN Error during processRollbackExceptions Method: ")
253                 }catch(Exception e){
254                         logger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
255                 }
256
257                 logger.trace("end processRollbackException")
258         }
259
260         public void processRollbackJavaException(DelegateExecution execution){
261
262                 logger.trace("start processRollbackJavaException")
263                 try{
264                         execution.setVariable("rollbackData", null)
265                         execution.setVariable("rolledBack", false)
266                         execution.setVariable("rollbackError", "Caught Java exception in AllottedResource Create Rollback")
267                         logger.debug("Caught Exception in processRollbackJavaException")
268
269                 }catch(Exception e){
270                         logger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
271                 }
272                 logger.trace("end processRollbackJavaException")
273         }
274
275 }