blob: d687a8b4d38591014a45983f7e77967db500e55b [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 /**
Michal Machce774332017-01-25 11:02:55 +010042 * Creates a port description using the supplied information.
43 *
44 * @param number port number
45 * @param isEnabled port enabled state
46 * @param isRemoved port removed state
47 * @param type port type
48 * @param portSpeed port speed in Mbps
49 * @param annotations optional key/value annotations map
50 */
Ray Milkeyf031e302018-09-07 10:07:24 -070051 private DefaultPortDescription(PortNumber number, boolean isEnabled, boolean isRemoved,
Michal Machce774332017-01-25 11:02:55 +010052 Type type, long portSpeed,
53 SparseAnnotations...annotations) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070054 super(annotations);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070055 this.number = checkNotNull(number);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070056 this.isEnabled = isEnabled;
Michal Machce774332017-01-25 11:02:55 +010057 this.isRemoved = isRemoved;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070058 this.type = type;
59 this.portSpeed = portSpeed;
60 }
61
62 // Default constructor for serialization
Ray Milkeydbf59f02016-08-19 12:54:16 -070063 protected DefaultPortDescription() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070064 this.number = null;
65 this.isEnabled = false;
Michal Machce774332017-01-25 11:02:55 +010066 this.isRemoved = false;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070067 this.portSpeed = DEFAULT_SPEED;
68 this.type = Type.COPPER;
69 }
70
tomca20e0c2014-09-03 23:22:24 -070071 @Override
72 public PortNumber portNumber() {
73 return number;
74 }
75
76 @Override
tomd40fc7a2014-09-04 16:41:10 -070077 public boolean isEnabled() {
78 return isEnabled;
tomca20e0c2014-09-03 23:22:24 -070079 }
80
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070081 @Override
Michal Machce774332017-01-25 11:02:55 +010082 public boolean isRemoved() {
83 return isRemoved;
84 }
85
86 @Override
Thomas Vachuskad16ce182014-10-29 17:25:29 -070087 public Type type() {
88 return type;
89 }
90
91 @Override
92 public long portSpeed() {
93 return portSpeed;
94 }
95
96 @Override
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070097 public String toString() {
98 return MoreObjects.toStringHelper(getClass())
99 .add("number", number)
100 .add("isEnabled", isEnabled)
Palash Kala3b4177b2017-08-22 15:48:15 +0900101 .add("isRemoved", isRemoved)
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700102 .add("type", type)
103 .add("portSpeed", portSpeed)
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700104 .add("annotations", annotations())
105 .toString();
106 }
107
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800108 @Override
109 public int hashCode() {
110 return Objects.hashCode(super.hashCode(), number, isEnabled, type,
111 portSpeed);
112 }
113
114 @Override
115 public boolean equals(Object object) {
116 if (object != null && getClass() == object.getClass()) {
117 if (!super.equals(object)) {
118 return false;
119 }
120 DefaultPortDescription that = (DefaultPortDescription) object;
121 return Objects.equal(this.number, that.number)
122 && Objects.equal(this.isEnabled, that.isEnabled)
123 && Objects.equal(this.type, that.type)
124 && Objects.equal(this.portSpeed, that.portSpeed);
125 }
126 return false;
127 }
128
Yuta HIGUCHI9f2d7242017-05-18 17:17:32 -0700129 /**
130 * Creates port description builder with default parameters.
131 *
132 * @return builder
133 */
134 public static Builder builder() {
135 return new Builder();
136 }
137
138 /**
139 * Creates port description builder inheriting with default parameters,
140 * from specified port description.
141 *
142 * @param desc to inherit default from
143 * @return builder
144 */
145 public static Builder builder(PortDescription desc) {
146 return new Builder(desc);
147 }
148
149 public static class Builder {
150 private PortNumber number;
151 private boolean isEnabled = true;
152 private boolean isRemoved = false;
153 private Type type = Type.COPPER;
154 private long portSpeed = DEFAULT_SPEED;
155 private SparseAnnotations annotations = DefaultAnnotations.EMPTY;
156
157 Builder() {}
158
159 Builder(PortDescription desc) {
160 this.number = desc.portNumber();
161 this.isEnabled = desc.isEnabled();
162 this.isRemoved = desc.isRemoved();
163 this.type = desc.type();
164 this.portSpeed = desc.portSpeed();
165 this.annotations = desc.annotations();
166 }
167
168 /**
169 * Sets mandatory field PortNumber.
170 *
171 * @param number to set
172 * @return self
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800173 */
174 public Builder withPortNumber(PortNumber number) {
Yuta HIGUCHI9f2d7242017-05-18 17:17:32 -0700175 this.number = checkNotNull(number);
176 return this;
177 }
178
179 /**
180 * Sets enabled state.
181 *
182 * @param enabled state
183 * @return self
184 */
185 public Builder isEnabled(boolean enabled) {
186 this.isEnabled = enabled;
187 return this;
188 }
189
190 /**
191 * Sets removed state.
192 *
193 * @param removed state
194 * @return self
195 */
196 public Builder isRemoved(boolean removed) {
197 this.isRemoved = removed;
198 return this;
199 }
200
201 /**
202 * Sets port type.
203 *
204 * @param type of the port
205 * @return self
206 */
207 public Builder type(Type type) {
208 this.type = type;
209 return this;
210 }
211
212 /**
213 * Sets port speed.
214 *
215 * @param mbps port speed in Mbps
216 * @return self
217 */
218 public Builder portSpeed(long mbps) {
219 this.portSpeed = mbps;
220 return this;
221 }
222
223 /**
224 * Sets annotations.
225 *
226 * @param annotations of the port
227 * @return self
228 */
229 public Builder annotations(SparseAnnotations annotations) {
230 this.annotations = checkNotNull(annotations);
231 return this;
232 }
233
234 /**
235 * Builds the port description.
236 *
237 * @return port description
238 */
239 public DefaultPortDescription build() {
240 return new DefaultPortDescription(number, isEnabled, isRemoved, type, portSpeed, annotations);
241 }
242 }
tomd1900f32014-09-03 14:08:16 -0700243}