blob: 37930e3cbd745bc7e7af1e0c3b8b5196866b3ff5 [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) {
106 this(base.portNumber(), base.isEnabled(), base.type(), base.portSpeed(),
107 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)
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700153 .add("type", type)
154 .add("portSpeed", portSpeed)
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700155 .add("annotations", annotations())
156 .toString();
157 }
158
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800159 @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 HIGUCHI9f2d7242017-05-18 17:17:32 -0700180 /**
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 }
tomd1900f32014-09-03 14:08:16 -0700294}