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 |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 38 | * @param tableId 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 |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 42 | * @param maxSize maximum size of this table |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 43 | */ |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 44 | private DefaultTableStatisticsEntry(DeviceId deviceId, |
| 45 | TableId tableId, |
| 46 | long activeFlowEntries, |
| 47 | long packetsLookedupCount, |
| 48 | long packetsMatchedCount, |
| 49 | long maxSize) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 50 | this.deviceId = checkNotNull(deviceId); |
| 51 | this.tableId = tableId; |
| 52 | this.activeFlowEntries = activeFlowEntries; |
| 53 | this.packetsLookedupCount = packetsLookedupCount; |
| 54 | this.packetsMatchedCount = packetsMatchedCount; |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 55 | this.maxSize = maxSize; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String toString() { |
Sho SHIMIZU | 8d50c8d | 2016-08-12 17:29:02 -0700 | [diff] [blame] | 60 | return "device: " + deviceId + ", " + |
| 61 | "tableId: " + this.tableId + ", " + |
| 62 | "activeEntries: " + this.activeFlowEntries + ", " + |
| 63 | "packetsLookedUp: " + this.packetsLookedupCount + ", " + |
| 64 | "packetsMatched: " + this.packetsMatchedCount; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int tableId() { |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 69 | return tableId.type() == TableId.Type.INDEX ? ((IndexTableId) tableId).id() : tableId.hashCode(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public TableId table() { |
| 74 | //TODO: this is a temporary method, should implement tableId() like this method. |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 75 | return tableId; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public long activeFlowEntries() { |
| 80 | return activeFlowEntries; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public long packetsLookedup() { |
| 85 | return packetsLookedupCount; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public long packetsMatched() { |
| 90 | return packetsMatchedCount; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public DeviceId deviceId() { |
| 95 | return deviceId; |
| 96 | } |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 97 | |
| 98 | @Override |
| 99 | public long maxSize() { |
| 100 | return maxSize; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean hasPacketsLookedup() { |
| 105 | return packetsLookedupCount == NOT_PRESENT ? false : true; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean hasMaxSize() { |
| 110 | return maxSize == NOT_PRESENT ? false : true; |
| 111 | } |
| 112 | |
| 113 | public static Builder builder() { |
| 114 | return new Builder(); |
| 115 | } |
| 116 | |
| 117 | public static final class Builder { |
| 118 | private DeviceId deviceId; |
| 119 | private TableId tableId; |
| 120 | private Long activeFlowEntries; |
| 121 | private Long packetsMatchedCount; |
| 122 | private Long packetsLookedUpCount = NOT_PRESENT; |
| 123 | private Long maxSize = NOT_PRESENT; |
| 124 | |
| 125 | public Builder withDeviceId(DeviceId deviceId) { |
| 126 | this.deviceId = deviceId; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | public Builder withTableId(TableId tableId) { |
| 131 | this.tableId = tableId; |
| 132 | return this; |
| 133 | } |
| 134 | |
| 135 | public Builder withActiveFlowEntries(long activeFlowEntries) { |
| 136 | this.activeFlowEntries = activeFlowEntries; |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | public Builder withPacketsLookedUpCount(long packetsLookedUpCount) { |
| 141 | this.packetsLookedUpCount = packetsLookedUpCount; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | public Builder withPacketsMatchedCount(long packetsMatchedCount) { |
| 146 | this.packetsMatchedCount = packetsMatchedCount; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | public Builder withMaxSize(long maxSize) { |
| 151 | this.maxSize = maxSize; |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | public TableStatisticsEntry build() { |
| 156 | checkNotNull(deviceId, "DeviceId cannot be null"); |
| 157 | checkNotNull(tableId, "TableId cannot be null"); |
| 158 | checkNotNull(activeFlowEntries, "ActiveFlowEntries cannot be null"); |
| 159 | checkNotNull(packetsMatchedCount, "PacketsMatchedCount cannot be null"); |
| 160 | |
| 161 | return new DefaultTableStatisticsEntry(deviceId, tableId, activeFlowEntries, packetsLookedUpCount, |
| 162 | packetsMatchedCount, maxSize); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | } |