diff options
Diffstat (limited to 'common/src/main/java/org')
14 files changed, 246 insertions, 24 deletions
diff --git a/common/src/main/java/org/onap/so/client/RestClient.java b/common/src/main/java/org/onap/so/client/RestClient.java index 76134a42f4..4284cea847 100644 --- a/common/src/main/java/org/onap/so/client/RestClient.java +++ b/common/src/main/java/org/onap/so/client/RestClient.java @@ -262,6 +262,10 @@ public abstract class RestClient { public <T> T put(Object obj, Class<T> resultClass) { return format(method("PUT", obj), resultClass).orElse(null); } + + public <T> T put(Object obj, GenericType<T> resultClass) { + return format(method("PUT", obj), resultClass).orElse(null); + } public <T> T delete(Class<T> resultClass) { return format(method("DELETE", null), resultClass).orElse(null); diff --git a/common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java b/common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java index 4cee4f3df7..5f2c623d57 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java @@ -20,12 +20,13 @@ package org.onap.so.client.aai; +import org.onap.so.client.aai.entities.AAIResultWrapper; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.graphinventory.GraphInventoryQueryClient; import org.onap.so.client.graphinventory.entities.DSLQuery; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; -public class AAIDSLQueryClient extends GraphInventoryQueryClient<AAIDSLQueryClient, DSLQuery> { +public class AAIDSLQueryClient extends GraphInventoryQueryClient<AAIDSLQueryClient, DSLQuery, AAIResultWrapper, AAIObjectType> { public AAIDSLQueryClient() { super(new AAIClient()); @@ -40,4 +41,15 @@ public class AAIDSLQueryClient extends GraphInventoryQueryClient<AAIDSLQueryClie return AAIUriFactory.createResourceUri(AAIObjectType.DSL); } + + @Override + public AAIResultWrapper createWrapper(String json) { + return new AAIResultWrapper(json); + } + + @Override + public AAIObjectType createType(String name) { + return AAIObjectType.fromTypeName(name); + } + } diff --git a/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java b/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java index 66d5d1f5cb..cc855e444a 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java @@ -20,12 +20,13 @@ package org.onap.so.client.aai; +import org.onap.so.client.aai.entities.AAIResultWrapper; import org.onap.so.client.aai.entities.CustomQuery; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.graphinventory.GraphInventoryQueryClient; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; -public class AAIQueryClient extends GraphInventoryQueryClient<AAIQueryClient, CustomQuery> { +public class AAIQueryClient extends GraphInventoryQueryClient<AAIQueryClient, CustomQuery, AAIResultWrapper, AAIObjectType> { public AAIQueryClient() { super(new AAIClient()); @@ -44,5 +45,15 @@ public class AAIQueryClient extends GraphInventoryQueryClient<AAIQueryClient, Cu protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) { return super.setupQueryParams(uri); } + + @Override + public AAIResultWrapper createWrapper(String json) { + return new AAIResultWrapper(json); + } + + @Override + public AAIObjectType createType(String name) { + return AAIObjectType.fromTypeName(name); + } } diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java index 324bb8abad..a627480d5d 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java @@ -33,6 +33,7 @@ import org.onap.so.client.aai.AAIObjectType; import org.onap.so.client.aai.AAIResourcesClient; import org.onap.so.client.aai.entities.Results; import org.onap.so.client.graphinventory.Format; +import org.onap.so.client.graphinventory.entities.Pathed; import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri; import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; @@ -82,9 +83,9 @@ public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri Optional<String> result; ObjectMapper mapper = new ObjectMapper(); - Results<Map<String, String>> results = mapper.readValue(jsonString, new TypeReference<Results<Map<String, String>>>(){}); + Results<Pathed> results = mapper.readValue(jsonString, new TypeReference<Results<Pathed>>(){}); if (results.getResult().size() == 1) { - String uriString = results.getResult().get(0).get("resource-link"); + String uriString = results.getResult().get(0).getResourceLink(); URI uri = UriBuilder.fromUri(uriString).build(); String rawPath = uri.getRawPath(); result = Optional.of(rawPath.replaceAll("/aai/v\\d+", "")); diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java index c4bf0f0beb..152e9d7a36 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java @@ -20,17 +20,33 @@ package org.onap.so.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 org.onap.so.client.aai.entities.CustomQuery; +import javax.ws.rs.core.GenericType; + +import org.onap.so.client.aai.entities.Results; +import org.onap.so.client.graphinventory.entities.GraphInventoryResultWrapper; +import org.onap.so.client.graphinventory.entities.Pathed; +import org.onap.so.client.graphinventory.entities.ResourceAndUrl; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; -public abstract class GraphInventoryQueryClient<S, I> { +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; @@ -42,6 +58,58 @@ public abstract class GraphInventoryQueryClient<S, I> { 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; diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java index 7da1408f2d..1e4750d2cf 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java @@ -64,13 +64,14 @@ public class DSLNode implements QueryStep { @Override public String build() { + StringBuilder result = new StringBuilder(query); if (output) { - query.append("*"); + result.append("*"); } for (DSLNodeKey key : nodeKeys) { - query.append(key.build()); + result.append(key.build()); } - return query.toString(); + return result.toString(); } } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java index 159bfb1c29..c40a3e6f96 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java @@ -49,11 +49,12 @@ public class DSLNodeKey implements QueryStep { @Override public String build() { - + StringBuilder result = new StringBuilder(query); + if (not) { - query.append(" !"); + result.append(" !"); } - query.append("('").append(keyName).append("', "); + result.append("('").append(keyName).append("', "); List<String> temp = new ArrayList<>(); for (String item : values) { if (item.equals("null")) { @@ -64,8 +65,8 @@ public class DSLNodeKey implements QueryStep { temp.add(String.format("'%s'", item)); } } - query.append(Joiner.on(", ").join(temp)).append(")"); + result.append(Joiner.on(", ").join(temp)).append(")"); - return query.toString(); + return result.toString(); } } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java index 73dccea8e6..3a47c38444 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.stream.Collectors; import org.onap.so.client.aai.entities.QueryStep; +import org.onap.so.client.graphinventory.GraphInventoryObjectName; import com.google.common.base.Joiner; @@ -33,7 +34,7 @@ import com.google.common.base.Joiner; public class DSLQueryBuilder<S, E> implements QueryStep { private List<QueryStep> steps = new ArrayList<>(); - + private String suffix = ""; public DSLQueryBuilder() { @@ -94,8 +95,17 @@ public class DSLQueryBuilder<S, E> implements QueryStep { return this; } - public String limit(int limit) { - return compile() + " LIMIT " + limit; + public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) { + return to(__.node(name)); + } + + public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) { + return to(__.node(name, key)); + } + + public DSLQueryBuilder<S, E> limit(int limit) { + suffix = " LIMIT " + limit; + return this; } @Override @@ -103,8 +113,31 @@ public class DSLQueryBuilder<S, E> implements QueryStep { return compile(); } + @Override + public String toString() { + return build(); + } + + @Override + public boolean equals(Object o) { + if (o != null) { + if (o instanceof QueryStep) { + return ((QueryStep)o).build().equals(this.build()); + } else if (o instanceof String) { + return o.equals(this.build()); + } + } + return false; + } + + @Override + public int hashCode() { + + return build().hashCode(); + } + private String compile() { - return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())); + return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix; } protected QueryStep getFirst() { diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryRelationships.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryRelationships.java index 759fad7e54..752a743247 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryRelationships.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryRelationships.java @@ -126,7 +126,7 @@ public abstract class GraphInventoryRelationships<Wrapper extends GraphInventory protected List<String> getRelatedLinks(Optional<GraphInventoryObjectName> type) { String matcher = ""; if (type.isPresent()) { - matcher = "[?(@.related-to=='" + type.get() + "')]"; + matcher = "[?(@.related-to=='" + type.get().typeName() + "')]"; } return JsonPathUtil.getInstance().locateResultList(this.jsonBody, String.format("$.relationship%s.related-link", matcher)); } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java index c5651c0d26..c0b29e46c2 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java @@ -86,22 +86,27 @@ public abstract class GraphInventoryResultWrapper<R extends GraphInventoryRelati } public Map<String, Object> asMap() { + + return asBean(new TypeReference<Map<String, Object>>(){}).orElse(new HashMap<>()); + } + + public <T> Optional<T> asBean(Class<T> clazz) { if (isEmpty()) { - return new HashMap<>(); + return Optional.empty(); } try { - return mapper.readValue(this.jsonBody, new TypeReference<Map<String, Object>>(){}); + return Optional.of(mapper.readValue(this.jsonBody, clazz)); } catch (IOException e) { - return new HashMap<>(); + return Optional.empty(); } } - public <T> Optional<T> asBean(Class<T> clazz) { + public <T> Optional<T> asBean(TypeReference<T> reference) { if (isEmpty()) { return Optional.empty(); } try { - return Optional.of(mapper.readValue(this.jsonBody, clazz)); + return Optional.of(mapper.readValue(this.jsonBody, reference)); } catch (IOException e) { return Optional.empty(); } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/Id.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/Id.java new file mode 100644 index 0000000000..76492dc38b --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/Id.java @@ -0,0 +1,5 @@ +package org.onap.so.client.graphinventory.entities; + +public class Id extends Resource { + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/Pathed.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/Pathed.java new file mode 100644 index 0000000000..a53bdc0259 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/Pathed.java @@ -0,0 +1,5 @@ +package org.onap.so.client.graphinventory.entities; + +public class Pathed extends Resource { + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/Resource.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/Resource.java new file mode 100644 index 0000000000..5c53f2cbf1 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/Resource.java @@ -0,0 +1,40 @@ +package org.onap.so.client.graphinventory.entities; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "resource-type", + "resource-link" +}) +public class Resource { + + @JsonProperty("resource-type") + private String resourceType; + @JsonProperty("resource-link") + private String resourceLink; + + @JsonProperty("resource-type") + public String getResourceType() { + return resourceType; + } + + @JsonProperty("resource-type") + public void setResourceType(String resourceType) { + this.resourceType = resourceType; + } + + @JsonProperty("resource-link") + public String getResourceLink() { + return resourceLink; + } + + @JsonProperty("resource-link") + public void setResourceLink(String resourceLink) { + this.resourceLink = resourceLink; + } + +} + diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/ResourceAndUrl.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/ResourceAndUrl.java new file mode 100644 index 0000000000..e53fc02da7 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/ResourceAndUrl.java @@ -0,0 +1,36 @@ +package org.onap.so.client.graphinventory.entities; + +import org.onap.so.client.graphinventory.GraphInventoryObjectType; + +public class ResourceAndUrl<Wrapper extends GraphInventoryResultWrapper> { + + private String url; + private GraphInventoryObjectType type; + private Wrapper wrapper; + + public ResourceAndUrl(String url, GraphInventoryObjectType type, Wrapper wrapper) { + this.url = url; + this.type = type; + this.wrapper = wrapper; + } + public String getUrl() { + return url; + } + public void setUrl(String url) { + this.url = url; + } + public Wrapper getWrapper() { + return wrapper; + } + public void setWrapper(Wrapper wrapper) { + this.wrapper = wrapper; + } + public GraphInventoryObjectType getType() { + return type; + } + + public void setType(GraphInventoryObjectType type) { + this.type = type; + } + +} |