8aa03e2920eaa249d71037f909788c5164d65527
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.aaiclient.client.graphinventory;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import javax.ws.rs.core.GenericType;
31 import org.onap.aaiclient.client.aai.entities.Results;
32 import org.onap.aaiclient.client.graphinventory.entities.GraphInventoryResultWrapper;
33 import org.onap.aaiclient.client.graphinventory.entities.Pathed;
34 import org.onap.aaiclient.client.graphinventory.entities.ResourceAndUrl;
35 import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryUri;
36 import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
37 import com.fasterxml.jackson.core.type.TypeReference;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 public abstract class GraphInventoryQueryClient<S, I, Wrapper extends GraphInventoryResultWrapper<?>, Type extends GraphInventoryObjectType> {
41
42     private Optional<String> depth = Optional.empty();
43     private boolean nodesOnly = false;
44     private Optional<GraphInventorySubgraphType> subgraph = Optional.empty();
45     private GraphInventoryClient client;
46     private GraphInventoryCommonObjectMapperProvider mapperProvider = new GraphInventoryCommonObjectMapperProvider();
47
48     public GraphInventoryQueryClient(GraphInventoryClient client) {
49         this.client = client;
50     }
51
52     protected abstract GraphInventoryUri getQueryUri();
53
54     public String query(Format format, I query) {
55         return client.createClient(setupQueryParams(getQueryUri().queryParam("format", format.toString()))).put(query,
56                 String.class);
57     }
58
59     protected <R> List<R> querySingleType(Format format, I query, Class<R> clazz) {
60         return client.createClient(setupQueryParams(getQueryUri().queryParam("format", format.toString())))
61                 .put(query, new GenericType<Results<Object>>() {}).getResult().stream().map(item -> {
62                     try {
63                         return mapperProvider.getMapper().readValue(mapperProvider.getMapper().writeValueAsString(item),
64                                 clazz);
65                     } catch (IOException e) {
66                         throw new IllegalArgumentException("could not map values from json", e);
67                     }
68                 }).collect(Collectors.toList());
69     }
70
71     public List<Pathed> queryPathed(I query) {
72         return querySingleType(Format.PATHED, query, Pathed.class);
73     }
74
75     public List<Id> queryId(I query) {
76         return querySingleType(Format.ID, query, Id.class);
77     }
78
79     public <R> List<R> querySingleResource(I query, Class<R> clazz) {
80         try {
81             return getResourceAndUrl(query).stream().map(item -> item.getWrapper().asBean(clazz).get())
82                     .collect(Collectors.toList());
83         } catch (IOException e) {
84             throw new IllegalArgumentException("could not map values from json", e);
85         }
86     }
87
88     public List<ResourceAndUrl<Wrapper>> getResourceAndUrl(I query) throws IOException {
89         List<ResourceAndUrl<Wrapper>> result = new ArrayList<>();
90         ObjectMapper mapper = mapperProvider.getMapper();
91         Results<Map<String, Object>> resultsFromJson = mapper.readValue(query(Format.RESOURCE_AND_URL, query),
92                 new TypeReference<Results<Map<String, Object>>>() {});
93         for (Map<String, Object> m : resultsFromJson.getResult()) {
94             for (Entry<String, Object> entrySet : m.entrySet()) {
95                 if (!entrySet.getKey().equals("url")) {
96                     String url = (String) m.get("url");
97                     String stringJson = mapper.writeValueAsString(entrySet.getValue());
98                     result.add(
99                             new ResourceAndUrl<Wrapper>(url, createType(entrySet.getKey()), createWrapper(stringJson)));
100                 }
101             }
102         }
103
104         return result;
105     }
106
107     public abstract Wrapper createWrapper(String json);
108
109     public abstract Type createType(String name);
110
111     public S depth(String depth) {
112         this.depth = Optional.of(depth);
113         return (S) this;
114     }
115
116     public S nodesOnly() {
117         this.nodesOnly = true;
118         return (S) this;
119     }
120
121     public S subgraph(GraphInventorySubgraphType type) {
122
123         subgraph = Optional.of(type);
124
125         return (S) this;
126     }
127
128     protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) {
129         GraphInventoryUri clone = uri.clone();
130         if (this.depth.isPresent()) {
131             clone.queryParam("depth", depth.get());
132         }
133         if (this.nodesOnly) {
134             clone.queryParam("nodesOnly", "");
135         }
136         if (this.subgraph.isPresent()) {
137             clone.queryParam("subgraph", this.subgraph.get().toString());
138         }
139         return clone;
140     }
141 }