Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 17 | |
Sho SHIMIZU | 4e2149e | 2016-02-22 14:42:34 -0800 | [diff] [blame] | 18 | import java.time.Duration; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 19 | import java.util.HashSet; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | import java.util.Set; |
| 23 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 25 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 27 | import org.onosproject.net.FilteredConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.NetTestTools; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 31 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 32 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 33 | import com.google.common.testing.EqualsTester; |
Sho SHIMIZU | 4e2149e | 2016-02-22 14:42:34 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.constraint.LatencyConstraint; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 35 | |
| 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.Matchers.hasSize; |
Sho SHIMIZU | 4e2149e | 2016-02-22 14:42:34 -0800 | [diff] [blame] | 38 | import static org.hamcrest.Matchers.instanceOf; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 39 | import static org.hamcrest.Matchers.is; |
| 40 | import static org.hamcrest.Matchers.nullValue; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 41 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 43 | import static org.onosproject.net.NetTestTools.link; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Unit tests for the LinkCollectionIntent class. |
| 47 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 48 | public class LinkCollectionIntentTest extends IntentTest { |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 49 | |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 50 | final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 2); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 51 | final ConnectPoint egress = NetTestTools.connectPoint("egress", 3); |
| 52 | final TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 53 | final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 54 | final FilteredConnectPoint filteredIngress = new FilteredConnectPoint(ingress); |
| 55 | final FilteredConnectPoint filteredEgress = new FilteredConnectPoint(egress); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Checks that the LinkCollectionIntent class is immutable. |
| 59 | */ |
| 60 | @Test |
| 61 | public void testImmutability() { |
| 62 | assertThatClassIsImmutable(LinkCollectionIntent.class); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Tests equals(), hashCode() and toString() methods. |
| 67 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 68 | @Test |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 69 | public void testEquals() { |
| 70 | |
| 71 | final HashSet<Link> links1 = new HashSet<>(); |
| 72 | links1.add(link("src", 1, "dst", 2)); |
| 73 | final LinkCollectionIntent collectionIntent1 = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 74 | LinkCollectionIntent.builder() |
| 75 | .appId(APP_ID) |
| 76 | .selector(selector) |
| 77 | .treatment(treatment) |
| 78 | .links(links1) |
| 79 | .ingressPoints(ImmutableSet.of(ingress)) |
| 80 | .egressPoints(ImmutableSet.of(egress)) |
| 81 | .build(); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 82 | |
| 83 | final HashSet<Link> links2 = new HashSet<>(); |
| 84 | links2.add(link("src", 1, "dst", 3)); |
| 85 | final LinkCollectionIntent collectionIntent2 = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 86 | LinkCollectionIntent.builder() |
| 87 | .appId(APP_ID) |
| 88 | .selector(selector) |
| 89 | .treatment(treatment) |
| 90 | .links(links2) |
| 91 | .ingressPoints(ImmutableSet.of(ingress)) |
| 92 | .egressPoints(ImmutableSet.of(egress)) |
| 93 | .build(); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 94 | |
| 95 | new EqualsTester() |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 96 | .addEqualityGroup(collectionIntent1) |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 97 | .addEqualityGroup(collectionIntent2) |
| 98 | .testEquals(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Tests constructor without constraints. |
| 103 | */ |
| 104 | @Test |
| 105 | public void testConstructor() { |
| 106 | final HashSet<Link> links1 = new HashSet<>(); |
| 107 | links1.add(link("src", 1, "dst", 2)); |
| 108 | final LinkCollectionIntent collectionIntent = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 109 | LinkCollectionIntent.builder() |
| 110 | .appId(APP_ID) |
| 111 | .selector(selector) |
| 112 | .treatment(treatment) |
| 113 | .links(links1) |
| 114 | .ingressPoints(ImmutableSet.of(ingress)) |
| 115 | .egressPoints(ImmutableSet.of(egress)) |
| 116 | .build(); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 117 | |
| 118 | final Set<Link> createdLinks = collectionIntent.links(); |
| 119 | assertThat(createdLinks, hasSize(1)); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 120 | assertThat(collectionIntent.isInstallable(), is(false)); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 121 | assertThat(collectionIntent.treatment(), is(treatment)); |
| 122 | assertThat(collectionIntent.selector(), is(selector)); |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 123 | assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress))); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 124 | assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress))); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 125 | assertThat(collectionIntent.resources(), hasSize(1)); |
| 126 | final List<Constraint> createdConstraints = collectionIntent.constraints(); |
| 127 | assertThat(createdConstraints, hasSize(0)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests constructor with constraints. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testConstructorWithConstraints() { |
| 135 | final HashSet<Link> links1 = new HashSet<>(); |
| 136 | final LinkedList<Constraint> constraints = new LinkedList<>(); |
| 137 | |
| 138 | links1.add(link("src", 1, "dst", 2)); |
Sho SHIMIZU | 4e2149e | 2016-02-22 14:42:34 -0800 | [diff] [blame] | 139 | constraints.add(new LatencyConstraint(Duration.ofMillis(100))); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 140 | final LinkCollectionIntent collectionIntent = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 141 | LinkCollectionIntent.builder() |
| 142 | .appId(APP_ID) |
| 143 | .selector(selector) |
| 144 | .treatment(treatment) |
| 145 | .links(links1) |
| 146 | .ingressPoints(ImmutableSet.of(ingress)) |
| 147 | .egressPoints(ImmutableSet.of(egress)) |
| 148 | .constraints(constraints) |
| 149 | .priority(8888) |
| 150 | .build(); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 151 | |
| 152 | final Set<Link> createdLinks = collectionIntent.links(); |
| 153 | assertThat(createdLinks, hasSize(1)); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 154 | assertThat(collectionIntent.isInstallable(), is(false)); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 155 | assertThat(collectionIntent.treatment(), is(treatment)); |
| 156 | assertThat(collectionIntent.selector(), is(selector)); |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 157 | assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress))); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 158 | assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress))); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 159 | |
| 160 | final List<Constraint> createdConstraints = collectionIntent.constraints(); |
| 161 | assertThat(createdConstraints, hasSize(1)); |
Sho SHIMIZU | 4e2149e | 2016-02-22 14:42:34 -0800 | [diff] [blame] | 162 | assertThat(createdConstraints.get(0), instanceOf(LatencyConstraint.class)); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Tests constructor with constraints. |
| 167 | */ |
| 168 | @Test |
| 169 | public void testSerializerConstructor() { |
| 170 | |
| 171 | final LinkCollectionIntent collectionIntent = |
| 172 | new LinkCollectionIntent(); |
| 173 | |
| 174 | final Set<Link> createdLinks = collectionIntent.links(); |
| 175 | assertThat(createdLinks, nullValue()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 176 | assertThat(collectionIntent.isInstallable(), is(false)); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 177 | assertThat(collectionIntent.treatment(), nullValue()); |
| 178 | assertThat(collectionIntent.selector(), nullValue()); |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 179 | assertThat(collectionIntent.ingressPoints(), nullValue()); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 180 | assertThat(collectionIntent.egressPoints(), nullValue()); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 181 | |
| 182 | final List<Constraint> createdConstraints = collectionIntent.constraints(); |
| 183 | assertThat(createdConstraints, hasSize(0)); |
| 184 | } |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 185 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Test filtered connection point for LinkCollection intent. |
| 188 | */ |
| 189 | @Test |
| 190 | public void testFilteredConnectedPoint() { |
| 191 | LinkCollectionIntent intent = createFilteredOne(); |
| 192 | Set<Link> links = Sets.newHashSet(); |
| 193 | links.add(link("A", 1, "B", 1)); |
| 194 | links.add(link("A", 2, "C", 1)); |
| 195 | |
| 196 | assertThat(intent.appId(), is(APP_ID)); |
| 197 | assertThat(intent.treatment(), is(treatment)); |
| 198 | assertThat(intent.links(), is(links)); |
| 199 | assertThat(intent.applyTreatmentOnEgress(), is(false)); |
| 200 | assertThat(intent.filteredIngressPoints(), is(ImmutableSet.of(filteredIngress))); |
| 201 | assertThat(intent.filteredEgressPoints(), is(ImmutableSet.of(filteredEgress))); |
| 202 | |
| 203 | intent = createAnotherFiltered(); |
| 204 | links = Sets.newHashSet(); |
| 205 | links.add(link("A", 1, "B", 1)); |
| 206 | links.add(link("A", 2, "C", 1)); |
| 207 | links.add(link("B", 2, "D", 1)); |
| 208 | links.add(link("B", 3, "E", 1)); |
| 209 | |
| 210 | assertThat(intent.appId(), is(APP_ID)); |
| 211 | assertThat(intent.treatment(), is(treatment)); |
| 212 | assertThat(intent.links(), is(links)); |
| 213 | assertThat(intent.applyTreatmentOnEgress(), is(true)); |
| 214 | assertThat(intent.filteredIngressPoints(), is(ImmutableSet.of(filteredIngress))); |
| 215 | assertThat(intent.filteredEgressPoints(), is(ImmutableSet.of(filteredEgress))); |
| 216 | |
| 217 | } |
| 218 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 219 | @Override |
| 220 | protected Intent createOne() { |
| 221 | HashSet<Link> links1 = new HashSet<>(); |
| 222 | links1.add(link("src", 1, "dst", 2)); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 223 | return LinkCollectionIntent.builder() |
| 224 | .appId(APP_ID) |
| 225 | .selector(selector) |
| 226 | .treatment(treatment) |
| 227 | .links(links1) |
| 228 | .ingressPoints(ImmutableSet.of(ingress)) |
| 229 | .egressPoints(ImmutableSet.of(egress)) |
| 230 | .build(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | protected Intent createAnother() { |
| 235 | HashSet<Link> links2 = new HashSet<>(); |
| 236 | links2.add(link("src", 1, "dst", 3)); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 237 | return LinkCollectionIntent.builder() |
| 238 | .appId(APP_ID) |
| 239 | .selector(selector) |
| 240 | .treatment(treatment) |
| 241 | .links(links2) |
| 242 | .ingressPoints(ImmutableSet.of(ingress)) |
| 243 | .egressPoints(ImmutableSet.of(egress)) |
| 244 | .build(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 245 | } |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 246 | |
| 247 | protected LinkCollectionIntent createFilteredOne() { |
| 248 | Set<Link> links = Sets.newHashSet(); |
| 249 | links.add(link("A", 1, "B", 1)); |
| 250 | links.add(link("A", 2, "C", 1)); |
| 251 | return LinkCollectionIntent.builder() |
| 252 | .appId(APP_ID) |
| 253 | .treatment(treatment) |
| 254 | .links(links) |
| 255 | .filteredIngressPoints(ImmutableSet.of(filteredIngress)) |
| 256 | .filteredEgressPoints(ImmutableSet.of(filteredEgress)) |
| 257 | .build(); |
| 258 | } |
| 259 | |
| 260 | protected LinkCollectionIntent createAnotherFiltered() { |
| 261 | Set<Link> links = Sets.newHashSet(); |
| 262 | links.add(link("A", 1, "B", 1)); |
| 263 | links.add(link("A", 2, "C", 1)); |
| 264 | links.add(link("B", 2, "D", 1)); |
| 265 | links.add(link("B", 3, "E", 1)); |
| 266 | return LinkCollectionIntent.builder() |
| 267 | .appId(APP_ID) |
| 268 | .treatment(treatment) |
| 269 | .links(links) |
| 270 | .applyTreatmentOnEgress(true) |
| 271 | .filteredIngressPoints(ImmutableSet.of(filteredIngress)) |
| 272 | .filteredEgressPoints(ImmutableSet.of(filteredEgress)) |
| 273 | .build(); |
| 274 | } |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 275 | } |