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