Merge "Add API for commit and abort OrchestrationTask"
[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.Arrays;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Optional;
31 import org.onap.aai.domain.yang.CloudRegion;
32 import org.onap.aai.domain.yang.Configuration;
33 import org.onap.aai.domain.yang.Configurations;
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.AAIResourcesClient;
54 import org.onap.so.client.aai.entities.AAIResultWrapper;
55 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
56 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
57 import org.onap.so.client.graphinventory.entities.uri.Depth;
58 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
59 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
60 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
61 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
62 import org.onap.so.db.catalog.beans.Service;
63 import org.onap.so.db.catalog.beans.VfModuleCustomization;
64 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
65 import org.onap.so.db.catalog.client.CatalogDbClient;
66 import org.onap.so.db.request.beans.InfraActiveRequests;
67 import org.onap.so.db.request.beans.RequestProcessingData;
68 import org.onap.so.db.request.client.RequestsDbClient;
69 import org.onap.so.serviceinstancebeans.CloudConfiguration;
70 import org.onap.so.serviceinstancebeans.ModelType;
71 import org.onap.so.serviceinstancebeans.RelatedInstance;
72 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
73 import org.onap.so.serviceinstancebeans.RequestDetails;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76 import org.springframework.beans.factory.annotation.Autowired;
77 import org.springframework.stereotype.Component;
78 import com.fasterxml.jackson.core.JsonProcessingException;
79 import com.fasterxml.jackson.databind.DeserializationFeature;
80 import com.fasterxml.jackson.databind.ObjectMapper;
81 import com.fasterxml.jackson.databind.SerializationFeature;
82 import com.fasterxml.jackson.databind.type.TypeFactory;
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         return this.requestsDbClient.getInfraActiveRequestbyRequestId(
179                 this.requestsDbClient.getInfraActiveRequestbyRequestId(requestId).getOriginalRequestId());
180     }
181
182     public List<ExecuteBuildingBlock> loadOriginalFlowExecutionPath(String requestId) {
183         if (requestId != null) {
184             InfraActiveRequests request = loadInfraActiveRequestById(requestId);
185             if (request.getOriginalRequestId() != null) {
186                 RequestProcessingData requestProcessingData =
187                         this.requestsDbClient.getRequestProcessingDataBySoRequestIdAndName(
188                                 request.getOriginalRequestId(), PROCESSING_DATA_NAME_EXECUTION_FLOWS);
189                 try {
190                     ObjectMapper om = new ObjectMapper();
191                     TypeFactory typeFactory = objectMapper.getTypeFactory();
192                     return om.readValue(requestProcessingData.getValue(),
193                             typeFactory.constructCollectionType(List.class, ExecuteBuildingBlock.class));
194                 } catch (Exception e) {
195                     logger.error(DATA_LOAD_ERROR, e);
196                     throw new RuntimeException("Error Loading Original Request Data", e);
197                 }
198             } else {
199                 throw new RuntimeException("Original Request Id is null for record: " + requestId);
200             }
201         } else {
202             throw new RuntimeException("Null Request Id Passed in");
203         }
204     }
205
206     public Service getCatalogServiceByModelUUID(String modelUUID) {
207         return catalogDbClient.getServiceByID(modelUUID);
208     }
209
210     public Service getCatalogServiceByModelVersionAndModelInvariantUUID(String modelVersion,
211             String modelInvariantUUID) {
212         return catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(modelVersion, modelInvariantUUID);
213     }
214
215     public CollectionNetworkResourceCustomization getCatalogCollectionNetworkResourceCustByID(String key) {
216         return catalogDbClient.getCollectionNetworkResourceCustomizationByID(key);
217     }
218
219     public NetworkCollectionResourceCustomization getCatalogNetworkCollectionResourceCustByID(
220             String collectionCustomizationId) {
221         return catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCustomizationId);
222     }
223
224     public VfModuleCustomization getVfModuleCustomizationByModelCuztomizationUUID(String modelCustomizationUUID) {
225         return catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(modelCustomizationUUID);
226     }
227
228     public CvnfcConfigurationCustomization getCvnfcConfigurationCustomization(String serviceModelUUID,
229             String vnfCustomizationUuid, String vfModuleCustomizationUuid, String cvnfcCustomizationUuid) {
230         return catalogDbClient.getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid, vfModuleCustomizationUuid,
231                 cvnfcCustomizationUuid);
232     }
233
234     public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
235         return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
236     }
237
238     public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
239         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
240         return uri.getURIKeys();
241     }
242
243     protected RequestDetails getRequestDetails(String requestId) throws IOException {
244         if (requestId != null && !requestId.isEmpty()) {
245             InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
246             String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
247             objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
248             objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
249             return objectMapper.readValue(requestBody, RequestDetails.class);
250         } else {
251             return null;
252         }
253     }
254
255     protected InfraActiveRequests getInfraActiveRequest(String requestId) {
256         if (requestId != null && !requestId.isEmpty()) {
257             return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
258         } else {
259             return null;
260         }
261     }
262
263     protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration) {
264         if (cloudConfiguration != null) {
265             String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
266             String cloudOwner = cloudConfiguration.getCloudOwner();
267             if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
268                 return injectionHelper.getAaiClient().get(CloudRegion.class, AAIUriFactory
269                         .createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner, cloudRegionId).depth(Depth.TWO))
270                         .orElse(null);
271
272             } else {
273                 return null;
274             }
275         } else {
276             return null;
277         }
278     }
279
280     public InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
281         return injectionHelper.getAaiClient().get(InstanceGroup.class,
282                 AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId)).orElse(null);
283     }
284
285     public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
286         return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
287                 AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId)).orElse(null);
288     }
289
290     public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
291
292         if (globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null
293                 || subscriptionServiceType.equals("")) {
294             return null;
295         } else {
296             return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory
297                     .createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
298                     .orElse(null);
299         }
300
301     }
302
303     public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
304         return injectionHelper.getAaiClient()
305                 .get(ServiceInstance.class, AAIUriFactory
306                         .createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
307                 .orElse(null);
308     }
309
310     protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
311             String serviceInstanceId) {
312         return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory
313                 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId)
314                 .depth(Depth.TWO)).orElse(null);
315     }
316
317     protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
318             Customer customer) {
319
320         return injectionHelper.getAaiClient()
321                 .get(org.onap.aai.domain.yang.ServiceInstances.class,
322                         AAIUriFactory
323                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
324                                         customer.getServiceSubscription().getServiceType())
325                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
326                 .orElseGet(() -> {
327                     logger.debug("No Service Instance matched by name");
328                     return null;
329                 });
330     }
331
332     public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
333             Customer customer) throws Exception {
334         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
335         org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
336         aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);
337
338         if (aaiServiceInstances == null) {
339             return null;
340         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
341             throw new Exception("Multiple Service Instances Returned");
342         } else {
343             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
344         }
345         return aaiServiceInstance;
346     }
347
348     protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
349             String serviceInstanceName) {
350
351         return injectionHelper.getAaiClient()
352                 .get(ServiceInstances.class,
353                         AAIUriFactory
354                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
355                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
356                 .orElseGet(() -> {
357                     logger.debug("No Service Instance matched by name");
358                     return null;
359                 });
360     }
361
362     public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
363             String serviceInstanceName) throws MultipleObjectsFoundException {
364         ServiceInstance aaiServiceInstance = null;
365         ServiceInstances aaiServiceInstances = null;
366         aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
367
368         if (aaiServiceInstances == null) {
369             return Optional.empty();
370         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
371             String message = String.format(
372                     "Multiple service instances found for customer-id: %s, service-type: %s and service-instance-name: %s.",
373                     globalCustomerId, serviceType, serviceInstanceName);
374             throw new MultipleObjectsFoundException(message);
375         } else {
376             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
377         }
378         return Optional.of(aaiServiceInstance);
379     }
380
381     public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
382         return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
383     }
384
385     public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
386             String modelCustomizationUUID) {
387         return this.catalogDbClient
388                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
389     }
390
391     public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
392         AAIResourceUri clonedUri = aaiResourceUri.clone();
393         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
394     }
395
396     public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
397         AAIResourceUri clonedUri = aaiResourceUri.clone();
398         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
399     }
400
401     public Configuration getAAIConfiguration(String configurationId) {
402         return this.getConcreteAAIResource(Configuration.class, AAIObjectType.CONFIGURATION, configurationId);
403     }
404
405     public GenericVnf getAAIGenericVnf(String vnfId) {
406         return getConcreteAAIResource(GenericVnf.class, AAIObjectType.GENERIC_VNF, vnfId);
407     }
408
409     public VpnBinding getAAIVpnBinding(String vpnBindingId) {
410         return getConcreteAAIResource(VpnBinding.class, AAIObjectType.VPN_BINDING, vpnBindingId);
411     }
412
413     public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
414         return getConcreteAAIResource(VolumeGroup.class, AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId,
415                 volumeGroupId);
416     }
417
418     public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
419         return getConcreteAAIResource(VfModule.class, AAIObjectType.VF_MODULE, vnfId, vfModuleId);
420     }
421
422     public L3Network getAAIL3Network(String networkId) {
423         return getConcreteAAIResource(L3Network.class, AAIObjectType.L3_NETWORK, networkId);
424     }
425
426     private <T> T getConcreteAAIResource(Class<T> clazz, AAIObjectType objectType, Object... ids) {
427         return injectionHelper.getAaiClient()
428                 .get(clazz, AAIUriFactory.createResourceUri(objectType, ids).depth(Depth.ONE)).orElseGet(() -> {
429                     logger.debug("No resource of type: {} matched by ids: {}", objectType.typeName(),
430                             Arrays.toString(ids));
431                     return null;
432                 });
433     }
434
435     public Optional<ServiceInstance> getRelatedServiceInstanceFromInstanceGroup(String instanceGroupId)
436             throws Exception {
437         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId);
438         uri.relatedTo(AAIObjectPlurals.SERVICE_INSTANCE);
439         Optional<ServiceInstances> serviceInstances = injectionHelper.getAaiClient().get(ServiceInstances.class, uri);
440         ServiceInstance serviceInstance = null;
441         if (!serviceInstances.isPresent()) {
442             logger.debug("No ServiceInstances were found");
443             return Optional.empty();
444         } else {
445             if (serviceInstances.get().getServiceInstance().isEmpty()) {
446                 throw new NoServiceInstanceFoundException("No ServiceInstances Returned");
447             } else if (serviceInstances.get().getServiceInstance().size() > 1) {
448                 String message = String.format("Mulitple service instances were found for instance-group-id: %s.",
449                         instanceGroupId);
450                 throw new MultipleObjectsFoundException(message);
451             } else {
452                 serviceInstance = serviceInstances.get().getServiceInstance().get(0);
453             }
454             return Optional.of(serviceInstance);
455         }
456     }
457
458     public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName)
459             throws MultipleObjectsFoundException {
460         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
461         uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
462         Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
463         L3Network network = null;
464         if (!networks.isPresent()) {
465             logger.debug("No Networks matched by name");
466             return Optional.empty();
467         } else {
468             if (networks.get().getL3Network().size() > 1) {
469                 String message =
470                         String.format("Multiple networks found for service-instance-id: %s and network-name: %s.",
471                                 serviceInstanceId, networkName);
472                 throw new MultipleObjectsFoundException(message);
473             } else {
474                 network = networks.get().getL3Network().get(0);
475             }
476             return Optional.of(network);
477         }
478     }
479
480     public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName)
481             throws MultipleObjectsFoundException {
482         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
483         uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
484         Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
485         GenericVnf vnf = null;
486         if (!vnfs.isPresent()) {
487             logger.debug("No Vnfs matched by name");
488             return Optional.empty();
489         } else {
490             if (vnfs.get().getGenericVnf().size() > 1) {
491                 String message = String.format("Multiple vnfs found for service-instance-id: %s and vnf-name: %s.",
492                         serviceInstanceId, vnfName);
493                 throw new MultipleObjectsFoundException(message);
494             } else {
495                 vnf = vnfs.get().getGenericVnf().get(0);
496             }
497             return Optional.of(vnf);
498         }
499     }
500
501     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName)
502             throws MultipleObjectsFoundException {
503         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
504         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
505         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
506         VolumeGroup volumeGroup = null;
507         if (!volumeGroups.isPresent()) {
508             logger.debug("No VolumeGroups matched by name");
509             return Optional.empty();
510         } else {
511             if (volumeGroups.get().getVolumeGroup().size() > 1) {
512                 String message = String.format("Multiple volume-groups found for vnf-id: %s and volume-group-name: %s.",
513                         vnfId, volumeGroupName);
514                 throw new MultipleObjectsFoundException(message);
515             } else {
516                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
517             }
518             return Optional.of(volumeGroup);
519         }
520     }
521
522     public Optional<VolumeGroup> getRelatedVolumeGroupByIdFromVnf(String vnfId, String volumeGroupId) {
523         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
524         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-id", volumeGroupId);
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 id");
529             return Optional.empty();
530         } else {
531             volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
532             return Optional.of(volumeGroup);
533         }
534     }
535
536     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId,
537             String volumeGroupName) throws MultipleObjectsFoundException {
538         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
539         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
540         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
541         VolumeGroup volumeGroup = null;
542         if (!volumeGroups.isPresent()) {
543             logger.debug("No VolumeGroups matched by name");
544             return Optional.empty();
545         } else {
546             if (volumeGroups.get().getVolumeGroup().size() > 1) {
547                 String message = String.format(
548                         "Multiple voulme-groups found for vnf-id: %s, vf-module-id: %s and volume-group-name: %s.",
549                         vnfId, vfModuleId, volumeGroupName);
550                 throw new MultipleObjectsFoundException(message);
551             } else {
552                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
553             }
554             return Optional.of(volumeGroup);
555         }
556     }
557
558     public Optional<VolumeGroup> getRelatedVolumeGroupFromVfModule(String vnfId, String vfModuleId) throws Exception {
559         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
560         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP);
561         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
562         VolumeGroup volumeGroup = null;
563         if (!volumeGroups.isPresent()) {
564             logger.debug("VfModule does not have a volume group attached");
565             return Optional.empty();
566         } else {
567             if (volumeGroups.get().getVolumeGroup().size() > 1) {
568                 throw new Exception("Multiple VolumeGroups Returned");
569             } else {
570                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
571             }
572             return Optional.of(volumeGroup);
573         }
574     }
575
576     public Optional<org.onap.aai.domain.yang.VpnBinding> getAICVpnBindingFromNetwork(
577             org.onap.aai.domain.yang.L3Network aaiLocalNetwork) {
578         AAIResultWrapper networkWrapper = new AAIResultWrapper(aaiLocalNetwork);
579         if (networkWrapper.getRelationships().isPresent()
580                 && !networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).isEmpty()) {
581             return getAAIResourceDepthOne(
582                     networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).get(0))
583                             .asBean(org.onap.aai.domain.yang.VpnBinding.class);
584         }
585         return Optional.empty();
586     }
587
588     public ServiceInstances getAAIServiceInstancesGloballyByName(String serviceInstanceName) {
589
590         return injectionHelper.getAaiClient()
591                 .get(ServiceInstances.class, AAIUriFactory.createNodesUri(AAIObjectPlurals.SERVICE_INSTANCE)
592                         .queryParam("service-instance-name", serviceInstanceName))
593                 .orElseGet(() -> {
594                     logger.debug("No Service Instance matched by name");
595                     return null;
596                 });
597     }
598
599     public boolean existsAAINetworksGloballyByName(String networkName) {
600
601         AAIResourceUri l3networkUri =
602                 AAIUriFactory.createResourceUri(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
603         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
604         return aaiRC.exists(l3networkUri);
605     }
606
607     public boolean existsAAIVfModuleGloballyByName(String vfModuleName) {
608         AAIResourceUri vfModuleUri =
609                 AAIUriFactory.createNodesUri(AAIObjectPlurals.VF_MODULE).queryParam("vf-module-name", vfModuleName);
610         return injectionHelper.getAaiClient().exists(vfModuleUri);
611     }
612
613     public boolean existsAAIConfigurationGloballyByName(String configurationName) {
614         AAIResourceUri configUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.CONFIGURATION)
615                 .queryParam("configuration-name", configurationName);
616         return injectionHelper.getAaiClient().exists(configUri);
617     }
618
619     public boolean existsAAIVolumeGroupGloballyByName(String volumeGroupName) {
620         AAIResourceUri volumeGroupUri = AAIUriFactory.createNodesUri(AAIObjectPlurals.VOLUME_GROUP)
621                 .queryParam("volume-group-name", volumeGroupName);
622         return injectionHelper.getAaiClient().exists(volumeGroupUri);
623     }
624
625     public GenericVnfs getAAIVnfsGloballyByName(String vnfName) {
626
627         return injectionHelper.getAaiClient()
628                 .get(GenericVnfs.class,
629                         AAIUriFactory.createNodesUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName))
630                 .orElseGet(() -> {
631                     logger.debug("No GenericVnfs matched by name");
632                     return null;
633                 });
634     }
635
636     public Optional<Configuration> getRelatedConfigurationByNameFromServiceInstance(String serviceInstanceId,
637             String configurationName) throws MultipleObjectsFoundException {
638         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
639         uri.relatedTo(AAIObjectPlurals.CONFIGURATION).queryParam("configuration-name", configurationName);
640         Optional<Configurations> configurations = injectionHelper.getAaiClient().get(Configurations.class, uri);
641         Configuration configuration = null;
642         if (!configurations.isPresent()) {
643             logger.debug("No Configurations matched by name");
644             return Optional.empty();
645         } else {
646             if (configurations.get().getConfiguration().size() > 1) {
647                 String message = String.format(
648                         "Multiple configurations found for service-instance-d: %s and configuration-name: %s.",
649                         serviceInstanceId, configurationName);
650                 throw new MultipleObjectsFoundException(message);
651             } else {
652                 configuration = configurations.get().getConfiguration().get(0);
653             }
654             return Optional.of(configuration);
655         }
656     }
657 }