move population of instance names in requestdb
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / servicedecomposition / tasks / BBInputSetupUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.servicedecomposition.tasks;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import org.onap.aai.domain.yang.CloudRegion;
31 import org.onap.aai.domain.yang.Configuration;
32 import org.onap.aai.domain.yang.Configurations;
33 import org.onap.aai.domain.yang.GenericVnf;
34 import org.onap.aai.domain.yang.GenericVnfs;
35 import org.onap.aai.domain.yang.InstanceGroup;
36 import org.onap.aai.domain.yang.L3Network;
37 import org.onap.aai.domain.yang.L3Networks;
38 import org.onap.aai.domain.yang.ServiceInstance;
39 import org.onap.aai.domain.yang.ServiceInstances;
40 import org.onap.aai.domain.yang.ServiceSubscription;
41 import org.onap.aai.domain.yang.VfModule;
42 import org.onap.aai.domain.yang.VolumeGroup;
43 import org.onap.aai.domain.yang.VolumeGroups;
44 import org.onap.aai.domain.yang.VpnBinding;
45 import org.onap.so.bpmn.common.InjectionHelper;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
47 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
48 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.MultipleObjectsFoundException;
49 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.NoServiceInstanceFoundException;
50 import org.onap.so.client.aai.AAIObjectPlurals;
51 import org.onap.so.client.aai.AAIObjectType;
52 import org.onap.so.client.aai.AAIResourcesClient;
53 import org.onap.so.client.aai.entities.AAIResultWrapper;
54 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
55 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
56 import org.onap.so.client.graphinventory.entities.uri.Depth;
57 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
58 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
59 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
60 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
61 import org.onap.so.db.catalog.beans.Service;
62 import org.onap.so.db.catalog.beans.VfModuleCustomization;
63 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
64 import org.onap.so.db.catalog.client.CatalogDbClient;
65 import org.onap.so.db.request.beans.InfraActiveRequests;
66 import org.onap.so.db.request.beans.RequestProcessingData;
67 import org.onap.so.db.request.client.RequestsDbClient;
68 import org.onap.so.serviceinstancebeans.CloudConfiguration;
69 import org.onap.so.serviceinstancebeans.ModelType;
70 import org.onap.so.serviceinstancebeans.RelatedInstance;
71 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
72 import org.onap.so.serviceinstancebeans.RequestDetails;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75 import org.springframework.beans.factory.annotation.Autowired;
76 import org.springframework.stereotype.Component;
77 import com.fasterxml.jackson.core.JsonProcessingException;
78 import com.fasterxml.jackson.databind.DeserializationFeature;
79 import com.fasterxml.jackson.databind.ObjectMapper;
80 import com.fasterxml.jackson.databind.SerializationFeature;
81 import com.fasterxml.jackson.databind.type.TypeFactory;
82
83 @Component("BBInputSetupUtils")
84 public class BBInputSetupUtils {
85
86     private static final Logger logger = LoggerFactory.getLogger(BBInputSetupUtils.class);
87     private ObjectMapper objectMapper = new ObjectMapper();
88     private static final String REQUEST_ERROR = "Could not find request.";
89     private static final String DATA_LOAD_ERROR = "Could not process loading data from database";
90     private static final String DATA_PARSE_ERROR = "Could not parse data";
91     private static final String PROCESSING_DATA_NAME_EXECUTION_FLOWS = "flowExecutionPath";
92
93     @Autowired
94     protected CatalogDbClient catalogDbClient;
95
96     @Autowired
97     protected RequestsDbClient requestsDbClient;
98
99     @Autowired
100     protected InjectionHelper injectionHelper;
101
102     public RelatedInstance getRelatedInstanceByType(RequestDetails requestDetails, ModelType modelType) {
103         if (requestDetails.getRelatedInstanceList() != null) {
104             for (RelatedInstanceList relatedInstanceList : requestDetails.getRelatedInstanceList()) {
105                 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
106                 if (relatedInstance != null && relatedInstance.getModelInfo() != null
107                         && relatedInstance.getModelInfo().getModelType() != null
108                         && relatedInstance.getModelInfo().getModelType().equals(modelType)) {
109                     return relatedInstance;
110                 }
111             }
112         }
113         return null;
114     }
115
116     public void updateInfraActiveRequestVnfId(InfraActiveRequests request, String vnfId) {
117         if (request != null) {
118             request.setVnfId(vnfId);
119             this.requestsDbClient.updateInfraActiveRequests(request);
120         } else {
121             logger.debug(REQUEST_ERROR);
122         }
123     }
124
125     public void updateInfraActiveRequestVfModuleId(InfraActiveRequests request, String vfModuleId) {
126         if (request != null) {
127             request.setVfModuleId(vfModuleId);
128             this.requestsDbClient.updateInfraActiveRequests(request);
129         } else {
130             logger.debug(REQUEST_ERROR);
131         }
132     }
133
134     public void updateInfraActiveRequestVolumeGroupId(InfraActiveRequests request, String volumeGroupId) {
135         if (request != null) {
136             request.setVolumeGroupId(volumeGroupId);
137             this.requestsDbClient.updateInfraActiveRequests(request);
138         } else {
139             logger.debug(REQUEST_ERROR);
140         }
141     }
142
143     public void updateInfraActiveRequestNetworkId(InfraActiveRequests request, String networkId) {
144         if (request != null) {
145             request.setNetworkId(networkId);
146             this.requestsDbClient.updateInfraActiveRequests(request);
147         } else {
148             logger.debug(REQUEST_ERROR);
149         }
150     }
151
152     public void persistFlowExecutionPath(String requestId, List<ExecuteBuildingBlock> flowsToExecute) {
153
154         if (requestId != null) {
155             List<String> flows = new ArrayList<>();
156             ObjectMapper om = new ObjectMapper();
157             try {
158                 for (ExecuteBuildingBlock ebb : flowsToExecute) {
159                     flows.add(om.writeValueAsString(ebb));
160                 }
161             } catch (JsonProcessingException e) {
162                 logger.error(DATA_PARSE_ERROR, e);
163             }
164
165             this.requestsDbClient.persistProcessingData(flows.toString(), requestId);
166         } else {
167             logger.debug(REQUEST_ERROR);
168         }
169     }
170
171     public InfraActiveRequests loadInfraActiveRequestById(String requestId) {
172
173         return this.requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
174     }
175
176     public InfraActiveRequests loadOriginalInfraActiveRequestById(String requestId) {
177         return this.requestsDbClient.getInfraActiveRequestbyRequestId(
178                 this.requestsDbClient.getInfraActiveRequestbyRequestId(requestId).getOriginalRequestId());
179     }
180
181     public List<ExecuteBuildingBlock> loadOriginalFlowExecutionPath(String requestId) {
182         if (requestId != null) {
183             InfraActiveRequests request = loadInfraActiveRequestById(requestId);
184             if (request.getOriginalRequestId() != null) {
185                 RequestProcessingData requestProcessingData =
186                         this.requestsDbClient.getRequestProcessingDataBySoRequestIdAndName(
187                                 request.getOriginalRequestId(), PROCESSING_DATA_NAME_EXECUTION_FLOWS);
188                 try {
189                     ObjectMapper om = new ObjectMapper();
190                     TypeFactory typeFactory = objectMapper.getTypeFactory();
191                     return om.readValue(requestProcessingData.getValue(),
192                             typeFactory.constructCollectionType(List.class, ExecuteBuildingBlock.class));
193                 } catch (Exception e) {
194                     logger.error(DATA_LOAD_ERROR, e);
195                     throw new RuntimeException("Error Loading Original Request Data", e);
196                 }
197             } else {
198                 throw new RuntimeException("Original Request Id is null for record: " + requestId);
199             }
200         } else {
201             throw new RuntimeException("Null Request Id Passed in");
202         }
203     }
204
205     public Service getCatalogServiceByModelUUID(String modelUUID) {
206         return catalogDbClient.getServiceByID(modelUUID);
207     }
208
209     public Service getCatalogServiceByModelVersionAndModelInvariantUUID(String modelVersion,
210             String modelInvariantUUID) {
211         return catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(modelVersion, modelInvariantUUID);
212     }
213
214     public CollectionNetworkResourceCustomization getCatalogCollectionNetworkResourceCustByID(String key) {
215         return catalogDbClient.getCollectionNetworkResourceCustomizationByID(key);
216     }
217
218     public NetworkCollectionResourceCustomization getCatalogNetworkCollectionResourceCustByID(
219             String collectionCustomizationId) {
220         return catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCustomizationId);
221     }
222
223     public VfModuleCustomization getVfModuleCustomizationByModelCuztomizationUUID(String modelCustomizationUUID) {
224         return catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(modelCustomizationUUID);
225     }
226
227     public CvnfcConfigurationCustomization getCvnfcConfigurationCustomization(String serviceModelUUID,
228             String vnfCustomizationUuid, String vfModuleCustomizationUuid, String cvnfcCustomizationUuid) {
229         return catalogDbClient.getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid, vfModuleCustomizationUuid,
230                 cvnfcCustomizationUuid);
231     }
232
233     public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
234         return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
235     }
236
237     public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
238         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
239         return uri.getURIKeys();
240     }
241
242     protected RequestDetails getRequestDetails(String requestId) throws IOException {
243         if (requestId != null && !requestId.isEmpty()) {
244             InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
245             String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
246             objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
247             objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
248             return objectMapper.readValue(requestBody, RequestDetails.class);
249         } else {
250             return null;
251         }
252     }
253
254     protected InfraActiveRequests getInfraActiveRequest(String requestId) {
255         if (requestId != null && !requestId.isEmpty()) {
256             return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
257         } else {
258             return null;
259         }
260     }
261
262     protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration) {
263         if (cloudConfiguration != null) {
264             String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
265             String cloudOwner = cloudConfiguration.getCloudOwner();
266             if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
267                 return injectionHelper.getAaiClient().get(CloudRegion.class, AAIUriFactory
268                         .createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner, cloudRegionId).depth(Depth.TWO))
269                         .orElse(null);
270
271             } else {
272                 return null;
273             }
274         } else {
275             return null;
276         }
277     }
278
279     public InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
280         return injectionHelper.getAaiClient().get(InstanceGroup.class,
281                 AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId)).orElse(null);
282     }
283
284     public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
285         return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
286                 AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId)).orElse(null);
287     }
288
289     public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
290
291         if (globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null
292                 || subscriptionServiceType.equals("")) {
293             return null;
294         } else {
295             return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory
296                     .createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
297                     .orElse(null);
298         }
299
300     }
301
302     public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
303         return injectionHelper.getAaiClient()
304                 .get(ServiceInstance.class, AAIUriFactory
305                         .createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
306                 .orElse(null);
307     }
308
309     protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
310             String serviceInstanceId) {
311         return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory
312                 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId)
313                 .depth(Depth.TWO)).orElse(null);
314     }
315
316     protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
317             Customer customer) {
318
319         return injectionHelper.getAaiClient()
320                 .get(org.onap.aai.domain.yang.ServiceInstances.class,
321                         AAIUriFactory
322                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
323                                         customer.getServiceSubscription().getServiceType())
324                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
325                 .orElseGet(() -> {
326                     logger.debug("No Service Instance matched by name");
327                     return null;
328                 });
329     }
330
331     public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
332             Customer customer) throws Exception {
333         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
334         org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
335         aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);
336
337         if (aaiServiceInstances == null) {
338             return null;
339         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
340             throw new Exception("Multiple Service Instances Returned");
341         } else {
342             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
343         }
344         return aaiServiceInstance;
345     }
346
347     protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
348             String serviceInstanceName) {
349
350         return injectionHelper.getAaiClient()
351                 .get(ServiceInstances.class,
352                         AAIUriFactory
353                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
354                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
355                 .orElseGet(() -> {
356                     logger.debug("No Service Instance matched by name");
357                     return null;
358                 });
359     }
360
361     public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
362             String serviceInstanceName) throws MultipleObjectsFoundException {
363         ServiceInstance aaiServiceInstance = null;
364         ServiceInstances aaiServiceInstances = null;
365         aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
366
367         if (aaiServiceInstances == null) {
368             return Optional.empty();
369         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
370             String message = String.format(
371                     "Multiple service instances found for customer-id: %s, service-type: %s and service-instance-name: %s.",
372                     globalCustomerId, serviceType, serviceInstanceName);
373             throw new MultipleObjectsFoundException(message);
374         } else {
375             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
376         }
377         return Optional.of(aaiServiceInstance);
378     }
379
380     public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
381         return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
382     }
383
384     public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
385             String modelCustomizationUUID) {
386         return this.catalogDbClient
387                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
388     }
389
390     public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
391         AAIResourceUri clonedUri = aaiResourceUri.clone();
392         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
393     }
394
395     public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
396         AAIResourceUri clonedUri = aaiResourceUri.clone();
397         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
398     }
399
400     public Configuration getAAIConfiguration(String configurationId) {
401         return this.injectionHelper.getAaiClient()
402                 .get(Configuration.class,
403                         AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
404                 .orElseGet(() -> {
405                     logger.debug("No Configuration matched by id");
406                     return null;
407                 });
408     }
409
410     public GenericVnf getAAIGenericVnf(String vnfId) {
411
412         return this.injectionHelper.getAaiClient()
413                 .get(GenericVnf.class,
414                         AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
415                 .orElseGet(() -> {
416                     logger.debug("No Generic Vnf matched by id");
417                     return null;
418                 });
419     }
420
421     public VpnBinding getAAIVpnBinding(String vpnBindingId) {
422
423         return this.injectionHelper.getAaiClient()
424                 .get(VpnBinding.class,
425                         AAIUriFactory.createResourceUri(AAIObjectType.VPN_BINDING, vpnBindingId).depth(Depth.ONE))
426                 .orElseGet(() -> {
427                     logger.debug("No VpnBinding matched by id");
428                     return null;
429                 });
430     }
431
432     public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
433         return this.injectionHelper.getAaiClient()
434                 .get(VolumeGroup.class, AAIUriFactory
435                         .createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId)
436                         .depth(Depth.ONE))
437                 .orElseGet(() -> {
438                     logger.debug("No Generic Vnf matched by id");
439                     return null;
440                 });
441     }
442
443     public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
444         return this.injectionHelper.getAaiClient()
445                 .get(VfModule.class,
446                         AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
447                 .orElseGet(() -> {
448                     logger.debug("No Generic Vnf matched by id");
449                     return null;
450                 });
451     }
452
453     public L3Network getAAIL3Network(String networkId) {
454
455         return this.injectionHelper.getAaiClient()
456                 .get(L3Network.class,
457                         AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
458                 .orElseGet(() -> {
459                     logger.debug("No Generic Vnf matched by id");
460                     return null;
461                 });
462
463     }
464
465     public Optional<ServiceInstance> getRelatedServiceInstanceFromInstanceGroup(String instanceGroupId)
466             throws Exception {
467         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId);
468         uri.relatedTo(AAIObjectPlurals.SERVICE_INSTANCE);
469         Optional<ServiceInstances> serviceInstances = injectionHelper.getAaiClient().get(ServiceInstances.class, uri);
470         ServiceInstance serviceInstance = null;
471         if (!serviceInstances.isPresent()) {
472             logger.debug("No ServiceInstances were found");
473             return Optional.empty();
474         } else {
475             if (serviceInstances.get().getServiceInstance().isEmpty()) {
476                 throw new NoServiceInstanceFoundException("No ServiceInstances Returned");
477             } else if (serviceInstances.get().getServiceInstance().size() > 1) {
478                 String message = String.format("Mulitple service instances were found for instance-group-id: %s.",
479                         instanceGroupId);
480                 throw new MultipleObjectsFoundException(message);
481             } else {
482                 serviceInstance = serviceInstances.get().getServiceInstance().get(0);
483             }
484             return Optional.of(serviceInstance);
485         }
486     }
487
488     public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName)
489             throws MultipleObjectsFoundException {
490         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
491         uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
492         Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
493         L3Network network = null;
494         if (!networks.isPresent()) {
495             logger.debug("No Networks matched by name");
496             return Optional.empty();
497         } else {
498             if (networks.get().getL3Network().size() > 1) {
499                 String message =
500                         String.format("Multiple networks found for service-instance-id: %s and network-name: %s.",
501                                 serviceInstanceId, networkName);
502                 throw new MultipleObjectsFoundException(message);
503             } else {
504                 network = networks.get().getL3Network().get(0);
505             }
506             return Optional.of(network);
507         }
508     }
509
510     public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName)
511             throws MultipleObjectsFoundException {
512         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
513         uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
514         Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
515         GenericVnf vnf = null;
516         if (!vnfs.isPresent()) {
517             logger.debug("No Vnfs matched by name");
518             return Optional.empty();
519         } else {
520             if (vnfs.get().getGenericVnf().size() > 1) {
521                 String message = String.format("Multiple vnfs found for service-instance-id: %s and vnf-name: %s.",
522                         serviceInstanceId, vnfName);
523                 throw new MultipleObjectsFoundException(message);
524             } else {
525                 vnf = vnfs.get().getGenericVnf().get(0);
526             }
527             return Optional.of(vnf);
528         }
529     }
530
531     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName)
532             throws MultipleObjectsFoundException {
533         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
534         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
535         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
536         VolumeGroup volumeGroup = null;
537         if (!volumeGroups.isPresent()) {
538             logger.debug("No VolumeGroups matched by name");
539             return Optional.empty();
540         } else {
541             if (volumeGroups.get().getVolumeGroup().size() > 1) {
542                 String message = String.format("Multiple volume-groups found for vnf-id: %s and volume-group-name: %s.",
543                         vnfId, volumeGroupName);
544                 throw new MultipleObjectsFoundException(message);
545             } else {
546                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
547             }
548             return Optional.of(volumeGroup);
549         }
550     }
551
552     public Optional<VolumeGroup> getRelatedVolumeGroupByIdFromVnf(String vnfId, String volumeGroupId) {
553         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
554         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-id", volumeGroupId);
555         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
556         VolumeGroup volumeGroup = null;
557         if (!volumeGroups.isPresent()) {
558             logger.debug("No VolumeGroups matched by id");
559             return Optional.empty();
560         } else {
561             volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
562             return Optional.of(volumeGroup);
563         }
564     }
565
566     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId,
567             String volumeGroupName) throws MultipleObjectsFoundException {
568         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
569         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
570         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
571         VolumeGroup volumeGroup = null;
572         if (!volumeGroups.isPresent()) {
573             logger.debug("No VolumeGroups matched by name");
574             return Optional.empty();
575         } else {
576             if (volumeGroups.get().getVolumeGroup().size() > 1) {
577                 String message = String.format(
578                         "Multiple voulme-groups found for vnf-id: %s, vf-module-id: %s and volume-group-name: %s.",
579                         vnfId, vfModuleId, volumeGroupName);
580                 throw new MultipleObjectsFoundException(message);
581             } else {
582                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
583             }
584             return Optional.of(volumeGroup);
585         }
586     }
587
588     public Optional<VolumeGroup> getRelatedVolumeGroupFromVfModule(String vnfId, String vfModuleId) throws Exception {
589         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
590         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP);
591         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
592         VolumeGroup volumeGroup = null;
593         if (!volumeGroups.isPresent()) {
594             logger.debug("VfModule does not have a volume group attached");
595             return Optional.empty();
596         } else {
597             if (volumeGroups.get().getVolumeGroup().size() > 1) {
598                 throw new Exception("Multiple VolumeGroups Returned");
599             } else {
600                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
601             }
602             return Optional.of(volumeGroup);
603         }
604     }
605
606     public Optional<org.onap.aai.domain.yang.VpnBinding> getAICVpnBindingFromNetwork(
607             org.onap.aai.domain.yang.L3Network aaiLocalNetwork) {
608         AAIResultWrapper networkWrapper = new AAIResultWrapper(aaiLocalNetwork);
609         if (networkWrapper.getRelationships().isPresent()
610                 && !networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).isEmpty()) {
611             return getAAIResourceDepthOne(
612                     networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).get(0))
613                             .asBean(org.onap.aai.domain.yang.VpnBinding.class);
614         }
615         return Optional.empty();
616     }
617
618     public ServiceInstances getAAIServiceInstancesGloballyByName(String serviceInstanceName) {
619
620         return injectionHelper.getAaiClient()
621                 .get(ServiceInstances.class, AAIUriFactory.createNodesUri(AAIObjectPlurals.SERVICE_INSTANCE)
622                         .queryParam("service-instance-name", serviceInstanceName))
623                 .orElseGet(() -> {
624                     logger.debug("No Service Instance matched by name");
625                     return null;
626                 });
627     }
628
629     public boolean existsAAINetworksGloballyByName(String networkName) {
630
631         AAIResourceUri l3networkUri =
632                 AAIUriFactory.createResourceUri(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
633         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
634         return aaiRC.exists(l3networkUri);
635     }
636
637     public boolean existsAAIVfModuleGloballyByName(String vfModuleName) {
638         AAIResourceUri vfModuleUri =
639                 AAIUriFactory.createNodesUri(AAIObjectPlurals.VF_MODULE).queryParam("vf-module-name", vfModuleName);
640         return injectionHelper.getAaiClient().exists(vfModuleUri);
641     }
642
643     public boolean existsAAIConfigurationGloballyByName(String configurationName) {
644         AAIResourceUri configUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.CONFIGURATION)
645                 .queryParam("configuration-name", configurationName);
646         return injectionHelper.getAaiClient().exists(configUri);
647     }
648
649     public boolean existsAAIVolumeGroupGloballyByName(String volumeGroupName) {
650         AAIResourceUri volumeGroupUri = AAIUriFactory.createNodesUri(AAIObjectPlurals.VOLUME_GROUP)
651                 .queryParam("volume-group-name", volumeGroupName);
652         return injectionHelper.getAaiClient().exists(volumeGroupUri);
653     }
654
655     public GenericVnfs getAAIVnfsGloballyByName(String vnfName) {
656
657         return injectionHelper.getAaiClient()
658                 .get(GenericVnfs.class,
659                         AAIUriFactory.createNodesUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName))
660                 .orElseGet(() -> {
661                     logger.debug("No GenericVnfs matched by name");
662                     return null;
663                 });
664     }
665
666     public Optional<Configuration> getRelatedConfigurationByNameFromServiceInstance(String serviceInstanceId,
667             String configurationName) throws MultipleObjectsFoundException {
668         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
669         uri.relatedTo(AAIObjectPlurals.CONFIGURATION).queryParam("configuration-name", configurationName);
670         Optional<Configurations> configurations = injectionHelper.getAaiClient().get(Configurations.class, uri);
671         Configuration configuration = null;
672         if (!configurations.isPresent()) {
673             logger.debug("No Configurations matched by name");
674             return Optional.empty();
675         } else {
676             if (configurations.get().getConfiguration().size() > 1) {
677                 String message = String.format(
678                         "Multiple configurations found for service-instance-d: %s and configuration-name: %s.",
679                         serviceInstanceId, configurationName);
680                 throw new MultipleObjectsFoundException(message);
681             } else {
682                 configuration = configurations.get().getConfiguration().get(0);
683             }
684             return Optional.of(configuration);
685         }
686     }
687 }