Update the license for 2017-2018 license
[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-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
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 package org.onap.aai.edges;
21
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;
27
28 import com.jayway.jsonpath.Filter;
29 import com.jayway.jsonpath.Predicate;
30
31 import static com.jayway.jsonpath.Filter.filter;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import static com.jayway.jsonpath.Criteria.where;
37
38 /**
39  * For querying the edge rules schema (not the database)
40  *
41  */
42 public class EdgeRuleQuery {
43         private Filter filter;
44         private Version v;
45         private String nodeA;
46         private String nodeB;
47         private String label;
48         private EdgeType type;
49
50         public static class Builder {
51                 //required 
52                 private String nodeA;
53                 
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
59                 
60                 public Builder(String nodeA) {
61                         this.nodeA = nodeA;
62                 }
63                 
64                 public Builder(String nodeA, String nodeB) {
65                         this.nodeA = nodeA;
66                         this.nodeB = nodeB;
67                 }
68                 
69                 private String getFirstNodeType() {
70                         return nodeA;
71                 }
72                 
73                 private String getSecondNodeType() {
74                         return nodeB;
75                 }
76                 
77                 public Builder label(String label) {
78                         this.label = label;
79                         return this;
80                 }
81                 
82                 private String getLabel() {
83                         return label;
84                 }
85                 
86                 public Builder edgeType(EdgeType type) {
87                         this.type = type;
88                         return this;
89                 }
90                 
91                 private EdgeType getEdgeType() {
92                         return type;
93                 }
94                 
95                 public Builder version(Version version) {
96                         this.version = version;
97                         return this;
98                 }
99                 private Version getVersion() {
100                         return version;
101                 }
102                 
103                 public EdgeRuleQuery build() {
104                         return new EdgeRuleQuery(this);
105                 }
106         }
107         
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();
114                 
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()));
121                 
122                 
123                 
124                 if (builder.getLabel() != null) {
125                         Predicate labelPred = addLabel(builder.getLabel());
126                         criteriaFromTo.add(labelPred);
127                         criteriaToFrom.add(labelPred);
128                 }
129                 
130                 if (builder.getEdgeType() != null) {
131                         Predicate typePred = addType(builder.getEdgeType());
132                         criteriaFromTo.add(typePred);
133                         criteriaToFrom.add(typePred);
134                 }
135                 
136
137                 
138                 this.filter = filter(criteriaFromTo).or(filter(criteriaToFrom));
139         }
140         
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");
144                 }
145                 
146                 Predicate p;
147                 
148                 if (to == null) {
149                         p = where(EdgeField.FROM.toString()).is(from);
150                 } else if (from == null) {
151                         p = where(EdgeField.TO.toString()).is(to);
152                 } else {
153                         p = where(EdgeField.FROM.toString()).is(from).and(EdgeField.TO.toString()).is(to);
154                 }
155                 
156                 return p;
157         }
158         
159         private Predicate addLabel(String label) {
160                 return where(EdgeField.LABEL.toString()).is(label);
161         }
162         
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());
168                 }
169         }
170         
171         /**
172          * Provides the JsonPath filter for actually querying the edge rule schema files
173          * @return Filter
174          */
175         public Filter getFilter() {
176                 return this.filter;
177         }
178         
179         /**
180          * So the Ingestor knows which version of the rules to search
181          * @return the Version
182          */
183         public Version getVersion() {
184                 return this.v;
185         }
186         
187         @Override
188         public String toString() {
189                 StringBuilder sb = new StringBuilder();
190                 
191                 sb.append("EdgeRuleQuery with filter params node type: ").append(nodeA);
192                 
193                 if (nodeB != null) {
194                         sb.append(", node type: ").append(nodeB);
195                 }
196                 
197                 if (label != null) {
198                         sb.append(", label: ").append(label);
199                 } 
200                 
201                 sb.append(", type: ");
202                 if (type != null) {
203                         sb.append(type.toString());
204                 } else {
205                         sb.append("any");
206                 }
207                 
208                 sb.append(", for version: ").append(v.toString()).append(".");
209                 return sb.toString();   
210         }
211 }