c1d1d7ad5a2c74e290a7866710d4565e8ffe3001
[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                 resourceList.add(vnfResource);
120                 processVfModules(vnf, aaiResourceIds, resourceList, vnfResource, execution);
121                 processVolumeGroups(vnf, aaiResourceIds, resourceList, vnfResource);
122                 break;
123             }
124         }
125     }
126
127     private void findVnfWithGivenIdAndAddCustomizationUUID(ServiceInstance serviceInstanceMSO, String vnfId,
128             List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, Resource serviceResource,
129             DelegateExecution execution) {
130         for (GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
131             if (vnf.getVnfId().equals(vnfId)) {
132                 aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
133                 Resource vnfResource = new Resource(WorkflowType.VNF,
134                         bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId(), false, serviceResource);
135                 resourceList.add(vnfResource);
136                 processVfModules(vnf, aaiResourceIds, resourceList, vnfResource, execution);
137                 processVolumeGroups(vnf, aaiResourceIds, resourceList, vnfResource);
138                 break;
139             }
140         }
141     }
142
143     private void findConfigurationsInsideVfModule(DelegateExecution execution, String vnfId, String vfModuleId,
144             List<Resource> resourceList, Resource vfModuleResource, List<Pair<WorkflowType, String>> aaiResourceIds) {
145         try {
146             org.onap.aai.domain.yang.VfModule aaiVfModule = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId);
147             AAIResultWrapper vfModuleWrapper = new AAIResultWrapper(
148                     new AAICommonObjectMapperProvider().getMapper().writeValueAsString(aaiVfModule));
149             Optional<Relationships> relationshipsOp;
150             relationshipsOp = vfModuleWrapper.getRelationships();
151             if (relationshipsOp.isPresent()) {
152                 relationshipsOp = workflowActionUtils.extractRelationshipsVnfc(relationshipsOp.get());
153                 addConfigToResources(relationshipsOp, resourceList, vfModuleResource, aaiResourceIds);
154             }
155         } catch (Exception ex) {
156             logger.error("Exception in findConfigurationsInsideVfModule", ex);
157             buildAndThrowException(execution, "Failed to find Configuration object from the vfModule.");
158         }
159     }
160
161     private void processVfModules(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
162             List<Resource> resourceList, Resource vnfResource, DelegateExecution execution) {
163         if (vnf.getVfModules() != null) {
164             for (VfModule vfModule : vnf.getVfModules()) {
165                 aaiResourceIds.add(new Pair<>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
166                 Resource vfModuleResource =
167                         new Resource(WorkflowType.VFMODULE, vfModule.getVfModuleId(), false, vnfResource);
168                 resourceList.add(vfModuleResource);
169                 findConfigurationsInsideVfModule(execution, vnf.getVnfId(), vfModule.getVfModuleId(), resourceList,
170                         vfModuleResource, aaiResourceIds);
171             }
172         }
173     }
174
175     private void processVolumeGroups(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
176             List<Resource> resourceList, Resource vnfResource) {
177         if (vnf.getVolumeGroups() != null) {
178             for (VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
179                 aaiResourceIds.add(new Pair<>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
180                 resourceList.add(
181                         new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false, vnfResource));
182             }
183         }
184     }
185
186     private void addConfigToResources(Optional<Relationships> relationshipsOp, List<Resource> resourceList,
187             Resource vfModuleResource, List<Pair<WorkflowType, String>> aaiResourceIds) {
188         if (relationshipsOp.isPresent()) {
189             Optional<Configuration> config =
190                     workflowActionUtils.extractRelationshipsConfiguration(relationshipsOp.get());
191             if (config.isPresent()) {
192                 aaiResourceIds.add(new Pair<>(WorkflowType.CONFIGURATION, config.get().getConfigurationId()));
193                 resourceList.add(new Resource(WorkflowType.CONFIGURATION, config.get().getConfigurationId(), false,
194                         vfModuleResource));
195             }
196         }
197     }
198
199     private void buildAndThrowException(DelegateExecution execution, String msg) {
200         logger.error(msg);
201         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
202         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
203     }
204 }