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