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 | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 23 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 24 | import java.util.Optional; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 30 | * Instance of a table entry in a protocol-independent pipeline. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 31 | */ |
| 32 | @Beta |
Carmelo Cascone | 1a7e4f9 | 2017-11-20 23:04:02 -0800 | [diff] [blame] | 33 | public final class PiTableEntry implements PiEntity { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 34 | |
| 35 | private static final int NO_PRIORITY = -1; |
| 36 | private static final double NO_TIMEOUT = -1; |
| 37 | |
| 38 | private final PiTableId tableId; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 39 | private final PiMatchKey matchKey; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 40 | private final PiTableAction tableAction; |
| 41 | private final long cookie; |
| 42 | private final int priority; |
| 43 | private final double timeout; |
| 44 | |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 45 | private PiTableEntry(PiTableId tableId, PiMatchKey matchKey, |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 46 | PiTableAction tableAction, long cookie, int priority, double timeout) { |
| 47 | this.tableId = tableId; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 48 | this.matchKey = matchKey; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 49 | this.tableAction = tableAction; |
| 50 | this.cookie = cookie; |
| 51 | this.priority = priority; |
| 52 | this.timeout = timeout; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the table where this entry is installed. |
| 57 | * |
| 58 | * @return table identifier |
| 59 | */ |
| 60 | public PiTableId table() { |
| 61 | return tableId; |
| 62 | } |
| 63 | |
| 64 | /** |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 65 | * Returns the match key of this table entry. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 66 | * |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 67 | * @return match key |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 68 | */ |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 69 | public PiMatchKey matchKey() { |
| 70 | return matchKey; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the action of this table entry. |
| 75 | * |
| 76 | * @return action |
| 77 | */ |
| 78 | public PiTableAction action() { |
| 79 | return tableAction; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the cookie of this table entry. |
| 84 | * |
| 85 | * @return cookie |
| 86 | */ |
| 87 | public long cookie() { |
| 88 | return cookie; |
| 89 | } |
| 90 | |
| 91 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 92 | * Returns the priority of this table entry, if present. If the priority value is not present, then this table entry |
| 93 | * has no explicit priority. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 94 | * |
| 95 | * @return optional priority |
| 96 | */ |
| 97 | public Optional<Integer> priority() { |
| 98 | return priority == NO_PRIORITY ? Optional.empty() : Optional.of(priority); |
| 99 | } |
| 100 | |
| 101 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 102 | * Returns the timeout in seconds of this table entry, if present. If the timeout value is not present, then this |
| 103 | * table entry is meant to be permanent. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 104 | * |
| 105 | * @return optional timeout value in seconds |
| 106 | */ |
| 107 | public Optional<Double> timeout() { |
| 108 | return timeout == NO_TIMEOUT ? Optional.empty() : Optional.of(timeout); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean equals(Object o) { |
| 113 | if (this == o) { |
| 114 | return true; |
| 115 | } |
| 116 | if (o == null || getClass() != o.getClass()) { |
| 117 | return false; |
| 118 | } |
| 119 | PiTableEntry that = (PiTableEntry) o; |
| 120 | return priority == that.priority && |
| 121 | Double.compare(that.timeout, timeout) == 0 && |
| 122 | Objects.equal(tableId, that.tableId) && |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 123 | Objects.equal(matchKey, that.matchKey) && |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 124 | Objects.equal(tableAction, that.tableAction); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int hashCode() { |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 129 | return Objects.hashCode(tableId, matchKey, tableAction, priority, timeout); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return MoreObjects.toStringHelper(this) |
| 135 | .add("tableId", tableId) |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 136 | .add("matchKey", matchKey) |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 137 | .add("tableAction", tableAction) |
| 138 | .add("priority", priority == NO_PRIORITY ? "N/A" : String.valueOf(priority)) |
| 139 | .add("timeout", timeout == NO_TIMEOUT ? "PERMANENT" : String.valueOf(timeout)) |
| 140 | .toString(); |
| 141 | } |
| 142 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 143 | /** |
| 144 | * Returns a table entry builder. |
| 145 | * |
| 146 | * @return a new builder |
| 147 | */ |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 148 | public static Builder builder() { |
| 149 | return new Builder(); |
| 150 | } |
| 151 | |
Carmelo Cascone | 1a7e4f9 | 2017-11-20 23:04:02 -0800 | [diff] [blame] | 152 | @Override |
| 153 | public PiEntityType piEntityType() { |
| 154 | return PiEntityType.TABLE_ENTRY; |
| 155 | } |
| 156 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 157 | public static final class Builder { |
| 158 | |
| 159 | private PiTableId tableId; |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 160 | private PiMatchKey matchKey = PiMatchKey.EMPTY; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 161 | private PiTableAction tableAction; |
| 162 | private long cookie = 0; |
| 163 | private int priority = NO_PRIORITY; |
| 164 | private double timeout = NO_TIMEOUT; |
| 165 | |
| 166 | private Builder() { |
| 167 | // Hides constructor. |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Sets the table identifier for this entry. |
| 172 | * |
| 173 | * @param tableId table identifier |
| 174 | * @return this |
| 175 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 176 | public Builder forTable(PiTableId tableId) { |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 177 | this.tableId = checkNotNull(tableId, "Table ID cannot be null"); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 178 | return this; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Sets the action of this table entry. |
| 183 | * |
| 184 | * @param tableAction table action |
| 185 | * @return this |
| 186 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 187 | public Builder withAction(PiTableAction tableAction) { |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 188 | this.tableAction = checkNotNull(tableAction, "Action cannot be null"); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 189 | return this; |
| 190 | } |
| 191 | |
| 192 | /** |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 193 | * Sets the match key of this table entry. By default, the match key is |
| 194 | * {@link PiMatchKey#EMPTY}, i.e. any match. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 195 | * |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 196 | * @param matchKey match key |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 197 | * @return this |
| 198 | */ |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 199 | public Builder withMatchKey(PiMatchKey matchKey) { |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 200 | this.matchKey = checkNotNull(matchKey, "Match key cannot be null"); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 201 | return this; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets the cookie, i.e. a controller-specific metadata. |
| 206 | * |
| 207 | * @param cookie cookie |
| 208 | * @return this |
| 209 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 210 | public Builder withCookie(long cookie) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 211 | this.cookie = cookie; |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Sets the priority of this table entry. |
| 217 | * |
| 218 | * @param priority priority |
| 219 | * @return this |
| 220 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 221 | public Builder withPriority(int priority) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 222 | checkArgument(priority >= 0, "Priority must be a positive integer."); |
| 223 | this.priority = priority; |
| 224 | return this; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Sets the timeout of this table entry. |
| 229 | * |
| 230 | * @param seconds timeout in seconds |
| 231 | * @return this |
| 232 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 233 | public Builder withTimeout(double seconds) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 234 | checkArgument(seconds > 0, "Timeout must be greater than zero."); |
| 235 | this.timeout = seconds; |
| 236 | return this; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Builds the table entry. |
| 241 | * |
| 242 | * @return a new table entry |
| 243 | */ |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 244 | public PiTableEntry build() { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 245 | checkNotNull(tableId); |
Carmelo Cascone | f03f6e9 | 2018-04-04 11:39:39 -0700 | [diff] [blame] | 246 | checkNotNull(matchKey); |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 247 | return new PiTableEntry(tableId, matchKey, tableAction, cookie, priority, timeout); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |