ONOS-1440: Implements port statistics feature, which polls port statistics of all devices every 10 seconds. Also, implemented a simple portstats ONOS CLI command to show the statistics.
Change-Id: I57e046ae2c2463a58b478d3a5b523422cde71ba2
diff --git a/core/api/src/main/java/org/onosproject/net/device/DefaultPortStatistics.java b/core/api/src/main/java/org/onosproject/net/device/DefaultPortStatistics.java
new file mode 100644
index 0000000..93c91ce
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/device/DefaultPortStatistics.java
@@ -0,0 +1,330 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.device;
+
+import org.onosproject.net.DeviceId;
+
+/**
+ * Default implementation of immutable port statistics.
+ */
+public final class DefaultPortStatistics implements PortStatistics {
+
+ private final DeviceId deviceId;
+ private final int port;
+ private final long packetsReceived;
+ private final long packetsSent;
+ private final long bytesReceived;
+ private final long bytesSent;
+ private final long packetsRxDropped;
+ private final long packetsTxDropped;
+ private final long packetsRxErrors;
+ private final long packetsTxErrors;
+ private final long durationSec;
+ private final long durationNano;
+
+ private DefaultPortStatistics(DeviceId deviceId,
+ int port,
+ long packetsReceived,
+ long packetsSent,
+ long bytesReceived,
+ long bytesSent,
+ long packetsRxDropped,
+ long packetsTxDropped,
+ long packetsRxErrors,
+ long packetsTxErrors,
+ long durationSec,
+ long durationNano) {
+ this.deviceId = deviceId;
+ this.port = port;
+ this.packetsReceived = packetsReceived;
+ this.packetsSent = packetsSent;
+ this.bytesReceived = bytesReceived;
+ this.bytesSent = bytesSent;
+ this.packetsRxDropped = packetsRxDropped;
+ this.packetsTxDropped = packetsTxDropped;
+ this.packetsRxErrors = packetsRxErrors;
+ this.packetsTxErrors = packetsTxErrors;
+ this.durationSec = durationSec;
+ this.durationNano = durationNano;
+ }
+
+ /**
+ * Creates a builder for DefaultPortStatistics object.
+ *
+ * @return builder object for DefaultPortStatistics object
+ */
+ public static DefaultPortStatistics.Builder builder() {
+ return new Builder();
+ }
+
+ @Override
+ public int port() {
+ return this.port;
+ }
+
+ @Override
+ public long packetsReceived() {
+ return this.packetsReceived;
+ }
+
+ @Override
+ public long packetsSent() {
+ return this.packetsSent;
+ }
+
+ @Override
+ public long bytesReceived() {
+ return this.bytesReceived;
+ }
+
+ @Override
+ public long bytesSent() {
+ return this.bytesSent;
+ }
+
+ @Override
+ public long packetsRxDropped() {
+ return this.packetsRxDropped;
+ }
+
+ @Override
+ public long packetsTxDropped() {
+ return this.packetsTxDropped;
+ }
+
+ @Override
+ public long packetsRxErrors() {
+ return this.packetsRxErrors;
+ }
+
+ @Override
+ public long packetsTxErrors() {
+ return this.packetsTxErrors;
+ }
+
+ @Override
+ public long durationSec() {
+ return this.durationSec;
+ }
+
+ @Override
+ public long durationNano() {
+ return this.durationNano;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder("device: " + deviceId + ", ");
+
+ sb.append("port: " + this.port + ", ");
+ sb.append("pktRx: " + this.packetsReceived + ", ");
+ sb.append("pktTx: " + this.packetsSent + ", ");
+ sb.append("byteRx: " + this.bytesReceived + ", ");
+ sb.append("byteTx: " + this.bytesSent + ", ");
+ sb.append("pktRxErr: " + this.packetsRxErrors + ", ");
+ sb.append("pktTxErr: " + this.packetsTxErrors + ", ");
+ sb.append("pktRxDrp: " + this.packetsRxDropped + ", ");
+ sb.append("pktTxDrp: " + this.packetsTxDropped);
+
+ return sb.toString();
+ }
+
+ public static final class Builder {
+
+ DeviceId deviceId;
+ int port;
+ long packetsReceived;
+ long packetsSent;
+ long bytesReceived;
+ long bytesSent;
+ long packetsRxDropped;
+ long packetsTxDropped;
+ long packetsRxErrors;
+ long packetsTxErrors;
+ long durationSec;
+ long durationNano;
+
+ private Builder() {
+
+ }
+
+ /**
+ * Sets port number.
+ *
+ * @param port port number
+ * @return builder object
+ */
+ public Builder setPort(int port) {
+ this.port = port;
+
+ return this;
+ }
+
+ /**
+ * Sets the device identifier.
+ *
+ * @param deviceId device identifier
+ * @return builder object
+ */
+ public Builder setDeviceId(DeviceId deviceId) {
+ this.deviceId = deviceId;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of packet received.
+ *
+ * @param packets number of packets received
+ * @return builder object
+ */
+ public Builder setPacketsReceived(long packets) {
+ packetsReceived = packets;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of packets sent.
+ *
+ * @param packets number of packets sent
+ * @return builder object
+ */
+ public Builder setPacketsSent(long packets) {
+ packetsSent = packets;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of received bytes.
+ *
+ * @param bytes number of received bytes.
+ * @return builder object
+ */
+ public Builder setBytesReceived(long bytes) {
+ bytesReceived = bytes;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of sent bytes.
+ *
+ * @param bytes number of sent bytes
+ * @return builder object
+ */
+ public Builder setBytesSent(long bytes) {
+ bytesSent = bytes;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of packets dropped by RX.
+ *
+ * @param packets number of packets dropped by RX
+ * @return builder object
+ */
+ public Builder setPacketsRxDropped(long packets) {
+ packetsRxDropped = packets;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of packets dropped by TX.
+ *
+ * @param packets
+ * @return builder object
+ */
+ public Builder setPacketsTxDropped(long packets) {
+ packetsTxDropped = packets;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of receive errors.
+ *
+ * @param packets number of receive errors
+ * @return builder object
+ */
+ public Builder setPacketsRxErrors(long packets) {
+ packetsRxErrors = packets;
+
+ return this;
+ }
+
+ /**
+ * Sets the number of transmit errors.
+ *
+ * @param packets number of transmit errors
+ * @return builder object
+ */
+ public Builder setPacketsTxErrors(long packets) {
+ packetsTxErrors = packets;
+
+ return this;
+ }
+
+ /**
+ * Sets the time port has been alive in seconds.
+ *
+ * @param sec time port has been alive in seconds
+ * @return builder object
+ */
+ public Builder setDurationSec(long sec) {
+ durationSec = sec;
+
+ return this;
+ }
+
+ /**
+ * Sets the time port has been alive in nano seconds.
+ *
+ * @param nano time port has been alive in nano seconds
+ * @return builder object
+ */
+ public Builder setDurationNano(long nano) {
+ durationNano = nano;
+
+ return this;
+ }
+
+ /**
+ * Creates a PortStatistics object.
+ *
+ * @return DefaultPortStatistics object
+ */
+ public DefaultPortStatistics build() {
+ return new DefaultPortStatistics(
+ deviceId,
+ port,
+ packetsReceived,
+ packetsSent,
+ bytesReceived,
+ bytesSent,
+ packetsRxDropped,
+ packetsTxDropped,
+ packetsRxErrors,
+ packetsTxErrors,
+ durationSec,
+ durationNano);
+ }
+
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/device/DeviceEvent.java b/core/api/src/main/java/org/onosproject/net/device/DeviceEvent.java
index 395c0eb..420f2da 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DeviceEvent.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DeviceEvent.java
@@ -72,7 +72,12 @@
/**
* Signifies that a port has been removed.
*/
- PORT_REMOVED
+ PORT_REMOVED,
+
+ /*
+ * Signifies that port statistics has been updated.
+ */
+ PORT_STATS_UPDATED
}
/**
diff --git a/core/api/src/main/java/org/onosproject/net/device/DeviceProviderService.java b/core/api/src/main/java/org/onosproject/net/device/DeviceProviderService.java
index f48543c..9a1d923 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DeviceProviderService.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DeviceProviderService.java
@@ -19,6 +19,7 @@
import org.onosproject.net.MastershipRole;
import org.onosproject.net.provider.ProviderService;
+import java.util.Collection;
import java.util.List;
/**
@@ -70,4 +71,12 @@
*/
void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response);
+ /**
+ * Sends statistics about all ports of a device.
+ *
+ * @param deviceId identity of the device
+ * @param portStatistics list of device port statistics
+ */
+ void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics);
+
}
diff --git a/core/api/src/main/java/org/onosproject/net/device/DeviceService.java b/core/api/src/main/java/org/onosproject/net/device/DeviceService.java
index 20b8a0b..1c0f647 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DeviceService.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DeviceService.java
@@ -78,6 +78,14 @@
List<Port> getPorts(DeviceId deviceId);
/**
+ * Returns the list of port statistics associated with the device.
+ *
+ * @param deviceId device identitifer
+ * @return list of port statistics
+ */
+ List<PortStatistics> getPortStatistics(DeviceId deviceId);
+
+ /**
* Returns the port with the specified number and hosted by the given device.
*
* @param deviceId device identifier
diff --git a/core/api/src/main/java/org/onosproject/net/device/DeviceStore.java b/core/api/src/main/java/org/onosproject/net/device/DeviceStore.java
index 4835504..88dc7f9 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DeviceStore.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DeviceStore.java
@@ -22,6 +22,7 @@
import org.onosproject.net.provider.ProviderId;
import org.onosproject.store.Store;
+import java.util.Collection;
import java.util.List;
/**
@@ -114,6 +115,26 @@
List<Port> getPorts(DeviceId deviceId);
/**
+ * Updates the port statistics of the specified device using the give port
+ * statistics.
+ *
+ * @param providerId provider identifier
+ * @param deviceId device identifier
+ * @param portStats list of port statistics
+ * @return ready to send event describing what occurred;
+ */
+ DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
+ Collection<PortStatistics> portStats);
+
+ /**
+ * Returns the list of port statistics of the specified device.
+ *
+ * @param deviceId device identifier
+ * @return list of port statistics of all ports of the device
+ */
+ List<PortStatistics> getPortStatistics(DeviceId deviceId);
+
+ /**
* Returns the specified device port.
*
* @param deviceId device identifier
@@ -137,4 +158,6 @@
* @return null if no such device, or was forwarded to remove master
*/
DeviceEvent removeDevice(DeviceId deviceId);
+
+
}
diff --git a/core/api/src/main/java/org/onosproject/net/device/PortStatistics.java b/core/api/src/main/java/org/onosproject/net/device/PortStatistics.java
new file mode 100644
index 0000000..b72d4e6
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/device/PortStatistics.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.device;
+
+/**
+ * Statistics of a port.
+ */
+public interface PortStatistics {
+
+ /**
+ * Returns the port number.
+ *
+ * @return port number
+ */
+ public int port();
+
+ /**
+ * Returns the number of packets received.
+ *
+ * @return the number of packets received
+ */
+ public long packetsReceived();
+
+ /**
+ * Returns the number of packets sent.
+ *
+ * @return the number of packets sent
+ */
+ public long packetsSent();
+
+ /**
+ * Returns the bytes received.
+ *
+ * @return the bytes received
+ */
+ public long bytesReceived();
+
+ /**
+ * Returns the bytes sent.
+ *
+ * @return the bytes sent
+ */
+ public long bytesSent();
+
+ /**
+ * Returns the number of packets dropped by RX.
+ *
+ * @return the number of packets dropped by RX
+ */
+ public long packetsRxDropped();
+
+ /**
+ * Returns the number of packets dropped by TX.
+ *
+ * @return the number of packets dropped by TX
+ */
+ public long packetsTxDropped();
+
+ /**
+ * Returns the number of transmit errors.
+ *
+ * @return the number of transmit errors
+ */
+ public long packetsRxErrors();
+
+ /**
+ * Returns the number of receive errors.
+ *
+ * @return the number of receive error
+ */
+ public long packetsTxErrors();
+
+ /**
+ * Returns the time port has been alive in seconds.
+ *
+ * @return the time port has been alive in seconds
+ */
+ public long durationSec();
+
+ /**
+ * Returns the time port has been alive in nano seconds.
+ *
+ * @return the time port has been alive in nano seconds
+ */
+ public long durationNano();
+
+}
diff --git a/core/api/src/test/java/org/onosproject/net/device/DeviceServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/device/DeviceServiceAdapter.java
index 422d062..4f72702 100644
--- a/core/api/src/test/java/org/onosproject/net/device/DeviceServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/net/device/DeviceServiceAdapter.java
@@ -69,6 +69,11 @@
}
@Override
+ public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
+ return null;
+ }
+
+ @Override
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
return null;
}
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
index 5cdaf99..842ae10 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
@@ -50,10 +50,12 @@
import org.onosproject.net.device.DeviceStore;
import org.onosproject.net.device.DeviceStoreDelegate;
import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.device.PortStatistics;
import org.onosproject.net.provider.AbstractProviderRegistry;
import org.onosproject.net.provider.AbstractProviderService;
import org.slf4j.Logger;
+import java.util.Collection;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -174,6 +176,12 @@
}
@Override
+ public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ return store.getPortStatistics(deviceId);
+ }
+
+ @Override
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portNumber, PORT_NUMBER_NULL);
@@ -463,6 +471,19 @@
}
}
+
+ @Override
+ public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ checkNotNull(portStatistics,
+ "Port statistics list cannot be null");
+ checkValidity();
+
+ DeviceEvent event = store.updatePortStatistics(this.provider().id(),
+ deviceId, portStatistics);
+
+ post(event);
+ }
}
// Posts the specified event to the local event dispatcher.
diff --git a/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java b/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
index 325242a..131000b 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
@@ -53,6 +53,7 @@
import org.onosproject.net.device.DeviceStore;
import org.onosproject.net.device.DeviceStoreDelegate;
import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.device.PortStatistics;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.store.AbstractStore;
import org.onosproject.store.Timestamp;
@@ -121,6 +122,8 @@
// cache of Device and Ports generated by compositing descriptions from providers
private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap();
private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = Maps.newConcurrentMap();
+ private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, PortStatistics>>
+ devicePortStats = Maps.newConcurrentMap();
// to be updated under Device lock
private final Map<DeviceId, Timestamp> offline = Maps.newHashMap();
@@ -800,6 +803,34 @@
}
@Override
+ public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
+ Collection<PortStatistics> portStats) {
+
+ ConcurrentMap<PortNumber, PortStatistics> statsMap = devicePortStats.get(deviceId);
+ if (statsMap == null) {
+ statsMap = Maps.newConcurrentMap();
+ devicePortStats.put(deviceId, statsMap);
+ }
+
+ for (PortStatistics stat: portStats) {
+ PortNumber portNumber = PortNumber.portNumber(stat.port());
+ statsMap.put(portNumber, stat);
+ }
+
+ return new DeviceEvent(PORT_STATS_UPDATED, devices.get(deviceId), null);
+ }
+
+ @Override
+ public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
+
+ Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId);
+ if (portStats == null) {
+ return Collections.emptyList();
+ }
+ return ImmutableList.copyOf(portStats.values());
+ }
+
+ @Override
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Map<PortNumber, Port> ports = devicePorts.get(deviceId);
return ports == null ? null : ports.get(portNumber);
diff --git a/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleDeviceStore.java b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleDeviceStore.java
index 2814be9..d8c55ff 100644
--- a/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleDeviceStore.java
+++ b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleDeviceStore.java
@@ -42,6 +42,7 @@
import org.onosproject.net.device.DeviceStore;
import org.onosproject.net.device.DeviceStoreDelegate;
import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.device.PortStatistics;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.store.AbstractStore;
import org.onlab.packet.ChassisId;
@@ -49,6 +50,7 @@
import org.slf4j.Logger;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -94,6 +96,8 @@
private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap();
private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>>
devicePorts = Maps.newConcurrentMap();
+ private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, PortStatistics>>
+ devicePortStats = Maps.newConcurrentMap();
// Available (=UP) devices
private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet();
@@ -416,12 +420,39 @@
}
@Override
+ public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
+ Collection<PortStatistics> portStats) {
+
+ ConcurrentMap<PortNumber, PortStatistics> statsMap = devicePortStats.get(deviceId);
+ if (statsMap == null) {
+ statsMap = Maps.newConcurrentMap();
+ devicePortStats.put(deviceId, statsMap);
+ }
+
+ for (PortStatistics stat: portStats) {
+ PortNumber portNumber = PortNumber.portNumber(stat.port());
+ statsMap.put(portNumber, stat);
+ }
+
+ return new DeviceEvent(PORT_STATS_UPDATED, devices.get(deviceId), null);
+ }
+
+ @Override
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Map<PortNumber, Port> ports = devicePorts.get(deviceId);
return ports == null ? null : ports.get(portNumber);
}
@Override
+ public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
+ Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId);
+ if (portStats == null) {
+ return Collections.emptyList();
+ }
+ return ImmutableList.copyOf(portStats.values());
+ }
+
+ @Override
public boolean isAvailable(DeviceId deviceId) {
return availableDevices.contains(deviceId);
}