2 * ============LICENSE_START=======================================================
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
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.aaiclient.client.graphinventory;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
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;
40 public abstract class GraphInventoryQueryClient<S, I, Wrapper extends GraphInventoryResultWrapper<?>, Type extends GraphInventoryObjectType> {
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();
48 public GraphInventoryQueryClient(GraphInventoryClient client) {
52 protected abstract GraphInventoryUri getQueryUri();
54 public String query(Format format, I query) {
55 return client.createClient(setupQueryParams(getQueryUri().queryParam("format", format.toString()))).put(query,
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 -> {
63 return mapperProvider.getMapper().readValue(mapperProvider.getMapper().writeValueAsString(item),
65 } catch (IOException e) {
66 throw new IllegalArgumentException("could not map values from json", e);
68 }).collect(Collectors.toList());
71 public List<Pathed> queryPathed(I query) {
72 return querySingleType(Format.PATHED, query, Pathed.class);
75 public List<Id> queryId(I query) {
76 return querySingleType(Format.ID, query, Id.class);
79 public <R> List<R> querySingleResource(I query, Class<R> clazz) {
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);
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(new ResourceAndUrl<Wrapper>(url, createType(entrySet.getKey(), url),
99 createWrapper(stringJson)));
107 public abstract Wrapper createWrapper(String json);
109 public abstract Type createType(String name, String uri);
111 public S depth(String depth) {
112 this.depth = Optional.of(depth);
116 public S nodesOnly() {
117 this.nodesOnly = true;
121 public S subgraph(GraphInventorySubgraphType type) {
123 subgraph = Optional.of(type);
128 protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) {
129 GraphInventoryUri clone = uri.clone();
130 if (this.depth.isPresent()) {
131 clone.queryParam("depth", depth.get());
133 if (this.nodesOnly) {
134 clone.queryParam("nodesOnly", "");
136 if (this.subgraph.isPresent()) {
137 clone.queryParam("subgraph", this.subgraph.get().toString());