Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.device; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.AbstractDescription; |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.SparseAnnotations; |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import static org.onosproject.net.Port.Type; |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 25 | import com.google.common.base.Objects; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 26 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Default implementation of immutable port description. |
| 29 | */ |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 30 | public class DefaultPortDescription extends AbstractDescription |
| 31 | implements PortDescription { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 32 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 33 | private static final long DEFAULT_SPEED = 1_000; |
| 34 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 35 | private final PortNumber number; |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 36 | private final boolean isEnabled; |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 37 | private final boolean isRemoved; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 38 | private final Type type; |
| 39 | private final long portSpeed; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 41 | /** |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 42 | * Creates a DEFAULT_SPEED COPPER port description using the supplied information. |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 43 | * |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 44 | * @param number port number |
| 45 | * @param isEnabled port enabled state |
| 46 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 47 | * |
| 48 | * @deprecated in 1.13.0 use {@link #builder()} instead |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 49 | */ |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 50 | @Deprecated |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 51 | public DefaultPortDescription(PortNumber number, boolean isEnabled, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 52 | SparseAnnotations... annotations) { |
| 53 | this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Creates a port description using the supplied information. |
| 58 | * |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 59 | * @param number port number |
| 60 | * @param isEnabled port enabled state |
| 61 | * @param type port type |
| 62 | * @param portSpeed port speed in Mbps |
| 63 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 64 | * @deprecated in 1.13.0 use {@link #builder()} instead |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 65 | */ |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 66 | @Deprecated |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 67 | public DefaultPortDescription(PortNumber number, boolean isEnabled, |
| 68 | Type type, long portSpeed, |
| 69 | SparseAnnotations...annotations) { |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 70 | this(number, isEnabled, false, type, portSpeed, annotations); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Creates a port description using the supplied information. |
| 75 | * |
| 76 | * @param number port number |
| 77 | * @param isEnabled port enabled state |
| 78 | * @param isRemoved port removed state |
| 79 | * @param type port type |
| 80 | * @param portSpeed port speed in Mbps |
| 81 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 82 | * @deprecated in 1.13.0 use {@link #builder()} instead |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 83 | */ |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 84 | @Deprecated // to be made non-public |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 85 | public DefaultPortDescription(PortNumber number, boolean isEnabled, boolean isRemoved, |
| 86 | Type type, long portSpeed, |
| 87 | SparseAnnotations...annotations) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 88 | super(annotations); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 89 | this.number = checkNotNull(number); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 90 | this.isEnabled = isEnabled; |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 91 | this.isRemoved = isRemoved; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 92 | this.type = type; |
| 93 | this.portSpeed = portSpeed; |
| 94 | } |
| 95 | |
| 96 | // Default constructor for serialization |
Ray Milkey | dbf59f0 | 2016-08-19 12:54:16 -0700 | [diff] [blame] | 97 | protected DefaultPortDescription() { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 98 | this.number = null; |
| 99 | this.isEnabled = false; |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 100 | this.isRemoved = false; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 101 | this.portSpeed = DEFAULT_SPEED; |
| 102 | this.type = Type.COPPER; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Creates a port description using the supplied information. |
| 107 | * |
| 108 | * @param base PortDescription to get basic information from |
| 109 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 110 | * |
| 111 | * @deprecated in 1.13.0 use {@link #builder(PortDescription)} instead. |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 112 | */ |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 113 | @Deprecated |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 114 | public DefaultPortDescription(PortDescription base, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 115 | SparseAnnotations annotations) { |
Palash Kala | 3b4177b | 2017-08-22 15:48:15 +0900 | [diff] [blame] | 116 | this(base.portNumber(), base.isEnabled(), base.isRemoved(), base.type(), base.portSpeed(), |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 117 | annotations); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Creates a port description using the supplied information. |
| 122 | * |
| 123 | * @param base port description to copy fields from |
| 124 | * @param annotations to be used in the copied description. |
| 125 | * Note: Annotations on {@code base} will be ignored. |
| 126 | * @return copied port description |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 127 | * |
| 128 | * @deprecated in 1.13.0 use {@link #builder(PortDescription)} instead. |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 129 | */ |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 130 | @Deprecated |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 131 | public static DefaultPortDescription copyReplacingAnnotation(PortDescription base, |
| 132 | SparseAnnotations annotations) { |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 133 | return DefaultPortDescription.builder(base) |
| 134 | .annotations(annotations) |
| 135 | .build(); |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 136 | } |
| 137 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public PortNumber portNumber() { |
| 140 | return number; |
| 141 | } |
| 142 | |
| 143 | @Override |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 144 | public boolean isEnabled() { |
| 145 | return isEnabled; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 148 | @Override |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 149 | public boolean isRemoved() { |
| 150 | return isRemoved; |
| 151 | } |
| 152 | |
| 153 | @Override |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 154 | public Type type() { |
| 155 | return type; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public long portSpeed() { |
| 160 | return portSpeed; |
| 161 | } |
| 162 | |
| 163 | @Override |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 164 | public String toString() { |
| 165 | return MoreObjects.toStringHelper(getClass()) |
| 166 | .add("number", number) |
| 167 | .add("isEnabled", isEnabled) |
Palash Kala | 3b4177b | 2017-08-22 15:48:15 +0900 | [diff] [blame] | 168 | .add("isRemoved", isRemoved) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 169 | .add("type", type) |
| 170 | .add("portSpeed", portSpeed) |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 171 | .add("annotations", annotations()) |
| 172 | .toString(); |
| 173 | } |
| 174 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 175 | @Override |
| 176 | public int hashCode() { |
| 177 | return Objects.hashCode(super.hashCode(), number, isEnabled, type, |
| 178 | portSpeed); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean equals(Object object) { |
| 183 | if (object != null && getClass() == object.getClass()) { |
| 184 | if (!super.equals(object)) { |
| 185 | return false; |
| 186 | } |
| 187 | DefaultPortDescription that = (DefaultPortDescription) object; |
| 188 | return Objects.equal(this.number, that.number) |
| 189 | && Objects.equal(this.isEnabled, that.isEnabled) |
| 190 | && Objects.equal(this.type, that.type) |
| 191 | && Objects.equal(this.portSpeed, that.portSpeed); |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Creates port description builder with default parameters. |
| 198 | * |
| 199 | * @return builder |
| 200 | */ |
| 201 | public static Builder builder() { |
| 202 | return new Builder(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Creates port description builder inheriting with default parameters, |
| 207 | * from specified port description. |
| 208 | * |
| 209 | * @param desc to inherit default from |
| 210 | * @return builder |
| 211 | */ |
| 212 | public static Builder builder(PortDescription desc) { |
| 213 | return new Builder(desc); |
| 214 | } |
| 215 | |
| 216 | public static class Builder { |
| 217 | private PortNumber number; |
| 218 | private boolean isEnabled = true; |
| 219 | private boolean isRemoved = false; |
| 220 | private Type type = Type.COPPER; |
| 221 | private long portSpeed = DEFAULT_SPEED; |
| 222 | private SparseAnnotations annotations = DefaultAnnotations.EMPTY; |
| 223 | |
| 224 | Builder() {} |
| 225 | |
| 226 | Builder(PortDescription desc) { |
| 227 | this.number = desc.portNumber(); |
| 228 | this.isEnabled = desc.isEnabled(); |
| 229 | this.isRemoved = desc.isRemoved(); |
| 230 | this.type = desc.type(); |
| 231 | this.portSpeed = desc.portSpeed(); |
| 232 | this.annotations = desc.annotations(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Sets mandatory field PortNumber. |
| 237 | * |
| 238 | * @param number to set |
| 239 | * @return self |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 240 | * |
| 241 | * @deprecated in 1.13.0 use withPortNumber() instead. |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 242 | */ |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 243 | |
| 244 | @Deprecated |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 245 | public Builder withPortNumer(PortNumber number) { |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 246 | return withPortNumber(number); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Sets mandatory field PortNumber. |
| 251 | * |
| 252 | * @param number to set |
| 253 | * @return self |
| 254 | */ |
| 255 | public Builder withPortNumber(PortNumber number) { |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 256 | this.number = checkNotNull(number); |
| 257 | return this; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Sets enabled state. |
| 262 | * |
| 263 | * @param enabled state |
| 264 | * @return self |
| 265 | */ |
| 266 | public Builder isEnabled(boolean enabled) { |
| 267 | this.isEnabled = enabled; |
| 268 | return this; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Sets removed state. |
| 273 | * |
| 274 | * @param removed state |
| 275 | * @return self |
| 276 | */ |
| 277 | public Builder isRemoved(boolean removed) { |
| 278 | this.isRemoved = removed; |
| 279 | return this; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Sets port type. |
| 284 | * |
| 285 | * @param type of the port |
| 286 | * @return self |
| 287 | */ |
| 288 | public Builder type(Type type) { |
| 289 | this.type = type; |
| 290 | return this; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Sets port speed. |
| 295 | * |
| 296 | * @param mbps port speed in Mbps |
| 297 | * @return self |
| 298 | */ |
| 299 | public Builder portSpeed(long mbps) { |
| 300 | this.portSpeed = mbps; |
| 301 | return this; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Sets annotations. |
| 306 | * |
| 307 | * @param annotations of the port |
| 308 | * @return self |
| 309 | */ |
| 310 | public Builder annotations(SparseAnnotations annotations) { |
| 311 | this.annotations = checkNotNull(annotations); |
| 312 | return this; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Builds the port description. |
| 317 | * |
| 318 | * @return port description |
| 319 | */ |
| 320 | public DefaultPortDescription build() { |
| 321 | return new DefaultPortDescription(number, isEnabled, isRemoved, type, portSpeed, annotations); |
| 322 | } |
| 323 | } |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 324 | } |