From ae4c35d6bd77a62c5a41a6d61ad868e8ee8453f5 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max" Date: Tue, 17 Mar 2020 00:13:39 -0400 Subject: reworked dsl client code to check for outputs reworked dsl client code to check for outputs Issue-ID: SO-2744 Signed-off-by: Benjamin, Max (mb388a) Change-Id: Id57bc03c68ec1ae0b0b0797d3a5824026aa8133f --- .../so/client/graphinventory/entities/DSLNode.java | 78 ------------------- .../graphinventory/entities/DSLNodeBase.java | 88 ++++++++++++++++++++++ .../graphinventory/entities/DSLOutputNode.java | 8 ++ .../client/graphinventory/entities/DSLQuery.java | 10 +-- .../graphinventory/entities/DSLQueryBuilder.java | 64 +++++++--------- .../graphinventory/entities/DSLStartNode.java | 19 +++++ .../graphinventory/entities/DSLTraversal.java | 33 ++++++++ .../so/client/graphinventory/entities/Node.java | 5 ++ .../so/client/graphinventory/entities/Output.java | 7 ++ .../so/client/graphinventory/entities/Start.java | 7 ++ .../graphinventory/entities/TraversalBuilder.java | 17 +++++ .../onap/so/client/graphinventory/entities/__.java | 13 ++-- 12 files changed, 221 insertions(+), 128 deletions(-) delete mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeBase.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLOutputNode.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLStartNode.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLTraversal.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/Node.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/Output.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/Start.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/TraversalBuilder.java (limited to 'common/src/main/java/org/onap') 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 deleted file mode 100644 index 7ee5bd328e..0000000000 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java +++ /dev/null @@ -1,78 +0,0 @@ -/*- - * ============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.so.client.graphinventory.entities; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.onap.so.client.aai.entities.QueryStep; -import org.onap.so.client.graphinventory.GraphInventoryObjectName; - -public class DSLNode implements QueryStep { - - private final String nodeName; - private final List nodeKeys; - private final StringBuilder query = new StringBuilder(); - private boolean output = false; - - public DSLNode() { - this.nodeName = ""; - this.nodeKeys = new ArrayList<>(); - - } - - public DSLNode(GraphInventoryObjectName name) { - this.nodeName = name.typeName(); - this.nodeKeys = new ArrayList<>(); - query.append(nodeName); - } - - public DSLNode(GraphInventoryObjectName name, DSLNodeKey... key) { - this.nodeName = name.typeName(); - this.nodeKeys = Arrays.asList(key); - query.append(nodeName); - } - - public DSLNode output() { - this.output = true; - - return this; - } - - public DSLNode and(DSLNodeKey... key) { - this.nodeKeys.addAll(Arrays.asList(key)); - - return 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/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeBase.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeBase.java new file mode 100644 index 0000000000..5802e9c4e8 --- /dev/null +++ b/common/src/main/java/org/onap/so/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.so.client.graphinventory.entities; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.onap.so.client.aai.entities.QueryStep; +import org.onap.so.client.graphinventory.GraphInventoryObjectName; + +public abstract class DSLNodeBase> implements QueryStep { + + protected final String nodeName; + protected final List 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/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 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 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 implements QueryStep { +public class DSLQueryBuilder { private List 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 node(DSLNode node) { + public DSLQueryBuilder> node(DSLNodeBase node) { steps.add(node); - return (DSLQueryBuilder) this; + return (DSLQueryBuilder>) this; } - public DSLQueryBuilder output() { + public DSLQueryBuilder 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 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) this; } - public DSLQueryBuilder union(final DSLQueryBuilder... union) { + @SafeVarargs + public final DSLQueryBuilder union(final DSLQueryBuilder... union) { List> 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 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 implements QueryStep { return this; } - public DSLQueryBuilder to(DSLQueryBuilder to) { + public DSLQueryBuilder to(DSLQueryBuilder to) { steps.add(() -> { StringBuilder query = new StringBuilder(); - query.append("> ").append(to.build()); + query.append("> ").append(to.compile()); return query.toString(); }); - return this; + return (DSLQueryBuilder) this; } public DSLQueryBuilder to(GraphInventoryObjectName name) { - return to(__.node(name)); + return (DSLQueryBuilder) to(__.node(name)); } public DSLQueryBuilder to(GraphInventoryObjectName name, DSLNodeKey... key) { - return to(__.node(name, key)); + return (DSLQueryBuilder) to(__.node(name, key)); } public DSLQueryBuilder limit(int limit) { @@ -128,24 +125,19 @@ public class DSLQueryBuilder implements QueryStep { return this; } - @Override - public String build() { - return compile(); + public DSLTraversal 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 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 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 { + + 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 fragment(Start node) { + return new DSLQueryBuilder<>(node); + } + + public static DSLQueryBuilder 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 DSLQueryBuilder start(DSLNode node) { + public static DSLQueryBuilder start(Start node) { return new DSLQueryBuilder<>(node); } - public static DSLQueryBuilder node(GraphInventoryObjectName name) { + public static DSLQueryBuilder node(GraphInventoryObjectName name) { - return __.start(new DSLNode(name)); + return __.start(new DSLStartNode(name)); } - public static DSLQueryBuilder node(GraphInventoryObjectName name, DSLNodeKey... key) { - return __.start(new DSLNode(name, key)); + public static DSLQueryBuilder node(GraphInventoryObjectName name, DSLNodeKey... key) { + return __.start(new DSLStartNode(name, key)); } public static DSLNodeKey key(String keyName, Object... value) { return new DSLNodeKey(keyName, value); } - public static DSLQueryBuilder union(final DSLQueryBuilder... traversal) { + @SafeVarargs + public static final DSLQueryBuilder union(final DSLQueryBuilder... traversal) { return __.identity().union(traversal); } -- cgit 1.2.3-korg