2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.aai.edges;
25 import org.apache.commons.lang.StringUtils;
26 import org.onap.aai.edges.enums.AAIDirection;
27 import org.onap.aai.edges.enums.EdgeField;
28 import org.onap.aai.edges.enums.EdgeProperty;
29 import org.onap.aai.edges.enums.EdgeType;
30 import org.onap.aai.setup.SchemaVersion;
32 import com.jayway.jsonpath.Filter;
33 import com.jayway.jsonpath.Predicate;
35 import static com.jayway.jsonpath.Filter.filter;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.Optional;
42 import static com.jayway.jsonpath.Criteria.where;
45 * For querying the edge rules schema (not the database)
48 public class EdgeRuleQuery {
49 private Filter filter;
50 private Optional<SchemaVersion> v;
54 private AAIDirection direction;
55 private EdgeType type;
56 private boolean isPrivate;
58 public static class Builder {
59 private static final String TO_ONLY = "ToOnly";
61 private static final String FROM_ONLY = "FromOnly";
66 //optional - null will translate to any value of the param
67 private String nodeB = null;
68 private String label = null;
69 private EdgeType type = null;
70 private AAIDirection direction = null;
71 private boolean isPrivate = false;
72 private SchemaVersion version = null;
74 public Builder(String nodeA) {
78 public Builder(String nodeA, String nodeB) {
83 private String getFirstNodeType() {
86 public Builder fromOnly() {
87 this.nodeB = FROM_ONLY;
91 private String getSecondNodeType() {
95 public Builder to(String nodeB){
100 public Builder toOnly() {
101 //Allows this to be used with single parameter constructor Builder(String nodeA)
102 if(StringUtils.isEmpty(this.nodeB) && StringUtils.isNotEmpty(this.nodeA) ) {
103 this.nodeB=this.nodeA;
105 this.nodeA = TO_ONLY;
109 public Builder label(String label) {
114 private String getLabel() {
118 public Builder edgeType(EdgeType type) {
123 private EdgeType getEdgeType() {
128 public Builder direction(AAIDirection direction) {
129 this.direction = direction;
133 private AAIDirection getDirection() {
137 public Builder version(SchemaVersion version) {
138 this.version = version;
142 public Builder setPrivate(boolean isPrivate){
143 this.isPrivate = isPrivate;
147 public boolean isPrivate(){
151 private Optional<SchemaVersion> getSchemaVersion() {
152 return Optional.ofNullable(version);
155 public EdgeRuleQuery build() {
156 return new EdgeRuleQuery(this);
160 private EdgeRuleQuery(Builder builder) {
161 this.v = builder.getSchemaVersion();
162 this.nodeA = builder.getFirstNodeType();
163 this.nodeB = builder.getSecondNodeType();
164 this.label = builder.getLabel();
165 this.type = builder.getEdgeType();
166 this.direction = builder.getDirection();
167 this.isPrivate = builder.isPrivate();
169 //will cover from A to B case
170 List<Predicate> criteriaFromTo = new ArrayList<>();
171 //Special logic to allow for A to B case only
172 if(("FromOnly").equals(builder.getSecondNodeType())) {
173 criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), null));
175 criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), builder.getSecondNodeType()));
177 //will cover from B to A case - must be separate bc jsonpath won't let me OR predicates >:C
178 List<Predicate> criteriaToFrom = new ArrayList<>();
179 //Special logic to allow for B to A case only
180 if(("ToOnly").equals(builder.getFirstNodeType())) {
181 criteriaToFrom.add(buildToFromPart(null, builder.getSecondNodeType()));
183 criteriaToFrom.add(buildToFromPart(builder.getSecondNodeType(), builder.getFirstNodeType()));
185 if (builder.getLabel() != null) {
186 Predicate labelPred = addLabel(builder.getLabel());
187 criteriaFromTo.add(labelPred);
188 criteriaToFrom.add(labelPred);
191 if (builder.getEdgeType() != null) {
192 Predicate typePred = addType(builder.getEdgeType());
193 criteriaFromTo.add(typePred);
194 criteriaToFrom.add(typePred);
196 Predicate privatePredicate = where("private").is(String.valueOf(isPrivate));
199 criteriaFromTo.add(privatePredicate);
200 criteriaToFrom.add(privatePredicate);
203 if (builder.getDirection() != null) {
204 Predicate directionPred = addDirection(builder.getDirection());
205 criteriaFromTo.add(directionPred);
206 criteriaToFrom.add(directionPred);
208 if(("ToOnly").equals(builder.getFirstNodeType())) {
209 this.filter = filter(criteriaToFrom);
210 } else if(("FromOnly").equals(builder.getSecondNodeType())) {
211 this.filter = filter(criteriaFromTo);
213 this.filter = filter(criteriaFromTo).or(filter(criteriaToFrom));
217 private Predicate buildToFromPart(String from, String to) {
218 if (from == null && to == null) { //shouldn't ever happen though
219 throw new IllegalStateException("must have at least one node defined");
225 p = where(EdgeField.FROM.toString()).is(from);
226 } else if (from == null) {
227 p = where(EdgeField.TO.toString()).is(to);
229 p = where(EdgeField.FROM.toString()).is(from).and(EdgeField.TO.toString()).is(to);
235 private Predicate addLabel(String label) {
236 return where(EdgeField.LABEL.toString()).is(label);
239 private Predicate addType(EdgeType type) {
240 if (type == EdgeType.COUSIN) {
241 return where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString());
242 } else { //equals TREE
243 return where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString());
247 private Predicate addDirection(AAIDirection direction) {
248 if (direction == AAIDirection.OUT) {
249 return where(EdgeField.DIRECTION.toString()).in(AAIDirection.OUT.toString(), AAIDirection.BOTH.toString());
250 } else if (direction == AAIDirection.IN) {
251 return where(EdgeField.DIRECTION.toString()).in(AAIDirection.IN.toString(), AAIDirection.BOTH.toString());
252 } else if (direction == AAIDirection.BOTH) {
253 return where(EdgeField.DIRECTION.toString()).is(AAIDirection.BOTH.toString());
254 } else if (direction == AAIDirection.NONE) {
255 return where(EdgeField.DIRECTION.toString()).is(AAIDirection.NONE.toString());
257 return where(EdgeField.DIRECTION.toString()).is(AAIDirection.NONE.toString());
261 * Provides the JsonPath filter for actually querying the edge rule schema files
264 public Filter getFilter() {
269 * Gets the first node type given for the query.
271 * ie, If you called Builder(A,B) this would return A,
272 * if you called Builder(B,A), it would return B,
273 * if you called Builder(A), it would return A.
275 * This is to maintain backwards compatibility with the
276 * EdgeRules API which flipped the direction of
277 * the result EdgeRule to match the input directionality.
279 * @return String first node type of the query
281 public String getFromType() {
286 * So the Ingestor knows which version of the rules to search
287 * @return the Version
289 public Optional<SchemaVersion> getVersion() {
294 public String toString() {
295 StringBuilder sb = new StringBuilder();
297 sb.append("EdgeRuleQuery with filter params node type: ").append(nodeA);
300 sb.append(", node type: ").append(nodeB);
304 sb.append(", label: ").append(label);
307 sb.append(", type: ");
309 sb.append(type.toString());
314 sb.append(", isPrivate: ");
315 sb.append(isPrivate);
318 sb.append(", for version: ").append(v.get().toString()).append(".");
320 return sb.toString();
324 public boolean equals(Object o) {
325 if (this == o) return true;
326 if (o == null || getClass() != o.getClass()) return false;
327 EdgeRuleQuery ruleQuery = (EdgeRuleQuery) o;
328 return isPrivate == ruleQuery.isPrivate &&
329 Objects.equals(v, ruleQuery.v) &&
330 Objects.equals(nodeA, ruleQuery.nodeA) &&
331 Objects.equals(nodeB, ruleQuery.nodeB) &&
332 Objects.equals(label, ruleQuery.label) &&
333 direction == ruleQuery.direction &&
334 type == ruleQuery.type;
338 public int hashCode() {
340 return Objects.hash(v.get(), nodeA, nodeB, label, direction, type, isPrivate);
342 return Objects.hash(nodeA, nodeB, label, direction, type, isPrivate);