blob: eeed07fb2b71d457ed89667c74b73cd58c893c8c [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 /**
56 * Returns the link type.
57 *
58 * @return link type override
59 */
60 public Link.Type type() {
61 return get(TYPE, Link.Type.DIRECT, Link.Type.class);
62 }
63
64 /**
65 * Sets the link type.
66 *
67 * @param type link type override
68 * @return self
69 */
70 public BasicLinkConfig type(Link.Type type) {
71 return (BasicLinkConfig) setOrClear(TYPE, type);
72 }
73
74 /**
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080075 * Returns link metric value for use by
76 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
77 *
78 * @return link metric; -1 if not set
79 */
80 public double metric() {
81 return get(METRIC, -1);
82 }
83
84 /**
85 * Sets the link metric for use by
86 * {@link org.onosproject.net.topology.MetricLinkWeight} function.
87 *
88 * @param metric new metric; null to clear
89 * @return self
90 */
91 public BasicLinkConfig metric(Double metric) {
92 return (BasicLinkConfig) setOrClear(METRIC, metric);
93 }
94
95 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -070096 * Returns link latency in terms of nanos.
97 *
98 * @return link latency; -1 if not set
99 */
100 public Duration latency() {
101 return Duration.ofNanos(get(LATENCY, -1));
102 }
103
104 /**
105 * Sets the link latency.
106 *
107 * @param latency new latency; null to clear
108 * @return self
109 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700110 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700111 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700112 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700113 }
114
115 /**
116 * Returns link bandwidth in terms of Mbps.
117 *
118 * @return link bandwidth; -1 if not set
119 */
120 public long bandwidth() {
121 return get(BANDWIDTH, -1);
122 }
123
124 /**
125 * Sets the link bandwidth.
126 *
127 * @param bandwidth new bandwidth; null to clear
128 * @return self
129 */
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700130 public BasicLinkConfig bandwidth(Long bandwidth) {
131 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700132 }
133
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700134 /**
135 * Returns if link is durable in the network model or not.
136 *
137 * @return true for durable, false otherwise
138 */
139 public Boolean isDurable() {
140 JsonNode res = object.path(IS_DURABLE);
141 if (res.isMissingNode()) {
142 return null;
143 }
144 return res.asBoolean();
145 }
146
147 /**
148 * Sets durability for this link.
149 *
150 * @param isDurable true for durable, false otherwise
151 * @return this BasicLinkConfig
152 */
153 public BasicLinkConfig isDurable(Boolean isDurable) {
154 return (BasicLinkConfig) setOrClear(IS_DURABLE, isDurable);
155 }
Marc De Leenheerec551d32017-03-10 15:48:18 -0800156
157 /**
158 * Returns if link is bidirectional in the network model or not.
159 *
160 * @return true for bidirectional, false otherwise
161 */
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700162 public boolean isBidirectional() {
Marc De Leenheerec551d32017-03-10 15:48:18 -0800163 JsonNode res = object.path(IS_BIDIRECTIONAL);
164 if (res.isMissingNode()) {
165 return true;
166 }
167 return res.asBoolean();
168 }
169
170 /**
171 * Sets durability for this link.
172 *
173 * @param isBidirectional true for directional, false otherwise
174 * @return this BasicLinkConfig
175 */
176 public BasicLinkConfig isBidirectional(Boolean isBidirectional) {
177 return (BasicLinkConfig) setOrClear(IS_BIDIRECTIONAL, isBidirectional);
178 }
Yuta HIGUCHI21033042017-05-04 17:58:24 -0700179
180 /**
181 * Create a {@link BasicLinkConfig} for specified Device.
182 * <p>
183 * Note: created instance is not bound to NetworkConfigService,
184 * cannot use {@link #apply()}. Must be passed to the service
185 * using NetworkConfigService#applyConfig
186 *
187 * @param linkKey subject of this Config
188 */
189 public BasicLinkConfig(LinkKey linkKey) {
190 ObjectMapper mapper = new ObjectMapper();
191 init(linkKey, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
192 }
193
194 /**
195 * Create a {@link BasicLinkConfig} instance.
196 * <p>
197 * Note: created instance needs to be initialized by #init(..) before using.
198 */
199 public BasicLinkConfig() {
200 super();
201 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700202}