summaryrefslogtreecommitdiffstats
path: root/bpmn/MSOGammaBPMN/src/main/groovy/com/att/bpm/scripts/CreateAAIVfModuleVolumeGroup.groovy
blob: 51e77a813de59ee0ec1cef222f18af3d393c00c9 (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
/*-
 * ============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 com.att.bpm.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 org.openecomp.mso.rest.APIResponse
import org.openecomp.mso.rest.RESTClient
import org.openecomp.mso.rest.RESTConfig
import org.openecomp.mso.bpmn.core.RollbackData
import org.openecomp.mso.bpmn.core.WorkflowException


public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {

	private XmlParser xmlParser = new XmlParser()

	/**
	 * Initialize the flow's variables.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void initProcessVariables(Execution execution) {
		execution.setVariable('prefix', 'CAAIVfModVG_')
		execution.setVariable('CAAIVfModVG_vnfId', null)
		execution.setVariable('CAAIVfModVG_vfModuleId', null)
		execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
		execution.setVariable('CAAIVfModVG_volumeGroupId', null)
		execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
		execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
		execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
		execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
	}

	/**
	 * Check for missing elements in the received 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)

		try {
			def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
			logDebug('Received request xml:\n' + xml, isDebugLogEnabled)
			initProcessVariables(execution)

			def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
			execution.setVariable('CAAIVfModVG_vnfId', vnfId)

			def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
			execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
			
			def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
			execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
			
			def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
			execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)

			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())
		}
	}

	/**
	 * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
	 * A 200 response is expected with the VF Module in the response body.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void getVfModule(Execution execution) {
		def method = getClass().getSimpleName() + '.getVfModule(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		try {
			def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
			def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')

			// Construct endpoint
			AaiUtil aaiUtil = new AaiUtil(this)
			def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
			logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
			String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + URLEncoder.encode(vnfId, "UTF-8") + '/vf-modules/vf-module/' + URLEncoder.encode(vfModuleId, "UTF-8")

			try {
				logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
				APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)
				def responseData = response.getResponseBodyAsString()
				execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', response.getStatusCode())
				execution.setVariable('CAAIVfModVG_getVfModuleResponse', responseData)
				logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
				logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
			} catch (Exception ex) {
				ex.printStackTrace()
				logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
				execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
				execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
			}
			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
		}
	}

	/**
	 * Construct and send a PUT request to AAI to update the VF Module with the
	 * created Volume Group relationship.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void updateVfModule(Execution execution) {
		def method = getClass().getSimpleName() + '.updateVfModule(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		try {
			def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
			def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
			def vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
			def origRequest = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
			def Node vfModuleNode = xmlParser.parseText(vfModule)
			
			// Confirm resource-version is in retrieved VF Module
			if (utils.getChildNode(vfModuleNode, 'resource-version') == null) {
				def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
				logError(msg)
				throw new Exception(msg)
			}
						
			// Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
			def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
			def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
			def Node vgRelationshipNode = createVolumeGroupRelationshipNode(aicCloudRegion, volumeGroupId)
			insertVolumeGroupRelationshipNode(vfModuleNode, vgRelationshipNode)
			def payload = utils.nodeToString(vfModuleNode)

			// Construct endpoint
			AaiUtil aaiUtil = new AaiUtil(this)
			def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
			logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
			String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + URLEncoder.encode(vnfId, "UTF-8") + '/vf-modules/vf-module/' + URLEncoder.encode(vfModuleId, "UTF-8")

			try {
				logDebug('sending PUT to AAI endpoint \'' + endPoint + '\'' + 'with payload \n' + payload, isDebugLogEnabled)
				APIResponse response = aaiUtil.executeAAIPutCall(execution, endPoint, payload)
				def responseData = response.getResponseBodyAsString()
				execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', response.getStatusCode())
				execution.setVariable('CAAIVfModVG_updateVfModuleResponse', responseData)
				logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
				logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
			} catch (Exception ex) {
				ex.printStackTrace()
				logDebug('Exception occurred while executing AAI PUT:' + ex.getMessage(),isDebugLogEnabled)
				execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
				execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
			}
			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			createWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
		}
	}

	/**
	 * Construct a Volume Group relationship Node with the given AIC Cloud Region and
	 * Volume Group ID for insertion into a VF Module.
	 * 
	 * @param aicCloudRegion Cloud Region ID to use in the Volume Group relationship
	 * @param volumeGroupId Volume Group ID to use in the Volume Group relationship
	 * @return a Node representing the new Volume Group relationship
	 */
	private Node createVolumeGroupRelationshipNode(String aicCloudRegion, String volumeGroupId) {
		
		def Node relatedTo = new Node(null, 'related-to', 'volume-group')
		
		def Node relationshipKeyCO = new Node(null, 'relationship-key', 'cloud-region.cloud-owner')
		def Node relationshipValueCO = new Node(null, 'relationship-value', 'att-aic')
		def Node relationshipDataCO = new Node(null, 'relationship-data')
		relationshipDataCO.append(relationshipKeyCO)
		relationshipDataCO.append(relationshipValueCO)
		
		def Node relationshipKeyCRI = new Node(null, 'relationship-key', 'cloud-region.cloud-region-id')
		def Node relationshipValueCRI = new Node(null, 'relationship-value', aicCloudRegion)
		def Node relationshipDataCRI = new Node(null, 'relationship-data')
		relationshipDataCRI.append(relationshipKeyCRI)
		relationshipDataCRI.append(relationshipValueCRI)

		def Node relationshipKeyVGI = new Node(null, 'relationship-key', 'volume-group.volume-group-id')
		def Node relationshipValueVGI = new Node(null, 'relationship-value', volumeGroupId)
		def Node relationshipDataVGI = new Node(null, 'relationship-data')
		relationshipDataVGI.append(relationshipKeyVGI)
		relationshipDataVGI.append(relationshipValueVGI)
		
		def Node volumeGroupRelationship = new Node(null, 'relationship')
		volumeGroupRelationship.append(relatedTo)
		volumeGroupRelationship.append(relationshipDataCO)
		volumeGroupRelationship.append(relationshipDataCRI)
		volumeGroupRelationship.append(relationshipDataVGI)
		
		return volumeGroupRelationship;
	}
	
	/**
	 * Insert the given Volume Group relationship Node into the given VF Module.
	 * If the VF Module does NOT contain a relationship list:
	 * 	- Create a relationship list containing the Volume Group relationship and insert it into the VF Module
	 * If the VF Module contains a relationship list but not a Volume Group relationship:
	 * 	- Insert the the Volume Group relationship into the relationship lsit
	 * If the VF Module contains a relationship list and has a Volume Group relationship:
	 * 	- Replace the existing Volume Group relationship with the new one
	 * @param vfModuleNode
	 * @param volumeGroupRelationshipNode
	 */
	private void insertVolumeGroupRelationshipNode(Node vfModuleNode, Node volumeGroupRelationshipNode) {
		def Node relationshipList = utils.getChildNode(vfModuleNode, 'relationship-list')
		if (relationshipList == null) {
			relationshipList = new Node(null, 'relationship-list')
			relationshipList.append(volumeGroupRelationshipNode)
			vfModuleNode.append(relationshipList)
		} else {
			def Node currVolumeGroupRelationshipNode = getCurrVolumeGroupRelationshipNode(relationshipList)
			if (currVolumeGroupRelationshipNode == null) {
				relationshipList.append(volumeGroupRelationshipNode)
			} else {
				currVolumeGroupRelationshipNode.replaceNode(volumeGroupRelationshipNode)
			}
		}
	}
		
	/**
	 * Find and return the value of the Volume Group ID for the specified VF Module.  If
	 * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
	 * 
	 * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
	 * @return the value of the Volume Group ID for the specified VF Module.  If the
	 * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
	 */
	private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
		def Node currVolumeGroupRelationshipNode = null
		def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
		for (Node relationshipNode in relationships) {
			def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
			if ((relatedTo != null) && relatedTo.equals('volume-group')) {
				currVolumeGroupRelationshipNode = relationshipNode
			}
		}
		return currVolumeGroupRelationshipNode
	}

	/**
	 * Generates a WorkflowException if the AAI query returns a response code other than 200.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void handleAAIQueryFailure(Execution execution) {
		def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		logError('Error occurred attempting to query AAI, Response Code ' +
			execution.getVariable('CAAIVfModVG_getVfModuleResponseCode') + ', Error Response ' +
			execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
		ExceptionUtil exceptionUtil = new ExceptionUtil()
		exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))

		logDebug('Exited ' + method, isDebugLogEnabled)
	}

	/**
	 * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
	 *
	 * @param execution The flow's execution instance.
	 */
	public void handleUpdateVfModuleFailure(Execution execution) {
		def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		logError('Error occurred attempting to update VF Module in AAI, Response Code ' +
			execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode') + ', Error Response ' +
			execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
		ExceptionUtil exceptionUtil = new ExceptionUtil()
		exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))

		logDebug('Exited ' + method, isDebugLogEnabled)
	}
}