blob: 65a27835d8faeaf625418b335722f20f969fec50 [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 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 Hunt53612212016-12-04 17:19:52 -080062 * Key for rack address.
63 */
Thomas Vachuska36008462016-01-07 15:38:20 -080064 protected static final String RACK_ADDRESS = "rackAddress";
Simon Hunt53612212016-12-04 17:19:52 -080065
66 /**
67 * Key for owner.
68 */
Thomas Vachuska36008462016-01-07 15:38:20 -080069 protected static final String OWNER = "owner";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070070
Simon Hunt53612212016-12-04 17:19:52 -080071 /**
72 * Threshold for detecting double value is zero.
73 */
Simon Huntf4fd2a22016-08-10 15:41:09 -070074 protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0;
Simon Hunt53612212016-12-04 17:19:52 -080075
Simon Huntf4fd2a22016-08-10 15:41:09 -070076 private static final double DEFAULT_COORD = 0.0;
Simon Hunteb3cf542017-02-10 13:18:41 -080077 private static final String LOC_TYPE_GEO = "geo";
78 private static final String LOC_TYPE_GRID = "grid";
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070079
Thomas Vachuska96d55b12015-05-11 08:52:03 -070080 /**
Simon Hunt53612212016-12-04 17:19:52 -080081 * Returns friendly label for the element. If not set, returns the
82 * element identifier.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070083 *
Simon Huntf4fd2a22016-08-10 15:41:09 -070084 * @return friendly label or element identifier itself if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -070085 */
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 Hunt1e20dae2016-10-28 11:26:26 -0700100 /**
101 * Returns the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -0800102 * the Topology View. If not set, null is returned.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700103 *
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 Hunt53612212016-12-04 17:19:52 -0800112 * the Topology View. Setting this to null will indicate that the
113 * default glyph image should be used for the element type.
Simon Hunt1e20dae2016-10-28 11:26:26 -0700114 *
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 Hunteb3cf542017-02-10 13:18:41 -0800122 /**
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 Hunt1e20dae2016-10-28 11:26:26 -0700144 private boolean doubleIsZero(double value) {
Simon Huntf4fd2a22016-08-10 15:41:09 -0700145 return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD;
146 }
147
148 /**
149 * Returns true if the geographical coordinates (latitude and longitude)
Simon Hunt1e20dae2016-10-28 11:26:26 -0700150 * are set on this element; false otherwise.
Simon Huntf4fd2a22016-08-10 15:41:09 -0700151 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700152 * @return true if geo-coordinates are set; false otherwise
Simon Huntf4fd2a22016-08-10 15:41:09 -0700153 */
154 public boolean geoCoordsSet() {
155 return !doubleIsZero(latitude()) || !doubleIsZero(longitude());
156 }
157
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700158 /**
159 * Returns element latitude.
160 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700161 * @return element latitude; 0.0 if (possibly) not set
162 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700163 */
164 public double latitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700165 return get(LATITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700166 }
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 Huntf4fd2a22016-08-10 15:41:09 -0700179 * Returns element longitude.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700180 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700181 * @return element longitude; 0 if (possibly) not set
182 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700183 */
184 public double longitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700185 return get(LONGITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700186 }
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 Hunteb3cf542017-02-10 13:18:41 -0800199 * 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 Vachuska96d55b12015-05-11 08:52:03 -0700237 * 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}