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