ONOS-3224 HostToHostIntent should have implicit non-OPTICAL constraint
Change-Id: Ibd6214e9b12199f56f0761cfdaa1eb45a600eb1c
diff --git a/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java b/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java
index bd4219a..c146724 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java
@@ -17,11 +17,15 @@
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
+
import org.onosproject.core.ApplicationId;
import org.onosproject.net.HostId;
+import org.onosproject.net.Link;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.intent.constraint.LinkTypeConstraint;
import java.util.List;
@@ -33,6 +37,8 @@
@Beta
public final class HostToHostIntent extends ConnectivityIntent {
+ static final LinkTypeConstraint NOT_OPTICAL = new LinkTypeConstraint(false, Link.Type.OPTICAL);
+
private final HostId one;
private final HostId two;
@@ -115,6 +121,15 @@
*/
public HostToHostIntent build() {
+ List<Constraint> theConstraints = constraints;
+ // If not-OPTICAL constraint hasn't been specified, add them
+ if (!constraints.contains(NOT_OPTICAL)) {
+ theConstraints = ImmutableList.<Constraint>builder()
+ .add(NOT_OPTICAL)
+ .addAll(constraints)
+ .build();
+ }
+
return new HostToHostIntent(
appId,
key,
@@ -122,7 +137,7 @@
two,
selector,
treatment,
- constraints,
+ theConstraints,
priority
);
}
diff --git a/core/api/src/test/java/org/onosproject/net/intent/HostToHostIntentTest.java b/core/api/src/test/java/org/onosproject/net/intent/HostToHostIntentTest.java
index 3f7650e..f870df6 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/HostToHostIntentTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/HostToHostIntentTest.java
@@ -16,15 +16,20 @@
package org.onosproject.net.intent;
import org.junit.Test;
+import org.onlab.util.Bandwidth;
import org.onosproject.TestApplicationId;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.HostId;
import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.intent.constraint.BandwidthConstraint;
+import org.onosproject.net.resource.link.BandwidthResource;
+import com.google.common.collect.ImmutableList;
import com.google.common.testing.EqualsTester;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
import static org.onosproject.net.NetTestTools.hid;
@@ -102,6 +107,56 @@
.testEquals();
}
+ @Test
+ public void testImplicitConstraintsAreAdded() {
+ final BandwidthConstraint other = new BandwidthConstraint(
+ new BandwidthResource(Bandwidth.gbps(1)));
+ final HostToHostIntent intent = HostToHostIntent.builder()
+ .appId(APPID)
+ .one(id1)
+ .two(id2)
+ .selector(selector)
+ .treatment(treatment)
+ .constraints(ImmutableList.of(other))
+ .build();
+
+ assertThat(intent.constraints(), hasItem(HostToHostIntent.NOT_OPTICAL));
+ }
+
+ @Test
+ public void testImplicitConstraints() {
+ final HostToHostIntent implicit = HostToHostIntent.builder()
+ .appId(APPID)
+ .one(id1)
+ .two(id2)
+ .selector(selector)
+ .treatment(treatment)
+ .build();
+ final HostToHostIntent empty = HostToHostIntent.builder()
+ .appId(APPID)
+ .one(id1)
+ .two(id2)
+ .selector(selector)
+ .treatment(treatment)
+ .constraints(ImmutableList.of())
+ .build();
+ final HostToHostIntent exact = HostToHostIntent.builder()
+ .appId(APPID)
+ .one(id1)
+ .two(id2)
+ .selector(selector)
+ .treatment(treatment)
+ .constraints(ImmutableList.of(HostToHostIntent.NOT_OPTICAL))
+ .build();
+
+ new EqualsTester()
+ .addEqualityGroup(implicit.constraints(),
+ empty.constraints(),
+ exact.constraints())
+ .testEquals();
+
+ }
+
@Override
protected Intent createOne() {
return HostToHostIntent.builder()