Added TierConstraint and MeteredConstraint
Change-Id: Iadc50c98dc0ba7092313f880df8055ab0e401c29
diff --git a/core/api/src/main/java/org/onosproject/net/intent/constraint/MeteredConstraint.java b/core/api/src/main/java/org/onosproject/net/intent/constraint/MeteredConstraint.java
new file mode 100644
index 0000000..584cd99
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/constraint/MeteredConstraint.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2014-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.constraint;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Link;
+import org.onosproject.net.intent.ResourceContext;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Constraint that evaluates links based on the metered flag.
+ */
+@Beta
+public class MeteredConstraint extends BooleanConstraint {
+
+ private final boolean useMetered;
+
+ /**
+ * Creates a new constraint for requesting connectivity using or avoiding
+ * the metered links.
+ *
+ * @param metered indicates whether a metered link can be used.
+ */
+ public MeteredConstraint(boolean metered) {
+ this.useMetered = metered;
+ }
+
+ // Constructor for serialization
+ private MeteredConstraint() {
+ this.useMetered = false;
+ }
+
+ // doesn't use LinkResourceService
+ @Override
+ public boolean isValid(Link link, ResourceContext context) {
+ // explicitly call a method not depending on LinkResourceService
+ return isValid(link);
+ }
+
+ private boolean isValid(Link link) {
+ return !isMeteredLink(link) || useMetered;
+ }
+
+ private boolean isMeteredLink(Link link) {
+ return link.annotations().keys().contains(AnnotationKeys.METERED)
+ && Boolean.valueOf(link.annotations().value(AnnotationKeys.METERED));
+ }
+
+ /**
+ * Indicates if the constraint is metered or not.
+ *
+ * @return true if metered
+ */
+ public boolean isUseMetered() {
+ return useMetered;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(useMetered);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final MeteredConstraint other = (MeteredConstraint) obj;
+ return Objects.equals(this.useMetered, other.useMetered);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .add("metered", useMetered)
+ .toString();
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/intent/constraint/TierConstraint.java b/core/api/src/main/java/org/onosproject/net/intent/constraint/TierConstraint.java
new file mode 100644
index 0000000..37e5776
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/constraint/TierConstraint.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2014-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.constraint;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Link;
+import org.onosproject.net.intent.ResourceContext;
+
+import java.util.Objects;
+import java.util.List;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Constraint that evaluates links based on their type.
+ */
+@Beta
+public class TierConstraint extends BooleanConstraint {
+
+ public enum CostType {
+ /**
+ * Configures the constraint to return the same cost (1.0) for any
+ * link that has a valid tier value.
+ */
+ VALID,
+ /**
+ * Configures the constraint to return the tier value as the cost.
+ */
+ TIER,
+ /**
+ * Configures the constraint to return the order the tier value was
+ * added to the list of included tiers on the constraint.
+ */
+ ORDER;
+ }
+
+ private final List<Integer> tiers;
+ private final boolean isInclusive;
+ private final CostType costType;
+
+ /**
+ * Creates a new constraint for requesting connectivity using or avoiding
+ * the specified link tiers.
+ *
+ * @param inclusive indicates whether the given link tiers are to be
+ * permitted or avoided
+ * @param costType defines the model used to calculate the link cost.
+ * @param tiers link tiers
+ */
+ public TierConstraint(boolean inclusive, CostType costType, Integer... tiers) {
+ checkNotNull(tiers, "Link tiers cannot be null");
+ checkArgument(tiers.length > 0, "There must at least one tier");
+ if (costType == CostType.ORDER) {
+ checkArgument(inclusive, "Order is only valid when inclusive=true");
+ }
+ this.tiers = ImmutableSet.copyOf(tiers).asList();
+ this.isInclusive = inclusive;
+ this.costType = costType;
+ }
+
+ /**
+ * Creates a new constraint for requesting connectivity using or avoiding
+ * the specified link tiers. The VALID cost type is used by default.
+ *
+ * @param inclusive indicates whether the given link tiers are to be
+ * permitted or avoided
+ * @param tiers link tiers
+ */
+ public TierConstraint(boolean inclusive, Integer... tiers) {
+ this(inclusive, CostType.VALID, tiers);
+ }
+
+ // Constructor for serialization
+ private TierConstraint() {
+ this.tiers = null;
+ this.isInclusive = false;
+ this.costType = CostType.VALID;
+ }
+
+ // doesn't use LinkResourceService
+ @Override
+ public boolean isValid(Link link, ResourceContext context) {
+ // explicitly call a method not depending on LinkResourceService
+ return isValid(link);
+ }
+
+ private boolean isValid(Link link) {
+ boolean contains = link.annotations().keys().contains(AnnotationKeys.TIER)
+ && tiers.contains(Integer.valueOf(
+ link.annotations().value(AnnotationKeys.TIER)));
+ return isInclusive == contains;
+ }
+
+ // doesn't use LinkResourceService
+ @Override
+ public double cost(Link link, ResourceContext context) {
+ // explicitly call a method not depending on LinkResourceService
+ return cost(link);
+ }
+
+ private double cost(Link link) {
+ double cost = -1.0;
+
+ if (isValid(link)) {
+ Integer tier = new Integer(link.annotations().value(AnnotationKeys.TIER));
+ if (costType == CostType.ORDER) {
+ cost = tiers.indexOf(tier) + 1;
+ } else if (costType == CostType.TIER) {
+ cost = tier;
+ } else {
+ cost = 1.0;
+ }
+ }
+
+ return cost;
+ }
+
+ /**
+ * Returns the set of link tiers.
+ *
+ * @return set of link tiers
+ */
+ public List<Integer> tiers() {
+ return tiers;
+ }
+
+ /**
+ * Indicates if the constraint is inclusive or exclusive.
+ *
+ * @return true if inclusive
+ */
+ public boolean isInclusive() {
+ return isInclusive;
+ }
+
+ /**
+ * Return the cost model used by this constraint.
+ *
+ * @return true if inclusive
+ */
+ public CostType costType() {
+ return costType;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(tiers, isInclusive);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final TierConstraint other = (TierConstraint) obj;
+ return Objects.equals(this.tiers, other.tiers) && Objects.equals(this.isInclusive, other.isInclusive)
+ && Objects.equals(this.costType, other.costType);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .add("inclusive", isInclusive)
+ .add("costType", costType)
+ .add("tiers", tiers)
+ .toString();
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/MeteredConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/MeteredConstraintTest.java
new file mode 100644
index 0000000..9ee145d
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/MeteredConstraintTest.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2014-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.constraint;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.graph.ScalarWeight;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultLink;
+import org.onosproject.net.DefaultPath;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.Path;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.intent.ResourceContext;
+import org.onosproject.net.provider.ProviderId;
+
+import java.util.Arrays;
+
+import static org.easymock.EasyMock.createMock;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.net.AnnotationKeys.METERED;
+import static org.onosproject.net.DefaultLinkTest.cp;
+import static org.onosproject.net.DeviceId.deviceId;
+import static org.onosproject.net.Link.Type.DIRECT;
+
+public class MeteredConstraintTest {
+
+ private static final DeviceId DID1 = deviceId("of:1");
+ private static final DeviceId DID2 = deviceId("of:2");
+ private static final DeviceId DID3 = deviceId("of:3");
+ private static final PortNumber PN1 = PortNumber.portNumber(1);
+ private static final PortNumber PN2 = PortNumber.portNumber(2);
+ private static final PortNumber PN3 = PortNumber.portNumber(3);
+ private static final PortNumber PN4 = PortNumber.portNumber(4);
+ private static final PortNumber PN5 = PortNumber.portNumber(5);
+ private static final PortNumber PN6 = PortNumber.portNumber(6);
+ private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
+ private static final String METERED1 = String.valueOf(true);
+ private static final String METERED2 = String.valueOf(false);
+
+ private ResourceContext resourceContext;
+
+ private Path meteredPath;
+ private Path nonMeteredPath;
+ private Link meteredLink;
+ private Link nonMeteredLink;
+ private Link unAnnotatedLink;
+
+ @Before
+ public void setUp() {
+ resourceContext = createMock(ResourceContext.class);
+
+ Annotations annotations1 = DefaultAnnotations.builder().set(METERED, METERED1).build();
+ Annotations annotations2 = DefaultAnnotations.builder().set(METERED, METERED2).build();
+
+ meteredLink = DefaultLink.builder()
+ .providerId(PROVIDER_ID)
+ .src(cp(DID1, PN1))
+ .dst(cp(DID2, PN2))
+ .type(DIRECT)
+ .annotations(annotations1)
+ .build();
+ nonMeteredLink = DefaultLink.builder()
+ .providerId(PROVIDER_ID)
+ .src(cp(DID2, PN3))
+ .dst(cp(DID3, PN4))
+ .type(DIRECT)
+ .annotations(annotations2)
+ .build();
+ unAnnotatedLink = DefaultLink.builder()
+ .providerId(PROVIDER_ID)
+ .src(cp(DID1, PN5))
+ .dst(cp(DID3, PN6))
+ .type(DIRECT)
+ .build();
+ meteredPath = new DefaultPath(PROVIDER_ID, Arrays.asList(meteredLink, nonMeteredLink),
+ ScalarWeight.toWeight(10));
+ nonMeteredPath = new DefaultPath(PROVIDER_ID, Arrays.asList(nonMeteredLink, unAnnotatedLink),
+ ScalarWeight.toWeight(10));
+ }
+
+ /**
+ * Tests the path is valid with a metered link and the supplied constraint.
+ */
+ @Test
+ public void testAllowedOnAllPaths() {
+ MeteredConstraint constraint = new MeteredConstraint(true);
+
+ assertThat(constraint.validate(meteredPath, resourceContext), is(true));
+ assertThat(constraint.validate(nonMeteredPath, resourceContext), is(true));
+ }
+
+ /**
+ * Tests the path is not valid with a metered link and the supplied constraint.
+ */
+ @Test
+ public void tesNotAllowedOntMeteredPath() {
+ MeteredConstraint constraint = new MeteredConstraint(false);
+
+ assertThat(constraint.validate(meteredPath, resourceContext), is(false));
+ }
+
+ /**
+ * Tests the path is not valid with a metered link and the supplied constraint.
+ */
+ @Test
+ public void testNotAllowsOnNonMeteredPath() {
+ MeteredConstraint constraint = new MeteredConstraint(false);
+
+ assertThat(constraint.validate(nonMeteredPath, resourceContext), is(true));
+ }
+
+ /**
+ * Tests that all links are valid for a constraint allowing metered links.
+ */
+ @Test
+ public void testMeteredAllowed() {
+ MeteredConstraint constraint = new MeteredConstraint(true);
+
+ assertThat(constraint.isValid(meteredLink, resourceContext), is(true));
+ assertThat(constraint.isValid(nonMeteredLink, resourceContext), is(true));
+ assertThat(constraint.isValid(unAnnotatedLink, resourceContext), is(true));
+ }
+
+ /**
+ * Tests that only non-metered links are valid for a constraint not allowing metered links.
+ */
+ @Test
+ public void testMeteredNotAllowed() {
+ MeteredConstraint constraint = new MeteredConstraint(false);
+
+ assertThat(constraint.isValid(meteredLink, resourceContext), is(false));
+ assertThat(constraint.isValid(nonMeteredLink, resourceContext), is(true));
+ assertThat(constraint.isValid(unAnnotatedLink, resourceContext), is(true));
+ }
+
+ /**
+ * Tests the link costs for a constraint allowing metered links.
+ */
+ @Test
+ public void testCostAllowed() {
+ MeteredConstraint constraint = new MeteredConstraint(true);
+
+ assertThat(constraint.cost(meteredLink, resourceContext), is(1.0));
+ assertThat(constraint.cost(nonMeteredLink, resourceContext), is(1.0));
+ assertThat(constraint.cost(unAnnotatedLink, resourceContext), is(1.0));
+ }
+
+ /**
+ * Tests the link costs for a constraint not allowing metered links.
+ */
+ @Test
+ public void testCostNotAllowed() {
+ MeteredConstraint constraint = new MeteredConstraint(false);
+
+ assertThat(constraint.cost(meteredLink, resourceContext), is(-1.0));
+ assertThat(constraint.cost(nonMeteredLink, resourceContext), is(1.0));
+ assertThat(constraint.cost(unAnnotatedLink, resourceContext), is(1.0));
+ }
+
+ /**
+ * Tests equality of the constraint instances.
+ */
+ @Test
+ public void testEquality() {
+ MeteredConstraint c1 = new MeteredConstraint(true);
+ MeteredConstraint c2 = new MeteredConstraint(true);
+
+ MeteredConstraint c3 = new MeteredConstraint(false);
+ MeteredConstraint c4 = new MeteredConstraint(false);
+
+ new EqualsTester()
+ .addEqualityGroup(c1, c2)
+ .addEqualityGroup(c3, c4)
+ .testEquals();
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/TierConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/TierConstraintTest.java
new file mode 100644
index 0000000..5a48d87
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/TierConstraintTest.java
@@ -0,0 +1,252 @@
+/*
+ * Copyright 2014-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.constraint;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.graph.ScalarWeight;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultLink;
+import org.onosproject.net.DefaultPath;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Path;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.intent.Constraint;
+import org.onosproject.net.intent.ResourceContext;
+import org.onosproject.net.provider.ProviderId;
+
+import java.util.Arrays;
+
+import static org.easymock.EasyMock.createMock;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.net.AnnotationKeys.TIER;
+import static org.onosproject.net.DefaultLinkTest.cp;
+import static org.onosproject.net.DeviceId.deviceId;
+import static org.onosproject.net.Link.Type.DIRECT;
+
+/**
+ * Test for constraint of intermediate elements.
+ */
+public class TierConstraintTest {
+
+ private static final DeviceId DID1 = deviceId("of:1");
+ private static final DeviceId DID2 = deviceId("of:2");
+ private static final DeviceId DID3 = deviceId("of:3");
+ private static final DeviceId DID4 = deviceId("of:4");
+ private static final PortNumber PN1 = PortNumber.portNumber(1);
+ private static final PortNumber PN2 = PortNumber.portNumber(2);
+ private static final PortNumber PN3 = PortNumber.portNumber(3);
+ private static final PortNumber PN4 = PortNumber.portNumber(4);
+ private static final PortNumber PN5 = PortNumber.portNumber(5);
+ private static final PortNumber PN6 = PortNumber.portNumber(6);
+ private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
+ private static final String TIER1 = "1";
+ private static final String TIER2 = "2";
+ private static final String TIER3 = "3";
+
+ private ResourceContext resourceContext;
+
+ private Path path12;
+ private Path path13;
+ private Path path23;
+ private DefaultLink link1;
+ private DefaultLink link2;
+ private DefaultLink link3;
+
+ @Before
+ public void setUp() {
+ resourceContext = createMock(ResourceContext.class);
+
+ Annotations annotations1 = DefaultAnnotations.builder().set(TIER, TIER1).build();
+ Annotations annotations2 = DefaultAnnotations.builder().set(TIER, TIER2).build();
+ Annotations annotations3 = DefaultAnnotations.builder().set(TIER, TIER3).build();
+
+ link1 = DefaultLink.builder()
+ .providerId(PROVIDER_ID)
+ .src(cp(DID1, PN1))
+ .dst(cp(DID2, PN2))
+ .type(DIRECT)
+ .annotations(annotations1)
+ .build();
+ link2 = DefaultLink.builder()
+ .providerId(PROVIDER_ID)
+ .src(cp(DID2, PN3))
+ .dst(cp(DID3, PN4))
+ .type(DIRECT)
+ .annotations(annotations2)
+ .build();
+ link3 = DefaultLink.builder()
+ .providerId(PROVIDER_ID)
+ .src(cp(DID2, PN5))
+ .dst(cp(DID4, PN6))
+ .type(DIRECT)
+ .annotations(annotations3)
+ .build();
+
+ path12 = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10));
+ path13 = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link3), ScalarWeight.toWeight(10));
+ path23 = new DefaultPath(PROVIDER_ID, Arrays.asList(link2, link3), ScalarWeight.toWeight(10));
+ }
+
+ /**
+ * Tests that all of the links in the specified path have a tier included in the specified included tiers.
+ */
+ @Test
+ public void testSatisfyIncludedTiers() {
+ TierConstraint constraint12 = new TierConstraint(true, 1, 2);
+ assertThat(constraint12.validate(path12, resourceContext), is(true));
+
+ TierConstraint constraint13 = new TierConstraint(true, 1, 3);
+ assertThat(constraint13.validate(path13, resourceContext), is(true));
+
+ TierConstraint constraint23 = new TierConstraint(true, 2, 3);
+ assertThat(constraint23.validate(path23, resourceContext), is(true));
+ }
+
+ /**
+ * Tests that at least one link in the specified path has a tier not included the specified included tiers.
+ */
+ @Test
+ public void testNotSatisfyIncludedTiers() {
+ TierConstraint constraint12 = new TierConstraint(true, 1, 2);
+ assertThat(constraint12.validate(path13, resourceContext), is(false));
+ assertThat(constraint12.validate(path23, resourceContext), is(false));
+
+ TierConstraint constraint13 = new TierConstraint(true, 1, 3);
+ assertThat(constraint13.validate(path12, resourceContext), is(false));
+ assertThat(constraint13.validate(path23, resourceContext), is(false));
+
+ TierConstraint constraint23 = new TierConstraint(true, 2, 3);
+ assertThat(constraint23.validate(path12, resourceContext), is(false));
+ assertThat(constraint23.validate(path13, resourceContext), is(false));
+ }
+
+ /**
+ * Tests that all of the links in the specified path do not have a tier in the specified excluded tiers.
+ */
+ @Test
+ public void testSatisfyExcludedTiers() {
+ TierConstraint constraint12 = new TierConstraint(false, 1);
+ assertThat(constraint12.validate(path23, resourceContext), is(true));
+
+ TierConstraint constraint13 = new TierConstraint(false, 2);
+ assertThat(constraint13.validate(path13, resourceContext), is(true));
+
+ TierConstraint constraint23 = new TierConstraint(false, 3);
+ assertThat(constraint23.validate(path12, resourceContext), is(true));
+ }
+
+ /**
+ * Tests that at least one link in the specified path has a tier in the specified excluded tiers.
+ */
+ @Test
+ public void testNotSatisfyExcludedTiers() {
+ TierConstraint constraint12 = new TierConstraint(false, 1);
+ assertThat(constraint12.validate(path12, resourceContext), is(false));
+ assertThat(constraint12.validate(path13, resourceContext), is(false));
+
+ TierConstraint constraint13 = new TierConstraint(false, 2);
+ assertThat(constraint13.validate(path12, resourceContext), is(false));
+ assertThat(constraint13.validate(path23, resourceContext), is(false));
+
+ TierConstraint constraint23 = new TierConstraint(false, 3);
+ assertThat(constraint23.validate(path13, resourceContext), is(false));
+ assertThat(constraint23.validate(path23, resourceContext), is(false));
+ }
+
+ /**
+ * Tests the link cost is equal to order in which a tier was added to the constraint.
+ */
+ @Test
+ public void testOrderCost() {
+ TierConstraint constraint32 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2);
+
+ assertThat(constraint32.cost(link1, resourceContext), is(-1.0));
+ assertThat(constraint32.cost(link2, resourceContext), is(2.0));
+ assertThat(constraint32.cost(link3, resourceContext), is(1.0));
+
+ TierConstraint constraint123 = new TierConstraint(true, TierConstraint.CostType.ORDER, 1, 2, 3);
+
+ assertThat(constraint123.cost(link1, resourceContext), is(1.0));
+ assertThat(constraint123.cost(link2, resourceContext), is(2.0));
+ assertThat(constraint123.cost(link3, resourceContext), is(3.0));
+
+ TierConstraint constraint231 = new TierConstraint(true, TierConstraint.CostType.ORDER, 2, 3, 1);
+
+ assertThat(constraint231.cost(link1, resourceContext), is(3.0));
+ assertThat(constraint231.cost(link2, resourceContext), is(1.0));
+ assertThat(constraint231.cost(link3, resourceContext), is(2.0));
+
+ TierConstraint constraint312 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 1, 2);
+
+ assertThat(constraint312.cost(link1, resourceContext), is(2.0));
+ assertThat(constraint312.cost(link2, resourceContext), is(3.0));
+ assertThat(constraint312.cost(link3, resourceContext), is(1.0));
+ }
+
+ /**
+ * Tests the link cost is equal to order in which a tier was added to the constraint.
+ */
+ @Test
+ public void testOrderCostWithDuplicates() {
+ TierConstraint constraint32 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2, 1, 1, 2, 3);
+
+ assertThat(constraint32.cost(link1, resourceContext), is(3.0));
+ assertThat(constraint32.cost(link2, resourceContext), is(2.0));
+ assertThat(constraint32.cost(link3, resourceContext), is(1.0));
+ }
+
+ /**
+ * Tests the link cost is equal to tier value.
+ */
+ @Test
+ public void testTierCost() {
+ TierConstraint constraint123 = new TierConstraint(true, TierConstraint.CostType.TIER, 3, 1);
+
+ assertThat(constraint123.cost(link1, resourceContext), is(1.0));
+ assertThat(constraint123.cost(link2, resourceContext), is(-1.0));
+ assertThat(constraint123.cost(link3, resourceContext), is(3.0));
+ }
+
+ /**
+ * Tests the link cost is 1 if valid and -1 if invalid.
+ */
+ @Test
+ public void testValidCost() {
+ TierConstraint constraint = new TierConstraint(true, TierConstraint.CostType.VALID, 2, 1);
+
+ assertThat(constraint.cost(link1, resourceContext), is(1.0));
+ assertThat(constraint.cost(link2, resourceContext), is(1.0));
+ assertThat(constraint.cost(link3, resourceContext), is(-1.0));
+ }
+
+ @Test
+ public void testEquality() {
+ Constraint c1 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2, 1);
+ Constraint c2 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2, 1);
+
+ Constraint c3 = new TierConstraint(false, TierConstraint.CostType.TIER, 1);
+ Constraint c4 = new TierConstraint(false, TierConstraint.CostType.TIER, 1);
+
+ new EqualsTester()
+ .addEqualityGroup(c1, c2)
+ .addEqualityGroup(c3, c4)
+ .testEquals();
+ }
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/ConstraintCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/ConstraintCodec.java
index 5c99650..d5d5a16 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/ConstraintCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/ConstraintCodec.java
@@ -36,10 +36,13 @@
static final String KEY = "key";
static final String THRESHOLD = "threshold";
static final String BANDWIDTH = "bandwidth";
+ static final String METERED = "metered";
static final String LAMBDA = "lambda";
static final String LATENCY_MILLIS = "latencyMillis";
static final String OBSTACLES = "obstacles";
static final String WAYPOINTS = "waypoints";
+ static final String TIERS = "tiers";
+ static final String COST_TYPE = "costType";
@Override
public ObjectNode encode(Constraint constraint, CodecContext context) {
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DecodeConstraintCodecHelper.java b/core/common/src/main/java/org/onosproject/codec/impl/DecodeConstraintCodecHelper.java
index 22c50b1..1e0b35f 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/DecodeConstraintCodecHelper.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DecodeConstraintCodecHelper.java
@@ -29,8 +29,10 @@
import org.onosproject.net.intent.constraint.DomainConstraint;
import org.onosproject.net.intent.constraint.LatencyConstraint;
import org.onosproject.net.intent.constraint.LinkTypeConstraint;
+import org.onosproject.net.intent.constraint.MeteredConstraint;
import org.onosproject.net.intent.constraint.NonDisruptiveConstraint;
import org.onosproject.net.intent.constraint.ObstacleConstraint;
+import org.onosproject.net.intent.constraint.TierConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import com.fasterxml.jackson.databind.JsonNode;
@@ -193,6 +195,41 @@
return nonDisruptive();
}
+ private Constraint decodeMeteredConstraint() {
+ boolean metered = nullIsIllegal(json.get(ConstraintCodec.METERED),
+ ConstraintCodec.METERED + ConstraintCodec.MISSING_MEMBER_MESSAGE).asBoolean();
+ return new MeteredConstraint(metered);
+ }
+
+ /**
+ * Decodes a link type constraint.
+ *
+ * @return link type constraint object.
+ */
+ private Constraint decodeTierConstraint() {
+ boolean inclusive = nullIsIllegal(json.get(ConstraintCodec.INCLUSIVE),
+ ConstraintCodec.INCLUSIVE + ConstraintCodec.MISSING_MEMBER_MESSAGE).asBoolean();
+
+ TierConstraint.CostType costType = TierConstraint.CostType.valueOf(nullIsIllegal(
+ json.get(ConstraintCodec.COST_TYPE), ConstraintCodec.COST_TYPE + ConstraintCodec.MISSING_MEMBER_MESSAGE
+ ).asText());
+
+ JsonNode tiers = nullIsIllegal(json.get(ConstraintCodec.TIERS),
+ ConstraintCodec.TIERS + ConstraintCodec.MISSING_MEMBER_MESSAGE);
+ if (tiers.size() < 1) {
+ throw new IllegalArgumentException(
+ ConstraintCodec.TIERS + " array in tier constraint must have at least one value");
+ }
+
+ ArrayList<Integer> tierEntries = new ArrayList<>(tiers.size());
+ IntStream.range(0, tiers.size())
+ .forEach(index ->
+ tierEntries.add(new Integer(tiers.get(index).asText())));
+
+ return new TierConstraint(inclusive, costType,
+ tierEntries.toArray(new Integer[tiers.size()]));
+ }
+
/**
* Decodes the given constraint.
*
@@ -221,7 +258,12 @@
return decodeDomainConstraint();
} else if (type.equals(NonDisruptiveConstraint.class.getSimpleName())) {
return decodeNonDisruptiveConstraint();
+ } else if (type.equals(MeteredConstraint.class.getSimpleName())) {
+ return decodeMeteredConstraint();
+ } else if (type.equals(TierConstraint.class.getSimpleName())) {
+ return decodeTierConstraint();
}
+
throw new IllegalArgumentException("Instruction type "
+ type + " is not supported");
}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/EncodeConstraintCodecHelper.java b/core/common/src/main/java/org/onosproject/codec/impl/EncodeConstraintCodecHelper.java
index 486f4e4..6c1c846 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/EncodeConstraintCodecHelper.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/EncodeConstraintCodecHelper.java
@@ -23,7 +23,9 @@
import org.onosproject.net.intent.constraint.BandwidthConstraint;
import org.onosproject.net.intent.constraint.LatencyConstraint;
import org.onosproject.net.intent.constraint.LinkTypeConstraint;
+import org.onosproject.net.intent.constraint.MeteredConstraint;
import org.onosproject.net.intent.constraint.ObstacleConstraint;
+import org.onosproject.net.intent.constraint.TierConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -155,6 +157,38 @@
return result;
}
+ private ObjectNode encodeMeteredConstraint() {
+ checkNotNull(constraint, "Metered constraint cannot be null");
+ final MeteredConstraint meteredConstraint =
+ (MeteredConstraint) constraint;
+ return context.mapper().createObjectNode()
+ .put("metered", meteredConstraint.isUseMetered());
+ }
+
+ /**
+ * Encodes a tier constraint.
+ *
+ * @return JSON ObjectNode representing the constraint
+ */
+ private ObjectNode encodeTierConstraint() {
+ checkNotNull(constraint, "Tier constraint cannot be null");
+ final TierConstraint tierConstraint = (TierConstraint) constraint;
+
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put(ConstraintCodec.INCLUSIVE, tierConstraint.isInclusive())
+ .put(ConstraintCodec.COST_TYPE, tierConstraint.costType().name());
+
+ final ArrayNode jsonTiers = result.putArray(ConstraintCodec.TIERS);
+
+ if (tierConstraint.tiers() != null) {
+ for (Integer tier : tierConstraint.tiers()) {
+ jsonTiers.add(tier);
+ }
+ }
+
+ return result;
+ }
+
/**
* Encodes the constraint in JSON.
*
@@ -174,6 +208,10 @@
result = encodeObstacleConstraint();
} else if (constraint instanceof WaypointConstraint) {
result = encodeWaypointConstraint();
+ } else if (constraint instanceof MeteredConstraint) {
+ result = encodeMeteredConstraint();
+ } else if (constraint instanceof TierConstraint) {
+ result = encodeTierConstraint();
} else {
result = context.mapper().createObjectNode();
}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/ConstraintCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/ConstraintCodecTest.java
index 569a775..2fdac58 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/ConstraintCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/ConstraintCodecTest.java
@@ -31,7 +31,9 @@
import org.onosproject.net.intent.constraint.DomainConstraint;
import org.onosproject.net.intent.constraint.LatencyConstraint;
import org.onosproject.net.intent.constraint.LinkTypeConstraint;
+import org.onosproject.net.intent.constraint.MeteredConstraint;
import org.onosproject.net.intent.constraint.ObstacleConstraint;
+import org.onosproject.net.intent.constraint.TierConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import com.fasterxml.jackson.databind.JsonNode;
@@ -196,4 +198,37 @@
Constraint constraint = getConstraint("DomainConstraint.json");
assertThat(constraint, instanceOf(DomainConstraint.class));
}
+
+ /**
+ * Tests metered constraint.
+ */
+ @Test
+ public void meteredConstraint() {
+ Constraint constraint = getConstraint("MeteredConstraint.json");
+ assertThat(constraint, instanceOf(MeteredConstraint.class));
+
+ MeteredConstraint meteredConstraint = (MeteredConstraint) constraint;
+
+ assertThat(meteredConstraint.isUseMetered(), is(true));
+ }
+
+ /**
+ * Tests tier constraint.
+ */
+ @Test
+ public void tierConstraint() {
+ Constraint constraint = getConstraint("TierConstraint.json");
+ assertThat(constraint, instanceOf(TierConstraint.class));
+
+ TierConstraint tierConstraint = (TierConstraint) constraint;
+
+ assertThat(tierConstraint.isInclusive(), is(true));
+ assertThat(tierConstraint.costType(), is(TierConstraint.CostType.ORDER));
+
+ assertThat(tierConstraint.tiers().get(0), is(3));
+ assertThat(tierConstraint.tiers().get(1), is(2));
+ assertThat(tierConstraint.tiers().get(2), is(1));
+ }
+
+
}
diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/MeteredConstraint.json b/core/common/src/test/resources/org/onosproject/codec/impl/MeteredConstraint.json
new file mode 100644
index 0000000..435f603
--- /dev/null
+++ b/core/common/src/test/resources/org/onosproject/codec/impl/MeteredConstraint.json
@@ -0,0 +1,4 @@
+{
+ "type":"MeteredConstraint",
+ "metered": true
+}
diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/TierConstraint.json b/core/common/src/test/resources/org/onosproject/codec/impl/TierConstraint.json
new file mode 100644
index 0000000..4da785d
--- /dev/null
+++ b/core/common/src/test/resources/org/onosproject/codec/impl/TierConstraint.json
@@ -0,0 +1,6 @@
+{
+ "type":"TierConstraint",
+ "inclusive": true,
+ "costType": "ORDER",
+ "tiers": [3, 2, 1]
+}