Merge "Added null check to server.getImage() to prevent crash at getId() call."
[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     protected 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 Exception {
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             throw new Exception("Multiple Service Instances Returned");
371         } else {
372             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
373         }
374         return Optional.of(aaiServiceInstance);
375     }
376
377     public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
378         return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
379     }
380
381     public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
382             String modelCustomizationUUID) {
383         return this.catalogDbClient
384                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
385     }
386
387     public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
388         AAIResourceUri clonedUri = aaiResourceUri.clone();
389         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
390     }
391
392     public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
393         AAIResourceUri clonedUri = aaiResourceUri.clone();
394         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
395     }
396
397     public Configuration getAAIConfiguration(String configurationId) {
398         return this.injectionHelper.getAaiClient()
399                 .get(Configuration.class,
400                         AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
401                 .orElseGet(() -> {
402                     logger.debug("No Configuration matched by id");
403                     return null;
404                 });
405     }
406
407     public GenericVnf getAAIGenericVnf(String vnfId) {
408
409         return this.injectionHelper.getAaiClient()
410                 .get(GenericVnf.class,
411                         AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
412                 .orElseGet(() -> {
413                     logger.debug("No Generic Vnf matched by id");
414                     return null;
415                 });
416     }
417
418     public VpnBinding getAAIVpnBinding(String vpnBindingId) {
419
420         return this.injectionHelper.getAaiClient()
421                 .get(VpnBinding.class,
422                         AAIUriFactory.createResourceUri(AAIObjectType.VPN_BINDING, vpnBindingId).depth(Depth.ONE))
423                 .orElseGet(() -> {
424                     logger.debug("No VpnBinding matched by id");
425                     return null;
426                 });
427     }
428
429     public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
430         return this.injectionHelper.getAaiClient()
431                 .get(VolumeGroup.class, AAIUriFactory
432                         .createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId)
433                         .depth(Depth.ONE))
434                 .orElseGet(() -> {
435                     logger.debug("No Generic Vnf matched by id");
436                     return null;
437                 });
438     }
439
440     public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
441         return this.injectionHelper.getAaiClient()
442                 .get(VfModule.class,
443                         AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
444                 .orElseGet(() -> {
445                     logger.debug("No Generic Vnf matched by id");
446                     return null;
447                 });
448     }
449
450     public L3Network getAAIL3Network(String networkId) {
451
452         return this.injectionHelper.getAaiClient()
453                 .get(L3Network.class,
454                         AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
455                 .orElseGet(() -> {
456                     logger.debug("No Generic Vnf matched by id");
457                     return null;
458                 });
459
460     }
461
462     public Optional<ServiceInstance> getRelatedServiceInstanceFromInstanceGroup(String instanceGroupId)
463             throws Exception {
464         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId);
465         uri.relatedTo(AAIObjectPlurals.SERVICE_INSTANCE);
466         Optional<ServiceInstances> serviceInstances = injectionHelper.getAaiClient().get(ServiceInstances.class, uri);
467         ServiceInstance serviceInstance = null;
468         if (!serviceInstances.isPresent()) {
469             logger.debug("No ServiceInstances were found");
470             return Optional.empty();
471         } else {
472             if (serviceInstances.get().getServiceInstance().isEmpty()) {
473                 throw new NoServiceInstanceFoundException("No ServiceInstances Returned");
474             } else if (serviceInstances.get().getServiceInstance().size() > 1) {
475                 throw new MultipleObjectsFoundException("Multiple ServiceInstances Returned");
476             } else {
477                 serviceInstance = serviceInstances.get().getServiceInstance().get(0);
478             }
479             return Optional.of(serviceInstance);
480         }
481     }
482
483     public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName)
484             throws Exception {
485         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
486         uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
487         Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
488         L3Network network = null;
489         if (!networks.isPresent()) {
490             logger.debug("No Networks matched by name");
491             return Optional.empty();
492         } else {
493             if (networks.get().getL3Network().size() > 1) {
494                 throw new Exception("Multiple Networks Returned");
495             } else {
496                 network = networks.get().getL3Network().get(0);
497             }
498             return Optional.of(network);
499         }
500     }
501
502     public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName)
503             throws Exception {
504         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
505         uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
506         Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
507         GenericVnf vnf = null;
508         if (!vnfs.isPresent()) {
509             logger.debug("No Vnfs matched by name");
510             return Optional.empty();
511         } else {
512             if (vnfs.get().getGenericVnf().size() > 1) {
513                 throw new Exception("Multiple Vnfs Returned");
514             } else {
515                 vnf = vnfs.get().getGenericVnf().get(0);
516             }
517             return Optional.of(vnf);
518         }
519     }
520
521     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName)
522             throws Exception {
523         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
524         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
525         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
526         VolumeGroup volumeGroup = null;
527         if (!volumeGroups.isPresent()) {
528             logger.debug("No VolumeGroups matched by name");
529             return Optional.empty();
530         } else {
531             if (volumeGroups.get().getVolumeGroup().size() > 1) {
532                 throw new Exception("Multiple VolumeGroups Returned");
533             } else {
534                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
535             }
536             return Optional.of(volumeGroup);
537         }
538     }
539
540     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId,
541             String volumeGroupName) throws Exception {
542         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
543         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
544         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
545         VolumeGroup volumeGroup = null;
546         if (!volumeGroups.isPresent()) {
547             logger.debug("No VolumeGroups matched by name");
548             return Optional.empty();
549         } else {
550             if (volumeGroups.get().getVolumeGroup().size() > 1) {
551                 throw new Exception("Multiple VolumeGroups Returned");
552             } else {
553                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
554             }
555             return Optional.of(volumeGroup);
556         }
557     }
558
559     public Optional<org.onap.aai.domain.yang.VpnBinding> getAICVpnBindingFromNetwork(
560             org.onap.aai.domain.yang.L3Network aaiLocalNetwork) {
561         AAIResultWrapper networkWrapper = new AAIResultWrapper(aaiLocalNetwork);
562         if (networkWrapper.getRelationships().isPresent()
563                 && !networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).isEmpty()) {
564             return getAAIResourceDepthOne(
565                     networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).get(0))
566                             .asBean(org.onap.aai.domain.yang.VpnBinding.class);
567         }
568         return Optional.empty();
569     }
570
571     public ServiceInstances getAAIServiceInstancesGloballyByName(String serviceInstanceName) {
572
573         return injectionHelper.getAaiClient()
574                 .get(ServiceInstances.class, AAIUriFactory.createNodesUri(AAIObjectPlurals.SERVICE_INSTANCE)
575                         .queryParam("service-instance-name", serviceInstanceName))
576                 .orElseGet(() -> {
577                     logger.debug("No Service Instance matched by name");
578                     return null;
579                 });
580     }
581
582     public boolean existsAAINetworksGloballyByName(String networkName) {
583
584         AAIResourceUri l3networkUri =
585                 AAIUriFactory.createResourceUri(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
586         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
587         return aaiRC.exists(l3networkUri);
588     }
589
590     public GenericVnfs getAAIVnfsGloballyByName(String vnfName) {
591
592         return injectionHelper.getAaiClient()
593                 .get(GenericVnfs.class,
594                         AAIUriFactory.createNodesUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName))
595                 .orElseGet(() -> {
596                     logger.debug("No GenericVnfs matched by name");
597                     return null;
598                 });
599     }
600
601     public Optional<Configuration> getRelatedConfigurationByNameFromServiceInstance(String serviceInstanceId,
602             String configurationName) throws Exception {
603         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
604         uri.relatedTo(AAIObjectPlurals.CONFIGURATION).queryParam("configuration-name", configurationName);
605         Optional<Configurations> configurations = injectionHelper.getAaiClient().get(Configurations.class, uri);
606         Configuration configuration = null;
607         if (!configurations.isPresent()) {
608             logger.debug("No Configurations matched by name");
609             return Optional.empty();
610         } else {
611             if (configurations.get().getConfiguration().size() > 1) {
612                 throw new Exception("Multiple Configurations Returned");
613             } else {
614                 configuration = configurations.get().getConfiguration().get(0);
615             }
616             return Optional.of(configuration);
617         }
618     }
619 }