aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/vcpe/scripts/DoDeleteAllottedResourceBRG.groovy
blob: 83ef53d5dd7782a1a69c6c098a4ed53885c48029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.mso.bpmn.vcpe.scripts;

import org.openecomp.mso.bpmn.common.scripts.*;
import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
import org.openecomp.mso.bpmn.common.scripts.MsoUtils
import org.openecomp.mso.bpmn.common.scripts.AaiUtil
import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
import org.openecomp.mso.rest.APIResponse

import java.util.UUID;
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.apache.commons.lang3.*
import org.springframework.web.util.UriUtils;
import static org.apache.commons.lang3.StringUtils.*

/**
 * This groovy class supports the <class>DoDeleteAllottedResourceBRG.bpmn</class> process.
 *
 * @author
 * 
 * Inputs:
 * @param - msoRequestId
 * @param - isDebugLogEnabled
 * @param - disableRollback - O ignored
 * @param - failNotfound  - O 
 * @param - serviceInstanceId
 * @param - globalCustomerId - O
 * @param - subscriptionServiceType - O
 * @param - parentServiceInstanceId
 * @param - allottedResourceId 
 *
 * Outputs:
 * @param - rollbackData - N/A
 * @param - rolledBack - true if no deletions performed
 * @param - WorkflowException - O
 * @param - wasDeleted - O (ie not silentSuccess)
 *
 */
public class DoDeleteAllottedResourceBRG extends AbstractServiceTaskProcessor{

	private static final String DebugFlag = "isDebugLogEnabled"

	String Prefix="DDARBRG_"
	ExceptionUtil exceptionUtil = new ExceptionUtil()

	public void preProcessRequest (DelegateExecution execution) {

		def isDebugEnabled = execution.getVariable(DebugFlag)
		String msg = ""
		utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)

		try {
			execution.setVariable("prefix", Prefix)

			//Config Inputs
			String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
			if (isBlank(sdncCallbackUrl)) {
				msg = "URN_mso_workflow_sdncadapter_callback is null"
				utils.log("DEBUG", msg, isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
			}
			execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
			utils.log("DEBUG","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)

			//Request Inputs
			if (isBlank(execution.getVariable("serviceInstanceId"))){
				msg = "Input serviceInstanceId is null"
				utils.log("DEBUG", msg, isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
			}
			if (isBlank(execution.getVariable("allottedResourceId"))){
				msg = "Input allottedResourceId is null"
				utils.log("DEBUG", msg, isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
			}

		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
			throw b
		} catch (Exception ex){
			msg = "Exception in preProcessRequest " + ex.getMessage()
			utils.log("DEBUG", msg, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
		}
		utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
	}

	public void getAaiAR (DelegateExecution execution) {

		def isDebugEnabled = execution.getVariable(DebugFlag)
		utils.log("DEBUG"," ***** getAaiAR ***** ", isDebugEnabled)

		String allottedResourceId = execution.getVariable("allottedResourceId")

		AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
		String ar = arUtils.getARbyId(execution, allottedResourceId)

		String errorMsg = ""
		if (isBlank(ar)) // AR was !found
		{
			errorMsg = "Allotted resource not found in AAI with AllottedResourceId:" + allottedResourceId
		}
		else
		{
			String aaiARPath = execution.getVariable("aaiARPath")
			String parentServiceInstanceId = arUtils.getPSIFmARLink(execution, aaiARPath)
			execution.setVariable("parentServiceInstanceId", parentServiceInstanceId)
		}
		if (!isBlank(errorMsg)) {
			utils.log("DEBUG", errorMsg, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 500, errorMsg)
		}
		utils.log("DEBUG"," ***** getAaiAR *****",  isDebugEnabled)

	}

	// aaiARPath set during query (existing AR)
	public void updateAaiAROrchStatus(DelegateExecution execution, String status){
		def isDebugEnabled = execution.getVariable(DebugFlag)
		utils.log("DEBUG", " *** updateAaiAROrchStatus *** ", isDebugEnabled)
		AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
		String aaiARPath = execution.getVariable("aaiARPath") //set during query (existing AR) 
		String orchStatus = arUtils.updateAROrchStatus(execution, status, aaiARPath)
		utils.log("DEBUG", " *** Exit updateAaiAROrchStatus *** ", isDebugEnabled)
	}

	public String buildSDNCRequest(DelegateExecution execution, String action, String sdncRequestId) {

		def isDebugEnabled = execution.getVariable(DebugFlag)
		String msg = ""
		utils.log("DEBUG"," ***** buildSDNCRequest *****", isDebugEnabled)
		String sdncReq = null

		try {

			String allottedResourceId = execution.getVariable("allottedResourceId")
			String serviceInstanceId = execution.getVariable("serviceInstanceId")
			String parentServiceInstanceId = execution.getVariable("parentServiceInstanceId")
			String globalCustomerId = execution.getVariable("globalCustomerId")
			String subscriptionServiceType = execution.getVariable("subscriptionServiceType")

			String callbackUrl = execution.getVariable("sdncCallbackUrl")
			String requestId = execution.getVariable("msoRequestId")

			String serviceChainServiceInstanceId = ""
			String sourceNetworkId = ""
			String sourceNetworkRole = ""
			String allottedResourceRole = ""

			String arModelInfo = ""
			String modelInvariantId = ""
			String modelVersion = ""
			String modelUUId = ""
			String modelCustomizationId = ""
			String modelName = ""


			sdncReq =
					"""<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
													xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
													xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
				   <sdncadapter:RequestHeader>
							<sdncadapter:RequestId>${sdncRequestId}</sdncadapter:RequestId>
							<sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>
							<sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
							<sdncadapter:SvcOperation>brg-topology-operation</sdncadapter:SvcOperation>
							<sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
					</sdncadapter:RequestHeader>
				<sdncadapterworkflow:SDNCRequestData>
					<request-information>
						<request-id>${requestId}</request-id>
						<request-action>DeleteBRGInstance</request-action>
						<source>MSO</source>
						<notification-url/>
						<order-number/>
						<order-version/>
					</request-information>
					<service-information>
						<service-id></service-id>
						<subscription-service-type>${subscriptionServiceType}</subscription-service-type>
						<onap-model-information></onap-model-information>
						<service-instance-id>${serviceInstanceId}</service-instance-id>
						<subscriber-name/>
						<global-customer-id>${globalCustomerId}</global-customer-id>
					</service-information>
					<allotted-resource-information>
						<allotted-resource-id>${allottedResourceId}</allotted-resource-id>    
						<allotted-resource-type>brg</allotted-resource-type>
						<parent-service-instance-id>${parentServiceInstanceId}</parent-service-instance-id>   
						<onap-model-information>
							<model-invariant-uuid>${modelInvariantId}</model-invariant-uuid>
							<model-uuid>${modelUUId}</model-uuid>
							<model-customization-uuid>${modelCustomizationId}</model-customization-uuid>
							<model-version>${modelVersion}</model-version>
							<model-name>${modelName}</model-name>
						</onap-model-information>
					</allotted-resource-information>
					<brg-request-input>
					</brg-request-input>
				</sdncadapterworkflow:SDNCRequestData>
				</sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""

			utils.log("DEBUG","sdncRequest:\n" + sdncReq, isDebugEnabled)
			sdncReq = utils.formatXml(sdncReq)

		} catch(Exception ex) {
			msg = "Exception in buildSDNCRequest. " + ex.getMessage()
			utils.log("DEBUG", msg, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
		}
		utils.log("DEBUG"," *****Exit buildSDNCRequest *****", isDebugEnabled)
		return sdncReq
	}

	public void preProcessSDNCUnassign(DelegateExecution execution) {

		def isDebugEnabled = execution.getVariable(DebugFlag)
		String msg = ""
		utils.log("DEBUG"," ***** preProcessSDNCUnassign *****", isDebugEnabled)

		try {
			String sdncRequestId = UUID.randomUUID().toString()
			String sdncUnassignReq = buildSDNCRequest(execution, "unassign", sdncRequestId)
			execution.setVariable("sdncUnassignRequest", sdncUnassignReq)
			utils.logAudit("sdncUnassignRequest:  " + sdncUnassignReq)
		} catch (BpmnError e) {
			throw e;
		} catch(Exception ex) {
			msg = "Exception in preProcessSDNCUnassign. " + ex.getMessage()
			utils.log("DEBUG", msg, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
		}
		utils.log("DEBUG"," *****Exit preProcessSDNCUnassign *****", isDebugEnabled)
	}

	public void preProcessSDNCDelete(DelegateExecution execution) {

		def isDebugEnabled = execution.getVariable(DebugFlag)
		String msg = ""
		utils.log("DEBUG"," ***** preProcessSDNCDelete *****", isDebugEnabled)

		try {
			String sdncRequestId = UUID.randomUUID().toString()
			String sdncDeleteReq = buildSDNCRequest(execution, "delete", sdncRequestId)
			execution.setVariable("sdncDeleteRequest", sdncDeleteReq)
			utils.logAudit("sdncDeleteReq:  " + sdncDeleteReq)
		} catch (BpmnError e) {
			throw e;
		} catch(Exception ex) {
			msg = "Exception in preProcessSDNCDelete. " + ex.getMessage()
			utils.log("DEBUG", msg, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
		}
		utils.log("DEBUG"," *****Exit preProcessSDNCDelete *****", isDebugEnabled)
	}

	public void preProcessSDNCDeactivate(DelegateExecution execution) {

		def isDebugEnabled = execution.getVariable(DebugFlag)
		String msg = ""
		utils.log("DEBUG"," ***** preProcessSDNCDeactivate *****", isDebugEnabled)

		try {
			String sdncRequestId = UUID.randomUUID().toString()
			String sdncDeactivateReq = buildSDNCRequest(execution, "deactivate", sdncRequestId)
			execution.setVariable("sdncDeactivateRequest", sdncDeactivateReq)
			utils.logAudit("sdncDeactivateReq:  " + sdncDeactivateReq)
		} catch (BpmnError e) {
			throw e;
		} catch(Exception ex) {
			msg = "Exception in preProcessSDNCDeactivate. " + ex.getMessage()
			utils.log("DEBUG", msg, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
		}
		utils.log("DEBUG"," *****Exit preProcessSDNCDeactivate *****", isDebugEnabled)
	}

	public void validateSDNCResp(DelegateExecution execution, String response, String method){

		def isDebugLogEnabled=execution.getVariable(DebugFlag)
		utils.log("DEBUG", " *** ValidateSDNCResponse Process*** ", isDebugLogEnabled)
		String msg = ""

		try {
			WorkflowException workflowException = execution.getVariable("WorkflowException")
			utils.logAudit("workflowException: " + workflowException)

			boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
			utils.logAudit("SDNCResponse: " + response)

			SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
			sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)

			if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
				utils.log("DEBUG", "Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + response, isDebugLogEnabled)

			}else{
				String sdncRespCode = execution.getVariable(Prefix + 'sdncRequestDataResponseCode')
				utils.log("DEBUG", method + " AllottedResource received error response from SDNC. ResponseCode:" +  sdncRespCode, isDebugLogEnabled)
				if (sdncRespCode.equals("404") && "deactivate".equals(method))
				{
					execution.setVariable("ARNotFoundInSDNC", true)
					if ("true".equals(execution.getVariable("failNotFound")))
					{
						msg = "Allotted Resource Not found in SDNC"
						utils.log("DEBUG", msg, isDebugLogEnabled)
						exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
					}
					else
					{
						execution.setVariable("wasDeleted", false)
					}
				}
				else
				{
					throw new BpmnError("MSOWorkflowException")
				}
			}
		} catch (BpmnError e) {
			throw e;
		} catch(Exception ex) {
			msg = "Exception in validateSDNCResp. " + ex.getMessage()
			utils.log("DEBUG", msg, isDebugLogEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
		}
		logDebug(" *** Exit ValidateSDNCResp Process*** ", isDebugLogEnabled)
	}

	public void deleteAaiAR(DelegateExecution execution){
		def isDebugLogEnabled = execution.getVariable(DebugFlag)
		try{
			utils.log("DEBUG", " *** deleteAaiAR *** ", isDebugLogEnabled)
			AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
			String ar = null //need to get resource-version again 
			String arLink = execution.getVariable("aaiARPath")
			if (!isBlank(arLink))
			{
				ar = arUtils.getARbyLink(execution, arLink, "")
			}
			arUtils.deleteAR(execution, arLink + '?resource-version=' + UriUtils.encode(execution.getVariable("aaiARResourceVersion"),"UTF-8"))
		} catch (BpmnError e) {
			throw e;
		}catch(Exception ex){
			utils.log("ERROR", "Exception Occurred Processing preProcessSDNCGetRequest. Exception is:\n" + ex, isDebugLogEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + ex.getMessage())
		}
		utils.log("DEBUG", " *** Exit deleteAaiAR *** ", isDebugLogEnabled)
	}

}