Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 3 | * |
| 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 Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.basics; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.Link; |
| 22 | import org.onosproject.net.LinkKey; |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 23 | import org.onosproject.net.config.inject.DeviceInjectionConfig; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | |
| 25 | import java.time.Duration; |
| 26 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 27 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 28 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Basic configuration for network infrastructure link. |
| 31 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 32 | public final class BasicLinkConfig extends AllowedEntityConfig<LinkKey> { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 33 | |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Configuration key for {@link DeviceInjectionConfig}. |
| 36 | */ |
| 37 | public static final String CONFIG_KEY = "basic"; |
| 38 | |
| 39 | |
Simon Hunt | db450ee | 2016-01-09 13:12:11 -0800 | [diff] [blame] | 40 | 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 Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 45 | public static final String IS_BIDIRECTIONAL = "bidirectional"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 46 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 47 | @Override |
| 48 | public boolean isValid() { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 49 | // Validate type/devices |
| 50 | type(); |
| 51 | |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 52 | return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE, IS_BIDIRECTIONAL) && |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 53 | isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) && |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 54 | isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) && |
| 55 | isBoolean(IS_BIDIRECTIONAL, OPTIONAL); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 58 | /** |
Yuta HIGUCHI | 3142f64 | 2017-06-07 11:12:45 -0700 | [diff] [blame] | 59 | * 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 68 | * 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 Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 87 | * 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 108 | * 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 Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 122 | public BasicLinkConfig latency(Duration latency) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 123 | Long nanos = latency == null ? null : latency.toNanos(); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 124 | return (BasicLinkConfig) setOrClear(LATENCY, nanos); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 125 | } |
| 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 Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 142 | public BasicLinkConfig bandwidth(Long bandwidth) { |
| 143 | return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 146 | /** |
| 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 Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Returns if link is bidirectional in the network model or not. |
| 171 | * |
| 172 | * @return true for bidirectional, false otherwise |
| 173 | */ |
Yuta HIGUCHI | 2ad387d | 2017-06-07 10:47:20 -0700 | [diff] [blame] | 174 | public boolean isBidirectional() { |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 175 | 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 HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 191 | |
| 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 214 | } |