blob: 7b8797ac90a554471ee4ad508166809a3282c4af [file] [log] [blame]
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04003 *
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
Carmelo Cascone87892e22017-11-13 16:01:29 -080022import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040023
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040024import java.util.Optional;
25
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080030 * Instance of a table entry in a protocol-independent pipeline.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040031 */
32@Beta
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080033public final class PiTableEntry implements PiEntity {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040034
35 private static final int NO_PRIORITY = -1;
36 private static final double NO_TIMEOUT = -1;
37
38 private final PiTableId tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020039 private final PiMatchKey matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040040 private final PiTableAction tableAction;
41 private final long cookie;
42 private final int priority;
43 private final double timeout;
44
Carmelo Cascone0e896a02017-07-31 07:22:27 +020045 private PiTableEntry(PiTableId tableId, PiMatchKey matchKey,
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040046 PiTableAction tableAction, long cookie, int priority, double timeout) {
47 this.tableId = tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020048 this.matchKey = matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040049 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 Cascone0e896a02017-07-31 07:22:27 +020065 * Returns the match key of this table entry.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040066 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +020067 * @return match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040068 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +020069 public PiMatchKey matchKey() {
70 return matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040071 }
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 Cascone87892e22017-11-13 16:01:29 -080092 * 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 Cascone1022a4e2017-05-25 00:16:18 -040094 *
95 * @return optional priority
96 */
97 public Optional<Integer> priority() {
98 return priority == NO_PRIORITY ? Optional.empty() : Optional.of(priority);
99 }
100
101 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -0800102 * 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 Cascone1022a4e2017-05-25 00:16:18 -0400104 *
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 Cascone0e896a02017-07-31 07:22:27 +0200123 Objects.equal(matchKey, that.matchKey) &&
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400124 Objects.equal(tableAction, that.tableAction);
125 }
126
127 @Override
128 public int hashCode() {
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200129 return Objects.hashCode(tableId, matchKey, tableAction, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400130 }
131
132 @Override
133 public String toString() {
134 return MoreObjects.toStringHelper(this)
135 .add("tableId", tableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200136 .add("matchKey", matchKey)
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400137 .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 Cascone7b821702017-06-19 11:26:08 +0900143 /**
144 * Returns a table entry builder.
145 *
146 * @return a new builder
147 */
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400148 public static Builder builder() {
149 return new Builder();
150 }
151
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -0800152 @Override
153 public PiEntityType piEntityType() {
154 return PiEntityType.TABLE_ENTRY;
155 }
156
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400157 public static final class Builder {
158
159 private PiTableId tableId;
Carmelo Casconef03f6e92018-04-04 11:39:39 -0700160 private PiMatchKey matchKey = PiMatchKey.EMPTY;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400161 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 Cascone00a59962017-06-16 17:51:49 +0900176 public Builder forTable(PiTableId tableId) {
Carmelo Casconef03f6e92018-04-04 11:39:39 -0700177 this.tableId = checkNotNull(tableId, "Table ID cannot be null");
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400178 return this;
179 }
180
181 /**
182 * Sets the action of this table entry.
183 *
184 * @param tableAction table action
185 * @return this
186 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900187 public Builder withAction(PiTableAction tableAction) {
Carmelo Casconef03f6e92018-04-04 11:39:39 -0700188 this.tableAction = checkNotNull(tableAction, "Action cannot be null");
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400189 return this;
190 }
191
192 /**
Carmelo Casconef03f6e92018-04-04 11:39:39 -0700193 * Sets the match key of this table entry. By default, the match key is
194 * {@link PiMatchKey#EMPTY}, i.e. any match.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400195 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200196 * @param matchKey match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400197 * @return this
198 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200199 public Builder withMatchKey(PiMatchKey matchKey) {
Carmelo Casconef03f6e92018-04-04 11:39:39 -0700200 this.matchKey = checkNotNull(matchKey, "Match key cannot be null");
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400201 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 Cascone00a59962017-06-16 17:51:49 +0900210 public Builder withCookie(long cookie) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400211 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 Cascone00a59962017-06-16 17:51:49 +0900221 public Builder withPriority(int priority) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400222 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 Cascone00a59962017-06-16 17:51:49 +0900233 public Builder withTimeout(double seconds) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400234 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 Cascone00a59962017-06-16 17:51:49 +0900244 public PiTableEntry build() {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400245 checkNotNull(tableId);
Carmelo Casconef03f6e92018-04-04 11:39:39 -0700246 checkNotNull(matchKey);
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200247 return new PiTableEntry(tableId, matchKey, tableAction, cookie, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400248 }
249 }
250}