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