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