nitinanand | 9e8f836 | 2018-05-31 15:11:04 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015-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 | package org.onosproject.ovsdb.controller.driver; |
| 17 | |
| 18 | |
| 19 | import org.onosproject.net.behaviour.DeviceCpuStats; |
| 20 | import org.onosproject.net.behaviour.DeviceMemoryStats; |
| 21 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 22 | import org.onosproject.ovsdb.rfc.table.CpuMemoryData; |
| 23 | import org.onosproject.ovsdb.rfc.table.OvsdbTable; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | import static org.onosproject.ovsdb.controller.OvsdbConstant.*; |
| 30 | import static org.onosproject.ovsdb.controller.OvsdbConstant.SWINVENTORY_DBNAME; |
| 31 | |
| 32 | /** |
| 33 | * A representation of an ovsdb client for Pica device. |
| 34 | */ |
| 35 | public final class PicaOvsdbClient { |
| 36 | |
| 37 | private final Logger log = LoggerFactory.getLogger(DefaultOvsdbClient.class); |
| 38 | |
| 39 | private OvsdbClientService ovsdbClientService; |
| 40 | |
| 41 | /** |
| 42 | * Creates an OvsdbClient for pica device. |
| 43 | * |
| 44 | * @param ovsdbClient default ovsdb client |
| 45 | */ |
| 46 | public PicaOvsdbClient(OvsdbClientService ovsdbClient) { |
| 47 | this.ovsdbClientService = ovsdbClient; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get memory usage of pica device. |
| 52 | * |
| 53 | * @return memoryStats, memory usage statistics if present |
| 54 | */ |
| 55 | public Optional<DeviceMemoryStats> getDeviceMemoryUsage() { |
| 56 | |
| 57 | Optional<Object> deviceMemoryDataObject = ovsdbClientService.getFirstRow( |
| 58 | SWINVENTORY_DBNAME, OvsdbTable.CPUMEMORYDATA); |
| 59 | |
| 60 | if (!deviceMemoryDataObject.isPresent()) { |
| 61 | log.debug("Could not find {} column in {} table", DEVICE_MEMORY, CPU_MEMORY_DATA); |
| 62 | return Optional.empty(); |
| 63 | } |
| 64 | CpuMemoryData deviceMemoryData = (CpuMemoryData) deviceMemoryDataObject.get(); |
| 65 | |
| 66 | long totalMem = deviceMemoryData.getTotalMemoryStats(); |
| 67 | long usedMem = deviceMemoryData.getUsedMemoryStats(); |
| 68 | long freeMem = deviceMemoryData.getFreeMemoryStats(); |
| 69 | |
| 70 | DeviceMemoryStats deviceMemoryStats = new DeviceMemoryStats(); |
| 71 | deviceMemoryStats.setFree(freeMem); |
| 72 | deviceMemoryStats.setUsed(usedMem); |
| 73 | deviceMemoryStats.setTotal(totalMem); |
| 74 | |
| 75 | return Optional.of(deviceMemoryStats); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get cpu usage of pica device. |
| 80 | * |
| 81 | * @return cpuStats, cpu usage statistics if present |
| 82 | */ |
| 83 | public Optional<DeviceCpuStats> getDeviceCpuUsage() { |
| 84 | |
| 85 | Optional<Object> deviceCpuDataObject = ovsdbClientService.getFirstRow( |
| 86 | SWINVENTORY_DBNAME, OvsdbTable.CPUMEMORYDATA); |
| 87 | |
| 88 | if (!deviceCpuDataObject.isPresent()) { |
| 89 | log.debug("Could not find {} column in {} table", DEVICE_CPU, CPU_MEMORY_DATA); |
| 90 | return Optional.empty(); |
| 91 | } |
| 92 | CpuMemoryData deviceCpuData = (CpuMemoryData) deviceCpuDataObject.get(); |
| 93 | |
| 94 | log.debug("GOT CpuMemoryData as {} ", deviceCpuData); |
| 95 | |
| 96 | float freeCpuStat = deviceCpuData.getFreeCpuStats(); |
| 97 | DeviceCpuStats deviceCpuStats = new DeviceCpuStats(); |
| 98 | deviceCpuStats.setUsed(100.0f - freeCpuStat); |
| 99 | return Optional.of(deviceCpuStats); |
| 100 | } |
| 101 | } |