Cleaned up content of MsoLogger
[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  * 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>DoDeleteAllottedResourceTXC.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 DoDeleteAllottedResourceTXC extends AbstractServiceTaskProcessor{
65         private static final Logger logger = LoggerFactory.getLogger(DoDeleteAllottedResourceTXC.class);
66
67         String Prefix="DDARTXC_"
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")
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         public AllottedResourceUtils getAllottedResourceUtils(){
138                 return new AllottedResourceUtils(this)
139         }
140
141         // aaiARPath set during query (existing AR)
142         public void updateAaiAROrchStatus(DelegateExecution execution, String status){
143                 logger.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                 logger.trace("end updateAaiAROrchStatus")
148         }
149
150         public String buildSDNCRequest(DelegateExecution execution, String action, String sdncRequestId) {
151
152                 String msg = ""
153                 logger.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                         logger.debug("sdncRequest:\n" + sdncReq)
226                         sdncReq = utils.formatXml(sdncReq)
227
228                 } catch(Exception ex) {
229                         msg = "Exception in buildSDNCRequest. " + ex.getMessage()
230                         logger.debug(msg)
231                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
232                 }
233                 logger.trace("end buildSDNCRequest")
234                 return sdncReq
235         }
236
237         public void preProcessSDNCUnassign(DelegateExecution execution) {
238
239                 String msg = ""
240                 logger.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                         logger.debug("sdncUnassignRequest:  " + sdncUnassignReq)
247                 } catch (BpmnError e) {
248                         throw e;
249                 } catch(Exception ex) {
250                         msg = "Exception in preProcessSDNCUnassign. " + ex.getMessage()
251                         logger.debug(msg)
252                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
253                 }
254                 logger.trace("end preProcessSDNCUnassign")
255         }
256
257         public void preProcessSDNCDelete(DelegateExecution execution) {
258
259                 String msg = ""
260                 logger.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                         logger.debug("sdncDeleteReq:  " + sdncDeleteReq)
267                 } catch (BpmnError e) {
268                         throw e;
269                 } catch(Exception ex) {
270                         msg = "Exception in preProcessSDNCDelete. " + ex.getMessage()
271                         logger.debug(msg)
272                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
273                 }
274                 logger.trace("end preProcessSDNCDelete")
275         }
276
277         public void preProcessSDNCDeactivate(DelegateExecution execution) {
278
279                 String msg = ""
280                 logger.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                         logger.debug("sdncDeactivateReq:  " + sdncDeactivateReq)
287                 } catch (BpmnError e) {
288                         throw e;
289                 } catch(Exception ex) {
290                         msg = "Exception in preProcessSDNCDeactivate. " + ex.getMessage()
291                         logger.debug(msg)
292                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
293                 }
294                 logger.trace("end preProcessSDNCDeactivate")
295         }
296
297         public void validateSDNCResp(DelegateExecution execution, String response, String method){
298
299                 logger.trace("start ValidateSDNCResponse Process")
300                 String msg = ""
301
302                 try {
303                         WorkflowException workflowException = execution.getVariable("WorkflowException")
304                         logger.debug("workflowException: " + workflowException)
305
306                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
307                         logger.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                                 logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n"
314                                                 + response)
315                         }else{
316                                 String sdncRespCode = execution.getVariable(Prefix + 'sdncRequestDataResponseCode')
317                                 logger.debug(method + " AllottedResource received error response from SDNC. ResponseCode:"
318                                                 + sdncRespCode)
319                                 if (sdncRespCode.equals("404") && "deactivate".equals(method))
320                                 {
321                                         execution.setVariable("ARNotFoundInSDNC", true)
322                                         if ("true".equals(execution.getVariable("failNotFound")))
323                                         {
324                                                 msg = "Allotted Resource Not found in SDNC"
325                                                 logger.debug(msg)
326                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
327                                         }
328                                         else
329                                         {
330                                                 execution.setVariable("wasDeleted", false)
331                                         }
332                                 }
333                                 else
334                                 {
335                                         throw new BpmnError("MSOWorkflowException")
336                                 }
337                         }
338                 } catch (BpmnError e) {
339                         throw e;
340                 } catch(Exception ex) {
341                         msg = "Exception in validateSDNCResp. " + ex.getMessage()
342                         logger.debug(msg)
343                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
344                 }
345                 logger.trace("end Exit ValidateSDNCResp Process")
346         }
347
348         public void deleteAaiAR(DelegateExecution execution){
349                 logger.trace("start deleteAaiAR")
350                 
351                 try{
352                         AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
353                         String arLink = execution.getVariable("aaiARPath")
354                         arUtils.deleteAR(execution, arLink)
355                 } catch (BpmnError e) {
356                         throw e;
357                 }catch(Exception ex){
358                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
359                                         "Exception Occurred Processing preProcessSDNCGetRequest.", "BPMN",
360                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + ex);
361                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + ex.getMessage())
362                 }
363                 logger.trace("end deleteAaiAR")
364         }
365
366 }