blob: 63e478dc89a3ee1ca5e4afec5b3924cedbc4f14b [file] [log] [blame]
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04001/*
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
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 Cascone00a59962017-06-16 17:51:49 +090022import com.google.common.collect.ImmutableMap;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040023import com.google.common.collect.Maps;
24
25import java.util.Collection;
26import java.util.Map;
27import java.util.Optional;
28
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Table entry in a protocol-independent pipeline.
34 */
35@Beta
36public final class PiTableEntry {
37
38 private static final int NO_PRIORITY = -1;
39 private static final double NO_TIMEOUT = -1;
40
41 private final PiTableId tableId;
Carmelo Cascone00a59962017-06-16 17:51:49 +090042 private final Map<PiHeaderFieldId, PiFieldMatch> fieldMatches;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040043 private final PiTableAction tableAction;
44 private final long cookie;
45 private final int priority;
46 private final double timeout;
47
48 private PiTableEntry(PiTableId tableId, Map<PiHeaderFieldId, PiFieldMatch> fieldMatches,
49 PiTableAction tableAction, long cookie, int priority, double timeout) {
50 this.tableId = tableId;
Carmelo Cascone00a59962017-06-16 17:51:49 +090051 this.fieldMatches = ImmutableMap.copyOf(fieldMatches);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040052 this.tableAction = tableAction;
53 this.cookie = cookie;
54 this.priority = priority;
55 this.timeout = timeout;
56 }
57
58 /**
59 * Returns the table where this entry is installed.
60 *
61 * @return table identifier
62 */
63 public PiTableId table() {
64 return tableId;
65 }
66
67 /**
68 * Returns an immutable view of the field matches of this table entry.
69 *
70 * @return collection of field matches
71 */
72 public Collection<PiFieldMatch> fieldMatches() {
Carmelo Cascone00a59962017-06-16 17:51:49 +090073 return fieldMatches.values();
74 }
75
76 /**
77 * If present, returns the field match associated with the given header field identifier.
78 *
79 * @param fieldId field identifier
80 * @return optional field match
81 */
82 public Optional<PiFieldMatch> fieldMatch(PiHeaderFieldId fieldId) {
83 return Optional.ofNullable(fieldMatches.get(fieldId));
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040084 }
85
86 /**
87 * Returns the action of this table entry.
88 *
89 * @return action
90 */
91 public PiTableAction action() {
92 return tableAction;
93 }
94
95 /**
96 * Returns the cookie of this table entry.
97 *
98 * @return cookie
99 */
100 public long cookie() {
101 return cookie;
102 }
103
104 /**
105 * Returns the priority of this table entry, if present.
106 * If the priority value is not present, then this table entry has no explicit priority.
107 *
108 * @return optional priority
109 */
110 public Optional<Integer> priority() {
111 return priority == NO_PRIORITY ? Optional.empty() : Optional.of(priority);
112 }
113
114 /**
115 * Returns the timeout in seconds of this table entry, if present.
116 * If the timeout value is not present, then this table entry is meant to be permanent.
117 *
118 * @return optional timeout value in seconds
119 */
120 public Optional<Double> timeout() {
121 return timeout == NO_TIMEOUT ? Optional.empty() : Optional.of(timeout);
122 }
123
124 @Override
125 public boolean equals(Object o) {
126 if (this == o) {
127 return true;
128 }
129 if (o == null || getClass() != o.getClass()) {
130 return false;
131 }
132 PiTableEntry that = (PiTableEntry) o;
133 return priority == that.priority &&
134 Double.compare(that.timeout, timeout) == 0 &&
135 Objects.equal(tableId, that.tableId) &&
136 Objects.equal(fieldMatches, that.fieldMatches) &&
137 Objects.equal(tableAction, that.tableAction);
138 }
139
140 @Override
141 public int hashCode() {
142 return Objects.hashCode(tableId, fieldMatches, tableAction, priority, timeout);
143 }
144
145 @Override
146 public String toString() {
147 return MoreObjects.toStringHelper(this)
148 .add("tableId", tableId)
149 .add("fieldMatches", fieldMatches)
150 .add("tableAction", tableAction)
151 .add("priority", priority == NO_PRIORITY ? "N/A" : String.valueOf(priority))
152 .add("timeout", timeout == NO_TIMEOUT ? "PERMANENT" : String.valueOf(timeout))
153 .toString();
154 }
155
Carmelo Cascone7b821702017-06-19 11:26:08 +0900156 /**
157 * Returns a table entry builder.
158 *
159 * @return a new builder
160 */
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400161 public static Builder builder() {
162 return new Builder();
163 }
164
165 public static final class Builder {
166
167 private PiTableId tableId;
168 private Map<PiHeaderFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
169 private PiTableAction tableAction;
170 private long cookie = 0;
171 private int priority = NO_PRIORITY;
172 private double timeout = NO_TIMEOUT;
173
174 private Builder() {
175 // Hides constructor.
176 }
177
178 /**
179 * Sets the table identifier for this entry.
180 *
181 * @param tableId table identifier
182 * @return this
183 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900184 public Builder forTable(PiTableId tableId) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400185 this.tableId = checkNotNull(tableId);
186 return this;
187 }
188
189 /**
190 * Sets the action of this table entry.
191 *
192 * @param tableAction table action
193 * @return this
194 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900195 public Builder withAction(PiTableAction tableAction) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400196 this.tableAction = checkNotNull(tableAction);
197 return this;
198 }
199
200 /**
201 * Adds one field match to this table entry.
202 *
203 * @param fieldMatch field match
204 * @return this
205 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900206 public Builder withFieldMatch(PiFieldMatch fieldMatch) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400207 this.fieldMatches.put(fieldMatch.fieldId(), fieldMatch);
208 return this;
209 }
210
211 /**
212 * Adds many field matches to this table entry.
213 *
214 * @param fieldMatches collection of field matches
215 * @return this
216 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900217 public Builder withFieldMatches(Collection<PiFieldMatch> fieldMatches) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400218 fieldMatches.forEach(f -> this.fieldMatches.put(f.fieldId(), f));
219 return this;
220 }
221
222 /**
223 * Sets the cookie, i.e. a controller-specific metadata.
224 *
225 * @param cookie cookie
226 * @return this
227 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900228 public Builder withCookie(long cookie) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400229 this.cookie = cookie;
230 return this;
231 }
232
233 /**
234 * Sets the priority of this table entry.
235 *
236 * @param priority priority
237 * @return this
238 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900239 public Builder withPriority(int priority) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400240 checkArgument(priority >= 0, "Priority must be a positive integer.");
241 this.priority = priority;
242 return this;
243 }
244
245 /**
246 * Sets the timeout of this table entry.
247 *
248 * @param seconds timeout in seconds
249 * @return this
250 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900251 public Builder withTimeout(double seconds) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400252 checkArgument(seconds > 0, "Timeout must be greater than zero.");
253 this.timeout = seconds;
254 return this;
255 }
256
257 /**
258 * Builds the table entry.
259 *
260 * @return a new table entry
261 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900262 public PiTableEntry build() {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400263 checkNotNull(tableId);
264 checkNotNull(tableAction);
265 return new PiTableEntry(tableId, fieldMatches, tableAction, cookie, priority, timeout);
266 }
267 }
268}