blob: ff4edb0715bd2c44080169b784cf6d803a98a900 [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";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070051
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080052 @Override
53 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070054 // Validate type/devices
55 type();
56
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060057 return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, JITTER, DELAY, LOSS, AVAILABILITY, FLAPPING,
58 IS_DURABLE, IS_BIDIRECTIONAL) &&
Thomas Vachuska36008462016-01-07 15:38:20 -080059 isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) &&
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060060 isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) && isDecimal(JITTER, OPTIONAL) &&
61 isDecimal(DELAY, OPTIONAL) && isDecimal(LOSS, OPTIONAL) && isDecimal(AVAILABILITY, OPTIONAL) &&
62 isDecimal(FLAPPING, OPTIONAL) &&
Marc De Leenheerec551d32017-03-10 15:48:18 -080063 isBoolean(IS_BIDIRECTIONAL, OPTIONAL);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080064 }
65
Thomas Vachuska96d55b12015-05-11 08:52:03 -070066 /**
Yuta HIGUCHI3142f642017-06-07 11:12:45 -070067 * Returns if the link type is configured.
68 *
69 * @return true if config contains link type
70 */
71 public boolean isTypeConfigured() {
72 return hasField(TYPE);
73 }
74
75 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -070076 * Returns the link type.
77 *
78 * @return link type override
79 */
80 public Link.Type type() {
81 return get(TYPE, Link.Type.DIRECT, Link.Type.class);
82 }
83
84 /**
85 * Sets the link type.
86 *
87 * @param type link type override
88 * @return self
89 */
90 public BasicLinkConfig type(Link.Type type) {
91 return (BasicLinkConfig) setOrClear(TYPE, type);
92 }
93
94 /**
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080095 * Returns link metric value for use by
96 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
97 *
98 * @return link metric; -1 if not set
99 */
100 public double metric() {
101 return get(METRIC, -1);
102 }
103
104 /**
105 * Sets the link metric for use by
106 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
107 *
108 * @param metric new metric; null to clear
109 * @return self
110 */
111 public BasicLinkConfig metric(Double metric) {
112 return (BasicLinkConfig) setOrClear(METRIC, metric);
113 }
114
115 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700116 * Returns link latency in terms of nanos.
117 *
118 * @return link latency; -1 if not set
119 */
120 public Duration latency() {
121 return Duration.ofNanos(get(LATENCY, -1));
122 }
123
124 /**
125 * Sets the link latency.
126 *
127 * @param latency new latency; null to clear
128 * @return self
129 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700130 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700131 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700132 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700133 }
134
135 /**
136 * Returns link bandwidth in terms of Mbps.
137 *
138 * @return link bandwidth; -1 if not set
139 */
140 public long bandwidth() {
141 return get(BANDWIDTH, -1);
142 }
143
144 /**
145 * Sets the link bandwidth.
146 *
147 * @param bandwidth new bandwidth; null to clear
148 * @return self
149 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700150 public BasicLinkConfig bandwidth(Long bandwidth) {
151 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700152 }
153
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700154 /**
155 * Returns if link is durable in the network model or not.
156 *
157 * @return true for durable, false otherwise
158 */
159 public Boolean isDurable() {
160 JsonNode res = object.path(IS_DURABLE);
161 if (res.isMissingNode()) {
162 return null;
163 }
164 return res.asBoolean();
165 }
166
167 /**
168 * Sets durability for this link.
169 *
170 * @param isDurable true for durable, false otherwise
171 * @return this BasicLinkConfig
172 */
173 public BasicLinkConfig isDurable(Boolean isDurable) {
174 return (BasicLinkConfig) setOrClear(IS_DURABLE, isDurable);
175 }
Marc De Leenheerec551d32017-03-10 15:48:18 -0800176
177 /**
178 * Returns if link is bidirectional in the network model or not.
179 *
180 * @return true for bidirectional, false otherwise
181 */
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700182 public boolean isBidirectional() {
Marc De Leenheerec551d32017-03-10 15:48:18 -0800183 JsonNode res = object.path(IS_BIDIRECTIONAL);
184 if (res.isMissingNode()) {
185 return true;
186 }
187 return res.asBoolean();
188 }
189
190 /**
191 * Sets durability for this link.
192 *
193 * @param isBidirectional true for directional, false otherwise
194 * @return this BasicLinkConfig
195 */
196 public BasicLinkConfig isBidirectional(Boolean isBidirectional) {
197 return (BasicLinkConfig) setOrClear(IS_BIDIRECTIONAL, isBidirectional);
198 }
Yuta HIGUCHI21033042017-05-04 17:58:24 -0700199
200 /**
201 * Create a {@link BasicLinkConfig} for specified Device.
202 * <p>
203 * Note: created instance is not bound to NetworkConfigService,
204 * cannot use {@link #apply()}. Must be passed to the service
205 * using NetworkConfigService#applyConfig
206 *
207 * @param linkKey subject of this Config
208 */
209 public BasicLinkConfig(LinkKey linkKey) {
210 ObjectMapper mapper = new ObjectMapper();
211 init(linkKey, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
212 }
213
214 /**
Aaron Dunlap239ea5c2020-02-26 12:10:31 -0600215 * Returns link jitter in terms of seconds.
216 *
217 * @return link jitter valuer; -1 if not set
218 */
219 public double jitter() {
220 return get(JITTER, -1.0);
221 }
222
223 /**
224 * Sets the link jitter.
225 *
226 * @param jitter new jitter value; null to clear
227 * @return self
228 */
229 public BasicLinkConfig jitter(Double jitter) {
230 return (BasicLinkConfig) setOrClear(JITTER, jitter);
231 }
232
233 /**
234 * Returns link delay in terms of seconds.
235 *
236 * @return link delay value; -1 if not set
237 */
238 public double delay() {
239 return get(DELAY, -1.0);
240 }
241
242 /**
243 * Sets the link delay.
244 *
245 * @param delay new delay value; null to clear
246 * @return self
247 */
248 public BasicLinkConfig delay(Double delay) {
249 return (BasicLinkConfig) setOrClear(DELAY, delay);
250 }
251
252 /**
253 * Returns link loss in terms of Percentage.
254 *
255 * @return link loss value; -1 if not set
256 */
257 public double loss() {
258 return get(LOSS, -1.0);
259 }
260
261 /**
262 * Sets the link loss.
263 *
264 * @param loss new loss value; null to clear
265 * @return self
266 */
267 public BasicLinkConfig loss(Double loss) {
268 return (BasicLinkConfig) setOrClear(LOSS, loss);
269 }
270
271 /**
272 * Returns link availability in terms of percentage.
273 *
274 * @return link availability value; -1 if not set
275 */
276 public double availability() {
277 return get(AVAILABILITY, -1.0);
278 }
279
280 /**
281 * Sets the link availability.
282 *
283 * @param availability new availability value; null to clear
284 * @return self
285 */
286 public BasicLinkConfig availability(Double availability) {
287 return (BasicLinkConfig) setOrClear(AVAILABILITY, availability);
288 }
289
290 /**
291 * Returns link flapping in terms of percentage.
292 *
293 * @return link flapping value; -1 if not set
294 */
295 public double flapping() {
296 return get(FLAPPING, -1.0);
297 }
298
299 /**
300 * Sets the link flapping.
301 *
302 * @param flapping new flapping value; null to clear
303 * @return self
304 */
305 public BasicLinkConfig flapping(Double flapping) {
306 return (BasicLinkConfig) setOrClear(FLAPPING, flapping);
307 }
308
309 /**
Yuta HIGUCHI21033042017-05-04 17:58:24 -0700310 * Create a {@link BasicLinkConfig} instance.
311 * <p>
312 * Note: created instance needs to be initialized by #init(..) before using.
313 */
314 public BasicLinkConfig() {
315 super();
316 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700317}