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