Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 23 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 24 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 25 | import java.util.Optional; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 26 | import java.util.OptionalInt; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 32 | * Instance of a table entry in a protocol-independent pipeline. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 33 | */ |
| 34 | @Beta |
Carmelo Cascone | 1a7e4f9 | 2017-11-20 23:04:02 -0800 | [diff] [blame] | 35 | public final class PiTableEntry implements PiEntity { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 36 | |
| 37 | private static final int NO_PRIORITY = -1; |
| 38 | private static final double NO_TIMEOUT = -1; |
| 39 | |
| 40 | private final PiTableId tableId; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 41 | private final PiMatchKey matchKey; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 42 | private final PiTableAction tableAction; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 43 | private final boolean isDefaultAction; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 44 | private final long cookie; |
| 45 | private final int priority; |
| 46 | private final double timeout; |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 47 | private final PiCounterCellData counterData; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 48 | |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 49 | private PiTableEntry(PiTableId tableId, PiMatchKey matchKey, |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 50 | PiTableAction tableAction, boolean isDefaultAction, |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 51 | long cookie, int priority, double timeout, PiCounterCellData data) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 52 | this.tableId = tableId; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 53 | this.matchKey = matchKey; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 54 | this.tableAction = tableAction; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 55 | this.isDefaultAction = isDefaultAction; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 56 | this.cookie = cookie; |
| 57 | this.priority = priority; |
| 58 | this.timeout = timeout; |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 59 | this.counterData = data; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the table where this entry is installed. |
| 64 | * |
| 65 | * @return table identifier |
| 66 | */ |
| 67 | public PiTableId table() { |
| 68 | return tableId; |
| 69 | } |
| 70 | |
| 71 | /** |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 72 | * Returns the match key of this table entry. |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 73 | * <p> |
| 74 | * If {@link #isDefaultAction()} is {@code true} this method returns the |
| 75 | * empty match key ({@link PiMatchKey#EMPTY}). |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 76 | * |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 77 | * @return match key |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 78 | */ |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 79 | public PiMatchKey matchKey() { |
| 80 | return matchKey; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the action of this table entry. |
| 85 | * |
| 86 | * @return action |
| 87 | */ |
| 88 | public PiTableAction action() { |
| 89 | return tableAction; |
| 90 | } |
| 91 | |
| 92 | /** |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 93 | * Returns true if this table entry contains the default action for this |
| 94 | * table, a.k.a. table-miss entry, false otherwise. |
| 95 | * |
| 96 | * @return boolean |
| 97 | */ |
| 98 | public boolean isDefaultAction() { |
| 99 | return isDefaultAction; |
| 100 | } |
| 101 | |
| 102 | /** |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 103 | * Returns the cookie of this table entry. |
| 104 | * |
| 105 | * @return cookie |
| 106 | */ |
| 107 | public long cookie() { |
| 108 | return cookie; |
| 109 | } |
| 110 | |
| 111 | /** |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 112 | * Returns the priority of this table entry, if present. If the priority |
| 113 | * value is not present, then this table entry has no explicit priority. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 114 | * |
| 115 | * @return optional priority |
| 116 | */ |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 117 | public OptionalInt priority() { |
| 118 | return priority == NO_PRIORITY ? OptionalInt.empty() : OptionalInt.of(priority); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 122 | * Returns the timeout in seconds of this table entry, if present. If the |
| 123 | * timeout value is not present, then this table entry is meant to be |
| 124 | * permanent. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 125 | * |
| 126 | * @return optional timeout value in seconds |
| 127 | */ |
| 128 | public Optional<Double> timeout() { |
| 129 | return timeout == NO_TIMEOUT ? Optional.empty() : Optional.of(timeout); |
| 130 | } |
| 131 | |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 132 | /** |
| 133 | * Returns the data of the counter cell associated with this table entry. |
| 134 | * This method is meaningful only if the table entry was read from the |
| 135 | * infrastructure device and the table has direct counters, otherwise |
| 136 | * returns null. |
| 137 | * |
| 138 | * @return counter cell data |
| 139 | */ |
| 140 | public PiCounterCellData counter() { |
| 141 | return counterData; |
| 142 | } |
| 143 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 144 | @Override |
| 145 | public boolean equals(Object o) { |
| 146 | if (this == o) { |
| 147 | return true; |
| 148 | } |
| 149 | if (o == null || getClass() != o.getClass()) { |
| 150 | return false; |
| 151 | } |
| 152 | PiTableEntry that = (PiTableEntry) o; |
| 153 | return priority == that.priority && |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 154 | cookie == that.cookie && |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 155 | Double.compare(that.timeout, timeout) == 0 && |
| 156 | Objects.equal(tableId, that.tableId) && |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 157 | Objects.equal(matchKey, that.matchKey) && |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 158 | Objects.equal(isDefaultAction, that.isDefaultAction) && |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 159 | Objects.equal(tableAction, that.tableAction); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public int hashCode() { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 164 | return Objects.hashCode(tableId, matchKey, isDefaultAction, tableAction, |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 165 | priority, cookie, timeout); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public String toString() { |
| 170 | return MoreObjects.toStringHelper(this) |
| 171 | .add("tableId", tableId) |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 172 | .add("matchKey", isDefaultAction ? "DEFAULT-ACTION" : matchKey) |
| 173 | .add("tableAction", tableActionToString(tableAction)) |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 174 | .add("priority", priority == NO_PRIORITY ? "N/A" : String.valueOf(priority)) |
| 175 | .add("timeout", timeout == NO_TIMEOUT ? "PERMANENT" : String.valueOf(timeout)) |
| 176 | .toString(); |
| 177 | } |
| 178 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 179 | private String tableActionToString(PiTableAction tableAction) { |
| 180 | if (tableAction == null) { |
| 181 | return "null"; |
| 182 | } |
| 183 | switch (tableAction.type()) { |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 184 | case ACTION_PROFILE_GROUP_ID: |
| 185 | return "ACT_PROF_GROUP:" + ((PiActionProfileGroupId) tableAction).id(); |
| 186 | case ACTION_PROFILE_MEMBER_ID: |
| 187 | return "ACT_PROF_MEMBER:" + ((PiActionProfileMemberId) tableAction).id(); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 188 | case ACTION: |
| 189 | default: |
| 190 | return tableAction.toString(); |
| 191 | } |
| 192 | } |
| 193 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 194 | /** |
| 195 | * Returns a table entry builder. |
| 196 | * |
| 197 | * @return a new builder |
| 198 | */ |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 199 | public static Builder builder() { |
| 200 | return new Builder(); |
| 201 | } |
| 202 | |
Carmelo Cascone | 1a7e4f9 | 2017-11-20 23:04:02 -0800 | [diff] [blame] | 203 | @Override |
| 204 | public PiEntityType piEntityType() { |
| 205 | return PiEntityType.TABLE_ENTRY; |
| 206 | } |
| 207 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 208 | @Override |
| 209 | public PiTableEntryHandle handle(DeviceId deviceId) { |
| 210 | return PiTableEntryHandle.of(deviceId, this); |
| 211 | } |
| 212 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 213 | public static final class Builder { |
| 214 | |
| 215 | private PiTableId tableId; |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 216 | private PiMatchKey matchKey = PiMatchKey.EMPTY; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 217 | private PiTableAction tableAction; |
| 218 | private long cookie = 0; |
| 219 | private int priority = NO_PRIORITY; |
| 220 | private double timeout = NO_TIMEOUT; |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 221 | private PiCounterCellData counterData; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 222 | |
| 223 | private Builder() { |
| 224 | // Hides constructor. |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Sets the table identifier for this entry. |
| 229 | * |
| 230 | * @param tableId table identifier |
| 231 | * @return this |
| 232 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 233 | public Builder forTable(PiTableId tableId) { |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 234 | this.tableId = checkNotNull(tableId, "Table ID cannot be null"); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 235 | return this; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Sets the action of this table entry. |
| 240 | * |
| 241 | * @param tableAction table action |
| 242 | * @return this |
| 243 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 244 | public Builder withAction(PiTableAction tableAction) { |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 245 | this.tableAction = checkNotNull(tableAction, "Action cannot be null"); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 246 | return this; |
| 247 | } |
| 248 | |
| 249 | /** |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 250 | * Sets the match key of this table entry. By default, the match key is |
| 251 | * {@link PiMatchKey#EMPTY}, i.e. any match. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 252 | * |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 253 | * @param matchKey match key |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 254 | * @return this |
| 255 | */ |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 256 | public Builder withMatchKey(PiMatchKey matchKey) { |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 257 | this.matchKey = checkNotNull(matchKey, "Match key cannot be null"); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 258 | return this; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Sets the cookie, i.e. a controller-specific metadata. |
| 263 | * |
| 264 | * @param cookie cookie |
| 265 | * @return this |
| 266 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 267 | public Builder withCookie(long cookie) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 268 | this.cookie = cookie; |
| 269 | return this; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Sets the priority of this table entry. |
| 274 | * |
| 275 | * @param priority priority |
| 276 | * @return this |
| 277 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 278 | public Builder withPriority(int priority) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 279 | checkArgument(priority >= 0, "Priority must be a positive integer."); |
| 280 | this.priority = priority; |
| 281 | return this; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Sets the timeout of this table entry. |
| 286 | * |
| 287 | * @param seconds timeout in seconds |
| 288 | * @return this |
| 289 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 290 | public Builder withTimeout(double seconds) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 291 | checkArgument(seconds > 0, "Timeout must be greater than zero."); |
| 292 | this.timeout = seconds; |
| 293 | return this; |
| 294 | } |
| 295 | |
| 296 | /** |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 297 | * Sets the counter cell data of this table entry. |
| 298 | * |
| 299 | * @param data counter cell data |
| 300 | * @return this |
| 301 | */ |
| 302 | public Builder withCounterCellData(PiCounterCellData data) { |
| 303 | this.counterData = checkNotNull(data, "Counter cell data cannot be null"); |
| 304 | return this; |
| 305 | } |
| 306 | |
| 307 | /** |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 308 | * Builds the table entry. |
| 309 | * |
| 310 | * @return a new table entry |
| 311 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 312 | public PiTableEntry build() { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 313 | checkNotNull(tableId); |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 314 | checkNotNull(matchKey); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 315 | final boolean isDefaultAction = matchKey.equals(PiMatchKey.EMPTY); |
| 316 | return new PiTableEntry(tableId, matchKey, tableAction, |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 317 | isDefaultAction, cookie, priority, timeout, counterData); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |