blob: c6f60943cacb326cc01b2f3c2180c32d47a57184 [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;
22import com.google.common.collect.ImmutableSet;
23import 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;
42 private final Collection<PiFieldMatch> fieldMatches;
43 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;
51 this.fieldMatches = ImmutableSet.copyOf(fieldMatches.values());
52 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() {
73 return fieldMatches;
74 }
75
76 /**
77 * Returns the action of this table entry.
78 *
79 * @return action
80 */
81 public PiTableAction action() {
82 return tableAction;
83 }
84
85 /**
86 * Returns the cookie of this table entry.
87 *
88 * @return cookie
89 */
90 public long cookie() {
91 return cookie;
92 }
93
94 /**
95 * Returns the priority of this table entry, if present.
96 * If the priority value is not present, then this table entry has no explicit priority.
97 *
98 * @return optional priority
99 */
100 public Optional<Integer> priority() {
101 return priority == NO_PRIORITY ? Optional.empty() : Optional.of(priority);
102 }
103
104 /**
105 * Returns the timeout in seconds of this table entry, if present.
106 * If the timeout value is not present, then this table entry is meant to be permanent.
107 *
108 * @return optional timeout value in seconds
109 */
110 public Optional<Double> timeout() {
111 return timeout == NO_TIMEOUT ? Optional.empty() : Optional.of(timeout);
112 }
113
114 @Override
115 public boolean equals(Object o) {
116 if (this == o) {
117 return true;
118 }
119 if (o == null || getClass() != o.getClass()) {
120 return false;
121 }
122 PiTableEntry that = (PiTableEntry) o;
123 return priority == that.priority &&
124 Double.compare(that.timeout, timeout) == 0 &&
125 Objects.equal(tableId, that.tableId) &&
126 Objects.equal(fieldMatches, that.fieldMatches) &&
127 Objects.equal(tableAction, that.tableAction);
128 }
129
130 @Override
131 public int hashCode() {
132 return Objects.hashCode(tableId, fieldMatches, tableAction, priority, timeout);
133 }
134
135 @Override
136 public String toString() {
137 return MoreObjects.toStringHelper(this)
138 .add("tableId", tableId)
139 .add("fieldMatches", fieldMatches)
140 .add("tableAction", tableAction)
141 .add("priority", priority == NO_PRIORITY ? "N/A" : String.valueOf(priority))
142 .add("timeout", timeout == NO_TIMEOUT ? "PERMANENT" : String.valueOf(timeout))
143 .toString();
144 }
145
Carmelo Cascone7b821702017-06-19 11:26:08 +0900146 /**
147 * Returns a table entry builder.
148 *
149 * @return a new builder
150 */
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400151 public static Builder builder() {
152 return new Builder();
153 }
154
155 public static final class Builder {
156
157 private PiTableId tableId;
158 private Map<PiHeaderFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
159 private PiTableAction tableAction;
160 private long cookie = 0;
161 private int priority = NO_PRIORITY;
162 private double timeout = NO_TIMEOUT;
163
164 private Builder() {
165 // Hides constructor.
166 }
167
168 /**
169 * Sets the table identifier for this entry.
170 *
171 * @param tableId table identifier
172 * @return this
173 */
174 Builder forTable(PiTableId tableId) {
175 this.tableId = checkNotNull(tableId);
176 return this;
177 }
178
179 /**
180 * Sets the action of this table entry.
181 *
182 * @param tableAction table action
183 * @return this
184 */
185 Builder withAction(PiTableAction tableAction) {
186 this.tableAction = checkNotNull(tableAction);
187 return this;
188 }
189
190 /**
191 * Adds one field match to this table entry.
192 *
193 * @param fieldMatch field match
194 * @return this
195 */
196 Builder withFieldMatch(PiFieldMatch fieldMatch) {
197 this.fieldMatches.put(fieldMatch.fieldId(), fieldMatch);
198 return this;
199 }
200
201 /**
202 * Adds many field matches to this table entry.
203 *
204 * @param fieldMatches collection of field matches
205 * @return this
206 */
207 Builder withFieldMatches(Collection<PiFieldMatch> fieldMatches) {
208 fieldMatches.forEach(f -> this.fieldMatches.put(f.fieldId(), f));
209 return this;
210 }
211
212 /**
213 * Sets the cookie, i.e. a controller-specific metadata.
214 *
215 * @param cookie cookie
216 * @return this
217 */
218 Builder withCookie(long cookie) {
219 this.cookie = cookie;
220 return this;
221 }
222
223 /**
224 * Sets the priority of this table entry.
225 *
226 * @param priority priority
227 * @return this
228 */
229 Builder withPriority(int priority) {
230 checkArgument(priority >= 0, "Priority must be a positive integer.");
231 this.priority = priority;
232 return this;
233 }
234
235 /**
236 * Sets the timeout of this table entry.
237 *
238 * @param seconds timeout in seconds
239 * @return this
240 */
241 Builder withTimeout(double seconds) {
242 checkArgument(seconds > 0, "Timeout must be greater than zero.");
243 this.timeout = seconds;
244 return this;
245 }
246
247 /**
248 * Builds the table entry.
249 *
250 * @return a new table entry
251 */
252 PiTableEntry build() {
253 checkNotNull(tableId);
254 checkNotNull(tableAction);
255 return new PiTableEntry(tableId, fieldMatches, tableAction, cookie, priority, timeout);
256 }
257 }
258}