Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 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.net.flow; |
| 17 | |
| 18 | import org.onosproject.net.DeviceId; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | /** |
| 22 | * Default implementation of table statistics entry interface. |
| 23 | */ |
| 24 | public final class DefaultTableStatisticsEntry implements TableStatisticsEntry { |
| 25 | |
| 26 | private final DeviceId deviceId; |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 27 | private final TableId tableId; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 28 | private final long activeFlowEntries; |
| 29 | private final long packetsLookedupCount; |
| 30 | private final long packetsMatchedCount; |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 31 | private final long maxSize; |
| 32 | private static final Long NOT_PRESENT = (long) -1; |
| 33 | |
| 34 | /** |
| 35 | * Default table statistics constructor. |
| 36 | * |
| 37 | * @param deviceId device identifier |
| 38 | * @param tableId index table identifier |
| 39 | * @param activeFlowEntries number of active flow entries in the table |
| 40 | * @param packetsLookedupCount number of packets looked up in table |
| 41 | * @param packetsMatchedCount number of packets that hit table |
| 42 | * @deprecated since 1.15, suggest using the Builder class. |
| 43 | */ |
| 44 | @Deprecated |
| 45 | public DefaultTableStatisticsEntry(DeviceId deviceId, |
| 46 | int tableId, |
| 47 | long activeFlowEntries, |
| 48 | long packetsLookedupCount, |
| 49 | long packetsMatchedCount) { |
| 50 | this.deviceId = checkNotNull(deviceId); |
| 51 | this.tableId = IndexTableId.of(tableId); |
| 52 | this.activeFlowEntries = activeFlowEntries; |
| 53 | this.packetsLookedupCount = packetsLookedupCount; |
| 54 | this.packetsMatchedCount = packetsMatchedCount; |
| 55 | this.maxSize = NOT_PRESENT; |
| 56 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Default table statistics constructor. |
| 60 | * |
| 61 | * @param deviceId device identifier |
| 62 | * @param tableId table identifier |
| 63 | * @param activeFlowEntries number of active flow entries in the table |
| 64 | * @param packetsLookedupCount number of packets looked up in table |
| 65 | * @param packetsMatchedCount number of packets that hit table |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 66 | * @param maxSize maximum size of this table |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 67 | */ |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 68 | private DefaultTableStatisticsEntry(DeviceId deviceId, |
| 69 | TableId tableId, |
| 70 | long activeFlowEntries, |
| 71 | long packetsLookedupCount, |
| 72 | long packetsMatchedCount, |
| 73 | long maxSize) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 74 | this.deviceId = checkNotNull(deviceId); |
| 75 | this.tableId = tableId; |
| 76 | this.activeFlowEntries = activeFlowEntries; |
| 77 | this.packetsLookedupCount = packetsLookedupCount; |
| 78 | this.packetsMatchedCount = packetsMatchedCount; |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 79 | this.maxSize = maxSize; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String toString() { |
Sho SHIMIZU | 8d50c8d | 2016-08-12 17:29:02 -0700 | [diff] [blame] | 84 | return "device: " + deviceId + ", " + |
| 85 | "tableId: " + this.tableId + ", " + |
| 86 | "activeEntries: " + this.activeFlowEntries + ", " + |
| 87 | "packetsLookedUp: " + this.packetsLookedupCount + ", " + |
| 88 | "packetsMatched: " + this.packetsMatchedCount; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public int tableId() { |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 93 | return tableId.type() == TableId.Type.INDEX ? ((IndexTableId) tableId).id() : tableId.hashCode(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public TableId table() { |
| 98 | //TODO: this is a temporary method, should implement tableId() like this method. |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 99 | return tableId; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public long activeFlowEntries() { |
| 104 | return activeFlowEntries; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public long packetsLookedup() { |
| 109 | return packetsLookedupCount; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public long packetsMatched() { |
| 114 | return packetsMatchedCount; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public DeviceId deviceId() { |
| 119 | return deviceId; |
| 120 | } |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 121 | |
| 122 | @Override |
| 123 | public long maxSize() { |
| 124 | return maxSize; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public boolean hasPacketsLookedup() { |
| 129 | return packetsLookedupCount == NOT_PRESENT ? false : true; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean hasMaxSize() { |
| 134 | return maxSize == NOT_PRESENT ? false : true; |
| 135 | } |
| 136 | |
| 137 | public static Builder builder() { |
| 138 | return new Builder(); |
| 139 | } |
| 140 | |
| 141 | public static final class Builder { |
| 142 | private DeviceId deviceId; |
| 143 | private TableId tableId; |
| 144 | private Long activeFlowEntries; |
| 145 | private Long packetsMatchedCount; |
| 146 | private Long packetsLookedUpCount = NOT_PRESENT; |
| 147 | private Long maxSize = NOT_PRESENT; |
| 148 | |
| 149 | public Builder withDeviceId(DeviceId deviceId) { |
| 150 | this.deviceId = deviceId; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | public Builder withTableId(TableId tableId) { |
| 155 | this.tableId = tableId; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | public Builder withActiveFlowEntries(long activeFlowEntries) { |
| 160 | this.activeFlowEntries = activeFlowEntries; |
| 161 | return this; |
| 162 | } |
| 163 | |
| 164 | public Builder withPacketsLookedUpCount(long packetsLookedUpCount) { |
| 165 | this.packetsLookedUpCount = packetsLookedUpCount; |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | public Builder withPacketsMatchedCount(long packetsMatchedCount) { |
| 170 | this.packetsMatchedCount = packetsMatchedCount; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | public Builder withMaxSize(long maxSize) { |
| 175 | this.maxSize = maxSize; |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | public TableStatisticsEntry build() { |
| 180 | checkNotNull(deviceId, "DeviceId cannot be null"); |
| 181 | checkNotNull(tableId, "TableId cannot be null"); |
| 182 | checkNotNull(activeFlowEntries, "ActiveFlowEntries cannot be null"); |
| 183 | checkNotNull(packetsMatchedCount, "PacketsMatchedCount cannot be null"); |
| 184 | |
| 185 | return new DefaultTableStatisticsEntry(deviceId, tableId, activeFlowEntries, packetsLookedUpCount, |
| 186 | packetsMatchedCount, maxSize); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } |