2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.edges;
22 import org.onap.aai.edges.enums.AAIDirection;
23 import org.onap.aai.edges.enums.EdgeField;
24 import org.onap.aai.edges.enums.EdgeProperty;
25 import org.onap.aai.edges.enums.EdgeType;
26 import org.onap.aai.setup.Version;
28 import com.jayway.jsonpath.Filter;
29 import com.jayway.jsonpath.Predicate;
31 import static com.jayway.jsonpath.Filter.filter;
33 import java.util.ArrayList;
34 import java.util.List;
36 import static com.jayway.jsonpath.Criteria.where;
39 * For querying the edge rules schema (not the database)
42 public class EdgeRuleQuery {
43 private Filter filter;
48 private EdgeType type;
50 public static class Builder {
54 //optional - null will translate to any value of the param
55 private String nodeB = null;
56 private String label = null;
57 private EdgeType type = null;
58 private Version version = Version.getLatest(); //default
60 public Builder(String nodeA) {
64 public Builder(String nodeA, String nodeB) {
69 private String getFirstNodeType() {
73 private String getSecondNodeType() {
77 public Builder label(String label) {
82 private String getLabel() {
86 public Builder edgeType(EdgeType type) {
91 private EdgeType getEdgeType() {
95 public Builder version(Version version) {
96 this.version = version;
99 private Version getVersion() {
103 public EdgeRuleQuery build() {
104 return new EdgeRuleQuery(this);
108 private EdgeRuleQuery(Builder builder) {
109 this.v = builder.getVersion();
110 this.nodeA = builder.getFirstNodeType();
111 this.nodeB = builder.getSecondNodeType();
112 this.label = builder.getLabel();
113 this.type = builder.getEdgeType();
115 //will cover from A to B case
116 List<Predicate> criteriaFromTo = new ArrayList<>();
117 criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), builder.getSecondNodeType()));
118 //will cover from B to A case - must be separate bc jsonpath won't let me OR predicates >:C
119 List<Predicate> criteriaToFrom = new ArrayList<>();
120 criteriaToFrom.add(buildToFromPart(builder.getSecondNodeType(), builder.getFirstNodeType()));
124 if (builder.getLabel() != null) {
125 Predicate labelPred = addLabel(builder.getLabel());
126 criteriaFromTo.add(labelPred);
127 criteriaToFrom.add(labelPred);
130 if (builder.getEdgeType() != null) {
131 Predicate typePred = addType(builder.getEdgeType());
132 criteriaFromTo.add(typePred);
133 criteriaToFrom.add(typePred);
138 this.filter = filter(criteriaFromTo).or(filter(criteriaToFrom));
141 private Predicate buildToFromPart(String from, String to) {
142 if (from == null && to == null) { //shouldn't ever happen though
143 throw new IllegalStateException("must have at least one node defined");
149 p = where(EdgeField.FROM.toString()).is(from);
150 } else if (from == null) {
151 p = where(EdgeField.TO.toString()).is(to);
153 p = where(EdgeField.FROM.toString()).is(from).and(EdgeField.TO.toString()).is(to);
159 private Predicate addLabel(String label) {
160 return where(EdgeField.LABEL.toString()).is(label);
163 private Predicate addType(EdgeType type) {
164 if (type == EdgeType.COUSIN) {
165 return where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString());
166 } else { //equals TREE
167 return where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString());
172 * Provides the JsonPath filter for actually querying the edge rule schema files
175 public Filter getFilter() {
180 * So the Ingestor knows which version of the rules to search
181 * @return the Version
183 public Version getVersion() {
188 public String toString() {
189 StringBuilder sb = new StringBuilder();
191 sb.append("EdgeRuleQuery with filter params node type: ").append(nodeA);
194 sb.append(", node type: ").append(nodeB);
198 sb.append(", label: ").append(label);
201 sb.append(", type: ");
203 sb.append(type.toString());
208 sb.append(", for version: ").append(v.toString()).append(".");
209 return sb.toString();