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