blob: 8ee0c1b16e06076992bdd430f4068c53f5f2fd0f [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";
48 public static final String FLAPPING = "flapping";
Simon Huntdb450ee2016-01-09 13:12:11 -080049 public static final String IS_DURABLE = "durable";
Marc De Leenheerec551d32017-03-10 15:48:18 -080050 public static final String IS_BIDIRECTIONAL = "bidirectional";
David Glantz0843f5d2020-03-05 21:23:10 -060051 public static final String IS_METERED = "metered";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070052
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080053 @Override
54 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070055 // Validate type/devices
56 type();
57
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060058 return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, JITTER, DELAY, LOSS, AVAILABILITY, FLAPPING,
David Glantz0843f5d2020-03-05 21:23:10 -060059 IS_DURABLE, IS_BIDIRECTIONAL, IS_METERED) &&
Thomas Vachuska36008462016-01-07 15:38:20 -080060 isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) &&
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060061 isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) && isDecimal(JITTER, OPTIONAL) &&
62 isDecimal(DELAY, OPTIONAL) && isDecimal(LOSS, OPTIONAL) && isDecimal(AVAILABILITY, OPTIONAL) &&
63 isDecimal(FLAPPING, OPTIONAL) &&
David Glantz0843f5d2020-03-05 21:23:10 -060064 isBoolean(IS_BIDIRECTIONAL, OPTIONAL) &&
65 isBoolean(IS_METERED, OPTIONAL);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080066 }
67
Thomas Vachuska96d55b12015-05-11 08:52:03 -070068 /**
Yuta HIGUCHI3142f642017-06-07 11:12:45 -070069 * 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 Vachuska96d55b12015-05-11 08:52:03 -070078 * 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 Vachuska41fe1ec2015-12-03 23:17:02 -080097 * 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 Vachuska96d55b12015-05-11 08:52:03 -0700118 * 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 Koshibecc260d22015-08-04 17:13:38 -0700132 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700133 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700134 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700135 }
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 Koshibecc260d22015-08-04 17:13:38 -0700152 public BasicLinkConfig bandwidth(Long bandwidth) {
153 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700154 }
155
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700156 /**
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 Leenheerec551d32017-03-10 15:48:18 -0800178
179 /**
180 * Returns if link is bidirectional in the network model or not.
181 *
182 * @return true for bidirectional, false otherwise
183 */
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700184 public boolean isBidirectional() {
Marc De Leenheerec551d32017-03-10 15:48:18 -0800185 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 HIGUCHI21033042017-05-04 17:58:24 -0700201
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 Dunlap239ea5c2020-02-26 12:10:31 -0600217 * 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 Glantz0843f5d2020-03-05 21:23:10 -0600312 * 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 HIGUCHI21033042017-05-04 17:58:24 -0700335 * 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 Vachuska96d55b12015-05-11 08:52:03 -0700342}