Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 18 | /** |
| 19 | * Basic configuration for network elements, e.g. devices, hosts. Such elements |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 20 | * can have a friendly name, geo-coordinates (or grid-coordinates), |
| 21 | * logical rack coordinates and an owner entity. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 22 | */ |
| 23 | public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { |
| 24 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 25 | /** |
| 26 | * Key for friendly name. |
| 27 | */ |
| 28 | public static final String NAME = "name"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 29 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Key for UI type (glyph identifier). |
| 32 | */ |
| 33 | public static final String UI_TYPE = "uiType"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 34 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 35 | /** |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 36 | * Key for location type (geo or grid). |
| 37 | */ |
| 38 | public static final String LOC_TYPE = "locType"; |
| 39 | |
| 40 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 41 | * 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 51 | * Key for grid Y coordinate. |
| 52 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 53 | public static final String GRID_Y = "gridY"; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 54 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Key for grid X coordinate. |
| 57 | */ |
| 58 | public static final String GRID_X = "gridX"; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 61 | * Key for rack address. |
| 62 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 63 | protected static final String RACK_ADDRESS = "rackAddress"; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Key for owner. |
| 67 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 68 | protected static final String OWNER = "owner"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 69 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Threshold for detecting double value is zero. |
| 72 | */ |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 73 | protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 74 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 75 | private static final double DEFAULT_COORD = 0.0; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 76 | private static final String LOC_TYPE_GEO = "geo"; |
| 77 | private static final String LOC_TYPE_GRID = "grid"; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 78 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 79 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 80 | * Returns friendly label for the element. If not set, returns the |
| 81 | * element identifier. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 82 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 83 | * @return friendly label or element identifier itself if not set |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 84 | */ |
| 85 | public String name() { |
| 86 | return get(NAME, subject.toString()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets friendly label for the element. |
| 91 | * |
| 92 | * @param name new friendly label; null to clear |
| 93 | * @return self |
| 94 | */ |
| 95 | public BasicElementConfig name(String name) { |
| 96 | return (BasicElementConfig) setOrClear(NAME, name); |
| 97 | } |
| 98 | |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 99 | /** |
| 100 | * 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] | 101 | * the Topology View. If not set, null is returned. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 102 | * |
| 103 | * @return the UI type |
| 104 | */ |
| 105 | public String uiType() { |
| 106 | return get(UI_TYPE, null); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * 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] | 111 | * the Topology View. Setting this to null will indicate that the |
| 112 | * default glyph image should be used for the element type. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 113 | * |
| 114 | * @param uiType the UI type; null for default |
| 115 | * @return self |
| 116 | */ |
| 117 | public BasicElementConfig uiType(String uiType) { |
| 118 | return (BasicElementConfig) setOrClear(UI_TYPE, uiType); |
| 119 | } |
| 120 | |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Returns the location type (geo or grid) for the element in |
| 123 | * the Topology View. If not set, returns the default of "geo". |
| 124 | * |
| 125 | * @return location type (string) |
| 126 | */ |
| 127 | public String locType() { |
| 128 | return get(LOC_TYPE, LOC_TYPE_GEO); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sets the location type (geo or grid) for the element in |
| 133 | * the Topology View. If null is passsed, it will default to "geo". |
| 134 | * |
| 135 | * @param locType the UI type; null for default |
| 136 | * @return self |
| 137 | */ |
| 138 | public BasicElementConfig locType(String locType) { |
| 139 | String lt = LOC_TYPE_GRID.equals(locType) ? LOC_TYPE_GRID : LOC_TYPE_GEO; |
| 140 | return (BasicElementConfig) setOrClear(LOC_TYPE, lt); |
| 141 | } |
| 142 | |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 143 | private boolean doubleIsZero(double value) { |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 144 | return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns true if the geographical coordinates (latitude and longitude) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 149 | * are set on this element; false otherwise. |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 150 | * |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 151 | * @return true if geo-coordinates are set; false otherwise |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 152 | */ |
| 153 | public boolean geoCoordsSet() { |
| 154 | return !doubleIsZero(latitude()) || !doubleIsZero(longitude()); |
| 155 | } |
| 156 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Returns element latitude. |
| 159 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 160 | * @return element latitude; 0.0 if (possibly) not set |
| 161 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 162 | */ |
| 163 | public double latitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 164 | return get(LATITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets the element latitude. |
| 169 | * |
| 170 | * @param latitude new latitude; null to clear |
| 171 | * @return self |
| 172 | */ |
| 173 | public BasicElementConfig latitude(Double latitude) { |
| 174 | return (BasicElementConfig) setOrClear(LATITUDE, latitude); |
| 175 | } |
| 176 | |
| 177 | /** |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 178 | * Returns element longitude. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 179 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 180 | * @return element longitude; 0 if (possibly) not set |
| 181 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 182 | */ |
| 183 | public double longitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 184 | return get(LONGITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sets the element longitude. |
| 189 | * |
| 190 | * @param longitude new longitude; null to clear |
| 191 | * @return self |
| 192 | */ |
| 193 | public BasicElementConfig longitude(Double longitude) { |
| 194 | return (BasicElementConfig) setOrClear(LONGITUDE, longitude); |
| 195 | } |
| 196 | |
| 197 | /** |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 198 | * Returns true if the grid coordinates (gridY and gridX) are set on |
| 199 | * this element; false otherwise. |
| 200 | * <p> |
| 201 | * It is assumed that elements will not be placed at {@code (0,0)}. |
| 202 | * If you really need to position the element there, consider setting the |
| 203 | * coordinates to something like {@code (0.000001, 0.000001)} instead. |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 204 | * |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 205 | * @return true if grid coordinates are set; false otherwise. |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 206 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 207 | public boolean gridCoordsSet() { |
| 208 | return !doubleIsZero(gridY()) || !doubleIsZero(gridX()); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Returns element grid y-coordinate. |
| 213 | * |
| 214 | * @return element y-coordinate |
| 215 | */ |
| 216 | public double gridY() { |
| 217 | return get(GRID_Y, DEFAULT_COORD); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Sets the element grid y-coordinate. |
| 222 | * |
| 223 | * @param y new y-coordinate; null to clear |
| 224 | * @return self |
| 225 | */ |
| 226 | public BasicElementConfig gridY(Double y) { |
| 227 | return (BasicElementConfig) setOrClear(GRID_Y, y); |
| 228 | } |
| 229 | |
| 230 | /** |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 231 | * Returns element grid x-coordinate. |
| 232 | * |
| 233 | * @return element x-coordinate |
| 234 | */ |
| 235 | public double gridX() { |
| 236 | return get(GRID_X, DEFAULT_COORD); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Sets the element grid x-coordinate. |
| 241 | * |
| 242 | * @param x new x-coordinate; null to clear |
| 243 | * @return self |
| 244 | */ |
| 245 | public BasicElementConfig gridX(Double x) { |
| 246 | return (BasicElementConfig) setOrClear(GRID_X, x); |
| 247 | } |
| 248 | |
| 249 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 250 | * Returns the element rack address. |
| 251 | * |
| 252 | * @return rack address; null if not set |
| 253 | */ |
| 254 | public String rackAddress() { |
| 255 | return get(RACK_ADDRESS, null); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Sets element rack address. |
| 260 | * |
| 261 | * @param address new rack address; null to clear |
| 262 | * @return self |
| 263 | */ |
| 264 | public BasicElementConfig rackAddress(String address) { |
| 265 | return (BasicElementConfig) setOrClear(RACK_ADDRESS, address); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns owner of the element. |
| 270 | * |
| 271 | * @return owner or null if not set |
| 272 | */ |
| 273 | public String owner() { |
| 274 | return get(OWNER, null); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Sets the owner of the element. |
| 279 | * |
| 280 | * @param owner new owner; null to clear |
| 281 | * @return self |
| 282 | */ |
| 283 | public BasicElementConfig owner(String owner) { |
| 284 | return (BasicElementConfig) setOrClear(OWNER, owner); |
| 285 | } |
| 286 | |
| 287 | } |