Thomas Vachuska
Committed by Gerrit Code Review

Adding copy builders for flow objectives.

Adding missing hashCode and equals methods.

Change-Id: I97b2d904eacf0c45a95905c0891dbc6465e18ec6
...@@ -36,7 +36,6 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -36,7 +36,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
36 @Beta 36 @Beta
37 public final class DefaultFilteringObjective implements FilteringObjective { 37 public final class DefaultFilteringObjective implements FilteringObjective {
38 38
39 -
40 private final Type type; 39 private final Type type;
41 private final boolean permanent; 40 private final boolean permanent;
42 private final int timeout; 41 private final int timeout;
...@@ -62,7 +61,7 @@ public final class DefaultFilteringObjective implements FilteringObjective { ...@@ -62,7 +61,7 @@ public final class DefaultFilteringObjective implements FilteringObjective {
62 this.meta = builder.meta; 61 this.meta = builder.meta;
63 62
64 this.id = Objects.hash(type, key, conditions, permanent, 63 this.id = Objects.hash(type, key, conditions, permanent,
65 - timeout, appId, priority); 64 + timeout, appId, priority);
66 } 65 }
67 66
68 @Override 67 @Override
...@@ -121,6 +120,33 @@ public final class DefaultFilteringObjective implements FilteringObjective { ...@@ -121,6 +120,33 @@ public final class DefaultFilteringObjective implements FilteringObjective {
121 return context; 120 return context;
122 } 121 }
123 122
123 +
124 + @Override
125 + public int hashCode() {
126 + return Objects.hash(type, permanent, timeout, appId, priority, key,
127 + conditions, op, meta);
128 + }
129 +
130 + @Override
131 + public boolean equals(Object obj) {
132 + if (this == obj) {
133 + return true;
134 + }
135 + if (obj instanceof DefaultFilteringObjective) {
136 + final DefaultFilteringObjective other = (DefaultFilteringObjective) obj;
137 + return Objects.equals(this.type, other.type)
138 + && Objects.equals(this.permanent, other.permanent)
139 + && Objects.equals(this.timeout, other.timeout)
140 + && Objects.equals(this.appId, other.appId)
141 + && Objects.equals(this.priority, other.priority)
142 + && Objects.equals(this.key, other.key)
143 + && Objects.equals(this.conditions, other.conditions)
144 + && Objects.equals(this.op, other.op)
145 + && Objects.equals(this.meta, other.meta);
146 + }
147 + return false;
148 + }
149 +
124 /** 150 /**
125 * Returns a new builder. 151 * Returns a new builder.
126 * 152 *
...@@ -130,6 +156,10 @@ public final class DefaultFilteringObjective implements FilteringObjective { ...@@ -130,6 +156,10 @@ public final class DefaultFilteringObjective implements FilteringObjective {
130 return new Builder(); 156 return new Builder();
131 } 157 }
132 158
159 + @Override
160 + public Builder copy() {
161 + return new Builder(this);
162 + }
133 163
134 public static final class Builder implements FilteringObjective.Builder { 164 public static final class Builder implements FilteringObjective.Builder {
135 private final ImmutableList.Builder<Criterion> listBuilder 165 private final ImmutableList.Builder<Criterion> listBuilder
...@@ -146,6 +176,23 @@ public final class DefaultFilteringObjective implements FilteringObjective { ...@@ -146,6 +176,23 @@ public final class DefaultFilteringObjective implements FilteringObjective {
146 private ObjectiveContext context; 176 private ObjectiveContext context;
147 private TrafficTreatment meta; 177 private TrafficTreatment meta;
148 178
179 + // Creates an empty builder
180 + private Builder() {
181 + }
182 +
183 + // Creates a builder set to create a copy of the specified objective.
184 + private Builder(FilteringObjective objective) {
185 + this.type = objective.type();
186 + this.key = objective.key();
187 + this.conditions = ImmutableList.copyOf(objective.conditions());
188 + this.permanent = objective.permanent();
189 + this.timeout = objective.timeout();
190 + this.priority = objective.priority();
191 + this.appId = objective.appId();
192 + this.meta = objective.meta();
193 + this.op = objective.op();
194 + }
195 +
149 @Override 196 @Override
150 public Builder withKey(Criterion key) { 197 public Builder withKey(Criterion key) {
151 this.key = key; 198 this.key = key;
...@@ -210,7 +257,6 @@ public final class DefaultFilteringObjective implements FilteringObjective { ...@@ -210,7 +257,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
210 checkNotNull(appId, "Must supply an application id"); 257 checkNotNull(appId, "Must supply an application id");
211 258
212 return new DefaultFilteringObjective(this); 259 return new DefaultFilteringObjective(this);
213 -
214 } 260 }
215 261
216 @Override 262 @Override
...@@ -222,7 +268,6 @@ public final class DefaultFilteringObjective implements FilteringObjective { ...@@ -222,7 +268,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
222 op = Operation.REMOVE; 268 op = Operation.REMOVE;
223 269
224 return new DefaultFilteringObjective(this); 270 return new DefaultFilteringObjective(this);
225 -
226 } 271 }
227 272
228 @Override 273 @Override
...@@ -249,7 +294,6 @@ public final class DefaultFilteringObjective implements FilteringObjective { ...@@ -249,7 +294,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
249 return new DefaultFilteringObjective(this); 294 return new DefaultFilteringObjective(this);
250 } 295 }
251 296
252 -
253 } 297 }
254 298
255 } 299 }
......
...@@ -120,49 +120,28 @@ public final class DefaultForwardingObjective implements ForwardingObjective { ...@@ -120,49 +120,28 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
120 return context; 120 return context;
121 } 121 }
122 122
123 - /*
124 - * (non-Javadoc)
125 - *
126 - * @see java.lang.Object#hashCode()
127 - */
128 @Override 123 @Override
129 public int hashCode() { 124 public int hashCode() {
130 - return Objects.hash(selector, flag, permanent, 125 + return Objects.hash(selector, flag, permanent, timeout, appId,
131 - timeout, appId, priority, nextId, 126 + priority, nextId, treatment, op);
132 - treatment, op);
133 } 127 }
134 128
135 - /*
136 - * (non-Javadoc)
137 - *
138 - * @see java.lang.Object#equals(java.lang.Object)
139 - */
140 @Override 129 @Override
141 - public boolean equals(final Object obj) { 130 + public boolean equals(Object obj) {
142 if (this == obj) { 131 if (this == obj) {
143 return true; 132 return true;
144 } 133 }
145 - if (!(obj instanceof DefaultForwardingObjective)) { 134 + if (obj instanceof DefaultForwardingObjective) {
146 - return false; 135 + final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
147 - } 136 + return Objects.equals(this.selector, other.selector)
148 - final DefaultForwardingObjective other = (DefaultForwardingObjective) obj; 137 + && Objects.equals(this.flag, other.flag)
149 - boolean nextEq = false, treatmentEq = false; 138 + && Objects.equals(this.permanent, other.permanent)
150 - if (this.selector.equals(other.selector) && 139 + && Objects.equals(this.timeout, other.timeout)
151 - this.flag == other.flag && 140 + && Objects.equals(this.appId, other.appId)
152 - this.permanent == other.permanent && 141 + && Objects.equals(this.priority, other.priority)
153 - this.timeout == other.timeout && 142 + && Objects.equals(this.nextId, other.nextId)
154 - this.appId.equals(other.appId) && 143 + && Objects.equals(this.treatment, other.treatment)
155 - this.priority == other.priority && 144 + && Objects.equals(this.op, other.op);
156 - this.op == other.op) {
157 - if (this.nextId != null && other.nextId != null) {
158 - nextEq = this.nextId == other.nextId;
159 - }
160 - if (this.treatment != null && other.treatment != null) {
161 - treatmentEq = this.treatment.equals(other.treatment);
162 - }
163 - if (nextEq && treatmentEq) {
164 - return true;
165 - }
166 } 145 }
167 return false; 146 return false;
168 } 147 }
...@@ -176,6 +155,13 @@ public final class DefaultForwardingObjective implements ForwardingObjective { ...@@ -176,6 +155,13 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
176 return new Builder(); 155 return new Builder();
177 } 156 }
178 157
158 +
159 + @Override
160 + public Builder copy() {
161 + return new Builder(this);
162 + }
163 +
164 +
179 public static final class Builder implements ForwardingObjective.Builder { 165 public static final class Builder implements ForwardingObjective.Builder {
180 166
181 private TrafficSelector selector; 167 private TrafficSelector selector;
...@@ -189,6 +175,23 @@ public final class DefaultForwardingObjective implements ForwardingObjective { ...@@ -189,6 +175,23 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
189 private Operation op; 175 private Operation op;
190 private ObjectiveContext context; 176 private ObjectiveContext context;
191 177
178 + // Creates an empty builder
179 + private Builder() {
180 + }
181 +
182 + // Creates a builder set to create a copy of the specified objective.
183 + private Builder(ForwardingObjective objective) {
184 + this.selector = objective.selector();
185 + this.flag = objective.flag();
186 + this.permanent = objective.permanent();
187 + this.timeout = objective.timeout();
188 + this.priority = objective.priority();
189 + this.appId = objective.appId();
190 + this.nextId = objective.nextId();
191 + this.treatment = objective.treatment();
192 + this.op = objective.op();
193 + }
194 +
192 @Override 195 @Override
193 public Builder withSelector(TrafficSelector selector) { 196 public Builder withSelector(TrafficSelector selector) {
194 this.selector = selector; 197 this.selector = selector;
...@@ -286,4 +289,5 @@ public final class DefaultForwardingObjective implements ForwardingObjective { ...@@ -286,4 +289,5 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
286 return new DefaultForwardingObjective(this); 289 return new DefaultForwardingObjective(this);
287 } 290 }
288 } 291 }
292 +
289 } 293 }
......
...@@ -23,6 +23,7 @@ import org.onosproject.net.flow.TrafficTreatment; ...@@ -23,6 +23,7 @@ import org.onosproject.net.flow.TrafficTreatment;
23 23
24 import java.util.Collection; 24 import java.util.Collection;
25 import java.util.List; 25 import java.util.List;
26 +import java.util.Objects;
26 import java.util.Optional; 27 import java.util.Optional;
27 28
28 import static com.google.common.base.Preconditions.checkArgument; 29 import static com.google.common.base.Preconditions.checkArgument;
...@@ -102,6 +103,28 @@ public final class DefaultNextObjective implements NextObjective { ...@@ -102,6 +103,28 @@ public final class DefaultNextObjective implements NextObjective {
102 return meta; 103 return meta;
103 } 104 }
104 105
106 + @Override
107 + public int hashCode() {
108 + return Objects.hash(treatments, appId, type, id, op, meta);
109 + }
110 +
111 + @Override
112 + public boolean equals(Object obj) {
113 + if (this == obj) {
114 + return true;
115 + }
116 + if (obj instanceof DefaultNextObjective) {
117 + final DefaultNextObjective other = (DefaultNextObjective) obj;
118 + return Objects.equals(this.treatments, other.treatments)
119 + && Objects.equals(this.appId, other.appId)
120 + && Objects.equals(this.type, other.type)
121 + && Objects.equals(this.id, other.id)
122 + && Objects.equals(this.op, other.op)
123 + && Objects.equals(this.meta, other.meta);
124 + }
125 + return false;
126 + }
127 +
105 /** 128 /**
106 * Returns a new builder. 129 * Returns a new builder.
107 * 130 *
...@@ -111,6 +134,11 @@ public final class DefaultNextObjective implements NextObjective { ...@@ -111,6 +134,11 @@ public final class DefaultNextObjective implements NextObjective {
111 return new Builder(); 134 return new Builder();
112 } 135 }
113 136
137 + @Override
138 + public Builder copy() {
139 + return new Builder(this);
140 + }
141 +
114 public static final class Builder implements NextObjective.Builder { 142 public static final class Builder implements NextObjective.Builder {
115 143
116 private ApplicationId appId; 144 private ApplicationId appId;
...@@ -124,6 +152,20 @@ public final class DefaultNextObjective implements NextObjective { ...@@ -124,6 +152,20 @@ public final class DefaultNextObjective implements NextObjective {
124 private final ImmutableList.Builder<TrafficTreatment> listBuilder 152 private final ImmutableList.Builder<TrafficTreatment> listBuilder
125 = ImmutableList.builder(); 153 = ImmutableList.builder();
126 154
155 + // Creates an empty builder
156 + private Builder() {
157 + }
158 +
159 + // Creates a builder set to create a copy of the specified objective.
160 + private Builder(NextObjective objective) {
161 + this.type = objective.type();
162 + this.id = objective.id();
163 + this.treatments = ImmutableList.copyOf(objective.next());
164 + this.meta = objective.meta();
165 + this.appId = objective.appId();
166 + this.op = objective.op();
167 + }
168 +
127 @Override 169 @Override
128 public Builder withId(int nextId) { 170 public Builder withId(int nextId) {
129 this.id = nextId; 171 this.id = nextId;
......
...@@ -149,6 +149,7 @@ public interface FilteringObjective extends Objective { ...@@ -149,6 +149,7 @@ public interface FilteringObjective extends Objective {
149 * 149 *
150 * @return a filtering objective 150 * @return a filtering objective
151 */ 151 */
152 + @Override
152 FilteringObjective add(); 153 FilteringObjective add();
153 154
154 /** 155 /**
...@@ -156,6 +157,7 @@ public interface FilteringObjective extends Objective { ...@@ -156,6 +157,7 @@ public interface FilteringObjective extends Objective {
156 * 157 *
157 * @return a filtering objective. 158 * @return a filtering objective.
158 */ 159 */
160 + @Override
159 FilteringObjective remove(); 161 FilteringObjective remove();
160 162
161 /** 163 /**
...@@ -165,6 +167,7 @@ public interface FilteringObjective extends Objective { ...@@ -165,6 +167,7 @@ public interface FilteringObjective extends Objective {
165 * @param context an objective context 167 * @param context an objective context
166 * @return a filtering objective 168 * @return a filtering objective
167 */ 169 */
170 + @Override
168 FilteringObjective add(ObjectiveContext context); 171 FilteringObjective add(ObjectiveContext context);
169 172
170 /** 173 /**
...@@ -174,6 +177,7 @@ public interface FilteringObjective extends Objective { ...@@ -174,6 +177,7 @@ public interface FilteringObjective extends Objective {
174 * @param context an objective context 177 * @param context an objective context
175 * @return a filtering objective 178 * @return a filtering objective
176 */ 179 */
180 + @Override
177 FilteringObjective remove(ObjectiveContext context); 181 FilteringObjective remove(ObjectiveContext context);
178 182
179 183
......
...@@ -28,7 +28,7 @@ public interface FlowObjectiveService { ...@@ -28,7 +28,7 @@ public interface FlowObjectiveService {
28 /** 28 /**
29 * Installs the filtering rules onto the specified device. 29 * Installs the filtering rules onto the specified device.
30 * 30 *
31 - * @param deviceId device identifier 31 + * @param deviceId device identifier
32 * @param filteringObjective the filtering objective 32 * @param filteringObjective the filtering objective
33 */ 33 */
34 void filter(DeviceId deviceId, FilteringObjective filteringObjective); 34 void filter(DeviceId deviceId, FilteringObjective filteringObjective);
...@@ -36,7 +36,7 @@ public interface FlowObjectiveService { ...@@ -36,7 +36,7 @@ public interface FlowObjectiveService {
36 /** 36 /**
37 * Installs the forwarding rules onto the specified device. 37 * Installs the forwarding rules onto the specified device.
38 * 38 *
39 - * @param deviceId device identifier 39 + * @param deviceId device identifier
40 * @param forwardingObjective the forwarding objective 40 * @param forwardingObjective the forwarding objective
41 */ 41 */
42 void forward(DeviceId deviceId, ForwardingObjective forwardingObjective); 42 void forward(DeviceId deviceId, ForwardingObjective forwardingObjective);
...@@ -44,7 +44,7 @@ public interface FlowObjectiveService { ...@@ -44,7 +44,7 @@ public interface FlowObjectiveService {
44 /** 44 /**
45 * Installs the next hop elements into the specified device. 45 * Installs the next hop elements into the specified device.
46 * 46 *
47 - * @param deviceId device identifier 47 + * @param deviceId device identifier
48 * @param nextObjective a next objective 48 * @param nextObjective a next objective
49 */ 49 */
50 void next(DeviceId deviceId, NextObjective nextObjective); 50 void next(DeviceId deviceId, NextObjective nextObjective);
...@@ -59,7 +59,25 @@ public interface FlowObjectiveService { ...@@ -59,7 +59,25 @@ public interface FlowObjectiveService {
59 /** 59 /**
60 * Installs the filtering rules onto the specified device. 60 * Installs the filtering rules onto the specified device.
61 * 61 *
62 - * @param policy policy expression 62 + * @param policy policy expression
63 */ 63 */
64 void initPolicy(String policy); 64 void initPolicy(String policy);
65 +
66 + /**
67 + * Installs the objective onto the specified device.
68 + *
69 + * @param deviceId device identifier
70 + * @param objective the objective
71 + */
72 + default void apply(DeviceId deviceId, Objective objective) {
73 + if (ForwardingObjective.class.isAssignableFrom(objective.getClass())) {
74 + forward(deviceId, (ForwardingObjective) objective);
75 + } else if (FilteringObjective.class.isAssignableFrom(objective.getClass())) {
76 + filter(deviceId, (FilteringObjective) objective);
77 + } else if (NextObjective.class.isAssignableFrom(objective.getClass())) {
78 + next(deviceId, (NextObjective) objective);
79 + } else {
80 + throw new UnsupportedOperationException("Unsupported objective of type " + objective.getClass());
81 + }
82 + }
65 } 83 }
......
...@@ -128,6 +128,7 @@ public interface ForwardingObjective extends Objective { ...@@ -128,6 +128,7 @@ public interface ForwardingObjective extends Objective {
128 * 128 *
129 * @return a forwarding objective 129 * @return a forwarding objective
130 */ 130 */
131 + @Override
131 ForwardingObjective add(); 132 ForwardingObjective add();
132 133
133 /** 134 /**
...@@ -135,6 +136,7 @@ public interface ForwardingObjective extends Objective { ...@@ -135,6 +136,7 @@ public interface ForwardingObjective extends Objective {
135 * 136 *
136 * @return a forwarding objective. 137 * @return a forwarding objective.
137 */ 138 */
139 + @Override
138 ForwardingObjective remove(); 140 ForwardingObjective remove();
139 141
140 /** 142 /**
...@@ -144,6 +146,7 @@ public interface ForwardingObjective extends Objective { ...@@ -144,6 +146,7 @@ public interface ForwardingObjective extends Objective {
144 * @param context an objective context 146 * @param context an objective context
145 * @return a forwarding objective 147 * @return a forwarding objective
146 */ 148 */
149 + @Override
147 ForwardingObjective add(ObjectiveContext context); 150 ForwardingObjective add(ObjectiveContext context);
148 151
149 /** 152 /**
...@@ -153,6 +156,7 @@ public interface ForwardingObjective extends Objective { ...@@ -153,6 +156,7 @@ public interface ForwardingObjective extends Objective {
153 * @param context an objective context 156 * @param context an objective context
154 * @return a forwarding objective 157 * @return a forwarding objective
155 */ 158 */
159 + @Override
156 ForwardingObjective remove(ObjectiveContext context); 160 ForwardingObjective remove(ObjectiveContext context);
157 } 161 }
158 } 162 }
......
...@@ -154,6 +154,7 @@ public interface NextObjective extends Objective { ...@@ -154,6 +154,7 @@ public interface NextObjective extends Objective {
154 * 154 *
155 * @return a next objective 155 * @return a next objective
156 */ 156 */
157 + @Override
157 NextObjective add(); 158 NextObjective add();
158 159
159 /** 160 /**
...@@ -161,6 +162,7 @@ public interface NextObjective extends Objective { ...@@ -161,6 +162,7 @@ public interface NextObjective extends Objective {
161 * 162 *
162 * @return a next objective. 163 * @return a next objective.
163 */ 164 */
165 + @Override
164 NextObjective remove(); 166 NextObjective remove();
165 167
166 /** 168 /**
...@@ -170,6 +172,7 @@ public interface NextObjective extends Objective { ...@@ -170,6 +172,7 @@ public interface NextObjective extends Objective {
170 * @param context an objective context 172 * @param context an objective context
171 * @return a next objective 173 * @return a next objective
172 */ 174 */
175 + @Override
173 NextObjective add(ObjectiveContext context); 176 NextObjective add(ObjectiveContext context);
174 177
175 /** 178 /**
...@@ -179,6 +182,7 @@ public interface NextObjective extends Objective { ...@@ -179,6 +182,7 @@ public interface NextObjective extends Objective {
179 * @param context an objective context 182 * @param context an objective context
180 * @return a next objective 183 * @return a next objective
181 */ 184 */
185 + @Override
182 NextObjective remove(ObjectiveContext context); 186 NextObjective remove(ObjectiveContext context);
183 187
184 /** 188 /**
......
...@@ -112,6 +112,13 @@ public interface Objective { ...@@ -112,6 +112,13 @@ public interface Objective {
112 Optional<ObjectiveContext> context(); 112 Optional<ObjectiveContext> context();
113 113
114 /** 114 /**
115 + * Returns a new builder set to create a copy of this objective.
116 + *
117 + * @return new builder
118 + */
119 + Objective.Builder copy();
120 +
121 + /**
115 * An objective builder. 122 * An objective builder.
116 */ 123 */
117 interface Builder { 124 interface Builder {
...@@ -146,6 +153,36 @@ public interface Objective { ...@@ -146,6 +153,36 @@ public interface Objective {
146 */ 153 */
147 Builder withPriority(int priority); 154 Builder withPriority(int priority);
148 155
149 - } 156 + /**
157 + * Builds the objective that will be added.
158 + *
159 + * @return an objective
160 + */
161 + Objective add();
162 +
163 + /**
164 + * Builds the objective that will be removed.
165 + *
166 + * @return an objective.
167 + */
168 + Objective remove();
169 +
170 + /**
171 + * Builds the objective that will be added.
172 + * The context will be used to notify the calling application.
173 + *
174 + * @param context an objective context
175 + * @return an objective
176 + */
177 + Objective add(ObjectiveContext context);
150 178
179 + /**
180 + * Builds the objective that will be removed.
181 + * The context will be used to notify the calling application.
182 + *
183 + * @param context an objective context
184 + * @return an objective
185 + */
186 + Objective remove(ObjectiveContext context);
187 + }
151 } 188 }
......