CORD-13:Table Statistics support along with CLI and REST
Change-Id: Ic7facc73754c4b1e7c9c5a9eecd217f89a67e135
diff --git a/core/api/src/main/java/org/onosproject/net/flow/DefaultTableStatisticsEntry.java b/core/api/src/main/java/org/onosproject/net/flow/DefaultTableStatisticsEntry.java
new file mode 100644
index 0000000..929b285
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/flow/DefaultTableStatisticsEntry.java
@@ -0,0 +1,89 @@
+/*
+ * 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.flow;
+
+import org.onosproject.net.DeviceId;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Default implementation of table statistics entry interface.
+ */
+public final class DefaultTableStatisticsEntry implements TableStatisticsEntry {
+
+ private final DeviceId deviceId;
+ private final int tableId;
+ private final long activeFlowEntries;
+ private final long packetsLookedupCount;
+ private final long packetsMatchedCount;
+
+ /**
+ * Default table statistics constructor.
+ *
+ * @param deviceId device identifier
+ * @param tableId table identifier
+ * @param activeFlowEntries number of active flow entries in the table
+ * @param packetsLookedupCount number of packets looked up in table
+ * @param packetsMatchedCount number of packets that hit table
+ */
+ public DefaultTableStatisticsEntry(DeviceId deviceId,
+ int tableId,
+ long activeFlowEntries,
+ long packetsLookedupCount,
+ long packetsMatchedCount) {
+ this.deviceId = checkNotNull(deviceId);
+ this.tableId = tableId;
+ this.activeFlowEntries = activeFlowEntries;
+ this.packetsLookedupCount = packetsLookedupCount;
+ this.packetsMatchedCount = packetsMatchedCount;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder("device: " + deviceId + ", ");
+
+ sb.append("tableId: " + this.tableId + ", ");
+ sb.append("activeEntries: " + this.activeFlowEntries + ", ");
+ sb.append("packetsLookedUp: " + this.packetsLookedupCount + ", ");
+ sb.append("packetsMatched: " + this.packetsMatchedCount);
+
+ return sb.toString();
+ }
+
+ @Override
+ public int tableId() {
+ return tableId;
+ }
+
+ @Override
+ public long activeFlowEntries() {
+ return activeFlowEntries;
+ }
+
+ @Override
+ public long packetsLookedup() {
+ return packetsLookedupCount;
+ }
+
+ @Override
+ public long packetsMatched() {
+ return packetsMatchedCount;
+ }
+
+ @Override
+ public DeviceId deviceId() {
+ return deviceId;
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java
index 48aa504..aefa96b 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java
@@ -15,6 +15,8 @@
*/
package org.onosproject.net.flow;
+import java.util.List;
+
import org.onosproject.net.DeviceId;
import org.onosproject.net.provider.ProviderService;
@@ -50,6 +52,15 @@
void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries);
/**
+ * Pushes the collection of table statistics entries currently extracted
+ * from the given device.
+ *
+ * @param deviceId device identifier
+ * @param tableStatsEntries collection of flow table statistics entries
+ */
+ void pushTableStatistics(DeviceId deviceId, List<TableStatisticsEntry> tableStatsEntries);
+
+ /**
* Indicates to the core that the requested batch operation has
* been completed.
*
@@ -57,5 +68,4 @@
* @param operation the resulting outcome of the operation
*/
void batchOperationCompleted(long batchId, CompletedBatchOperation operation);
-
}
diff --git a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java
index d4f959c..ee8d5a9 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java
@@ -104,4 +104,11 @@
*/
void apply(FlowRuleOperations ops);
+ /**
+ * Returns the collection of flow table statistics of the specified device.
+ *
+ * @param deviceId device identifier
+ * @return collection of flow table statistics
+ */
+ Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId);
}
diff --git a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java
index cece989..d81c73c 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java
@@ -15,6 +15,8 @@
*/
package org.onosproject.net.flow;
+import java.util.List;
+
import org.onosproject.net.DeviceId;
import org.onosproject.store.Store;
@@ -93,4 +95,23 @@
* @return flow_removed event, or null if nothing removed
*/
FlowRuleEvent removeFlowRule(FlowEntry rule);
+
+ /**
+ * Updates the flow table statistics of the specified device using
+ * the given statistics.
+ *
+ * @param deviceId device identifier
+ * @param tableStats list of table statistics
+ * @return ready to send event describing what occurred;
+ */
+ FlowRuleEvent updateTableStatistics(DeviceId deviceId,
+ List<TableStatisticsEntry> tableStats);
+
+ /**
+ * Returns the flow table statistics associated with a device.
+ *
+ * @param deviceId the device ID
+ * @return the flow table statistics
+ */
+ Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId);
}
diff --git a/core/api/src/main/java/org/onosproject/net/flow/TableStatisticsEntry.java b/core/api/src/main/java/org/onosproject/net/flow/TableStatisticsEntry.java
new file mode 100644
index 0000000..563f31c
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/flow/TableStatisticsEntry.java
@@ -0,0 +1,59 @@
+/*
+ * 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.flow;
+
+import org.onosproject.net.DeviceId;
+
+/**
+ * Interface for flow table statistics of a device.
+ */
+public interface TableStatisticsEntry {
+
+ /**
+ * Returns the device Id.
+ *
+ * @return device id
+ */
+ DeviceId deviceId();
+
+ /**
+ * Returns the table number.
+ *
+ * @return table number
+ */
+ int tableId();
+
+ /**
+ * Returns the number of active flow entries in this table.
+ *
+ * @return the number of active flow entries
+ */
+ long activeFlowEntries();
+
+ /**
+ * Returns the number of packets looked up in the table.
+ *
+ * @return the number of packets looked up in the table
+ */
+ long packetsLookedup();
+
+ /**
+ * Returns the number of packets that successfully matched in the table.
+ *
+ * @return the number of packets that successfully matched in the table
+ */
+ long packetsMatched();
+}
diff --git a/core/api/src/test/java/org/onosproject/net/flow/FlowRuleServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/flow/FlowRuleServiceAdapter.java
index c7b7879..56e5911 100644
--- a/core/api/src/test/java/org/onosproject/net/flow/FlowRuleServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/net/flow/FlowRuleServiceAdapter.java
@@ -35,17 +35,14 @@
@Override
public void applyFlowRules(FlowRule... flowRules) {
-
}
@Override
public void removeFlowRules(FlowRule... flowRules) {
-
}
@Override
public void removeFlowRulesById(ApplicationId appId) {
-
}
@Override
@@ -60,16 +57,18 @@
@Override
public void apply(FlowRuleOperations ops) {
-
}
@Override
public void addListener(FlowRuleListener listener) {
-
}
@Override
public void removeListener(FlowRuleListener listener) {
+ }
+ @Override
+ public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
+ return null;
}
}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index eb53152..3433b3b 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -16,6 +16,7 @@
package org.onosproject.codec.impl;
import com.google.common.collect.ImmutableSet;
+
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -36,6 +37,7 @@
import org.onosproject.net.driver.Driver;
import org.onosproject.net.flow.FlowEntry;
import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.TableStatisticsEntry;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.criteria.Criterion;
@@ -99,6 +101,7 @@
registerCodec(Driver.class, new DriverCodec());
registerCodec(GroupBucket.class, new GroupBucketCodec());
registerCodec(Load.class, new LoadCodec());
+ registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
log.info("Started");
}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/TableStatisticsEntryCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/TableStatisticsEntryCodec.java
new file mode 100644
index 0000000..7834ceb
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/TableStatisticsEntryCodec.java
@@ -0,0 +1,46 @@
+/*
+ * 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.codec.impl;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.flow.TableStatisticsEntry;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Table statistics entry JSON codec.
+ */
+public final class TableStatisticsEntryCodec extends JsonCodec<TableStatisticsEntry> {
+
+ @Override
+ public ObjectNode encode(TableStatisticsEntry entry, CodecContext context) {
+ checkNotNull(entry, "Table Statistics entry cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put("tableId", entry.tableId())
+ .put("deviceId", entry.deviceId().toString())
+ .put("activeEntries", entry.activeFlowEntries())
+ .put("packetsLookedUp", entry.packetsLookedup())
+ .put("packetsMatched", entry.packetsMatched());
+
+ return result;
+ }
+
+}
+
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java
index c8c92aa..bed32a2 100644
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java
+++ b/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java
@@ -20,8 +20,10 @@
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.SettableFuture;
+
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -44,6 +46,7 @@
import org.onosproject.net.flow.FlowRuleStore;
import org.onosproject.net.flow.FlowRuleStoreDelegate;
import org.onosproject.net.flow.StoredFlowEntry;
+import org.onosproject.net.flow.TableStatisticsEntry;
import org.onosproject.store.AbstractStore;
import org.slf4j.Logger;
@@ -79,6 +82,9 @@
private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
flowEntries = new ConcurrentHashMap<>();
+ private final ConcurrentMap<DeviceId, List<TableStatisticsEntry>>
+ deviceTableStats = new ConcurrentHashMap<>();
+
private final AtomicInteger localBatchIdGen = new AtomicInteger();
// TODO: make this configurable
@@ -97,6 +103,7 @@
@Deactivate
public void deactivate() {
+ deviceTableStats.clear();
flowEntries.clear();
log.info("Stopped");
}
@@ -315,4 +322,20 @@
}
}
}
+
+ @Override
+ public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
+ List<TableStatisticsEntry> tableStats) {
+ deviceTableStats.put(deviceId, tableStats);
+ return null;
+ }
+
+ @Override
+ public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
+ List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
+ if (tableStats == null) {
+ return Collections.emptyList();
+ }
+ return ImmutableList.copyOf(tableStats);
+ }
}
diff --git a/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java b/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java
index 9bbd0aa..5958d1f 100644
--- a/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java
+++ b/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java
@@ -22,6 +22,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
+
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -58,6 +59,7 @@
import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.flow.FlowRuleStore;
import org.onosproject.net.flow.FlowRuleStoreDelegate;
+import org.onosproject.net.flow.TableStatisticsEntry;
import org.onosproject.net.provider.AbstractProviderService;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
@@ -448,6 +450,12 @@
operation
));
}
+
+ @Override
+ public void pushTableStatistics(DeviceId deviceId,
+ List<TableStatisticsEntry> tableStats) {
+ store.updateTableStatistics(deviceId, tableStats);
+ }
}
// Store delegate to re-post events emitted from the store.
@@ -603,4 +611,10 @@
}
}
+
+ @Override
+ public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
+ checkPermission(FLOWRULE_READ);
+ return store.getTableStatistics(deviceId);
+ }
}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/flow/impl/NewDistributedFlowRuleStore.java b/core/store/dist/src/main/java/org/onosproject/store/flow/impl/NewDistributedFlowRuleStore.java
index de7a3ac..8cd63e7 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/flow/impl/NewDistributedFlowRuleStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/flow/impl/NewDistributedFlowRuleStore.java
@@ -16,6 +16,7 @@
package org.onosproject.store.flow.impl;
import com.google.common.base.Objects;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
@@ -57,6 +58,7 @@
import org.onosproject.net.flow.FlowRuleStore;
import org.onosproject.net.flow.FlowRuleStoreDelegate;
import org.onosproject.net.flow.StoredFlowEntry;
+import org.onosproject.net.flow.TableStatisticsEntry;
import org.onosproject.store.AbstractStore;
import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
import org.onosproject.store.cluster.messaging.ClusterMessage;
@@ -64,9 +66,16 @@
import org.onosproject.store.flow.ReplicaInfoEvent;
import org.onosproject.store.flow.ReplicaInfoEventListener;
import org.onosproject.store.flow.ReplicaInfoService;
+import org.onosproject.store.impl.MastershipBasedTimestamp;
+import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.serializers.KryoSerializer;
import org.onosproject.store.serializers.StoreSerializer;
import org.onosproject.store.serializers.custom.DistributedStoreSerializers;
+import org.onosproject.store.service.EventuallyConsistentMap;
+import org.onosproject.store.service.EventuallyConsistentMapEvent;
+import org.onosproject.store.service.EventuallyConsistentMapListener;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.WallClockTimestamp;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
@@ -151,6 +160,13 @@
private final ScheduledExecutorService backupSenderExecutor =
Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/flow", "backup-sender"));
+ private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats;
+ private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener =
+ new InternalTableStatsListener();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected StorageService storageService;
+
protected static final StoreSerializer SERIALIZER = new KryoSerializer() {
@Override
protected void setupKryoPool() {
@@ -161,6 +177,11 @@
}
};
+ protected static final KryoNamespace.Builder SERIALIZER_BUILDER = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(MastershipBasedTimestamp.class);
+
+
private IdGenerator idGenerator;
private NodeId local;
@@ -186,6 +207,15 @@
TimeUnit.MILLISECONDS);
}
+ deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder()
+ .withName("onos-flow-table-stats")
+ .withSerializer(SERIALIZER_BUILDER)
+ .withAntiEntropyPeriod(5, TimeUnit.SECONDS)
+ .withTimestampProvider((k, v) -> new WallClockTimestamp())
+ .withTombstonesDisabled()
+ .build();
+ deviceTableStats.addListener(tableStatsListener);
+
logConfig("Started");
}
@@ -197,6 +227,8 @@
}
configService.unregisterProperties(getClass(), false);
unregisterMessageHandlers();
+ deviceTableStats.removeListener(tableStatsListener);
+ deviceTableStats.destroy();
messageHandlingExecutor.shutdownNow();
backupSenderExecutor.shutdownNow();
log.info("Stopped");
@@ -786,4 +818,36 @@
return backedupDevices;
}
}
+
+ @Override
+ public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
+ List<TableStatisticsEntry> tableStats) {
+ deviceTableStats.put(deviceId, tableStats);
+ return null;
+ }
+
+ @Override
+ public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
+ NodeId master = mastershipService.getMasterFor(deviceId);
+
+ if (master == null) {
+ log.debug("Failed to getTableStats: No master for {}", deviceId);
+ return Collections.emptyList();
+ }
+
+ List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
+ if (tableStats == null) {
+ return Collections.emptyList();
+ }
+ return ImmutableList.copyOf(tableStats);
+ }
+
+ private class InternalTableStatsListener
+ implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> {
+ @Override
+ public void event(EventuallyConsistentMapEvent<DeviceId,
+ List<TableStatisticsEntry>> event) {
+ //TODO: Generate an event to listeners (do we need?)
+ }
+ }
}
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index 4734672..5b5056c 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -84,6 +84,7 @@
import org.onosproject.net.flow.CompletedBatchOperation;
import org.onosproject.net.flow.DefaultFlowEntry;
import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTableStatisticsEntry;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.FlowEntry;
@@ -95,6 +96,7 @@
import org.onosproject.net.flow.FlowRuleEvent;
import org.onosproject.net.flow.FlowRuleExtPayLoad;
import org.onosproject.net.flow.StoredFlowEntry;
+import org.onosproject.net.flow.TableStatisticsEntry;
import org.onosproject.net.flow.criteria.Criterion;
import org.onosproject.net.flow.criteria.EthCriterion;
import org.onosproject.net.flow.criteria.EthTypeCriterion;
@@ -421,7 +423,9 @@
DefaultAnnotations.class,
PortStatistics.class,
DefaultPortStatistics.class,
- IntentDomainId.class
+ IntentDomainId.class,
+ TableStatisticsEntry.class,
+ DefaultTableStatisticsEntry.class
)
.register(new DefaultApplicationIdSerializer(), DefaultApplicationId.class)
.register(new URISerializer(), URI.class)