Update schema ingest library call schema service
[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.*;
24
25 import java.util.EnumMap;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 /**
30  * Container for A&AI edge rule information
31  */
32 public class EdgeRule {
33         private String from;
34         private String to;
35         private String label;
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;
42
43         /**
44          * Instantiates a new edge rule.
45          * 
46          * @param fieldVals - Map<String, String> where first string is
47          *                                      an EdgeField value and second string is the 
48          *                                      value of that field
49          */
50         public EdgeRule(Map<String, String> fieldVals) {
51                 edgeFields = new EnumMap<>(EdgeProperty.class);
52                 
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"));
59
60         for (EdgeProperty prop : EdgeProperty.values()) {
61                         String rawVal = fieldVals.get(prop.toString());
62                         edgeFields.put(prop, convertNotation(direction, rawVal));
63                 }
64                 
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
68                         description = "";
69                 }
70         }
71
72     // Copy Constructor
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;
83     }
84
85         /**
86          * Converts whatever string was in the json for an edge property value into
87          * the appropriate AAIDirection
88          * 
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
94          */
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;
104                 }
105                 
106                 DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
107                 if (DirectionNotation.DIRECTION.equals(rawDN)) {
108                         return AAIDirection.getValue(dir);
109                 } else {
110                         return AAIDirection.getValue(dir.opposite());
111                 }
112         }
113         
114         /**
115          * Gets the name of the node type in the "from" field
116          * @return String nodetype
117          */
118         public String getFrom() {
119                 return from;
120         }
121
122         /**
123          * Gets the name of the node type in the "to" field
124          * @return String nodetype
125          */
126         public String getTo() {
127                 return to;
128         }
129
130         /**
131          * Gets the edge label
132          *
133          * @return String label
134          */
135         public String getLabel() {
136                 return label;
137         }
138         
139         /**
140          * Gets the multiplicity rule.
141          *
142          * @return MultiplicityRule
143          */
144         public MultiplicityRule getMultiplicityRule() {
145                 return multiplicityRule;
146         }
147         
148         /**
149          * Gets the edge direction
150          *
151          * @return Direction
152          */
153         public Direction getDirection() {
154                 return direction;
155         }
156         
157         /**
158          * Gets the value of contains-other-v
159          *
160          * @return the value of contains-other-v
161          */
162         public String getContains() {
163                 return edgeFields.get(EdgeProperty.CONTAINS).toString();
164         }
165         
166         /**
167          * Gets the value of delete-other-v
168          *
169          * @return the value of delete-other-v
170          */
171         public String getDeleteOtherV() {
172                 return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
173         }
174         
175         /**
176          * Gets the value of the prevent-delete property
177          * 
178          * @return String prevent-delete property value
179          */
180         public String getPreventDelete() {
181                 return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
182         }
183
184         /**
185          * Returns if this rule is a default or not
186          * 
187          * @return boolean
188          */
189         public boolean isDefault() {
190                 return isDefaultEdge;
191         }
192         
193         /**
194          * Gets the description on the edge rule (if there is one)
195          * @return String description
196          */
197         public String getDescription() {
198                 return this.description;
199         }
200         
201         /**
202          * Flips the direction value
203          * IN -> OUT
204          * OUT -> IN
205          * BOTH -> BOTH
206          */
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;
212                 }
213                 //else BOTH just stays the same
214         }
215
216         public boolean isPrivateEdge() {
217                 return isPrivateEdge;
218         }
219
220         public void setPrivateEdge(boolean privateEdge) {
221                 isPrivateEdge = privateEdge;
222         }
223
224         public void setPrivateEdge(String isPrivateEdge){
225                 this.isPrivateEdge = "true".equals(isPrivateEdge);
226         }
227 }