AAI-common sonar fixes
[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.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.parseBoolean(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.parseBoolean(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 EnumMap<>(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      *
117      * @return String nodetype
118      */
119     public String getFrom() {
120         return from;
121     }
122
123     /**
124      * Gets the name of the node type in the "to" field
125      *
126      * @return String nodetype
127      */
128     public String getTo() {
129         return to;
130     }
131
132     /**
133      * Gets the edge label
134      *
135      * @return String label
136      */
137     public String getLabel() {
138         return label;
139     }
140
141     /**
142      * Gets the multiplicity rule.
143      *
144      * @return MultiplicityRule
145      */
146     public MultiplicityRule getMultiplicityRule() {
147         return multiplicityRule;
148     }
149
150     /**
151      * Gets the edge direction
152      *
153      * @return Direction
154      */
155     public Direction getDirection() {
156         return direction;
157     }
158
159     /**
160      * Gets the value of contains-other-v
161      *
162      * @return the value of contains-other-v
163      */
164     public String getContains() {
165         return edgeFields.get(EdgeProperty.CONTAINS).toString();
166     }
167
168     /**
169      * Gets the value of delete-other-v
170      *
171      * @return the value of delete-other-v
172      */
173     public String getDeleteOtherV() {
174         return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString();
175     }
176
177     /**
178      * Gets the value of the prevent-delete property
179      *
180      * @return String prevent-delete property value
181      */
182     public String getPreventDelete() {
183         return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString();
184     }
185
186     /**
187      * Returns if this rule is a default or not
188      *
189      * @return boolean
190      */
191     public boolean isDefault() {
192         return isDefaultEdge;
193     }
194
195     /**
196      * Gets the description on the edge rule (if there is one)
197      *
198      * @return String description
199      */
200     public String getDescription() {
201         return this.description;
202     }
203
204     /**
205      * Flips the direction value
206      * IN -> OUT
207      * OUT -> IN
208      * BOTH -> BOTH
209      */
210     public void flipDirection() {
211         if (Direction.OUT.equals(direction)) {
212             direction = Direction.IN;
213         } else if (Direction.IN.equals(direction)) {
214             direction = Direction.OUT;
215         }
216         // else BOTH just stays the same
217     }
218
219     public boolean isPrivateEdge() {
220         return isPrivateEdge;
221     }
222
223     public void setPrivateEdge(boolean privateEdge) {
224         isPrivateEdge = privateEdge;
225     }
226
227     public void setPrivateEdge(String isPrivateEdge) {
228         this.isPrivateEdge = "true".equals(isPrivateEdge);
229     }
230 }