blob: 9f455825a9c467247301600cc7200d6e8ff67253 [file] [log] [blame]
Ray Milkeyc8f481f2014-11-18 15:37:12 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Ray Milkeyc8f481f2014-11-18 15:37:12 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080017
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080018import java.time.Duration;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080019import java.util.HashSet;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Set;
23
Yi Tseng2a81c9d2016-09-14 10:14:24 -070024import com.google.common.collect.Sets;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080025import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070027import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Link;
29import org.onosproject.net.NetTestTools;
Luca Prete670ac5d2017-02-03 15:55:43 -080030import org.onosproject.net.ResourceGroup;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.TrafficSelector;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080032
Michele Santuari4a338072014-11-05 18:38:55 +010033import com.google.common.collect.ImmutableSet;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080034import com.google.common.testing.EqualsTester;
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080035import org.onosproject.net.intent.constraint.LatencyConstraint;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080036
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.hasSize;
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080039import static org.hamcrest.Matchers.instanceOf;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080040import static org.hamcrest.Matchers.is;
41import static org.hamcrest.Matchers.nullValue;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080042import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import static org.onosproject.net.NetTestTools.APP_ID;
44import static org.onosproject.net.NetTestTools.link;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080045
46/**
47 * Unit tests for the LinkCollectionIntent class.
48 */
Brian O'Connor520c0522014-11-23 23:50:47 -080049public class LinkCollectionIntentTest extends IntentTest {
Ray Milkeyc8f481f2014-11-18 15:37:12 -080050
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080051 final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 2);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080052 final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
53 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
54 final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
Yi Tseng2a81c9d2016-09-14 10:14:24 -070055 final FilteredConnectPoint filteredIngress = new FilteredConnectPoint(ingress);
56 final FilteredConnectPoint filteredEgress = new FilteredConnectPoint(egress);
Luca Prete670ac5d2017-02-03 15:55:43 -080057 final ResourceGroup resourceGroup = ResourceGroup.of(0L);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080058
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 Milkey37f6a382014-11-25 14:54:42 -080070 @Test
Ray Milkeyc8f481f2014-11-18 15:37:12 -080071 public void testEquals() {
72
73 final HashSet<Link> links1 = new HashSet<>();
74 links1.add(link("src", 1, "dst", 2));
75 final LinkCollectionIntent collectionIntent1 =
Ray Milkeyebc5d222015-03-18 15:45:36 -070076 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 Prete670ac5d2017-02-03 15:55:43 -080083 .resourceGroup(resourceGroup)
Ray Milkeyebc5d222015-03-18 15:45:36 -070084 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -080085
86 final HashSet<Link> links2 = new HashSet<>();
87 links2.add(link("src", 1, "dst", 3));
88 final LinkCollectionIntent collectionIntent2 =
Ray Milkeyebc5d222015-03-18 15:45:36 -070089 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 Prete670ac5d2017-02-03 15:55:43 -080096 .resourceGroup(resourceGroup)
Ray Milkeyebc5d222015-03-18 15:45:36 -070097 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -080098
99 new EqualsTester()
Ray Milkey37f6a382014-11-25 14:54:42 -0800100 .addEqualityGroup(collectionIntent1)
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800101 .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 Milkeyebc5d222015-03-18 15:45:36 -0700113 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 Prete670ac5d2017-02-03 15:55:43 -0800120 .resourceGroup(resourceGroup)
Ray Milkeyebc5d222015-03-18 15:45:36 -0700121 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800122
123 final Set<Link> createdLinks = collectionIntent.links();
124 assertThat(createdLinks, hasSize(1));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800125 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800126 assertThat(collectionIntent.treatment(), is(treatment));
127 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800128 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100129 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Luca Prete670ac5d2017-02-03 15:55:43 -0800130 assertThat(collectionIntent.resourceGroup(), is(resourceGroup));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800131 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 SHIMIZU4e2149e2016-02-22 14:42:34 -0800145 constraints.add(new LatencyConstraint(Duration.ofMillis(100)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800146 final LinkCollectionIntent collectionIntent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700147 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 Milkeyc8f481f2014-11-18 15:37:12 -0800157
158 final Set<Link> createdLinks = collectionIntent.links();
159 assertThat(createdLinks, hasSize(1));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800160 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800161 assertThat(collectionIntent.treatment(), is(treatment));
162 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800163 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100164 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800165
166 final List<Constraint> createdConstraints = collectionIntent.constraints();
167 assertThat(createdConstraints, hasSize(1));
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -0800168 assertThat(createdConstraints.get(0), instanceOf(LatencyConstraint.class));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800169 }
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 SHIMIZUee2aa652015-02-25 18:56:43 -0800182 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800183 assertThat(collectionIntent.treatment(), nullValue());
184 assertThat(collectionIntent.selector(), nullValue());
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800185 assertThat(collectionIntent.ingressPoints(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100186 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800187
188 final List<Constraint> createdConstraints = collectionIntent.constraints();
189 assertThat(createdConstraints, hasSize(0));
190 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800191
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700192 /**
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'Connor520c0522014-11-23 23:50:47 -0800225 @Override
226 protected Intent createOne() {
227 HashSet<Link> links1 = new HashSet<>();
228 links1.add(link("src", 1, "dst", 2));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700229 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'Connor520c0522014-11-23 23:50:47 -0800237 }
238
239 @Override
240 protected Intent createAnother() {
241 HashSet<Link> links2 = new HashSet<>();
242 links2.add(link("src", 1, "dst", 3));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700243 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'Connor520c0522014-11-23 23:50:47 -0800251 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700252
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 Milkeyc8f481f2014-11-18 15:37:12 -0800281}