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