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=========================================================
21 package org.onap.aai.edges;
23 import java.util.EnumMap;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.onap.aai.edges.enums.*;
30 * Container for A&AI edge rule information
32 public class EdgeRule {
36 private Direction direction;
37 private MultiplicityRule multiplicityRule;
38 private Map<EdgeProperty, AAIDirection> edgeFields;
39 private boolean isDefaultEdge;
40 private String description;
41 private boolean isPrivateEdge = false;
44 * Instantiates a new edge rule.
46 * @param fieldVals - Map<String, String> where first string is
47 * an EdgeField value and second string is the
50 public EdgeRule(Map<String, String> fieldVals) {
51 edgeFields = new EnumMap<>(EdgeProperty.class);
53 from = fieldVals.get(EdgeField.FROM.toString());
54 to = fieldVals.get(EdgeField.TO.toString());
55 label = fieldVals.get(EdgeField.LABEL.toString());
56 direction = Direction.valueOf(fieldVals.get(EdgeField.DIRECTION.toString()));
57 multiplicityRule = MultiplicityRule.getValue(fieldVals.get(EdgeField.MULTIPLICITY.toString()));
58 isPrivateEdge = Boolean.parseBoolean(fieldVals.getOrDefault(EdgeField.PRIVATE.toString(), "false"));
60 for (EdgeProperty prop : EdgeProperty.values()) {
61 String rawVal = fieldVals.get(prop.toString());
62 edgeFields.put(prop, convertNotation(direction, rawVal));
65 isDefaultEdge = Boolean.parseBoolean(fieldVals.get(EdgeField.DEFAULT.toString()));
66 description = fieldVals.get(EdgeField.DESCRIPTION.toString());
67 if (description == null) { // bc description is optional and not in v12 and earlier
73 public EdgeRule(EdgeRule edgeRule) {
74 this.from = edgeRule.from;
75 this.to = edgeRule.to;
76 this.label = edgeRule.label;
77 this.direction = Direction.valueOf(edgeRule.direction.toString());
78 this.multiplicityRule = MultiplicityRule.valueOf(edgeRule.multiplicityRule.toString());
79 this.edgeFields = new EnumMap<>(edgeRule.edgeFields);
80 this.isDefaultEdge = edgeRule.isDefaultEdge;
81 this.description = edgeRule.description;
82 this.isPrivateEdge = edgeRule.isPrivateEdge;
86 * Converts whatever string was in the json for an edge property value into
87 * the appropriate AAIDirection
89 * @param Direction dir - the edge direction
90 * @param String rawVal - property value from the json, may be
91 * IN, OUT, BOTH, NONE, ${direction}, or !${direction}
92 * @return AAIDirection - IN/OUT/BOTH/NONE if that's the rawVal, or
93 * translates the direction notation into the correct IN/OUT
95 private AAIDirection convertNotation(Direction dir, String rawVal) {
96 if (AAIDirection.NONE.toString().equalsIgnoreCase(rawVal)) {
97 return AAIDirection.NONE;
98 } else if (AAIDirection.BOTH.toString().equalsIgnoreCase(rawVal)) {
99 return AAIDirection.BOTH;
100 } else if (AAIDirection.OUT.toString().equalsIgnoreCase(rawVal)) {
101 return AAIDirection.OUT;
102 } else if (AAIDirection.IN.toString().equalsIgnoreCase(rawVal)) {
103 return AAIDirection.IN;
106 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
107 if (DirectionNotation.DIRECTION.equals(rawDN)) {
108 return AAIDirection.getValue(dir);
110 return AAIDirection.getValue(dir.opposite());
115 * Gets the name of the node type in the "from" field
117 * @return String nodetype
119 public String getFrom() {
124 * Gets the name of the node type in the "to" field
126 * @return String nodetype
128 public String getTo() {
133 * Gets the edge label
135 * @return String label
137 public String getLabel() {
142 * Gets the multiplicity rule.
144 * @return MultiplicityRule
146 public MultiplicityRule getMultiplicityRule() {
147 return multiplicityRule;
151 * Gets the edge direction
155 public Direction getDirection() {
160 * Gets the value of contains-other-v
162 * @return the value of contains-other-v
164 public String getContains() {
165 return edgeFields.get(EdgeProperty.CONTAINS).toString();
169 * Gets the value of delete-other-v
171 * @return the value of delete-other-v
173 public String getDeleteOtherV() {
174 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
178 * Gets the value of the prevent-delete property
180 * @return String prevent-delete property value
182 public String getPreventDelete() {
183 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
187 * Returns if this rule is a default or not
191 public boolean isDefault() {
192 return isDefaultEdge;
196 * Gets the description on the edge rule (if there is one)
198 * @return String description
200 public String getDescription() {
201 return this.description;
205 * Flips the direction value
210 public void flipDirection() {
211 if (Direction.OUT.equals(direction)) {
212 direction = Direction.IN;
213 } else if (Direction.IN.equals(direction)) {
214 direction = Direction.OUT;
216 // else BOTH just stays the same
219 public boolean isPrivateEdge() {
220 return isPrivateEdge;
223 public void setPrivateEdge(boolean privateEdge) {
224 isPrivateEdge = privateEdge;
227 public void setPrivateEdge(String isPrivateEdge) {
228 this.isPrivateEdge = "true".equals(isPrivateEdge);