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.*;
25 import java.util.EnumMap;
26 import java.util.HashMap;
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.valueOf(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.valueOf(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 HashMap<>(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
116 * @return String nodetype
118 public String getFrom() {
123 * Gets the name of the node type in the "to" field
124 * @return String nodetype
126 public String getTo() {
131 * Gets the edge label
133 * @return String label
135 public String getLabel() {
140 * Gets the multiplicity rule.
142 * @return MultiplicityRule
144 public MultiplicityRule getMultiplicityRule() {
145 return multiplicityRule;
149 * Gets the edge direction
153 public Direction getDirection() {
158 * Gets the value of contains-other-v
160 * @return the value of contains-other-v
162 public String getContains() {
163 return edgeFields.get(EdgeProperty.CONTAINS).toString();
167 * Gets the value of delete-other-v
169 * @return the value of delete-other-v
171 public String getDeleteOtherV() {
172 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
176 * Gets the value of the prevent-delete property
178 * @return String prevent-delete property value
180 public String getPreventDelete() {
181 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
185 * Returns if this rule is a default or not
189 public boolean isDefault() {
190 return isDefaultEdge;
194 * Gets the description on the edge rule (if there is one)
195 * @return String description
197 public String getDescription() {
198 return this.description;
202 * Flips the direction value
207 public void flipDirection() {
208 if (Direction.OUT.equals(direction)) {
209 direction = Direction.IN;
210 } else if (Direction.IN.equals(direction)) {
211 direction = Direction.OUT;
213 //else BOTH just stays the same
216 public boolean isPrivateEdge() {
217 return isPrivateEdge;
220 public void setPrivateEdge(boolean privateEdge) {
221 isPrivateEdge = privateEdge;
224 public void setPrivateEdge(String isPrivateEdge){
225 this.isPrivateEdge = "true".equals(isPrivateEdge);