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