aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/AllottedResourceUtils.groovy
blob: 540fe036fc38cc56d22e1db4b78756dae9e6cada (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
package org.openecomp.mso.bpmn.common.scripts

import org.apache.commons.lang3.StringEscapeUtils;
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.runtime.Execution;
import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.rest.APIResponse;

class AllottedResourceUtils {
	
	private AbstractServiceTaskProcessor taskProcessor
	ExceptionUtil exceptionUtil = new ExceptionUtil()
	MsoUtils utils;

	public AllottedResourceUtils(AbstractServiceTaskProcessor taskProcessor) {
		this.taskProcessor = taskProcessor
		this.utils = taskProcessor.utils
	}

	/*Used on Create - called from DoCreate
	* Using Consuming ServiceInstanceId get related Allotted Resources Orchestration status from AAI
	* 1) get related AR links for CSI 2) get AR from AR links
	* return: null -> AR Not found
	* return: " " -> AR found with empty orchStatus
	* return: orchStatus - > AR found with this orchStatus
	* setsVariable aaiARGetResponse
	*/
	public String getAROrchStatus (Execution execution) {

		def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG"," ***** getAROrchStatus *****", isDebugEnabled)
		String msg = ""
		String serviceInstanceId = execution.getVariable("serviceInstanceId")
		String arType = execution.getVariable("allottedResourceType")
		String arRole = execution.getVariable("allottedResourceRole")
		String siXml = execution.getVariable("CSI_service")
		String ar = null
		String orchStatus = null
		XmlParser xmlParser = new XmlParser()
		utils.log("DEBUG","getAROrchStatus siXml:" + siXml, isDebugEnabled)
		try {
			if (!isBlank(siXml)) {
				def groovy.util.Node siNode = xmlParser.parseText(siXml)
				def groovy.util.Node relationshipList = utils.getChildNode(siNode, 'relationship-list')
				if (relationshipList != null) {
					def groovy.util.NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
					for (groovy.util.Node relationship in relationships) {
						def groovy.util.Node relatedTo = utils.getChildNode(relationship, 'related-to')
						if ((relatedTo != null) && (relatedTo.text().equals('allotted-resource'))) {
							utils.log("DEBUG","getARORchStatus AR found", isDebugEnabled)
							def groovy.util.Node relatedLink = utils.getChildNode(relationship, 'related-link')
							if (relatedLink != null){
								ar = getARbyLink(execution, relatedLink.text(), arRole)
								if (!isBlank(ar))
								{
									orchStatus = execution.getVariable("aaiAROrchStatus")
									break
								}
							}
						}
					}
				}
			}
		}catch(Exception e){
			utils.log("DEBUG", " Error encountered in getAROrchStatus" + e.getMessage(), isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in getAROrchStatus" + e.getMessage())
		}
		utils.log("DEBUG"," *****Exit getAROrchStatus **** OrchStatus:" + orchStatus, isDebugEnabled)
		return orchStatus
	}

	// get Allotted Resource by AllottedResourceId
	// used on Delete - called from doDeleteAR
	// setsVariable aaiARGetResponse
	public String getARbyId (Execution execution, String allottedResourceId) {
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG", " ***** getARbyId ***** ", isDebugEnabled)
		String arLink = getARLinkbyId(execution, allottedResourceId)
		String ar = null
		if (!isBlank(arLink))
		{
			ar = getARbyLink(execution, arLink, "")
		}
		utils.log("DEBUG", " ***** Exit GetARbyId ***** AR:" + ar, isDebugEnabled)
		return ar;
	}

	// get Allotted Resource Link by AllottedResourceId using Nodes Query
	// used on Delete - called from getARbyId
	public String getARLinkbyId (Execution execution, String allottedResourceId) {
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG", " ***** getARLinkbyId ***** ", isDebugEnabled)
		String arLink = null
		try {
			AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
			String aaiNQUri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
			String aaiEndpoint = execution.getVariable("URN_aai_endpoint")
			String aaiUrl = "${aaiNQUri}?search-node-type=allotted-resource&filter=id:EQUALS:${allottedResourceId}"

			utils.log("DEBUG", "getARLinkbyId url: \n" + aaiUrl, isDebugEnabled)

			APIResponse response = aaiUriUtil.executeAAIGetCall(execution, aaiUrl)
			int responseCode = response.getStatusCode()
			utils.log("DEBUG", "  GET AR response code is: " + responseCode, isDebugEnabled)

			String aaiResponse = response.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			utils.log("DEBUG", "GET AR:" + aaiResponse, isDebugEnabled)
			if(responseCode == 200 || responseCode == 202){
				utils.log("DEBUG", "GET AR Received a Good Response Code", isDebugEnabled)
				if(utils.nodeExists(aaiResponse, "result-data")){
					utils.log("DEBUG", "Query for AllottedResource Url Response Does Contain Data" , isDebugEnabled)
					arLink = utils.getNodeText1(aaiResponse, "resource-link")
				}else{
					utils.log("DEBUG", "GET AR Response Does NOT Contain Data" , isDebugEnabled)
				}
			}else if(responseCode == 404){
				utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)
			}
			else{
				utils.log("DEBUG", "  GET AR received a Bad Response: \n" + aaiResponse, isDebugEnabled)
				buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")
			}
		}catch(Exception e){
			utils.log("DEBUG", " Error encountered within GetAaiAR" + e.getMessage(), isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetARbyId" + e.getMessage())
		}
		utils.log("DEBUG", " ***** Exit GetARLinkbyId ***** Link:" + arLink, isDebugEnabled)
		return arLink
	}

	// get Allotted resource using Link
	// used on Create called from getARORchStatus
	// used on Delete called from getARbyId
	// setsVariable aaiARPath - used for Patch in create
	public String getARbyLink (Execution execution, String link, String role) {
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG", " ***** getARbyLink ***** ", isDebugEnabled)
		String ar = null
		String arUrl = null
		try {
			AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
			String aai_endpoint = execution.getVariable("URN_aai_endpoint")
			String arEndpoint = ""

			if(!isBlank(link)) {
				utils.log("DEBUG", "Incoming AR Resource Link is: " + link, isDebugEnabled)
				String[] split = link.split("/aai/")
				arEndpoint = "/aai/" + split[1]
			}

			arUrl = "${aai_endpoint}" + arEndpoint
		
			utils.log("DEBUG", "GET AR Aai Path is: \n" + arUrl, isDebugEnabled)

			APIResponse response = aaiUriUtil.executeAAIGetCall(execution, arUrl)
			int responseCode = response.getStatusCode()
			utils.log("DEBUG", "  GET AR response code is: " + responseCode, isDebugEnabled)

			String aaiResponse = response.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			utils.log("DEBUG", "GET AR:" + aaiResponse, isDebugEnabled)
			if(responseCode == 200 || responseCode == 202){
				utils.log("DEBUG", "GET AR Received a Good Response Code", isDebugEnabled)
				if(utils.nodeExists(aaiResponse, "allotted-resource")){
					if (!isBlank(role))
					{
						if (utils.nodeExists(aaiResponse, "role") && role.equals(utils.getNodeText1(aaiResponse, "role"))) {
							ar = aaiResponse
						}else{
							utils.log("DEBUG", "AAI AR does not match input role:" + role, isDebugEnabled)
						}
					}
					else
					{
						ar = aaiResponse
					}
				}
				else
				{
					utils.log("DEBUG", "GET AR Does NOT Contain Data" , isDebugEnabled)
				}
			}else if(responseCode == 404){
				utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)
			}
			else{
				utils.log("DEBUG", "  GET AR received a Bad Response: \n" + aaiResponse, isDebugEnabled)
				buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")
			}
		}catch(Exception e){
			utils.log("DEBUG", " Error encountered within GetAaiAR" + e.getMessage(), isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetAaiAR" + e.getMessage())
		}
		if (!isBlank(ar))
		{
			execution.setVariable("aaiARGetResponse", ar)
			execution.setVariable("aaiARPath", arUrl)
			
			String resourceVersion = null
			if (utils.nodeExists(ar, "resource-version")) {
				resourceVersion = utils.getNodeText1(ar, "resource-version")
				execution.setVariable("aaiARResourceVersion", resourceVersion)
			}
			
			String orchStatus = null
			if (utils.nodeExists(ar, "orchestration-status")) {
				orchStatus= utils.getNodeText1(ar, "orchestration-status")
			}
			else
			{
				orchStatus = " "
			}
			execution.setVariable("aaiAROrchStatus", orchStatus)
		}
		utils.log("DEBUG", " ***** Exit GetARbyLink ***** AR:" + ar, isDebugEnabled)
		return ar
	}

	public void updateAROrchStatus(Execution execution, String status, String aaiARPath){
		def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG", " *** updaAROrchStatus *** ", isDebugEnabled)
		try{

			String updateReq =	"""
				{
				"orchestration-status": "Created""
				}
				"""

			utils.log("DEBUG", 'AAI AR URI: ' + aaiARPath, isDebugEnabled)

			AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
			APIResponse apiResponse = aaiUriUtil.executeAAIPatchCall(execution, aaiARPath, updateReq)
			def aaiResponse = StringEscapeUtils.unescapeXml(apiResponse.getResponseBodyAsString())
			def responseCode = apiResponse.getStatusCode()

			utils.logAudit("AAI Response Code: " + responseCode)
			utils.logAudit("AAI Response: " + aaiResponse)
			if(responseCode == 200){
				utils.log("DEBUG", "UpdateAR Good REST Response is: " + "\n" + aaiResponse, isDebugEnabled)
			}else{
				utils.log("DEBUG", "UpdateAROrchStatus Bad REST Response!", isDebugEnabled)
				buildAAIErrorResponse(execution, aaiResponse, "Error updating AR OrchStatus in AAI")
			}

		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException ", isDebugEnabled)
			throw b
		}catch(Exception e){
			utils.log("ERROR", "Exception in updateAR. Exception is:\n" + e.getMessage(), isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 500, 'Internal Error in updateAROrchStatus.' + e.getMessage())
		}
		utils.log("DEBUG", " *** Exit updateAROrchStatus *** ", isDebugEnabled)
	}
	
	//Sets Variable "wasDeleted"
	public void deleteAR(Execution execution, String aaiARPath){
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG", " *** deleteAR *** aaiARPath:" + aaiARPath, isDebugEnabled)
		try {
			AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
			APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, aaiARPath)
			int responseCode = response.getStatusCode()
			execution.setVariable("deleteARResponseCode", responseCode)
			
			utils.log("DEBUG", "  Delete AR response code:" + responseCode, isDebugEnabled)

			String aaiResponse = response.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			execution.setVariable("aaiARDeleteResponse", aaiResponse)

			utils.log("DEBUG", "Delete AR Response:" + aaiResponse)
			//Process Response
			if(responseCode == 204){
				utils.log("DEBUG", "  Delete AR Received a Good Response", isDebugEnabled)
				execution.setVariable("wasDeleted", "true")
			}else if(responseCode == 404){
				utils.log("DEBUG", "  Delete AR Received a Not Found (404) Response", isDebugEnabled)
			}else if(responseCode == 412){
				utils.log("DEBUG", "Delete AR Received a Resource Version Mismatch Error: \n" + aaiResponse, isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 412, "DeleteAR Received a resource-version Mismatch Error Response from AAI")
			}else{
				utils.log("DEBUG", "Delete AR Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
				buildAAIErrorResponse(execution, aaiResponse, "Error deleting AR in AAI")
				exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
			}
		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
			throw b
		}catch(Exception e){
			utils.log("DEBUG", " Error encountered in deleteAR!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete AR")
		}
		utils.log("DEBUG", " *** Exit deleteAR *** ", isDebugEnabled)
	}

	public void buildAAIErrorResponse(Execution execution, String response, String errorMessage){
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG", " *** BuildAAIErrorResponse*** ", isDebugEnabled)

		if((response != null) && (response.contains("Fault") || response.contains("RESTFault"))){
			WorkflowException workflowException = exceptionUtil.MapAAIExceptionToWorkflowException(response, execution)
			execution.setVariable("WorkflowException", workflowException)
		}else{
			exceptionUtil.buildWorkflowException(execution, 500, errorMessage)
		}

		utils.log("DEBUG", " *** Exit BuildAAIErrorResponse Process*** ", isDebugEnabled)
		throw new BpmnError("MSOWorkflowException")
	}

}