nitinanand | f14dccd | 2018-05-31 15:11:04 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | |
| 17 | package org.onosproject.drivers.ovsdb; |
| 18 | |
| 19 | |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.behaviour.DeviceCpuStats; |
| 23 | import org.onosproject.net.behaviour.DeviceMemoryStats; |
| 24 | import org.onosproject.net.behaviour.DeviceSystemStatisticsQuery; |
| 25 | import org.onosproject.net.behaviour.DeviceSystemStats; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.net.driver.DriverHandler; |
| 28 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 29 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 30 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 31 | import org.onosproject.ovsdb.controller.driver.PicaOvsdbClient; |
| 32 | |
| 33 | import java.util.Optional; |
| 34 | |
| 35 | /** |
| 36 | * The implementation of PicaOvsdbSystemStatsQuery. |
| 37 | */ |
| 38 | public class PicaOvsdbSystemStatsQuery extends AbstractHandlerBehaviour |
| 39 | implements DeviceSystemStatisticsQuery { |
| 40 | |
| 41 | // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP) |
| 42 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 43 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
| 44 | String[] splits = deviceId.toString().split(":"); |
| 45 | if (splits == null || splits.length < 1) { |
| 46 | return null; |
| 47 | } |
| 48 | IpAddress ipAddress = IpAddress.valueOf(splits[1]); |
| 49 | return new OvsdbNodeId(ipAddress, 0); |
| 50 | } |
| 51 | |
| 52 | // Used for getting OvsdbClientService. |
| 53 | private OvsdbClientService getOvsdbClientService(DriverHandler handler) { |
| 54 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 55 | DeviceId deviceId = handler.data().deviceId(); |
| 56 | OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId); |
| 57 | return ovsController.getOvsdbClient(nodeId); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get cpu usage of device. |
| 62 | * |
| 63 | * @return cpuStats, device cpu usage stats if available |
| 64 | */ |
| 65 | private Optional<DeviceCpuStats> getCpuUsage() { |
| 66 | OvsdbClientService client = getOvsdbClientService(handler()); |
| 67 | if (client == null) { |
| 68 | return Optional.empty(); |
| 69 | } |
| 70 | PicaOvsdbClient picaOvsdbClient = new PicaOvsdbClient(client); |
| 71 | return picaOvsdbClient.getDeviceCpuUsage(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get memory usage of device in KB. |
| 76 | * |
| 77 | * @return memoryStats, device memory usage stats if available |
| 78 | */ |
| 79 | private Optional<DeviceMemoryStats> getMemoryUsage() { |
| 80 | OvsdbClientService client = getOvsdbClientService(handler()); |
| 81 | if (client == null) { |
| 82 | return Optional.empty(); |
| 83 | } |
| 84 | PicaOvsdbClient picaOvsdbClient = new PicaOvsdbClient(client); |
| 85 | return picaOvsdbClient.getDeviceMemoryUsage(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get system stats (cpu/mmeory usage) of device. |
| 90 | * |
| 91 | * @return deviceSystemStats, system stats (cpu/memory usage) of device if available |
| 92 | */ |
| 93 | @Override |
| 94 | public Optional<DeviceSystemStats> getDeviceSystemStats() { |
| 95 | Optional<DeviceCpuStats> cpuStats = getCpuUsage(); |
| 96 | Optional<DeviceMemoryStats> memoryStats = getMemoryUsage(); |
| 97 | |
| 98 | if (cpuStats.isPresent() && memoryStats.isPresent()) { |
| 99 | DeviceSystemStats systemStats = new DeviceSystemStats(memoryStats.get(), cpuStats.get()); |
| 100 | return Optional.of(systemStats); |
| 101 | } else { |
| 102 | return Optional.empty(); |
| 103 | } |
| 104 | } |
| 105 | } |