diff options
Diffstat (limited to 'graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities')
30 files changed, 1670 insertions, 0 deletions
diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLNodeBase.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLNodeBase.java new file mode 100644 index 0000000000..c071e24391 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLNodeBase.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.onap.aaiclient.client.aai.entities.QueryStep; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName; + +public abstract class DSLNodeBase<T extends DSLNodeBase<?>> implements QueryStep { + + protected final String nodeName; + protected final List<DSLNodeKey> nodeKeys; + protected final StringBuilder query; + protected boolean output = false; + + public DSLNodeBase() { + this.nodeName = ""; + this.nodeKeys = new ArrayList<>(); + this.query = new StringBuilder(); + + } + + public DSLNodeBase(GraphInventoryObjectName name) { + this.nodeName = name.typeName(); + this.nodeKeys = new ArrayList<>(); + this.query = new StringBuilder(); + query.append(nodeName); + } + + public DSLNodeBase(GraphInventoryObjectName name, DSLNodeKey... key) { + this.nodeName = name.typeName(); + this.nodeKeys = Arrays.asList(key); + this.query = new StringBuilder(); + query.append(nodeName); + } + + public DSLNodeBase(DSLNodeBase<?> copy) { + this.nodeName = copy.nodeName; + this.nodeKeys = copy.nodeKeys; + this.query = new StringBuilder(copy.query); + this.output = copy.output; + } + + public DSLOutputNode output() { + this.output = true; + + return new DSLOutputNode(this); + } + + public T and(DSLNodeKey... key) { + this.nodeKeys.addAll(Arrays.asList(key)); + + return (T) this; + } + + @Override + public String build() { + StringBuilder result = new StringBuilder(query); + if (output) { + result.append("*"); + } + for (DSLNodeKey key : nodeKeys) { + result.append(key.build()); + } + + return result.toString(); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLNodeKey.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLNodeKey.java new file mode 100644 index 0000000000..f7a9fddba0 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLNodeKey.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.onap.aaiclient.client.aai.entities.QueryStep; +import com.google.common.base.Joiner; + + +public class DSLNodeKey implements QueryStep { + + private boolean not = false; + private final StringBuilder query = new StringBuilder(); + private final String keyName; + private final List<Object> values; + + public DSLNodeKey(String keyName, Object... value) { + + this.keyName = keyName; + this.values = Arrays.asList(value); + } + + public DSLNodeKey not() { + + this.not = true; + return this; + } + + @Override + public String build() { + StringBuilder result = new StringBuilder(query); + + if (not) { + result.append(" !"); + } + result.append("('").append(keyName).append("', "); + List<Object> temp = new ArrayList<>(); + for (Object item : values) { + if ("null".equals(item)) { + temp.add(String.format("' %s '", item)); + } else if ("".equals(item)) { + temp.add("' '"); + } else { + if (item instanceof String) { + temp.add(String.format("'%s'", item)); + } else { + temp.add(item); + } + } + } + result.append(Joiner.on(", ").join(temp)).append(")"); + + return result.toString(); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLOutputNode.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLOutputNode.java new file mode 100644 index 0000000000..2e329b7841 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLOutputNode.java @@ -0,0 +1,8 @@ +package org.onap.aaiclient.client.graphinventory.entities; + +public class DSLOutputNode extends DSLNodeBase<DSLOutputNode> implements Output { + + public DSLOutputNode(DSLNodeBase<?> copy) { + super(copy); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLQuery.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLQuery.java new file mode 100644 index 0000000000..6d421913e3 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLQuery.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +import com.fasterxml.jackson.annotation.JsonInclude; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class DSLQuery { + + private String dsl; + + public DSLQuery() { + + } + + public DSLQuery(DSLTraversal<? extends Output> dsl) { + this.dsl = dsl.get(); + } + + public String getDsl() { + return dsl; + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLQueryBuilder.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLQueryBuilder.java new file mode 100644 index 0000000000..762203258a --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLQueryBuilder.java @@ -0,0 +1,158 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import org.onap.aaiclient.client.aai.entities.QueryStep; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName; +import com.google.common.base.Joiner; + +public class DSLQueryBuilder<S, E> { + + private List<QueryStep> steps = new ArrayList<>(); + private String suffix = ""; + + protected DSLQueryBuilder() { + + } + + protected DSLQueryBuilder(QueryStep node) { + steps.add(node); + } + + public <T> DSLQueryBuilder<S, DSLNodeBase<?>> node(DSLNodeBase<?> node) { + steps.add(node); + + return (DSLQueryBuilder<S, DSLNodeBase<?>>) this; + } + + public DSLQueryBuilder<S, Node> output() { + Object obj = steps.get(steps.size() - 1); + if (obj instanceof DSLNodeBase) { + ((DSLNodeBase) steps.get(steps.size() - 1)).output(); + } else if (obj.getClass().getName().contains("$$Lambda$")) { + // process lambda expressions + for (Field f : obj.getClass().getDeclaredFields()) { + f.setAccessible(true); + Object o; + try { + o = f.get(obj); + if (o instanceof DSLQueryBuilder && ((DSLQueryBuilder) o).steps.get(0) instanceof DSLNodeBase) { + ((DSLNodeBase) ((DSLQueryBuilder) o).steps.get(0)).output(); + } + } catch (IllegalArgumentException | IllegalAccessException e) { + } + f.setAccessible(false); + break; + } + } + return (DSLQueryBuilder<S, Node>) this; + } + + @SafeVarargs + public final <E2> DSLQueryBuilder<S, E2> union(final DSLQueryBuilder<?, E2>... union) { + + List<DSLQueryBuilder<?, ?>> unions = Arrays.asList(union); + steps.add(() -> { + StringBuilder query = new StringBuilder(); + + query.append("> [ ") + .append(Joiner.on(", ") + .join(unions.stream().map(item -> item.compile()).collect(Collectors.toList()))) + .append(" ]"); + return query.toString(); + }); + + return (DSLQueryBuilder<S, E2>) this; + } + + public DSLQueryBuilder<S, E> where(DSLQueryBuilder<?, ?> where) { + + steps.add(() -> { + StringBuilder query = new StringBuilder(); + query.append(where.compile()).append(")"); + String result = query.toString(); + if (!result.startsWith(">")) { + result = "> " + result; + } + return "(" + result; + }); + return this; + } + + public <E2> DSLQueryBuilder<S, E2> to(DSLQueryBuilder<?, E2> to) { + steps.add(() -> { + StringBuilder query = new StringBuilder(); + + query.append("> ").append(to.compile()); + return query.toString(); + }); + return (DSLQueryBuilder<S, E2>) this; + } + + public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) { + return (DSLQueryBuilder<S, E>) to(__.node(name)); + } + + public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) { + return (DSLQueryBuilder<S, E>) to(__.node(name, key)); + } + + public DSLQueryBuilder<S, E> limit(int limit) { + suffix = " LIMIT " + limit; + return this; + } + + public DSLTraversal<E> build() { + return new DSLTraversal<>(compile()); + } + + @Override + public String toString() { + return build().get(); + } + + @Override + public boolean equals(Object o) { + if (o != null) { + return o.toString().equals(toString()); + } + return false; + } + + @Override + public int hashCode() { + + return compile().hashCode(); + } + + private String compile() { + return String.join(" ", steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix; + } + + protected QueryStep getFirst() { + return steps.get(0); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLStartNode.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLStartNode.java new file mode 100644 index 0000000000..8abe918a9a --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLStartNode.java @@ -0,0 +1,19 @@ +package org.onap.aaiclient.client.graphinventory.entities; + +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName; + +public class DSLStartNode extends DSLNodeBase<DSLStartNode> implements Start { + + + public DSLStartNode() { + super(); + } + + public DSLStartNode(GraphInventoryObjectName name) { + super(name); + } + + public DSLStartNode(GraphInventoryObjectName name, DSLNodeKey... key) { + super(name, key); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLTraversal.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLTraversal.java new file mode 100644 index 0000000000..3befbb7220 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/DSLTraversal.java @@ -0,0 +1,33 @@ +package org.onap.aaiclient.client.graphinventory.entities; + +public class DSLTraversal<T> { + + private final String traversal; + + protected DSLTraversal(String traversal) { + this.traversal = traversal; + } + + public String get() { + return traversal; + } + + @Override + public String toString() { + return traversal; + } + + @Override + public int hashCode() { + return traversal.hashCode(); + } + + @Override + public boolean equals(Object o) { + if (o != null) { + return this.toString().equals(o); + } + return false; + } + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryEdgeLabel.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryEdgeLabel.java new file mode 100644 index 0000000000..e0e4a5ded5 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryEdgeLabel.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.entities; + +public interface GraphInventoryEdgeLabel { + + + @Override + public String toString(); +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryRelationships.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryRelationships.java new file mode 100644 index 0000000000..7d960877b0 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryRelationships.java @@ -0,0 +1,133 @@ +/*- + * ============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.entities; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Predicate; +import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType; +import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventorySingleResourceUri; +import org.onap.so.jsonpath.JsonPathUtil; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public abstract class GraphInventoryRelationships<Wrapper extends GraphInventoryResultWrapper<?>, Uri extends GraphInventorySingleResourceUri<?, ?, ?, ?>, Type extends GraphInventoryObjectType> { + + protected final ObjectMapper mapper; + protected Map<String, Object> map; + protected final String jsonBody; + + public GraphInventoryRelationships(String json) { + this.jsonBody = json; + this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper(); + try { + this.map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {}); + } catch (IOException e) { + this.map = new HashMap<>(); + } + } + + public List<Wrapper> getByType(GraphInventoryObjectName type) { + + return this.getAll(Optional.of(type)); + } + + public List<Wrapper> getAll() { + + return this.getAll(Optional.empty()); + } + + + public List<String> getRelatedLinks() { + return this.getRelatedLinks(Optional.empty()); + } + + public List<String> getRelatedLinks(GraphInventoryObjectName type) { + return this.getRelatedLinks(Optional.of(type)); + } + + public List<Uri> getRelatedUris() { + return this.getRelatedUris(x -> true); + } + + public List<Uri> getRelatedUris(GraphInventoryObjectName type) { + return this.getRelatedUris(x -> type.typeName().equals(x)); + } + + protected List<Uri> getRelatedUris(Predicate<String> p) { + List<Uri> result = new ArrayList<>(); + if (map.containsKey("relationship")) { + List<Map<String, Object>> relationships = (List<Map<String, Object>>) map.get("relationship"); + for (Map<String, Object> relationship : relationships) { + final String relatedTo = (String) relationship.get("related-to"); + if (p.test(relatedTo)) { + Type type; + type = fromTypeName(relatedTo); + final String relatedLink = (String) relationship.get("related-link"); + + result.add(createUri(type, relatedLink)); + } + } + } + return result; + } + + + + protected List<Wrapper> getAll(final Optional<GraphInventoryObjectName> type) { + List<Uri> relatedLinks; + if (type.isPresent()) { + relatedLinks = this.getRelatedUris(type.get()); + } else { + relatedLinks = this.getRelatedUris(); + } + ArrayList<Wrapper> result = new ArrayList<>(); + for (Uri link : relatedLinks) { + result.add(this.get(link)); + } + return result; + } + + protected abstract Wrapper get(Uri uri); + + protected abstract Uri createUri(Type type, String relatedLink); + + protected abstract Type fromTypeName(String name); + + protected List<String> getRelatedLinks(Optional<GraphInventoryObjectName> type) { + String matcher = ""; + if (type.isPresent()) { + matcher = "[?(@.related-to=='" + type.get().typeName() + "')]"; + } + return JsonPathUtil.getInstance().locateResultList(this.jsonBody, + String.format("$.relationship%s.related-link", matcher)); + } + + public String getJson() { + return this.jsonBody; + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryResultWrapper.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryResultWrapper.java new file mode 100644 index 0000000000..9e6572388b --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryResultWrapper.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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.entities; + +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.onap.so.jsonpath.JsonPathUtil; +import org.slf4j.Logger; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public abstract class GraphInventoryResultWrapper<R extends GraphInventoryRelationships<?, ?, ?>> + implements Serializable { + + private static final long serialVersionUID = 5895841925807816727L; + protected final String jsonBody; + protected final ObjectMapper mapper; + private final transient Logger logger; + + protected GraphInventoryResultWrapper(String json, Logger logger) { + this.jsonBody = json; + this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper(); + this.logger = logger; + } + + protected GraphInventoryResultWrapper(Object aaiObject, Logger logger) { + this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper(); + this.jsonBody = mapObjectToString(aaiObject); + this.logger = logger; + } + + protected String mapObjectToString(Object aaiObject) { + try { + return mapper.writeValueAsString(aaiObject); + } catch (JsonProcessingException e) { + logger.warn("could not parse object into json - defaulting to empty object"); + return "{}"; + } + } + + public Optional<R> getRelationships() { + final String path = "$.relationship-list"; + if (isEmpty()) { + return Optional.empty(); + } + Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path); + if (result.isPresent()) { + return Optional.of(createRelationships(result.get())); + } else { + return Optional.empty(); + } + } + + protected abstract R createRelationships(String json); + + public String getJson() { + if (jsonBody == null) { + return "{}"; + } else { + return jsonBody; + } + } + + 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 Optional.empty(); + } + try { + return Optional.of(mapper.readValue(this.jsonBody, clazz)); + } catch (IOException e) { + return Optional.empty(); + } + } + + public <T> Optional<T> asBean(TypeReference<T> reference) { + if (isEmpty()) { + return Optional.empty(); + } + try { + return Optional.of(mapper.readValue(this.jsonBody, reference)); + } catch (IOException e) { + return Optional.empty(); + } + } + + public boolean isEmpty() { + return jsonBody == null; + } + + @Override + public String toString() { + return this.getJson(); + } + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Id.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Id.java new file mode 100644 index 0000000000..6eaaadd7fa --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Id.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +public class Id extends Resource { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Node.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Node.java new file mode 100644 index 0000000000..c893f66df0 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Node.java @@ -0,0 +1,5 @@ +package org.onap.aaiclient.client.graphinventory.entities; + +public interface Node extends Output { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Output.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Output.java new file mode 100644 index 0000000000..c8b98aa693 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Output.java @@ -0,0 +1,7 @@ +package org.onap.aaiclient.client.graphinventory.entities; + +import org.onap.aaiclient.client.aai.entities.QueryStep; + +public interface Output extends QueryStep { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Pathed.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Pathed.java new file mode 100644 index 0000000000..874dbc183b --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Pathed.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +public class Pathed extends Resource { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Resource.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Resource.java new file mode 100644 index 0000000000..b1e6104702 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Resource.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.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/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/ResourceAndUrl.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/ResourceAndUrl.java new file mode 100644 index 0000000000..fa41ce4c0e --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/ResourceAndUrl.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +import org.onap.aaiclient.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; + } + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Start.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Start.java new file mode 100644 index 0000000000..8282beef23 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/Start.java @@ -0,0 +1,7 @@ +package org.onap.aaiclient.client.graphinventory.entities; + +import org.onap.aaiclient.client.aai.entities.QueryStep; + +public interface Start extends QueryStep { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/TraversalBuilder.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/TraversalBuilder.java new file mode 100644 index 0000000000..14e9715074 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/TraversalBuilder.java @@ -0,0 +1,17 @@ +package org.onap.aaiclient.client.graphinventory.entities; + +public class TraversalBuilder { + + + private TraversalBuilder() { + + } + + public static DSLQueryBuilder<Start, Start> fragment(Start node) { + return new DSLQueryBuilder<>(node); + } + + public static DSLQueryBuilder<Output, Output> traversal(Output node) { + return new DSLQueryBuilder<>(node); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/__.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/__.java new file mode 100644 index 0000000000..6f7bcdc2c9 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/__.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 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.entities; + +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName; + +public class __ { + + protected __() { + + } + + public static <A> DSLQueryBuilder<A, A> identity() { + return new DSLQueryBuilder<>(); + } + + public static <A> DSLQueryBuilder<A, A> start(Start node) { + return new DSLQueryBuilder<>(node); + } + + public static DSLQueryBuilder<DSLStartNode, DSLStartNode> node(GraphInventoryObjectName name) { + + return __.<DSLStartNode>start(new DSLStartNode(name)); + } + + public static DSLQueryBuilder<DSLStartNode, DSLStartNode> node(GraphInventoryObjectName name, DSLNodeKey... key) { + return __.<DSLStartNode>start(new DSLStartNode(name, key)); + } + + public static DSLNodeKey key(String keyName, Object... value) { + return new DSLNodeKey(keyName, value); + } + + @SafeVarargs + public static final <A, B> DSLQueryBuilder<A, B> union(final DSLQueryBuilder<?, B>... traversal) { + + return __.<A>identity().union(traversal); + } + + public static <A> DSLQueryBuilder<A, A> where(DSLQueryBuilder<A, A> traversal) { + + return __.<A>identity().where(traversal); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/Depth.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/Depth.java new file mode 100644 index 0000000000..9f2ef42b50 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/Depth.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.entities.uri; + +public enum Depth { + ZERO("0"), ONE("1"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), ALL("all"); + + private final String depth; + + private Depth(String s) { + + this.depth = s; + } + + + @Override + public String toString() { + return this.depth; + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryPluralResourceUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryPluralResourceUri.java new file mode 100644 index 0000000000..8326c7b9df --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryPluralResourceUri.java @@ -0,0 +1,8 @@ +package org.onap.aaiclient.client.graphinventory.entities.uri; + +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectBase; + +public interface GraphInventoryPluralResourceUri<T extends GraphInventoryResourceUri<?, ?>, OT extends GraphInventoryObjectBase> + extends GraphInventoryResourceUri<T, OT> { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryResourceUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryResourceUri.java new file mode 100644 index 0000000000..d70b2d74f1 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryResourceUri.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.entities.uri; + +import org.onap.aaiclient.client.graphinventory.Format; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectBase; + +public interface GraphInventoryResourceUri<T extends GraphInventoryResourceUri<?, ?>, OT extends GraphInventoryObjectBase> + extends GraphInventoryUri<T, OT> { + + public T format(Format format); + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventorySingleResourceUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventorySingleResourceUri.java new file mode 100644 index 0000000000..ac0e4a0c94 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventorySingleResourceUri.java @@ -0,0 +1,16 @@ +package org.onap.aaiclient.client.graphinventory.entities.uri; + +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectPlurals; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType; + +public interface GraphInventorySingleResourceUri<T extends GraphInventorySingleResourceUri<?, ?, ?, ?>, P extends GraphInventoryPluralResourceUri<?, ?>, SingleObject extends GraphInventoryObjectType, PluralObject extends GraphInventoryObjectPlurals> + extends GraphInventoryResourceUri<T, SingleObject> { + + public T resourceVersion(String version); + + public T relationshipAPI(); + + public P relatedTo(PluralObject plural); + + public T relatedTo(SingleObject type, String... values); +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryUri.java new file mode 100644 index 0000000000..414bf3b071 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/GraphInventoryUri.java @@ -0,0 +1,81 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.entities.uri; + +import java.io.Serializable; +import java.net.URI; +import java.util.Map; +import java.util.regex.Pattern; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectBase; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType; + +public interface GraphInventoryUri<T extends GraphInventoryUri<?, ?>, OT extends GraphInventoryObjectBase> + extends Serializable { + + public URI build(); + + /** + * By default GraphInventory enforces a depth of 1. Some objects can be told to retrieve objects nested beneath them + * by increasing this number. + * + * You can use 0 to restrict the returned information to only the object you requested You can use all to retrieve + * all nested objects (this should only be used if you really need a massive amount of information and are caching + * the retrieval) + * + * @param depth + * @return + */ + public T depth(Depth depth); + + /** + * Makes client only return object fields, no relationships + * + * @return + */ + public T nodesOnly(boolean nodesOnly); + + public T queryParam(String name, String... values); + + public T replaceQueryParam(String name, String... values); + + public T resultIndex(int index); + + public T resultSize(int size); + + public T limit(int size); + + public T clone(); + + /** + * returns all key values of the URI as a map. Key names can be found in {@link GraphInventoryObjectType} + * + * @return + */ + public Map<String, String> getURIKeys(); + + public OT getObjectType(); + + public boolean equals(Object o); + + public int hashCode(); + + public Pattern getPrefixPattern(); +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/HttpAwareUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/HttpAwareUri.java new file mode 100644 index 0000000000..ab46f24464 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/HttpAwareUri.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.entities.uri; + +import java.net.URI; +import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri; + +public interface HttpAwareUri extends AAIResourceUri { + + + public URI buildNoNetwork(); + + public URI locateAndBuild(); +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimpleBaseUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimpleBaseUri.java new file mode 100644 index 0000000000..b668f465e9 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimpleBaseUri.java @@ -0,0 +1,237 @@ +package org.onap.aaiclient.client.graphinventory.entities.uri; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.ws.rs.core.UriBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.onap.aaiclient.client.graphinventory.Format; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectBase; +import org.onap.aaiclient.client.graphinventory.entities.uri.parsers.UriParser; +import org.onap.aaiclient.client.graphinventory.entities.uri.parsers.UriParserSpringImpl; +import org.onap.aaiclient.client.graphinventory.exceptions.IncorrectNumberOfUriKeys; +import org.springframework.web.util.UriUtils; + +public abstract class SimpleBaseUri<T extends GraphInventoryResourceUri<?, ?>, Parent extends GraphInventorySingleResourceUri<?, ?, ?, ?>, S extends GraphInventoryObjectBase> + implements GraphInventoryResourceUri<T, S> { + + private static final long serialVersionUID = -1011069933894179423L; + protected transient UriBuilder internalURI; + protected static final String relationshipAPI = "/relationship-list/relationship"; + protected static final String relatedTo = "/related-to"; + protected Object[] values; + protected final S type; + protected final Parent parentUri; + protected final Map<String, Set<String>> queryParams = new HashMap<>(); + + protected SimpleBaseUri(S type, Object... values) { + this.type = type; + this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); + this.values = values; + this.parentUri = null; + validateValuesSize(this.getTemplate(type), values); + } + + protected SimpleBaseUri(S type, URI uri) { + if (!type.passThrough()) { + this.type = type; + this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); + this.values = + this.getURIKeys(uri.getRawPath().replaceAll(getPrefixPattern().toString(), "")).values().toArray(); + this.parentUri = null; + } else { + this.type = type; + this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll(getPrefixPattern().toString(), "")); + this.values = new Object[0]; + this.parentUri = null; + } + + } + + protected SimpleBaseUri(S type, UriBuilder builder, Object... values) { + this.internalURI = builder; + this.values = values; + this.type = type; + this.parentUri = null; + + } + + protected SimpleBaseUri(Parent parentUri, S childType, Object... childValues) { + this.type = childType; + this.internalURI = UriBuilder.fromUri(type.partialUri()); + this.values = childValues; + this.parentUri = parentUri; + + validateValuesSize(childType.partialUri(), values); + } + + protected SimpleBaseUri(SimpleBaseUri<T, Parent, S> copy) { + this.type = copy.type; + this.internalURI = copy.internalURI.clone(); + this.values = copy.values.clone(); + if (copy.parentUri != null) { + this.parentUri = (Parent) copy.parentUri.clone(); + } else { + this.parentUri = null; + } + } + + protected void setInternalURI(UriBuilder builder) { + this.internalURI = builder; + } + + @Override + public T queryParam(String name, String... values) { + this.internalURI = internalURI.queryParam(name, values); + if (queryParams.containsKey(name)) { + queryParams.get(name).addAll(Arrays.asList(values)); + } else { + queryParams.put(name, Stream.of(values).collect(Collectors.toSet())); + } + return (T) this; + } + + @Override + public T replaceQueryParam(String name, String... values) { + this.internalURI = internalURI.replaceQueryParam(name, values); + queryParams.put(name, Stream.of(values).collect(Collectors.toSet())); + return (T) this; + } + + @Override + public T resultIndex(int index) { + this.internalURI = internalURI.replaceQueryParam("resultIndex", index); + return (T) this; + } + + @Override + public T resultSize(int size) { + this.internalURI = internalURI.replaceQueryParam("resultSize", size); + return (T) this; + } + + @Override + public T limit(int size) { + this.resultIndex(0).resultSize(size); + return (T) this; + } + + @Override + public URI build() { + return build(this.values); + } + + protected URI build(Object... values) { + + // This is a workaround because resteasy does not encode URIs correctly + final String[] encoded = new String[values.length]; + for (int i = 0; i < values.length; i++) { + encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString()); + } + if (this.parentUri != null) { + return UriBuilder + .fromUri(this.parentUri.build().toString() + internalURI.buildFromEncoded(encoded).toString()) + .build(); + } else { + return internalURI.buildFromEncoded(encoded); + } + } + + @Override + public Map<String, String> getURIKeys() { + return this.getURIKeys(this.build().toString()); + } + + protected Map<String, String> getURIKeys(String uri) { + UriParser parser; + if (!("".equals(this.getTemplate(type)))) { + parser = new UriParserSpringImpl(this.getTemplate(type)); + } else { + return new HashMap<>(); + } + + + return parser.parse(uri); + } + + @Override + public abstract T clone(); + + @Override + public S getObjectType() { + return this.type; + } + + @Override + public boolean equals(Object o) { + if (o != null) { + return this.toString().equals(o.toString()); + } + return false; + } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(this.toString()).toHashCode(); + } + + + @Override + public T depth(Depth depth) { + this.internalURI.replaceQueryParam("depth", depth.toString()); + return (T) this; + } + + @Override + public T nodesOnly(boolean nodesOnly) { + if (nodesOnly) { + this.internalURI.replaceQueryParam("nodes-only", ""); + } + return (T) this; + } + + @Override + public T format(Format format) { + this.internalURI.replaceQueryParam("format", format); + return (T) this; + } + + public void validateValuesSize(String template, Object... values) { + UriParser parser = new UriParserSpringImpl(template); + Set<String> variables = parser.getVariables(); + if (variables.size() != values.length) { + throw new IncorrectNumberOfUriKeys(String.format("Expected %s variables: %s", variables.size(), variables)); + } + } + + protected String getTemplate(GraphInventoryObjectBase type) { + return type.uriTemplate(); + } + + private void writeObject(ObjectOutputStream oos) throws IOException { + oos.defaultWriteObject(); + oos.writeUTF(this.internalURI.toTemplate()); + } + + private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { + ois.defaultReadObject(); + String uri = ois.readUTF(); + this.setInternalURI(UriBuilder.fromUri(uri)); + } + + @Override + public String toString() { + return new ToStringBuilder(null, ToStringStyle.NO_CLASS_NAME_STYLE).append("type", type) + .append("parentUri", parentUri).append("values", values).append("queryParams", queryParams).toString(); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimplePluralUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimplePluralUri.java new file mode 100644 index 0000000000..d76661526c --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimplePluralUri.java @@ -0,0 +1,77 @@ +/*- + * ============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.entities.uri; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectPlurals; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType; + +public abstract class SimplePluralUri<T extends GraphInventoryPluralResourceUri<?, ?>, Parent extends GraphInventorySingleResourceUri<?, ?, ?, ?>, PT extends GraphInventoryObjectPlurals, OT extends GraphInventoryObjectType> + extends SimpleBaseUri<T, Parent, PT> implements GraphInventoryPluralResourceUri<T, PT>, Serializable { + + private static final long serialVersionUID = -337701171277616439L; + + protected final PT pluralType; + + protected SimplePluralUri(PT type, UriBuilder builder, Object... values) { + super(type, builder, values); + this.pluralType = type; + } + + protected SimplePluralUri(PT type) { + super(type, new Object[0]); + this.pluralType = type; + } + + protected SimplePluralUri(PT type, Object... values) { + super(type, values); + this.pluralType = type; + } + + protected SimplePluralUri(Parent parentUri, PT childType) { + super(parentUri, childType, new Object[0]); + this.pluralType = childType; + } + + public SimplePluralUri(SimplePluralUri<T, Parent, PT, OT> copy) { + super(copy); + this.pluralType = copy.pluralType; + } + + protected void setInternalURI(UriBuilder builder) { + this.internalURI = builder; + } + + private void writeObject(ObjectOutputStream oos) throws IOException { + oos.defaultWriteObject(); + oos.writeUTF(this.internalURI.toTemplate()); + } + + private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { + ois.defaultReadObject(); + String uri = ois.readUTF(); + this.setInternalURI(UriBuilder.fromUri(uri)); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimpleUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimpleUri.java new file mode 100644 index 0000000000..e6a162bfda --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/SimpleUri.java @@ -0,0 +1,83 @@ +/*- + * ============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.entities.uri; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.URI; +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectPlurals; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType; + +public abstract class SimpleUri<T extends GraphInventorySingleResourceUri<?, ?, ?, ?>, PT extends GraphInventoryPluralResourceUri<?, ?>, S extends GraphInventoryObjectType, P extends GraphInventoryObjectPlurals> + extends SimpleBaseUri<T, T, S> implements GraphInventorySingleResourceUri<T, PT, S, P> { + + private static final long serialVersionUID = -337701171277616439L; + protected static final String relationshipAPI = "/relationship-list/relationship"; + protected static final String relatedTo = "/related-to"; + + protected SimpleUri(S type, Object... values) { + super(type, values); + } + + protected SimpleUri(S type, URI uri) { + super(type, uri); + + } + + protected SimpleUri(S type, UriBuilder builder, Object... values) { + super(type, builder, values); + + } + + protected SimpleUri(T parentUri, S childType, Object... childValues) { + super(parentUri, childType, childValues); + } + + protected SimpleUri(SimpleBaseUri<T, T, S> copy) { + super(copy); + } + + @Override + public T resourceVersion(String version) { + this.internalURI = internalURI.replaceQueryParam("resource-version", version); + return (T) this; + } + + @Override + public T relationshipAPI() { + this.internalURI = internalURI.path(relationshipAPI); + return (T) this; + } + + private void writeObject(ObjectOutputStream oos) throws IOException { + oos.defaultWriteObject(); + oos.writeUTF(this.internalURI.toTemplate()); + } + + private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { + ois.defaultReadObject(); + String uri = ois.readUTF(); + this.setInternalURI(UriBuilder.fromUri(uri)); + } + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParser.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParser.java new file mode 100644 index 0000000000..24a20240e0 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParser.java @@ -0,0 +1,30 @@ +/*- + * ============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.entities.uri.parsers; + +import java.util.Map; +import java.util.Set; + +public interface UriParser { + public Set<String> getVariables(); + + public Map<String, String> parse(final String uri); +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParserSpringImpl.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParserSpringImpl.java new file mode 100644 index 0000000000..db13ef742a --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParserSpringImpl.java @@ -0,0 +1,63 @@ +/*- + * ============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.entities.uri.parsers; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import org.springframework.web.util.UriTemplate; +import org.springframework.web.util.UriUtils; + +public class UriParserSpringImpl implements UriParser { + + private final UriTemplate uriTemplate; + + public UriParserSpringImpl(final String template) { + this.uriTemplate = new UriTemplate(template); + } + + @Override + public Map<String, String> parse(final String uri) { + final boolean match = this.uriTemplate.matches(uri); + if (!match) { + return new LinkedHashMap<>(); + } + return Collections.unmodifiableMap(decodeParams(this.uriTemplate.match(uri))); + } + + @Override + public Set<String> getVariables() { + return Collections.unmodifiableSet(new LinkedHashSet<String>(this.uriTemplate.getVariableNames())); + } + + protected Map<String, String> decodeParams(Map<String, String> map) { + final Map<String, String> result = new LinkedHashMap<>(); + + for (Entry<String, String> entry : map.entrySet()) { + result.put(entry.getKey(), UriUtils.decode(entry.getValue(), "UTF-8")); + } + + return result; + } +} |