blob: 8846832f4f9f19b42a046b869648db151971c56e [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska96d55b12015-05-11 08:52:03 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Thomas Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.net.config.basics;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070017
18/**
19 * Basic configuration for network elements, e.g. devices, hosts. Such elements
Simon Hunteb3cf542017-02-10 13:18:41 -080020 * can have a friendly name, geo-coordinates (or grid-coordinates),
21 * logical rack coordinates and an owner entity.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070022 */
23public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> {
24
Simon Hunt53612212016-12-04 17:19:52 -080025 /**
26 * Key for friendly name.
27 */
28 public static final String NAME = "name";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070029
Simon Hunt53612212016-12-04 17:19:52 -080030 /**
31 * Key for UI type (glyph identifier).
32 */
33 public static final String UI_TYPE = "uiType";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070034
Simon Hunt53612212016-12-04 17:19:52 -080035 /**
Simon Hunteb3cf542017-02-10 13:18:41 -080036 * Key for location type (geo or grid).
37 */
38 public static final String LOC_TYPE = "locType";
39
40 /**
Simon Hunt53612212016-12-04 17:19:52 -080041 * Key for latitude.
42 */
43 public static final String LATITUDE = "latitude";
44
45 /**
46 * Key for longitude.
47 */
48 public static final String LONGITUDE = "longitude";
49
50 /**
Simon Hunteb3cf542017-02-10 13:18:41 -080051 * Key for grid Y coordinate.
52 */
Simon Huntbc30e682017-02-15 18:39:23 -080053 public static final String GRID_Y = "gridY";
Simon Hunteb3cf542017-02-10 13:18:41 -080054
Simon Huntbc30e682017-02-15 18:39:23 -080055 /**
56 * Key for grid X coordinate.
57 */
58 public static final String GRID_X = "gridX";
Simon Hunteb3cf542017-02-10 13:18:41 -080059
60 /**
Simon Hunt53612212016-12-04 17:19:52 -080061 * Key for rack address.
62 */
Thomas Vachuska36008462016-01-07 15:38:20 -080063 protected static final String RACK_ADDRESS = "rackAddress";
Simon Hunt53612212016-12-04 17:19:52 -080064
65 /**
66 * Key for owner.
67 */
Thomas Vachuska36008462016-01-07 15:38:20 -080068 protected static final String OWNER = "owner";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070069
Simon Hunt53612212016-12-04 17:19:52 -080070 /**
71 * Threshold for detecting double value is zero.
72 */
Simon Huntf4fd2a22016-08-10 15:41:09 -070073 protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0;
Simon Hunt53612212016-12-04 17:19:52 -080074
Simon Huntf4fd2a22016-08-10 15:41:09 -070075 private static final double DEFAULT_COORD = 0.0;
Simon Hunteb3cf542017-02-10 13:18:41 -080076 private static final String LOC_TYPE_GEO = "geo";
77 private static final String LOC_TYPE_GRID = "grid";
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070078
Jordan Halterman83949a12017-06-21 10:35:38 -070079 private static final int NAME_MAX_LENGTH = 256;
80 private static final int UI_TYPE_MAX_LENGTH = 128;
81 private static final int LOC_TYPE_MAX_LENGTH = 32;
82 private static final int RACK_ADDRESS_MAX_LENGTH = 256;
83 private static final int OWNER_MAX_LENGTH = 128;
84
Thomas Vachuska96d55b12015-05-11 08:52:03 -070085 /**
Simon Hunt53612212016-12-04 17:19:52 -080086 * Returns friendly label for the element. If not set, returns the
87 * element identifier.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070088 *
Simon Huntf4fd2a22016-08-10 15:41:09 -070089 * @return friendly label or element identifier itself if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -070090 */
91 public String name() {
92 return get(NAME, subject.toString());
93 }
94
95 /**
96 * Sets friendly label for the element.
97 *
98 * @param name new friendly label; null to clear
99 * @return self
100 */
101 public BasicElementConfig name(String name) {
102 return (BasicElementConfig) setOrClear(NAME, name);
103 }
104
Simon Hunt1e20dae2016-10-28 11:26:26 -0700105 /**
106 * Returns the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800107 * the Topology View. If not set, null is returned.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700108 *
109 * @return the UI type
110 */
111 public String uiType() {
112 return get(UI_TYPE, null);
113 }
114
115 /**
116 * Sets the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800117 * the Topology View. Setting this to null will indicate that the
118 * default glyph image should be used for the element type.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700119 *
120 * @param uiType the UI type; null for default
121 * @return self
122 */
123 public BasicElementConfig uiType(String uiType) {
124 return (BasicElementConfig) setOrClear(UI_TYPE, uiType);
125 }
126
Simon Hunteb3cf542017-02-10 13:18:41 -0800127 /**
128 * Returns the location type (geo or grid) for the element in
129 * the Topology View. If not set, returns the default of "geo".
130 *
131 * @return location type (string)
132 */
133 public String locType() {
134 return get(LOC_TYPE, LOC_TYPE_GEO);
135 }
136
137 /**
138 * Sets the location type (geo or grid) for the element in
139 * the Topology View. If null is passsed, it will default to "geo".
140 *
141 * @param locType the UI type; null for default
142 * @return self
143 */
144 public BasicElementConfig locType(String locType) {
145 String lt = LOC_TYPE_GRID.equals(locType) ? LOC_TYPE_GRID : LOC_TYPE_GEO;
146 return (BasicElementConfig) setOrClear(LOC_TYPE, lt);
147 }
148
Simon Hunt1e20dae2016-10-28 11:26:26 -0700149 private boolean doubleIsZero(double value) {
Simon Huntf4fd2a22016-08-10 15:41:09 -0700150 return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD;
151 }
152
153 /**
154 * Returns true if the geographical coordinates (latitude and longitude)
Simon Hunt1e20dae2016-10-28 11:26:26 -0700155 * are set on this element; false otherwise.
Simon Huntf4fd2a22016-08-10 15:41:09 -0700156 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700157 * @return true if geo-coordinates are set; false otherwise
Simon Huntf4fd2a22016-08-10 15:41:09 -0700158 */
159 public boolean geoCoordsSet() {
160 return !doubleIsZero(latitude()) || !doubleIsZero(longitude());
161 }
162
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700163 /**
164 * Returns element latitude.
165 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700166 * @return element latitude; 0.0 if (possibly) not set
167 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700168 */
169 public double latitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700170 return get(LATITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700171 }
172
173 /**
174 * Sets the element latitude.
175 *
176 * @param latitude new latitude; null to clear
177 * @return self
178 */
179 public BasicElementConfig latitude(Double latitude) {
180 return (BasicElementConfig) setOrClear(LATITUDE, latitude);
181 }
182
183 /**
Simon Huntf4fd2a22016-08-10 15:41:09 -0700184 * Returns element longitude.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700185 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700186 * @return element longitude; 0 if (possibly) not set
187 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700188 */
189 public double longitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700190 return get(LONGITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700191 }
192
193 /**
194 * Sets the element longitude.
195 *
196 * @param longitude new longitude; null to clear
197 * @return self
198 */
199 public BasicElementConfig longitude(Double longitude) {
200 return (BasicElementConfig) setOrClear(LONGITUDE, longitude);
201 }
202
203 /**
Simon Huntbc30e682017-02-15 18:39:23 -0800204 * Returns true if the grid coordinates (gridY and gridX) are set on
205 * this element; false otherwise.
206 * <p>
207 * It is assumed that elements will not be placed at {@code (0,0)}.
208 * If you really need to position the element there, consider setting the
209 * coordinates to something like {@code (0.000001, 0.000001)} instead.
Simon Hunteb3cf542017-02-10 13:18:41 -0800210 *
Simon Huntbc30e682017-02-15 18:39:23 -0800211 * @return true if grid coordinates are set; false otherwise.
Simon Hunteb3cf542017-02-10 13:18:41 -0800212 */
Simon Huntbc30e682017-02-15 18:39:23 -0800213 public boolean gridCoordsSet() {
214 return !doubleIsZero(gridY()) || !doubleIsZero(gridX());
Simon Hunteb3cf542017-02-10 13:18:41 -0800215 }
216
217 /**
218 * Returns element grid y-coordinate.
219 *
220 * @return element y-coordinate
221 */
222 public double gridY() {
223 return get(GRID_Y, DEFAULT_COORD);
224 }
225
226 /**
227 * Sets the element grid y-coordinate.
228 *
229 * @param y new y-coordinate; null to clear
230 * @return self
231 */
232 public BasicElementConfig gridY(Double y) {
233 return (BasicElementConfig) setOrClear(GRID_Y, y);
234 }
235
236 /**
Simon Huntbc30e682017-02-15 18:39:23 -0800237 * Returns element grid x-coordinate.
238 *
239 * @return element x-coordinate
240 */
241 public double gridX() {
242 return get(GRID_X, DEFAULT_COORD);
243 }
244
245 /**
246 * Sets the element grid x-coordinate.
247 *
248 * @param x new x-coordinate; null to clear
249 * @return self
250 */
251 public BasicElementConfig gridX(Double x) {
252 return (BasicElementConfig) setOrClear(GRID_X, x);
253 }
254
255 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700256 * Returns the element rack address.
257 *
258 * @return rack address; null if not set
259 */
260 public String rackAddress() {
261 return get(RACK_ADDRESS, null);
262 }
263
264 /**
265 * Sets element rack address.
266 *
267 * @param address new rack address; null to clear
268 * @return self
269 */
270 public BasicElementConfig rackAddress(String address) {
271 return (BasicElementConfig) setOrClear(RACK_ADDRESS, address);
272 }
273
274 /**
275 * Returns owner of the element.
276 *
277 * @return owner or null if not set
278 */
279 public String owner() {
280 return get(OWNER, null);
281 }
282
283 /**
284 * Sets the owner of the element.
285 *
286 * @param owner new owner; null to clear
287 * @return self
288 */
289 public BasicElementConfig owner(String owner) {
290 return (BasicElementConfig) setOrClear(OWNER, owner);
291 }
292
Jordan Halterman83949a12017-06-21 10:35:38 -0700293 @Override
294 public boolean isValid() {
295 return isValidLength(NAME, NAME_MAX_LENGTH)
296 && isValidLength(UI_TYPE, UI_TYPE_MAX_LENGTH)
297 && isValidLength(LOC_TYPE, LOC_TYPE_MAX_LENGTH)
298 && isValidLength(RACK_ADDRESS, RACK_ADDRESS_MAX_LENGTH)
299 && isValidLength(OWNER, OWNER_MAX_LENGTH);
300 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700301}