Adding copy builders for flow objectives.

Adding missing hashCode and equals methods.

Change-Id: I97b2d904eacf0c45a95905c0891dbc6465e18ec6
This commit is contained in:
Thomas Vachuska 2016-02-29 17:07:23 -08:00 committed by Gerrit Code Review
parent b6d998ecc7
commit 00f481629a
8 changed files with 202 additions and 45 deletions

View File

@ -36,7 +36,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Beta @Beta
public final class DefaultFilteringObjective implements FilteringObjective { public final class DefaultFilteringObjective implements FilteringObjective {
private final Type type; private final Type type;
private final boolean permanent; private final boolean permanent;
private final int timeout; private final int timeout;
@ -62,7 +61,7 @@ public final class DefaultFilteringObjective implements FilteringObjective {
this.meta = builder.meta; this.meta = builder.meta;
this.id = Objects.hash(type, key, conditions, permanent, this.id = Objects.hash(type, key, conditions, permanent,
timeout, appId, priority); timeout, appId, priority);
} }
@Override @Override
@ -121,6 +120,33 @@ public final class DefaultFilteringObjective implements FilteringObjective {
return context; 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. * Returns a new builder.
* *
@ -130,6 +156,10 @@ public final class DefaultFilteringObjective implements FilteringObjective {
return new Builder(); return new Builder();
} }
@Override
public Builder copy() {
return new Builder(this);
}
public static final class Builder implements FilteringObjective.Builder { public static final class Builder implements FilteringObjective.Builder {
private final ImmutableList.Builder<Criterion> listBuilder private final ImmutableList.Builder<Criterion> listBuilder
@ -146,6 +176,23 @@ public final class DefaultFilteringObjective implements FilteringObjective {
private ObjectiveContext context; private ObjectiveContext context;
private TrafficTreatment meta; 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 @Override
public Builder withKey(Criterion key) { public Builder withKey(Criterion key) {
this.key = key; this.key = key;
@ -210,7 +257,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
checkNotNull(appId, "Must supply an application id"); checkNotNull(appId, "Must supply an application id");
return new DefaultFilteringObjective(this); return new DefaultFilteringObjective(this);
} }
@Override @Override
@ -222,7 +268,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
op = Operation.REMOVE; op = Operation.REMOVE;
return new DefaultFilteringObjective(this); return new DefaultFilteringObjective(this);
} }
@Override @Override
@ -249,7 +294,6 @@ public final class DefaultFilteringObjective implements FilteringObjective {
return new DefaultFilteringObjective(this); return new DefaultFilteringObjective(this);
} }
} }
} }

View File

@ -120,49 +120,28 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
return context; return context;
} }
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(selector, flag, permanent, return Objects.hash(selector, flag, permanent, timeout, appId,
timeout, appId, priority, nextId, priority, nextId, treatment, op);
treatment, op);
} }
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override @Override
public boolean equals(final Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (!(obj instanceof DefaultForwardingObjective)) { if (obj instanceof DefaultForwardingObjective) {
return false; final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
} return Objects.equals(this.selector, other.selector)
final DefaultForwardingObjective other = (DefaultForwardingObjective) obj; && Objects.equals(this.flag, other.flag)
boolean nextEq = false, treatmentEq = false; && Objects.equals(this.permanent, other.permanent)
if (this.selector.equals(other.selector) && && Objects.equals(this.timeout, other.timeout)
this.flag == other.flag && && Objects.equals(this.appId, other.appId)
this.permanent == other.permanent && && Objects.equals(this.priority, other.priority)
this.timeout == other.timeout && && Objects.equals(this.nextId, other.nextId)
this.appId.equals(other.appId) && && Objects.equals(this.treatment, other.treatment)
this.priority == other.priority && && Objects.equals(this.op, other.op);
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 false; return false;
} }
@ -176,6 +155,13 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
return new Builder(); return new Builder();
} }
@Override
public Builder copy() {
return new Builder(this);
}
public static final class Builder implements ForwardingObjective.Builder { public static final class Builder implements ForwardingObjective.Builder {
private TrafficSelector selector; private TrafficSelector selector;
@ -189,6 +175,23 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
private Operation op; private Operation op;
private ObjectiveContext context; 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 @Override
public Builder withSelector(TrafficSelector selector) { public Builder withSelector(TrafficSelector selector) {
this.selector = selector; this.selector = selector;
@ -286,4 +289,5 @@ public final class DefaultForwardingObjective implements ForwardingObjective {
return new DefaultForwardingObjective(this); return new DefaultForwardingObjective(this);
} }
} }
} }

View File

@ -23,6 +23,7 @@ import org.onosproject.net.flow.TrafficTreatment;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
@ -102,6 +103,28 @@ public final class DefaultNextObjective implements NextObjective {
return meta; 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. * Returns a new builder.
* *
@ -111,6 +134,11 @@ public final class DefaultNextObjective implements NextObjective {
return new Builder(); return new Builder();
} }
@Override
public Builder copy() {
return new Builder(this);
}
public static final class Builder implements NextObjective.Builder { public static final class Builder implements NextObjective.Builder {
private ApplicationId appId; private ApplicationId appId;
@ -124,6 +152,20 @@ public final class DefaultNextObjective implements NextObjective {
private final ImmutableList.Builder<TrafficTreatment> listBuilder private final ImmutableList.Builder<TrafficTreatment> listBuilder
= ImmutableList.builder(); = 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 @Override
public Builder withId(int nextId) { public Builder withId(int nextId) {
this.id = nextId; this.id = nextId;

View File

@ -149,6 +149,7 @@ public interface FilteringObjective extends Objective {
* *
* @return a filtering objective * @return a filtering objective
*/ */
@Override
FilteringObjective add(); FilteringObjective add();
/** /**
@ -156,6 +157,7 @@ public interface FilteringObjective extends Objective {
* *
* @return a filtering objective. * @return a filtering objective.
*/ */
@Override
FilteringObjective remove(); FilteringObjective remove();
/** /**
@ -165,6 +167,7 @@ public interface FilteringObjective extends Objective {
* @param context an objective context * @param context an objective context
* @return a filtering objective * @return a filtering objective
*/ */
@Override
FilteringObjective add(ObjectiveContext context); FilteringObjective add(ObjectiveContext context);
/** /**
@ -174,6 +177,7 @@ public interface FilteringObjective extends Objective {
* @param context an objective context * @param context an objective context
* @return a filtering objective * @return a filtering objective
*/ */
@Override
FilteringObjective remove(ObjectiveContext context); FilteringObjective remove(ObjectiveContext context);

View File

@ -28,7 +28,7 @@ public interface FlowObjectiveService {
/** /**
* Installs the filtering rules onto the specified device. * Installs the filtering rules onto the specified device.
* *
* @param deviceId device identifier * @param deviceId device identifier
* @param filteringObjective the filtering objective * @param filteringObjective the filtering objective
*/ */
void filter(DeviceId deviceId, FilteringObjective filteringObjective); void filter(DeviceId deviceId, FilteringObjective filteringObjective);
@ -36,7 +36,7 @@ public interface FlowObjectiveService {
/** /**
* Installs the forwarding rules onto the specified device. * Installs the forwarding rules onto the specified device.
* *
* @param deviceId device identifier * @param deviceId device identifier
* @param forwardingObjective the forwarding objective * @param forwardingObjective the forwarding objective
*/ */
void forward(DeviceId deviceId, ForwardingObjective forwardingObjective); void forward(DeviceId deviceId, ForwardingObjective forwardingObjective);
@ -44,7 +44,7 @@ public interface FlowObjectiveService {
/** /**
* Installs the next hop elements into the specified device. * Installs the next hop elements into the specified device.
* *
* @param deviceId device identifier * @param deviceId device identifier
* @param nextObjective a next objective * @param nextObjective a next objective
*/ */
void next(DeviceId deviceId, NextObjective nextObjective); void next(DeviceId deviceId, NextObjective nextObjective);
@ -59,7 +59,25 @@ public interface FlowObjectiveService {
/** /**
* Installs the filtering rules onto the specified device. * Installs the filtering rules onto the specified device.
* *
* @param policy policy expression * @param policy policy expression
*/ */
void initPolicy(String policy); 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());
}
}
} }

View File

@ -128,6 +128,7 @@ public interface ForwardingObjective extends Objective {
* *
* @return a forwarding objective * @return a forwarding objective
*/ */
@Override
ForwardingObjective add(); ForwardingObjective add();
/** /**
@ -135,6 +136,7 @@ public interface ForwardingObjective extends Objective {
* *
* @return a forwarding objective. * @return a forwarding objective.
*/ */
@Override
ForwardingObjective remove(); ForwardingObjective remove();
/** /**
@ -144,6 +146,7 @@ public interface ForwardingObjective extends Objective {
* @param context an objective context * @param context an objective context
* @return a forwarding objective * @return a forwarding objective
*/ */
@Override
ForwardingObjective add(ObjectiveContext context); ForwardingObjective add(ObjectiveContext context);
/** /**
@ -153,6 +156,7 @@ public interface ForwardingObjective extends Objective {
* @param context an objective context * @param context an objective context
* @return a forwarding objective * @return a forwarding objective
*/ */
@Override
ForwardingObjective remove(ObjectiveContext context); ForwardingObjective remove(ObjectiveContext context);
} }
} }

View File

@ -154,6 +154,7 @@ public interface NextObjective extends Objective {
* *
* @return a next objective * @return a next objective
*/ */
@Override
NextObjective add(); NextObjective add();
/** /**
@ -161,6 +162,7 @@ public interface NextObjective extends Objective {
* *
* @return a next objective. * @return a next objective.
*/ */
@Override
NextObjective remove(); NextObjective remove();
/** /**
@ -170,6 +172,7 @@ public interface NextObjective extends Objective {
* @param context an objective context * @param context an objective context
* @return a next objective * @return a next objective
*/ */
@Override
NextObjective add(ObjectiveContext context); NextObjective add(ObjectiveContext context);
/** /**
@ -179,6 +182,7 @@ public interface NextObjective extends Objective {
* @param context an objective context * @param context an objective context
* @return a next objective * @return a next objective
*/ */
@Override
NextObjective remove(ObjectiveContext context); NextObjective remove(ObjectiveContext context);
/** /**

View File

@ -111,6 +111,13 @@ public interface Objective {
*/ */
Optional<ObjectiveContext> context(); Optional<ObjectiveContext> context();
/**
* Returns a new builder set to create a copy of this objective.
*
* @return new builder
*/
Objective.Builder copy();
/** /**
* An objective builder. * An objective builder.
*/ */
@ -146,6 +153,36 @@ public interface Objective {
*/ */
Builder withPriority(int priority); 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);
}
} }