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 | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 21 | |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 25 | import com.google.common.base.Strings; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 26 | |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | import static org.onosproject.net.Port.Type; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | |
| 33 | /** |
| 34 | * Default implementation for NIC devices. |
| 35 | */ |
| 36 | public class DefaultNicDevice implements NicDevice, Comparable { |
| 37 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 38 | private final String name; |
| 39 | private final long portNumber; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 40 | private final long speed; |
| 41 | private final Type portType; |
| 42 | private boolean status; |
| 43 | private final MacAddress macAddress; |
| 44 | private NicRxFilter rxFilterMechanisms; |
| 45 | |
| 46 | // 200 Gbps or 200.000 Mbps |
| 47 | public static final long MAX_SPEED = 200000; |
| 48 | |
| 49 | public DefaultNicDevice( |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 50 | String name, |
| 51 | long portNumber, |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 52 | Type portType, |
| 53 | long speed, |
| 54 | boolean status, |
| 55 | String macStr, |
| 56 | NicRxFilter rxFilterMechanisms) { |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 57 | checkArgument(!Strings.isNullOrEmpty(name), "NIC name cannot be empty or NULL"); |
| 58 | checkArgument(portNumber >= 0, "NIC port number must be non-negative"); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 59 | checkNotNull(portType, "NIC port type cannot be null"); |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 60 | checkArgument((speed >= 0) && (speed <= MAX_SPEED), |
| 61 | "NIC speed must be positive and less or equal than " + MAX_SPEED + " Mbps"); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 62 | checkNotNull(macStr, "NIC MAC address cannot be null"); |
| 63 | checkNotNull(rxFilterMechanisms, "NIC Rx filter mechanisms cannot be null"); |
| 64 | |
| 65 | // Implies a problem |
| 66 | if (speed == 0) { |
| 67 | status = false; |
| 68 | } |
| 69 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 70 | this.name = name; |
| 71 | this.portNumber = portNumber; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 72 | this.speed = speed; |
| 73 | this.portType = portType; |
| 74 | this.status = status; |
| 75 | this.macAddress = MacAddress.valueOf(macStr); |
| 76 | this.rxFilterMechanisms = rxFilterMechanisms; |
| 77 | } |
| 78 | |
| 79 | @Override |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 80 | public String name() { |
| 81 | return this.name; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 85 | public long portNumber() { |
| 86 | return this.portNumber; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Type portType() { |
| 91 | return this.portType; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public long speed() { |
| 96 | return this.speed; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean status() { |
| 101 | return this.status; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void setStatus(boolean status) { |
| 106 | this.status = status; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public MacAddress macAddress() { |
| 111 | return this.macAddress; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public NicRxFilter rxFilterMechanisms() { |
| 116 | return this.rxFilterMechanisms; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void setRxFilterMechanisms(NicRxFilter rxFilterMechanisms) { |
| 121 | checkNotNull(rxFilterMechanisms, "NIC Rx filter mechanisms cannot be null"); |
| 122 | this.rxFilterMechanisms = rxFilterMechanisms; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void addRxFilterMechanism(NicRxFilter.RxFilter rxFilter) { |
| 127 | this.rxFilterMechanisms.addRxFilter(rxFilter); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(this) |
| 133 | .omitNullValues() |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 134 | .add("name", name()) |
| 135 | .add("port", portNumber()) |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 136 | .add("mac", macAddress.toString()) |
| 137 | .add("portType", portType()) |
| 138 | .add("speed", speed()) |
| 139 | .add("status", status ? "active" : "inactive") |
| 140 | .add("rxFilters", rxFilterMechanisms()) |
| 141 | .toString(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean equals(Object obj) { |
| 146 | if (obj == this) { |
| 147 | return true; |
| 148 | } |
| 149 | if (!(obj instanceof NicDevice)) { |
| 150 | return false; |
| 151 | } |
| 152 | NicDevice device = (NicDevice) obj; |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 153 | return this.name().equals(device.name()) && |
| 154 | this.portNumber() == device.portNumber() && |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 155 | this.speed == device.speed() && |
| 156 | this.macAddress.equals(device.macAddress()); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public int hashCode() { |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 161 | return Objects.hash(name, portNumber, speed, macAddress); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public int compareTo(Object other) { |
| 166 | if (this == other) { |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | if (other == null) { |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | if (other instanceof NicDevice) { |
| 175 | NicDevice otherNic = (NicDevice) other; |
| 176 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 177 | if (this.portNumber() == otherNic.portNumber()) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 178 | return 0; |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 179 | } else if (this.portNumber() > otherNic.portNumber()) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 180 | return 1; |
| 181 | } else { |
| 182 | return -1; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | } |