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