2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.edges;
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;
29 import java.util.Collections;
30 import java.util.EnumMap;
31 import java.util.HashMap;
35 * Container for A&AI edge rule information
37 public class EdgeRule {
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;
49 * Instantiates a new edge rule.
51 * @param fieldVals - Map<String, String> where first string is
52 * an EdgeField value and second string is the
55 public EdgeRule(Map<String, String> fieldVals) {
56 edgeFields = new EnumMap<>(EdgeProperty.class);
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));
69 isDefaultEdge = Boolean.valueOf(fieldVals.get(EdgeField.DEFAULT.toString()));
71 description = fieldVals.get(EdgeField.DESCRIPTION.toString());
72 if (description == null) { //bc description is optional and not in v12 and earlier
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;
91 * Converts whatever string was in the json for an edge property value into
92 * the appropriate AAIDirection
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
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;
111 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
112 if (DirectionNotation.DIRECTION.equals(rawDN)) {
113 return AAIDirection.getValue(dir);
115 return AAIDirection.getValue(dir.opposite());
120 * Gets the name of the node type in the "from" field
121 * @return String nodetype
123 public String getFrom() {
128 * Gets the name of the node type in the "to" field
129 * @return String nodetype
131 public String getTo() {
136 * Gets the edge label
138 * @return String label
140 public String getLabel() {
145 * Gets the multiplicity rule.
147 * @return MultiplicityRule
149 public MultiplicityRule getMultiplicityRule() {
150 return multiplicityRule;
154 * Gets the edge direction
158 public Direction getDirection() {
163 * Gets the value of contains-other-v
165 * @return the value of contains-other-v
167 public String getContains() {
168 return edgeFields.get(EdgeProperty.CONTAINS).toString();
172 * Gets the value of delete-other-v
174 * @return the value of delete-other-v
176 public String getDeleteOtherV() {
177 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
181 * Gets the value of the prevent-delete property
183 * @return String prevent-delete property value
185 public String getPreventDelete() {
186 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
190 * Returns if this rule is a default or not
194 public boolean isDefault() {
195 return isDefaultEdge;
199 * Gets the description on the edge rule (if there is one)
200 * @return String description
202 public String getDescription() {
203 return this.description;
207 * Flips the direction value
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;
218 //else BOTH just stays the same
221 public boolean isPrivateEdge() {
222 return isPrivateEdge;
225 public void setPrivateEdge(boolean privateEdge) {
226 isPrivateEdge = privateEdge;
229 public void setPrivateEdge(String isPrivateEdge){
230 this.isPrivateEdge = "true".equals(isPrivateEdge);