Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.Link; |
| 22 | import org.onosproject.net.LinkKey; |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 23 | import org.onosproject.net.config.inject.DeviceInjectionConfig; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | |
| 25 | import java.time.Duration; |
| 26 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 27 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 28 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Basic configuration for network infrastructure link. |
| 31 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 32 | public final class BasicLinkConfig extends AllowedEntityConfig<LinkKey> { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 33 | |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Configuration key for {@link DeviceInjectionConfig}. |
| 36 | */ |
| 37 | public static final String CONFIG_KEY = "basic"; |
| 38 | |
| 39 | |
Simon Hunt | db450ee | 2016-01-09 13:12:11 -0800 | [diff] [blame] | 40 | public static final String TYPE = "type"; |
| 41 | public static final String METRIC = "metric"; |
| 42 | public static final String LATENCY = "latency"; |
| 43 | public static final String BANDWIDTH = "bandwidth"; |
Aaron Dunlap | 239ea5c | 2020-02-26 12:10:31 -0600 | [diff] [blame] | 44 | public static final String JITTER = "jitter"; |
| 45 | public static final String DELAY = "delay"; |
| 46 | public static final String LOSS = "loss"; |
| 47 | public static final String AVAILABILITY = "availability"; |
David Glantz | de82ada | 2020-03-19 14:43:32 -0500 | [diff] [blame] | 48 | public static final String TIER = "tier"; |
| 49 | public static final String METERED_USAGE = "meteredUsage"; |
Aaron Dunlap | 239ea5c | 2020-02-26 12:10:31 -0600 | [diff] [blame] | 50 | public static final String FLAPPING = "flapping"; |
Simon Hunt | db450ee | 2016-01-09 13:12:11 -0800 | [diff] [blame] | 51 | public static final String IS_DURABLE = "durable"; |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 52 | public static final String IS_BIDIRECTIONAL = "bidirectional"; |
David Glantz | 0843f5d | 2020-03-05 21:23:10 -0600 | [diff] [blame] | 53 | public static final String IS_METERED = "metered"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 54 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 55 | @Override |
| 56 | public boolean isValid() { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 57 | // Validate type/devices |
| 58 | type(); |
| 59 | |
Aaron Dunlap | 239ea5c | 2020-02-26 12:10:31 -0600 | [diff] [blame] | 60 | return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, JITTER, DELAY, LOSS, AVAILABILITY, FLAPPING, |
David Glantz | de82ada | 2020-03-19 14:43:32 -0500 | [diff] [blame] | 61 | IS_DURABLE, IS_BIDIRECTIONAL, IS_METERED, TIER, METERED_USAGE) && |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 62 | isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) && |
Aaron Dunlap | 239ea5c | 2020-02-26 12:10:31 -0600 | [diff] [blame] | 63 | isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) && isDecimal(JITTER, OPTIONAL) && |
| 64 | isDecimal(DELAY, OPTIONAL) && isDecimal(LOSS, OPTIONAL) && isDecimal(AVAILABILITY, OPTIONAL) && |
| 65 | isDecimal(FLAPPING, OPTIONAL) && |
David Glantz | de82ada | 2020-03-19 14:43:32 -0500 | [diff] [blame] | 66 | isNumber(TIER, OPTIONAL) && |
| 67 | isDecimal(METERED_USAGE, OPTIONAL) && |
David Glantz | 0843f5d | 2020-03-05 21:23:10 -0600 | [diff] [blame] | 68 | isBoolean(IS_BIDIRECTIONAL, OPTIONAL) && |
| 69 | isBoolean(IS_METERED, OPTIONAL); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 72 | /** |
Yuta HIGUCHI | 3142f64 | 2017-06-07 11:12:45 -0700 | [diff] [blame] | 73 | * Returns if the link type is configured. |
| 74 | * |
| 75 | * @return true if config contains link type |
| 76 | */ |
| 77 | public boolean isTypeConfigured() { |
| 78 | return hasField(TYPE); |
| 79 | } |
| 80 | |
| 81 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 82 | * Returns the link type. |
| 83 | * |
| 84 | * @return link type override |
| 85 | */ |
| 86 | public Link.Type type() { |
| 87 | return get(TYPE, Link.Type.DIRECT, Link.Type.class); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets the link type. |
| 92 | * |
| 93 | * @param type link type override |
| 94 | * @return self |
| 95 | */ |
| 96 | public BasicLinkConfig type(Link.Type type) { |
| 97 | return (BasicLinkConfig) setOrClear(TYPE, type); |
| 98 | } |
| 99 | |
| 100 | /** |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 101 | * Returns link metric value for use by |
| 102 | * {@link org.onosproject.net.topology.MetricLinkWeight} function. |
| 103 | * |
| 104 | * @return link metric; -1 if not set |
| 105 | */ |
| 106 | public double metric() { |
| 107 | return get(METRIC, -1); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sets the link metric for use by |
| 112 | * {@link org.onosproject.net.topology.MetricLinkWeight} function. |
| 113 | * |
| 114 | * @param metric new metric; null to clear |
| 115 | * @return self |
| 116 | */ |
| 117 | public BasicLinkConfig metric(Double metric) { |
| 118 | return (BasicLinkConfig) setOrClear(METRIC, metric); |
| 119 | } |
| 120 | |
| 121 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 122 | * Returns link latency in terms of nanos. |
| 123 | * |
| 124 | * @return link latency; -1 if not set |
| 125 | */ |
| 126 | public Duration latency() { |
| 127 | return Duration.ofNanos(get(LATENCY, -1)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the link latency. |
| 132 | * |
| 133 | * @param latency new latency; null to clear |
| 134 | * @return self |
| 135 | */ |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 136 | public BasicLinkConfig latency(Duration latency) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 137 | Long nanos = latency == null ? null : latency.toNanos(); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 138 | return (BasicLinkConfig) setOrClear(LATENCY, nanos); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns link bandwidth in terms of Mbps. |
| 143 | * |
| 144 | * @return link bandwidth; -1 if not set |
| 145 | */ |
| 146 | public long bandwidth() { |
| 147 | return get(BANDWIDTH, -1); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Sets the link bandwidth. |
| 152 | * |
| 153 | * @param bandwidth new bandwidth; null to clear |
| 154 | * @return self |
| 155 | */ |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 156 | public BasicLinkConfig bandwidth(Long bandwidth) { |
| 157 | return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Returns if link is durable in the network model or not. |
| 162 | * |
| 163 | * @return true for durable, false otherwise |
| 164 | */ |
| 165 | public Boolean isDurable() { |
| 166 | JsonNode res = object.path(IS_DURABLE); |
| 167 | if (res.isMissingNode()) { |
| 168 | return null; |
| 169 | } |
| 170 | return res.asBoolean(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Sets durability for this link. |
| 175 | * |
| 176 | * @param isDurable true for durable, false otherwise |
| 177 | * @return this BasicLinkConfig |
| 178 | */ |
| 179 | public BasicLinkConfig isDurable(Boolean isDurable) { |
| 180 | return (BasicLinkConfig) setOrClear(IS_DURABLE, isDurable); |
| 181 | } |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Returns if link is bidirectional in the network model or not. |
| 185 | * |
| 186 | * @return true for bidirectional, false otherwise |
| 187 | */ |
Yuta HIGUCHI | 2ad387d | 2017-06-07 10:47:20 -0700 | [diff] [blame] | 188 | public boolean isBidirectional() { |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 189 | JsonNode res = object.path(IS_BIDIRECTIONAL); |
| 190 | if (res.isMissingNode()) { |
| 191 | return true; |
| 192 | } |
| 193 | return res.asBoolean(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Sets durability for this link. |
| 198 | * |
| 199 | * @param isBidirectional true for directional, false otherwise |
| 200 | * @return this BasicLinkConfig |
| 201 | */ |
| 202 | public BasicLinkConfig isBidirectional(Boolean isBidirectional) { |
| 203 | return (BasicLinkConfig) setOrClear(IS_BIDIRECTIONAL, isBidirectional); |
| 204 | } |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Create a {@link BasicLinkConfig} for specified Device. |
| 208 | * <p> |
| 209 | * Note: created instance is not bound to NetworkConfigService, |
| 210 | * cannot use {@link #apply()}. Must be passed to the service |
| 211 | * using NetworkConfigService#applyConfig |
| 212 | * |
| 213 | * @param linkKey subject of this Config |
| 214 | */ |
| 215 | public BasicLinkConfig(LinkKey linkKey) { |
| 216 | ObjectMapper mapper = new ObjectMapper(); |
| 217 | init(linkKey, CONFIG_KEY, mapper.createObjectNode(), mapper, null); |
| 218 | } |
| 219 | |
| 220 | /** |
Aaron Dunlap | 239ea5c | 2020-02-26 12:10:31 -0600 | [diff] [blame] | 221 | * Returns link jitter in terms of seconds. |
| 222 | * |
| 223 | * @return link jitter valuer; -1 if not set |
| 224 | */ |
| 225 | public double jitter() { |
| 226 | return get(JITTER, -1.0); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sets the link jitter. |
| 231 | * |
| 232 | * @param jitter new jitter value; null to clear |
| 233 | * @return self |
| 234 | */ |
| 235 | public BasicLinkConfig jitter(Double jitter) { |
| 236 | return (BasicLinkConfig) setOrClear(JITTER, jitter); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns link delay in terms of seconds. |
| 241 | * |
| 242 | * @return link delay value; -1 if not set |
| 243 | */ |
| 244 | public double delay() { |
| 245 | return get(DELAY, -1.0); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Sets the link delay. |
| 250 | * |
| 251 | * @param delay new delay value; null to clear |
| 252 | * @return self |
| 253 | */ |
| 254 | public BasicLinkConfig delay(Double delay) { |
| 255 | return (BasicLinkConfig) setOrClear(DELAY, delay); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns link loss in terms of Percentage. |
| 260 | * |
| 261 | * @return link loss value; -1 if not set |
| 262 | */ |
| 263 | public double loss() { |
| 264 | return get(LOSS, -1.0); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Sets the link loss. |
| 269 | * |
| 270 | * @param loss new loss value; null to clear |
| 271 | * @return self |
| 272 | */ |
| 273 | public BasicLinkConfig loss(Double loss) { |
| 274 | return (BasicLinkConfig) setOrClear(LOSS, loss); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Returns link availability in terms of percentage. |
| 279 | * |
| 280 | * @return link availability value; -1 if not set |
| 281 | */ |
| 282 | public double availability() { |
| 283 | return get(AVAILABILITY, -1.0); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Sets the link availability. |
| 288 | * |
| 289 | * @param availability new availability value; null to clear |
| 290 | * @return self |
| 291 | */ |
| 292 | public BasicLinkConfig availability(Double availability) { |
| 293 | return (BasicLinkConfig) setOrClear(AVAILABILITY, availability); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Returns link flapping in terms of percentage. |
| 298 | * |
| 299 | * @return link flapping value; -1 if not set |
| 300 | */ |
| 301 | public double flapping() { |
| 302 | return get(FLAPPING, -1.0); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Sets the link flapping. |
| 307 | * |
| 308 | * @param flapping new flapping value; null to clear |
| 309 | * @return self |
| 310 | */ |
| 311 | public BasicLinkConfig flapping(Double flapping) { |
| 312 | return (BasicLinkConfig) setOrClear(FLAPPING, flapping); |
| 313 | } |
| 314 | |
| 315 | /** |
David Glantz | 0843f5d | 2020-03-05 21:23:10 -0600 | [diff] [blame] | 316 | * Returns if link is metered in the network model or not. |
| 317 | * |
| 318 | * @return true for metered, false otherwise |
| 319 | */ |
| 320 | public Boolean isMetered() { |
| 321 | JsonNode res = object.path(IS_METERED); |
| 322 | if (res.isMissingNode()) { |
| 323 | return true; |
| 324 | } |
| 325 | return res.asBoolean(); |
| 326 | } |
| 327 | |
| 328 | /** |
David Glantz | de82ada | 2020-03-19 14:43:32 -0500 | [diff] [blame] | 329 | * Sets metered flag for this link. |
David Glantz | 0843f5d | 2020-03-05 21:23:10 -0600 | [diff] [blame] | 330 | * |
| 331 | * @param isMetered true for metered, false otherwise |
| 332 | * @return this BasicLinkConfig |
| 333 | */ |
| 334 | public BasicLinkConfig isMetered(Boolean isMetered) { |
| 335 | return (BasicLinkConfig) setOrClear(IS_METERED, isMetered); |
| 336 | } |
| 337 | |
| 338 | /** |
David Glantz | de82ada | 2020-03-19 14:43:32 -0500 | [diff] [blame] | 339 | * Returns link tier. |
| 340 | * |
| 341 | * @return link tier value; -1 if not set |
| 342 | */ |
| 343 | public long tier() { |
| 344 | return get(TIER, -1); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Sets the link tier. |
| 349 | * |
| 350 | * @param tier new link tier value; null to clear |
| 351 | * @return self |
| 352 | */ |
| 353 | public BasicLinkConfig tier(Long tier) { |
| 354 | return (BasicLinkConfig) setOrClear(TIER, tier); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Returns metered link usage in terms of percentage. |
| 359 | * |
| 360 | * @return metered link usage value; -1 if not set |
| 361 | */ |
| 362 | public double meteredUsage() { |
| 363 | return get(METERED_USAGE, -1.0); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Sets the metered link usage. |
| 368 | * |
| 369 | * @param meteredUsage new metered usage value; null to clear |
| 370 | * @return self |
| 371 | */ |
| 372 | public BasicLinkConfig meteredUsage(Double meteredUsage) { |
| 373 | return (BasicLinkConfig) setOrClear(METERED_USAGE, meteredUsage); |
| 374 | } |
| 375 | |
| 376 | /** |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 377 | * Create a {@link BasicLinkConfig} instance. |
| 378 | * <p> |
| 379 | * Note: created instance needs to be initialized by #init(..) before using. |
| 380 | */ |
| 381 | public BasicLinkConfig() { |
| 382 | super(); |
| 383 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 384 | } |