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.EnumMap;
33 * Container for A&AI edge rule information
35 public class EdgeRule {
39 private Direction direction;
40 private MultiplicityRule multiplicityRule;
41 private Map<EdgeProperty, AAIDirection> edgeFields;
42 private boolean isDefaultEdge;
43 private String description;
44 private boolean isPrivateEdge = false;
47 * Instantiates a new edge rule.
49 * @param fieldVals - Map<String, String> where first string is
50 * an EdgeField value and second string is the
53 public EdgeRule(Map<String, String> fieldVals) {
54 edgeFields = new EnumMap<>(EdgeProperty.class);
56 from = fieldVals.get(EdgeField.FROM.toString());
57 to = fieldVals.get(EdgeField.TO.toString());
58 label = fieldVals.get(EdgeField.LABEL.toString());
59 direction = Direction.valueOf(fieldVals.get(EdgeField.DIRECTION.toString()));
60 multiplicityRule = MultiplicityRule.getValue(fieldVals.get(EdgeField.MULTIPLICITY.toString()));
61 isPrivateEdge = Boolean.valueOf(fieldVals.getOrDefault(EdgeField.PRIVATE.toString(), "false"));
62 for (EdgeProperty prop : EdgeProperty.values()) {
63 String rawVal = fieldVals.get(prop.toString());
64 edgeFields.put(prop, convertNotation(direction, rawVal));
67 isDefaultEdge = Boolean.valueOf(fieldVals.get(EdgeField.DEFAULT.toString()));
69 description = fieldVals.get(EdgeField.DESCRIPTION.toString());
70 if (description == null) { //bc description is optional and not in v12 and earlier
76 * Converts whatever string was in the json for an edge property value into
77 * the appropriate AAIDirection
79 * @param Direction dir - the edge direction
80 * @param String rawVal - property value from the json, may be
81 * IN, OUT, BOTH, NONE, ${direction}, or !${direction}
82 * @return AAIDirection - IN/OUT/BOTH/NONE if that's the rawVal, or
83 * translates the direction notation into the correct IN/OUT
85 private AAIDirection convertNotation(Direction dir, String rawVal) {
86 if (AAIDirection.NONE.toString().equalsIgnoreCase(rawVal)) {
87 return AAIDirection.NONE;
88 } else if (AAIDirection.BOTH.toString().equalsIgnoreCase(rawVal)) {
89 return AAIDirection.BOTH;
90 } else if (AAIDirection.OUT.toString().equalsIgnoreCase(rawVal)) {
91 return AAIDirection.OUT;
92 } else if (AAIDirection.IN.toString().equalsIgnoreCase(rawVal)) {
93 return AAIDirection.IN;
96 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
97 if (DirectionNotation.DIRECTION.equals(rawDN)) {
98 return AAIDirection.getValue(dir);
100 return AAIDirection.getValue(dir.opposite());
105 * Gets the name of the node type in the "from" field
106 * @return String nodetype
108 public String getFrom() {
113 * Gets the name of the node type in the "to" field
114 * @return String nodetype
116 public String getTo() {
121 * Gets the edge label
123 * @return String label
125 public String getLabel() {
130 * Gets the multiplicity rule.
132 * @return MultiplicityRule
134 public MultiplicityRule getMultiplicityRule() {
135 return multiplicityRule;
139 * Gets the edge direction
143 public Direction getDirection() {
148 * Gets the value of contains-other-v
150 * @return the value of contains-other-v
152 public String getContains() {
153 return edgeFields.get(EdgeProperty.CONTAINS).toString();
157 * Gets the value of delete-other-v
159 * @return the value of delete-other-v
161 public String getDeleteOtherV() {
162 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
166 * Gets the value of the prevent-delete property
168 * @return String prevent-delete property value
170 public String getPreventDelete() {
171 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
175 * Returns if this rule is a default or not
179 public boolean isDefault() {
180 return isDefaultEdge;
184 * Gets the description on the edge rule (if there is one)
185 * @return String description
187 public String getDescription() {
188 return this.description;
192 * Flips the direction value
197 public void flipDirection() {
198 if (Direction.OUT.equals(direction)) {
199 direction = Direction.IN;
200 } else if (Direction.IN.equals(direction)) {
201 direction = Direction.OUT;
203 //else BOTH just stays the same
206 public boolean isPrivateEdge() {
207 return isPrivateEdge;
210 public void setPrivateEdge(boolean privateEdge) {
211 isPrivateEdge = privateEdge;
214 public void setPrivateEdge(String isPrivateEdge){
215 this.isPrivateEdge = "true".equals(isPrivateEdge);