blob: 6a5f75bbd5c545493a21104ba7d907e5f0595ea7 [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 Cascone1022a4e2017-05-25 00:16:18 -040022
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040023import java.util.Optional;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Table entry in a protocol-independent pipeline.
30 */
31@Beta
32public final class PiTableEntry {
33
Carmelo Cascone7f75be42017-09-07 14:37:02 +020034 public static final PiTableEntry EMTPY = new PiTableEntry();
35
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040036 private static final int NO_PRIORITY = -1;
37 private static final double NO_TIMEOUT = -1;
38
39 private final PiTableId tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020040 private final PiMatchKey matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040041 private final PiTableAction tableAction;
42 private final long cookie;
43 private final int priority;
44 private final double timeout;
45
Carmelo Cascone7f75be42017-09-07 14:37:02 +020046 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 Cascone0e896a02017-07-31 07:22:27 +020055 private PiTableEntry(PiTableId tableId, PiMatchKey matchKey,
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040056 PiTableAction tableAction, long cookie, int priority, double timeout) {
57 this.tableId = tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020058 this.matchKey = matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040059 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 Cascone0e896a02017-07-31 07:22:27 +020075 * Returns the match key of this table entry.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040076 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +020077 * @return match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040078 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +020079 public PiMatchKey matchKey() {
80 return matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040081 }
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 Cascone0e896a02017-07-31 07:22:27 +0200133 Objects.equal(matchKey, that.matchKey) &&
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400134 Objects.equal(tableAction, that.tableAction);
135 }
136
137 @Override
138 public int hashCode() {
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200139 return Objects.hashCode(tableId, matchKey, tableAction, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400140 }
141
142 @Override
143 public String toString() {
144 return MoreObjects.toStringHelper(this)
145 .add("tableId", tableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200146 .add("matchKey", matchKey)
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400147 .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 Cascone7b821702017-06-19 11:26:08 +0900153 /**
154 * Returns a table entry builder.
155 *
156 * @return a new builder
157 */
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400158 public static Builder builder() {
159 return new Builder();
160 }
161
162 public static final class Builder {
163
164 private PiTableId tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200165 private PiMatchKey matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400166 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 Cascone00a59962017-06-16 17:51:49 +0900181 public Builder forTable(PiTableId tableId) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400182 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 Cascone00a59962017-06-16 17:51:49 +0900192 public Builder withAction(PiTableAction tableAction) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400193 this.tableAction = checkNotNull(tableAction);
194 return this;
195 }
196
197 /**
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200198 * Sets the match key of this table entry.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400199 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200200 * @param matchKey match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400201 * @return this
202 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200203 public Builder withMatchKey(PiMatchKey matchKey) {
204 this.matchKey = matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400205 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 Cascone00a59962017-06-16 17:51:49 +0900214 public Builder withCookie(long cookie) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400215 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 Cascone00a59962017-06-16 17:51:49 +0900225 public Builder withPriority(int priority) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400226 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 Cascone00a59962017-06-16 17:51:49 +0900237 public Builder withTimeout(double seconds) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400238 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 Cascone00a59962017-06-16 17:51:49 +0900248 public PiTableEntry build() {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400249 checkNotNull(tableId);
250 checkNotNull(tableAction);
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200251 return new PiTableEntry(tableId, matchKey, tableAction, cookie, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400252 }
253 }
254}