blob: 1d18e805c3e9fbeba7692fcf06fa1aaafa69a0c9 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18/**
19 * Basic configuration for network elements, e.g. devices, hosts. Such elements
Simon Hunteb3cf542017-02-10 13:18:41 -080020 * can have a friendly name, geo-coordinates (or grid-coordinates),
21 * logical rack coordinates and an owner entity.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070022 */
23public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> {
24
Simon Hunt53612212016-12-04 17:19:52 -080025 /**
26 * Key for friendly name.
27 */
28 public static final String NAME = "name";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070029
Simon Hunt53612212016-12-04 17:19:52 -080030 /**
31 * Key for UI type (glyph identifier).
32 */
33 public static final String UI_TYPE = "uiType";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070034
Simon Hunt53612212016-12-04 17:19:52 -080035 /**
Simon Hunteb3cf542017-02-10 13:18:41 -080036 * Key for location type (geo or grid).
37 */
38 public static final String LOC_TYPE = "locType";
39
40 /**
Simon Hunt53612212016-12-04 17:19:52 -080041 * 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 Hunteb3cf542017-02-10 13:18:41 -080051 * Key for grid Y coordinate.
52 */
Simon Huntbc30e682017-02-15 18:39:23 -080053 public static final String GRID_Y = "gridY";
Simon Hunteb3cf542017-02-10 13:18:41 -080054
Simon Huntbc30e682017-02-15 18:39:23 -080055 /**
56 * Key for grid X coordinate.
57 */
58 public static final String GRID_X = "gridX";
Simon Hunteb3cf542017-02-10 13:18:41 -080059
60 /**
Simon Hunt53612212016-12-04 17:19:52 -080061 * Key for rack address.
62 */
Thomas Vachuska36008462016-01-07 15:38:20 -080063 protected static final String RACK_ADDRESS = "rackAddress";
Simon Hunt53612212016-12-04 17:19:52 -080064
65 /**
66 * Key for owner.
67 */
Thomas Vachuska36008462016-01-07 15:38:20 -080068 protected static final String OWNER = "owner";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070069
Simon Hunt53612212016-12-04 17:19:52 -080070 /**
71 * Threshold for detecting double value is zero.
72 */
Simon Huntf4fd2a22016-08-10 15:41:09 -070073 protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0;
Simon Hunt53612212016-12-04 17:19:52 -080074
Simon Huntf4fd2a22016-08-10 15:41:09 -070075 private static final double DEFAULT_COORD = 0.0;
Simon Hunteb3cf542017-02-10 13:18:41 -080076 private static final String LOC_TYPE_GEO = "geo";
77 private static final String LOC_TYPE_GRID = "grid";
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070078
Thomas Vachuska96d55b12015-05-11 08:52:03 -070079 /**
Simon Hunt53612212016-12-04 17:19:52 -080080 * Returns friendly label for the element. If not set, returns the
81 * element identifier.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070082 *
Simon Huntf4fd2a22016-08-10 15:41:09 -070083 * @return friendly label or element identifier itself if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -070084 */
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 Hunt1e20dae2016-10-28 11:26:26 -070099 /**
100 * Returns the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800101 * the Topology View. If not set, null is returned.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700102 *
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 Hunt53612212016-12-04 17:19:52 -0800111 * the Topology View. Setting this to null will indicate that the
112 * default glyph image should be used for the element type.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700113 *
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 Hunteb3cf542017-02-10 13:18:41 -0800121 /**
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 Hunt1e20dae2016-10-28 11:26:26 -0700143 private boolean doubleIsZero(double value) {
Simon Huntf4fd2a22016-08-10 15:41:09 -0700144 return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD;
145 }
146
147 /**
148 * Returns true if the geographical coordinates (latitude and longitude)
Simon Hunt1e20dae2016-10-28 11:26:26 -0700149 * are set on this element; false otherwise.
Simon Huntf4fd2a22016-08-10 15:41:09 -0700150 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700151 * @return true if geo-coordinates are set; false otherwise
Simon Huntf4fd2a22016-08-10 15:41:09 -0700152 */
153 public boolean geoCoordsSet() {
154 return !doubleIsZero(latitude()) || !doubleIsZero(longitude());
155 }
156
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700157 /**
158 * Returns element latitude.
159 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700160 * @return element latitude; 0.0 if (possibly) not set
161 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700162 */
163 public double latitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700164 return get(LATITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700165 }
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 Huntf4fd2a22016-08-10 15:41:09 -0700178 * Returns element longitude.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700179 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700180 * @return element longitude; 0 if (possibly) not set
181 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700182 */
183 public double longitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700184 return get(LONGITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700185 }
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 Huntbc30e682017-02-15 18:39:23 -0800198 * 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 Hunteb3cf542017-02-10 13:18:41 -0800204 *
Simon Huntbc30e682017-02-15 18:39:23 -0800205 * @return true if grid coordinates are set; false otherwise.
Simon Hunteb3cf542017-02-10 13:18:41 -0800206 */
Simon Huntbc30e682017-02-15 18:39:23 -0800207 public boolean gridCoordsSet() {
208 return !doubleIsZero(gridY()) || !doubleIsZero(gridX());
Simon Hunteb3cf542017-02-10 13:18:41 -0800209 }
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 Huntbc30e682017-02-15 18:39:23 -0800231 * 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 Vachuska96d55b12015-05-11 08:52:03 -0700250 * 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}