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