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