2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.aai.util;
23 import com.google.common.collect.ImmutableMap;
24 import org.apache.commons.text.StrSubstitutor;
25 import org.onap.vid.aai.AaiClientInterface;
26 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.*;
27 import org.onap.vid.aai.model.interfaces.AaiModelWithRelationships;
28 import org.onap.vid.utils.Multival;
29 import org.onap.vid.utils.Unchecked;
31 import javax.inject.Inject;
34 import java.util.function.Predicate;
35 import java.util.stream.Stream;
37 import static java.util.stream.Collectors.toSet;
39 public class ServiceInstanceStandardQuery {
41 private static final String SERVICE_INSTANCE_URI_TEMPLATE = "" +
42 "business/customers/customer/${global-customer-id}" +
43 "/service-subscriptions/service-subscription/${service-type}" +
44 "/service-instances/service-instance/${service-instance-id}";
46 private final AaiClientInterface aaiClient;
49 public ServiceInstanceStandardQuery(AaiClientInterface aaiClient) {
50 this.aaiClient = aaiClient;
53 public ServiceInstance fetchServiceInstance(String globalCustomerId, String serviceType, String serviceInstanceId) {
54 final String serviceInstanceUri = getServiceInstanceUri(globalCustomerId, serviceType, serviceInstanceId);
56 return fetchServiceInstance(Unchecked.toURI(serviceInstanceUri));
59 ServiceInstance fetchServiceInstance(URI serviceInstanceUri) {
60 return objectByUri(ServiceInstance.class, serviceInstanceUri);
63 protected <T> T objectByUri(Class<T> clazz, URI aaiResourceUri) {
64 return aaiClient.typedAaiGet(aaiResourceUri, clazz);
67 public Multival<ServiceInstance, Vnf> fetchRelatedVnfs(ServiceInstance serviceInstance) {
68 return fetchRelated("service", serviceInstance, "generic-vnf", Vnf.class);
71 public <K extends AaiModelWithRelationships> Multival<K, Network> fetchRelatedL3Networks(String sourceType, K source) {
72 return fetchRelated(sourceType, source, "l3-network", Network.class);
75 public Multival<Network, Vlan> fetchRelatedVlanTags(Network network) {
76 return fetchRelated("network", network, "vlan-tag", Vlan.class);
79 private String getServiceInstanceUri(String globalCustomerId, String serviceType, String serviceInstanceId) {
80 return new StrSubstitutor(ImmutableMap.of(
81 "global-customer-id", globalCustomerId,
82 "service-type", serviceType,
83 "service-instance-id", serviceInstanceId
84 )).replace(SERVICE_INSTANCE_URI_TEMPLATE);
87 private <K extends AaiModelWithRelationships, V> Multival<K, V> fetchRelated(String sourceType, K source, String destType, Class<V> destClass) {
92 fetchRelatedInner(source, destType, destClass)
96 private <K extends AaiModelWithRelationships, V> Set<V> fetchRelatedInner(K source, String destType, Class<V> destClass) {
97 return getURIsOf(source, relationship -> relatedTo(relationship, destType))
98 .map(destUri -> objectByUri(destClass, destUri))
102 protected Stream<URI> getURIsOf(AaiModelWithRelationships aaiModel, Predicate<Relationship> predicate) {
103 return aaiModel.getRelationshipList().getRelationship().stream()
105 .map(r -> r.relatedLink)
106 .map(Unchecked::toURI);
109 protected static boolean relatedTo(Relationship r, String relationshipName) {
110 return relationshipName.equals(r.getRelatedTo());