aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/DeleteVfModuleInfra.groovy
blob: 93890be2af11256271c3f809e07cc22d9eac106a (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
373
374
375
376
377
378
379
380
381
382
383
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * 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.infrastructure.scripts

import groovy.util.Node
import groovy.util.XmlParser;
import groovy.xml.QName

import java.io.Serializable;

import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.runtime.Execution

import groovy.json.JsonSlurper

import org.openecomp.mso.rest.APIResponse
import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
import org.openecomp.mso.bpmn.common.scripts.VidUtils;
import org.openecomp.mso.bpmn.core.RollbackData
import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.bpmn.core.json.JsonUtils;


public class DeleteVfModuleInfra extends AbstractServiceTaskProcessor {
	
	ExceptionUtil exceptionUtil = new ExceptionUtil()
	JsonUtils jsonUtil = new JsonUtils()
	/**
	 * Initialize the flow's variables.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void initProcessVariables(Execution execution) {
		execution.setVariable('prefix', 'DELVfModI_')
		execution.setVariable('DELVfModI_requestInfo', null)
		execution.setVariable('DELVfModI_requestId', null)
		execution.setVariable('DELVfModI_source', null)
		execution.setVariable('DELVfModI_vnfInputs', null)
		execution.setVariable('DELVfModI_vnfId', null)
		execution.setVariable('DELVfModI_vfModuleId', null)
		execution.setVariable('DELVfModI_tenantId', null)
		execution.setVariable('DELVfModI_volumeGroupId', null)
		execution.setVariable('DELVfModI_vnfParams', null)
		execution.setVariable('DELVfModI_updateInfraRequest', null)
		execution.setVariable('DeleteVfModuleRequest', null)
		execution.setVariable('DeleteVfModuleSuccessIndicator', false)
	}

	/**
	 * Process the incoming DELETE_VF_MODULE vnf-request.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void preProcessRequest(Execution execution) {
		def method = getClass().getSimpleName() + '.preProcessRequest(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)
		execution.setVariable("isVidRequest", "false")
		initProcessVariables(execution)
		
		def prefix = execution.getVariable('prefix')
		
		def incomingRequest = execution.getVariable('bpmnRequest')
		
		utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)
		utils.logAudit("DeleteVfModule Infra incoming Request: " + incomingRequest)
		
		// check if request is xml or json
		try {
			def jsonSlurper = new JsonSlurper()
			Map reqMap = jsonSlurper.parseText(incomingRequest)
			utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
			
			def serviceInstanceId = execution.getVariable('serviceInstanceId')
			utils.log("DEBUG", "serviceInstanceId is: " + serviceInstanceId, isDebugLogEnabled)
			def vnfId = execution.getVariable('vnfId')
			utils.log("DEBUG", "vnfId is: " + vnfId, isDebugLogEnabled)
			def cloudConfiguration = jsonUtil.getJsonValue(incomingRequest, "requestDetails.cloudConfiguration")
			execution.setVariable("cloudConfiguration", cloudConfiguration)
			utils.log("DEBUG", "CloudConfiguration is: " + cloudConfiguration, isDebugLogEnabled)
			def vfModuleModelInfo = jsonUtil.getJsonValue(incomingRequest, "requestDetails.modelInfo")
			
			execution.setVariable("vfModuleModelInfo", vfModuleModelInfo)
			utils.log("DEBUG", "VfModuleModelInfo is: " + vfModuleModelInfo, isDebugLogEnabled)
			
			def vidUtils = new VidUtils(this)
			
			String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'DELETE_VF_MODULE', serviceInstanceId)
			
			utils.log("DEBUG", " Request in XML format: " + requestInXmlFormat, isDebugLogEnabled)
			
			try {
				// Catalog DB headers Authorization
				String basicAuthValueDB = execution.getVariable("URN_mso_adapters_db_auth")
				utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB, isDebugLogEnabled)
				
				def encodedString = utils.getBasicAuth(basicAuthValueDB, execution.getVariable("URN_mso_msoKey"))
				execution.setVariable("BasicAuthHeaderValueDB",encodedString)
			} catch (IOException ex) {
				String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
				utils.log("DEBUG", dataErrorMessage, isDebugLogEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
			}
			
			execution.setVariable(prefix + 'Request', requestInXmlFormat)
			execution.setVariable(prefix+'vnfId', vnfId)
			execution.setVariable("isVidRequest", "true")
			
		}
		catch(groovy.json.JsonException je) {
			utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
			workflowException(execution, "Invalid request format", 400)
			
		}
		catch(Exception e) {
			String restFaultMessage = e.getMessage()
			utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
			workflowException(execution, restFaultMessage, 400)
		}
		
	
		try {
			
			String request = validateRequest(execution)
			execution.setVariable('DeleteVfModuleRequest', request)
			utils.logAudit("DeleteVfModuleInfra Request: " + request)
			
			def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
			execution.setVariable('DELVfModI_requestInfo', requestInfo)
			execution.setVariable('DELVfModI_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
			execution.setVariable('DELVfModI_source', getNodeTextForce(requestInfo, 'source'))
			
			def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
			execution.setVariable('DELVfModI_vnfInputs', vnfInputs)
			execution.setVariable('DELVfModI_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
			execution.setVariable('DELVfModI_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
			execution.setVariable('DELVfModI_vfModuleName', getNodeTextForce(vnfInputs, 'vf-module-name'))
			execution.setVariable('DELVfModI_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
			execution.setVariable('DELVfModI_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
			
			def vnfParams = utils.getNodeXml(request, 'vnf-params')
			execution.setVariable('DELVfModI_vnfParams', vnfParams)
	
			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
		}
	}

	/**
	 * Sends the 'IN_PROGRESS' synchronous response.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void sendSynchResponse(Execution execution) {
		def method = getClass().getSimpleName() + '.sendResponse(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)
	
		try {
			def requestInfo = execution.getVariable('DELVfModI_requestInfo')
			def requestId = execution.getVariable('DELVfModI_requestId')
			def source = execution.getVariable('DELVfModI_source')
			def progress = getNodeTextForce(requestInfo, 'progress')
			if (progress.isEmpty()) {
				progress = '0'
			}
			def startTime = getNodeTextForce(requestInfo, 'start-time')
			if (startTime.isEmpty()) {
				startTime = System.currentTimeMillis()
			}
				
			// RESTResponse (for API Handler (APIH) Reply Task)
			def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
			String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()
				
			utils.logAudit("DeleteVfModuleInfra Synch Response: " + synchResponse)
			sendWorkflowResponse(execution, 200, synchResponse)

			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
		}
	}
		
	/**
	 * Currently passing the entire DELETE_VF_MODULE vnf-request to DoDeleteVfModule.
	 * 'DeleteVfModuleRequest' is now being set in preProcessRequest().
	 * TBD: may want to eventually create a specific request that only contains the needed fields.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void prepDoDeleteVfModule(Execution execution) {
		def method = getClass().getSimpleName() + '.prepDoDeleteVfModule(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		try {

			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 1002, 'Error in prepDoDeleteVfModule(): ' + e.getMessage())
		}
	}
		
	/**
	 * Prepare the DB update to add an entry for the Vf Module request.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void prepUpdateInfraRequest(Execution execution) {
		def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		try {
			def requestId = execution.getVariable('DELVfModI_requestId')
			def vnfId = execution.getVariable('DELVfModI_vnfId')
			def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
			def tenantId = execution.getVariable('DELVfModI_tenantId')
			def volumeGroupId = execution.getVariable('DELVfModI_volumeGroupId')

			String updateInfraRequest = """
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
					xmlns:req="http://org.openecomp.mso/requestsdb">
				<soapenv:Header/>
				<soapenv:Body>
					<req:updateInfraRequest>
						<requestId>${requestId}</requestId>
						<lastModifiedBy>BPMN</lastModifiedBy>
						<requestStatus>COMPLETED</requestStatus>
						<progress>100</progress>
					</req:updateInfraRequest>
				</soapenv:Body>
			</soapenv:Envelope>
		"""

			updateInfraRequest = utils.formatXml(updateInfraRequest)
			execution.setVariable('DELVfModI_updateInfraRequest', updateInfraRequest)
			utils.logAudit("DeleteAAIVfModuleInfra Update Request: " + updateInfraRequest)
			logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)

			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 1002, 'Error in prepInfraRequest(): ' + e.getMessage())
		}
	}

	/**
	 * Builds a "CompletionHandler" request and stores it in the specified execution variable.
	 *
	 * @param execution the execution
	 * @param resultVar the execution variable in which the result will be stored
	 */
	public void completionHandlerPrep(Execution execution, String resultVar) {
		def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
			'execution=' + execution.getId() +
			', resultVar=' + resultVar +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		try {
			def request = execution.getVariable("DeleteVfModuleRequest")
			def requestInfo = utils.getNodeXml(request, 'request-info', false)
			def action = utils.getNodeText1(requestInfo, "action")
			
			String content =
					"""  <aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
						   xmlns:ns="http://org.openecomp/mso/request/types/v1"
						   xmlns:ns8="http://org.openecomp/mso/workflow/schema/v1">
		<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
		${requestInfo}
		</request-info>
		<ns8:status-message>Vf Module has been deleted successfully.</ns8:status-message>
		<ns8:mso-bpel-name>BPMN</ns8:mso-bpel-name>
		</aetgt:MsoCompletionRequest>"""
			
			content = utils.formatXml(content)
			logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
			utils.logAudit("DeleteVfModule Infra Completion Handler Request: " + content)
			execution.setVariable(resultVar, content)

			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 2000, 'Internal Error')
		}
	}

	/**
	 * Builds a "FalloutHandler" request and stores it in the specified execution variable.
	 *
	 * @param execution the execution
	 * @param resultVar the execution variable in which the result will be stored
	 */
	public void falloutHandlerPrep(Execution execution, String resultVar) {
		def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
			'execution=' + execution.getId() +
			', resultVar=' + resultVar +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)
		
		try {
			def prefix = execution.getVariable('prefix')
			def request = execution.getVariable("DeleteVfModuleRequest")
			def requestInfo = utils.getNodeXml(request, 'request-info', false)
			def WorkflowException workflowException = execution.getVariable("WorkflowException")
			def errorResponseCode = workflowException.getErrorCode()
			def errorResponseMsg = workflowException.getErrorMessage()
			def encErrorResponseMsg = ""
			if (errorResponseMsg != null) {
				encErrorResponseMsg = errorResponseMsg.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
			}

			String content = """
			<sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
					xmlns:reqtype="http://org.openecomp/mso/request/types/v1"
					xmlns:msoservtypes="http://org.openecomp/mso/request/types/v1"
					xmlns:structuredtypes="http://org.openecomp/mso/structured/types/v1">
				${requestInfo}
				<sdncadapterworkflow:WorkflowException>
					<sdncadapterworkflow:ErrorMessage>${encErrorResponseMsg}</sdncadapterworkflow:ErrorMessage>
					<sdncadapterworkflow:ErrorCode>${errorResponseCode}</sdncadapterworkflow:ErrorCode>
				</sdncadapterworkflow:WorkflowException>
			</sdncadapterworkflow:FalloutHandlerRequest>
		"""
			content = utils.formatXml(content)
			utils.logAudit("DeleteVfModuleInfra Fallout Handler Request: " + content)
			logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
			execution.setVariable(resultVar, content)

			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 2000, 'Internal Error')
		}
	}
}