2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.common.scripts
25 import org.onap.so.client.aai.entities.AAIResultWrapper
26 import org.onap.so.logger.ErrorCode
28 import static org.apache.commons.lang3.StringUtils.isBlank;
30 import javax.ws.rs.NotFoundException
31 import javax.ws.rs.core.UriBuilder
33 import org.apache.commons.lang.StringUtils
34 import org.camunda.bpm.engine.delegate.BpmnError
35 import org.camunda.bpm.engine.delegate.DelegateExecution
36 import org.onap.aai.domain.yang.AllottedResource
37 import org.onap.so.bpmn.core.WorkflowException
38 import org.onap.so.client.PreconditionFailedException
39 import org.onap.so.client.aai.AAIObjectType
40 import org.onap.so.client.aai.AAIResourcesClient
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory
43 import org.onap.so.logger.MessageEnum
44 import org.slf4j.Logger
45 import org.slf4j.LoggerFactory
49 class AllottedResourceUtils {
50 private static final Logger logger = LoggerFactory.getLogger( AllottedResourceUtils.class);
53 private AbstractServiceTaskProcessor taskProcessor
54 ExceptionUtil exceptionUtil = new ExceptionUtil()
57 public AllottedResourceUtils(AbstractServiceTaskProcessor taskProcessor) {
58 this.taskProcessor = taskProcessor
59 this.utils = taskProcessor.utils
62 /*Used on Create - called from DoCreate
63 * Using Consuming ServiceInstanceId get related Allotted Resources Orchestration status from AAI
64 * 1) get related AR links for CSI 2) get AR from AR links
65 * return: null -> AR Not found
66 * return: " " -> AR found with empty orchStatus
67 * return: orchStatus - > AR found with this orchStatus
68 * setsVariable aaiARGetResponse
70 public String getAROrchStatus (DelegateExecution execution) {
72 logger.trace("getAROrchStatus ")
74 String serviceInstanceId = execution.getVariable("serviceInstanceId")
75 String arType = execution.getVariable("allottedResourceType")
76 String arRole = execution.getVariable("allottedResourceRole")
77 String siXml = execution.getVariable("CSI_service")
78 String orchStatus = null
79 XmlParser xmlParser = new XmlParser()
80 logger.debug("getAROrchStatus siXml:" + siXml)
82 if (!isBlank(siXml)) {
83 def groovy.util.Node siNode = xmlParser.parseText(siXml)
84 def groovy.util.Node relationshipList = utils.getChildNode(siNode, 'relationship-list')
85 if (relationshipList != null) {
86 def groovy.util.NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
87 for (groovy.util.Node relationship in relationships) {
88 def groovy.util.Node relatedTo = utils.getChildNode(relationship, 'related-to')
89 if ((relatedTo != null) && (relatedTo.text().equals('allotted-resource'))) {
90 logger.debug("getARORchStatus AR found")
91 def groovy.util.Node relatedLink = utils.getChildNode(relationship, 'related-link')
92 if (relatedLink != null){
93 Optional<AllottedResource> ar = getARbyLink(execution, relatedLink.text(), arRole)
95 orchStatus = execution.getVariable("aaiAROrchStatus")
104 logger.debug(" Error encountered in getAROrchStatus" + e.getMessage())
105 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in getAROrchStatus" + e.getMessage())
107 logger.trace(" Exit getAROrchStatus - OrchStatus:" + orchStatus)
111 // get Allotted Resource by AllottedResourceId
112 // used on Delete - called from doDeleteAR
113 // setsVariable aaiARGetResponse
114 public boolean ifExistsAR(DelegateExecution execution, String allottedResourceId) {
115 logger.trace("ifExistsAR ")
117 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE, allottedResourceId)
118 AAIResultWrapper wrapper = getAAIClient().get(resourceUri)
119 Optional<AllottedResource> allottedResource = wrapper.asBean(AllottedResource.class)
120 if(allottedResource.isPresent()) {
121 setExecutionVariables(execution , allottedResource.get(),resourceUri)
127 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in ifExistsAR" + e.getMessage())
131 public String getPSIFmARLink(DelegateExecution execution, String arLink)
133 // Path: /aai/{version}/business/customers/customer/{cust}/service-subscriptions/service-subscription/{subs}/service-instances/service-instance/{psiid}/allotted-resources/allotted-resource/{arid}
134 logger.trace(" getPSIFmARLink - path:" + arLink)
135 String[] split = arLink.split("/service-instance/")
136 String[] splitB = split[1].split("/allotted-resources/")
137 String siId = splitB[0]
138 logger.trace(" Exit getPSIFmARLink - parentServiceInstanceId:" + siId )
142 // get Allotted resource using Link
143 // used on Create called from getARORchStatus
144 // used on Delete called from ifExistsAR
145 // setsVariable aaiARPath - used for Patch in create
147 public Optional<AllottedResource> getARbyLink (DelegateExecution execution, String link, String role) {
148 logger.trace("getARbyLink ")
149 Optional<AllottedResource> allottedResource = Optional.empty()
151 logger.debug("GET AR Aai Path is: \n" + link)
152 AAIResourceUri uri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.ALLOTTED_RESOURCE, UriBuilder.fromPath(link).build())
153 allottedResource = getAAIClient().get(AllottedResource.class,uri);
154 if(allottedResource.isPresent()) {
155 if (!isBlank(role)) {
156 if (role == allottedResource.get().getRole()) {
157 setExecutionVariables(execution,allottedResource.get(),uri)
159 logger.debug("AAI AR does not match input role:" + role)
162 setExecutionVariables(execution,allottedResource.get(),uri)
165 logger.debug("GET AR received a Not Found (404) Response")
168 logger.debug(" Error encountered within GetAaiAR" + e.getMessage())
169 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetAaiAR" + e.getMessage())
171 return allottedResource
174 public void setExecutionVariables(DelegateExecution execution, AllottedResource ar, AAIResourceUri arUrl) {
175 execution.setVariable("aaiARGetResponse", ar)
176 execution.setVariable("aaiARPath", arUrl.build().toString())
177 execution.setVariable("aaiARResourceVersion", ar.getResourceVersion())
178 if (StringUtils.isNotEmpty(ar.getOrchestrationStatus())) {
179 execution.setVariable("aaiAROrchStatus", ar.getOrchestrationStatus())
183 execution.setVariable("aaiAROrchStatus", " ")
187 public void updateAROrchStatus(DelegateExecution execution, String status, String aaiARPath){
188 logger.trace("updaAROrchStatus ")
191 AllottedResource allottedResource = new AllottedResource();
192 allottedResource.setOrchestrationStatus(status)
193 logger.debug('AAI AR URI: ' + aaiARPath)
195 AAIResourceUri uri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.ALLOTTED_RESOURCE, UriBuilder.fromPath(aaiARPath).build())
196 getAAIClient().update(uri,allottedResource)
198 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
199 "Exception in updateAR.", "BPMN", ErrorCode.UnknownError.getValue(), e.getMessage());
200 exceptionUtil.buildAndThrowWorkflowException(execution, 500, 'Internal Error in updateAROrchStatus.' + e.getMessage())
202 logger.trace("Exit updateAROrchStatus ")
205 //Sets Variable "wasDeleted"
206 public void deleteAR(DelegateExecution execution, String aaiARPath){
207 logger.trace(" deleteAR - aaiARPath:" + aaiARPath)
210 AAIResourceUri uri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.ALLOTTED_RESOURCE, UriBuilder.fromPath(aaiARPath).build())
211 getAAIClient().delete(uri);
212 }catch(NotFoundException ex){
213 logger.debug(" Delete AR Received a Not Found (404) Response")
214 }catch(PreconditionFailedException ex){
215 logger.debug("Delete AR Received a Resource Version Mismatch Error: \n")
216 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "DeleteAR Received a resource-version Mismatch Error Response from AAI")
218 logger.debug(" Error encountered in deleteAR!" + e)
219 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete AR")
221 logger.debug(" Delete AR Received a Good Response")
222 execution.setVariable("wasDeleted", "true")
223 logger.trace("Exit deleteAR ")
226 public void buildAAIErrorResponse(DelegateExecution execution, String response, String errorMessage){
227 logger.trace("BuildAAIErrorResponse")
229 if((response != null) && (response.contains("Fault") || response.contains("RESTFault"))){
230 WorkflowException workflowException = exceptionUtil.MapAAIExceptionToWorkflowException(response, execution)
231 execution.setVariable("WorkflowException", workflowException)
233 exceptionUtil.buildWorkflowException(execution, 500, errorMessage)
236 logger.trace("Exit BuildAAIErrorResponse Process")
237 throw new BpmnError("MSOWorkflowException")
240 public AAIResourcesClient getAAIClient(){
241 return new AAIResourcesClient()