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