blob: 40b2f0593405ba8072c797d3af2766cd6ef0590a [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 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
34 private static final int NO_PRIORITY = -1;
35 private static final double NO_TIMEOUT = -1;
36
37 private final PiTableId tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020038 private final PiMatchKey matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040039 private final PiTableAction tableAction;
40 private final long cookie;
41 private final int priority;
42 private final double timeout;
43
Carmelo Cascone0e896a02017-07-31 07:22:27 +020044 private PiTableEntry(PiTableId tableId, PiMatchKey matchKey,
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040045 PiTableAction tableAction, long cookie, int priority, double timeout) {
46 this.tableId = tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020047 this.matchKey = matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040048 this.tableAction = tableAction;
49 this.cookie = cookie;
50 this.priority = priority;
51 this.timeout = timeout;
52 }
53
54 /**
55 * Returns the table where this entry is installed.
56 *
57 * @return table identifier
58 */
59 public PiTableId table() {
60 return tableId;
61 }
62
63 /**
Carmelo Cascone0e896a02017-07-31 07:22:27 +020064 * Returns the match key of this table entry.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040065 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +020066 * @return match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040067 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +020068 public PiMatchKey matchKey() {
69 return matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040070 }
71
72 /**
73 * Returns the action of this table entry.
74 *
75 * @return action
76 */
77 public PiTableAction action() {
78 return tableAction;
79 }
80
81 /**
82 * Returns the cookie of this table entry.
83 *
84 * @return cookie
85 */
86 public long cookie() {
87 return cookie;
88 }
89
90 /**
91 * Returns the priority of this table entry, if present.
92 * If the priority value is not present, then this table entry has no explicit priority.
93 *
94 * @return optional priority
95 */
96 public Optional<Integer> priority() {
97 return priority == NO_PRIORITY ? Optional.empty() : Optional.of(priority);
98 }
99
100 /**
101 * Returns the timeout in seconds of this table entry, if present.
102 * If the timeout value is not present, then this table entry is meant to be permanent.
103 *
104 * @return optional timeout value in seconds
105 */
106 public Optional<Double> timeout() {
107 return timeout == NO_TIMEOUT ? Optional.empty() : Optional.of(timeout);
108 }
109
110 @Override
111 public boolean equals(Object o) {
112 if (this == o) {
113 return true;
114 }
115 if (o == null || getClass() != o.getClass()) {
116 return false;
117 }
118 PiTableEntry that = (PiTableEntry) o;
119 return priority == that.priority &&
120 Double.compare(that.timeout, timeout) == 0 &&
121 Objects.equal(tableId, that.tableId) &&
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200122 Objects.equal(matchKey, that.matchKey) &&
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400123 Objects.equal(tableAction, that.tableAction);
124 }
125
126 @Override
127 public int hashCode() {
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200128 return Objects.hashCode(tableId, matchKey, tableAction, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400129 }
130
131 @Override
132 public String toString() {
133 return MoreObjects.toStringHelper(this)
134 .add("tableId", tableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200135 .add("matchKey", matchKey)
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400136 .add("tableAction", tableAction)
137 .add("priority", priority == NO_PRIORITY ? "N/A" : String.valueOf(priority))
138 .add("timeout", timeout == NO_TIMEOUT ? "PERMANENT" : String.valueOf(timeout))
139 .toString();
140 }
141
Carmelo Cascone7b821702017-06-19 11:26:08 +0900142 /**
143 * Returns a table entry builder.
144 *
145 * @return a new builder
146 */
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400147 public static Builder builder() {
148 return new Builder();
149 }
150
151 public static final class Builder {
152
153 private PiTableId tableId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200154 private PiMatchKey matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400155 private PiTableAction tableAction;
156 private long cookie = 0;
157 private int priority = NO_PRIORITY;
158 private double timeout = NO_TIMEOUT;
159
160 private Builder() {
161 // Hides constructor.
162 }
163
164 /**
165 * Sets the table identifier for this entry.
166 *
167 * @param tableId table identifier
168 * @return this
169 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900170 public Builder forTable(PiTableId tableId) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400171 this.tableId = checkNotNull(tableId);
172 return this;
173 }
174
175 /**
176 * Sets the action of this table entry.
177 *
178 * @param tableAction table action
179 * @return this
180 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900181 public Builder withAction(PiTableAction tableAction) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400182 this.tableAction = checkNotNull(tableAction);
183 return this;
184 }
185
186 /**
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200187 * Sets the match key of this table entry.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400188 *
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200189 * @param matchKey match key
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400190 * @return this
191 */
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200192 public Builder withMatchKey(PiMatchKey matchKey) {
193 this.matchKey = matchKey;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400194 return this;
195 }
196
197 /**
198 * Sets the cookie, i.e. a controller-specific metadata.
199 *
200 * @param cookie cookie
201 * @return this
202 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900203 public Builder withCookie(long cookie) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400204 this.cookie = cookie;
205 return this;
206 }
207
208 /**
209 * Sets the priority of this table entry.
210 *
211 * @param priority priority
212 * @return this
213 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900214 public Builder withPriority(int priority) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400215 checkArgument(priority >= 0, "Priority must be a positive integer.");
216 this.priority = priority;
217 return this;
218 }
219
220 /**
221 * Sets the timeout of this table entry.
222 *
223 * @param seconds timeout in seconds
224 * @return this
225 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900226 public Builder withTimeout(double seconds) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400227 checkArgument(seconds > 0, "Timeout must be greater than zero.");
228 this.timeout = seconds;
229 return this;
230 }
231
232 /**
233 * Builds the table entry.
234 *
235 * @return a new table entry
236 */
Carmelo Cascone00a59962017-06-16 17:51:49 +0900237 public PiTableEntry build() {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400238 checkNotNull(tableId);
239 checkNotNull(tableAction);
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200240 return new PiTableEntry(tableId, matchKey, tableAction, cookie, priority, timeout);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400241 }
242 }
243}