Use resource list for SkipCDSBuildingBlockListener
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / VnfEBBLoader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
18  *
19  *      http://www.apache.org/licenses/LICENSE-2.0
20  *
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=========================================================
27  */
28
29 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
30
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.tasks.BBInputSetup;
44 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
45 import org.onap.so.client.exception.ExceptionBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.stereotype.Component;
49 import java.util.List;
50 import java.util.Optional;
51 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
52
53 @Component
54 public class VnfEBBLoader {
55
56     private static final Logger logger = LoggerFactory.getLogger(VnfEBBLoader.class);
57
58     private final BBInputSetupUtils bbInputSetupUtils;
59     private final BBInputSetup bbInputSetup;
60     private final WorkflowActionExtractResourcesAAI workflowActionUtils;
61     private final ExceptionBuilder exceptionBuilder;
62
63     VnfEBBLoader(BBInputSetupUtils bbInputSetupUtils, BBInputSetup bbInputSetup,
64             WorkflowActionExtractResourcesAAI workflowActionUtils, ExceptionBuilder exceptionBuilder) {
65         this.bbInputSetupUtils = bbInputSetupUtils;
66         this.bbInputSetup = bbInputSetup;
67         this.workflowActionUtils = workflowActionUtils;
68         this.exceptionBuilder = exceptionBuilder;
69     }
70
71
72     public void traverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId, String vnfId,
73             List<Pair<WorkflowType, String>> aaiResourceIds) {
74         try {
75             org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI =
76                     bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
77             ServiceInstance serviceInstanceMSO = bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
78             Resource serviceResource =
79                     new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false, null);
80             resourceList.add(serviceResource);
81             if (serviceInstanceMSO.getVnfs() != null) {
82                 findVnfWithGivenId(serviceInstanceMSO, vnfId, aaiResourceIds, resourceList, serviceResource, execution);
83             }
84         } catch (Exception ex) {
85             logger.error("Exception in traverseAAIVnf", ex);
86             buildAndThrowException(execution,
87                     "Could not find existing Vnf or related Instances to execute the request on.");
88         }
89     }
90
91     public void customTraverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId,
92             String vnfId, List<Pair<WorkflowType, String>> aaiResourceIds) {
93         try {
94             org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI =
95                     bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
96             ServiceInstance serviceInstanceMSO = bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
97             Resource serviceResource =
98                     new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false, null);
99             resourceList.add(serviceResource);
100             if (serviceInstanceMSO.getVnfs() != null) {
101                 findVnfWithGivenIdAndAddCustomizationUUID(serviceInstanceMSO, vnfId, aaiResourceIds, resourceList,
102                         serviceResource, execution);
103             }
104         } catch (Exception ex) {
105             logger.error("Exception in customTraverseAAIVnf", ex);
106             buildAndThrowException(execution,
107                     "Could not find existing Vnf or related Instances to execute the request on.");
108         }
109
110     }
111
112     private void findVnfWithGivenId(ServiceInstance serviceInstanceMSO, String vnfId,
113             List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, Resource serviceResource,
114             DelegateExecution execution) {
115         for (GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
116             if (vnf.getVnfId().equals(vnfId)) {
117                 aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
118                 Resource vnfResource = new Resource(WorkflowType.VNF, vnf.getVnfId(), false, serviceResource);
119                 org.onap.aai.domain.yang.GenericVnf aaiGenericVnf = bbInputSetupUtils.getAAIGenericVnf(vnfId);
120                 vnfResource.setModelCustomizationId(aaiGenericVnf.getModelCustomizationId());
121                 vnfResource.setModelVersionId(aaiGenericVnf.getModelVersionId());
122                 resourceList.add(vnfResource);
123                 processVfModules(vnf, aaiResourceIds, resourceList, vnfResource, execution);
124                 processVolumeGroups(vnf, aaiResourceIds, resourceList, vnfResource);
125                 break;
126             }
127         }
128     }
129
130     private void findVnfWithGivenIdAndAddCustomizationUUID(ServiceInstance serviceInstanceMSO, String vnfId,
131             List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, Resource serviceResource,
132             DelegateExecution execution) {
133         for (GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
134             if (vnf.getVnfId().equals(vnfId)) {
135                 aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
136                 org.onap.aai.domain.yang.GenericVnf aaiGenericVnf = bbInputSetupUtils.getAAIGenericVnf(vnfId);
137                 Resource vnfResource =
138                         new Resource(WorkflowType.VNF, aaiGenericVnf.getModelCustomizationId(), false, serviceResource);
139                 vnfResource.setModelCustomizationId(aaiGenericVnf.getModelCustomizationId());
140                 vnfResource.setModelVersionId(aaiGenericVnf.getModelVersionId());
141                 resourceList.add(vnfResource);
142                 processVfModules(vnf, aaiResourceIds, resourceList, vnfResource, execution);
143                 processVolumeGroups(vnf, aaiResourceIds, resourceList, vnfResource);
144                 break;
145             }
146         }
147     }
148
149     private void findConfigurationsInsideVfModule(DelegateExecution execution,
150             org.onap.aai.domain.yang.VfModule aaiVfModule, List<Resource> resourceList, Resource vfModuleResource,
151             List<Pair<WorkflowType, String>> aaiResourceIds) {
152         try {
153             AAIResultWrapper vfModuleWrapper = new AAIResultWrapper(
154                     new AAICommonObjectMapperProvider().getMapper().writeValueAsString(aaiVfModule));
155             Optional<Relationships> relationshipsOp;
156             relationshipsOp = vfModuleWrapper.getRelationships();
157             if (relationshipsOp.isPresent()) {
158                 relationshipsOp = workflowActionUtils.extractRelationshipsVnfc(relationshipsOp.get());
159                 addConfigToResources(relationshipsOp, resourceList, vfModuleResource, aaiResourceIds);
160             }
161         } catch (Exception ex) {
162             logger.error("Exception in findConfigurationsInsideVfModule", ex);
163             buildAndThrowException(execution, "Failed to find Configuration object from the vfModule.");
164         }
165     }
166
167     private void processVfModules(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
168             List<Resource> resourceList, Resource vnfResource, DelegateExecution execution) {
169         if (vnf.getVfModules() != null) {
170             for (VfModule vfModule : vnf.getVfModules()) {
171                 aaiResourceIds.add(new Pair<>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
172                 Resource vfModuleResource =
173                         new Resource(WorkflowType.VFMODULE, vfModule.getVfModuleId(), false, vnfResource);
174                 org.onap.aai.domain.yang.VfModule aaiVfModule =
175                         bbInputSetupUtils.getAAIVfModule(vnf.getVnfId(), vfModule.getVfModuleId());
176                 vfModuleResource.setModelInvariantId(aaiVfModule.getModelInvariantId());
177                 vfModuleResource.setModelCustomizationId(aaiVfModule.getModelCustomizationId());
178                 vfModuleResource.setBaseVfModule(aaiVfModule.isIsBaseVfModule());
179                 resourceList.add(vfModuleResource);
180                 findConfigurationsInsideVfModule(execution, aaiVfModule, resourceList, vfModuleResource,
181                         aaiResourceIds);
182             }
183         }
184     }
185
186     private void processVolumeGroups(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
187             List<Resource> resourceList, Resource vnfResource) {
188         if (vnf.getVolumeGroups() != null) {
189             for (VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
190                 aaiResourceIds.add(new Pair<>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
191                 resourceList.add(
192                         new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false, vnfResource));
193             }
194         }
195     }
196
197     private void addConfigToResources(Optional<Relationships> relationshipsOp, List<Resource> resourceList,
198             Resource vfModuleResource, List<Pair<WorkflowType, String>> aaiResourceIds) {
199         if (relationshipsOp.isPresent()) {
200             Optional<Configuration> config =
201                     workflowActionUtils.extractRelationshipsConfiguration(relationshipsOp.get());
202             if (config.isPresent()) {
203                 aaiResourceIds.add(new Pair<>(WorkflowType.CONFIGURATION, config.get().getConfigurationId()));
204                 resourceList.add(new Resource(WorkflowType.CONFIGURATION, config.get().getConfigurationId(), false,
205                         vfModuleResource));
206             }
207         }
208     }
209
210     private void buildAndThrowException(DelegateExecution execution, String msg) {
211         logger.error(msg);
212         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
213         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
214     }
215 }