Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / ServiceInstanceStandardQuery.java
1 package org.onap.vid.aai.util;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.apache.commons.text.StrSubstitutor;
5 import org.onap.vid.aai.AaiClientInterface;
6 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.*;
7 import org.onap.vid.aai.model.interfaces.AaiModelWithRelationships;
8 import org.onap.vid.utils.Multival;
9 import org.onap.vid.utils.Unchecked;
10
11 import javax.inject.Inject;
12 import java.net.URI;
13 import java.util.Set;
14 import java.util.function.Predicate;
15 import java.util.stream.Stream;
16
17 import static java.util.stream.Collectors.toSet;
18
19 public class ServiceInstanceStandardQuery {
20
21     private static final String SERVICE_INSTANCE_URI_TEMPLATE = "" +
22             "business/customers/customer/${global-customer-id}" +
23             "/service-subscriptions/service-subscription/${service-type}" +
24             "/service-instances/service-instance/${service-instance-id}";
25     
26     private final AaiClientInterface aaiClient;
27
28     @Inject
29     public ServiceInstanceStandardQuery(AaiClientInterface aaiClient) {
30         this.aaiClient = aaiClient;
31     }
32
33     public ServiceInstance fetchServiceInstance(String globalCustomerId, String serviceType, String serviceInstanceId) {
34         final String serviceInstanceUri = getServiceInstanceUri(globalCustomerId, serviceType, serviceInstanceId);
35
36         return fetchServiceInstance(Unchecked.toURI(serviceInstanceUri));
37     }
38
39     ServiceInstance fetchServiceInstance(URI serviceInstanceUri) {
40         return objectByUri(ServiceInstance.class, serviceInstanceUri);
41     }
42
43     protected <T> T objectByUri(Class<T> clazz, URI aaiResourceUri) {
44         return aaiClient.typedAaiGet(aaiResourceUri, clazz);
45     }
46
47     public Multival<ServiceInstance, Vnf> fetchRelatedVnfs(ServiceInstance serviceInstance) {
48         return fetchRelated("service", serviceInstance, "generic-vnf", Vnf.class);
49     }
50
51     public <K extends AaiModelWithRelationships> Multival<K, Network> fetchRelatedL3Networks(String sourceType, K source) {
52         return fetchRelated(sourceType, source, "l3-network", Network.class);
53     }
54
55     public Multival<Network, Vlan> fetchRelatedVlanTags(Network network) {
56         return fetchRelated("network", network, "vlan-tag", Vlan.class);
57     }
58
59     private String getServiceInstanceUri(String globalCustomerId, String serviceType, String serviceInstanceId) {
60         return new StrSubstitutor(ImmutableMap.of(
61                 "global-customer-id", globalCustomerId,
62                 "service-type", serviceType,
63                 "service-instance-id", serviceInstanceId
64         )).replace(SERVICE_INSTANCE_URI_TEMPLATE);
65     }
66
67     private <K extends AaiModelWithRelationships, V> Multival<K, V> fetchRelated(String sourceType, K source, String destType, Class<V> destClass) {
68         return Multival.of(
69                 sourceType,
70                 source,
71                 destType,
72                 fetchRelatedInner(source, destType, destClass)
73         );
74     }
75
76     private <K extends AaiModelWithRelationships, V> Set<V> fetchRelatedInner(K source, String destType, Class<V> destClass) {
77         return getURIsOf(source, relationship -> relatedTo(relationship, destType))
78                 .map(destUri -> objectByUri(destClass, destUri))
79                 .collect(toSet());
80     }
81
82     protected Stream<URI> getURIsOf(AaiModelWithRelationships aaiModel, Predicate<Relationship> predicate) {
83         return aaiModel.getRelationshipList().getRelationship().stream()
84                 .filter(predicate)
85                 .map(r -> r.relatedLink)
86                 .map(Unchecked::toURI);
87     }
88
89     protected static boolean relatedTo(Relationship r, String relationshipName) {
90         return relationshipName.equals(r.getRelatedTo());
91     }
92
93 }