blob: 9f782d86b9f7b8ef5d50b59e4cf6df5b6f4521d9 [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 Katsikas83600982017-05-28 20:41:45 +020021
22import org.onlab.packet.MacAddress;
23
24import com.google.common.base.MoreObjects;
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020025import com.google.common.base.Strings;
Georgios Katsikas83600982017-05-28 20:41:45 +020026
27import java.util.Objects;
28
29import static org.onosproject.net.Port.Type;
30import static com.google.common.base.Preconditions.checkNotNull;
31import static com.google.common.base.Preconditions.checkArgument;
32
33/**
34 * Default implementation for NIC devices.
35 */
36public class DefaultNicDevice implements NicDevice, Comparable {
37
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020038 private final String name;
39 private final long portNumber;
Georgios Katsikas83600982017-05-28 20:41:45 +020040 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 Katsikas80e0b9f2018-07-21 20:29:18 +020050 String name,
51 long portNumber,
Georgios Katsikas83600982017-05-28 20:41:45 +020052 Type portType,
53 long speed,
54 boolean status,
55 String macStr,
56 NicRxFilter rxFilterMechanisms) {
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020057 checkArgument(!Strings.isNullOrEmpty(name), "NIC name cannot be empty or NULL");
58 checkArgument(portNumber >= 0, "NIC port number must be non-negative");
Georgios Katsikas83600982017-05-28 20:41:45 +020059 checkNotNull(portType, "NIC port type cannot be null");
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020060 checkArgument((speed >= 0) && (speed <= MAX_SPEED),
61 "NIC speed must be positive and less or equal than " + MAX_SPEED + " Mbps");
Georgios Katsikas83600982017-05-28 20:41:45 +020062 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 Katsikas80e0b9f2018-07-21 20:29:18 +020070 this.name = name;
71 this.portNumber = portNumber;
Georgios Katsikas83600982017-05-28 20:41:45 +020072 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 Katsikas80e0b9f2018-07-21 20:29:18 +020080 public String name() {
81 return this.name;
Georgios Katsikas83600982017-05-28 20:41:45 +020082 }
83
84 @Override
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020085 public long portNumber() {
86 return this.portNumber;
Georgios Katsikas83600982017-05-28 20:41:45 +020087 }
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 Katsikas80e0b9f2018-07-21 20:29:18 +0200134 .add("name", name())
135 .add("port", portNumber())
Georgios Katsikas83600982017-05-28 20:41:45 +0200136 .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 Katsikas80e0b9f2018-07-21 20:29:18 +0200153 return this.name().equals(device.name()) &&
154 this.portNumber() == device.portNumber() &&
Georgios Katsikas83600982017-05-28 20:41:45 +0200155 this.speed == device.speed() &&
156 this.macAddress.equals(device.macAddress());
157 }
158
159 @Override
160 public int hashCode() {
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200161 return Objects.hash(name, portNumber, speed, macAddress);
Georgios Katsikas83600982017-05-28 20:41:45 +0200162 }
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 Katsikas80e0b9f2018-07-21 20:29:18 +0200177 if (this.portNumber() == otherNic.portNumber()) {
Georgios Katsikas83600982017-05-28 20:41:45 +0200178 return 0;
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +0200179 } else if (this.portNumber() > otherNic.portNumber()) {
Georgios Katsikas83600982017-05-28 20:41:45 +0200180 return 1;
181 } else {
182 return -1;
183 }
184 }
185
186 return -1;
187 }
188
189}