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