blob: cd7e493a06b7bd8b89ad05d9167ff1ae3a77cac5 [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
33public final class PiTableEntry {
34
Carmelo Cascone7f75be42017-09-07 14:37:02 +020035 public static final PiTableEntry EMTPY = new PiTableEntry();
36
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040037 private static final int NO_PRIORITY = -1;
38 private static final double NO_TIMEOUT = -1;
39
40 private final PiTableId tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020041 private final PiMatchKey matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040042 private final PiTableAction tableAction;
43 private final long cookie;
44 private final int priority;
45 private final double timeout;
46
Carmelo Cascone7f75be42017-09-07 14:37:02 +020047 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 Cascone0e896a02017-07-31 07:22:27 +020056 private PiTableEntry(PiTableId tableId, PiMatchKey matchKey,
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040057 PiTableAction tableAction, long cookie, int priority, double timeout) {
58 this.tableId = tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020059 this.matchKey = matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040060 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 Cascone0e896a02017-07-31 07:22:27 +020076 * Returns the match key of this table entry.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040077 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +020078 * @return match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040079 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +020080 public PiMatchKey matchKey() {
81 return matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040082 }
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 Cascone87892e22017-11-13 16:01:29 -0800103 * 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 Cascone1022a4e2017-05-25 00:16:18 -0400105 *
106 * @return optional priority
107 */
108 public Optional<Integer> priority() {
109 return priority == NO_PRIORITY ? Optional.empty() : Optional.of(priority);
110 }
111
112 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -0800113 * 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 Cascone1022a4e2017-05-25 00:16:18 -0400115 *
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 Cascone0e896a02017-07-31 07:22:27 +0200134 Objects.equal(matchKey, that.matchKey) &&
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400135 Objects.equal(tableAction, that.tableAction);
136 }
137
138 @Override
139 public int hashCode() {
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200140 return Objects.hashCode(tableId, matchKey, tableAction, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400141 }
142
143 @Override
144 public String toString() {
145 return MoreObjects.toStringHelper(this)
146 .add("tableId", tableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200147 .add("matchKey", matchKey)
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400148 .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 Cascone7b821702017-06-19 11:26:08 +0900154 /**
155 * Returns a table entry builder.
156 *
157 * @return a new builder
158 */
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400159 public static Builder builder() {
160 return new Builder();
161 }
162
163 public static final class Builder {
164
165 private PiTableId tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200166 private PiMatchKey matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400167 private PiTableAction tableAction;
168 private long cookie = 0;
169 private int priority = NO_PRIORITY;
170 private double timeout = NO_TIMEOUT;
171
172 private Builder() {
173 // Hides constructor.
174 }
175
176 /**
177 * Sets the table identifier for this entry.
178 *
179 * @param tableId table identifier
180 * @return this
181 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900182 public Builder forTable(PiTableId tableId) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400183 this.tableId = checkNotNull(tableId);
184 return this;
185 }
186
187 /**
188 * Sets the action of this table entry.
189 *
190 * @param tableAction table action
191 * @return this
192 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900193 public Builder withAction(PiTableAction tableAction) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400194 this.tableAction = checkNotNull(tableAction);
195 return this;
196 }
197
198 /**
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200199 * Sets the match key of this table entry.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400200 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200201 * @param matchKey match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400202 * @return this
203 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200204 public Builder withMatchKey(PiMatchKey matchKey) {
205 this.matchKey = matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400206 return this;
207 }
208
209 /**
210 * Sets the cookie, i.e. a controller-specific metadata.
211 *
212 * @param cookie cookie
213 * @return this
214 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900215 public Builder withCookie(long cookie) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400216 this.cookie = cookie;
217 return this;
218 }
219
220 /**
221 * Sets the priority of this table entry.
222 *
223 * @param priority priority
224 * @return this
225 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900226 public Builder withPriority(int priority) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400227 checkArgument(priority >= 0, "Priority must be a positive integer.");
228 this.priority = priority;
229 return this;
230 }
231
232 /**
233 * Sets the timeout of this table entry.
234 *
235 * @param seconds timeout in seconds
236 * @return this
237 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900238 public Builder withTimeout(double seconds) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400239 checkArgument(seconds > 0, "Timeout must be greater than zero.");
240 this.timeout = seconds;
241 return this;
242 }
243
244 /**
245 * Builds the table entry.
246 *
247 * @return a new table entry
248 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900249 public PiTableEntry build() {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400250 checkNotNull(tableId);
251 checkNotNull(tableAction);
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200252 return new PiTableEntry(tableId, matchKey, tableAction, cookie, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400253 }
254 }
255}