Merge "Vulnerability fixes in ServicePluginFactory"
[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.io.ObjectOutputStream;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import org.onap.aai.domain.yang.CloudRegion;
33 import org.onap.aai.domain.yang.Configuration;
34 import org.onap.aai.domain.yang.Configurations;
35 import org.onap.aai.domain.yang.GenericVnf;
36 import org.onap.aai.domain.yang.GenericVnfs;
37 import org.onap.aai.domain.yang.InstanceGroup;
38 import org.onap.aai.domain.yang.L3Network;
39 import org.onap.aai.domain.yang.L3Networks;
40 import org.onap.aai.domain.yang.ServiceInstance;
41 import org.onap.aai.domain.yang.ServiceInstances;
42 import org.onap.aai.domain.yang.ServiceSubscription;
43 import org.onap.aai.domain.yang.VfModule;
44 import org.onap.aai.domain.yang.VolumeGroup;
45 import org.onap.aai.domain.yang.VolumeGroups;
46 import org.onap.aai.domain.yang.VpnBinding;
47 import org.onap.so.bpmn.common.InjectionHelper;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
49 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
50 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.MultipleObjectsFoundException;
51 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.NoServiceInstanceFoundException;
52 import org.onap.so.client.aai.AAIObjectPlurals;
53 import org.onap.so.client.aai.AAIObjectType;
54 import org.onap.so.client.aai.AAIResourcesClient;
55 import org.onap.so.client.aai.entities.AAIResultWrapper;
56 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
57 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
58 import org.onap.so.client.graphinventory.entities.uri.Depth;
59 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
60 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
61 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
62 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
63 import org.onap.so.db.catalog.beans.Service;
64 import org.onap.so.db.catalog.beans.VfModuleCustomization;
65 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
66 import org.onap.so.db.catalog.client.CatalogDbClient;
67 import org.onap.so.db.request.beans.InfraActiveRequests;
68 import org.onap.so.db.request.beans.RequestProcessingData;
69 import org.onap.so.db.request.client.RequestsDbClient;
70 import org.onap.so.serviceinstancebeans.CloudConfiguration;
71 import org.onap.so.serviceinstancebeans.ModelType;
72 import org.onap.so.serviceinstancebeans.RelatedInstance;
73 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
74 import org.onap.so.serviceinstancebeans.RequestDetails;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77 import org.springframework.beans.factory.annotation.Autowired;
78 import org.springframework.stereotype.Component;
79 import com.fasterxml.jackson.core.JsonParseException;
80 import com.fasterxml.jackson.core.JsonProcessingException;
81 import com.fasterxml.jackson.databind.DeserializationFeature;
82 import com.fasterxml.jackson.databind.JsonMappingException;
83 import com.fasterxml.jackson.databind.ObjectMapper;
84 import com.fasterxml.jackson.databind.SerializationFeature;
85
86 @Component("BBInputSetupUtils")
87 public class BBInputSetupUtils {
88
89     private static final Logger logger = LoggerFactory.getLogger(BBInputSetupUtils.class);
90     private ObjectMapper objectMapper = new ObjectMapper();
91     private static final String REQUEST_ERROR = "Could not find request.";
92     private static final String DATA_LOAD_ERROR = "Could not process loading data from database";
93     private static final String DATA_PARSE_ERROR = "Could not parse data";
94     private static final String PROCESSING_DATA_NAME_EXECUTION_FLOWS = "flowExecutionPath";
95
96     @Autowired
97     protected CatalogDbClient catalogDbClient;
98
99     @Autowired
100     protected RequestsDbClient requestsDbClient;
101
102     @Autowired
103     protected InjectionHelper injectionHelper;
104
105     public RelatedInstance getRelatedInstanceByType(RequestDetails requestDetails, ModelType modelType) {
106         if (requestDetails.getRelatedInstanceList() != null) {
107             for (RelatedInstanceList relatedInstanceList : requestDetails.getRelatedInstanceList()) {
108                 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
109                 if (relatedInstance != null && relatedInstance.getModelInfo() != null
110                         && relatedInstance.getModelInfo().getModelType() != null
111                         && relatedInstance.getModelInfo().getModelType().equals(modelType)) {
112                     return relatedInstance;
113                 }
114             }
115         }
116         return null;
117     }
118
119     public void updateInfraActiveRequestVnfId(InfraActiveRequests request, String vnfId) {
120         if (request != null) {
121             request.setVnfId(vnfId);
122             this.requestsDbClient.updateInfraActiveRequests(request);
123         } else {
124             logger.debug(REQUEST_ERROR);
125         }
126     }
127
128     public void updateInfraActiveRequestVfModuleId(InfraActiveRequests request, String vfModuleId) {
129         if (request != null) {
130             request.setVfModuleId(vfModuleId);
131             this.requestsDbClient.updateInfraActiveRequests(request);
132         } else {
133             logger.debug(REQUEST_ERROR);
134         }
135     }
136
137     public void updateInfraActiveRequestVolumeGroupId(InfraActiveRequests request, String volumeGroupId) {
138         if (request != null) {
139             request.setVolumeGroupId(volumeGroupId);
140             this.requestsDbClient.updateInfraActiveRequests(request);
141         } else {
142             logger.debug(REQUEST_ERROR);
143         }
144     }
145
146     public void updateInfraActiveRequestNetworkId(InfraActiveRequests request, String networkId) {
147         if (request != null) {
148             request.setNetworkId(networkId);
149             this.requestsDbClient.updateInfraActiveRequests(request);
150         } else {
151             logger.debug(REQUEST_ERROR);
152         }
153     }
154
155     public void persistFlowExecutionPath(String requestId, List<ExecuteBuildingBlock> flowsToExecute) {
156
157         if (requestId != null) {
158             List<String> flows = new ArrayList<>();
159             ObjectMapper om = new ObjectMapper();
160             try {
161                 for (ExecuteBuildingBlock ebb : flowsToExecute) {
162                     flows.add(om.writeValueAsString(ebb));
163                 }
164             } catch (JsonProcessingException e) {
165                 logger.error(DATA_PARSE_ERROR, e);
166             }
167
168             this.requestsDbClient.persistProcessingData(flows.toString(), requestId);
169         } else {
170             logger.debug(REQUEST_ERROR);
171         }
172     }
173
174     public InfraActiveRequests loadInfraActiveRequestById(String requestId) {
175
176         return this.requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
177     }
178
179     public InfraActiveRequests loadOriginalInfraActiveRequestById(String requestId) {
180
181         return this.requestsDbClient.getInfraActiveRequestbyRequestId(
182                 this.requestsDbClient.getInfraActiveRequestbyRequestId(requestId).getOriginalRequestId());
183     }
184
185     public List<ExecuteBuildingBlock> loadOriginalFlowExecutionPath(String requestId) {
186
187         List<ExecuteBuildingBlock> asList = null;
188         if (requestId != null) {
189
190             InfraActiveRequests request = loadInfraActiveRequestById(requestId);
191
192             if (request.getOriginalRequestId() != null) {
193
194                 RequestProcessingData requestProcessingData =
195                         this.requestsDbClient.getRequestProcessingDataBySoRequestIdAndName(
196                                 request.getOriginalRequestId(), PROCESSING_DATA_NAME_EXECUTION_FLOWS);
197
198                 ObjectMapper om = new ObjectMapper();
199                 try {
200                     ExecuteBuildingBlock[] asArray =
201                             om.readValue(requestProcessingData.getValue(), ExecuteBuildingBlock[].class);
202                     asList = Arrays.asList(asArray);
203                 } catch (Exception e) {
204                     logger.error(DATA_LOAD_ERROR, e);
205                 }
206             }
207
208         } else {
209             logger.debug(REQUEST_ERROR);
210         }
211
212         return asList;
213     }
214
215     public Service getCatalogServiceByModelUUID(String modelUUID) {
216         return catalogDbClient.getServiceByID(modelUUID);
217     }
218
219     public Service getCatalogServiceByModelVersionAndModelInvariantUUID(String modelVersion,
220             String modelInvariantUUID) {
221         return catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(modelVersion, modelInvariantUUID);
222     }
223
224     public CollectionNetworkResourceCustomization getCatalogCollectionNetworkResourceCustByID(String key) {
225         return catalogDbClient.getCollectionNetworkResourceCustomizationByID(key);
226     }
227
228     public NetworkCollectionResourceCustomization getCatalogNetworkCollectionResourceCustByID(
229             String collectionCustomizationId) {
230         return catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCustomizationId);
231     }
232
233     public VfModuleCustomization getVfModuleCustomizationByModelCuztomizationUUID(String modelCustomizationUUID) {
234         return catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(modelCustomizationUUID);
235     }
236
237     public CvnfcConfigurationCustomization getCvnfcConfigurationCustomization(String serviceModelUUID,
238             String vnfCustomizationUuid, String vfModuleCustomizationUuid, String cvnfcCustomizationUuid) {
239         return catalogDbClient.getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid, vfModuleCustomizationUuid,
240                 cvnfcCustomizationUuid);
241     }
242
243     public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
244         return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
245     }
246
247     public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
248         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
249         return uri.getURIKeys();
250     }
251
252     protected RequestDetails getRequestDetails(String requestId) throws IOException {
253         if (requestId != null && !requestId.isEmpty()) {
254             InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
255             String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
256             objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
257             objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
258             return objectMapper.readValue(requestBody, RequestDetails.class);
259         } else {
260             return null;
261         }
262     }
263
264     protected InfraActiveRequests getInfraActiveRequest(String requestId) {
265         if (requestId != null && !requestId.isEmpty()) {
266             return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
267         } else {
268             return null;
269         }
270     }
271
272     protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration) {
273         if (cloudConfiguration != null) {
274             String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
275             String cloudOwner = cloudConfiguration.getCloudOwner();
276             if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
277                 return injectionHelper.getAaiClient().get(CloudRegion.class, AAIUriFactory
278                         .createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner, cloudRegionId).depth(Depth.TWO))
279                         .orElse(null);
280
281             } else {
282                 return null;
283             }
284         } else {
285             return null;
286         }
287     }
288
289     protected InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
290         return injectionHelper.getAaiClient().get(InstanceGroup.class,
291                 AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId)).orElse(null);
292     }
293
294     public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
295         return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
296                 AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId)).orElse(null);
297     }
298
299     public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
300
301         if (globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null
302                 || subscriptionServiceType.equals("")) {
303             return null;
304         } else {
305             return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory
306                     .createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
307                     .orElse(null);
308         }
309
310     }
311
312     public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
313         return injectionHelper.getAaiClient()
314                 .get(ServiceInstance.class, AAIUriFactory
315                         .createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
316                 .orElse(null);
317     }
318
319     protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
320             String serviceInstanceId) {
321         return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory
322                 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId)
323                 .depth(Depth.TWO)).orElse(null);
324     }
325
326     protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
327             Customer customer) {
328
329         return injectionHelper.getAaiClient()
330                 .get(org.onap.aai.domain.yang.ServiceInstances.class,
331                         AAIUriFactory
332                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
333                                         customer.getServiceSubscription().getServiceType())
334                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
335                 .orElseGet(() -> {
336                     logger.debug("No Service Instance matched by name");
337                     return null;
338                 });
339     }
340
341     public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
342             Customer customer) throws Exception {
343         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
344         org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
345         aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);
346
347         if (aaiServiceInstances == null) {
348             return null;
349         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
350             throw new Exception("Multiple Service Instances Returned");
351         } else {
352             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
353         }
354         return aaiServiceInstance;
355     }
356
357     protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
358             String serviceInstanceName) {
359
360         return injectionHelper.getAaiClient()
361                 .get(ServiceInstances.class,
362                         AAIUriFactory
363                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
364                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
365                 .orElseGet(() -> {
366                     logger.debug("No Service Instance matched by name");
367                     return null;
368                 });
369     }
370
371     public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
372             String serviceInstanceName) throws Exception {
373         ServiceInstance aaiServiceInstance = null;
374         ServiceInstances aaiServiceInstances = null;
375         aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
376
377         if (aaiServiceInstances == null) {
378             return Optional.empty();
379         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
380             throw new Exception("Multiple Service Instances Returned");
381         } else {
382             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
383         }
384         return Optional.of(aaiServiceInstance);
385     }
386
387     public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
388         return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
389     }
390
391     public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
392             String modelCustomizationUUID) {
393         return this.catalogDbClient
394                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
395     }
396
397     public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
398         AAIResourceUri clonedUri = aaiResourceUri.clone();
399         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
400     }
401
402     public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
403         AAIResourceUri clonedUri = aaiResourceUri.clone();
404         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
405     }
406
407     public Configuration getAAIConfiguration(String configurationId) {
408         return this.injectionHelper.getAaiClient()
409                 .get(Configuration.class,
410                         AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
411                 .orElseGet(() -> {
412                     logger.debug("No Configuration matched by id");
413                     return null;
414                 });
415     }
416
417     public GenericVnf getAAIGenericVnf(String vnfId) {
418
419         return this.injectionHelper.getAaiClient()
420                 .get(GenericVnf.class,
421                         AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
422                 .orElseGet(() -> {
423                     logger.debug("No Generic Vnf matched by id");
424                     return null;
425                 });
426     }
427
428     public VpnBinding getAAIVpnBinding(String vpnBindingId) {
429
430         return this.injectionHelper.getAaiClient()
431                 .get(VpnBinding.class,
432                         AAIUriFactory.createResourceUri(AAIObjectType.VPN_BINDING, vpnBindingId).depth(Depth.ONE))
433                 .orElseGet(() -> {
434                     logger.debug("No VpnBinding matched by id");
435                     return null;
436                 });
437     }
438
439     public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
440         return this.injectionHelper.getAaiClient()
441                 .get(VolumeGroup.class, AAIUriFactory
442                         .createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId)
443                         .depth(Depth.ONE))
444                 .orElseGet(() -> {
445                     logger.debug("No Generic Vnf matched by id");
446                     return null;
447                 });
448     }
449
450     public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
451         return this.injectionHelper.getAaiClient()
452                 .get(VfModule.class,
453                         AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
454                 .orElseGet(() -> {
455                     logger.debug("No Generic Vnf matched by id");
456                     return null;
457                 });
458     }
459
460     public L3Network getAAIL3Network(String networkId) {
461
462         return this.injectionHelper.getAaiClient()
463                 .get(L3Network.class,
464                         AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
465                 .orElseGet(() -> {
466                     logger.debug("No Generic Vnf matched by id");
467                     return null;
468                 });
469
470     }
471
472     public Optional<ServiceInstance> getRelatedServiceInstanceFromInstanceGroup(String instanceGroupId)
473             throws Exception {
474         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId);
475         uri.relatedTo(AAIObjectPlurals.SERVICE_INSTANCE);
476         Optional<ServiceInstances> serviceInstances = injectionHelper.getAaiClient().get(ServiceInstances.class, uri);
477         ServiceInstance serviceInstance = null;
478         if (!serviceInstances.isPresent()) {
479             logger.debug("No ServiceInstances were found");
480             return Optional.empty();
481         } else {
482             if (serviceInstances.get().getServiceInstance().isEmpty()) {
483                 throw new NoServiceInstanceFoundException("No ServiceInstances Returned");
484             } else if (serviceInstances.get().getServiceInstance().size() > 1) {
485                 throw new MultipleObjectsFoundException("Multiple ServiceInstances Returned");
486             } else {
487                 serviceInstance = serviceInstances.get().getServiceInstance().get(0);
488             }
489             return Optional.of(serviceInstance);
490         }
491     }
492
493     public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName)
494             throws Exception {
495         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
496         uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
497         Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
498         L3Network network = null;
499         if (!networks.isPresent()) {
500             logger.debug("No Networks matched by name");
501             return Optional.empty();
502         } else {
503             if (networks.get().getL3Network().size() > 1) {
504                 throw new Exception("Multiple Networks Returned");
505             } else {
506                 network = networks.get().getL3Network().get(0);
507             }
508             return Optional.of(network);
509         }
510     }
511
512     public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName)
513             throws Exception {
514         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
515         uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
516         Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
517         GenericVnf vnf = null;
518         if (!vnfs.isPresent()) {
519             logger.debug("No Vnfs matched by name");
520             return Optional.empty();
521         } else {
522             if (vnfs.get().getGenericVnf().size() > 1) {
523                 throw new Exception("Multiple Vnfs Returned");
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 Exception {
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                 throw new Exception("Multiple VolumeGroups Returned");
543             } else {
544                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
545             }
546             return Optional.of(volumeGroup);
547         }
548     }
549
550     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId,
551             String volumeGroupName) throws Exception {
552         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
553         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
554         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
555         VolumeGroup volumeGroup = null;
556         if (!volumeGroups.isPresent()) {
557             logger.debug("No VolumeGroups matched by name");
558             return Optional.empty();
559         } else {
560             if (volumeGroups.get().getVolumeGroup().size() > 1) {
561                 throw new Exception("Multiple VolumeGroups Returned");
562             } else {
563                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
564             }
565             return Optional.of(volumeGroup);
566         }
567     }
568
569     public Optional<org.onap.aai.domain.yang.VpnBinding> getAICVpnBindingFromNetwork(
570             org.onap.aai.domain.yang.L3Network aaiLocalNetwork) {
571         AAIResultWrapper networkWrapper = new AAIResultWrapper(aaiLocalNetwork);
572         if (networkWrapper.getRelationships().isPresent()
573                 && !networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).isEmpty()) {
574             return getAAIResourceDepthOne(
575                     networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).get(0))
576                             .asBean(org.onap.aai.domain.yang.VpnBinding.class);
577         }
578         return Optional.empty();
579     }
580
581     public ServiceInstances getAAIServiceInstancesGloballyByName(String serviceInstanceName) {
582
583         return injectionHelper.getAaiClient()
584                 .get(ServiceInstances.class, AAIUriFactory.createNodesUri(AAIObjectPlurals.SERVICE_INSTANCE)
585                         .queryParam("service-instance-name", serviceInstanceName))
586                 .orElseGet(() -> {
587                     logger.debug("No Service Instance matched by name");
588                     return null;
589                 });
590     }
591
592     public boolean existsAAINetworksGloballyByName(String networkName) {
593
594         AAIResourceUri l3networkUri =
595                 AAIUriFactory.createResourceUri(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
596         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
597         return aaiRC.exists(l3networkUri);
598     }
599
600     public GenericVnfs getAAIVnfsGloballyByName(String vnfName) {
601
602         return injectionHelper.getAaiClient()
603                 .get(GenericVnfs.class,
604                         AAIUriFactory.createNodesUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName))
605                 .orElseGet(() -> {
606                     logger.debug("No GenericVnfs matched by name");
607                     return null;
608                 });
609     }
610
611     public Optional<Configuration> getRelatedConfigurationByNameFromServiceInstance(String serviceInstanceId,
612             String configurationName) throws Exception {
613         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
614         uri.relatedTo(AAIObjectPlurals.CONFIGURATION).queryParam("configuration-name", configurationName);
615         Optional<Configurations> configurations = injectionHelper.getAaiClient().get(Configurations.class, uri);
616         Configuration configuration = null;
617         if (!configurations.isPresent()) {
618             logger.debug("No Configurations matched by name");
619             return Optional.empty();
620         } else {
621             if (configurations.get().getConfiguration().size() > 1) {
622                 throw new Exception("Multiple Configurations Returned");
623             } else {
624                 configuration = configurations.get().getConfiguration().get(0);
625             }
626             return Optional.of(configuration);
627         }
628     }
629 }