f859aae62a32b9cfd1dd35ff05897276e1e4ac17
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / edges / EdgeRule.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.edges;
23
24 import org.apache.tinkerpop.gremlin.structure.Direction;
25 import org.onap.aai.edges.enums.AAIDirection;
26 import org.onap.aai.edges.enums.DirectionNotation;
27 import org.onap.aai.edges.enums.EdgeField;
28 import org.onap.aai.edges.enums.EdgeProperty;
29 import org.onap.aai.edges.enums.MultiplicityRule;
30
31 import java.util.EnumMap;
32 import java.util.Map;
33
34 /**
35  * Container for A&AI edge rule information
36  */
37 public class EdgeRule {
38         private String from;
39         private String to;
40         private String label;
41         private Direction direction;
42         private MultiplicityRule multiplicityRule;
43         private Map<EdgeProperty, AAIDirection> edgeFields;
44         private boolean isDefaultEdge;
45         private String description;
46
47         /**
48          * Instantiates a new edge rule.
49          * 
50          * @param fieldVals - Map<String, String> where first string is
51          *                                      an EdgeField value and second string is the 
52          *                                      value of that field
53          */
54         public EdgeRule(Map<String, String> fieldVals) {
55                 edgeFields = new EnumMap<>(EdgeProperty.class);
56                 
57                 from = fieldVals.get(EdgeField.FROM.toString());
58                 to = fieldVals.get(EdgeField.TO.toString());
59                 label = fieldVals.get(EdgeField.LABEL.toString());
60                 direction = Direction.valueOf(fieldVals.get(EdgeField.DIRECTION.toString()));
61                 multiplicityRule = MultiplicityRule.getValue(fieldVals.get(EdgeField.MULTIPLICITY.toString()));
62                 
63                 for (EdgeProperty prop : EdgeProperty.values()) {
64                         String rawVal = fieldVals.get(prop.toString());
65                         edgeFields.put(prop, convertNotation(direction, rawVal));
66                 }
67                 
68                 isDefaultEdge = Boolean.valueOf(fieldVals.get(EdgeField.DEFAULT.toString()));
69                 
70                 description = fieldVals.get(EdgeField.DESCRIPTION.toString());
71                 if (description == null) { //bc description is optional and not in v12 and earlier
72                         description = "";
73                 }
74         }
75
76         /**
77          * Converts whatever string was in the json for an edge property value into
78          * the appropriate AAIDirection
79          * 
80          * @param Direction dir - the edge direction
81          * @param String rawVal - property value from the json, may be
82          *                      IN, OUT, BOTH, NONE, ${direction}, or !${direction}
83          * @return AAIDirection - IN/OUT/BOTH/NONE if that's the rawVal, or
84          *                      translates the direction notation into the correct IN/OUT
85          */
86         private AAIDirection convertNotation(Direction dir, String rawVal) {
87                 if (AAIDirection.NONE.toString().equals(rawVal)) {
88                         return AAIDirection.NONE;
89                 } else if (AAIDirection.BOTH.toString().equals(rawVal)) {
90                         return AAIDirection.BOTH;
91                 } else if (AAIDirection.OUT.toString().equals(rawVal)) {
92                         return AAIDirection.OUT;
93                 } else if (AAIDirection.IN.toString().equals(rawVal)) {
94                         return AAIDirection.IN;
95                 }
96                 
97                 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
98                 if (DirectionNotation.DIRECTION.equals(rawDN)) {
99                         return AAIDirection.getValue(dir);
100                 } else {
101                         return AAIDirection.getValue(dir.opposite());
102                 }
103         }
104         
105         /**
106          * Gets the name of the node type in the "from" field
107          * @return String nodetype
108          */
109         public String getFrom() {
110                 return from;
111         }
112
113         /**
114          * Gets the name of the node type in the "to" field
115          * @return String nodetype
116          */
117         public String getTo() {
118                 return to;
119         }
120
121         /**
122          * Gets the edge label
123          *
124          * @return String label
125          */
126         public String getLabel() {
127                 return label;
128         }
129         
130         /**
131          * Gets the multiplicity rule.
132          *
133          * @return MultiplicityRule
134          */
135         public MultiplicityRule getMultiplicityRule() {
136                 return multiplicityRule;
137         }
138         
139         /**
140          * Gets the edge direction
141          *
142          * @return Direction
143          */
144         public Direction getDirection() {
145                 return direction;
146         }
147         
148         /**
149          * Gets the value of contains-other-v
150          *
151          * @return the value of contains-other-v
152          */
153         public String getContains() {
154                 return edgeFields.get(EdgeProperty.CONTAINS).toString();
155         }
156         
157         /**
158          * Gets the value of delete-other-v
159          *
160          * @return the value of delete-other-v
161          */
162         public String getDeleteOtherV() {
163                 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
164         }
165         
166         /**
167          * Gets the value of the prevent-delete property
168          * 
169          * @return String prevent-delete property value
170          */
171         public String getPreventDelete() {
172                 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
173         }
174
175         /**
176          * Returns if this rule is a default or not
177          * 
178          * @return boolean
179          */
180         public boolean isDefault() {
181                 return isDefaultEdge;
182         }
183         
184         /**
185          * Gets the description on the edge rule (if there is one)
186          * @return String description
187          */
188         public String getDescription() {
189                 return this.description;
190         }
191 }