Improve the performance of resoures microservice
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / edges / EdgeRule.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-18 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.apache.tinkerpop.gremlin.structure.Direction;
23 import org.onap.aai.edges.enums.AAIDirection;
24 import org.onap.aai.edges.enums.DirectionNotation;
25 import org.onap.aai.edges.enums.EdgeField;
26 import org.onap.aai.edges.enums.EdgeProperty;
27 import org.onap.aai.edges.enums.MultiplicityRule;
28
29 import java.util.Collections;
30 import java.util.EnumMap;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 /**
35  * Container for A&AI edge rule information
36  */
37 public class EdgeRule {
38         private String from;
39         private String to;
40         private String label;
41         private Direction direction;
42         private MultiplicityRule multiplicityRule;
43         private Map<EdgeProperty, AAIDirection> edgeFields;
44         private boolean isDefaultEdge;
45         private String description;
46         private boolean isPrivateEdge = false;
47
48     /**
49          * Instantiates a new edge rule.
50          *
51          * @param fieldVals - Map<String, String> where first string is
52          *                                      an EdgeField value and second string is the
53          *                                      value of that field
54          */
55         public EdgeRule(Map<String, String> fieldVals) {
56                 edgeFields = new EnumMap<>(EdgeProperty.class);
57
58                 from = fieldVals.get(EdgeField.FROM.toString());
59                 to = fieldVals.get(EdgeField.TO.toString());
60                 label = fieldVals.get(EdgeField.LABEL.toString());
61                 direction = Direction.valueOf(fieldVals.get(EdgeField.DIRECTION.toString()));
62                 multiplicityRule = MultiplicityRule.getValue(fieldVals.get(EdgeField.MULTIPLICITY.toString()));
63                 isPrivateEdge = Boolean.valueOf(fieldVals.getOrDefault(EdgeField.PRIVATE.toString(), "false"));
64                 for (EdgeProperty prop : EdgeProperty.values()) {
65                         String rawVal = fieldVals.get(prop.toString());
66                         edgeFields.put(prop, convertNotation(direction, rawVal));
67                 }
68
69                 isDefaultEdge = Boolean.valueOf(fieldVals.get(EdgeField.DEFAULT.toString()));
70
71                 description = fieldVals.get(EdgeField.DESCRIPTION.toString());
72                 if (description == null) { //bc description is optional and not in v12 and earlier
73                         description = "";
74                 }
75         }
76
77         // Copy Constructor
78         public EdgeRule(EdgeRule edgeRule){
79             this.from = edgeRule.from;
80             this.to   = edgeRule.to;
81             this.label = edgeRule.label;
82             this.direction = Direction.valueOf(edgeRule.direction.toString());
83             this.multiplicityRule = MultiplicityRule.valueOf(edgeRule.multiplicityRule.toString());
84         this.edgeFields = new HashMap<>(edgeRule.edgeFields);
85             this.isDefaultEdge    = edgeRule.isDefaultEdge;
86             this.description = edgeRule.description;
87             this.isPrivateEdge = edgeRule.isPrivateEdge;
88     }
89
90         /**
91          * Converts whatever string was in the json for an edge property value into
92          * the appropriate AAIDirection
93          *
94          * @param Direction dir - the edge direction
95          * @param String rawVal - property value from the json, may be
96          *                      IN, OUT, BOTH, NONE, ${direction}, or !${direction}
97          * @return AAIDirection - IN/OUT/BOTH/NONE if that's the rawVal, or
98          *                      translates the direction notation into the correct IN/OUT
99          */
100         private AAIDirection convertNotation(Direction dir, String rawVal) {
101                 if (AAIDirection.NONE.toString().equalsIgnoreCase(rawVal)) {
102                         return AAIDirection.NONE;
103                 } else if (AAIDirection.BOTH.toString().equalsIgnoreCase(rawVal)) {
104                         return AAIDirection.BOTH;
105                 } else if (AAIDirection.OUT.toString().equalsIgnoreCase(rawVal)) {
106                         return AAIDirection.OUT;
107                 } else if (AAIDirection.IN.toString().equalsIgnoreCase(rawVal)) {
108                         return AAIDirection.IN;
109                 }
110
111                 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
112                 if (DirectionNotation.DIRECTION.equals(rawDN)) {
113                         return AAIDirection.getValue(dir);
114                 } else {
115                         return AAIDirection.getValue(dir.opposite());
116                 }
117         }
118
119         /**
120          * Gets the name of the node type in the "from" field
121          * @return String nodetype
122          */
123         public String getFrom() {
124                 return from;
125         }
126
127         /**
128          * Gets the name of the node type in the "to" field
129          * @return String nodetype
130          */
131         public String getTo() {
132                 return to;
133         }
134
135         /**
136          * Gets the edge label
137          *
138          * @return String label
139          */
140         public String getLabel() {
141                 return label;
142         }
143
144         /**
145          * Gets the multiplicity rule.
146          *
147          * @return MultiplicityRule
148          */
149         public MultiplicityRule getMultiplicityRule() {
150                 return multiplicityRule;
151         }
152
153         /**
154          * Gets the edge direction
155          *
156          * @return Direction
157          */
158         public Direction getDirection() {
159                 return direction;
160         }
161
162         /**
163          * Gets the value of contains-other-v
164          *
165          * @return the value of contains-other-v
166          */
167         public String getContains() {
168                 return edgeFields.get(EdgeProperty.CONTAINS).toString();
169         }
170
171         /**
172          * Gets the value of delete-other-v
173          *
174          * @return the value of delete-other-v
175          */
176         public String getDeleteOtherV() {
177                 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
178         }
179
180         /**
181          * Gets the value of the prevent-delete property
182          *
183          * @return String prevent-delete property value
184          */
185         public String getPreventDelete() {
186                 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
187         }
188
189         /**
190          * Returns if this rule is a default or not
191          *
192          * @return boolean
193          */
194         public boolean isDefault() {
195                 return isDefaultEdge;
196         }
197
198         /**
199          * Gets the description on the edge rule (if there is one)
200          * @return String description
201          */
202         public String getDescription() {
203                 return this.description;
204         }
205
206         /**
207          * Flips the direction value
208          * IN -> OUT
209          * OUT -> IN
210          * BOTH -> BOTH
211          */
212         public void flipDirection() {
213                 if (Direction.OUT.equals(direction)) {
214                         direction = Direction.IN;
215                 } else if (Direction.IN.equals(direction)) {
216                         direction = Direction.OUT;
217                 }
218                 //else BOTH just stays the same
219         }
220
221         public boolean isPrivateEdge() {
222                 return isPrivateEdge;
223         }
224
225         public void setPrivateEdge(boolean privateEdge) {
226                 isPrivateEdge = privateEdge;
227         }
228
229         public void setPrivateEdge(String isPrivateEdge){
230                 this.isPrivateEdge = "true".equals(isPrivateEdge);
231         }
232 }