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