diff options
author | Steve Smokowski <ss835w@att.com> | 2020-03-19 13:04:24 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-03-19 13:04:24 +0000 |
commit | 773d3f0ef4338aba93bb613e92362412864ba251 (patch) | |
tree | 54fb6c65340572e45700b78c34ffc2c021aad047 /common/src/main/java | |
parent | 79830870fc1cfc3ae65a6c869eb7ece195733350 (diff) | |
parent | ae4c35d6bd77a62c5a41a6d61ad868e8ee8453f5 (diff) |
Merge "reworked dsl client code to check for outputs"
Diffstat (limited to 'common/src/main/java')
11 files changed, 155 insertions, 62 deletions
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/DSLNodeBase.java index 7ee5bd328e..5802e9c4e8 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/DSLNodeBase.java @@ -26,41 +26,51 @@ import java.util.List; import org.onap.so.client.aai.entities.QueryStep; import org.onap.so.client.graphinventory.GraphInventoryObjectName; -public class DSLNode implements QueryStep { +public abstract class DSLNodeBase<T extends DSLNodeBase<?>> implements QueryStep { - private final String nodeName; - private final List<DSLNodeKey> nodeKeys; - private final StringBuilder query = new StringBuilder(); - private boolean output = false; + protected final String nodeName; + protected final List<DSLNodeKey> nodeKeys; + protected final StringBuilder query; + protected boolean output = false; - public DSLNode() { + public DSLNodeBase() { this.nodeName = ""; this.nodeKeys = new ArrayList<>(); + this.query = new StringBuilder(); } - public DSLNode(GraphInventoryObjectName name) { + public DSLNodeBase(GraphInventoryObjectName name) { this.nodeName = name.typeName(); this.nodeKeys = new ArrayList<>(); + this.query = new StringBuilder(); query.append(nodeName); } - public DSLNode(GraphInventoryObjectName name, DSLNodeKey... key) { + public DSLNodeBase(GraphInventoryObjectName name, DSLNodeKey... key) { this.nodeName = name.typeName(); this.nodeKeys = Arrays.asList(key); + this.query = new StringBuilder(); query.append(nodeName); } - public DSLNode output() { + 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 this; + return new DSLOutputNode(this); } - public DSLNode and(DSLNodeKey... key) { + public T and(DSLNodeKey... key) { this.nodeKeys.addAll(Arrays.asList(key)); - return this; + return (T) this; } @Override diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLOutputNode.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLOutputNode.java new file mode 100644 index 0000000000..d799795650 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLOutputNode.java @@ -0,0 +1,8 @@ +package org.onap.so.client.graphinventory.entities; + +public class DSLOutputNode extends DSLNodeBase<DSLOutputNode> implements Output { + + public DSLOutputNode(DSLNodeBase<?> copy) { + super(copy); + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java index 3056c9ca80..1fa79a714c 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java @@ -31,17 +31,11 @@ public class DSLQuery { } - public DSLQuery(String dsl) { - this.dsl = dsl; + public DSLQuery(DSLTraversal<? extends Output> dsl) { + this.dsl = dsl.get(); } public String getDsl() { return dsl; } - - public void setDsl(String dsl) { - this.dsl = dsl; - } - - } 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 540472a88d..7ff6e2ed22 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 @@ -27,35 +27,31 @@ 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 org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.google.common.base.Joiner; - -public class DSLQueryBuilder<S, E> implements QueryStep { +public class DSLQueryBuilder<S, E> { private List<QueryStep> steps = new ArrayList<>(); private String suffix = ""; - private static final Logger logger = LoggerFactory.getLogger(DSLQueryBuilder.class); - public DSLQueryBuilder() { + protected DSLQueryBuilder() { } - public DSLQueryBuilder(DSLNode node) { + protected DSLQueryBuilder(QueryStep node) { steps.add(node); } - public DSLQueryBuilder<S, DSLNode> node(DSLNode node) { + public <T> DSLQueryBuilder<S, DSLNodeBase<?>> node(DSLNodeBase<?> node) { steps.add(node); - return (DSLQueryBuilder<S, DSLNode>) this; + return (DSLQueryBuilder<S, DSLNodeBase<?>>) this; } - public DSLQueryBuilder<S, E> output() { + public DSLQueryBuilder<S, Node> output() { Object obj = steps.get(steps.size() - 1); - if (obj instanceof DSLNode) { - ((DSLNode) steps.get(steps.size() - 1)).output(); + 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()) { @@ -63,27 +59,28 @@ public class DSLQueryBuilder<S, E> implements QueryStep { Object o; try { o = f.get(obj); - if (o instanceof DSLQueryBuilder && ((DSLQueryBuilder) o).steps.get(0) instanceof DSLNode) { - ((DSLNode) ((DSLQueryBuilder) o).steps.get(0)).output(); + if (o instanceof DSLQueryBuilder && ((DSLQueryBuilder) o).steps.get(0) instanceof DSLNodeBase) { + ((DSLNodeBase) ((DSLQueryBuilder) o).steps.get(0)).output(); } } catch (IllegalArgumentException | IllegalAccessException e) { - logger.error("Exception occured", e); } f.setAccessible(false); break; } } - return this; + return (DSLQueryBuilder<S, Node>) this; } - public <E2> DSLQueryBuilder<S, E2> union(final DSLQueryBuilder<?, E2>... union) { + @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.build()).collect(Collectors.toList()))) + query.append("> [ ") + .append(Joiner.on(", ") + .join(unions.stream().map(item -> item.compile()).collect(Collectors.toList()))) .append(" ]"); return query.toString(); }); @@ -95,7 +92,7 @@ public class DSLQueryBuilder<S, E> implements QueryStep { steps.add(() -> { StringBuilder query = new StringBuilder(); - query.append(where.build()).append(")"); + query.append(where.compile()).append(")"); String result = query.toString(); if (!result.startsWith(">")) { result = "> " + result; @@ -105,22 +102,22 @@ public class DSLQueryBuilder<S, E> implements QueryStep { return this; } - public DSLQueryBuilder<S, E> to(DSLQueryBuilder<?, ?> to) { + public <E2> DSLQueryBuilder<S, E2> to(DSLQueryBuilder<?, E2> to) { steps.add(() -> { StringBuilder query = new StringBuilder(); - query.append("> ").append(to.build()); + query.append("> ").append(to.compile()); return query.toString(); }); - return this; + return (DSLQueryBuilder<S, E2>) this; } public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) { - return to(__.node(name)); + return (DSLQueryBuilder<S, E>) to(__.node(name)); } public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) { - return to(__.node(name, key)); + return (DSLQueryBuilder<S, E>) to(__.node(name, key)); } public DSLQueryBuilder<S, E> limit(int limit) { @@ -128,24 +125,19 @@ public class DSLQueryBuilder<S, E> implements QueryStep { return this; } - @Override - public String build() { - return compile(); + public DSLTraversal<E> build() { + return new DSLTraversal<>(compile()); } @Override public String toString() { - return build(); + return build().get(); } @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 o.toString().equals(toString()); } return false; } @@ -153,11 +145,11 @@ public class DSLQueryBuilder<S, E> implements QueryStep { @Override public int hashCode() { - return build().hashCode(); + return compile().hashCode(); } private String compile() { - return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix; + return String.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/DSLStartNode.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLStartNode.java new file mode 100644 index 0000000000..fb0772726f --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLStartNode.java @@ -0,0 +1,19 @@ +package org.onap.so.client.graphinventory.entities; + +import org.onap.so.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/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLTraversal.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLTraversal.java new file mode 100644 index 0000000000..40f80463b2 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLTraversal.java @@ -0,0 +1,33 @@ +package org.onap.so.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/common/src/main/java/org/onap/so/client/graphinventory/entities/Node.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/Node.java new file mode 100644 index 0000000000..cdaae7d91f --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/Node.java @@ -0,0 +1,5 @@ +package org.onap.so.client.graphinventory.entities; + +public interface Node extends Output { + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/Output.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/Output.java new file mode 100644 index 0000000000..06b8b65984 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/Output.java @@ -0,0 +1,7 @@ +package org.onap.so.client.graphinventory.entities; + +import org.onap.so.client.aai.entities.QueryStep; + +public interface Output extends QueryStep { + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/Start.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/Start.java new file mode 100644 index 0000000000..c649e36eca --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/Start.java @@ -0,0 +1,7 @@ +package org.onap.so.client.graphinventory.entities; + +import org.onap.so.client.aai.entities.QueryStep; + +public interface Start extends QueryStep { + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/TraversalBuilder.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/TraversalBuilder.java new file mode 100644 index 0000000000..0af29f78e6 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/TraversalBuilder.java @@ -0,0 +1,17 @@ +package org.onap.so.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/common/src/main/java/org/onap/so/client/graphinventory/entities/__.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/__.java index 87d4d84cac..ca0bd9e00d 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/__.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/__.java @@ -32,24 +32,25 @@ public class __ { return new DSLQueryBuilder<>(); } - public static <A> DSLQueryBuilder<A, A> start(DSLNode node) { + public static <A> DSLQueryBuilder<A, A> start(Start node) { return new DSLQueryBuilder<>(node); } - public static DSLQueryBuilder<DSLNode, DSLNode> node(GraphInventoryObjectName name) { + public static DSLQueryBuilder<DSLStartNode, DSLStartNode> node(GraphInventoryObjectName name) { - return __.<DSLNode>start(new DSLNode(name)); + return __.<DSLStartNode>start(new DSLStartNode(name)); } - public static DSLQueryBuilder<DSLNode, DSLNode> node(GraphInventoryObjectName name, DSLNodeKey... key) { - return __.<DSLNode>start(new DSLNode(name, key)); + 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); } - public static <A, B> DSLQueryBuilder<A, B> union(final DSLQueryBuilder<?, B>... traversal) { + @SafeVarargs + public static final <A, B> DSLQueryBuilder<A, B> union(final DSLQueryBuilder<?, B>... traversal) { return __.<A>identity().union(traversal); } |