blob: ccce84e6c3f8b3414d1d6563f6c0db58d7d02998 [file] [log] [blame]
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -07003 *
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 */
16package org.onosproject.net.flow;
17
18import org.onosproject.net.DeviceId;
Georgios Katsikas9e75f8b2020-03-22 11:47:13 +010019import com.google.common.base.MoreObjects.ToStringHelper;
20import static com.google.common.base.MoreObjects.toStringHelper;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070021import static com.google.common.base.Preconditions.checkNotNull;
22
23/**
24 * Default implementation of table statistics entry interface.
25 */
26public final class DefaultTableStatisticsEntry implements TableStatisticsEntry {
27
28 private final DeviceId deviceId;
hjtsao1a4333c2018-10-22 11:02:00 -070029 private final TableId tableId;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070030 private final long activeFlowEntries;
31 private final long packetsLookedupCount;
32 private final long packetsMatchedCount;
hjtsao1a4333c2018-10-22 11:02:00 -070033 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 Vavilapalli95810f52015-09-14 15:49:56 -070040 * @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
hjtsao1a4333c2018-10-22 11:02:00 -070044 * @param maxSize maximum size of this table
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070045 */
hjtsao1a4333c2018-10-22 11:02:00 -070046 private DefaultTableStatisticsEntry(DeviceId deviceId,
47 TableId tableId,
48 long activeFlowEntries,
49 long packetsLookedupCount,
50 long packetsMatchedCount,
51 long maxSize) {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070052 this.deviceId = checkNotNull(deviceId);
53 this.tableId = tableId;
54 this.activeFlowEntries = activeFlowEntries;
55 this.packetsLookedupCount = packetsLookedupCount;
56 this.packetsMatchedCount = packetsMatchedCount;
hjtsao1a4333c2018-10-22 11:02:00 -070057 this.maxSize = maxSize;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070058 }
59
60 @Override
61 public String toString() {
Georgios Katsikas9e75f8b2020-03-22 11:47:13 +010062 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 Vavilapalli95810f52015-09-14 15:49:56 -070077 }
78
79 @Override
80 public int tableId() {
hjtsao1a4333c2018-10-22 11:02:00 -070081 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 Vavilapalli95810f52015-09-14 15:49:56 -070087 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 }
hjtsao1a4333c2018-10-22 11:02:00 -0700109
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 Katsikas9e75f8b2020-03-22 11:47:13 +0100178}