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.Optional;
41 import static com.jayway.jsonpath.Criteria.where;
44 * For querying the edge rules schema (not the database)
47 public class EdgeRuleQuery {
48 private Filter filter;
49 private Optional<SchemaVersion> v;
53 private AAIDirection direction;
54 private EdgeType type;
55 private boolean isPrivate;
57 public static class Builder {
58 private static final String TO_ONLY = "ToOnly";
60 private static final String FROM_ONLY = "FromOnly";
65 //optional - null will translate to any value of the param
66 private String nodeB = null;
67 private String label = null;
68 private EdgeType type = null;
69 private AAIDirection direction = null;
70 private boolean isPrivate = false;
71 private SchemaVersion version = null;
73 public Builder(String nodeA) {
77 public Builder(String nodeA, String nodeB) {
82 private String getFirstNodeType() {
85 public Builder fromOnly() {
86 this.nodeB = FROM_ONLY;
90 private String getSecondNodeType() {
93 public Builder toOnly() {
94 //Allows this to be used with single parameter constructor Builder(String nodeA)
95 if(StringUtils.isEmpty(this.nodeB) && StringUtils.isNotEmpty(this.nodeA) ) {
96 this.nodeB=this.nodeA;
102 public Builder label(String label) {
107 private String getLabel() {
111 public Builder edgeType(EdgeType type) {
116 private EdgeType getEdgeType() {
121 public Builder direction(AAIDirection direction) {
122 this.direction = direction;
126 private AAIDirection getDirection() {
130 public Builder version(SchemaVersion version) {
131 this.version = version;
135 public Builder setPrivate(boolean isPrivate){
136 this.isPrivate = isPrivate;
140 public boolean isPrivate(){
144 private Optional<SchemaVersion> getSchemaVersion() {
145 return Optional.ofNullable(version);
148 public EdgeRuleQuery build() {
149 return new EdgeRuleQuery(this);
153 private EdgeRuleQuery(Builder builder) {
154 this.v = builder.getSchemaVersion();
155 this.nodeA = builder.getFirstNodeType();
156 this.nodeB = builder.getSecondNodeType();
157 this.label = builder.getLabel();
158 this.type = builder.getEdgeType();
159 this.direction = builder.getDirection();
160 this.isPrivate = builder.isPrivate();
162 //will cover from A to B case
163 List<Predicate> criteriaFromTo = new ArrayList<>();
164 //Special logic to allow for A to B case only
165 if(("FromOnly").equals(builder.getSecondNodeType())) {
166 criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), null));
168 criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), builder.getSecondNodeType()));
170 //will cover from B to A case - must be separate bc jsonpath won't let me OR predicates >:C
171 List<Predicate> criteriaToFrom = new ArrayList<>();
172 //Special logic to allow for B to A case only
173 if(("ToOnly").equals(builder.getFirstNodeType())) {
174 criteriaToFrom.add(buildToFromPart(null, builder.getSecondNodeType()));
176 criteriaToFrom.add(buildToFromPart(builder.getSecondNodeType(), builder.getFirstNodeType()));
178 if (builder.getLabel() != null) {
179 Predicate labelPred = addLabel(builder.getLabel());
180 criteriaFromTo.add(labelPred);
181 criteriaToFrom.add(labelPred);
184 if (builder.getEdgeType() != null) {
185 Predicate typePred = addType(builder.getEdgeType());
186 criteriaFromTo.add(typePred);
187 criteriaToFrom.add(typePred);
189 Predicate privatePredicate = where("private").is(String.valueOf(isPrivate));
192 criteriaFromTo.add(privatePredicate);
193 criteriaToFrom.add(privatePredicate);
196 if (builder.getDirection() != null) {
197 Predicate directionPred = addDirection(builder.getDirection());
198 criteriaFromTo.add(directionPred);
199 criteriaToFrom.add(directionPred);
201 if(("ToOnly").equals(builder.getFirstNodeType())) {
202 this.filter = filter(criteriaToFrom);
203 } else if(("FromOnly").equals(builder.getSecondNodeType())) {
204 this.filter = filter(criteriaFromTo);
206 this.filter = filter(criteriaFromTo).or(filter(criteriaToFrom));
210 private Predicate buildToFromPart(String from, String to) {
211 if (from == null && to == null) { //shouldn't ever happen though
212 throw new IllegalStateException("must have at least one node defined");
218 p = where(EdgeField.FROM.toString()).is(from);
219 } else if (from == null) {
220 p = where(EdgeField.TO.toString()).is(to);
222 p = where(EdgeField.FROM.toString()).is(from).and(EdgeField.TO.toString()).is(to);
228 private Predicate addLabel(String label) {
229 return where(EdgeField.LABEL.toString()).is(label);
232 private Predicate addType(EdgeType type) {
233 if (type == EdgeType.COUSIN) {
234 return where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString());
235 } else { //equals TREE
236 return where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString());
240 private Predicate addDirection(AAIDirection direction) {
241 if (direction == AAIDirection.OUT) {
242 return where(EdgeField.DIRECTION.toString()).in(AAIDirection.OUT.toString(), AAIDirection.BOTH.toString());
243 } else if (direction == AAIDirection.IN) {
244 return where(EdgeField.DIRECTION.toString()).in(AAIDirection.IN.toString(), AAIDirection.BOTH.toString());
245 } else if (direction == AAIDirection.BOTH) {
246 return where(EdgeField.DIRECTION.toString()).is(AAIDirection.BOTH.toString());
247 } else if (direction == AAIDirection.NONE) {
248 return where(EdgeField.DIRECTION.toString()).is(AAIDirection.NONE.toString());
250 return where(EdgeField.DIRECTION.toString()).is(AAIDirection.NONE.toString());
254 * Provides the JsonPath filter for actually querying the edge rule schema files
257 public Filter getFilter() {
262 * Gets the first node type given for the query.
264 * ie, If you called Builder(A,B) this would return A,
265 * if you called Builder(B,A), it would return B,
266 * if you called Builder(A), it would return A.
268 * This is to maintain backwards compatibility with the
269 * EdgeRules API which flipped the direction of
270 * the result EdgeRule to match the input directionality.
272 * @return String first node type of the query
274 public String getFromType() {
279 * So the Ingestor knows which version of the rules to search
280 * @return the Version
282 public Optional<SchemaVersion> getVersion() {
287 public String toString() {
288 StringBuilder sb = new StringBuilder();
290 sb.append("EdgeRuleQuery with filter params node type: ").append(nodeA);
293 sb.append(", node type: ").append(nodeB);
297 sb.append(", label: ").append(label);
300 sb.append(", type: ");
302 sb.append(type.toString());
307 sb.append(", isPrivate: ");
308 sb.append(isPrivate);
311 sb.append(", for version: ").append(v.get().toString()).append(".");
313 return sb.toString();