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.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;
41 public abstract class GraphInventoryQueryClient<S, I, Wrapper extends GraphInventoryResultWrapper<?>, Type extends GraphInventoryObjectType> {
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();
49 public GraphInventoryQueryClient(GraphInventoryClient client) {
53 protected abstract GraphInventoryUri getQueryUri();
55 public String query(Format format, I query) {
56 return client.createClient(setupQueryParams(getQueryUri().queryParam("format", format.toString()))).put(query,
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 -> {
64 return mapperProvider.getMapper().readValue(mapperProvider.getMapper().writeValueAsString(item),
66 } catch (IOException e) {
67 throw new IllegalArgumentException("could not map values from json", e);
69 }).collect(Collectors.toList());
72 public List<Pathed> queryPathed(I query) {
73 return querySingleType(Format.PATHED, query, Pathed.class);
76 public List<Id> queryId(I query) {
77 return querySingleType(Format.ID, query, Id.class);
80 public <R> List<R> querySingleResource(I query, Class<R> clazz) {
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);
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)));
108 public abstract Wrapper createWrapper(String json);
110 public abstract Type createType(String name, String uri);
112 public S depth(Depth depth) {
113 this.depth = Optional.of(depth);
117 public S nodesOnly() {
118 this.nodesOnly = true;
122 public S subgraph(GraphInventorySubgraphType type) {
124 subgraph = Optional.of(type);
129 protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) {
130 GraphInventoryUri clone = uri.clone();
131 if (this.depth.isPresent()) {
132 clone.queryParam("depth", depth.get().toString());
134 if (this.nodesOnly) {
135 clone.queryParam("nodesOnly", "");
137 if (this.subgraph.isPresent()) {
138 clone.queryParam("subgraph", this.subgraph.get().toString());
143 public GraphInventoryClient getClient() {