blob: 741fc041f769ba1de07b07e3519d274e997e32f1 [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
146 public static Builder builder() {
147 return new Builder();
148 }
149
150 public static final class Builder {
151
152 private PiTableId tableId;
153 private Map<PiHeaderFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
154 private PiTableAction tableAction;
155 private long cookie = 0;
156 private int priority = NO_PRIORITY;
157 private double timeout = NO_TIMEOUT;
158
159 private Builder() {
160 // Hides constructor.
161 }
162
163 /**
164 * Sets the table identifier for this entry.
165 *
166 * @param tableId table identifier
167 * @return this
168 */
169 Builder forTable(PiTableId tableId) {
170 this.tableId = checkNotNull(tableId);
171 return this;
172 }
173
174 /**
175 * Sets the action of this table entry.
176 *
177 * @param tableAction table action
178 * @return this
179 */
180 Builder withAction(PiTableAction tableAction) {
181 this.tableAction = checkNotNull(tableAction);
182 return this;
183 }
184
185 /**
186 * Adds one field match to this table entry.
187 *
188 * @param fieldMatch field match
189 * @return this
190 */
191 Builder withFieldMatch(PiFieldMatch fieldMatch) {
192 this.fieldMatches.put(fieldMatch.fieldId(), fieldMatch);
193 return this;
194 }
195
196 /**
197 * Adds many field matches to this table entry.
198 *
199 * @param fieldMatches collection of field matches
200 * @return this
201 */
202 Builder withFieldMatches(Collection<PiFieldMatch> fieldMatches) {
203 fieldMatches.forEach(f -> this.fieldMatches.put(f.fieldId(), f));
204 return this;
205 }
206
207 /**
208 * Sets the cookie, i.e. a controller-specific metadata.
209 *
210 * @param cookie cookie
211 * @return this
212 */
213 Builder withCookie(long cookie) {
214 this.cookie = cookie;
215 return this;
216 }
217
218 /**
219 * Sets the priority of this table entry.
220 *
221 * @param priority priority
222 * @return this
223 */
224 Builder withPriority(int priority) {
225 checkArgument(priority >= 0, "Priority must be a positive integer.");
226 this.priority = priority;
227 return this;
228 }
229
230 /**
231 * Sets the timeout of this table entry.
232 *
233 * @param seconds timeout in seconds
234 * @return this
235 */
236 Builder withTimeout(double seconds) {
237 checkArgument(seconds > 0, "Timeout must be greater than zero.");
238 this.timeout = seconds;
239 return this;
240 }
241
242 /**
243 * Builds the table entry.
244 *
245 * @return a new table entry
246 */
247 PiTableEntry build() {
248 checkNotNull(tableId);
249 checkNotNull(tableAction);
250 return new PiTableEntry(tableId, fieldMatches, tableAction, cookie, priority, timeout);
251 }
252 }
253}