alshabib
Committed by Ray Milkey

Introduced a builder for flowrules and deprecated the old flowrule constructors.

Change-Id: I4bf6e102c67c2effd0d8b65538fcf3a8a1db69e1
...@@ -23,6 +23,8 @@ import org.onosproject.net.DeviceId; ...@@ -23,6 +23,8 @@ import org.onosproject.net.DeviceId;
23 import java.util.Objects; 23 import java.util.Objects;
24 24
25 import static com.google.common.base.MoreObjects.toStringHelper; 25 import static com.google.common.base.MoreObjects.toStringHelper;
26 +import static com.google.common.base.Preconditions.checkArgument;
27 +import static com.google.common.base.Preconditions.checkNotNull;
26 28
27 public class DefaultFlowRule implements FlowRule { 29 public class DefaultFlowRule implements FlowRule {
28 30
...@@ -34,15 +36,17 @@ public class DefaultFlowRule implements FlowRule { ...@@ -34,15 +36,17 @@ public class DefaultFlowRule implements FlowRule {
34 36
35 private final FlowId id; 37 private final FlowId id;
36 38
37 - private final short appId; 39 + private final Short appId;
38 40
39 private final int timeout; 41 private final int timeout;
40 private final boolean permanent; 42 private final boolean permanent;
41 private final GroupId groupId; 43 private final GroupId groupId;
42 44
43 private final Type type; 45 private final Type type;
46 + private final Integer tableId;
44 47
45 48
49 + @Deprecated
46 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, 50 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
47 TrafficTreatment treatment, int priority, long flowId, 51 TrafficTreatment treatment, int priority, long flowId,
48 int timeout, boolean permanent) { 52 int timeout, boolean permanent) {
...@@ -58,8 +62,10 @@ public class DefaultFlowRule implements FlowRule { ...@@ -58,8 +62,10 @@ public class DefaultFlowRule implements FlowRule {
58 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF)); 62 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF));
59 this.id = FlowId.valueOf(flowId); 63 this.id = FlowId.valueOf(flowId);
60 this.type = Type.DEFAULT; 64 this.type = Type.DEFAULT;
65 + this.tableId = 0;
61 } 66 }
62 67
68 + @Deprecated
63 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, 69 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
64 TrafficTreatment treatment, int priority, long flowId, 70 TrafficTreatment treatment, int priority, long flowId,
65 int timeout, boolean permanent, Type tableType) { 71 int timeout, boolean permanent, Type tableType) {
...@@ -75,8 +81,11 @@ public class DefaultFlowRule implements FlowRule { ...@@ -75,8 +81,11 @@ public class DefaultFlowRule implements FlowRule {
75 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF)); 81 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF));
76 this.id = FlowId.valueOf(flowId); 82 this.id = FlowId.valueOf(flowId);
77 this.type = tableType; 83 this.type = tableType;
84 + this.tableId = 0;
85 +
78 } 86 }
79 87
88 + @Deprecated
80 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, 89 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
81 TrafficTreatment treatment, int priority, ApplicationId appId, 90 TrafficTreatment treatment, int priority, ApplicationId appId,
82 int timeout, boolean permanent) { 91 int timeout, boolean permanent) {
...@@ -84,6 +93,7 @@ public class DefaultFlowRule implements FlowRule { ...@@ -84,6 +93,7 @@ public class DefaultFlowRule implements FlowRule {
84 timeout, permanent); 93 timeout, permanent);
85 } 94 }
86 95
96 + @Deprecated
87 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, 97 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
88 TrafficTreatment treatment, int priority, ApplicationId appId, 98 TrafficTreatment treatment, int priority, ApplicationId appId,
89 int timeout, boolean permanent, Type type) { 99 int timeout, boolean permanent, Type type) {
...@@ -102,6 +112,7 @@ public class DefaultFlowRule implements FlowRule { ...@@ -102,6 +112,7 @@ public class DefaultFlowRule implements FlowRule {
102 this.permanent = permanent; 112 this.permanent = permanent;
103 this.created = System.currentTimeMillis(); 113 this.created = System.currentTimeMillis();
104 this.type = type; 114 this.type = type;
115 + this.tableId = 0;
105 116
106 /* 117 /*
107 * id consists of the following. 118 * id consists of the following.
...@@ -112,6 +123,7 @@ public class DefaultFlowRule implements FlowRule { ...@@ -112,6 +123,7 @@ public class DefaultFlowRule implements FlowRule {
112 123
113 } 124 }
114 125
126 + @Deprecated
115 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, 127 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
116 TrafficTreatment treatment, int priority, ApplicationId appId, 128 TrafficTreatment treatment, int priority, ApplicationId appId,
117 GroupId groupId, int timeout, boolean permanent) { 129 GroupId groupId, int timeout, boolean permanent) {
...@@ -130,6 +142,7 @@ public class DefaultFlowRule implements FlowRule { ...@@ -130,6 +142,7 @@ public class DefaultFlowRule implements FlowRule {
130 this.permanent = permanent; 142 this.permanent = permanent;
131 this.created = System.currentTimeMillis(); 143 this.created = System.currentTimeMillis();
132 this.type = Type.DEFAULT; 144 this.type = Type.DEFAULT;
145 + this.tableId = 0;
133 146
134 /* 147 /*
135 * id consists of the following. 148 * id consists of the following.
...@@ -151,6 +164,31 @@ public class DefaultFlowRule implements FlowRule { ...@@ -151,6 +164,31 @@ public class DefaultFlowRule implements FlowRule {
151 this.permanent = rule.isPermanent(); 164 this.permanent = rule.isPermanent();
152 this.created = System.currentTimeMillis(); 165 this.created = System.currentTimeMillis();
153 this.type = rule.type(); 166 this.type = rule.type();
167 + this.tableId = rule.tableId();
168 +
169 + }
170 +
171 + private DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
172 + TrafficTreatment treatment, Integer priority,
173 + FlowId flowId, Boolean permanent, Integer timeout,
174 + Integer tableId) {
175 +
176 + this.deviceId = deviceId;
177 + this.selector = selector;
178 + this.treatment = treatment;
179 + this.priority = priority;
180 + this.appId = (short) (flowId.value() >>> 48);
181 + this.id = flowId;
182 + this.permanent = permanent;
183 + this.timeout = timeout;
184 + this.tableId = tableId;
185 + this.created = System.currentTimeMillis();
186 +
187 +
188 + //FIXME: fields below will be removed.
189 + this.groupId = null;
190 + this.type = null;
191 +
154 192
155 } 193 }
156 194
...@@ -197,7 +235,7 @@ public class DefaultFlowRule implements FlowRule { ...@@ -197,7 +235,7 @@ public class DefaultFlowRule implements FlowRule {
197 * @see java.lang.Object#equals(java.lang.Object) 235 * @see java.lang.Object#equals(java.lang.Object)
198 */ 236 */
199 public int hashCode() { 237 public int hashCode() {
200 - return Objects.hash(deviceId, selector, priority, type); 238 + return Objects.hash(deviceId, selector, priority, type, tableId);
201 } 239 }
202 240
203 public int hash() { 241 public int hash() {
...@@ -220,6 +258,7 @@ public class DefaultFlowRule implements FlowRule { ...@@ -220,6 +258,7 @@ public class DefaultFlowRule implements FlowRule {
220 return Objects.equals(deviceId, that.deviceId) && 258 return Objects.equals(deviceId, that.deviceId) &&
221 Objects.equals(priority, that.priority) && 259 Objects.equals(priority, that.priority) &&
222 Objects.equals(selector, that.selector) && 260 Objects.equals(selector, that.selector) &&
261 + Objects.equals(tableId, that.tableId) &&
223 Objects.equals(type, that.type); 262 Objects.equals(type, that.type);
224 263
225 } 264 }
...@@ -254,4 +293,107 @@ public class DefaultFlowRule implements FlowRule { ...@@ -254,4 +293,107 @@ public class DefaultFlowRule implements FlowRule {
254 return type; 293 return type;
255 } 294 }
256 295
296 + @Override
297 + public int tableId() {
298 + return tableId;
299 + }
300 +
301 + public static Builder builder() {
302 + return new Builder();
303 + }
304 +
305 + private static final class Builder implements FlowRule.Builder {
306 +
307 + private FlowId flowId;
308 + private Integer priority;
309 + private DeviceId deviceId;
310 + private Integer tableId = 0;
311 + private TrafficSelector selector;
312 + private TrafficTreatment treatment;
313 + private Integer timeout;
314 + private Boolean permanent;
315 +
316 + @Override
317 + public FlowRule.Builder withCookie(long cookie) {
318 + this.flowId = FlowId.valueOf(cookie);
319 + return this;
320 + }
321 +
322 + @Override
323 + public FlowRule.Builder fromApp(ApplicationId appId) {
324 + this.flowId = computeFlowId(appId);
325 + return this;
326 + }
327 +
328 + @Override
329 + public FlowRule.Builder withPriority(int priority) {
330 + this.priority = priority;
331 + return this;
332 + }
333 +
334 + @Override
335 + public FlowRule.Builder forDevice(DeviceId deviceId) {
336 + this.deviceId = deviceId;
337 + return this;
338 + }
339 +
340 + @Override
341 + public FlowRule.Builder forTable(int tableId) {
342 + this.tableId = tableId;
343 + return this;
344 + }
345 +
346 + @Override
347 + public FlowRule.Builder withSelector(TrafficSelector selector) {
348 + this.selector = selector;
349 + return this;
350 + }
351 +
352 + @Override
353 + public FlowRule.Builder withTreatment(TrafficTreatment treatment) {
354 + this.treatment = treatment;
355 + return this;
356 + }
357 +
358 + @Override
359 + public FlowRule.Builder makePermanent() {
360 + this.timeout = 0;
361 + this.permanent = true;
362 + return this;
363 + }
364 +
365 + @Override
366 + public FlowRule.Builder makeTemporary(int timeout) {
367 + this.permanent = false;
368 + this.timeout = timeout;
369 + return this;
370 + }
371 +
372 + @Override
373 + public FlowRule build() {
374 + checkNotNull(flowId != null, "Either an application" +
375 + " id or a cookie must be supplied");
376 + checkNotNull(selector != null, "Traffic selector cannot be null");
377 + checkNotNull(timeout != null || permanent != null, "Must either have " +
378 + "a timeout or be permanent");
379 + checkNotNull(deviceId != null, "Must refer to a device");
380 + checkNotNull(priority != null, "Priority cannot be null");
381 + checkArgument(priority < MIN_PRIORITY, "Priority cannot be less than " +
382 + MIN_PRIORITY);
383 +
384 + return new DefaultFlowRule(deviceId, selector, treatment, priority,
385 + flowId, permanent, timeout, tableId);
386 + }
387 +
388 + private FlowId computeFlowId(ApplicationId appId) {
389 + return FlowId.valueOf((((long) appId.id()) << 48)
390 + | (hash() & 0xffffffffL));
391 + }
392 +
393 + private int hash() {
394 + return Objects.hash(deviceId, selector, treatment, tableId);
395 + }
396 +
397 + }
398 +
257 } 399 }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.net.flow; 16 package org.onosproject.net.flow;
17 17
18 +import org.onosproject.core.ApplicationId;
18 import org.onosproject.core.GroupId; 19 import org.onosproject.core.GroupId;
19 import org.onosproject.net.DeviceId; 20 import org.onosproject.net.DeviceId;
20 21
...@@ -32,6 +33,7 @@ public interface FlowRule { ...@@ -32,6 +33,7 @@ public interface FlowRule {
32 * needs to be put for multi-table support switch. 33 * needs to be put for multi-table support switch.
33 * For single table switch, Default is used. 34 * For single table switch, Default is used.
34 */ 35 */
36 + @Deprecated
35 public static enum Type { 37 public static enum Type {
36 /* Default type - used in flow rule for single table switch 38 /* Default type - used in flow rule for single table switch
37 * NOTE: this setting should not be used as Table 0 in a multi-table pipeline*/ 39 * NOTE: this setting should not be used as Table 0 in a multi-table pipeline*/
...@@ -59,7 +61,6 @@ public interface FlowRule { ...@@ -59,7 +61,6 @@ public interface FlowRule {
59 FIRST, 61 FIRST,
60 } 62 }
61 63
62 - //TODO: build cookie value
63 /** 64 /**
64 * Returns the ID of this flow. 65 * Returns the ID of this flow.
65 * 66 *
...@@ -130,6 +131,105 @@ public interface FlowRule { ...@@ -130,6 +131,105 @@ public interface FlowRule {
130 * 131 *
131 * @return flow rule type 132 * @return flow rule type
132 */ 133 */
134 + @Deprecated
133 Type type(); 135 Type type();
134 136
137 + /**
138 + * Returns the table id for this rule.
139 + *
140 + * @return an integer.
141 + */
142 + int tableId();
143 +
144 + /**
145 + * A flowrule builder.
146 + */
147 + public interface Builder {
148 +
149 + /**
150 + * Assigns a cookie value to this flowrule. Mutually exclusive with the
151 + * fromApp method. This method is intended to take a cookie value from
152 + * the dataplane and not from the application.
153 + *
154 + * @param cookie a long value
155 + * @return this
156 + */
157 + Builder withCookie(long cookie);
158 +
159 + /**
160 + * Assigns the application that built this flow rule to this object.
161 + * The short value of the appId will be used as a basis for the
162 + * cookie value computation. It is expected that application use this
163 + * call to set their application id.
164 + *
165 + * @param appId an application id
166 + * @return this
167 + */
168 + Builder fromApp(ApplicationId appId);
169 +
170 + /**
171 + * Sets the priority for this flow rule.
172 + *
173 + * @param priority an integer
174 + * @return this
175 + */
176 + Builder withPriority(int priority);
177 +
178 + /**
179 + * Sets the deviceId for this flow rule.
180 + *
181 + * @param deviceId a device id
182 + * @return this
183 + */
184 + Builder forDevice(DeviceId deviceId);
185 +
186 + /**
187 + * Sets the table id for this flow rule. Default value is 0.
188 + *
189 + * @param tableId an integer
190 + * @return this
191 + */
192 + Builder forTable(int tableId);
193 +
194 + /**
195 + * Sets the selector (or match field) for this flow rule.
196 + *
197 + * @param selector a traffic selector
198 + * @return this
199 + */
200 + Builder withSelector(TrafficSelector selector);
201 +
202 + /**
203 + * Sets the traffic treatment for this flow rule.
204 + *
205 + * @param treatment a traffic treatment
206 + * @return this
207 + */
208 + Builder withTreatment(TrafficTreatment treatment);
209 +
210 + /**
211 + * Makes this rule permanent on the dataplane.
212 + *
213 + * @return this
214 + */
215 + Builder makePermanent();
216 +
217 + /**
218 + * Makes this rule temporary and timeout after the specified amount
219 + * of time.
220 + *
221 + * @param timeout an integer
222 + * @return this
223 + */
224 + Builder makeTemporary(int timeout);
225 +
226 + /**
227 + * Builds a flow rule object.
228 + *
229 + * @return a flow rule.
230 + */
231 + FlowRule build();
232 +
233 + }
234 +
135 } 235 }
......
...@@ -411,6 +411,11 @@ public class IntentTestsMocks { ...@@ -411,6 +411,11 @@ public class IntentTestsMocks {
411 public Type type() { 411 public Type type() {
412 return type; 412 return type;
413 } 413 }
414 +
415 + @Override
416 + public int tableId() {
417 + return 0;
418 + }
414 } 419 }
415 420
416 public static class MockIntent extends Intent { 421 public static class MockIntent extends Intent {
......
...@@ -190,6 +190,11 @@ public class FlowsResourceTest extends ResourceTest { ...@@ -190,6 +190,11 @@ public class FlowsResourceTest extends ResourceTest {
190 public Type type() { 190 public Type type() {
191 return Type.DEFAULT; 191 return Type.DEFAULT;
192 } 192 }
193 +
194 + @Override
195 + public int tableId() {
196 + return 0;
197 + }
193 } 198 }
194 199
195 /** 200 /**
......