2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 Nokia
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Modifications Copyright (c) 2021 Nokia
10 * ================================================================================
11 * Modifications Copyright (c) 2020 Tech Mahindra
12 * ================================================================================
13 * Modifications Copyright (c) 2021 Orange
14 * ================================================================================
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 * ============LICENSE_END=========================================================
29 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
32 import org.javatuples.Pair;
33 import org.onap.aaiclient.client.aai.AAICommonObjectMapperProvider;
34 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
35 import org.onap.aaiclient.client.aai.entities.Relationships;
36 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
37 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
43 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoVfModule;
44 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
45 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
46 import org.onap.so.client.exception.ExceptionBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
50 import java.util.List;
51 import java.util.Optional;
52 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
55 public class VnfEBBLoader {
57 private static final Logger logger = LoggerFactory.getLogger(VnfEBBLoader.class);
59 private final BBInputSetupUtils bbInputSetupUtils;
60 private final BBInputSetup bbInputSetup;
61 private final WorkflowActionExtractResourcesAAI workflowActionUtils;
62 private final ExceptionBuilder exceptionBuilder;
64 VnfEBBLoader(BBInputSetupUtils bbInputSetupUtils, BBInputSetup bbInputSetup,
65 WorkflowActionExtractResourcesAAI workflowActionUtils, ExceptionBuilder exceptionBuilder) {
66 this.bbInputSetupUtils = bbInputSetupUtils;
67 this.bbInputSetup = bbInputSetup;
68 this.workflowActionUtils = workflowActionUtils;
69 this.exceptionBuilder = exceptionBuilder;
73 public void traverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId, String vnfId,
74 List<Pair<WorkflowType, String>> aaiResourceIds) {
76 org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI =
77 bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
78 ServiceInstance serviceInstanceMSO = bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
79 Resource serviceResource =
80 new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false, null);
81 resourceList.add(serviceResource);
82 if (serviceInstanceMSO.getVnfs() != null) {
83 findVnfWithGivenId(serviceInstanceMSO, vnfId, aaiResourceIds, resourceList, serviceResource, execution);
85 } catch (Exception ex) {
86 logger.error("Exception in traverseAAIVnf", ex);
87 buildAndThrowException(execution,
88 "Could not find existing Vnf or related Instances to execute the request on.");
92 public void customTraverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId,
93 String vnfId, List<Pair<WorkflowType, String>> aaiResourceIds) {
95 org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI =
96 bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
97 ServiceInstance serviceInstanceMSO = bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
98 Resource serviceResource =
99 new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false, null);
100 resourceList.add(serviceResource);
101 if (serviceInstanceMSO.getVnfs() != null) {
102 findVnfWithGivenIdAndAddCustomizationUUID(serviceInstanceMSO, vnfId, aaiResourceIds, resourceList,
103 serviceResource, execution);
105 } catch (Exception ex) {
106 logger.error("Exception in customTraverseAAIVnf", ex);
107 buildAndThrowException(execution,
108 "Could not find existing Vnf or related Instances to execute the request on.");
113 private void findVnfWithGivenId(ServiceInstance serviceInstanceMSO, String vnfId,
114 List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, Resource serviceResource,
115 DelegateExecution execution) {
116 for (GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
117 if (vnf.getVnfId().equals(vnfId)) {
118 aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
119 Resource vnfResource = new Resource(WorkflowType.VNF, vnf.getVnfId(), false, serviceResource);
120 resourceList.add(vnfResource);
121 processVfModules(vnf, aaiResourceIds, resourceList, vnfResource, execution);
122 processVolumeGroups(vnf, aaiResourceIds, resourceList, vnfResource);
128 private void findVnfWithGivenIdAndAddCustomizationUUID(ServiceInstance serviceInstanceMSO, String vnfId,
129 List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, Resource serviceResource,
130 DelegateExecution execution) {
131 for (GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
132 if (vnf.getVnfId().equals(vnfId)) {
133 aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
134 Resource vnfResource = new Resource(WorkflowType.VNF,
135 bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId(), false, serviceResource);
136 resourceList.add(vnfResource);
137 processVfModules(vnf, aaiResourceIds, resourceList, vnfResource, execution);
138 processVolumeGroups(vnf, aaiResourceIds, resourceList, vnfResource);
144 private void findConfigurationsInsideVfModule(DelegateExecution execution, String vnfId, String vfModuleId,
145 List<Resource> resourceList, Resource vfModuleResource, List<Pair<WorkflowType, String>> aaiResourceIds) {
147 org.onap.aai.domain.yang.VfModule aaiVfModule = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId);
148 AAIResultWrapper vfModuleWrapper = new AAIResultWrapper(
149 new AAICommonObjectMapperProvider().getMapper().writeValueAsString(aaiVfModule));
150 Optional<Relationships> relationshipsOp;
151 relationshipsOp = vfModuleWrapper.getRelationships();
152 if (relationshipsOp.isPresent()) {
153 relationshipsOp = workflowActionUtils.extractRelationshipsVnfc(relationshipsOp.get());
154 addConfigToResources(relationshipsOp, resourceList, vfModuleResource, aaiResourceIds);
156 } catch (Exception ex) {
157 logger.error("Exception in findConfigurationsInsideVfModule", ex);
158 buildAndThrowException(execution, "Failed to find Configuration object from the vfModule.");
162 private void processVfModules(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
163 List<Resource> resourceList, Resource vnfResource, DelegateExecution execution) {
164 if (vnf.getVfModules() != null) {
165 for (VfModule vfModule : vnf.getVfModules()) {
166 aaiResourceIds.add(new Pair<>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
167 Resource vfModuleResource =
168 new Resource(WorkflowType.VFMODULE, vfModule.getVfModuleId(), false, vnfResource);
169 Optional.ofNullable(vfModule.getModelInfoVfModule()).map(ModelInfoVfModule::getIsBaseBoolean)
170 .ifPresent(vfModuleResource::setBaseVfModule);
171 resourceList.add(vfModuleResource);
172 findConfigurationsInsideVfModule(execution, vnf.getVnfId(), vfModule.getVfModuleId(), resourceList,
173 vfModuleResource, aaiResourceIds);
178 private void processVolumeGroups(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
179 List<Resource> resourceList, Resource vnfResource) {
180 if (vnf.getVolumeGroups() != null) {
181 for (VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
182 aaiResourceIds.add(new Pair<>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
184 new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false, vnfResource));
189 private void addConfigToResources(Optional<Relationships> relationshipsOp, List<Resource> resourceList,
190 Resource vfModuleResource, List<Pair<WorkflowType, String>> aaiResourceIds) {
191 if (relationshipsOp.isPresent()) {
192 Optional<Configuration> config =
193 workflowActionUtils.extractRelationshipsConfiguration(relationshipsOp.get());
194 if (config.isPresent()) {
195 aaiResourceIds.add(new Pair<>(WorkflowType.CONFIGURATION, config.get().getConfigurationId()));
196 resourceList.add(new Resource(WorkflowType.CONFIGURATION, config.get().getConfigurationId(), false,
202 private void buildAndThrowException(DelegateExecution execution, String msg) {
204 execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
205 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);