blob: 2354fcea40fe684554f8f96e8efdbf46fab36bf6 [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 HIGUCHI55710e72014-10-02 14:58:32 -070047 */
48 public DefaultPortDescription(PortNumber number, boolean isEnabled,
Thomas Vachuskad16ce182014-10-29 17:25:29 -070049 SparseAnnotations... annotations) {
50 this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations);
tomca20e0c2014-09-03 23:22:24 -070051 }
52
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070053 /**
54 * Creates a port description using the supplied information.
55 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070056 * @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 Machce774332017-01-25 11:02:55 +010065 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 Vachuskad16ce182014-10-29 17:25:29 -070081 super(annotations);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070082 this.number = checkNotNull(number);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070083 this.isEnabled = isEnabled;
Michal Machce774332017-01-25 11:02:55 +010084 this.isRemoved = isRemoved;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070085 this.type = type;
86 this.portSpeed = portSpeed;
87 }
88
89 // Default constructor for serialization
Ray Milkeydbf59f02016-08-19 12:54:16 -070090 protected DefaultPortDescription() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070091 this.number = null;
92 this.isEnabled = false;
Michal Machce774332017-01-25 11:02:55 +010093 this.isRemoved = false;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070094 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 HIGUCHI55710e72014-10-02 14:58:32 -0700103 */
104 public DefaultPortDescription(PortDescription base,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700105 SparseAnnotations annotations) {
Palash Kala3b4177b2017-08-22 15:48:15 +0900106 this(base.portNumber(), base.isEnabled(), base.isRemoved(), base.type(), base.portSpeed(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700107 annotations);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700108 }
109
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -0800110 /**
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
tomca20e0c2014-09-03 23:22:24 -0700123 @Override
124 public PortNumber portNumber() {
125 return number;
126 }
127
128 @Override
tomd40fc7a2014-09-04 16:41:10 -0700129 public boolean isEnabled() {
130 return isEnabled;
tomca20e0c2014-09-03 23:22:24 -0700131 }
132
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700133 @Override
Michal Machce774332017-01-25 11:02:55 +0100134 public boolean isRemoved() {
135 return isRemoved;
136 }
137
138 @Override
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700139 public Type type() {
140 return type;
141 }
142
143 @Override
144 public long portSpeed() {
145 return portSpeed;
146 }
147
148 @Override
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700149 public String toString() {
150 return MoreObjects.toStringHelper(getClass())
151 .add("number", number)
152 .add("isEnabled", isEnabled)
Palash Kala3b4177b2017-08-22 15:48:15 +0900153 .add("isRemoved", isRemoved)
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700154 .add("type", type)
155 .add("portSpeed", portSpeed)
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700156 .add("annotations", annotations())
157 .toString();
158 }
159
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800160 @Override
161 public int hashCode() {
162 return Objects.hashCode(super.hashCode(), number, isEnabled, type,
163 portSpeed);
164 }
165
166 @Override
167 public boolean equals(Object object) {
168 if (object != null && getClass() == object.getClass()) {
169 if (!super.equals(object)) {
170 return false;
171 }
172 DefaultPortDescription that = (DefaultPortDescription) object;
173 return Objects.equal(this.number, that.number)
174 && Objects.equal(this.isEnabled, that.isEnabled)
175 && Objects.equal(this.type, that.type)
176 && Objects.equal(this.portSpeed, that.portSpeed);
177 }
178 return false;
179 }
180
Yuta HIGUCHI9f2d7242017-05-18 17:17:32 -0700181 /**
182 * Creates port description builder with default parameters.
183 *
184 * @return builder
185 */
186 public static Builder builder() {
187 return new Builder();
188 }
189
190 /**
191 * Creates port description builder inheriting with default parameters,
192 * from specified port description.
193 *
194 * @param desc to inherit default from
195 * @return builder
196 */
197 public static Builder builder(PortDescription desc) {
198 return new Builder(desc);
199 }
200
201 public static class Builder {
202 private PortNumber number;
203 private boolean isEnabled = true;
204 private boolean isRemoved = false;
205 private Type type = Type.COPPER;
206 private long portSpeed = DEFAULT_SPEED;
207 private SparseAnnotations annotations = DefaultAnnotations.EMPTY;
208
209 Builder() {}
210
211 Builder(PortDescription desc) {
212 this.number = desc.portNumber();
213 this.isEnabled = desc.isEnabled();
214 this.isRemoved = desc.isRemoved();
215 this.type = desc.type();
216 this.portSpeed = desc.portSpeed();
217 this.annotations = desc.annotations();
218 }
219
220 /**
221 * Sets mandatory field PortNumber.
222 *
223 * @param number to set
224 * @return self
225 */
226 public Builder withPortNumer(PortNumber number) {
227 this.number = checkNotNull(number);
228 return this;
229 }
230
231 /**
232 * Sets enabled state.
233 *
234 * @param enabled state
235 * @return self
236 */
237 public Builder isEnabled(boolean enabled) {
238 this.isEnabled = enabled;
239 return this;
240 }
241
242 /**
243 * Sets removed state.
244 *
245 * @param removed state
246 * @return self
247 */
248 public Builder isRemoved(boolean removed) {
249 this.isRemoved = removed;
250 return this;
251 }
252
253 /**
254 * Sets port type.
255 *
256 * @param type of the port
257 * @return self
258 */
259 public Builder type(Type type) {
260 this.type = type;
261 return this;
262 }
263
264 /**
265 * Sets port speed.
266 *
267 * @param mbps port speed in Mbps
268 * @return self
269 */
270 public Builder portSpeed(long mbps) {
271 this.portSpeed = mbps;
272 return this;
273 }
274
275 /**
276 * Sets annotations.
277 *
278 * @param annotations of the port
279 * @return self
280 */
281 public Builder annotations(SparseAnnotations annotations) {
282 this.annotations = checkNotNull(annotations);
283 return this;
284 }
285
286 /**
287 * Builds the port description.
288 *
289 * @return port description
290 */
291 public DefaultPortDescription build() {
292 return new DefaultPortDescription(number, isEnabled, isRemoved, type, portSpeed, annotations);
293 }
294 }
tomd1900f32014-09-03 14:08:16 -0700295}