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 org.apache.tinkerpop.gremlin.structure.Direction;
24 import org.onap.aai.edges.enums.*;
26 import java.util.EnumMap;
27 import java.util.HashMap;
31 * Container for A&AI edge rule information
33 public class EdgeRule {
37 private Direction direction;
38 private MultiplicityRule multiplicityRule;
39 private Map<EdgeProperty, AAIDirection> edgeFields;
40 private boolean isDefaultEdge;
41 private String description;
42 private boolean isPrivateEdge = false;
45 * Instantiates a new edge rule.
47 * @param fieldVals - Map<String, String> where first string is
48 * an EdgeField value and second string is the
51 public EdgeRule(Map<String, String> fieldVals) {
52 edgeFields = new EnumMap<>(EdgeProperty.class);
54 from = fieldVals.get(EdgeField.FROM.toString());
55 to = fieldVals.get(EdgeField.TO.toString());
56 label = fieldVals.get(EdgeField.LABEL.toString());
57 direction = Direction.valueOf(fieldVals.get(EdgeField.DIRECTION.toString()));
58 multiplicityRule = MultiplicityRule.getValue(fieldVals.get(EdgeField.MULTIPLICITY.toString()));
59 isPrivateEdge = Boolean.parseBoolean(fieldVals.getOrDefault(EdgeField.PRIVATE.toString(), "false"));
61 for (EdgeProperty prop : EdgeProperty.values()) {
62 String rawVal = fieldVals.get(prop.toString());
63 edgeFields.put(prop, convertNotation(direction, rawVal));
66 isDefaultEdge = Boolean.parseBoolean(fieldVals.get(EdgeField.DEFAULT.toString()));
67 description = fieldVals.get(EdgeField.DESCRIPTION.toString());
68 if (description == null) { // bc description is optional and not in v12 and earlier
74 public EdgeRule(EdgeRule edgeRule) {
75 this.from = edgeRule.from;
76 this.to = edgeRule.to;
77 this.label = edgeRule.label;
78 this.direction = Direction.valueOf(edgeRule.direction.toString());
79 this.multiplicityRule = MultiplicityRule.valueOf(edgeRule.multiplicityRule.toString());
80 this.edgeFields = new HashMap<>(edgeRule.edgeFields);
81 this.isDefaultEdge = edgeRule.isDefaultEdge;
82 this.description = edgeRule.description;
83 this.isPrivateEdge = edgeRule.isPrivateEdge;
87 * Converts whatever string was in the json for an edge property value into
88 * the appropriate AAIDirection
90 * @param Direction dir - the edge direction
91 * @param String rawVal - property value from the json, may be
92 * IN, OUT, BOTH, NONE, ${direction}, or !${direction}
93 * @return AAIDirection - IN/OUT/BOTH/NONE if that's the rawVal, or
94 * translates the direction notation into the correct IN/OUT
96 private AAIDirection convertNotation(Direction dir, String rawVal) {
97 if (AAIDirection.NONE.toString().equalsIgnoreCase(rawVal)) {
98 return AAIDirection.NONE;
99 } else if (AAIDirection.BOTH.toString().equalsIgnoreCase(rawVal)) {
100 return AAIDirection.BOTH;
101 } else if (AAIDirection.OUT.toString().equalsIgnoreCase(rawVal)) {
102 return AAIDirection.OUT;
103 } else if (AAIDirection.IN.toString().equalsIgnoreCase(rawVal)) {
104 return AAIDirection.IN;
107 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
108 if (DirectionNotation.DIRECTION.equals(rawDN)) {
109 return AAIDirection.getValue(dir);
111 return AAIDirection.getValue(dir.opposite());
116 * Gets the name of the node type in the "from" field
118 * @return String nodetype
120 public String getFrom() {
125 * Gets the name of the node type in the "to" field
127 * @return String nodetype
129 public String getTo() {
134 * Gets the edge label
136 * @return String label
138 public String getLabel() {
143 * Gets the multiplicity rule.
145 * @return MultiplicityRule
147 public MultiplicityRule getMultiplicityRule() {
148 return multiplicityRule;
152 * Gets the edge direction
156 public Direction getDirection() {
161 * Gets the value of contains-other-v
163 * @return the value of contains-other-v
165 public String getContains() {
166 return edgeFields.get(EdgeProperty.CONTAINS).toString();
170 * Gets the value of delete-other-v
172 * @return the value of delete-other-v
174 public String getDeleteOtherV() {
175 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
179 * Gets the value of the prevent-delete property
181 * @return String prevent-delete property value
183 public String getPreventDelete() {
184 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
188 * Returns if this rule is a default or not
192 public boolean isDefault() {
193 return isDefaultEdge;
197 * Gets the description on the edge rule (if there is one)
199 * @return String description
201 public String getDescription() {
202 return this.description;
206 * Flips the direction value
211 public void flipDirection() {
212 if (Direction.OUT.equals(direction)) {
213 direction = Direction.IN;
214 } else if (Direction.IN.equals(direction)) {
215 direction = Direction.OUT;
217 // else BOTH just stays the same
220 public boolean isPrivateEdge() {
221 return isPrivateEdge;
224 public void setPrivateEdge(boolean privateEdge) {
225 isPrivateEdge = privateEdge;
228 public void setPrivateEdge(String isPrivateEdge) {
229 this.isPrivateEdge = "true".equals(isPrivateEdge);