blob: e6a5c85f8af61b94be057d184688f6b8a26e76c0 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080018import com.fasterxml.jackson.databind.JsonNode;
Yuta HIGUCHI21033042017-05-04 17:58:24 -070019import com.fasterxml.jackson.databind.ObjectMapper;
20
Thomas Vachuska96d55b12015-05-11 08:52:03 -070021import org.onosproject.net.Link;
22import org.onosproject.net.LinkKey;
Yuta HIGUCHI21033042017-05-04 17:58:24 -070023import org.onosproject.net.config.inject.DeviceInjectionConfig;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070024
25import java.time.Duration;
26
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080027import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
28
Thomas Vachuska96d55b12015-05-11 08:52:03 -070029/**
30 * Basic configuration for network infrastructure link.
31 */
Thomas Vachuska36008462016-01-07 15:38:20 -080032public final class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -070033
Yuta HIGUCHI21033042017-05-04 17:58:24 -070034 /**
35 * Configuration key for {@link DeviceInjectionConfig}.
36 */
37 public static final String CONFIG_KEY = "basic";
38
39
Simon Huntdb450ee2016-01-09 13:12:11 -080040 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 Dunlap239ea5c2020-02-26 12:10:31 -060044 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 Glantzde82ada2020-03-19 14:43:32 -050048 public static final String TIER = "tier";
49 public static final String METERED_USAGE = "meteredUsage";
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060050 public static final String FLAPPING = "flapping";
Simon Huntdb450ee2016-01-09 13:12:11 -080051 public static final String IS_DURABLE = "durable";
Marc De Leenheerec551d32017-03-10 15:48:18 -080052 public static final String IS_BIDIRECTIONAL = "bidirectional";
David Glantz0843f5d2020-03-05 21:23:10 -060053 public static final String IS_METERED = "metered";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070054
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080055 @Override
56 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070057 // Validate type/devices
58 type();
59
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060060 return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, JITTER, DELAY, LOSS, AVAILABILITY, FLAPPING,
David Glantzde82ada2020-03-19 14:43:32 -050061 IS_DURABLE, IS_BIDIRECTIONAL, IS_METERED, TIER, METERED_USAGE) &&
Thomas Vachuska36008462016-01-07 15:38:20 -080062 isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) &&
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060063 isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) && isDecimal(JITTER, OPTIONAL) &&
64 isDecimal(DELAY, OPTIONAL) && isDecimal(LOSS, OPTIONAL) && isDecimal(AVAILABILITY, OPTIONAL) &&
65 isDecimal(FLAPPING, OPTIONAL) &&
David Glantzde82ada2020-03-19 14:43:32 -050066 isNumber(TIER, OPTIONAL) &&
67 isDecimal(METERED_USAGE, OPTIONAL) &&
David Glantz0843f5d2020-03-05 21:23:10 -060068 isBoolean(IS_BIDIRECTIONAL, OPTIONAL) &&
69 isBoolean(IS_METERED, OPTIONAL);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080070 }
71
Thomas Vachuska96d55b12015-05-11 08:52:03 -070072 /**
Yuta HIGUCHI3142f642017-06-07 11:12:45 -070073 * 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 Vachuska96d55b12015-05-11 08:52:03 -070082 * 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 Vachuska41fe1ec2015-12-03 23:17:02 -0800101 * 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 Vachuska96d55b12015-05-11 08:52:03 -0700122 * 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 Koshibecc260d22015-08-04 17:13:38 -0700136 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700137 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700138 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700139 }
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 Koshibecc260d22015-08-04 17:13:38 -0700156 public BasicLinkConfig bandwidth(Long bandwidth) {
157 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700158 }
159
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700160 /**
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 Leenheerec551d32017-03-10 15:48:18 -0800182
183 /**
184 * Returns if link is bidirectional in the network model or not.
185 *
186 * @return true for bidirectional, false otherwise
187 */
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700188 public boolean isBidirectional() {
Marc De Leenheerec551d32017-03-10 15:48:18 -0800189 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 HIGUCHI21033042017-05-04 17:58:24 -0700205
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 Dunlap239ea5c2020-02-26 12:10:31 -0600221 * 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 Glantz0843f5d2020-03-05 21:23:10 -0600316 * 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 Glantzde82ada2020-03-19 14:43:32 -0500329 * Sets metered flag for this link.
David Glantz0843f5d2020-03-05 21:23:10 -0600330 *
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 Glantzde82ada2020-03-19 14:43:32 -0500339 * 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 HIGUCHI21033042017-05-04 17:58:24 -0700377 * 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 Vachuska96d55b12015-05-11 08:52:03 -0700384}