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