blob: 1cb00dc01d56e2a5073c2301851b59022ed0781e [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;
Simon Hunteb3cf542017-02-10 13:18:41 -080087 private static final String LOC_TYPE_GEO = "geo";
88 private static final String LOC_TYPE_GRID = "grid";
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070089
Jordan Halterman83949a12017-06-21 10:35:38 -070090 private static final int NAME_MAX_LENGTH = 256;
91 private static final int UI_TYPE_MAX_LENGTH = 128;
92 private static final int LOC_TYPE_MAX_LENGTH = 32;
93 private static final int RACK_ADDRESS_MAX_LENGTH = 256;
94 private static final int OWNER_MAX_LENGTH = 128;
95
Thomas Vachuska96d55b12015-05-11 08:52:03 -070096 /**
Simon Hunt53612212016-12-04 17:19:52 -080097 * Returns friendly label for the element. If not set, returns the
98 * element identifier.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070099 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700100 * @return friendly label or element identifier itself if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700101 */
102 public String name() {
103 return get(NAME, subject.toString());
104 }
105
106 /**
107 * Sets friendly label for the element.
108 *
109 * @param name new friendly label; null to clear
110 * @return self
111 */
112 public BasicElementConfig name(String name) {
113 return (BasicElementConfig) setOrClear(NAME, name);
114 }
115
Simon Hunt1e20dae2016-10-28 11:26:26 -0700116 /**
117 * Returns the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800118 * the Topology View. If not set, null is returned.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700119 *
120 * @return the UI type
121 */
122 public String uiType() {
123 return get(UI_TYPE, null);
124 }
125
126 /**
127 * Sets the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800128 * the Topology View. Setting this to null will indicate that the
129 * default glyph image should be used for the element type.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700130 *
131 * @param uiType the UI type; null for default
132 * @return self
133 */
134 public BasicElementConfig uiType(String uiType) {
135 return (BasicElementConfig) setOrClear(UI_TYPE, uiType);
136 }
137
Simon Hunteb3cf542017-02-10 13:18:41 -0800138 /**
139 * Returns the location type (geo or grid) for the element in
140 * the Topology View. If not set, returns the default of "geo".
141 *
142 * @return location type (string)
143 */
144 public String locType() {
145 return get(LOC_TYPE, LOC_TYPE_GEO);
146 }
147
148 /**
149 * Sets the location type (geo or grid) for the element in
150 * the Topology View. If null is passsed, it will default to "geo".
151 *
152 * @param locType the UI type; null for default
153 * @return self
154 */
155 public BasicElementConfig locType(String locType) {
156 String lt = LOC_TYPE_GRID.equals(locType) ? LOC_TYPE_GRID : LOC_TYPE_GEO;
157 return (BasicElementConfig) setOrClear(LOC_TYPE, lt);
158 }
159
Simon Hunt1e20dae2016-10-28 11:26:26 -0700160 private boolean doubleIsZero(double value) {
Simon Huntf4fd2a22016-08-10 15:41:09 -0700161 return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD;
162 }
163
164 /**
165 * Returns true if the geographical coordinates (latitude and longitude)
Simon Hunt1e20dae2016-10-28 11:26:26 -0700166 * are set on this element; false otherwise.
Simon Huntf4fd2a22016-08-10 15:41:09 -0700167 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700168 * @return true if geo-coordinates are set; false otherwise
Simon Huntf4fd2a22016-08-10 15:41:09 -0700169 */
170 public boolean geoCoordsSet() {
171 return !doubleIsZero(latitude()) || !doubleIsZero(longitude());
172 }
173
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700174 /**
175 * Returns element latitude.
176 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700177 * @return element latitude; 0.0 if (possibly) not set
178 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700179 */
180 public double latitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700181 return get(LATITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700182 }
183
184 /**
185 * Sets the element latitude.
186 *
187 * @param latitude new latitude; null to clear
188 * @return self
189 */
190 public BasicElementConfig latitude(Double latitude) {
191 return (BasicElementConfig) setOrClear(LATITUDE, latitude);
192 }
193
194 /**
Simon Huntf4fd2a22016-08-10 15:41:09 -0700195 * Returns element longitude.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700196 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700197 * @return element longitude; 0 if (possibly) not set
198 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700199 */
200 public double longitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700201 return get(LONGITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700202 }
203
204 /**
205 * Sets the element longitude.
206 *
207 * @param longitude new longitude; null to clear
208 * @return self
209 */
210 public BasicElementConfig longitude(Double longitude) {
211 return (BasicElementConfig) setOrClear(LONGITUDE, longitude);
212 }
213
214 /**
Simon Huntbc30e682017-02-15 18:39:23 -0800215 * Returns true if the grid coordinates (gridY and gridX) are set on
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800216 * this element, i.e. if locType is set to 'grid'; false otherwise.
Simon Hunteb3cf542017-02-10 13:18:41 -0800217 *
Simon Huntbc30e682017-02-15 18:39:23 -0800218 * @return true if grid coordinates are set; false otherwise.
Simon Hunteb3cf542017-02-10 13:18:41 -0800219 */
Simon Huntbc30e682017-02-15 18:39:23 -0800220 public boolean gridCoordsSet() {
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800221 return Objects.equals(locType(), LOC_TYPE_GRID);
Simon Hunteb3cf542017-02-10 13:18:41 -0800222 }
223
224 /**
225 * Returns element grid y-coordinate.
226 *
227 * @return element y-coordinate
228 */
229 public double gridY() {
230 return get(GRID_Y, DEFAULT_COORD);
231 }
232
233 /**
234 * Sets the element grid y-coordinate.
235 *
236 * @param y new y-coordinate; null to clear
237 * @return self
238 */
239 public BasicElementConfig gridY(Double y) {
240 return (BasicElementConfig) setOrClear(GRID_Y, y);
241 }
242
243 /**
Simon Huntbc30e682017-02-15 18:39:23 -0800244 * Returns element grid x-coordinate.
245 *
246 * @return element x-coordinate
247 */
248 public double gridX() {
249 return get(GRID_X, DEFAULT_COORD);
250 }
251
252 /**
253 * Sets the element grid x-coordinate.
254 *
255 * @param x new x-coordinate; null to clear
256 * @return self
257 */
258 public BasicElementConfig gridX(Double x) {
259 return (BasicElementConfig) setOrClear(GRID_X, x);
260 }
261
262 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700263 * Returns the element rack address.
264 *
265 * @return rack address; null if not set
266 */
267 public String rackAddress() {
268 return get(RACK_ADDRESS, null);
269 }
270
271 /**
272 * Sets element rack address.
273 *
274 * @param address new rack address; null to clear
275 * @return self
276 */
277 public BasicElementConfig rackAddress(String address) {
278 return (BasicElementConfig) setOrClear(RACK_ADDRESS, address);
279 }
280
281 /**
282 * Returns owner of the element.
283 *
284 * @return owner or null if not set
285 */
286 public String owner() {
287 return get(OWNER, null);
288 }
289
290 /**
291 * Sets the owner of the element.
292 *
293 * @param owner new owner; null to clear
294 * @return self
295 */
296 public BasicElementConfig owner(String owner) {
297 return (BasicElementConfig) setOrClear(OWNER, owner);
298 }
299
Thomas Vachuska3516f062018-03-19 10:12:25 -0700300 /**
301 * Returns set of roles assigned to the element.
302 *
303 * @return set of roles
304 */
305 public Set<String> roles() {
306 ImmutableSet.Builder<String> roles = ImmutableSet.builder();
307 if (object.has(ROLES)) {
308 ArrayNode roleNodes = (ArrayNode) object.path(ROLES);
309 roleNodes.forEach(r -> roles.add(r.asText()));
310 }
311 return roles.build();
312 }
313
314 /**
315 * Sets the roles of the element.
316 *
317 * @param roles new roles; null to clear
318 * @return self
319 */
320 public BasicElementConfig roles(Set<String> roles) {
321 return (BasicElementConfig) setOrClear(ROLES, roles);
322 }
323
Jordan Halterman83949a12017-06-21 10:35:38 -0700324 @Override
325 public boolean isValid() {
326 return isValidLength(NAME, NAME_MAX_LENGTH)
327 && isValidLength(UI_TYPE, UI_TYPE_MAX_LENGTH)
328 && isValidLength(LOC_TYPE, LOC_TYPE_MAX_LENGTH)
329 && isValidLength(RACK_ADDRESS, RACK_ADDRESS_MAX_LENGTH)
330 && isValidLength(OWNER, OWNER_MAX_LENGTH);
331 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700332}