blob: 7e6af79fcd6c7e931a57bc02e86a77439a9012d2 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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() {
Marc De Leenheerec551d32017-03-10 15:48:18 -080049 return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE, IS_BIDIRECTIONAL) &&
Thomas Vachuska36008462016-01-07 15:38:20 -080050 isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) &&
Marc De Leenheerec551d32017-03-10 15:48:18 -080051 isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) &&
52 isBoolean(IS_BIDIRECTIONAL, OPTIONAL);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080053 }
54
Thomas Vachuska96d55b12015-05-11 08:52:03 -070055 /**
Yuta HIGUCHI3142f642017-06-07 11:12:45 -070056 * Returns if the link type is configured.
57 *
58 * @return true if config contains link type
59 */
60 public boolean isTypeConfigured() {
61 return hasField(TYPE);
62 }
63
64 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -070065 * Returns the link type.
66 *
67 * @return link type override
68 */
69 public Link.Type type() {
70 return get(TYPE, Link.Type.DIRECT, Link.Type.class);
71 }
72
73 /**
74 * Sets the link type.
75 *
76 * @param type link type override
77 * @return self
78 */
79 public BasicLinkConfig type(Link.Type type) {
80 return (BasicLinkConfig) setOrClear(TYPE, type);
81 }
82
83 /**
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080084 * Returns link metric value for use by
85 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
86 *
87 * @return link metric; -1 if not set
88 */
89 public double metric() {
90 return get(METRIC, -1);
91 }
92
93 /**
94 * Sets the link metric for use by
95 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
96 *
97 * @param metric new metric; null to clear
98 * @return self
99 */
100 public BasicLinkConfig metric(Double metric) {
101 return (BasicLinkConfig) setOrClear(METRIC, metric);
102 }
103
104 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700105 * Returns link latency in terms of nanos.
106 *
107 * @return link latency; -1 if not set
108 */
109 public Duration latency() {
110 return Duration.ofNanos(get(LATENCY, -1));
111 }
112
113 /**
114 * Sets the link latency.
115 *
116 * @param latency new latency; null to clear
117 * @return self
118 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700119 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700120 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700121 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700122 }
123
124 /**
125 * Returns link bandwidth in terms of Mbps.
126 *
127 * @return link bandwidth; -1 if not set
128 */
129 public long bandwidth() {
130 return get(BANDWIDTH, -1);
131 }
132
133 /**
134 * Sets the link bandwidth.
135 *
136 * @param bandwidth new bandwidth; null to clear
137 * @return self
138 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700139 public BasicLinkConfig bandwidth(Long bandwidth) {
140 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700141 }
142
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700143 /**
144 * Returns if link is durable in the network model or not.
145 *
146 * @return true for durable, false otherwise
147 */
148 public Boolean isDurable() {
149 JsonNode res = object.path(IS_DURABLE);
150 if (res.isMissingNode()) {
151 return null;
152 }
153 return res.asBoolean();
154 }
155
156 /**
157 * Sets durability for this link.
158 *
159 * @param isDurable true for durable, false otherwise
160 * @return this BasicLinkConfig
161 */
162 public BasicLinkConfig isDurable(Boolean isDurable) {
163 return (BasicLinkConfig) setOrClear(IS_DURABLE, isDurable);
164 }
Marc De Leenheerec551d32017-03-10 15:48:18 -0800165
166 /**
167 * Returns if link is bidirectional in the network model or not.
168 *
169 * @return true for bidirectional, false otherwise
170 */
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700171 public boolean isBidirectional() {
Marc De Leenheerec551d32017-03-10 15:48:18 -0800172 JsonNode res = object.path(IS_BIDIRECTIONAL);
173 if (res.isMissingNode()) {
174 return true;
175 }
176 return res.asBoolean();
177 }
178
179 /**
180 * Sets durability for this link.
181 *
182 * @param isBidirectional true for directional, false otherwise
183 * @return this BasicLinkConfig
184 */
185 public BasicLinkConfig isBidirectional(Boolean isBidirectional) {
186 return (BasicLinkConfig) setOrClear(IS_BIDIRECTIONAL, isBidirectional);
187 }
Yuta HIGUCHI21033042017-05-04 17:58:24 -0700188
189 /**
190 * Create a {@link BasicLinkConfig} for specified Device.
191 * <p>
192 * Note: created instance is not bound to NetworkConfigService,
193 * cannot use {@link #apply()}. Must be passed to the service
194 * using NetworkConfigService#applyConfig
195 *
196 * @param linkKey subject of this Config
197 */
198 public BasicLinkConfig(LinkKey linkKey) {
199 ObjectMapper mapper = new ObjectMapper();
200 init(linkKey, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
201 }
202
203 /**
204 * Create a {@link BasicLinkConfig} instance.
205 * <p>
206 * Note: created instance needs to be initialized by #init(..) before using.
207 */
208 public BasicLinkConfig() {
209 super();
210 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700211}