[ONOS-7704] device memory/cpu stats for pica8 switch using Switch_Inventory DB
Change-Id: I1956d06ab373119da59561252f5b35561f8f5619
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/DeviceCpuStats.java b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceCpuStats.java
new file mode 100644
index 0000000..ccc1fc1
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceCpuStats.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.behaviour;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * A representation of cpu stats of device.
+ */
+public class DeviceCpuStats {
+ private float used;
+
+ /**
+ * Instantiates DeviceCpuStats object with default value.
+ */
+ public DeviceCpuStats() {
+ used = 0.0f;
+ }
+
+ /**
+ * Creates DeviceCpuStats object with given value.
+ *
+ * @param used cpu usage of device
+ */
+ public DeviceCpuStats(float used) {
+ this.used = used;
+ }
+
+ /**
+ * Get cpu usage of device.
+ *
+ * @return usedCpu, cpu usage stats of device
+ */
+ public float getUsed() {
+ return used;
+ }
+
+ /**
+ * Set cpu usage of device.
+ *
+ * @param used cpu usage of device
+ */
+ public void setUsed(float used) {
+ this.used = used;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("used", used)
+ .toString();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ DeviceCpuStats that = (DeviceCpuStats) o;
+ return Float.compare(that.used, used) == 0;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(used);
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/DeviceMemoryStats.java b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceMemoryStats.java
new file mode 100644
index 0000000..e90d9e8
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceMemoryStats.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.behaviour;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * A representation of memory stats of device.
+ */
+
+public class DeviceMemoryStats {
+ private long free = 0; // in Bytes
+ private long used = 0;
+ private long total = 0;
+
+ /**
+ * Instantiates DeviceMemoryStats object.
+ */
+ public DeviceMemoryStats() {
+ }
+
+ /**
+ * Creates DeviceMemoryStats object with given data.
+ *
+ * @param free free memory
+ * @param used used memory
+ * @param total total memory
+ */
+ public DeviceMemoryStats(long free, long used, long total) {
+ this.free = free;
+ this.used = used;
+ this.total = total;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("free", free)
+ .add("used", used)
+ .add("total", total)
+ .toString();
+ }
+
+ /**
+ * Get total memory of device.
+ *
+ * @return totalMmeory, total memory
+ */
+ public long getTotal() {
+ return total;
+ }
+
+ /**
+ * Get used memory of device.
+ *
+ * @return usedMemory, used memory
+ */
+ public long getUsed() {
+ return used;
+ }
+
+ /**
+ * Get free memory of device.
+ *
+ * @return freeMemory, free memory
+ */
+ public long getFree() {
+ return free;
+ }
+
+ /**
+ * Set free memory of device.
+ *
+ * @param free free memory stats of device
+ */
+ public void setFree(long free) {
+ this.free = free;
+ }
+
+ /**
+ * Set used memory of device.
+ *
+ * @param used used memory stats of device
+ */
+ public void setUsed(long used) {
+ this.used = used;
+ }
+
+ /**
+ * Set total memory of device.
+ *
+ * @param total total memory stats of device
+ */
+ public void setTotal(long total) {
+ this.total = total;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ DeviceMemoryStats that = (DeviceMemoryStats) o;
+ return free == that.free &&
+ used == that.used &&
+ total == that.total;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(free, used, total);
+ }
+}
+
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/DeviceSystemStatisticsQuery.java b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceSystemStatisticsQuery.java
new file mode 100644
index 0000000..ba34872
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceSystemStatisticsQuery.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.behaviour;
+
+import org.onosproject.net.driver.HandlerBehaviour;
+
+import java.util.Optional;
+
+/**
+ * A HandlerBehaviour to get System Stats for the Device.
+ */
+public interface DeviceSystemStatisticsQuery extends HandlerBehaviour {
+ /**
+ * Get System stats available for the device.
+ * @return success or failure
+ */
+
+ Optional<DeviceSystemStats> getDeviceSystemStats();
+}
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/DeviceSystemStats.java b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceSystemStats.java
new file mode 100644
index 0000000..ccddd85
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/DeviceSystemStats.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.behaviour;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * A representation of system stats of device.
+ */
+public class DeviceSystemStats {
+
+ private final DeviceMemoryStats memory;
+ private final DeviceCpuStats cpu;
+
+ /**
+ * Creates deviceSystemStats object.
+ *
+ * @param memoryStats memory statisics of the device
+ * @param cpuStats cpu statistics of the device
+ */
+ public DeviceSystemStats(DeviceMemoryStats memoryStats, DeviceCpuStats cpuStats) {
+ this.memory = memoryStats;
+ this.cpu = cpuStats;
+ }
+
+ /**
+ * Get memory usage statistics.
+ *
+ * @return deviceMemoryStats, device memory usage stats in KB
+ */
+ public DeviceMemoryStats getMemory() {
+ return this.memory;
+ }
+
+ /**
+ * Get cpu usage statistics.
+ *
+ * @return deviceCpuStats, device cpu usage stats
+ */
+ public DeviceCpuStats getCpu() {
+ return this.cpu;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("memory", memory)
+ .add("cpu", cpu)
+ .toString();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ DeviceSystemStats that = (DeviceSystemStats) o;
+ return Objects.equals(memory, that.memory) &&
+ Objects.equals(cpu, that.cpu);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(memory, cpu);
+ }
+}
+
+
diff --git a/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/PicaOvsdbSystemStatsQuery.java b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/PicaOvsdbSystemStatsQuery.java
new file mode 100644
index 0000000..1a5b41f
--- /dev/null
+++ b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/PicaOvsdbSystemStatsQuery.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.drivers.ovsdb;
+
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.DeviceCpuStats;
+import org.onosproject.net.behaviour.DeviceMemoryStats;
+import org.onosproject.net.behaviour.DeviceSystemStatisticsQuery;
+import org.onosproject.net.behaviour.DeviceSystemStats;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbNodeId;
+import org.onosproject.ovsdb.controller.driver.PicaOvsdbClient;
+
+import java.util.Optional;
+
+/**
+ * The implementation of PicaOvsdbSystemStatsQuery.
+ */
+public class PicaOvsdbSystemStatsQuery extends AbstractHandlerBehaviour
+ implements DeviceSystemStatisticsQuery {
+
+ // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
+ // is used in the core. So DeviceId need be changed to OvsdbNodeId.
+ private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
+ String[] splits = deviceId.toString().split(":");
+ if (splits == null || splits.length < 1) {
+ return null;
+ }
+ IpAddress ipAddress = IpAddress.valueOf(splits[1]);
+ return new OvsdbNodeId(ipAddress, 0);
+ }
+
+ // Used for getting OvsdbClientService.
+ private OvsdbClientService getOvsdbClientService(DriverHandler handler) {
+ OvsdbController ovsController = handler.get(OvsdbController.class);
+ DeviceId deviceId = handler.data().deviceId();
+ OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId);
+ return ovsController.getOvsdbClient(nodeId);
+ }
+
+ /**
+ * Get cpu usage of device.
+ *
+ * @return cpuStats, device cpu usage stats if available
+ */
+ private Optional<DeviceCpuStats> getCpuUsage() {
+ OvsdbClientService client = getOvsdbClientService(handler());
+ if (client == null) {
+ return Optional.empty();
+ }
+ PicaOvsdbClient picaOvsdbClient = new PicaOvsdbClient(client);
+ return picaOvsdbClient.getDeviceCpuUsage();
+ }
+
+ /**
+ * Get memory usage of device in KB.
+ *
+ * @return memoryStats, device memory usage stats if available
+ */
+ private Optional<DeviceMemoryStats> getMemoryUsage() {
+ OvsdbClientService client = getOvsdbClientService(handler());
+ if (client == null) {
+ return Optional.empty();
+ }
+ PicaOvsdbClient picaOvsdbClient = new PicaOvsdbClient(client);
+ return picaOvsdbClient.getDeviceMemoryUsage();
+ }
+
+ /**
+ * Get system stats (cpu/mmeory usage) of device.
+ *
+ * @return deviceSystemStats, system stats (cpu/memory usage) of device if available
+ */
+ @Override
+ public Optional<DeviceSystemStats> getDeviceSystemStats() {
+ Optional<DeviceCpuStats> cpuStats = getCpuUsage();
+ Optional<DeviceMemoryStats> memoryStats = getMemoryUsage();
+
+ if (cpuStats.isPresent() && memoryStats.isPresent()) {
+ DeviceSystemStats systemStats = new DeviceSystemStats(memoryStats.get(), cpuStats.get());
+ return Optional.of(systemStats);
+ } else {
+ return Optional.empty();
+ }
+ }
+}
diff --git a/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml b/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
index 33b970a..970a0f6 100644
--- a/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
+++ b/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
@@ -38,5 +38,11 @@
<driver name="liquidio" extends="ovs"
manufacturer="Cavium, Inc\." hwVersion="LiquidIO-II 210 SVN" swVersion="2\..*">
</driver>
+ <driver name="pica8-ovsdb" extends="default"
+ manufacturer="Pica8" hwVersion="*" swVersion="*">
+ <behaviour api="org.onosproject.net.behaviour.DeviceSystemStatisticsQuery"
+ impl="org.onosproject.drivers.ovsdb.PicaOvsdbSystemStatsQuery"/>
+ </driver>
+
</drivers>
diff --git a/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbClientService.java b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbClientService.java
index dfa1518..d552a70 100644
--- a/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbClientService.java
+++ b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbClientService.java
@@ -17,11 +17,14 @@
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.behaviour.ControllerInfo;
+import org.onosproject.net.behaviour.DeviceCpuStats;
+import org.onosproject.net.behaviour.DeviceMemoryStats;
import org.onosproject.net.behaviour.MirroringName;
import org.onosproject.net.behaviour.MirroringStatistics;
import org.onosproject.net.behaviour.QosId;
@@ -33,6 +36,7 @@
import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
import com.google.common.util.concurrent.ListenableFuture;
+import org.onosproject.ovsdb.rfc.table.OvsdbTable;
/**
* Represents to provider facing side of a node.
@@ -372,4 +376,27 @@
* @return errorstatus true if input port list contains error, false otherwise
*/
boolean getPortError(List<OvsdbPortName> portNames, DeviceId bridgeId);
+
+ /**
+ * Gets First row for the given table of given DB.
+ *
+ * @param dbName db name
+ * @param tblName table name
+ * @return first table entry
+ */
+
+ public Optional<Object> getFirstRow(String dbName, OvsdbTable tblName);
+
+ /**
+ * Gets device CPU usage in percentage.
+ * @return device memory usage.
+ */
+ Optional<DeviceCpuStats> getDeviceCpuUsage();
+
+ /**
+ * Gets device memory usage in kilobytes.
+ * @return device memory usage.
+ */
+ Optional<DeviceMemoryStats> getDeviceMemoryUsage();
+
}
diff --git a/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbConstant.java b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbConstant.java
index 6919461..12447e3 100644
--- a/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbConstant.java
+++ b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/OvsdbConstant.java
@@ -112,4 +112,16 @@
public static final int OFPORT_ERROR = -1;
public static final boolean SERVER_MODE = true;
+
+ /** Ovsdb database Switch_Inventory. */
+ public static final String SWINVENTORY_DBNAME = "Switch_Inventory";
+
+ /** Cpu_Memory_Data table. */
+ public static final String CPU_MEMORY_DATA = "Cpu_Memory_Data";
+
+ /** Cpu column of Cpu_Memory_Data table. */
+ public static final String DEVICE_CPU = "cpu";
+
+ /** Memory column of Cpu_Memory_Data table. */
+ public static final String DEVICE_MEMORY = "memory";
}
diff --git a/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/driver/DefaultOvsdbClient.java b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/driver/DefaultOvsdbClient.java
index 17d931f..38f8ede 100644
--- a/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/driver/DefaultOvsdbClient.java
+++ b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/driver/DefaultOvsdbClient.java
@@ -31,6 +31,8 @@
import org.onosproject.net.PortNumber;
import org.onosproject.net.behaviour.ControlProtocolVersion;
import org.onosproject.net.behaviour.ControllerInfo;
+import org.onosproject.net.behaviour.DeviceCpuStats;
+import org.onosproject.net.behaviour.DeviceMemoryStats;
import org.onosproject.net.behaviour.MirroringName;
import org.onosproject.net.behaviour.MirroringStatistics;
import org.onosproject.net.behaviour.QosId;
@@ -1871,4 +1873,66 @@
.filter(intf -> Objects.nonNull(intf) && portName.equalsIgnoreCase(intf.getName()))
.findFirst().orElse(null);
}
+
+ /**
+ * Get first row of given table from given db.
+ *
+ * @param dbName db name
+ * @param tblName table name
+ * @return firstRow, first row of the given table from given db if present
+ */
+ @Override
+ public Optional<Object> getFirstRow(String dbName, OvsdbTable tblName) {
+
+ DatabaseSchema dbSchema = getDatabaseSchema(dbName);
+ if (Objects.isNull(dbSchema)) {
+ return Optional.empty();
+ }
+
+ OvsdbTableStore tableStore = ovsdbStore.getOvsdbTableStore(dbName);
+ if (tableStore == null) {
+ return Optional.empty();
+ }
+ OvsdbRowStore rowStore = tableStore.getRows(tblName.tableName());
+ if (rowStore == null) {
+ return Optional.empty();
+ }
+
+ ConcurrentMap<String, Row> rows = rowStore.getRowStore();
+ if (rows == null) {
+ log.debug("The {} Table Rows is null", tblName);
+ return Optional.empty();
+ }
+
+ // There should be only 1 row in this table
+ Optional<String> uuid = rows.keySet().stream().findFirst();
+ if (uuid.isPresent() && rows.containsKey(uuid.get())) {
+ return Optional.of(TableGenerator.getTable(dbSchema,
+ rows.get(uuid.get()), tblName));
+ } else {
+ return Optional.empty();
+ }
+ }
+
+
+ /**
+ * Get memory usage of device.
+ *
+ * @return memoryStats, empty data as there is no generic way to fetch such stats
+ */
+ @Override
+ public Optional<DeviceMemoryStats> getDeviceMemoryUsage() {
+ return Optional.empty();
+ }
+
+
+ /**
+ * Get cpu usage of device.
+ *
+ * @return cpuStats, empty data as there is no generic way to fetch such stats
+ */
+ @Override
+ public Optional<DeviceCpuStats> getDeviceCpuUsage() {
+ return Optional.empty();
+ }
}
diff --git a/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/driver/PicaOvsdbClient.java b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/driver/PicaOvsdbClient.java
new file mode 100644
index 0000000..a5a33d1
--- /dev/null
+++ b/protocols/ovsdb/api/src/main/java/org/onosproject/ovsdb/controller/driver/PicaOvsdbClient.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.ovsdb.controller.driver;
+
+
+import org.onosproject.net.behaviour.DeviceCpuStats;
+import org.onosproject.net.behaviour.DeviceMemoryStats;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.rfc.table.CpuMemoryData;
+import org.onosproject.ovsdb.rfc.table.OvsdbTable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+
+import static org.onosproject.ovsdb.controller.OvsdbConstant.*;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.SWINVENTORY_DBNAME;
+
+/**
+ * A representation of an ovsdb client for Pica device.
+ */
+public final class PicaOvsdbClient {
+
+ private final Logger log = LoggerFactory.getLogger(DefaultOvsdbClient.class);
+
+ private OvsdbClientService ovsdbClientService;
+
+ /**
+ * Creates an OvsdbClient for pica device.
+ *
+ * @param ovsdbClient default ovsdb client
+ */
+ public PicaOvsdbClient(OvsdbClientService ovsdbClient) {
+ this.ovsdbClientService = ovsdbClient;
+ }
+
+ /**
+ * Get memory usage of pica device.
+ *
+ * @return memoryStats, memory usage statistics if present
+ */
+ public Optional<DeviceMemoryStats> getDeviceMemoryUsage() {
+
+ Optional<Object> deviceMemoryDataObject = ovsdbClientService.getFirstRow(
+ SWINVENTORY_DBNAME, OvsdbTable.CPUMEMORYDATA);
+
+ if (!deviceMemoryDataObject.isPresent()) {
+ log.debug("Could not find {} column in {} table", DEVICE_MEMORY, CPU_MEMORY_DATA);
+ return Optional.empty();
+ }
+ CpuMemoryData deviceMemoryData = (CpuMemoryData) deviceMemoryDataObject.get();
+
+ long totalMem = deviceMemoryData.getTotalMemoryStats();
+ long usedMem = deviceMemoryData.getUsedMemoryStats();
+ long freeMem = deviceMemoryData.getFreeMemoryStats();
+
+ DeviceMemoryStats deviceMemoryStats = new DeviceMemoryStats();
+ deviceMemoryStats.setFree(freeMem);
+ deviceMemoryStats.setUsed(usedMem);
+ deviceMemoryStats.setTotal(totalMem);
+
+ return Optional.of(deviceMemoryStats);
+ }
+
+ /**
+ * Get cpu usage of pica device.
+ *
+ * @return cpuStats, cpu usage statistics if present
+ */
+ public Optional<DeviceCpuStats> getDeviceCpuUsage() {
+
+ Optional<Object> deviceCpuDataObject = ovsdbClientService.getFirstRow(
+ SWINVENTORY_DBNAME, OvsdbTable.CPUMEMORYDATA);
+
+ if (!deviceCpuDataObject.isPresent()) {
+ log.debug("Could not find {} column in {} table", DEVICE_CPU, CPU_MEMORY_DATA);
+ return Optional.empty();
+ }
+ CpuMemoryData deviceCpuData = (CpuMemoryData) deviceCpuDataObject.get();
+
+ log.debug("GOT CpuMemoryData as {} ", deviceCpuData);
+
+ float freeCpuStat = deviceCpuData.getFreeCpuStats();
+ DeviceCpuStats deviceCpuStats = new DeviceCpuStats();
+ deviceCpuStats.setUsed(100.0f - freeCpuStat);
+ return Optional.of(deviceCpuStats);
+ }
+}
diff --git a/protocols/ovsdb/api/src/test/java/org/onosproject/ovsdb/controller/driver/OvsdbClientServiceAdapter.java b/protocols/ovsdb/api/src/test/java/org/onosproject/ovsdb/controller/driver/OvsdbClientServiceAdapter.java
index 6282c5b..2d2b674 100644
--- a/protocols/ovsdb/api/src/test/java/org/onosproject/ovsdb/controller/driver/OvsdbClientServiceAdapter.java
+++ b/protocols/ovsdb/api/src/test/java/org/onosproject/ovsdb/controller/driver/OvsdbClientServiceAdapter.java
@@ -19,11 +19,14 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.behaviour.ControllerInfo;
+import org.onosproject.net.behaviour.DeviceCpuStats;
+import org.onosproject.net.behaviour.DeviceMemoryStats;
import org.onosproject.net.behaviour.MirroringName;
import org.onosproject.net.behaviour.MirroringStatistics;
import org.onosproject.net.behaviour.QosId;
@@ -45,6 +48,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.util.concurrent.ListenableFuture;
+import org.onosproject.ovsdb.rfc.table.OvsdbTable;
/**
* Test Adapter for OvsdbClientService.
@@ -302,4 +306,19 @@
public boolean getPortError(List<OvsdbPortName> portNames, DeviceId bridgeId) {
return false;
}
+
+ @Override
+ public Optional<Object> getFirstRow(String dbName, OvsdbTable tblName) {
+ return null;
+ }
+
+ @Override
+ public Optional<DeviceCpuStats> getDeviceCpuUsage() {
+ return null;
+ }
+
+ @Override
+ public Optional<DeviceMemoryStats> getDeviceMemoryUsage() {
+ return null;
+ }
}
diff --git a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/CpuMemoryData.java b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/CpuMemoryData.java
new file mode 100644
index 0000000..a08226b
--- /dev/null
+++ b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/CpuMemoryData.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.ovsdb.rfc.table;
+
+
+import org.onosproject.ovsdb.rfc.notation.Column;
+import org.onosproject.ovsdb.rfc.notation.Row;
+import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
+import org.onosproject.ovsdb.rfc.tableservice.AbstractOvsdbTableService;
+import org.onosproject.ovsdb.rfc.tableservice.ColumnDescription;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * This class provides operations of Cpu Memory Table.
+ */
+public class CpuMemoryData extends AbstractOvsdbTableService {
+
+ private static final Pattern PATTERN_MEMORY = Pattern.compile(
+ "Mem:\\s*(\\d+) total,\\s*(\\d+) used,\\s*(\\d+) free,\\s*(\\d+) buffers");
+ private static final Pattern PATTERN_CPU = Pattern.compile(
+ ":\\s*(\\d*\\.\\d+) us,\\s*(\\d*\\.\\d+) sy,\\s*(\\d*\\.\\d+) ni," +
+ "\\s*(\\d*\\.\\d+) id,\\s*(\\d*\\.\\d+) wa," +
+ "\\s*(\\d*\\.\\d+) hi,\\s*(\\d*\\.\\d+) si,\\s*(\\d*\\.\\d+) st");
+ private static final long BYTES_IN_KILOBYTE = 1024L;
+ /**
+ * Cpu Memory Data table column name.
+ */
+ public enum CpuMemoryDataColumn {
+ CPU("cpu"), UPTIME("uptime"),
+ TASKS("tasks"), MEMORY("memory");
+
+ private final String columnName;
+
+ private CpuMemoryDataColumn(String columnName) {
+ this.columnName = columnName;
+ }
+
+ /**
+ * Returns the table column name for CpuMemoryDataColumn.
+ * @return the table column name
+ */
+ public String columnName() {
+ return columnName;
+ }
+ }
+
+ /**
+ * Constructs a CpuMemoryData object. Generate Cpu Memory Data Table Description.
+ * @param dbSchema DatabaseSchema
+ * @param row Row
+ */
+ public CpuMemoryData(DatabaseSchema dbSchema, Row row) {
+ super(dbSchema, row, OvsdbTable.CPUMEMORYDATA, VersionNum.VERSION010, VersionNum.VERSION010);
+ }
+
+ /**
+ * Get the Column entity with column name "memory" from the Row entity of
+ * attributes.
+ * @return the Column entity with column name "memory"
+ */
+ public Column getMemoryColumn() {
+ ColumnDescription columnDescription = new ColumnDescription(
+ CpuMemoryDataColumn.MEMORY
+ .columnName(),
+ "getMemoryColumn",
+ VersionNum.VERSION010);
+ return super.getColumnHandler(columnDescription);
+ }
+
+ /**
+ * Get the Column entity with column name "cpu" from the Row entity of
+ * attributes.
+ * @return the Column entity with column name "cpu"
+ */
+ public Column getCpuColumn() {
+ ColumnDescription columnDescription = new ColumnDescription(
+ CpuMemoryDataColumn.CPU
+ .columnName(),
+ "getCpuColumn",
+ VersionNum.VERSION010);
+ return super.getColumnHandler(columnDescription);
+ }
+
+ /**
+ * Get the total memory avaliable in KB.
+ * @return the total memory (in KB)
+ */
+ public long getTotalMemoryStats() {
+
+ String memoryStats = getMemoryColumn().data().toString();
+ Matcher matcher = PATTERN_MEMORY.matcher(memoryStats);
+ if (!matcher.find()) {
+ return 0;
+ }
+ return Long.parseLong(matcher.group(1)) * BYTES_IN_KILOBYTE;
+ }
+ /**
+ * Get used memory stats in KB.
+ * @return used memory in device (in KB)
+ */
+ public long getUsedMemoryStats() {
+
+ String memoryStats = getMemoryColumn().data().toString();
+ Matcher matcher = PATTERN_MEMORY.matcher(memoryStats);
+ if (!matcher.find()) {
+ return 0;
+ }
+ return Long.parseLong(matcher.group(2)) * BYTES_IN_KILOBYTE;
+ }
+
+ /**
+ * Get free memory stats in KB.
+ * @return free memory in device (in KB)
+ */
+ public long getFreeMemoryStats() {
+
+ String memoryStats = getMemoryColumn().data().toString();
+ Matcher matcher = PATTERN_MEMORY.matcher(memoryStats);
+ if (!matcher.find()) {
+ return 0;
+ }
+ return Long.parseLong(matcher.group(3)) * BYTES_IN_KILOBYTE;
+ }
+
+ /**
+ * Get free cpu usage stats.
+ * @return free cpu stats
+ */
+ public float getFreeCpuStats() {
+
+ System.out.println("COLUMN RECIVED IS {}" + getCpuColumn());
+ String cpuStatsStr = getCpuColumn().data().toString();
+ Matcher matcher = PATTERN_CPU.matcher(cpuStatsStr);
+ if (!matcher.find()) {
+ return 0;
+ }
+ return Float.parseFloat(matcher.group(4));
+ }
+}
diff --git a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/OvsdbTable.java b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/OvsdbTable.java
index 0594dd2..85b7049 100644
--- a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/OvsdbTable.java
+++ b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/OvsdbTable.java
@@ -23,7 +23,8 @@
PORT("Port"), OPENVSWITCH("Open_vSwitch"), FLWTABLE("Flow_Table"),
QOS("QoS"), QUEUE("Queue"), MIRROR("Mirror"), MANAGER("Manager"),
NETFLOW("NetFlow"), SSL("SSL"), SFLOW("sFlow"), IPFIX("IPFIX"),
- FLOWSAMPLECOLLECTORSET("Flow_Sample_Collector_Set");
+ FLOWSAMPLECOLLECTORSET("Flow_Sample_Collector_Set"),
+ CPUMEMORYDATA("Cpu_Memory_Data");
private final String tableName;
diff --git a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/TableGenerator.java b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/TableGenerator.java
index 6b5d2d7..f813bb2 100644
--- a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/TableGenerator.java
+++ b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/TableGenerator.java
@@ -96,6 +96,8 @@
return new Ipfix(dbSchema, row);
case FLOWSAMPLECOLLECTORSET:
return new FlowSampleCollectorSet(dbSchema, row);
+ case CPUMEMORYDATA:
+ return new CpuMemoryData(dbSchema, row);
default:
return null;
}
diff --git a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/VersionNum.java b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/VersionNum.java
index 7e7825e..5e5e34c 100644
--- a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/VersionNum.java
+++ b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/table/VersionNum.java
@@ -19,6 +19,7 @@
* The version number of tables and columns.
*/
public enum VersionNum {
+ VERSION010("0.1.0"),
VERSION100("1.0.0"), VERSION102("1.0.2"), VERSION103("1.0.3"),
VERSION104("1.0.4"), VERSION106("1.0.6"), VERSION110("1.1.0"),
VERSION130("1.3.0"), VERSION200("2.0.0"), VERSION300("3.0.0"),
diff --git a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java
index cbcd41c..107abac 100644
--- a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java
+++ b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java
@@ -63,6 +63,28 @@
}
/**
+ * Constructs a AbstractOvsdbTableService object.
+ * @param dbSchema DatabaseSchema entity
+ * @param row Row entity
+ * @param table table name
+ * @param formVersion the initial version
+ * @param untilVersion the until version
+ */
+ public AbstractOvsdbTableService(DatabaseSchema dbSchema, Row row, OvsdbTable table,
+ VersionNum formVersion, VersionNum untilVersion) {
+ checkNotNull(dbSchema, "database schema cannot be null");
+ checkNotNull(row, "row cannot be null");
+ checkNotNull(table, "table cannot be null");
+ checkNotNull(formVersion, "the initial version cannot be null");
+ checkNotNull(untilVersion, "the end of the version cannot be null");
+ this.dbSchema = dbSchema;
+ row.setTableName(table.tableName());
+ this.row = row;
+ TableDescription tableDesc = new TableDescription(table, dbSchema.name(), formVersion, untilVersion);
+ this.tableDesc = tableDesc;
+ }
+
+ /**
* Check whether the parameter of dbSchema is valid and check whether the
* table is existent in Database Schema.
*/
diff --git a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/TableDescription.java b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/TableDescription.java
index de0e849..c835aeb 100644
--- a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/TableDescription.java
+++ b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/TableDescription.java
@@ -29,7 +29,7 @@
// The table name
private final String name;
// The database name
- private final String database = "Open_vSwitch";
+ private final String database;
// The initial version
private final String fromVersion;
// The end of the version
@@ -44,6 +44,7 @@
this.name = table.tableName();
this.fromVersion = VersionUtil.DEFAULT_VERSION_STRING;
this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING;
+ this.database = "Open_vSwitch";
}
/**
@@ -57,6 +58,25 @@
this.name = table.tableName();
this.fromVersion = fromVersion.versionNum();
this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING;
+ this.database = "Open_vSwitch";
+ }
+
+ /**
+ * Constructs a MonitorRequest object.
+ * @param table OvsdbTable entity
+ * @param database database name
+ * @param fromVersion the initial version
+ * @param untilVersion the untill version
+ */
+ public TableDescription(OvsdbTable table, String database, VersionNum fromVersion, VersionNum untilVersion) {
+ checkNotNull(table, "table cannot be null");
+ checkNotNull(database, "database cannot be null");
+ checkNotNull(fromVersion, "the initial version cannot be null");
+ checkNotNull(untilVersion, "the end of the version cannot be null");
+ this.name = table.tableName();
+ this.fromVersion = fromVersion.versionNum();
+ this.untilVersion = untilVersion.versionNum();
+ this.database = database;
}
/**
@@ -72,6 +92,7 @@
this.name = table.tableName();
this.fromVersion = fromVersion.versionNum();
this.untilVersion = untilVersion.versionNum();
+ this.database = "Open_vSwitch";
}
/**