SO WorkflowAction refactor
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / AaiResourceIdValidator.java
1 package org.onap.so.bpmn.infrastructure.workflow.tasks;
2
3 import java.util.Map;
4 import java.util.Optional;
5 import org.onap.aai.domain.yang.GenericVnf;
6 import org.onap.aai.domain.yang.GenericVnfs;
7 import org.onap.aai.domain.yang.L3Network;
8 import org.onap.aai.domain.yang.ServiceInstance;
9 import org.onap.aai.domain.yang.ServiceInstances;
10 import org.onap.aai.domain.yang.VolumeGroup;
11 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
12 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
13 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
14 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.DuplicateNameException;
15 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.MultipleObjectsFoundException;
16 import org.onap.so.serviceinstancebeans.RequestDetails;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.springframework.stereotype.Component;
20
21 @Component
22 public class AaiResourceIdValidator {
23
24     private static final Logger LOGGER = LoggerFactory.getLogger(AaiResourceIdValidator.class);
25
26     private static final String SERVICE_INSTANCE = "serviceInstance";
27     private static final String NAME_EXISTS_WITH_DIFF_VERSION_ID = "(%s) and different version id (%s)";
28     private static final String WORKFLOW_ACTION_WAS_UNABLE_TO_VERIFY_IF_THE_INSTANCE_NAME_ALREADY_EXIST_IN_AAI =
29             "WorkflowAction was unable to verify if the instance name already exist in AAI.";
30     private static final String NAME_EXISTS_MULTIPLE =
31             "(%s) and multiple combination of model-version-id + service-type + global-customer-id";
32     private static final String NAME_EXISTS_WITH_DIFF_COMBINATION =
33             "(%s) and global-customer-id (%s), service-type (%s), model-version-id (%s)";
34     private static final String NAME_EXISTS_WITH_DIFF_CUSTOMIZATION_ID =
35             "(%s), same parent and different customization id (%s)";
36     private static final String NAME_EXISTS_WITH_DIFF_PARENT = "(%s) id (%s) and different parent relationship";
37
38
39     private final BBInputSetupUtils bbInputSetupUtils;
40
41     public AaiResourceIdValidator(BBInputSetupUtils bbInputSetupUtils) {
42         this.bbInputSetupUtils = bbInputSetupUtils;
43     }
44
45     protected String validateResourceIdInAAI(String generatedResourceId, WorkflowType type, String instanceName,
46             RequestDetails reqDetails, WorkflowResourceIds workflowResourceIds) throws Exception {
47         try {
48             if ("SERVICE".equalsIgnoreCase(type.toString())) {
49                 return validateServiceResourceIdInAAI(generatedResourceId, instanceName, reqDetails);
50             } else if ("NETWORK".equalsIgnoreCase(type.toString())) {
51                 return validateNetworkResourceIdInAAI(generatedResourceId, instanceName, reqDetails,
52                         workflowResourceIds);
53             } else if ("VNF".equalsIgnoreCase(type.toString())) {
54                 return validateVnfResourceIdInAAI(generatedResourceId, instanceName, reqDetails, workflowResourceIds);
55             } else if ("VFMODULE".equalsIgnoreCase(type.toString())) {
56                 return validateVfModuleResourceIdInAAI(generatedResourceId, instanceName, reqDetails,
57                         workflowResourceIds);
58             } else if ("VOLUMEGROUP".equalsIgnoreCase(type.toString())) {
59                 return validateVolumeGroupResourceIdInAAI(generatedResourceId, instanceName, reqDetails,
60                         workflowResourceIds);
61             } else if ("CONFIGURATION".equalsIgnoreCase(type.toString())) {
62                 return validateConfigurationResourceIdInAAI(generatedResourceId, instanceName, reqDetails,
63                         workflowResourceIds);
64             }
65             return generatedResourceId;
66         } catch (DuplicateNameException dne) {
67             throw dne;
68         } catch (Exception ex) {
69             LOGGER.error(WORKFLOW_ACTION_WAS_UNABLE_TO_VERIFY_IF_THE_INSTANCE_NAME_ALREADY_EXIST_IN_AAI, ex);
70             throw new IllegalStateException(
71                     WORKFLOW_ACTION_WAS_UNABLE_TO_VERIFY_IF_THE_INSTANCE_NAME_ALREADY_EXIST_IN_AAI);
72         }
73     }
74
75     protected String validateServiceResourceIdInAAI(String generatedResourceId, String instanceName,
76             RequestDetails reqDetails) throws DuplicateNameException {
77         String globalCustomerId = reqDetails.getSubscriberInfo().getGlobalSubscriberId();
78         String serviceType = reqDetails.getRequestParameters().getSubscriptionServiceType();
79         if (instanceName != null) {
80             Optional<ServiceInstance> serviceInstanceAAI =
81                     bbInputSetupUtils.getAAIServiceInstanceByName(globalCustomerId, serviceType, instanceName);
82             if (serviceInstanceAAI.isPresent()) {
83                 if (serviceInstanceAAI.get().getModelVersionId()
84                         .equalsIgnoreCase(reqDetails.getModelInfo().getModelVersionId())) {
85                     return serviceInstanceAAI.get().getServiceInstanceId();
86                 } else {
87                     throw new DuplicateNameException(SERVICE_INSTANCE, String.format(NAME_EXISTS_WITH_DIFF_VERSION_ID,
88                             instanceName, reqDetails.getModelInfo().getModelVersionId()));
89                 }
90             } else {
91                 ServiceInstances aaiServiceInstances =
92                         bbInputSetupUtils.getAAIServiceInstancesGloballyByName(instanceName);
93                 if (aaiServiceInstances != null) {
94                     if (aaiServiceInstances.getServiceInstance() != null
95                             && !aaiServiceInstances.getServiceInstance().isEmpty()) {
96                         if (aaiServiceInstances.getServiceInstance().size() > 1) {
97                             throw new DuplicateNameException(SERVICE_INSTANCE,
98                                     String.format(NAME_EXISTS_MULTIPLE, instanceName));
99                         } else {
100                             ServiceInstance si = aaiServiceInstances.getServiceInstance().stream().findFirst().get();
101                             Map<String, String> keys =
102                                     bbInputSetupUtils.getURIKeysFromServiceInstance(si.getServiceInstanceId());
103
104                             throw new DuplicateNameException(SERVICE_INSTANCE, String.format(
105                                     NAME_EXISTS_WITH_DIFF_COMBINATION, instanceName,
106                                     keys.get(AAIFluentTypeBuilder.Types.CUSTOMER.getUriParams().globalCustomerId),
107                                     keys.get(
108                                             AAIFluentTypeBuilder.Types.SERVICE_SUBSCRIPTION.getUriParams().serviceType),
109                                     si.getModelVersionId()));
110                         }
111                     }
112                 }
113             }
114         }
115         return generatedResourceId;
116     }
117
118     protected String validateNetworkResourceIdInAAI(String generatedResourceId, String instanceName,
119             RequestDetails reqDetails, WorkflowResourceIds workflowResourceIds)
120             throws DuplicateNameException, MultipleObjectsFoundException {
121         Optional<L3Network> network = bbInputSetupUtils
122                 .getRelatedNetworkByNameFromServiceInstance(workflowResourceIds.getServiceInstanceId(), instanceName);
123         if (network.isPresent()) {
124             if (network.get().getModelCustomizationId()
125                     .equalsIgnoreCase(reqDetails.getModelInfo().getModelCustomizationId())) {
126                 return network.get().getNetworkId();
127             } else {
128                 throw new DuplicateNameException("l3Network", String.format(NAME_EXISTS_WITH_DIFF_CUSTOMIZATION_ID,
129                         instanceName, network.get().getModelCustomizationId()));
130             }
131         }
132         if (bbInputSetupUtils.existsAAINetworksGloballyByName(instanceName)) {
133             throw new DuplicateNameException("l3Network", String.format(NAME_EXISTS_WITH_DIFF_PARENT, instanceName,
134                     workflowResourceIds.getServiceInstanceId()));
135         }
136         return generatedResourceId;
137     }
138
139     protected String validateVnfResourceIdInAAI(String generatedResourceId, String instanceName,
140             RequestDetails reqDetails, WorkflowResourceIds workflowResourceIds) throws DuplicateNameException {
141         Optional<GenericVnf> vnf = bbInputSetupUtils
142                 .getRelatedVnfByNameFromServiceInstance(workflowResourceIds.getServiceInstanceId(), instanceName);
143         if (vnf.isPresent()) {
144             if (vnf.get().getModelCustomizationId()
145                     .equalsIgnoreCase(reqDetails.getModelInfo().getModelCustomizationId())) {
146                 return vnf.get().getVnfId();
147             } else {
148                 throw new DuplicateNameException("generic-vnf", String.format(NAME_EXISTS_WITH_DIFF_CUSTOMIZATION_ID,
149                         instanceName, vnf.get().getModelCustomizationId()));
150             }
151         }
152         GenericVnfs vnfs = bbInputSetupUtils.getAAIVnfsGloballyByName(instanceName);
153         if (vnfs != null) {
154             throw new DuplicateNameException("generic-vnf",
155                     String.format(NAME_EXISTS_WITH_DIFF_PARENT, instanceName, vnfs.getGenericVnf().get(0).getVnfId()));
156         }
157         return generatedResourceId;
158     }
159
160     protected String validateVfModuleResourceIdInAAI(String generatedResourceId, String instanceName,
161             RequestDetails reqDetails, WorkflowResourceIds workflowResourceIds) throws DuplicateNameException {
162         GenericVnf vnf = bbInputSetupUtils.getAAIGenericVnf(workflowResourceIds.getVnfId());
163         if (vnf != null && vnf.getVfModules() != null) {
164             for (org.onap.aai.domain.yang.VfModule vfModule : vnf.getVfModules().getVfModule()) {
165                 if (vfModule.getVfModuleName().equalsIgnoreCase(instanceName)) {
166                     if (vfModule.getModelCustomizationId()
167                             .equalsIgnoreCase(reqDetails.getModelInfo().getModelCustomizationId())) {
168                         return vfModule.getVfModuleId();
169                     } else {
170                         throw new DuplicateNameException("vfModule",
171                                 String.format(NAME_EXISTS_WITH_DIFF_CUSTOMIZATION_ID, instanceName,
172                                         reqDetails.getModelInfo().getModelCustomizationId()));
173                     }
174                 }
175             }
176         }
177         if (bbInputSetupUtils.existsAAIVfModuleGloballyByName(instanceName)) {
178             throw new DuplicateNameException("vfModule", instanceName);
179         }
180         return generatedResourceId;
181     }
182
183     protected String validateVolumeGroupResourceIdInAAI(String generatedResourceId, String instanceName,
184             RequestDetails reqDetails, WorkflowResourceIds workflowResourceIds) throws DuplicateNameException {
185         Optional<VolumeGroup> volumeGroup =
186                 bbInputSetupUtils.getRelatedVolumeGroupByNameFromVnf(workflowResourceIds.getVnfId(), instanceName);
187         if (volumeGroup.isPresent()) {
188             if (volumeGroup.get().getVfModuleModelCustomizationId()
189                     .equalsIgnoreCase(reqDetails.getModelInfo().getModelCustomizationId())) {
190                 return volumeGroup.get().getVolumeGroupId();
191             } else {
192                 throw new DuplicateNameException("volumeGroup", volumeGroup.get().getVolumeGroupName());
193             }
194         }
195         if (bbInputSetupUtils.existsAAIVolumeGroupGloballyByName(instanceName)) {
196             throw new DuplicateNameException("volumeGroup", instanceName);
197         }
198         return generatedResourceId;
199     }
200
201     protected String validateConfigurationResourceIdInAAI(String generatedResourceId, String instanceName,
202             RequestDetails reqDetails, WorkflowResourceIds workflowResourceIds) throws DuplicateNameException {
203         Optional<org.onap.aai.domain.yang.Configuration> configuration =
204                 bbInputSetupUtils.getRelatedConfigurationByNameFromServiceInstance(
205                         workflowResourceIds.getServiceInstanceId(), instanceName);
206         if (configuration.isPresent()) {
207             if (configuration.get().getModelCustomizationId()
208                     .equalsIgnoreCase(reqDetails.getModelInfo().getModelCustomizationId())) {
209                 return configuration.get().getConfigurationId();
210             } else {
211                 throw new DuplicateNameException("configuration", String.format(NAME_EXISTS_WITH_DIFF_CUSTOMIZATION_ID,
212                         instanceName, configuration.get().getConfigurationId()));
213             }
214         }
215         if (bbInputSetupUtils.existsAAIConfigurationGloballyByName(instanceName)) {
216             throw new DuplicateNameException("configuration", instanceName);
217         }
218         return generatedResourceId;
219     }
220 }