Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / vcpe / scripts / DoDeleteAllottedResourceTXC.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 package org.openecomp.mso.bpmn.vcpe.scripts;
21
22 import org.openecomp.mso.bpmn.common.scripts.*;
23 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
24 import org.openecomp.mso.bpmn.core.WorkflowException
25 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
26 import org.openecomp.mso.bpmn.common.scripts.MsoUtils
27 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
28 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
29 import org.openecomp.mso.rest.APIResponse
30
31 import java.util.UUID;
32 import org.camunda.bpm.engine.delegate.BpmnError
33 import org.camunda.bpm.engine.delegate.DelegateExecution
34 import org.apache.commons.lang3.*
35 import org.springframework.web.util.UriUtils;
36 import static org.apache.commons.lang3.StringUtils.*
37
38 /**
39  * This groovy class supports the <class>DoDeleteAllottedResourceTXC.bpmn</class> process.
40  *
41  * @author
42  * 
43  * Inputs:
44  * @param - msoRequestId
45  * @param - isDebugLogEnabled
46  * @param - disableRollback - O ignored
47  * @param - failNotfound  - O 
48  * @param - serviceInstanceId
49  * @param - globalCustomerId - O
50  * @param - subscriptionServiceType - O
51  * @param - parentServiceInstanceId
52  * @param - allottedResourceId 
53  *
54  * Outputs:
55  * @param - rollbackData - N/A
56  * @param - rolledBack - true if no deletions performed
57  * @param - WorkflowException - O
58  * @param - wasDeleted - O (ie not silentSuccess)
59  *
60  */
61 public class DoDeleteAllottedResourceTXC extends AbstractServiceTaskProcessor{
62
63         private static final String DebugFlag = "isDebugLogEnabled"
64
65         String Prefix="DDARTXC_"
66         ExceptionUtil exceptionUtil = new ExceptionUtil()
67
68         public void preProcessRequest (DelegateExecution execution) {
69
70                 def isDebugEnabled = execution.getVariable(DebugFlag)
71                 String msg = ""
72                 utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)
73
74                 try {
75                         execution.setVariable("prefix", Prefix)
76
77                         //Config Inputs
78                         String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
79                         if (isBlank(sdncCallbackUrl)) {
80                                 msg = "URN_mso_workflow_sdncadapter_callback is null"
81                                 utils.log("DEBUG", msg, isDebugEnabled)
82                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
83                         }
84                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
85                         utils.log("DEBUG","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
86
87                         //Request Inputs
88                         if (isBlank(execution.getVariable("serviceInstanceId"))){
89                                 msg = "Input serviceInstanceId is null"
90                                 utils.log("DEBUG", msg, isDebugEnabled)
91                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
92                         }
93                         if (isBlank(execution.getVariable("allottedResourceId"))){
94                                 msg = "Input allottedResourceId is null"
95                                 utils.log("DEBUG", msg, isDebugEnabled)
96                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
97                         }
98
99                 }catch(BpmnError b){
100                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
101                         throw b
102                 } catch (Exception ex){
103                         msg = "Exception in preProcessRequest " + ex.getMessage()
104                         utils.log("DEBUG", msg, isDebugEnabled)
105                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
106                 }
107                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
108         }
109
110         public void getAaiAR (DelegateExecution execution) {
111
112                 def isDebugEnabled = execution.getVariable(DebugFlag)
113                 utils.log("DEBUG"," ***** getAaiAR ***** ", isDebugEnabled)
114
115                 String allottedResourceId = execution.getVariable("allottedResourceId")
116
117                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
118                 String ar = arUtils.getARbyId(execution, allottedResourceId)
119
120                 String errorMsg = ""
121                 if (isBlank(ar)) // AR was !found
122                 {
123                         errorMsg = "Allotted resource not found in AAI with AllottedResourceId:" + allottedResourceId
124                 }
125                 else
126                 {
127                         String aaiARPath = execution.getVariable("aaiARPath")
128                         String parentServiceInstanceId = arUtils.getPSIFmARLink(execution, aaiARPath)
129                         execution.setVariable("parentServiceInstanceId", parentServiceInstanceId)
130                 }
131                 if (!isBlank(errorMsg)) {
132                         utils.log("DEBUG", errorMsg, isDebugEnabled)
133                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, errorMsg)
134                 }
135                 utils.log("DEBUG"," ***** getAaiAR *****",  isDebugEnabled)
136
137         }
138
139         // aaiARPath set during query (existing AR)
140         public void updateAaiAROrchStatus(DelegateExecution execution, String status){
141                 def isDebugEnabled = execution.getVariable(DebugFlag)
142                 utils.log("DEBUG", " *** updateAaiAROrchStatus *** ", isDebugEnabled)
143                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
144                 String aaiARPath = execution.getVariable("aaiARPath") //set during query (existing AR) 
145                 String orchStatus = arUtils.updateAROrchStatus(execution, status, aaiARPath)
146                 utils.log("DEBUG", " *** Exit updateAaiAROrchStatus *** ", isDebugEnabled)
147         }
148
149         public String buildSDNCRequest(DelegateExecution execution, String action, String sdncRequestId) {
150
151                 def isDebugEnabled = execution.getVariable(DebugFlag)
152                 String msg = ""
153                 utils.log("DEBUG"," ***** buildSDNCRequest *****", isDebugEnabled)
154                 String sdncReq = null
155
156                 try {
157
158                         String allottedResourceId = execution.getVariable("allottedResourceId")
159                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
160                         String parentServiceInstanceId = execution.getVariable("parentServiceInstanceId")
161                         String globalCustomerId = execution.getVariable("globalCustomerId")
162                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
163
164                         String callbackUrl = execution.getVariable("sdncCallbackUrl")
165                         String requestId = execution.getVariable("msoRequestId")
166
167                         String serviceChainServiceInstanceId = ""
168                         String sourceNetworkId = ""
169                         String sourceNetworkRole = ""
170                         String allottedResourceRole = ""
171
172                         String arModelInfo = ""
173                         String modelInvariantId = ""
174                         String modelVersion = ""
175                         String modelUUId = ""
176                         String modelCustomizationId = ""
177                         String modelName = ""
178
179
180                         sdncReq =
181                                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
182                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
183                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
184                                    <sdncadapter:RequestHeader>
185                                                         <sdncadapter:RequestId>${sdncRequestId}</sdncadapter:RequestId>
186                                                         <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>
187                                                         <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
188                                                         <sdncadapter:SvcOperation>tunnelxconn-topology-operation</sdncadapter:SvcOperation>
189                                                         <sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
190                                         </sdncadapter:RequestHeader>
191                                 <sdncadapterworkflow:SDNCRequestData>
192                                         <request-information>
193                                                 <request-id>${requestId}</request-id>
194                                                 <request-action>DeleteTunnelXConnInstance</request-action>
195                                                 <source>MSO</source>
196                                                 <notification-url/>
197                                                 <order-number/>
198                                                 <order-version/>
199                                         </request-information>
200                                         <service-information>
201                                                 <service-id></service-id>
202                                                 <subscription-service-type>${subscriptionServiceType}</subscription-service-type>
203                                                 <onap-model-information></onap-model-information>
204                                                 <service-instance-id>${serviceInstanceId}</service-instance-id>
205                                                 <subscriber-name/>
206                                                 <global-customer-id>${globalCustomerId}</global-customer-id>
207                                         </service-information>
208                                         <allotted-resource-information>
209                                                 <allotted-resource-id>${allottedResourceId}</allotted-resource-id>    
210                                                 <allotted-resource-type>tunnelxconn</allotted-resource-type>
211                                                 <parent-service-instance-id>${parentServiceInstanceId}</parent-service-instance-id>   
212                                                 <onap-model-information>
213                                                         <model-invariant-uuid>${modelInvariantId}</model-invariant-uuid>
214                                                         <model-uuid>${modelUUId}</model-uuid>
215                                                         <model-customization-uuid>${modelCustomizationId}</model-customization-uuid>
216                                                         <model-version>${modelVersion}</model-version>
217                                                         <model-name>${modelName}</model-name>
218                                                 </onap-model-information>
219                                         </allotted-resource-information>
220                                         <tunnelxconn-request-input>
221                                         </tunnelxconn-request-input>
222                                 </sdncadapterworkflow:SDNCRequestData>
223                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
224
225                         utils.log("DEBUG","sdncRequest:\n" + sdncReq, isDebugEnabled)
226                         sdncReq = utils.formatXml(sdncReq)
227
228                 } catch(Exception ex) {
229                         msg = "Exception in buildSDNCRequest. " + ex.getMessage()
230                         utils.log("DEBUG", msg, isDebugEnabled)
231                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
232                 }
233                 utils.log("DEBUG"," *****Exit buildSDNCRequest *****", isDebugEnabled)
234                 return sdncReq
235         }
236
237         public void preProcessSDNCUnassign(DelegateExecution execution) {
238
239                 def isDebugEnabled = execution.getVariable(DebugFlag)
240                 String msg = ""
241                 utils.log("DEBUG"," ***** preProcessSDNCUnassign *****", isDebugEnabled)
242
243                 try {
244                         String sdncRequestId = UUID.randomUUID().toString()
245                         String sdncUnassignReq = buildSDNCRequest(execution, "unassign", sdncRequestId)
246                         execution.setVariable("sdncUnassignRequest", sdncUnassignReq)
247                         utils.logAudit("sdncUnassignRequest:  " + sdncUnassignReq)
248                 } catch (BpmnError e) {
249                         throw e;
250                 } catch(Exception ex) {
251                         msg = "Exception in preProcessSDNCUnassign. " + ex.getMessage()
252                         utils.log("DEBUG", msg, isDebugEnabled)
253                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
254                 }
255                 utils.log("DEBUG"," *****Exit preProcessSDNCUnassign *****", isDebugEnabled)
256         }
257
258         public void preProcessSDNCDelete(DelegateExecution execution) {
259
260                 def isDebugEnabled = execution.getVariable(DebugFlag)
261                 String msg = ""
262                 utils.log("DEBUG"," ***** preProcessSDNCDelete *****", isDebugEnabled)
263
264                 try {
265                         String sdncRequestId = UUID.randomUUID().toString()
266                         String sdncDeleteReq = buildSDNCRequest(execution, "delete", sdncRequestId)
267                         execution.setVariable("sdncDeleteRequest", sdncDeleteReq)
268                         utils.logAudit("sdncDeleteReq:  " + sdncDeleteReq)
269                 } catch (BpmnError e) {
270                         throw e;
271                 } catch(Exception ex) {
272                         msg = "Exception in preProcessSDNCDelete. " + ex.getMessage()
273                         utils.log("DEBUG", msg, isDebugEnabled)
274                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
275                 }
276                 utils.log("DEBUG"," *****Exit preProcessSDNCDelete *****", isDebugEnabled)
277         }
278
279         public void preProcessSDNCDeactivate(DelegateExecution execution) {
280
281                 def isDebugEnabled = execution.getVariable(DebugFlag)
282                 String msg = ""
283                 utils.log("DEBUG"," ***** preProcessSDNCDeactivate *****", isDebugEnabled)
284
285                 try {
286                         String sdncRequestId = UUID.randomUUID().toString()
287                         String sdncDeactivateReq = buildSDNCRequest(execution, "deactivate", sdncRequestId)
288                         execution.setVariable("sdncDeactivateRequest", sdncDeactivateReq)
289                         utils.logAudit("sdncDeactivateReq:  " + sdncDeactivateReq)
290                 } catch (BpmnError e) {
291                         throw e;
292                 } catch(Exception ex) {
293                         msg = "Exception in preProcessSDNCDeactivate. " + ex.getMessage()
294                         utils.log("DEBUG", msg, isDebugEnabled)
295                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
296                 }
297                 utils.log("DEBUG"," *****Exit preProcessSDNCDeactivate *****", isDebugEnabled)
298         }
299
300         public void validateSDNCResp(DelegateExecution execution, String response, String method){
301
302                 def isDebugLogEnabled=execution.getVariable(DebugFlag)
303                 utils.log("DEBUG", " *** ValidateSDNCResponse Process*** ", isDebugLogEnabled)
304                 String msg = ""
305
306                 try {
307                         WorkflowException workflowException = execution.getVariable("WorkflowException")
308                         utils.logAudit("workflowException: " + workflowException)
309
310                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
311                         utils.logAudit("SDNCResponse: " + response)
312
313                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
314                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
315
316                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
317                                 utils.log("DEBUG", "Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + response, isDebugLogEnabled)
318
319                         }else{
320                                 String sdncRespCode = execution.getVariable(Prefix + 'sdncRequestDataResponseCode')
321                                 utils.log("DEBUG", method + " AllottedResource received error response from SDNC. ResponseCode:" +  sdncRespCode, isDebugLogEnabled)
322                                 if (sdncRespCode.equals("404") && "deactivate".equals(method))
323                                 {
324                                         execution.setVariable("ARNotFoundInSDNC", true)
325                                         if ("true".equals(execution.getVariable("failNotFound")))
326                                         {
327                                                 msg = "Allotted Resource Not found in SDNC"
328                                                 utils.log("DEBUG", msg, isDebugLogEnabled)
329                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
330                                         }
331                                         else
332                                         {
333                                                 execution.setVariable("wasDeleted", false)
334                                         }
335                                 }
336                                 else
337                                 {
338                                         throw new BpmnError("MSOWorkflowException")
339                                 }
340                         }
341                 } catch (BpmnError e) {
342                         throw e;
343                 } catch(Exception ex) {
344                         msg = "Exception in validateSDNCResp. " + ex.getMessage()
345                         utils.log("DEBUG", msg, isDebugLogEnabled)
346                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
347                 }
348                 logDebug(" *** Exit ValidateSDNCResp Process*** ", isDebugLogEnabled)
349         }
350
351         public void deleteAaiAR(DelegateExecution execution){
352                 def isDebugLogEnabled = execution.getVariable(DebugFlag)
353                 try{
354                         utils.log("DEBUG", " *** deleteAaiAR *** ", isDebugLogEnabled)
355                         AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
356                         String ar = null //need to get resource-version again 
357                         String arLink = execution.getVariable("aaiARPath")
358                         if (!isBlank(arLink))
359                         {
360                                 ar = arUtils.getARbyLink(execution, arLink, "")
361                         }
362                         arUtils.deleteAR(execution, arLink + '?resource-version=' + UriUtils.encode(execution.getVariable("aaiARResourceVersion"),"UTF-8"))
363                 } catch (BpmnError e) {
364                         throw e;
365                 }catch(Exception ex){
366                         utils.log("ERROR", "Exception Occurred Processing preProcessSDNCGetRequest. Exception is:\n" + ex, isDebugLogEnabled)
367                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + ex.getMessage())
368                 }
369                 utils.log("DEBUG", " *** Exit deleteAaiAR *** ", isDebugLogEnabled)
370         }
371
372 }