Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.server.impl.devices; |
| 18 | |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 19 | import org.onosproject.drivers.server.devices.nic.NicDevice; |
| 20 | import org.onosproject.drivers.server.devices.nic.NicRxFilter; |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 21 | import org.onosproject.drivers.server.devices.nic.NicRxFilter.RxFilter; |
| 22 | import org.onosproject.net.Port; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 23 | |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | |
| 26 | import com.google.common.base.MoreObjects; |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 27 | import com.google.common.base.Strings; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 28 | |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 29 | import java.util.Collections; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 33 | import java.util.Objects; |
| 34 | |
| 35 | import static org.onosproject.net.Port.Type; |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static com.google.common.base.Preconditions.checkArgument; |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 38 | import static org.onosproject.drivers.server.Constants.MSG_NIC_NAME_NULL; |
| 39 | import static org.onosproject.drivers.server.Constants.MSG_NIC_MAC_NULL; |
| 40 | import static org.onosproject.drivers.server.Constants.MSG_NIC_PORT_NUMBER_NEGATIVE; |
| 41 | import static org.onosproject.drivers.server.Constants.MSG_NIC_PORT_TYPE_NULL; |
| 42 | import static org.onosproject.drivers.server.Constants.MSG_NIC_RX_FILTER_NULL; |
| 43 | import static org.onosproject.drivers.server.Constants.MSG_NIC_RX_FILTERS_NULL; |
| 44 | import static org.onosproject.drivers.server.Constants.MSG_NIC_SPEED_NEGATIVE; |
| 45 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_PORT_TYPE_COPPER; |
| 46 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_PORT_TYPE_FIBER; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Default implementation for NIC devices. |
| 50 | */ |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 51 | public final class DefaultNicDevice implements NicDevice, Comparable { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 52 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 53 | private final String name; |
| 54 | private final long portNumber; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 55 | private final Type portType; |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 56 | private final long speed; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 57 | private boolean status; |
| 58 | private final MacAddress macAddress; |
| 59 | private NicRxFilter rxFilterMechanisms; |
| 60 | |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 61 | private DefaultNicDevice( |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 62 | String name, |
| 63 | long portNumber, |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 64 | Type portType, |
| 65 | long speed, |
| 66 | boolean status, |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 67 | MacAddress mac, |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 68 | NicRxFilter rxFilterMechanisms) { |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 69 | 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 Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 76 | |
| 77 | // Implies a problem |
| 78 | if (speed == 0) { |
| 79 | status = false; |
| 80 | } |
| 81 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 82 | this.name = name; |
| 83 | this.portNumber = portNumber; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 84 | this.speed = speed; |
| 85 | this.portType = portType; |
| 86 | this.status = status; |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 87 | 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 Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 101 | public String name() { |
| 102 | return this.name; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 106 | public long portNumber() { |
| 107 | return this.portNumber; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 108 | } |
| 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 Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 147 | public void addRxFilterMechanism(RxFilter rxFilter) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 148 | this.rxFilterMechanisms.addRxFilter(rxFilter); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(this) |
| 154 | .omitNullValues() |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 155 | .add("name", name()) |
| 156 | .add("port", portNumber()) |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 157 | .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 Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 174 | return this.name().equals(device.name()) && |
| 175 | this.portNumber() == device.portNumber() && |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 176 | this.speed == device.speed() && |
| 177 | this.macAddress.equals(device.macAddress()); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public int hashCode() { |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 182 | return Objects.hash(name, portNumber, speed, macAddress); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 183 | } |
| 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 Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 198 | if (this.portNumber() == otherNic.portNumber()) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 199 | return 0; |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 200 | } else if (this.portNumber() > otherNic.portNumber()) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 201 | return 1; |
| 202 | } else { |
| 203 | return -1; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return -1; |
| 208 | } |
| 209 | |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 210 | 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 Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 343 | } |