Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 3 | * |
| 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 Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.basics; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | |
Thomas Vachuska | c67a991 | 2018-03-06 14:37:45 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 22 | import java.util.Set; |
Thomas Vachuska | c67a991 | 2018-03-06 14:37:45 -0800 | [diff] [blame] | 23 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Basic configuration for network elements, e.g. devices, hosts. Such elements |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 26 | * can have a friendly name, geo-coordinates (or grid-coordinates), |
| 27 | * logical rack coordinates and an owner entity. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 28 | */ |
| 29 | public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { |
| 30 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Key for friendly name. |
| 33 | */ |
| 34 | public static final String NAME = "name"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 35 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Key for UI type (glyph identifier). |
| 38 | */ |
| 39 | public static final String UI_TYPE = "uiType"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 40 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 41 | /** |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 42 | * Key for location type (geo or grid). |
| 43 | */ |
| 44 | public static final String LOC_TYPE = "locType"; |
| 45 | |
| 46 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 47 | * 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 57 | * Key for grid Y coordinate. |
| 58 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 59 | public static final String GRID_Y = "gridY"; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 60 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Key for grid X coordinate. |
| 63 | */ |
| 64 | public static final String GRID_X = "gridX"; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 67 | * Key for rack address. |
| 68 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 69 | protected static final String RACK_ADDRESS = "rackAddress"; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Key for owner. |
| 73 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 74 | protected static final String OWNER = "owner"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 75 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 76 | /** |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 77 | * Key for roles. |
| 78 | */ |
| 79 | protected static final String ROLES = "roles"; |
| 80 | |
| 81 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 82 | * Threshold for detecting double value is zero. |
| 83 | */ |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 84 | protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 85 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 86 | private static final double DEFAULT_COORD = 0.0; |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 87 | |
| 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 Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 91 | |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 92 | 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 98 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 99 | * Returns friendly label for the element. If not set, returns the |
| 100 | * element identifier. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 101 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 102 | * @return friendly label or element identifier itself if not set |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 103 | */ |
| 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 Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Returns the UI type (glyph image to be used) for the element in |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 120 | * the Topology View. If not set, null is returned. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 121 | * |
| 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 Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 130 | * the Topology View. Setting this to null will indicate that the |
| 131 | * default glyph image should be used for the element type. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 132 | * |
| 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Returns the location type (geo or grid) for the element in |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 142 | * 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 145 | * |
| 146 | * @return location type (string) |
| 147 | */ |
| 148 | public String locType() { |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 149 | 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Sets the location type (geo or grid) for the element in |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 157 | * the Topology View. If null is passed, it will default to "geo". |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 158 | * |
| 159 | * @param locType the UI type; null for default |
| 160 | * @return self |
| 161 | */ |
| 162 | public BasicElementConfig locType(String locType) { |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 163 | String lt = Objects.equals(LOC_TYPE_GRID, locType) || Objects.equals(LOC_TYPE_GEO, locType) |
| 164 | ? locType : LOC_TYPE_NONE; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 165 | return (BasicElementConfig) setOrClear(LOC_TYPE, lt); |
| 166 | } |
| 167 | |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 168 | private boolean doubleIsZero(double value) { |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 169 | return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns true if the geographical coordinates (latitude and longitude) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 174 | * are set on this element; false otherwise. |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 175 | * |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 176 | * @return true if geo-coordinates are set; false otherwise |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 177 | */ |
| 178 | public boolean geoCoordsSet() { |
| 179 | return !doubleIsZero(latitude()) || !doubleIsZero(longitude()); |
| 180 | } |
| 181 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Returns element latitude. |
| 184 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 185 | * @return element latitude; 0.0 if (possibly) not set |
| 186 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 187 | */ |
| 188 | public double latitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 189 | return get(LATITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 190 | } |
| 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 Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 203 | * Returns element longitude. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 204 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 205 | * @return element longitude; 0 if (possibly) not set |
| 206 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 207 | */ |
| 208 | public double longitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 209 | return get(LONGITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 210 | } |
| 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 Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 223 | * Returns true if the grid coordinates (gridY and gridX) are set on |
Thomas Vachuska | c67a991 | 2018-03-06 14:37:45 -0800 | [diff] [blame] | 224 | * this element, i.e. if locType is set to 'grid'; false otherwise. |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 225 | * |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 226 | * @return true if grid coordinates are set; false otherwise. |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 227 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 228 | public boolean gridCoordsSet() { |
Thomas Vachuska | c67a991 | 2018-03-06 14:37:45 -0800 | [diff] [blame] | 229 | return Objects.equals(locType(), LOC_TYPE_GRID); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 230 | } |
| 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 Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 252 | * 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 271 | * 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 Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 308 | /** |
| 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 Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 332 | @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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 340 | } |