aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/onap/so/bpmn/common/scripts/AllottedResourceUtils.groovy
blob: 4df38edcc37595561a13cf505edded29e9053c7f (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.onap.so.bpmn.common.scripts

import org.onap.so.logger.LoggingAnchor
import org.onap.so.client.aai.entities.AAIResultWrapper
import org.onap.so.logger.ErrorCode

import static org.apache.commons.lang3.StringUtils.isBlank;

import javax.ws.rs.NotFoundException
import javax.ws.rs.core.UriBuilder

import org.apache.commons.lang.StringUtils
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.aai.domain.yang.AllottedResource
import org.onap.so.bpmn.core.WorkflowException
import org.onap.so.client.PreconditionFailedException
import org.onap.so.client.aai.AAIObjectType
import org.onap.so.client.aai.AAIResourcesClient
import org.onap.so.client.aai.entities.uri.AAIResourceUri
import org.onap.so.client.aai.entities.uri.AAIUriFactory
import org.onap.so.logger.MessageEnum
import org.slf4j.Logger
import org.slf4j.LoggerFactory



class AllottedResourceUtils {
    private static final Logger logger = LoggerFactory.getLogger( AllottedResourceUtils.class);


	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 (DelegateExecution execution) {

		logger.trace("getAROrchStatus ")
		String msg = ""
		String serviceInstanceId = execution.getVariable("serviceInstanceId")
		String arType = execution.getVariable("allottedResourceType")
		String arRole = execution.getVariable("allottedResourceRole")
		String siXml = execution.getVariable("CSI_service")
		String orchStatus = null
		XmlParser xmlParser = new XmlParser()
		logger.debug("getAROrchStatus siXml:" + siXml)
		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'))) {
							logger.debug("getARORchStatus AR found")
							def groovy.util.Node relatedLink = utils.getChildNode(relationship, 'related-link')
							if (relatedLink != null){
								Optional<AllottedResource> ar = getARbyLink(execution, relatedLink.text(), arRole)
								if (ar.isPresent()){
									orchStatus = execution.getVariable("aaiAROrchStatus")
									break
								}
							}
						}
					}
				}
			}
		}catch(Exception e){
			logger.debug(" Error encountered in getAROrchStatus" + e.getMessage())
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in getAROrchStatus" + e.getMessage())
		}
		logger.trace(" Exit getAROrchStatus - OrchStatus:" + orchStatus)
		return orchStatus
	}

	// get Allotted Resource by AllottedResourceId
	// used on Delete - called from doDeleteAR
	// setsVariable aaiARGetResponse
	public boolean ifExistsAR(DelegateExecution execution, String allottedResourceId) {
		logger.trace("ifExistsAR ")
		try {
			AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE, allottedResourceId)
            AAIResultWrapper wrapper = getAAIClient().get(resourceUri)
            Optional<AllottedResource> allottedResource = wrapper.asBean(AllottedResource.class)
            if(allottedResource.isPresent()) {
                setExecutionVariables(execution , allottedResource.get(),resourceUri)
                return true
            }else {
                return false
            }
		}catch(Exception e){
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in ifExistsAR" + e.getMessage())
		}
	}

	public String getPSIFmARLink(DelegateExecution execution, String arLink)
	{
		// Path: /aai/{version}/business/customers/customer/{cust}/service-subscriptions/service-subscription/{subs}/service-instances/service-instance/{psiid}/allotted-resources/allotted-resource/{arid}
		logger.trace(" getPSIFmARLink - path:" + arLink)
		String[] split = arLink.split("/service-instance/")
		String[] splitB =  split[1].split("/allotted-resources/")
		String siId = splitB[0]
		logger.trace(" Exit getPSIFmARLink - parentServiceInstanceId:" + siId )
		return siId
	}

	// get Allotted resource using Link
	// used on Create called from getARORchStatus
	// used on Delete called from ifExistsAR
	// setsVariable aaiARPath - used for Patch in create

	public Optional<AllottedResource> getARbyLink (DelegateExecution execution, String link, String role) {
		logger.trace("getARbyLink ")
		Optional<AllottedResource> allottedResource = Optional.empty()
		try {
			logger.debug("GET AR Aai Path is: \n" + link)
			AAIResourceUri uri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.ALLOTTED_RESOURCE, UriBuilder.fromPath(link).build())
			allottedResource = getAAIClient().get(AllottedResource.class,uri);
			if(allottedResource.isPresent()) {
				if (!isBlank(role)) {
					if (role == allottedResource.get().getRole()) {
						setExecutionVariables(execution,allottedResource.get(),uri)
					} else {
						logger.debug("AAI AR does not match input role:" + role)
					}
				} else {
					setExecutionVariables(execution,allottedResource.get(),uri)
				}
			}else{
				logger.debug("GET AR received a Not Found (404) Response")
			}
		}catch(Exception e){
			logger.debug(" Error encountered within GetAaiAR" + e.getMessage())
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetAaiAR" + e.getMessage())
		}
		return allottedResource
	}

	public void setExecutionVariables(DelegateExecution execution, AllottedResource ar, AAIResourceUri arUrl) {
		execution.setVariable("aaiARGetResponse", ar)
		execution.setVariable("aaiARPath", arUrl.build().toString())
		execution.setVariable("aaiARResourceVersion", ar.getResourceVersion())
		if (StringUtils.isNotEmpty(ar.getOrchestrationStatus())) {
			execution.setVariable("aaiAROrchStatus", ar.getOrchestrationStatus())
		}
		else
		{
			execution.setVariable("aaiAROrchStatus", " ")
		}
	}

	public void updateAROrchStatus(DelegateExecution execution, String status, String aaiARPath){
		logger.trace("updaAROrchStatus ")
		try{

			AllottedResource allottedResource = new AllottedResource();
			allottedResource.setOrchestrationStatus(status)
			logger.debug('AAI AR URI: ' + aaiARPath)

			AAIResourceUri uri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.ALLOTTED_RESOURCE, UriBuilder.fromPath(aaiARPath).build())
			getAAIClient().update(uri,allottedResource)
		}catch(Exception e){
			logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
					"Exception in updateAR.", "BPMN", ErrorCode.UnknownError.getValue(), e.getMessage());
			exceptionUtil.buildAndThrowWorkflowException(execution, 500, 'Internal Error in updateAROrchStatus.' + e.getMessage())
		}
		logger.trace("Exit updateAROrchStatus ")
	}

	//Sets Variable "wasDeleted"
	public void deleteAR(DelegateExecution execution, String aaiARPath){
		logger.trace(" deleteAR - aaiARPath:" + aaiARPath)
		try {

			AAIResourceUri uri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.ALLOTTED_RESOURCE, UriBuilder.fromPath(aaiARPath).build())
			getAAIClient().delete(uri);
		}catch(NotFoundException ex){
			logger.debug("  Delete AR Received a Not Found (404) Response")
		}catch(PreconditionFailedException ex){
			logger.debug("Delete AR Received a Resource Version Mismatch Error: \n")
			exceptionUtil.buildAndThrowWorkflowException(execution, 412, "DeleteAR Received a resource-version Mismatch Error Response from AAI")
		}catch(Exception e){
			logger.debug(" Error encountered in deleteAR!" + e)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete AR")
		}
		logger.debug("  Delete AR Received a Good Response")
		execution.setVariable("wasDeleted", "true")
		logger.trace("Exit deleteAR ")
	}

	public void buildAAIErrorResponse(DelegateExecution execution, String response, String errorMessage){
		logger.trace("BuildAAIErrorResponse")

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

		logger.trace("Exit BuildAAIErrorResponse Process")
		throw new BpmnError("MSOWorkflowException")
	}

	public  AAIResourcesClient getAAIClient(){
		return new AAIResourcesClient()
	}

}