blob: 295995f5df34d71d3a4d46c6619b687b5c231d3f [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
20 * can have a friendly name, geo-coordinates, logical rack coordinates and
21 * an owner entity.
22 */
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 /**
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 Vachuska36008462016-01-07 15:38:20 -080048 protected static final String RACK_ADDRESS = "rackAddress";
Simon Hunt53612212016-12-04 17:19:52 -080049
50 /**
51 * Key for owner.
52 */
Thomas Vachuska36008462016-01-07 15:38:20 -080053 protected static final String OWNER = "owner";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070054
Simon Hunt53612212016-12-04 17:19:52 -080055 /**
56 * Threshold for detecting double value is zero.
57 */
Simon Huntf4fd2a22016-08-10 15:41:09 -070058 protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0;
Simon Hunt53612212016-12-04 17:19:52 -080059
Simon Huntf4fd2a22016-08-10 15:41:09 -070060 private static final double DEFAULT_COORD = 0.0;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070061
Thomas Vachuska96d55b12015-05-11 08:52:03 -070062 /**
Simon Hunt53612212016-12-04 17:19:52 -080063 * Returns friendly label for the element. If not set, returns the
64 * element identifier.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070065 *
Simon Huntf4fd2a22016-08-10 15:41:09 -070066 * @return friendly label or element identifier itself if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -070067 */
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 Hunt1e20dae2016-10-28 11:26:26 -070082 /**
83 * Returns the UI type (glyph image to be used) for the element in
Simon Hunt53612212016-12-04 17:19:52 -080084 * the Topology View. If not set, null is returned.
Simon Hunt1e20dae2016-10-28 11:26:26 -070085 *
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 Hunt53612212016-12-04 17:19:52 -080094 * the Topology View. Setting this to null will indicate that the
95 * default glyph image should be used for the element type.
Simon Hunt1e20dae2016-10-28 11:26:26 -070096 *
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 Huntf4fd2a22016-08-10 15:41:09 -0700105 return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD;
106 }
107
108 /**
109 * Returns true if the geographical coordinates (latitude and longitude)
Simon Hunt1e20dae2016-10-28 11:26:26 -0700110 * are set on this element; false otherwise.
Simon Huntf4fd2a22016-08-10 15:41:09 -0700111 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700112 * @return true if geo-coordinates are set; false otherwise
Simon Huntf4fd2a22016-08-10 15:41:09 -0700113 */
114 public boolean geoCoordsSet() {
115 return !doubleIsZero(latitude()) || !doubleIsZero(longitude());
116 }
117
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700118 /**
119 * Returns element latitude.
120 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700121 * @return element latitude; 0.0 if (possibly) not set
122 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700123 */
124 public double latitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700125 return get(LATITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700126 }
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 Huntf4fd2a22016-08-10 15:41:09 -0700139 * Returns element longitude.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700140 *
Simon Huntf4fd2a22016-08-10 15:41:09 -0700141 * @return element longitude; 0 if (possibly) not set
142 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700143 */
144 public double longitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700145 return get(LONGITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700146 }
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}