210d5195e50183ad5e0fa8afabf178ebdb4253dc
[so.git] /
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  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  * ============LICENSE_END=========================================================
25  */
26
27 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
28
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.javatuples.Pair;
31 import org.onap.aaiclient.client.aai.AAICommonObjectMapperProvider;
32 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
33 import org.onap.aaiclient.client.aai.entities.Relationships;
34 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
35 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionExtractResourcesAAI;
36 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
42 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
43 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
44 import org.onap.so.client.exception.ExceptionBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
48 import java.util.List;
49 import java.util.Optional;
50 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
51
52 @Component
53 public class VnfEBBLoader {
54
55     private static final Logger logger = LoggerFactory.getLogger(VnfEBBLoader.class);
56
57     private final BBInputSetupUtils bbInputSetupUtils;
58     private final BBInputSetup bbInputSetup;
59     private final WorkflowActionExtractResourcesAAI workflowActionUtils;
60     private final ExceptionBuilder exceptionBuilder;
61
62     VnfEBBLoader(BBInputSetupUtils bbInputSetupUtils, BBInputSetup bbInputSetup,
63             WorkflowActionExtractResourcesAAI workflowActionUtils, ExceptionBuilder exceptionBuilder) {
64         this.bbInputSetupUtils = bbInputSetupUtils;
65         this.bbInputSetup = bbInputSetup;
66         this.workflowActionUtils = workflowActionUtils;
67         this.exceptionBuilder = exceptionBuilder;
68     }
69
70
71     public void traverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId, String vnfId,
72             List<Pair<WorkflowType, String>> aaiResourceIds) {
73         try {
74             org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI =
75                     bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
76             ServiceInstance serviceInstanceMSO = bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
77             resourceList.add(new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false));
78             if (serviceInstanceMSO.getVnfs() != null) {
79                 findVnfWithGivenId(serviceInstanceMSO, vnfId, aaiResourceIds, resourceList, execution);
80             }
81         } catch (Exception ex) {
82             logger.error("Exception in traverseAAIVnf", ex);
83             buildAndThrowException(execution,
84                     "Could not find existing Vnf or related Instances to execute the request on.");
85         }
86     }
87
88     public void customTraverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId,
89             String vnfId, List<Pair<WorkflowType, String>> aaiResourceIds) {
90         try {
91             org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI =
92                     bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
93             ServiceInstance serviceInstanceMSO = bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
94             resourceList.add(new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false));
95             if (serviceInstanceMSO.getVnfs() != null) {
96                 findVnfWithGivenIdAndAddCustomizationUUID(serviceInstanceMSO, vnfId, aaiResourceIds, resourceList,
97                         execution);
98             }
99         } catch (Exception ex) {
100             logger.error("Exception in customTraverseAAIVnf", ex);
101             buildAndThrowException(execution,
102                     "Could not find existing Vnf or related Instances to execute the request on.");
103         }
104
105     }
106
107     private void findVnfWithGivenId(ServiceInstance serviceInstanceMSO, String vnfId,
108             List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, DelegateExecution execution) {
109         for (GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
110             if (vnf.getVnfId().equals(vnfId)) {
111                 aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
112                 resourceList.add(new Resource(WorkflowType.VNF, vnf.getVnfId(), false));
113                 processVfModules(vnf, aaiResourceIds, resourceList, execution);
114                 processVolumeGroups(vnf, aaiResourceIds, resourceList);
115                 break;
116             }
117         }
118     }
119
120     private void findVnfWithGivenIdAndAddCustomizationUUID(ServiceInstance serviceInstanceMSO, String vnfId,
121             List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, DelegateExecution execution) {
122         for (GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
123             if (vnf.getVnfId().equals(vnfId)) {
124                 aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
125                 resourceList.add(new Resource(WorkflowType.VNF,
126                         bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId(), false));
127                 processVfModules(vnf, aaiResourceIds, resourceList, execution);
128                 processVolumeGroups(vnf, aaiResourceIds, resourceList);
129                 break;
130             }
131         }
132     }
133
134     private void findConfigurationsInsideVfModule(DelegateExecution execution, String vnfId, String vfModuleId,
135             List<Resource> resourceList, List<Pair<WorkflowType, String>> aaiResourceIds) {
136         try {
137             org.onap.aai.domain.yang.VfModule aaiVfModule = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId);
138             AAIResultWrapper vfModuleWrapper = new AAIResultWrapper(
139                     new AAICommonObjectMapperProvider().getMapper().writeValueAsString(aaiVfModule));
140             Optional<Relationships> relationshipsOp;
141             relationshipsOp = vfModuleWrapper.getRelationships();
142             if (relationshipsOp.isPresent()) {
143                 relationshipsOp = workflowActionUtils.extractRelationshipsVnfc(relationshipsOp.get());
144                 addConfigToResources(relationshipsOp, resourceList, aaiResourceIds);
145             }
146         } catch (Exception ex) {
147             logger.error("Exception in findConfigurationsInsideVfModule", ex);
148             buildAndThrowException(execution, "Failed to find Configuration object from the vfModule.");
149         }
150     }
151
152     private void processVfModules(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
153             List<Resource> resourceList, DelegateExecution execution) {
154         if (vnf.getVfModules() != null) {
155             for (VfModule vfModule : vnf.getVfModules()) {
156                 aaiResourceIds.add(new Pair<>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
157                 resourceList.add(new Resource(WorkflowType.VFMODULE, vfModule.getVfModuleId(), false));
158                 findConfigurationsInsideVfModule(execution, vnf.getVnfId(), vfModule.getVfModuleId(), resourceList,
159                         aaiResourceIds);
160             }
161         }
162     }
163
164     private void processVolumeGroups(GenericVnf vnf, List<Pair<WorkflowType, String>> aaiResourceIds,
165             List<Resource> resourceList) {
166         if (vnf.getVolumeGroups() != null) {
167             for (VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
168                 aaiResourceIds.add(new Pair<>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
169                 resourceList.add(new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false));
170             }
171         }
172     }
173
174     private void addConfigToResources(Optional<Relationships> relationshipsOp, List<Resource> resourceList,
175             List<Pair<WorkflowType, String>> aaiResourceIds) {
176         if (relationshipsOp.isPresent()) {
177             Optional<Configuration> config =
178                     workflowActionUtils.extractRelationshipsConfiguration(relationshipsOp.get());
179             if (config.isPresent()) {
180                 aaiResourceIds.add(new Pair<>(WorkflowType.CONFIGURATION, config.get().getConfigurationId()));
181                 resourceList.add(new Resource(WorkflowType.CONFIGURATION, config.get().getConfigurationId(), false));
182             }
183         }
184     }
185
186     private void buildAndThrowException(DelegateExecution execution, String msg) {
187         logger.error(msg);
188         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
189         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
190     }
191 }