blob: 96adadd3def5648151f17217ce60e1d9e8a03fb3 [file] [log] [blame]
Carmelo Cascone2ea177b2016-02-25 18:38:42 -08001/*
Carmelo Casconeaa8b6292016-04-13 14:27:06 -07002 * Copyright 2016-present Open Networking Laboratory
Carmelo Cascone2ea177b2016-02-25 18:38:42 -08003 *
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
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070017package org.onosproject.bmv2.api.runtime;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080018
19import java.util.Objects;
20
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070021import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080024/**
25 * Bmv2 representation of a table entry.
26 */
27public final class Bmv2TableEntry {
28
29 private static final int NO_PRIORITY_VALUE = -1;
30 private static final int NO_TIMEOUT_VALUE = -1;
31
32 private final String tableName;
33 private final Bmv2MatchKey matchKey;
34 private final Bmv2Action action;
35 private final int priority;
36 private final double timeout;
37
38 private Bmv2TableEntry(String tableName, Bmv2MatchKey matchKey,
39 Bmv2Action action, int priority, double timeout) {
40 this.tableName = tableName;
41 this.matchKey = matchKey;
42 this.action = action;
43 this.priority = priority;
44 this.timeout = timeout;
45 }
46
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070047 /**
48 * Returns a new Bmv2 table entry builder.
49 *
50 * @return a new builder.
51 */
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080052 public static Builder builder() {
53 return new Builder();
54 }
55
56 /**
57 * Returns the name of the table where this entry is installed.
58 *
59 * @return table name
60 */
61 public final String tableName() {
62 return this.tableName;
63 }
64
65 /**
66 * Returns the match key.
67 *
68 * @return match key
69 */
70 public final Bmv2MatchKey matchKey() {
71 return matchKey;
72 }
73
74 /**
75 * Returns the action.
76 *
77 * @return action
78 */
79 public final Bmv2Action action() {
80 return action;
81 }
82
83 /**
84 * Returns true is the entry has a valid priority value set.
85 *
86 * @return true if priority is set, false elsewhere
87 */
88 public final boolean hasPriority() {
89 return this.priority != NO_PRIORITY_VALUE;
90 }
91
92 /**
93 * Return the entry priority.
94 *
95 * @return priority
96 */
97 public final int priority() {
98 return priority;
99 }
100
101 /**
102 * Returns true is the entry has a valid timeout value set.
103 *
104 * @return true if timeout is set, false elsewhere
105 */
106 public final boolean hasTimeout() {
107 return this.timeout != NO_PRIORITY_VALUE;
108 }
109
110 /**
111 * Returns the entry timeout in fractional seconds.
112 *
113 * @return timeout
114 */
115 public final double timeout() {
116 return timeout;
117 }
118
119 @Override
120 public final int hashCode() {
121 return Objects.hash(matchKey, action, priority, timeout);
122 }
123
124 @Override
125 public final boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129 if (obj == null || getClass() != obj.getClass()) {
130 return false;
131 }
132 final Bmv2TableEntry other = (Bmv2TableEntry) obj;
133 return Objects.equals(this.matchKey, other.matchKey)
134 && Objects.equals(this.action, other.action)
135 && Objects.equals(this.priority, other.priority)
136 && Objects.equals(this.timeout, other.timeout);
137 }
138
139 @Override
140 public final String toString() {
141 return com.google.common.base.MoreObjects.toStringHelper(this)
142 .addValue(matchKey)
143 .addValue(action)
144 .add("priority", priority)
145 .add("timeout", timeout)
146 .toString();
147 }
148
149 public static final class Builder {
150
151 private String tableName;
152 private Bmv2MatchKey matchKey;
153 private Bmv2Action action;
154 private int priority = NO_PRIORITY_VALUE;
155 private double timeout = NO_TIMEOUT_VALUE;
156
157 private Builder() {
158 // hide constructor
159 }
160
161 /**
162 * Sets the table name.
163 *
164 * @param tableName a string value
165 * @return this
166 */
167 public Builder withTableName(String tableName) {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700168 this.tableName = checkNotNull(tableName, "table name cannot be null");
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800169 return this;
170 }
171
172 /**
173 * Sets the match key.
174 *
175 * @param matchKey a match key value
176 * @return this
177 */
178 public Builder withMatchKey(Bmv2MatchKey matchKey) {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700179 this.matchKey = checkNotNull(matchKey, "match key cannot be null");
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800180 return this;
181 }
182
183 /**
184 * Sets the action.
185 *
186 * @param action an action value
187 * @return this
188 */
189 public Builder withAction(Bmv2Action action) {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700190 this.action = checkNotNull(action, "action cannot be null");
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800191 return this;
192 }
193
194 public Builder withPriority(int priority) {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700195 checkArgument(priority >= 0, "priority cannot be negative");
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800196 this.priority = priority;
197 return this;
198 }
199
200 /**
201 * Sets the timeout.
202 *
203 * @param timeout a timeout value in fractional seconds
204 * @return this
205 */
206 public Builder withTimeout(double timeout) {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700207 checkArgument(timeout > 0, "timeout must be a positive non-zero value");
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800208 this.timeout = timeout;
209 return this;
210 }
211
212 /**
213 * Build the table entry.
214 *
215 * @return a new table entry object
216 */
217 public Bmv2TableEntry build() {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800218 return new Bmv2TableEntry(tableName, matchKey, action, priority,
219 timeout);
220
221 }
222 }
223}