b950eb41be4a3d2d1f2726e8e3070981f080a544
[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.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;
53
54 @Component
55 public class VnfEBBLoader {
56
57     private static final Logger logger = LoggerFactory.getLogger(VnfEBBLoader.class);
58
59     private final BBInputSetupUtils bbInputSetupUtils;
60     private final BBInputSetup bbInputSetup;
61     private final WorkflowActionExtractResourcesAAI workflowActionUtils;
62     private final ExceptionBuilder exceptionBuilder;
63
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;
70     }
71
72
73     public void traverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId, String vnfId,
74             List<Pair<WorkflowType, String>> aaiResourceIds) {
75         try {
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);
84             }
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.");
89         }
90     }
91
92     public void customTraverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId,
93             String vnfId, List<Pair<WorkflowType, String>> aaiResourceIds) {
94         try {
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);
104             }
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.");
109         }
110
111     }
112
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);
123                 break;
124             }
125         }
126     }
127
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);
139                 break;
140             }
141         }
142     }
143
144     private void findConfigurationsInsideVfModule(DelegateExecution execution, String vnfId, String vfModuleId,
145             List<Resource> resourceList, Resource vfModuleResource, List<Pair<WorkflowType, String>> aaiResourceIds) {
146         try {
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);
155             }
156         } catch (Exception ex) {
157             logger.error("Exception in findConfigurationsInsideVfModule", ex);
158             buildAndThrowException(execution, "Failed to find Configuration object from the vfModule.");
159         }
160     }
161
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);
174             }
175         }
176     }
177
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()));
183                 resourceList.add(
184                         new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false, vnfResource));
185             }
186         }
187     }
188
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,
197                         vfModuleResource));
198             }
199         }
200     }
201
202     private void buildAndThrowException(DelegateExecution execution, String msg) {
203         logger.error(msg);
204         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
205         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
206     }
207 }