blob: a8270f5bdb2abecd580b0bdc20023f74216abfef [file] [log] [blame]
Georgios Katsikas83600982017-05-28 20:41:45 +02001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16
17package org.onosproject.drivers.server.impl.devices;
18
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020019import org.onosproject.drivers.server.devices.nic.NicDevice;
20import org.onosproject.drivers.server.devices.nic.NicRxFilter;
Georgios Katsikas740d3282020-03-18 12:05:03 +010021import org.onosproject.drivers.server.devices.nic.NicRxFilter.RxFilter;
22import org.onosproject.net.Port;
Georgios Katsikas83600982017-05-28 20:41:45 +020023
24import org.onlab.packet.MacAddress;
25
26import com.google.common.base.MoreObjects;
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020027import com.google.common.base.Strings;
Georgios Katsikas83600982017-05-28 20:41:45 +020028
Georgios Katsikas740d3282020-03-18 12:05:03 +010029import java.util.Collections;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
Georgios Katsikas83600982017-05-28 20:41:45 +020033import java.util.Objects;
34
35import static org.onosproject.net.Port.Type;
36import static com.google.common.base.Preconditions.checkNotNull;
37import static com.google.common.base.Preconditions.checkArgument;
Georgios Katsikas740d3282020-03-18 12:05:03 +010038import static org.onosproject.drivers.server.Constants.MSG_NIC_NAME_NULL;
39import static org.onosproject.drivers.server.Constants.MSG_NIC_MAC_NULL;
40import static org.onosproject.drivers.server.Constants.MSG_NIC_PORT_NUMBER_NEGATIVE;
41import static org.onosproject.drivers.server.Constants.MSG_NIC_PORT_TYPE_NULL;
42import static org.onosproject.drivers.server.Constants.MSG_NIC_RX_FILTER_NULL;
43import static org.onosproject.drivers.server.Constants.MSG_NIC_RX_FILTERS_NULL;
44import static org.onosproject.drivers.server.Constants.MSG_NIC_SPEED_NEGATIVE;
45import static org.onosproject.drivers.server.Constants.PARAM_NIC_PORT_TYPE_COPPER;
46import static org.onosproject.drivers.server.Constants.PARAM_NIC_PORT_TYPE_FIBER;
Georgios Katsikas83600982017-05-28 20:41:45 +020047
48/**
49 * Default implementation for NIC devices.
50 */
Georgios Katsikas740d3282020-03-18 12:05:03 +010051public final class DefaultNicDevice implements NicDevice, Comparable {
Georgios Katsikas83600982017-05-28 20:41:45 +020052
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020053 private final String name;
54 private final long portNumber;
Georgios Katsikas83600982017-05-28 20:41:45 +020055 private final Type portType;
Georgios Katsikas740d3282020-03-18 12:05:03 +010056 private final long speed;
Georgios Katsikas83600982017-05-28 20:41:45 +020057 private boolean status;
58 private final MacAddress macAddress;
59 private NicRxFilter rxFilterMechanisms;
60
Georgios Katsikas740d3282020-03-18 12:05:03 +010061 private DefaultNicDevice(
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020062 String name,
63 long portNumber,
Georgios Katsikas83600982017-05-28 20:41:45 +020064 Type portType,
65 long speed,
66 boolean status,
Georgios Katsikas740d3282020-03-18 12:05:03 +010067 MacAddress mac,
Georgios Katsikas83600982017-05-28 20:41:45 +020068 NicRxFilter rxFilterMechanisms) {
Georgios Katsikas740d3282020-03-18 12:05:03 +010069 checkArgument(!Strings.isNullOrEmpty(name), MSG_NIC_NAME_NULL);
70 checkArgument(portNumber >= 0, MSG_NIC_PORT_NUMBER_NEGATIVE);
71 checkNotNull(portType, MSG_NIC_PORT_TYPE_NULL);
72 checkArgument((speed >= 0) && (speed <= NicDevice.MAX_SPEED),
73 MSG_NIC_SPEED_NEGATIVE);
74 checkNotNull(mac, MSG_NIC_MAC_NULL);
75 checkNotNull(rxFilterMechanisms, MSG_NIC_RX_FILTERS_NULL);
Georgios Katsikas83600982017-05-28 20:41:45 +020076
77 // Implies a problem
78 if (speed == 0) {
79 status = false;
80 }
81
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020082 this.name = name;
83 this.portNumber = portNumber;
Georgios Katsikas83600982017-05-28 20:41:45 +020084 this.speed = speed;
85 this.portType = portType;
86 this.status = status;
Georgios Katsikas740d3282020-03-18 12:05:03 +010087 this.macAddress = mac;
88 this.rxFilterMechanisms = rxFilterMechanisms;
89 }
90
91 /**
92 * Creates a builder for DefaultNicDevice object.
93 *
94 * @return builder object for DefaultNicDevice object
95 */
96 public static DefaultNicDevice.Builder builder() {
97 return new Builder();
Georgios Katsikas83600982017-05-28 20:41:45 +020098 }
99
100 @Override
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200101 public String name() {
102 return this.name;
Georgios Katsikas83600982017-05-28 20:41:45 +0200103 }
104
105 @Override
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200106 public long portNumber() {
107 return this.portNumber;
Georgios Katsikas83600982017-05-28 20:41:45 +0200108 }
109
110 @Override
111 public Type portType() {
112 return this.portType;
113 }
114
115 @Override
116 public long speed() {
117 return this.speed;
118 }
119
120 @Override
121 public boolean status() {
122 return this.status;
123 }
124
125 @Override
126 public void setStatus(boolean status) {
127 this.status = status;
128 }
129
130 @Override
131 public MacAddress macAddress() {
132 return this.macAddress;
133 }
134
135 @Override
136 public NicRxFilter rxFilterMechanisms() {
137 return this.rxFilterMechanisms;
138 }
139
140 @Override
141 public void setRxFilterMechanisms(NicRxFilter rxFilterMechanisms) {
142 checkNotNull(rxFilterMechanisms, "NIC Rx filter mechanisms cannot be null");
143 this.rxFilterMechanisms = rxFilterMechanisms;
144 }
145
146 @Override
Georgios Katsikas740d3282020-03-18 12:05:03 +0100147 public void addRxFilterMechanism(RxFilter rxFilter) {
Georgios Katsikas83600982017-05-28 20:41:45 +0200148 this.rxFilterMechanisms.addRxFilter(rxFilter);
149 }
150
151 @Override
152 public String toString() {
153 return MoreObjects.toStringHelper(this)
154 .omitNullValues()
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200155 .add("name", name())
156 .add("port", portNumber())
Georgios Katsikas83600982017-05-28 20:41:45 +0200157 .add("mac", macAddress.toString())
158 .add("portType", portType())
159 .add("speed", speed())
160 .add("status", status ? "active" : "inactive")
161 .add("rxFilters", rxFilterMechanisms())
162 .toString();
163 }
164
165 @Override
166 public boolean equals(Object obj) {
167 if (obj == this) {
168 return true;
169 }
170 if (!(obj instanceof NicDevice)) {
171 return false;
172 }
173 NicDevice device = (NicDevice) obj;
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200174 return this.name().equals(device.name()) &&
175 this.portNumber() == device.portNumber() &&
Georgios Katsikas83600982017-05-28 20:41:45 +0200176 this.speed == device.speed() &&
177 this.macAddress.equals(device.macAddress());
178 }
179
180 @Override
181 public int hashCode() {
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200182 return Objects.hash(name, portNumber, speed, macAddress);
Georgios Katsikas83600982017-05-28 20:41:45 +0200183 }
184
185 @Override
186 public int compareTo(Object other) {
187 if (this == other) {
188 return 0;
189 }
190
191 if (other == null) {
192 return -1;
193 }
194
195 if (other instanceof NicDevice) {
196 NicDevice otherNic = (NicDevice) other;
197
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200198 if (this.portNumber() == otherNic.portNumber()) {
Georgios Katsikas83600982017-05-28 20:41:45 +0200199 return 0;
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200200 } else if (this.portNumber() > otherNic.portNumber()) {
Georgios Katsikas83600982017-05-28 20:41:45 +0200201 return 1;
202 } else {
203 return -1;
204 }
205 }
206
207 return -1;
208 }
209
Georgios Katsikas740d3282020-03-18 12:05:03 +0100210 public static final class Builder {
211 String name;
212 long portNumber = -1;
213 Type portType = Type.FIBER;
214 long speed = -1;
215 boolean status = false;
216 MacAddress macAddress = MacAddress.ZERO;
217 NicRxFilter rxFilterMechanisms = new NicRxFilter();
218
219 /**
220 * Port types that usually appear in commodity servers.
221 */
222 static final Map<String, Port.Type> PORT_TYPE_MAP =
223 Collections.unmodifiableMap(
224 new HashMap<String, Port.Type>() {
225 {
226 put(PARAM_NIC_PORT_TYPE_COPPER, Port.Type.COPPER);
227 put(PARAM_NIC_PORT_TYPE_FIBER, Port.Type.FIBER);
228 }
229 }
230 );
231
232 private Builder() {
233
234 }
235
236 /**
237 * Sets the name of this NIC.
238 *
239 * @param name NIC name
240 * @return builder object
241 */
242 public Builder setName(String name) {
243 this.name = name;
244
245 return this;
246 }
247
248 /**
249 * Sets the NIC's port number.
250 *
251 * @param portNumber NIC's port number
252 * @return builder object
253 */
254 public Builder setPortNumber(long portNumber) {
255 this.portNumber = portNumber;
256
257 return this;
258 }
259
260 /**
261 * Sets the NIC's port type as a string.
262 *
263 * @param portTypeStr NIC's port type
264 * @return builder object
265 */
266 public Builder setPortType(String portTypeStr) {
267 portType = PORT_TYPE_MAP.get(portTypeStr);
268 if (portType == null) {
269 throw new IllegalArgumentException(
270 portTypeStr + " is not a valid NIC port type");
271 }
272
273 return this;
274 }
275
276 /**
277 * Sets the NIC's speed.
278 *
279 * @param speed NIC's speed
280 * @return builder object
281 */
282 public Builder setSpeed(long speed) {
283 this.speed = speed;
284
285 return this;
286 }
287
288 /**
289 * Sets the NIC's status.
290 *
291 * @param status NIC's status
292 * @return builder object
293 */
294 public Builder setStatus(boolean status) {
295 this.status = status;
296
297 return this;
298 }
299
300 /**
301 * Sets the NIC's MAC address.
302 *
303 * @param macAddressStr NIC's MAC address
304 * @return builder object
305 */
306 public Builder setMacAddress(String macAddressStr) {
307 this.macAddress = MacAddress.valueOf(macAddressStr);
308
309 return this;
310 }
311
312 /**
313 * Sets the NIC's list of Rx filters as strings.
314 *
315 * @param rxFilters NIC's list of Rx filters
316 * @return builder object
317 */
318 public Builder setRxFilters(List<String> rxFilters) {
319 checkNotNull(rxFilters, MSG_NIC_RX_FILTERS_NULL);
320 for (String s : rxFilters) {
321 // Verify that this is a valid Rx filter
322 RxFilter rf = RxFilter.getByName(s);
323 checkNotNull(rf, MSG_NIC_RX_FILTER_NULL);
324 this.rxFilterMechanisms.addRxFilter(rf);
325 }
326
327 return this;
328 }
329
330 /**
331 * Creates a DefaultNicDevice object.
332 *
333 * @return DefaultNicDevice object
334 */
335 public DefaultNicDevice build() {
336 return new DefaultNicDevice(
337 name, portNumber, portType, speed,
338 status, macAddress, rxFilterMechanisms);
339 }
340
341 }
342
Georgios Katsikas83600982017-05-28 20:41:45 +0200343}