aboutsummaryrefslogtreecommitdiffstats
path: root/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java')
-rw-r--r--graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java141
1 files changed, 141 insertions, 0 deletions
diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java
new file mode 100644
index 0000000000..8aa03e2920
--- /dev/null
+++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java
@@ -0,0 +1,141 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.aaiclient.client.graphinventory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import javax.ws.rs.core.GenericType;
+import org.onap.aaiclient.client.aai.entities.Results;
+import org.onap.aaiclient.client.graphinventory.entities.GraphInventoryResultWrapper;
+import org.onap.aaiclient.client.graphinventory.entities.Pathed;
+import org.onap.aaiclient.client.graphinventory.entities.ResourceAndUrl;
+import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryUri;
+import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public abstract class GraphInventoryQueryClient<S, I, Wrapper extends GraphInventoryResultWrapper<?>, Type extends GraphInventoryObjectType> {
+
+ private Optional<String> depth = Optional.empty();
+ private boolean nodesOnly = false;
+ private Optional<GraphInventorySubgraphType> subgraph = Optional.empty();
+ private GraphInventoryClient client;
+ private GraphInventoryCommonObjectMapperProvider mapperProvider = new GraphInventoryCommonObjectMapperProvider();
+
+ public GraphInventoryQueryClient(GraphInventoryClient client) {
+ this.client = client;
+ }
+
+ protected abstract GraphInventoryUri getQueryUri();
+
+ public String query(Format format, I query) {
+ return client.createClient(setupQueryParams(getQueryUri().queryParam("format", format.toString()))).put(query,
+ String.class);
+ }
+
+ protected <R> List<R> querySingleType(Format format, I query, Class<R> clazz) {
+ return client.createClient(setupQueryParams(getQueryUri().queryParam("format", format.toString())))
+ .put(query, new GenericType<Results<Object>>() {}).getResult().stream().map(item -> {
+ try {
+ return mapperProvider.getMapper().readValue(mapperProvider.getMapper().writeValueAsString(item),
+ clazz);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("could not map values from json", e);
+ }
+ }).collect(Collectors.toList());
+ }
+
+ public List<Pathed> queryPathed(I query) {
+ return querySingleType(Format.PATHED, query, Pathed.class);
+ }
+
+ public List<Id> queryId(I query) {
+ return querySingleType(Format.ID, query, Id.class);
+ }
+
+ public <R> List<R> querySingleResource(I query, Class<R> clazz) {
+ try {
+ return getResourceAndUrl(query).stream().map(item -> item.getWrapper().asBean(clazz).get())
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new IllegalArgumentException("could not map values from json", e);
+ }
+ }
+
+ public List<ResourceAndUrl<Wrapper>> getResourceAndUrl(I query) throws IOException {
+ List<ResourceAndUrl<Wrapper>> result = new ArrayList<>();
+ ObjectMapper mapper = mapperProvider.getMapper();
+ Results<Map<String, Object>> resultsFromJson = mapper.readValue(query(Format.RESOURCE_AND_URL, query),
+ new TypeReference<Results<Map<String, Object>>>() {});
+ for (Map<String, Object> m : resultsFromJson.getResult()) {
+ for (Entry<String, Object> entrySet : m.entrySet()) {
+ if (!entrySet.getKey().equals("url")) {
+ String url = (String) m.get("url");
+ String stringJson = mapper.writeValueAsString(entrySet.getValue());
+ result.add(
+ new ResourceAndUrl<Wrapper>(url, createType(entrySet.getKey()), createWrapper(stringJson)));
+ }
+ }
+ }
+
+ return result;
+ }
+
+ public abstract Wrapper createWrapper(String json);
+
+ public abstract Type createType(String name);
+
+ public S depth(String depth) {
+ this.depth = Optional.of(depth);
+ return (S) this;
+ }
+
+ public S nodesOnly() {
+ this.nodesOnly = true;
+ return (S) this;
+ }
+
+ public S subgraph(GraphInventorySubgraphType type) {
+
+ subgraph = Optional.of(type);
+
+ return (S) this;
+ }
+
+ protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) {
+ GraphInventoryUri clone = uri.clone();
+ if (this.depth.isPresent()) {
+ clone.queryParam("depth", depth.get());
+ }
+ if (this.nodesOnly) {
+ clone.queryParam("nodesOnly", "");
+ }
+ if (this.subgraph.isPresent()) {
+ clone.queryParam("subgraph", this.subgraph.get().toString());
+ }
+ return clone;
+ }
+}