2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.edges;
25 import org.onap.aai.edges.enums.AAIDirection;
26 import org.onap.aai.edges.enums.EdgeField;
27 import org.onap.aai.edges.enums.EdgeProperty;
28 import org.onap.aai.edges.enums.EdgeType;
29 import org.onap.aai.setup.Version;
31 import com.jayway.jsonpath.Filter;
32 import com.jayway.jsonpath.Predicate;
34 import static com.jayway.jsonpath.Filter.filter;
36 import java.util.ArrayList;
37 import java.util.List;
39 import static com.jayway.jsonpath.Criteria.where;
42 * For querying the edge rules schema (not the database)
45 public class EdgeRuleQuery {
46 private Filter filter;
51 private EdgeType type;
53 public static class Builder {
57 //optional - null will translate to any value of the param
58 private String nodeB = null;
59 private String label = null;
60 private EdgeType type = null;
61 private Version version = Version.getLatest(); //default
63 public Builder(String nodeA) {
67 public Builder(String nodeA, String nodeB) {
72 private String getFirstNodeType() {
76 private String getSecondNodeType() {
80 public Builder label(String label) {
85 private String getLabel() {
89 public Builder edgeType(EdgeType type) {
94 private EdgeType getEdgeType() {
98 public Builder version(Version version) {
99 this.version = version;
102 private Version getVersion() {
106 public EdgeRuleQuery build() {
107 return new EdgeRuleQuery(this);
111 private EdgeRuleQuery(Builder builder) {
112 this.v = builder.getVersion();
113 this.nodeA = builder.getFirstNodeType();
114 this.nodeB = builder.getSecondNodeType();
115 this.label = builder.getLabel();
116 this.type = builder.getEdgeType();
118 //will cover from A to B case
119 List<Predicate> criteriaFromTo = new ArrayList<>();
120 criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), builder.getSecondNodeType()));
121 //will cover from B to A case - must be separate bc jsonpath won't let me OR predicates >:C
122 List<Predicate> criteriaToFrom = new ArrayList<>();
123 criteriaToFrom.add(buildToFromPart(builder.getSecondNodeType(), builder.getFirstNodeType()));
127 if (builder.getLabel() != null) {
128 Predicate labelPred = addLabel(builder.getLabel());
129 criteriaFromTo.add(labelPred);
130 criteriaToFrom.add(labelPred);
133 if (builder.getEdgeType() != null) {
134 Predicate typePred = addType(builder.getEdgeType());
135 criteriaFromTo.add(typePred);
136 criteriaToFrom.add(typePred);
141 this.filter = filter(criteriaFromTo).or(filter(criteriaToFrom));
144 private Predicate buildToFromPart(String from, String to) {
145 if (from == null && to == null) { //shouldn't ever happen though
146 throw new IllegalStateException("must have at least one node defined");
152 p = where(EdgeField.FROM.toString()).is(from);
153 } else if (from == null) {
154 p = where(EdgeField.TO.toString()).is(to);
156 p = where(EdgeField.FROM.toString()).is(from).and(EdgeField.TO.toString()).is(to);
162 private Predicate addLabel(String label) {
163 return where(EdgeField.LABEL.toString()).is(label);
166 private Predicate addType(EdgeType type) {
167 if (type == EdgeType.COUSIN) {
168 return where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString());
169 } else { //equals TREE
170 return where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString());
175 * Provides the JsonPath filter for actually querying the edge rule schema files
178 public Filter getFilter() {
183 * So the Ingestor knows which version of the rules to search
184 * @return the Version
186 public Version getVersion() {
191 public String toString() {
192 StringBuilder sb = new StringBuilder();
194 sb.append("EdgeRuleQuery with filter params node type: ").append(nodeA);
197 sb.append(", node type: ").append(nodeB);
201 sb.append(", label: ").append(label);
204 sb.append(", type: ");
206 sb.append(type.toString());
211 sb.append(", for version: ").append(v.toString()).append(".");
212 return sb.toString();