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 |
| 20 | * can have a friendly name, geo-coordinates, logical rack coordinates and |
| 21 | * an owner entity. |
| 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 | /** |
| 36 | * Key for latitude. |
| 37 | */ |
| 38 | public static final String LATITUDE = "latitude"; |
| 39 | |
| 40 | /** |
| 41 | * Key for longitude. |
| 42 | */ |
| 43 | public static final String LONGITUDE = "longitude"; |
| 44 | |
| 45 | /** |
| 46 | * Key for rack address. |
| 47 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 48 | protected static final String RACK_ADDRESS = "rackAddress"; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Key for owner. |
| 52 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 53 | protected static final String OWNER = "owner"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 54 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Threshold for detecting double value is zero. |
| 57 | */ |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 58 | protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 59 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 60 | private static final double DEFAULT_COORD = 0.0; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 61 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 62 | /** |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 63 | * Returns friendly label for the element. If not set, returns the |
| 64 | * element identifier. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 65 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 66 | * @return friendly label or element identifier itself if not set |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 67 | */ |
| 68 | public String name() { |
| 69 | return get(NAME, subject.toString()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets friendly label for the element. |
| 74 | * |
| 75 | * @param name new friendly label; null to clear |
| 76 | * @return self |
| 77 | */ |
| 78 | public BasicElementConfig name(String name) { |
| 79 | return (BasicElementConfig) setOrClear(NAME, name); |
| 80 | } |
| 81 | |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 82 | /** |
| 83 | * 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] | 84 | * the Topology View. If not set, null is returned. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 85 | * |
| 86 | * @return the UI type |
| 87 | */ |
| 88 | public String uiType() { |
| 89 | return get(UI_TYPE, null); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * 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] | 94 | * the Topology View. Setting this to null will indicate that the |
| 95 | * default glyph image should be used for the element type. |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 96 | * |
| 97 | * @param uiType the UI type; null for default |
| 98 | * @return self |
| 99 | */ |
| 100 | public BasicElementConfig uiType(String uiType) { |
| 101 | return (BasicElementConfig) setOrClear(UI_TYPE, uiType); |
| 102 | } |
| 103 | |
| 104 | private boolean doubleIsZero(double value) { |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 105 | return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns true if the geographical coordinates (latitude and longitude) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 110 | * are set on this element; false otherwise. |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 111 | * |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 112 | * @return true if geo-coordinates are set; false otherwise |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 113 | */ |
| 114 | public boolean geoCoordsSet() { |
| 115 | return !doubleIsZero(latitude()) || !doubleIsZero(longitude()); |
| 116 | } |
| 117 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Returns element latitude. |
| 120 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 121 | * @return element latitude; 0.0 if (possibly) not set |
| 122 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 123 | */ |
| 124 | public double latitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 125 | return get(LATITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sets the element latitude. |
| 130 | * |
| 131 | * @param latitude new latitude; null to clear |
| 132 | * @return self |
| 133 | */ |
| 134 | public BasicElementConfig latitude(Double latitude) { |
| 135 | return (BasicElementConfig) setOrClear(LATITUDE, latitude); |
| 136 | } |
| 137 | |
| 138 | /** |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 139 | * Returns element longitude. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 140 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 141 | * @return element longitude; 0 if (possibly) not set |
| 142 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 143 | */ |
| 144 | public double longitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 145 | return get(LONGITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets the element longitude. |
| 150 | * |
| 151 | * @param longitude new longitude; null to clear |
| 152 | * @return self |
| 153 | */ |
| 154 | public BasicElementConfig longitude(Double longitude) { |
| 155 | return (BasicElementConfig) setOrClear(LONGITUDE, longitude); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the element rack address. |
| 160 | * |
| 161 | * @return rack address; null if not set |
| 162 | */ |
| 163 | public String rackAddress() { |
| 164 | return get(RACK_ADDRESS, null); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets element rack address. |
| 169 | * |
| 170 | * @param address new rack address; null to clear |
| 171 | * @return self |
| 172 | */ |
| 173 | public BasicElementConfig rackAddress(String address) { |
| 174 | return (BasicElementConfig) setOrClear(RACK_ADDRESS, address); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns owner of the element. |
| 179 | * |
| 180 | * @return owner or null if not set |
| 181 | */ |
| 182 | public String owner() { |
| 183 | return get(OWNER, null); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Sets the owner of the element. |
| 188 | * |
| 189 | * @param owner new owner; null to clear |
| 190 | * @return self |
| 191 | */ |
| 192 | public BasicElementConfig owner(String owner) { |
| 193 | return (BasicElementConfig) setOrClear(OWNER, owner); |
| 194 | } |
| 195 | |
| 196 | } |