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