nitinanand | 9e8f836 | 2018-05-31 15:11:04 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-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.rfc.table; |
| 17 | |
| 18 | |
| 19 | import org.onosproject.ovsdb.rfc.notation.Column; |
| 20 | import org.onosproject.ovsdb.rfc.notation.Row; |
| 21 | import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; |
| 22 | import org.onosproject.ovsdb.rfc.tableservice.AbstractOvsdbTableService; |
| 23 | import org.onosproject.ovsdb.rfc.tableservice.ColumnDescription; |
| 24 | |
| 25 | import java.util.regex.Matcher; |
| 26 | import java.util.regex.Pattern; |
| 27 | |
| 28 | /** |
| 29 | * This class provides operations of Cpu Memory Table. |
| 30 | */ |
| 31 | public class CpuMemoryData extends AbstractOvsdbTableService { |
| 32 | |
| 33 | private static final Pattern PATTERN_MEMORY = Pattern.compile( |
| 34 | "Mem:\\s*(\\d+) total,\\s*(\\d+) used,\\s*(\\d+) free,\\s*(\\d+) buffers"); |
| 35 | private static final Pattern PATTERN_CPU = Pattern.compile( |
| 36 | ":\\s*(\\d*\\.\\d+) us,\\s*(\\d*\\.\\d+) sy,\\s*(\\d*\\.\\d+) ni," + |
| 37 | "\\s*(\\d*\\.\\d+) id,\\s*(\\d*\\.\\d+) wa," + |
| 38 | "\\s*(\\d*\\.\\d+) hi,\\s*(\\d*\\.\\d+) si,\\s*(\\d*\\.\\d+) st"); |
| 39 | private static final long BYTES_IN_KILOBYTE = 1024L; |
| 40 | /** |
| 41 | * Cpu Memory Data table column name. |
| 42 | */ |
| 43 | public enum CpuMemoryDataColumn { |
| 44 | CPU("cpu"), UPTIME("uptime"), |
| 45 | TASKS("tasks"), MEMORY("memory"); |
| 46 | |
| 47 | private final String columnName; |
| 48 | |
| 49 | private CpuMemoryDataColumn(String columnName) { |
| 50 | this.columnName = columnName; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the table column name for CpuMemoryDataColumn. |
| 55 | * @return the table column name |
| 56 | */ |
| 57 | public String columnName() { |
| 58 | return columnName; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructs a CpuMemoryData object. Generate Cpu Memory Data Table Description. |
| 64 | * @param dbSchema DatabaseSchema |
| 65 | * @param row Row |
| 66 | */ |
| 67 | public CpuMemoryData(DatabaseSchema dbSchema, Row row) { |
| 68 | super(dbSchema, row, OvsdbTable.CPUMEMORYDATA, VersionNum.VERSION010, VersionNum.VERSION010); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the Column entity with column name "memory" from the Row entity of |
| 73 | * attributes. |
| 74 | * @return the Column entity with column name "memory" |
| 75 | */ |
| 76 | public Column getMemoryColumn() { |
| 77 | ColumnDescription columnDescription = new ColumnDescription( |
| 78 | CpuMemoryDataColumn.MEMORY |
| 79 | .columnName(), |
| 80 | "getMemoryColumn", |
| 81 | VersionNum.VERSION010); |
| 82 | return super.getColumnHandler(columnDescription); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the Column entity with column name "cpu" from the Row entity of |
| 87 | * attributes. |
| 88 | * @return the Column entity with column name "cpu" |
| 89 | */ |
| 90 | public Column getCpuColumn() { |
| 91 | ColumnDescription columnDescription = new ColumnDescription( |
| 92 | CpuMemoryDataColumn.CPU |
| 93 | .columnName(), |
| 94 | "getCpuColumn", |
| 95 | VersionNum.VERSION010); |
| 96 | return super.getColumnHandler(columnDescription); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the total memory avaliable in KB. |
| 101 | * @return the total memory (in KB) |
| 102 | */ |
| 103 | public long getTotalMemoryStats() { |
| 104 | |
| 105 | String memoryStats = getMemoryColumn().data().toString(); |
| 106 | Matcher matcher = PATTERN_MEMORY.matcher(memoryStats); |
| 107 | if (!matcher.find()) { |
| 108 | return 0; |
| 109 | } |
| 110 | return Long.parseLong(matcher.group(1)) * BYTES_IN_KILOBYTE; |
| 111 | } |
| 112 | /** |
| 113 | * Get used memory stats in KB. |
| 114 | * @return used memory in device (in KB) |
| 115 | */ |
| 116 | public long getUsedMemoryStats() { |
| 117 | |
| 118 | String memoryStats = getMemoryColumn().data().toString(); |
| 119 | Matcher matcher = PATTERN_MEMORY.matcher(memoryStats); |
| 120 | if (!matcher.find()) { |
| 121 | return 0; |
| 122 | } |
| 123 | return Long.parseLong(matcher.group(2)) * BYTES_IN_KILOBYTE; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get free memory stats in KB. |
| 128 | * @return free memory in device (in KB) |
| 129 | */ |
| 130 | public long getFreeMemoryStats() { |
| 131 | |
| 132 | String memoryStats = getMemoryColumn().data().toString(); |
| 133 | Matcher matcher = PATTERN_MEMORY.matcher(memoryStats); |
| 134 | if (!matcher.find()) { |
| 135 | return 0; |
| 136 | } |
| 137 | return Long.parseLong(matcher.group(3)) * BYTES_IN_KILOBYTE; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get free cpu usage stats. |
| 142 | * @return free cpu stats |
| 143 | */ |
| 144 | public float getFreeCpuStats() { |
| 145 | |
| 146 | System.out.println("COLUMN RECIVED IS {}" + getCpuColumn()); |
| 147 | String cpuStatsStr = getCpuColumn().data().toString(); |
| 148 | Matcher matcher = PATTERN_CPU.matcher(cpuStatsStr); |
| 149 | if (!matcher.find()) { |
| 150 | return 0; |
| 151 | } |
| 152 | return Float.parseFloat(matcher.group(4)); |
| 153 | } |
| 154 | } |