blob: 5d2728484bc55b58efcd0eff2ff578c2d8f0a4e9 [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";
44 public static final String IS_DURABLE = "durable";
Marc De Leenheerec551d32017-03-10 15:48:18 -080045 public static final String IS_BIDIRECTIONAL = "bidirectional";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070046
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080047 @Override
48 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070049 // Validate type/devices
50 type();
51
Marc De Leenheerec551d32017-03-10 15:48:18 -080052 return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE, IS_BIDIRECTIONAL) &&
Thomas Vachuska36008462016-01-07 15:38:20 -080053 isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) &&
Marc De Leenheerec551d32017-03-10 15:48:18 -080054 isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) &&
55 isBoolean(IS_BIDIRECTIONAL, OPTIONAL);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080056 }
57
Thomas Vachuska96d55b12015-05-11 08:52:03 -070058 /**
Yuta HIGUCHI3142f642017-06-07 11:12:45 -070059 * Returns if the link type is configured.
60 *
61 * @return true if config contains link type
62 */
63 public boolean isTypeConfigured() {
64 return hasField(TYPE);
65 }
66
67 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -070068 * Returns the link type.
69 *
70 * @return link type override
71 */
72 public Link.Type type() {
73 return get(TYPE, Link.Type.DIRECT, Link.Type.class);
74 }
75
76 /**
77 * Sets the link type.
78 *
79 * @param type link type override
80 * @return self
81 */
82 public BasicLinkConfig type(Link.Type type) {
83 return (BasicLinkConfig) setOrClear(TYPE, type);
84 }
85
86 /**
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080087 * Returns link metric value for use by
88 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
89 *
90 * @return link metric; -1 if not set
91 */
92 public double metric() {
93 return get(METRIC, -1);
94 }
95
96 /**
97 * Sets the link metric for use by
98 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
99 *
100 * @param metric new metric; null to clear
101 * @return self
102 */
103 public BasicLinkConfig metric(Double metric) {
104 return (BasicLinkConfig) setOrClear(METRIC, metric);
105 }
106
107 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700108 * Returns link latency in terms of nanos.
109 *
110 * @return link latency; -1 if not set
111 */
112 public Duration latency() {
113 return Duration.ofNanos(get(LATENCY, -1));
114 }
115
116 /**
117 * Sets the link latency.
118 *
119 * @param latency new latency; null to clear
120 * @return self
121 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700122 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700123 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700124 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700125 }
126
127 /**
128 * Returns link bandwidth in terms of Mbps.
129 *
130 * @return link bandwidth; -1 if not set
131 */
132 public long bandwidth() {
133 return get(BANDWIDTH, -1);
134 }
135
136 /**
137 * Sets the link bandwidth.
138 *
139 * @param bandwidth new bandwidth; null to clear
140 * @return self
141 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700142 public BasicLinkConfig bandwidth(Long bandwidth) {
143 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700144 }
145
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700146 /**
147 * Returns if link is durable in the network model or not.
148 *
149 * @return true for durable, false otherwise
150 */
151 public Boolean isDurable() {
152 JsonNode res = object.path(IS_DURABLE);
153 if (res.isMissingNode()) {
154 return null;
155 }
156 return res.asBoolean();
157 }
158
159 /**
160 * Sets durability for this link.
161 *
162 * @param isDurable true for durable, false otherwise
163 * @return this BasicLinkConfig
164 */
165 public BasicLinkConfig isDurable(Boolean isDurable) {
166 return (BasicLinkConfig) setOrClear(IS_DURABLE, isDurable);
167 }
Marc De Leenheerec551d32017-03-10 15:48:18 -0800168
169 /**
170 * Returns if link is bidirectional in the network model or not.
171 *
172 * @return true for bidirectional, false otherwise
173 */
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700174 public boolean isBidirectional() {
Marc De Leenheerec551d32017-03-10 15:48:18 -0800175 JsonNode res = object.path(IS_BIDIRECTIONAL);
176 if (res.isMissingNode()) {
177 return true;
178 }
179 return res.asBoolean();
180 }
181
182 /**
183 * Sets durability for this link.
184 *
185 * @param isBidirectional true for directional, false otherwise
186 * @return this BasicLinkConfig
187 */
188 public BasicLinkConfig isBidirectional(Boolean isBidirectional) {
189 return (BasicLinkConfig) setOrClear(IS_BIDIRECTIONAL, isBidirectional);
190 }
Yuta HIGUCHI21033042017-05-04 17:58:24 -0700191
192 /**
193 * Create a {@link BasicLinkConfig} for specified Device.
194 * <p>
195 * Note: created instance is not bound to NetworkConfigService,
196 * cannot use {@link #apply()}. Must be passed to the service
197 * using NetworkConfigService#applyConfig
198 *
199 * @param linkKey subject of this Config
200 */
201 public BasicLinkConfig(LinkKey linkKey) {
202 ObjectMapper mapper = new ObjectMapper();
203 init(linkKey, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
204 }
205
206 /**
207 * Create a {@link BasicLinkConfig} instance.
208 * <p>
209 * Note: created instance needs to be initialized by #init(..) before using.
210 */
211 public BasicLinkConfig() {
212 super();
213 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700214}