Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 1 | /* |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [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 | |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 17 | package org.onosproject.bmv2.api.runtime; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 18 | |
| 19 | import java.util.Objects; |
| 20 | |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Bmv2 representation of a table entry. |
| 26 | */ |
| 27 | public final class Bmv2TableEntry { |
| 28 | |
| 29 | private static final int NO_PRIORITY_VALUE = -1; |
| 30 | private static final int NO_TIMEOUT_VALUE = -1; |
| 31 | |
| 32 | private final String tableName; |
| 33 | private final Bmv2MatchKey matchKey; |
| 34 | private final Bmv2Action action; |
| 35 | private final int priority; |
| 36 | private final double timeout; |
| 37 | |
| 38 | private Bmv2TableEntry(String tableName, Bmv2MatchKey matchKey, |
| 39 | Bmv2Action action, int priority, double timeout) { |
| 40 | this.tableName = tableName; |
| 41 | this.matchKey = matchKey; |
| 42 | this.action = action; |
| 43 | this.priority = priority; |
| 44 | this.timeout = timeout; |
| 45 | } |
| 46 | |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 47 | /** |
| 48 | * Returns a new Bmv2 table entry builder. |
| 49 | * |
| 50 | * @return a new builder. |
| 51 | */ |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 52 | public static Builder builder() { |
| 53 | return new Builder(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the name of the table where this entry is installed. |
| 58 | * |
| 59 | * @return table name |
| 60 | */ |
| 61 | public final String tableName() { |
| 62 | return this.tableName; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the match key. |
| 67 | * |
| 68 | * @return match key |
| 69 | */ |
| 70 | public final Bmv2MatchKey matchKey() { |
| 71 | return matchKey; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the action. |
| 76 | * |
| 77 | * @return action |
| 78 | */ |
| 79 | public final Bmv2Action action() { |
| 80 | return action; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns true is the entry has a valid priority value set. |
| 85 | * |
| 86 | * @return true if priority is set, false elsewhere |
| 87 | */ |
| 88 | public final boolean hasPriority() { |
| 89 | return this.priority != NO_PRIORITY_VALUE; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Return the entry priority. |
| 94 | * |
| 95 | * @return priority |
| 96 | */ |
| 97 | public final int priority() { |
| 98 | return priority; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns true is the entry has a valid timeout value set. |
| 103 | * |
| 104 | * @return true if timeout is set, false elsewhere |
| 105 | */ |
| 106 | public final boolean hasTimeout() { |
| 107 | return this.timeout != NO_PRIORITY_VALUE; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the entry timeout in fractional seconds. |
| 112 | * |
| 113 | * @return timeout |
| 114 | */ |
| 115 | public final double timeout() { |
| 116 | return timeout; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public final int hashCode() { |
| 121 | return Objects.hash(matchKey, action, priority, timeout); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public final boolean equals(Object obj) { |
| 126 | if (this == obj) { |
| 127 | return true; |
| 128 | } |
| 129 | if (obj == null || getClass() != obj.getClass()) { |
| 130 | return false; |
| 131 | } |
| 132 | final Bmv2TableEntry other = (Bmv2TableEntry) obj; |
| 133 | return Objects.equals(this.matchKey, other.matchKey) |
| 134 | && Objects.equals(this.action, other.action) |
| 135 | && Objects.equals(this.priority, other.priority) |
| 136 | && Objects.equals(this.timeout, other.timeout); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public final String toString() { |
| 141 | return com.google.common.base.MoreObjects.toStringHelper(this) |
| 142 | .addValue(matchKey) |
| 143 | .addValue(action) |
| 144 | .add("priority", priority) |
| 145 | .add("timeout", timeout) |
| 146 | .toString(); |
| 147 | } |
| 148 | |
| 149 | public static final class Builder { |
| 150 | |
| 151 | private String tableName; |
| 152 | private Bmv2MatchKey matchKey; |
| 153 | private Bmv2Action action; |
| 154 | private int priority = NO_PRIORITY_VALUE; |
| 155 | private double timeout = NO_TIMEOUT_VALUE; |
| 156 | |
| 157 | private Builder() { |
| 158 | // hide constructor |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Sets the table name. |
| 163 | * |
| 164 | * @param tableName a string value |
| 165 | * @return this |
| 166 | */ |
| 167 | public Builder withTableName(String tableName) { |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 168 | this.tableName = checkNotNull(tableName, "table name cannot be null"); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 169 | return this; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Sets the match key. |
| 174 | * |
| 175 | * @param matchKey a match key value |
| 176 | * @return this |
| 177 | */ |
| 178 | public Builder withMatchKey(Bmv2MatchKey matchKey) { |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 179 | this.matchKey = checkNotNull(matchKey, "match key cannot be null"); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 180 | return this; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Sets the action. |
| 185 | * |
| 186 | * @param action an action value |
| 187 | * @return this |
| 188 | */ |
| 189 | public Builder withAction(Bmv2Action action) { |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 190 | this.action = checkNotNull(action, "action cannot be null"); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 191 | return this; |
| 192 | } |
| 193 | |
| 194 | public Builder withPriority(int priority) { |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 195 | checkArgument(priority >= 0, "priority cannot be negative"); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 196 | this.priority = priority; |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets the timeout. |
| 202 | * |
| 203 | * @param timeout a timeout value in fractional seconds |
| 204 | * @return this |
| 205 | */ |
| 206 | public Builder withTimeout(double timeout) { |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 207 | checkArgument(timeout > 0, "timeout must be a positive non-zero value"); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 208 | this.timeout = timeout; |
| 209 | return this; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Build the table entry. |
| 214 | * |
| 215 | * @return a new table entry object |
| 216 | */ |
| 217 | public Bmv2TableEntry build() { |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 218 | return new Bmv2TableEntry(tableName, matchKey, action, priority, |
| 219 | timeout); |
| 220 | |
| 221 | } |
| 222 | } |
| 223 | } |