3029685f4e3cf917ad13b24f524c6a4f125bec2b
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / edges / EdgeRuleQuery.java
1 /** 
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.edges;
24
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;
30
31 import com.jayway.jsonpath.Filter;
32 import com.jayway.jsonpath.Predicate;
33
34 import static com.jayway.jsonpath.Filter.filter;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import static com.jayway.jsonpath.Criteria.where;
40
41 /**
42  * For querying the edge rules schema (not the database)
43  *
44  */
45 public class EdgeRuleQuery {
46         private Filter filter;
47         private Version v;
48         private String nodeA;
49         private String nodeB;
50         private String label;
51         private EdgeType type;
52
53         public static class Builder {
54                 //required 
55                 private String nodeA;
56                 
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
62                 
63                 public Builder(String nodeA) {
64                         this.nodeA = nodeA;
65                 }
66                 
67                 public Builder(String nodeA, String nodeB) {
68                         this.nodeA = nodeA;
69                         this.nodeB = nodeB;
70                 }
71                 
72                 private String getFirstNodeType() {
73                         return nodeA;
74                 }
75                 
76                 private String getSecondNodeType() {
77                         return nodeB;
78                 }
79                 
80                 public Builder label(String label) {
81                         this.label = label;
82                         return this;
83                 }
84                 
85                 private String getLabel() {
86                         return label;
87                 }
88                 
89                 public Builder edgeType(EdgeType type) {
90                         this.type = type;
91                         return this;
92                 }
93                 
94                 private EdgeType getEdgeType() {
95                         return type;
96                 }
97                 
98                 public Builder version(Version version) {
99                         this.version = version;
100                         return this;
101                 }
102                 private Version getVersion() {
103                         return version;
104                 }
105                 
106                 public EdgeRuleQuery build() {
107                         return new EdgeRuleQuery(this);
108                 }
109         }
110         
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();
117                 
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()));
124                 
125                 
126                 
127                 if (builder.getLabel() != null) {
128                         Predicate labelPred = addLabel(builder.getLabel());
129                         criteriaFromTo.add(labelPred);
130                         criteriaToFrom.add(labelPred);
131                 }
132                 
133                 if (builder.getEdgeType() != null) {
134                         Predicate typePred = addType(builder.getEdgeType());
135                         criteriaFromTo.add(typePred);
136                         criteriaToFrom.add(typePred);
137                 }
138                 
139
140                 
141                 this.filter = filter(criteriaFromTo).or(filter(criteriaToFrom));
142         }
143         
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");
147                 }
148                 
149                 Predicate p;
150                 
151                 if (to == null) {
152                         p = where(EdgeField.FROM.toString()).is(from);
153                 } else if (from == null) {
154                         p = where(EdgeField.TO.toString()).is(to);
155                 } else {
156                         p = where(EdgeField.FROM.toString()).is(from).and(EdgeField.TO.toString()).is(to);
157                 }
158                 
159                 return p;
160         }
161         
162         private Predicate addLabel(String label) {
163                 return where(EdgeField.LABEL.toString()).is(label);
164         }
165         
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());
171                 }
172         }
173         
174         /**
175          * Provides the JsonPath filter for actually querying the edge rule schema files
176          * @return Filter
177          */
178         public Filter getFilter() {
179                 return this.filter;
180         }
181         
182         /**
183          * So the Ingestor knows which version of the rules to search
184          * @return the Version
185          */
186         public Version getVersion() {
187                 return this.v;
188         }
189         
190         @Override
191         public String toString() {
192                 StringBuilder sb = new StringBuilder();
193                 
194                 sb.append("EdgeRuleQuery with filter params node type: ").append(nodeA);
195                 
196                 if (nodeB != null) {
197                         sb.append(", node type: ").append(nodeB);
198                 }
199                 
200                 if (label != null) {
201                         sb.append(", label: ").append(label);
202                 } 
203                 
204                 sb.append(", type: ");
205                 if (type != null) {
206                         sb.append(type.toString());
207                 } else {
208                         sb.append("any");
209                 }
210                 
211                 sb.append(", for version: ").append(v.toString()).append(".");
212                 return sb.toString();   
213         }
214 }