7db93e797925ba57aa128017310326e921e08ab0
[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.aai.domain.yang.ServiceInstance;
32 import org.onap.aaiclient.client.aai.AAICommonObjectMapperProvider;
33 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
34 import org.onap.aaiclient.client.aai.entities.Relationships;
35 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
36 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionExtractResourcesAAI;
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.VfModule;
40 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
41 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
42 import org.onap.so.client.exception.ExceptionBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Component;
46 import java.util.List;
47 import java.util.Optional;
48 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
49
50 @Component
51 public class VnfEBBLoader {
52
53     private static final Logger logger = LoggerFactory.getLogger(VnfEBBLoader.class);
54
55     private final BBInputSetupUtils bbInputSetupUtils;
56     private final BBInputSetup bbInputSetup;
57     private final WorkflowActionExtractResourcesAAI workflowActionUtils;
58     private final ExceptionBuilder exceptionBuilder;
59
60     VnfEBBLoader(BBInputSetupUtils bbInputSetupUtils, BBInputSetup bbInputSetup,
61             WorkflowActionExtractResourcesAAI workflowActionUtils, ExceptionBuilder exceptionBuilder) {
62         this.bbInputSetupUtils = bbInputSetupUtils;
63         this.bbInputSetup = bbInputSetup;
64         this.workflowActionUtils = workflowActionUtils;
65         this.exceptionBuilder = exceptionBuilder;
66     }
67
68
69     public void traverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId, String vnfId,
70             List<Pair<WorkflowType, String>> aaiResourceIds) {
71         try {
72             ServiceInstance serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
73             org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstanceMSO =
74                     bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
75             resourceList.add(new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false));
76             if (serviceInstanceMSO.getVnfs() != null) {
77                 for (org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
78                     if (vnf.getVnfId().equals(vnfId)) {
79                         aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
80                         resourceList.add(new Resource(WorkflowType.VNF, vnf.getVnfId(), false));
81                         if (vnf.getVfModules() != null) {
82                             for (VfModule vfModule : vnf.getVfModules()) {
83                                 aaiResourceIds.add(new Pair<>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
84                                 resourceList.add(new Resource(WorkflowType.VFMODULE, vfModule.getVfModuleId(), false));
85                                 findConfigurationsInsideVfModule(execution, vnf.getVnfId(), vfModule.getVfModuleId(),
86                                         resourceList, aaiResourceIds);
87                             }
88                         }
89                         if (vnf.getVolumeGroups() != null) {
90                             for (org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup : vnf
91                                     .getVolumeGroups()) {
92                                 aaiResourceIds
93                                         .add(new Pair<>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
94                                 resourceList.add(
95                                         new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false));
96                             }
97                         }
98                         break;
99                     }
100                 }
101             }
102         } catch (Exception ex) {
103             logger.error("Exception in traverseAAIVnf", ex);
104             buildAndThrowException(execution,
105                     "Could not find existing Vnf or related Instances to execute the request on.");
106         }
107     }
108
109     public void customTraverseAAIVnf(DelegateExecution execution, List<Resource> resourceList, String serviceId,
110             String vnfId, List<Pair<WorkflowType, String>> aaiResourceIds) {
111         try {
112             ServiceInstance serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
113             org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstanceMSO =
114                     bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
115             resourceList.add(new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false));
116             if (serviceInstanceMSO.getVnfs() != null) {
117                 for (org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
118                     if (vnf.getVnfId().equals(vnfId)) {
119                         aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
120
121                         String vnfCustomizationUUID =
122                                 bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId();
123                         resourceList.add(new Resource(WorkflowType.VNF, vnfCustomizationUUID, false));
124
125                         if (vnf.getVfModules() != null) {
126                             for (VfModule vfModule : vnf.getVfModules()) {
127                                 aaiResourceIds.add(new Pair<>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
128                                 resourceList.add(new Resource(WorkflowType.VFMODULE, vfModule.getVfModuleId(), false));
129                                 findConfigurationsInsideVfModule(execution, vnf.getVnfId(), vfModule.getVfModuleId(),
130                                         resourceList, aaiResourceIds);
131                             }
132                         }
133                         if (vnf.getVolumeGroups() != null) {
134                             for (org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup : vnf
135                                     .getVolumeGroups()) {
136                                 aaiResourceIds
137                                         .add(new Pair<>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
138                                 resourceList.add(
139                                         new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false));
140                             }
141                         }
142                         break;
143                     }
144                 }
145             }
146         } catch (Exception ex) {
147             logger.error("Exception in customTraverseAAIVnf", ex);
148             buildAndThrowException(execution,
149                     "Could not find existing Vnf or related Instances to execute the request on.");
150         }
151
152     }
153
154     private void findConfigurationsInsideVfModule(DelegateExecution execution, String vnfId, String vfModuleId,
155             List<Resource> resourceList, List<Pair<WorkflowType, String>> aaiResourceIds) {
156         try {
157             org.onap.aai.domain.yang.VfModule aaiVfModule = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId);
158             AAIResultWrapper vfModuleWrapper = new AAIResultWrapper(
159                     new AAICommonObjectMapperProvider().getMapper().writeValueAsString(aaiVfModule));
160             Optional<Relationships> relationshipsOp;
161             relationshipsOp = vfModuleWrapper.getRelationships();
162             if (relationshipsOp.isPresent()) {
163                 relationshipsOp = workflowActionUtils.extractRelationshipsVnfc(relationshipsOp.get());
164                 if (relationshipsOp.isPresent()) {
165                     Optional<Configuration> config =
166                             workflowActionUtils.extractRelationshipsConfiguration(relationshipsOp.get());
167                     if (config.isPresent()) {
168                         aaiResourceIds.add(new Pair<>(WorkflowType.CONFIGURATION, config.get().getConfigurationId()));
169                         resourceList.add(
170                                 new Resource(WorkflowType.CONFIGURATION, config.get().getConfigurationId(), false));
171                     }
172                 }
173             }
174         } catch (Exception ex) {
175             logger.error("Exception in findConfigurationsInsideVfModule", ex);
176             buildAndThrowException(execution, "Failed to find Configuration object from the vfModule.");
177         }
178     }
179
180     private void buildAndThrowException(DelegateExecution execution, String msg) {
181         logger.error(msg);
182         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
183         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
184     }
185 }