adding support of WCMP weights to Next Objective
Change-Id: Id8f9a4222d0d9d98995f727dbfbf467ab2104468
diff --git a/core/api/src/main/java/org/onosproject/net/flowobjective/DefaultNextTreatment.java b/core/api/src/main/java/org/onosproject/net/flowobjective/DefaultNextTreatment.java
index 4387b56..554be28 100644
--- a/core/api/src/main/java/org/onosproject/net/flowobjective/DefaultNextTreatment.java
+++ b/core/api/src/main/java/org/onosproject/net/flowobjective/DefaultNextTreatment.java
@@ -22,13 +22,15 @@
import static com.google.common.base.MoreObjects.toStringHelper;
/**
- * Represents a next action specified by traffic treatment.
+ * Represents a next action specified by traffic treatment and weight.
*/
public final class DefaultNextTreatment implements NextTreatment {
private final TrafficTreatment treatment;
+ private final int weight;
- private DefaultNextTreatment(TrafficTreatment treatment) {
+ private DefaultNextTreatment(TrafficTreatment treatment, int weight) {
this.treatment = treatment;
+ this.weight = weight;
}
/**
@@ -47,7 +49,23 @@
* @return an instance of DefaultNextTreatment
*/
public static DefaultNextTreatment of(TrafficTreatment treatment) {
- return new DefaultNextTreatment(treatment);
+ return new DefaultNextTreatment(treatment, DEFAULT_WEIGHT);
+ }
+
+ /**
+ * Returns an instance of DefaultNextTreatment with given traffic treatment and weight.
+ *
+ * @param treatment traffic treatment
+ * @param weight the weight of next treatment
+ * @return an instance of DefaultNextTreatment
+ */
+ public static DefaultNextTreatment of(TrafficTreatment treatment, int weight) {
+ return new DefaultNextTreatment(treatment, weight);
+ }
+
+ @Override
+ public int weight() {
+ return weight;
}
@Override
@@ -57,7 +75,7 @@
@Override
public int hashCode() {
- return Objects.hash(treatment);
+ return Objects.hash(treatment, weight);
}
@Override
@@ -67,7 +85,7 @@
}
if (obj instanceof DefaultNextTreatment) {
final DefaultNextTreatment other = (DefaultNextTreatment) obj;
- return Objects.equals(this.treatment, other.treatment);
+ return Objects.equals(this.treatment, other.treatment) && Objects.equals(this.weight, other.weight);
}
return false;
}
@@ -76,6 +94,7 @@
public String toString() {
return toStringHelper(this)
.add("treatment", treatment)
+ .add("weight", weight)
.toString();
}
}
diff --git a/core/api/src/main/java/org/onosproject/net/flowobjective/IdNextTreatment.java b/core/api/src/main/java/org/onosproject/net/flowobjective/IdNextTreatment.java
index 98a8928..8b47361 100644
--- a/core/api/src/main/java/org/onosproject/net/flowobjective/IdNextTreatment.java
+++ b/core/api/src/main/java/org/onosproject/net/flowobjective/IdNextTreatment.java
@@ -24,14 +24,17 @@
*/
public final class IdNextTreatment implements NextTreatment {
private final int nextId;
+ private final int weight;
/**
* Constructs IdNextTreatment.
*
* @param nextId next id
+ * @param weight weight
*/
- private IdNextTreatment(int nextId) {
+ private IdNextTreatment(int nextId, int weight) {
this.nextId = nextId;
+ this.weight = weight;
}
/**
@@ -50,7 +53,22 @@
* @return an instance of IdNextTreatment
*/
public static IdNextTreatment of(int nextId) {
- return new IdNextTreatment(nextId);
+ return new IdNextTreatment(nextId, DEFAULT_WEIGHT);
+ }
+ /**
+ * Returns an instance of IdNextTreatment with given next id and weight.
+ *
+ * @param nextId next id
+ * @param weight weight
+ * @return an instance of IdNextTreatment
+ */
+ public static IdNextTreatment of(int nextId, int weight) {
+ return new IdNextTreatment(nextId, weight);
+ }
+
+ @Override
+ public int weight() {
+ return weight;
}
@Override
@@ -59,7 +77,7 @@
}
@Override
public int hashCode() {
- return Objects.hash(nextId);
+ return Objects.hash(nextId, weight);
}
@Override
@@ -69,7 +87,7 @@
}
if (obj instanceof IdNextTreatment) {
final IdNextTreatment other = (IdNextTreatment) obj;
- return this.nextId == other.nextId;
+ return this.nextId == other.nextId && this.weight == other.weight;
}
return false;
}
@@ -78,6 +96,7 @@
public String toString() {
return toStringHelper(this)
.add("nextId", nextId)
+ .add("weight", weight)
.toString();
}
}
diff --git a/core/api/src/main/java/org/onosproject/net/flowobjective/NextTreatment.java b/core/api/src/main/java/org/onosproject/net/flowobjective/NextTreatment.java
index af849a2..cdeff98 100644
--- a/core/api/src/main/java/org/onosproject/net/flowobjective/NextTreatment.java
+++ b/core/api/src/main/java/org/onosproject/net/flowobjective/NextTreatment.java
@@ -19,6 +19,7 @@
* Represents next action in the NextObjective.
*/
public interface NextTreatment {
+ int DEFAULT_WEIGHT = 1;
/**
* Types of next action.
*/
@@ -40,4 +41,10 @@
* @return type
*/
Type type();
+ /**
+ * weight of this next action.
+ *
+ * @return weight
+ */
+ int weight();
}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/NextObjectiveCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/NextObjectiveCodec.java
index 3cb5178..1dd3399 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/NextObjectiveCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/NextObjectiveCodec.java
@@ -25,6 +25,8 @@
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flowobjective.DefaultNextObjective;
import org.onosproject.net.flowobjective.NextObjective;
+import org.onosproject.net.flowobjective.NextTreatment;
+import org.onosproject.net.flowobjective.DefaultNextTreatment;
import org.slf4j.Logger;
import java.util.stream.IntStream;
@@ -47,6 +49,7 @@
private static final String OPERATION = "operation";
private static final String TREATMENTS = "treatments";
private static final String META = "meta";
+ private static final String WEIGHT = "weight";
// messages to be printed out
private static final String MISSING_MEMBER_MESSAGE =
@@ -79,9 +82,13 @@
// encode treatments
ArrayNode treatments = context.mapper().createArrayNode();
- nextObjective.next().forEach(t -> {
- ObjectNode treatmentJson = trafficTreatmentCodec.encode(t, context);
- treatments.add(treatmentJson);
+ nextObjective.nextTreatments().forEach(nt -> {
+ if (nt.type().equals(NextTreatment.Type.TREATMENT)) {
+ TrafficTreatment tt = ((DefaultNextTreatment) nt).treatment();
+ ObjectNode treatmentJson = trafficTreatmentCodec.encode(tt, context);
+ treatmentJson.put(WEIGHT, nt.weight());
+ treatments.add(treatmentJson);
+ }
});
result.set(TREATMENTS, treatments);
@@ -148,7 +155,10 @@
if (treatmentsJson != null) {
IntStream.range(0, treatmentsJson.size()).forEach(i -> {
ObjectNode treatmentJson = get(treatmentsJson, i);
- builder.addTreatment(trafficTreatmentCodec.decode(treatmentJson, context));
+ JsonNode weightJson = treatmentJson.get(WEIGHT);
+ int weight = (weightJson != null) ? weightJson.asInt() : NextTreatment.DEFAULT_WEIGHT;
+ builder.addTreatment(DefaultNextTreatment.of(
+ trafficTreatmentCodec.decode(treatmentJson, context), weight));
});
}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveCodecTest.java
index 355209f..fb4f1e8 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveCodecTest.java
@@ -26,7 +26,9 @@
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flowobjective.DefaultNextObjective;
+import org.onosproject.net.flowobjective.DefaultNextTreatment;
import org.onosproject.net.flowobjective.NextObjective;
+import org.onosproject.net.flowobjective.NextTreatment;
import java.io.IOException;
import java.io.InputStream;
@@ -70,6 +72,7 @@
public void testNextObjectiveEncode() {
TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
+ NextTreatment nextTreatment = DefaultNextTreatment.of(treatment, 5);
NextObjective nextObj = DefaultNextObjective.builder()
.makePermanent()
@@ -77,7 +80,7 @@
.fromApp(APP_ID)
.withPriority(60)
.withId(5)
- .addTreatment(treatment)
+ .addTreatment(nextTreatment)
.add();
ObjectNode nextObjJson = nextObjectiveCodec.encode(nextObj, context);
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveJsonMatcher.java
index bb86771..cdf4da9 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveJsonMatcher.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveJsonMatcher.java
@@ -16,9 +16,13 @@
package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onosproject.net.flowobjective.NextObjective;
+import org.onosproject.net.flowobjective.NextTreatment;
+import java.util.ArrayList;
+import java.util.List;
/**
* Hamcrest matcher for nextObjective.
@@ -58,6 +62,22 @@
return false;
}
+ // check the weight
+ boolean result = true;
+ List<NextTreatment> nt = new ArrayList(nextObjective.nextTreatments());
+ for (int i = 0; i < jsonTreatments.size(); i++) {
+ ObjectNode jsonTreatment = jsonTreatments.path(i).isObject() &&
+ !jsonTreatments.path(i).isNull() ? (ObjectNode) jsonTreatments.path(i) : null;
+ int jsonWeight = jsonTreatment.get("weight").asInt();
+ if (jsonWeight != nt.get(i).weight()) {
+ description.appendText("weight of NextTreatment with index " + i + " was " + jsonWeight);
+ result = false;
+ }
+ }
+ if (!result) {
+ return false;
+ }
+
// TODO: need to check the content of treatment collection
// TODO: need to check the content of selector instance