e1cb240e4d674536bded413c5f2acc91463a0762
[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-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
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         private boolean isPrivateEdge = false;
45
46         /**
47          * Instantiates a new edge rule.
48          * 
49          * @param fieldVals - Map<String, String> where first string is
50          *                                      an EdgeField value and second string is the 
51          *                                      value of that field
52          */
53         public EdgeRule(Map<String, String> fieldVals) {
54                 edgeFields = new EnumMap<>(EdgeProperty.class);
55                 
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));
65                 }
66                 
67                 isDefaultEdge = Boolean.valueOf(fieldVals.get(EdgeField.DEFAULT.toString()));
68                 
69                 description = fieldVals.get(EdgeField.DESCRIPTION.toString());
70                 if (description == null) { //bc description is optional and not in v12 and earlier
71                         description = "";
72                 }
73         }
74
75         /**
76          * Converts whatever string was in the json for an edge property value into
77          * the appropriate AAIDirection
78          * 
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
84          */
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;
94                 }
95                 
96                 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
97                 if (DirectionNotation.DIRECTION.equals(rawDN)) {
98                         return AAIDirection.getValue(dir);
99                 } else {
100                         return AAIDirection.getValue(dir.opposite());
101                 }
102         }
103         
104         /**
105          * Gets the name of the node type in the "from" field
106          * @return String nodetype
107          */
108         public String getFrom() {
109                 return from;
110         }
111
112         /**
113          * Gets the name of the node type in the "to" field
114          * @return String nodetype
115          */
116         public String getTo() {
117                 return to;
118         }
119
120         /**
121          * Gets the edge label
122          *
123          * @return String label
124          */
125         public String getLabel() {
126                 return label;
127         }
128         
129         /**
130          * Gets the multiplicity rule.
131          *
132          * @return MultiplicityRule
133          */
134         public MultiplicityRule getMultiplicityRule() {
135                 return multiplicityRule;
136         }
137         
138         /**
139          * Gets the edge direction
140          *
141          * @return Direction
142          */
143         public Direction getDirection() {
144                 return direction;
145         }
146         
147         /**
148          * Gets the value of contains-other-v
149          *
150          * @return the value of contains-other-v
151          */
152         public String getContains() {
153                 return edgeFields.get(EdgeProperty.CONTAINS).toString();
154         }
155         
156         /**
157          * Gets the value of delete-other-v
158          *
159          * @return the value of delete-other-v
160          */
161         public String getDeleteOtherV() {
162                 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
163         }
164         
165         /**
166          * Gets the value of the prevent-delete property
167          * 
168          * @return String prevent-delete property value
169          */
170         public String getPreventDelete() {
171                 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
172         }
173
174         /**
175          * Returns if this rule is a default or not
176          * 
177          * @return boolean
178          */
179         public boolean isDefault() {
180                 return isDefaultEdge;
181         }
182         
183         /**
184          * Gets the description on the edge rule (if there is one)
185          * @return String description
186          */
187         public String getDescription() {
188                 return this.description;
189         }
190         
191         /**
192          * Flips the direction value
193          * IN -> OUT
194          * OUT -> IN
195          * BOTH -> BOTH
196          */
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;
202                 }
203                 //else BOTH just stays the same
204         }
205
206         public boolean isPrivateEdge() {
207                 return isPrivateEdge;
208         }
209
210         public void setPrivateEdge(boolean privateEdge) {
211                 isPrivateEdge = privateEdge;
212         }
213
214         public void setPrivateEdge(String isPrivateEdge){
215                 this.isPrivateEdge = "true".equals(isPrivateEdge);
216         }
217 }