Merge "align logic to new so api"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / ServiceInstanceStandardQuery.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.aai.util;
22
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;
30
31 import javax.inject.Inject;
32 import java.net.URI;
33 import java.util.Set;
34 import java.util.function.Predicate;
35 import java.util.stream.Stream;
36
37 import static java.util.stream.Collectors.toSet;
38
39 public class ServiceInstanceStandardQuery {
40
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}";
45     
46     private final AaiClientInterface aaiClient;
47
48     @Inject
49     public ServiceInstanceStandardQuery(AaiClientInterface aaiClient) {
50         this.aaiClient = aaiClient;
51     }
52
53     public ServiceInstance fetchServiceInstance(String globalCustomerId, String serviceType, String serviceInstanceId) {
54         final String serviceInstanceUri = getServiceInstanceUri(globalCustomerId, serviceType, serviceInstanceId);
55
56         return fetchServiceInstance(Unchecked.toURI(serviceInstanceUri));
57     }
58
59     ServiceInstance fetchServiceInstance(URI serviceInstanceUri) {
60         return objectByUri(ServiceInstance.class, serviceInstanceUri);
61     }
62
63     protected <T> T objectByUri(Class<T> clazz, URI aaiResourceUri) {
64         return aaiClient.typedAaiGet(aaiResourceUri, clazz);
65     }
66
67     public Multival<ServiceInstance, Vnf> fetchRelatedVnfs(ServiceInstance serviceInstance) {
68         return fetchRelated("service", serviceInstance, "generic-vnf", Vnf.class);
69     }
70
71     public <K extends AaiModelWithRelationships> Multival<K, Network> fetchRelatedL3Networks(String sourceType, K source) {
72         return fetchRelated(sourceType, source, "l3-network", Network.class);
73     }
74
75     public Multival<Network, Vlan> fetchRelatedVlanTags(Network network) {
76         return fetchRelated("network", network, "vlan-tag", Vlan.class);
77     }
78
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);
85     }
86
87     private <K extends AaiModelWithRelationships, V> Multival<K, V> fetchRelated(String sourceType, K source, String destType, Class<V> destClass) {
88         return Multival.of(
89                 sourceType,
90                 source,
91                 destType,
92                 fetchRelatedInner(source, destType, destClass)
93         );
94     }
95
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))
99                 .collect(toSet());
100     }
101
102     protected Stream<URI> getURIsOf(AaiModelWithRelationships aaiModel, Predicate<Relationship> predicate) {
103         return aaiModel.getRelationshipList().getRelationship().stream()
104                 .filter(predicate)
105                 .map(r -> r.relatedLink)
106                 .map(Unchecked::toURI);
107     }
108
109     protected static boolean relatedTo(Relationship r, String relationshipName) {
110         return relationshipName.equals(r.getRelatedTo());
111     }
112
113 }