Enhancements for the aai-common library
[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
21 package org.onap.aai.edges;
22
23 import org.apache.tinkerpop.gremlin.structure.Direction;
24 import org.onap.aai.edges.enums.*;
25
26 import java.util.EnumMap;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 /**
31  * Container for A&AI edge rule information
32  */
33 public class EdgeRule {
34     private String from;
35     private String to;
36     private String label;
37     private Direction direction;
38     private MultiplicityRule multiplicityRule;
39     private Map<EdgeProperty, AAIDirection> edgeFields;
40     private boolean isDefaultEdge;
41     private String description;
42     private boolean isPrivateEdge = false;
43
44     /**
45      * Instantiates a new edge rule.
46      *
47      * @param fieldVals - Map<String, String> where first string is
48      *        an EdgeField value and second string is the
49      *        value of that field
50      */
51     public EdgeRule(Map<String, String> fieldVals) {
52         edgeFields = new EnumMap<>(EdgeProperty.class);
53
54         from = fieldVals.get(EdgeField.FROM.toString());
55         to = fieldVals.get(EdgeField.TO.toString());
56         label = fieldVals.get(EdgeField.LABEL.toString());
57         direction = Direction.valueOf(fieldVals.get(EdgeField.DIRECTION.toString()));
58         multiplicityRule = MultiplicityRule.getValue(fieldVals.get(EdgeField.MULTIPLICITY.toString()));
59         isPrivateEdge = Boolean.parseBoolean(fieldVals.getOrDefault(EdgeField.PRIVATE.toString(), "false"));
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.parseBoolean(fieldVals.get(EdgeField.DEFAULT.toString()));
67         description = fieldVals.get(EdgeField.DESCRIPTION.toString());
68         if (description == null) { // bc description is optional and not in v12 and earlier
69             description = "";
70         }
71     }
72
73     // Copy Constructor
74     public EdgeRule(EdgeRule edgeRule) {
75         this.from = edgeRule.from;
76         this.to = edgeRule.to;
77         this.label = edgeRule.label;
78         this.direction = Direction.valueOf(edgeRule.direction.toString());
79         this.multiplicityRule = MultiplicityRule.valueOf(edgeRule.multiplicityRule.toString());
80         this.edgeFields = new HashMap<>(edgeRule.edgeFields);
81         this.isDefaultEdge = edgeRule.isDefaultEdge;
82         this.description = edgeRule.description;
83         this.isPrivateEdge = edgeRule.isPrivateEdge;
84     }
85
86     /**
87      * Converts whatever string was in the json for an edge property value into
88      * the appropriate AAIDirection
89      *
90      * @param Direction dir - the edge direction
91      * @param String rawVal - property value from the json, may be
92      *        IN, OUT, BOTH, NONE, ${direction}, or !${direction}
93      * @return AAIDirection - IN/OUT/BOTH/NONE if that's the rawVal, or
94      *         translates the direction notation into the correct IN/OUT
95      */
96     private AAIDirection convertNotation(Direction dir, String rawVal) {
97         if (AAIDirection.NONE.toString().equalsIgnoreCase(rawVal)) {
98             return AAIDirection.NONE;
99         } else if (AAIDirection.BOTH.toString().equalsIgnoreCase(rawVal)) {
100             return AAIDirection.BOTH;
101         } else if (AAIDirection.OUT.toString().equalsIgnoreCase(rawVal)) {
102             return AAIDirection.OUT;
103         } else if (AAIDirection.IN.toString().equalsIgnoreCase(rawVal)) {
104             return AAIDirection.IN;
105         }
106
107         DirectionNotation rawDN = DirectionNotation.getValue(rawVal);
108         if (DirectionNotation.DIRECTION.equals(rawDN)) {
109             return AAIDirection.getValue(dir);
110         } else {
111             return AAIDirection.getValue(dir.opposite());
112         }
113     }
114
115     /**
116      * Gets the name of the node type in the "from" field
117      *
118      * @return String nodetype
119      */
120     public String getFrom() {
121         return from;
122     }
123
124     /**
125      * Gets the name of the node type in the "to" field
126      *
127      * @return String nodetype
128      */
129     public String getTo() {
130         return to;
131     }
132
133     /**
134      * Gets the edge label
135      *
136      * @return String label
137      */
138     public String getLabel() {
139         return label;
140     }
141
142     /**
143      * Gets the multiplicity rule.
144      *
145      * @return MultiplicityRule
146      */
147     public MultiplicityRule getMultiplicityRule() {
148         return multiplicityRule;
149     }
150
151     /**
152      * Gets the edge direction
153      *
154      * @return Direction
155      */
156     public Direction getDirection() {
157         return direction;
158     }
159
160     /**
161      * Gets the value of contains-other-v
162      *
163      * @return the value of contains-other-v
164      */
165     public String getContains() {
166         return edgeFields.get(EdgeProperty.CONTAINS).toString();
167     }
168
169     /**
170      * Gets the value of delete-other-v
171      *
172      * @return the value of delete-other-v
173      */
174     public String getDeleteOtherV() {
175         return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
176     }
177
178     /**
179      * Gets the value of the prevent-delete property
180      *
181      * @return String prevent-delete property value
182      */
183     public String getPreventDelete() {
184         return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
185     }
186
187     /**
188      * Returns if this rule is a default or not
189      *
190      * @return boolean
191      */
192     public boolean isDefault() {
193         return isDefaultEdge;
194     }
195
196     /**
197      * Gets the description on the edge rule (if there is one)
198      *
199      * @return String description
200      */
201     public String getDescription() {
202         return this.description;
203     }
204
205     /**
206      * Flips the direction value
207      * IN -> OUT
208      * OUT -> IN
209      * BOTH -> BOTH
210      */
211     public void flipDirection() {
212         if (Direction.OUT.equals(direction)) {
213             direction = Direction.IN;
214         } else if (Direction.IN.equals(direction)) {
215             direction = Direction.OUT;
216         }
217         // else BOTH just stays the same
218     }
219
220     public boolean isPrivateEdge() {
221         return isPrivateEdge;
222     }
223
224     public void setPrivateEdge(boolean privateEdge) {
225         isPrivateEdge = privateEdge;
226     }
227
228     public void setPrivateEdge(String isPrivateEdge) {
229         this.isPrivateEdge = "true".equals(isPrivateEdge);
230     }
231 }