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 X coordinate. |
| 52 | */ |
| 53 | public static final String GRID_X = "gridx"; |
| 54 | |
| 55 | /** |
| 56 | * Key for grid Y coordinate. |
| 57 | */ |
| 58 | public static final String GRID_Y = "gridy"; |
| 59 | |
| 60 | |
| 61 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 62 | * Key for rack address. |
| 63 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 64 | protected static final String RACK_ADDRESS = "rackAddress"; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Key for owner. |
| 68 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 69 | protected static final String OWNER = "owner"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 70 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 71 | /** |
| 72 | * Threshold for detecting double value is zero. |
| 73 | */ |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 74 | protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 75 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 76 | private static final double DEFAULT_COORD = 0.0; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame^] | 77 | private static final String LOC_TYPE_GEO = "geo"; |
| 78 | private static final String LOC_TYPE_GRID = "grid"; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 79 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 80 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 81 | * Returns friendly label for the element. If not set, returns the |
| 82 | * element identifier. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 83 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 84 | * @return friendly label or element identifier itself if not set |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 85 | */ |
| 86 | public String name() { |
| 87 | return get(NAME, subject.toString()); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets friendly label for the element. |
| 92 | * |
| 93 | * @param name new friendly label; null to clear |
| 94 | * @return self |
| 95 | */ |
| 96 | public BasicElementConfig name(String name) { |
| 97 | return (BasicElementConfig) setOrClear(NAME, name); |
| 98 | } |
| 99 | |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 100 | /** |
| 101 | * 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] | 102 | * the Topology View. If not set, null is returned. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 103 | * |
| 104 | * @return the UI type |
| 105 | */ |
| 106 | public String uiType() { |
| 107 | return get(UI_TYPE, null); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * 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] | 112 | * the Topology View. Setting this to null will indicate that the |
| 113 | * default glyph image should be used for the element type. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 114 | * |
| 115 | * @param uiType the UI type; null for default |
| 116 | * @return self |
| 117 | */ |
| 118 | public BasicElementConfig uiType(String uiType) { |
| 119 | return (BasicElementConfig) setOrClear(UI_TYPE, uiType); |
| 120 | } |
| 121 | |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame^] | 122 | /** |
| 123 | * Returns the location type (geo or grid) for the element in |
| 124 | * the Topology View. If not set, returns the default of "geo". |
| 125 | * |
| 126 | * @return location type (string) |
| 127 | */ |
| 128 | public String locType() { |
| 129 | return get(LOC_TYPE, LOC_TYPE_GEO); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Sets the location type (geo or grid) for the element in |
| 134 | * the Topology View. If null is passsed, it will default to "geo". |
| 135 | * |
| 136 | * @param locType the UI type; null for default |
| 137 | * @return self |
| 138 | */ |
| 139 | public BasicElementConfig locType(String locType) { |
| 140 | String lt = LOC_TYPE_GRID.equals(locType) ? LOC_TYPE_GRID : LOC_TYPE_GEO; |
| 141 | return (BasicElementConfig) setOrClear(LOC_TYPE, lt); |
| 142 | } |
| 143 | |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 144 | private boolean doubleIsZero(double value) { |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 145 | return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns true if the geographical coordinates (latitude and longitude) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 150 | * are set on this element; false otherwise. |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 151 | * |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 152 | * @return true if geo-coordinates are set; false otherwise |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 153 | */ |
| 154 | public boolean geoCoordsSet() { |
| 155 | return !doubleIsZero(latitude()) || !doubleIsZero(longitude()); |
| 156 | } |
| 157 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Returns element latitude. |
| 160 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 161 | * @return element latitude; 0.0 if (possibly) not set |
| 162 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 163 | */ |
| 164 | public double latitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 165 | return get(LATITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Sets the element latitude. |
| 170 | * |
| 171 | * @param latitude new latitude; null to clear |
| 172 | * @return self |
| 173 | */ |
| 174 | public BasicElementConfig latitude(Double latitude) { |
| 175 | return (BasicElementConfig) setOrClear(LATITUDE, latitude); |
| 176 | } |
| 177 | |
| 178 | /** |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 179 | * Returns element longitude. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 180 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 181 | * @return element longitude; 0 if (possibly) not set |
| 182 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 183 | */ |
| 184 | public double longitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 185 | return get(LONGITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Sets the element longitude. |
| 190 | * |
| 191 | * @param longitude new longitude; null to clear |
| 192 | * @return self |
| 193 | */ |
| 194 | public BasicElementConfig longitude(Double longitude) { |
| 195 | return (BasicElementConfig) setOrClear(LONGITUDE, longitude); |
| 196 | } |
| 197 | |
| 198 | /** |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame^] | 199 | * Returns element grid x-coordinate. |
| 200 | * |
| 201 | * @return element x-coordinate |
| 202 | */ |
| 203 | public double gridX() { |
| 204 | return get(GRID_X, DEFAULT_COORD); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Sets the element grid x-coordinate. |
| 209 | * |
| 210 | * @param x new x-coordinate; null to clear |
| 211 | * @return self |
| 212 | */ |
| 213 | public BasicElementConfig gridX(Double x) { |
| 214 | return (BasicElementConfig) setOrClear(GRID_X, x); |
| 215 | } |
| 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 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 237 | * Returns the element rack address. |
| 238 | * |
| 239 | * @return rack address; null if not set |
| 240 | */ |
| 241 | public String rackAddress() { |
| 242 | return get(RACK_ADDRESS, null); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Sets element rack address. |
| 247 | * |
| 248 | * @param address new rack address; null to clear |
| 249 | * @return self |
| 250 | */ |
| 251 | public BasicElementConfig rackAddress(String address) { |
| 252 | return (BasicElementConfig) setOrClear(RACK_ADDRESS, address); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Returns owner of the element. |
| 257 | * |
| 258 | * @return owner or null if not set |
| 259 | */ |
| 260 | public String owner() { |
| 261 | return get(OWNER, null); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Sets the owner of the element. |
| 266 | * |
| 267 | * @param owner new owner; null to clear |
| 268 | * @return self |
| 269 | */ |
| 270 | public BasicElementConfig owner(String owner) { |
| 271 | return (BasicElementConfig) setOrClear(OWNER, owner); |
| 272 | } |
| 273 | |
| 274 | } |