blob: 05046247904602bf038c2b4cd5a8999a5531f8c5 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Thomas Vachuska3516f062018-03-19 10:12:25 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.google.common.collect.ImmutableSet;
20
Thomas Vachuskac67a9912018-03-06 14:37:45 -080021import java.util.Objects;
Thomas Vachuska3516f062018-03-19 10:12:25 -070022import java.util.Set;
Thomas Vachuskac67a9912018-03-06 14:37:45 -080023
Thomas Vachuska96d55b12015-05-11 08:52:03 -070024/**
25 * Basic configuration for network elements, e.g. devices, hosts. Such elements
Simon Hunteb3cf542017-02-10 13:18:41 -080026 * can have a friendly name, geo-coordinates (or grid-coordinates),
27 * logical rack coordinates and an owner entity.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070028 */
29public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> {
30
Simon Hunt53612212016-12-04 17:19:52 -080031 /**
32 * Key for friendly name.
33 */
34 public static final String NAME = "name";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070035
Simon Hunt53612212016-12-04 17:19:52 -080036 /**
37 * Key for UI type (glyph identifier).
38 */
39 public static final String UI_TYPE = "uiType";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070040
Simon Hunt53612212016-12-04 17:19:52 -080041 /**
Simon Hunteb3cf542017-02-10 13:18:41 -080042 * Key for location type (geo or grid).
43 */
44 public static final String LOC_TYPE = "locType";
45
46 /**
Simon Hunt53612212016-12-04 17:19:52 -080047 * Key for latitude.
48 */
49 public static final String LATITUDE = "latitude";
50
51 /**
52 * Key for longitude.
53 */
54 public static final String LONGITUDE = "longitude";
55
56 /**
Simon Hunteb3cf542017-02-10 13:18:41 -080057 * Key for grid Y coordinate.
58 */
Simon Huntbc30e682017-02-15 18:39:23 -080059 public static final String GRID_Y = "gridY";
Simon Hunteb3cf542017-02-10 13:18:41 -080060
Simon Huntbc30e682017-02-15 18:39:23 -080061 /**
62 * Key for grid X coordinate.
63 */
64 public static final String GRID_X = "gridX";
Simon Hunteb3cf542017-02-10 13:18:41 -080065
66 /**
Simon Hunt53612212016-12-04 17:19:52 -080067 * Key for rack address.
68 */
Thomas Vachuska36008462016-01-07 15:38:20 -080069 protected static final String RACK_ADDRESS = "rackAddress";
Simon Hunt53612212016-12-04 17:19:52 -080070
71 /**
72 * Key for owner.
73 */
Thomas Vachuska36008462016-01-07 15:38:20 -080074 protected static final String OWNER = "owner";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070075
Simon Hunt53612212016-12-04 17:19:52 -080076 /**
Thomas Vachuska3516f062018-03-19 10:12:25 -070077 * Key for roles.
78 */
79 protected static final String ROLES = "roles";
80
81 /**
Simon Hunt53612212016-12-04 17:19:52 -080082 * Threshold for detecting double value is zero.
83 */
Simon Huntf4fd2a22016-08-10 15:41:09 -070084 protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0;
Simon Hunt53612212016-12-04 17:19:52 -080085
Simon Huntf4fd2a22016-08-10 15:41:09 -070086 private static final double DEFAULT_COORD = 0.0;
Thomas Vachuskac616e172018-04-17 16:57:12 -070087
88 public static final String LOC_TYPE_GEO = "geo";
89 public static final String LOC_TYPE_GRID = "grid";
90 public static final String LOC_TYPE_NONE = "none";
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070091
Jordan Halterman83949a12017-06-21 10:35:38 -070092 private static final int NAME_MAX_LENGTH = 256;
93 private static final int UI_TYPE_MAX_LENGTH = 128;
94 private static final int LOC_TYPE_MAX_LENGTH = 32;
95 private static final int RACK_ADDRESS_MAX_LENGTH = 256;
96 private static final int OWNER_MAX_LENGTH = 128;
97
Thomas Vachuska96d55b12015-05-11 08:52:03 -070098 /**
Simon Hunt53612212016-12-04 17:19:52 -080099 * Returns friendly label for the element. If not set, returns the
100 * element identifier.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700101 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700102 * @return friendly label or element identifier itself if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700103 */
104 public String name() {
105 return get(NAME, subject.toString());
106 }
107
108 /**
109 * Sets friendly label for the element.
110 *
111 * @param name new friendly label; null to clear
112 * @return self
113 */
114 public BasicElementConfig name(String name) {
115 return (BasicElementConfig) setOrClear(NAME, name);
116 }
117
Simon Hunt1e20dae2016-10-28 11:26:26 -0700118 /**
119 * Returns the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800120 * the Topology View. If not set, null is returned.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700121 *
122 * @return the UI type
123 */
124 public String uiType() {
125 return get(UI_TYPE, null);
126 }
127
128 /**
129 * Sets the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800130 * the Topology View. Setting this to null will indicate that the
131 * default glyph image should be used for the element type.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700132 *
133 * @param uiType the UI type; null for default
134 * @return self
135 */
136 public BasicElementConfig uiType(String uiType) {
137 return (BasicElementConfig) setOrClear(UI_TYPE, uiType);
138 }
139
Simon Hunteb3cf542017-02-10 13:18:41 -0800140 /**
141 * Returns the location type (geo or grid) for the element in
Thomas Vachuskac616e172018-04-17 16:57:12 -0700142 * the Topology View. If not set, the type will be determined implicitly
143 * by latitude being set ("geo") or gridX being set ("grid");
144 * otherwise returns the default of "none".
Simon Hunteb3cf542017-02-10 13:18:41 -0800145 *
146 * @return location type (string)
147 */
148 public String locType() {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700149 String l = get(LATITUDE, null);
150 String x = get(GRID_X, null);
151 String def = l != null ? LOC_TYPE_GEO : (x != null ? LOC_TYPE_GRID : LOC_TYPE_NONE);
152 return get(LOC_TYPE, def);
Simon Hunteb3cf542017-02-10 13:18:41 -0800153 }
154
155 /**
156 * Sets the location type (geo or grid) for the element in
Thomas Vachuskac616e172018-04-17 16:57:12 -0700157 * the Topology View. If null is passed, it will default to "geo".
Simon Hunteb3cf542017-02-10 13:18:41 -0800158 *
159 * @param locType the UI type; null for default
160 * @return self
161 */
162 public BasicElementConfig locType(String locType) {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700163 String lt = Objects.equals(LOC_TYPE_GRID, locType) || Objects.equals(LOC_TYPE_GEO, locType)
164 ? locType : LOC_TYPE_NONE;
Simon Hunteb3cf542017-02-10 13:18:41 -0800165 return (BasicElementConfig) setOrClear(LOC_TYPE, lt);
166 }
167
Simon Hunt1e20dae2016-10-28 11:26:26 -0700168 private boolean doubleIsZero(double value) {
Simon Huntf4fd2a22016-08-10 15:41:09 -0700169 return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD;
170 }
171
172 /**
173 * Returns true if the geographical coordinates (latitude and longitude)
Simon Hunt1e20dae2016-10-28 11:26:26 -0700174 * are set on this element; false otherwise.
Simon Huntf4fd2a22016-08-10 15:41:09 -0700175 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700176 * @return true if geo-coordinates are set; false otherwise
Simon Huntf4fd2a22016-08-10 15:41:09 -0700177 */
178 public boolean geoCoordsSet() {
179 return !doubleIsZero(latitude()) || !doubleIsZero(longitude());
180 }
181
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700182 /**
183 * Returns element latitude.
184 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700185 * @return element latitude; 0.0 if (possibly) not set
186 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700187 */
188 public double latitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700189 return get(LATITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700190 }
191
192 /**
193 * Sets the element latitude.
194 *
195 * @param latitude new latitude; null to clear
196 * @return self
197 */
198 public BasicElementConfig latitude(Double latitude) {
199 return (BasicElementConfig) setOrClear(LATITUDE, latitude);
200 }
201
202 /**
Simon Huntf4fd2a22016-08-10 15:41:09 -0700203 * Returns element longitude.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700204 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700205 * @return element longitude; 0 if (possibly) not set
206 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700207 */
208 public double longitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700209 return get(LONGITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700210 }
211
212 /**
213 * Sets the element longitude.
214 *
215 * @param longitude new longitude; null to clear
216 * @return self
217 */
218 public BasicElementConfig longitude(Double longitude) {
219 return (BasicElementConfig) setOrClear(LONGITUDE, longitude);
220 }
221
222 /**
Simon Huntbc30e682017-02-15 18:39:23 -0800223 * Returns true if the grid coordinates (gridY and gridX) are set on
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800224 * this element, i.e. if locType is set to 'grid'; false otherwise.
Simon Hunteb3cf542017-02-10 13:18:41 -0800225 *
Simon Huntbc30e682017-02-15 18:39:23 -0800226 * @return true if grid coordinates are set; false otherwise.
Simon Hunteb3cf542017-02-10 13:18:41 -0800227 */
Simon Huntbc30e682017-02-15 18:39:23 -0800228 public boolean gridCoordsSet() {
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800229 return Objects.equals(locType(), LOC_TYPE_GRID);
Simon Hunteb3cf542017-02-10 13:18:41 -0800230 }
231
232 /**
233 * Returns element grid y-coordinate.
234 *
235 * @return element y-coordinate
236 */
237 public double gridY() {
238 return get(GRID_Y, DEFAULT_COORD);
239 }
240
241 /**
242 * Sets the element grid y-coordinate.
243 *
244 * @param y new y-coordinate; null to clear
245 * @return self
246 */
247 public BasicElementConfig gridY(Double y) {
248 return (BasicElementConfig) setOrClear(GRID_Y, y);
249 }
250
251 /**
Simon Huntbc30e682017-02-15 18:39:23 -0800252 * Returns element grid x-coordinate.
253 *
254 * @return element x-coordinate
255 */
256 public double gridX() {
257 return get(GRID_X, DEFAULT_COORD);
258 }
259
260 /**
261 * Sets the element grid x-coordinate.
262 *
263 * @param x new x-coordinate; null to clear
264 * @return self
265 */
266 public BasicElementConfig gridX(Double x) {
267 return (BasicElementConfig) setOrClear(GRID_X, x);
268 }
269
270 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700271 * Returns the element rack address.
272 *
273 * @return rack address; null if not set
274 */
275 public String rackAddress() {
276 return get(RACK_ADDRESS, null);
277 }
278
279 /**
280 * Sets element rack address.
281 *
282 * @param address new rack address; null to clear
283 * @return self
284 */
285 public BasicElementConfig rackAddress(String address) {
286 return (BasicElementConfig) setOrClear(RACK_ADDRESS, address);
287 }
288
289 /**
290 * Returns owner of the element.
291 *
292 * @return owner or null if not set
293 */
294 public String owner() {
295 return get(OWNER, null);
296 }
297
298 /**
299 * Sets the owner of the element.
300 *
301 * @param owner new owner; null to clear
302 * @return self
303 */
304 public BasicElementConfig owner(String owner) {
305 return (BasicElementConfig) setOrClear(OWNER, owner);
306 }
307
Thomas Vachuska3516f062018-03-19 10:12:25 -0700308 /**
309 * Returns set of roles assigned to the element.
310 *
311 * @return set of roles
312 */
313 public Set<String> roles() {
314 ImmutableSet.Builder<String> roles = ImmutableSet.builder();
315 if (object.has(ROLES)) {
316 ArrayNode roleNodes = (ArrayNode) object.path(ROLES);
317 roleNodes.forEach(r -> roles.add(r.asText()));
318 }
319 return roles.build();
320 }
321
322 /**
323 * Sets the roles of the element.
324 *
325 * @param roles new roles; null to clear
326 * @return self
327 */
328 public BasicElementConfig roles(Set<String> roles) {
329 return (BasicElementConfig) setOrClear(ROLES, roles);
330 }
331
Jordan Halterman83949a12017-06-21 10:35:38 -0700332 @Override
333 public boolean isValid() {
334 return isValidLength(NAME, NAME_MAX_LENGTH)
335 && isValidLength(UI_TYPE, UI_TYPE_MAX_LENGTH)
336 && isValidLength(LOC_TYPE, LOC_TYPE_MAX_LENGTH)
337 && isValidLength(RACK_ADDRESS, RACK_ADDRESS_MAX_LENGTH)
338 && isValidLength(OWNER, OWNER_MAX_LENGTH);
339 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700340}