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;
@Beta
public final class DefaultFilteringObjective implements FilteringObjective {
private final Type type;
private final boolean permanent;
private final int timeout;
......@@ -121,6 +120,33 @@ public final class DefaultFilteringObjective implements FilteringObjective {
return context;
}
@Override
public int hashCode() {
return Objects.hash(type, permanent, timeout, appId, priority, key,
conditions, op, meta);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof DefaultFilteringObjective) {
final DefaultFilteringObjective other = (DefaultFilteringObjective) obj;
return Objects.equals(this.type, other.type)
&& Objects.equals(this.permanent, other.permanent)
&& Objects.equals(this.timeout, other.timeout)
&& Objects.equals(this.appId, other.appId)
&& Objects.equals(this.priority, other.priority)
&& Objects.equals(this.key, other.key)
&& Objects.equals(this.conditions, other.conditions)
&& Objects.equals(this.op, other.op)
&& Objects.equals(this.meta, other.meta);
}
return false;
}
/**
* Returns a new builder.
*
......@@ -130,6 +156,10 @@ public final class DefaultFilteringObjective implements FilteringObjective {
return new Builder();
}
@Override
public Builder copy() {
return new Builder(this);
}
public static final class Builder implements FilteringObjective.Builder {
private final ImmutableList.Builder<Criterion> listBuilder
......@@ -146,6 +176,23 @@ public final class DefaultFilteringObjective implements FilteringObjective {
private ObjectiveContext context;
private TrafficTreatment meta;
// Creates an empty builder
private Builder() {
}
// Creates a builder set to create a copy of the specified objective.
private Builder(FilteringObjective objective) {
this.type = objective.type();
this.key = objective.key();
this.conditions = ImmutableList.copyOf(objective.conditions());
this.permanent = objective.permanent();
this.timeout = objective.timeout();
this.priority = objective.priority();
this.appId = objective.appId();
this.meta = objective.meta();
this.op = objective.op();
}
@Override
public Builder withKey(Criterion key) {
this.key = key;
......@@ -210,7 +257,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
checkNotNull(appId, "Must supply an application id");
return new DefaultFilteringObjective(this);
}
@Override
......@@ -222,7 +268,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
op = Operation.REMOVE;
return new DefaultFilteringObjective(this);
}
@Override
......@@ -249,7 +294,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
return new DefaultFilteringObjective(this);
}
}
}
......
......@@ -120,49 +120,28 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
return context;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hash(selector, flag, permanent,
timeout, appId, priority, nextId,
treatment, op);
return Objects.hash(selector, flag, permanent, timeout, appId,
priority, nextId, treatment, op);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof DefaultForwardingObjective)) {
return false;
}
if (obj instanceof DefaultForwardingObjective) {
final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
boolean nextEq = false, treatmentEq = false;
if (this.selector.equals(other.selector) &&
this.flag == other.flag &&
this.permanent == other.permanent &&
this.timeout == other.timeout &&
this.appId.equals(other.appId) &&
this.priority == other.priority &&
this.op == other.op) {
if (this.nextId != null && other.nextId != null) {
nextEq = this.nextId == other.nextId;
}
if (this.treatment != null && other.treatment != null) {
treatmentEq = this.treatment.equals(other.treatment);
}
if (nextEq && treatmentEq) {
return true;
}
return Objects.equals(this.selector, other.selector)
&& Objects.equals(this.flag, other.flag)
&& Objects.equals(this.permanent, other.permanent)
&& Objects.equals(this.timeout, other.timeout)
&& Objects.equals(this.appId, other.appId)
&& Objects.equals(this.priority, other.priority)
&& Objects.equals(this.nextId, other.nextId)
&& Objects.equals(this.treatment, other.treatment)
&& Objects.equals(this.op, other.op);
}
return false;
}
......@@ -176,6 +155,13 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
return new Builder();
}
@Override
public Builder copy() {
return new Builder(this);
}
public static final class Builder implements ForwardingObjective.Builder {
private TrafficSelector selector;
......@@ -189,6 +175,23 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
private Operation op;
private ObjectiveContext context;
// Creates an empty builder
private Builder() {
}
// Creates a builder set to create a copy of the specified objective.
private Builder(ForwardingObjective objective) {
this.selector = objective.selector();
this.flag = objective.flag();
this.permanent = objective.permanent();
this.timeout = objective.timeout();
this.priority = objective.priority();
this.appId = objective.appId();
this.nextId = objective.nextId();
this.treatment = objective.treatment();
this.op = objective.op();
}
@Override
public Builder withSelector(TrafficSelector selector) {
this.selector = selector;
......@@ -286,4 +289,5 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
return new DefaultForwardingObjective(this);
}
}
}
......
......@@ -23,6 +23,7 @@ import org.onosproject.net.flow.TrafficTreatment;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
......@@ -102,6 +103,28 @@ public final class DefaultNextObjective implements NextObjective {
return meta;
}
@Override
public int hashCode() {
return Objects.hash(treatments, appId, type, id, op, meta);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof DefaultNextObjective) {
final DefaultNextObjective other = (DefaultNextObjective) obj;
return Objects.equals(this.treatments, other.treatments)
&& Objects.equals(this.appId, other.appId)
&& Objects.equals(this.type, other.type)
&& Objects.equals(this.id, other.id)
&& Objects.equals(this.op, other.op)
&& Objects.equals(this.meta, other.meta);
}
return false;
}
/**
* Returns a new builder.
*
......@@ -111,6 +134,11 @@ public final class DefaultNextObjective implements NextObjective {
return new Builder();
}
@Override
public Builder copy() {
return new Builder(this);
}
public static final class Builder implements NextObjective.Builder {
private ApplicationId appId;
......@@ -124,6 +152,20 @@ public final class DefaultNextObjective implements NextObjective {
private final ImmutableList.Builder<TrafficTreatment> listBuilder
= ImmutableList.builder();
// Creates an empty builder
private Builder() {
}
// Creates a builder set to create a copy of the specified objective.
private Builder(NextObjective objective) {
this.type = objective.type();
this.id = objective.id();
this.treatments = ImmutableList.copyOf(objective.next());
this.meta = objective.meta();
this.appId = objective.appId();
this.op = objective.op();
}
@Override
public Builder withId(int nextId) {
this.id = nextId;
......
......@@ -149,6 +149,7 @@ public interface FilteringObjective extends Objective {
*
* @return a filtering objective
*/
@Override
FilteringObjective add();
/**
......@@ -156,6 +157,7 @@ public interface FilteringObjective extends Objective {
*
* @return a filtering objective.
*/
@Override
FilteringObjective remove();
/**
......@@ -165,6 +167,7 @@ public interface FilteringObjective extends Objective {
* @param context an objective context
* @return a filtering objective
*/
@Override
FilteringObjective add(ObjectiveContext context);
/**
......@@ -174,6 +177,7 @@ public interface FilteringObjective extends Objective {
* @param context an objective context
* @return a filtering objective
*/
@Override
FilteringObjective remove(ObjectiveContext context);
......
......@@ -62,4 +62,22 @@ public interface FlowObjectiveService {
* @param policy policy expression
*/
void initPolicy(String policy);
/**
* Installs the objective onto the specified device.
*
* @param deviceId device identifier
* @param objective the objective
*/
default void apply(DeviceId deviceId, Objective objective) {
if (ForwardingObjective.class.isAssignableFrom(objective.getClass())) {
forward(deviceId, (ForwardingObjective) objective);
} else if (FilteringObjective.class.isAssignableFrom(objective.getClass())) {
filter(deviceId, (FilteringObjective) objective);
} else if (NextObjective.class.isAssignableFrom(objective.getClass())) {
next(deviceId, (NextObjective) objective);
} else {
throw new UnsupportedOperationException("Unsupported objective of type " + objective.getClass());
}
}
}
......
......@@ -128,6 +128,7 @@ public interface ForwardingObjective extends Objective {
*
* @return a forwarding objective
*/
@Override
ForwardingObjective add();
/**
......@@ -135,6 +136,7 @@ public interface ForwardingObjective extends Objective {
*
* @return a forwarding objective.
*/
@Override
ForwardingObjective remove();
/**
......@@ -144,6 +146,7 @@ public interface ForwardingObjective extends Objective {
* @param context an objective context
* @return a forwarding objective
*/
@Override
ForwardingObjective add(ObjectiveContext context);
/**
......@@ -153,6 +156,7 @@ public interface ForwardingObjective extends Objective {
* @param context an objective context
* @return a forwarding objective
*/
@Override
ForwardingObjective remove(ObjectiveContext context);
}
}
......
......@@ -154,6 +154,7 @@ public interface NextObjective extends Objective {
*
* @return a next objective
*/
@Override
NextObjective add();
/**
......@@ -161,6 +162,7 @@ public interface NextObjective extends Objective {
*
* @return a next objective.
*/
@Override
NextObjective remove();
/**
......@@ -170,6 +172,7 @@ public interface NextObjective extends Objective {
* @param context an objective context
* @return a next objective
*/
@Override
NextObjective add(ObjectiveContext context);
/**
......@@ -179,6 +182,7 @@ public interface NextObjective extends Objective {
* @param context an objective context
* @return a next objective
*/
@Override
NextObjective remove(ObjectiveContext context);
/**
......
......@@ -112,6 +112,13 @@ public interface Objective {
Optional<ObjectiveContext> context();
/**
* Returns a new builder set to create a copy of this objective.
*
* @return new builder
*/
Objective.Builder copy();
/**
* An objective builder.
*/
interface Builder {
......@@ -146,6 +153,36 @@ public interface Objective {
*/
Builder withPriority(int priority);
}
/**
* Builds the objective that will be added.
*
* @return an objective
*/
Objective add();
/**
* Builds the objective that will be removed.
*
* @return an objective.
*/
Objective remove();
/**
* Builds the objective that will be added.
* The context will be used to notify the calling application.
*
* @param context an objective context
* @return an objective
*/
Objective add(ObjectiveContext context);
/**
* Builds the objective that will be removed.
* The context will be used to notify the calling application.
*
* @param context an objective context
* @return an objective
*/
Objective remove(ObjectiveContext context);
}
}
......