blob: 57fdbe22ea1f3cb280178564438f1040d0f14541 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.device;
tomd1900f32014-09-03 14:08:16 -070017
Thomas Vachuskad16ce182014-10-29 17:25:29 -070018import com.google.common.base.MoreObjects;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.AbstractDescription;
Yuta HIGUCHI9f2d7242017-05-18 17:17:32 -070020import org.onosproject.net.DefaultAnnotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.PortNumber;
22import org.onosproject.net.SparseAnnotations;
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070023import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import static org.onosproject.net.Port.Type;
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -080025import com.google.common.base.Objects;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070026
tomd1900f32014-09-03 14:08:16 -070027/**
28 * Default implementation of immutable port description.
29 */
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070030public class DefaultPortDescription extends AbstractDescription
31 implements PortDescription {
tomca20e0c2014-09-03 23:22:24 -070032
Thomas Vachuskad16ce182014-10-29 17:25:29 -070033 private static final long DEFAULT_SPEED = 1_000;
34
tomca20e0c2014-09-03 23:22:24 -070035 private final PortNumber number;
tomd40fc7a2014-09-04 16:41:10 -070036 private final boolean isEnabled;
Michal Machce774332017-01-25 11:02:55 +010037 private final boolean isRemoved;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070038 private final Type type;
39 private final long portSpeed;
tomca20e0c2014-09-03 23:22:24 -070040
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070041 /**
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080042 * Creates a DEFAULT_SPEED COPPER port description using the supplied information.
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070043 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070044 * @param number port number
45 * @param isEnabled port enabled state
46 * @param annotations optional key/value annotations map
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080047 *
48 * @deprecated in 1.13.0 use {@link #builder()} instead
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070049 */
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080050 @Deprecated
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070051 public DefaultPortDescription(PortNumber number, boolean isEnabled,
Thomas Vachuskad16ce182014-10-29 17:25:29 -070052 SparseAnnotations... annotations) {
53 this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations);
tomca20e0c2014-09-03 23:22:24 -070054 }
55
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070056 /**
57 * Creates a port description using the supplied information.
58 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070059 * @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 HIGUCHI53e47962018-03-01 23:50:48 -080064 * @deprecated in 1.13.0 use {@link #builder()} instead
Thomas Vachuskad16ce182014-10-29 17:25:29 -070065 */
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080066 @Deprecated
Thomas Vachuskad16ce182014-10-29 17:25:29 -070067 public DefaultPortDescription(PortNumber number, boolean isEnabled,
68 Type type, long portSpeed,
69 SparseAnnotations...annotations) {
Michal Machce774332017-01-25 11:02:55 +010070 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 HIGUCHI53e47962018-03-01 23:50:48 -080082 * @deprecated in 1.13.0 use {@link #builder()} instead
Michal Machce774332017-01-25 11:02:55 +010083 */
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080084 @Deprecated // to be made non-public
Michal Machce774332017-01-25 11:02:55 +010085 public DefaultPortDescription(PortNumber number, boolean isEnabled, boolean isRemoved,
86 Type type, long portSpeed,
87 SparseAnnotations...annotations) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070088 super(annotations);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070089 this.number = checkNotNull(number);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070090 this.isEnabled = isEnabled;
Michal Machce774332017-01-25 11:02:55 +010091 this.isRemoved = isRemoved;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070092 this.type = type;
93 this.portSpeed = portSpeed;
94 }
95
96 // Default constructor for serialization
Ray Milkeydbf59f02016-08-19 12:54:16 -070097 protected DefaultPortDescription() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070098 this.number = null;
99 this.isEnabled = false;
Michal Machce774332017-01-25 11:02:55 +0100100 this.isRemoved = false;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700101 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 HIGUCHI53e47962018-03-01 23:50:48 -0800110 *
111 * @deprecated in 1.13.0 use {@link #builder(PortDescription)} instead.
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700112 */
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800113 @Deprecated
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700114 public DefaultPortDescription(PortDescription base,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700115 SparseAnnotations annotations) {
Palash Kala3b4177b2017-08-22 15:48:15 +0900116 this(base.portNumber(), base.isEnabled(), base.isRemoved(), base.type(), base.portSpeed(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700117 annotations);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700118 }
119
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -0800120 /**
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 HIGUCHI53e47962018-03-01 23:50:48 -0800127 *
128 * @deprecated in 1.13.0 use {@link #builder(PortDescription)} instead.
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -0800129 */
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800130 @Deprecated
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -0800131 public static DefaultPortDescription copyReplacingAnnotation(PortDescription base,
132 SparseAnnotations annotations) {
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800133 return DefaultPortDescription.builder(base)
134 .annotations(annotations)
135 .build();
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -0800136 }
137
tomca20e0c2014-09-03 23:22:24 -0700138 @Override
139 public PortNumber portNumber() {
140 return number;
141 }
142
143 @Override
tomd40fc7a2014-09-04 16:41:10 -0700144 public boolean isEnabled() {
145 return isEnabled;
tomca20e0c2014-09-03 23:22:24 -0700146 }
147
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700148 @Override
Michal Machce774332017-01-25 11:02:55 +0100149 public boolean isRemoved() {
150 return isRemoved;
151 }
152
153 @Override
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700154 public Type type() {
155 return type;
156 }
157
158 @Override
159 public long portSpeed() {
160 return portSpeed;
161 }
162
163 @Override
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700164 public String toString() {
165 return MoreObjects.toStringHelper(getClass())
166 .add("number", number)
167 .add("isEnabled", isEnabled)
Palash Kala3b4177b2017-08-22 15:48:15 +0900168 .add("isRemoved", isRemoved)
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700169 .add("type", type)
170 .add("portSpeed", portSpeed)
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700171 .add("annotations", annotations())
172 .toString();
173 }
174
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800175 @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 HIGUCHI9f2d7242017-05-18 17:17:32 -0700196 /**
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 HIGUCHI53e47962018-03-01 23:50:48 -0800240 *
241 * @deprecated in 1.13.0 use withPortNumber() instead.
Yuta HIGUCHI9f2d7242017-05-18 17:17:32 -0700242 */
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800243
244 @Deprecated
Yuta HIGUCHI9f2d7242017-05-18 17:17:32 -0700245 public Builder withPortNumer(PortNumber number) {
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800246 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 HIGUCHI9f2d7242017-05-18 17:17:32 -0700256 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 }
tomd1900f32014-09-03 14:08:16 -0700324}