blob: cb25b3cc21ef421f23919d0af673950c2c773d6d [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;
30import org.onosproject.net.flow.TrafficSelector;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080031
Michele Santuari4a338072014-11-05 18:38:55 +010032import com.google.common.collect.ImmutableSet;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080033import com.google.common.testing.EqualsTester;
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080034import org.onosproject.net.intent.constraint.LatencyConstraint;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080035
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.hasSize;
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080038import static org.hamcrest.Matchers.instanceOf;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080039import static org.hamcrest.Matchers.is;
40import static org.hamcrest.Matchers.nullValue;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080041import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import static org.onosproject.net.NetTestTools.APP_ID;
43import static org.onosproject.net.NetTestTools.link;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080044
45/**
46 * Unit tests for the LinkCollectionIntent class.
47 */
Brian O'Connor520c0522014-11-23 23:50:47 -080048public class LinkCollectionIntentTest extends IntentTest {
Ray Milkeyc8f481f2014-11-18 15:37:12 -080049
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080050 final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 2);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080051 final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
52 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
53 final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
Yi Tseng2a81c9d2016-09-14 10:14:24 -070054 final FilteredConnectPoint filteredIngress = new FilteredConnectPoint(ingress);
55 final FilteredConnectPoint filteredEgress = new FilteredConnectPoint(egress);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080056
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 Milkey37f6a382014-11-25 14:54:42 -080068 @Test
Ray Milkeyc8f481f2014-11-18 15:37:12 -080069 public void testEquals() {
70
71 final HashSet<Link> links1 = new HashSet<>();
72 links1.add(link("src", 1, "dst", 2));
73 final LinkCollectionIntent collectionIntent1 =
Ray Milkeyebc5d222015-03-18 15:45:36 -070074 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 Milkeyc8f481f2014-11-18 15:37:12 -080082
83 final HashSet<Link> links2 = new HashSet<>();
84 links2.add(link("src", 1, "dst", 3));
85 final LinkCollectionIntent collectionIntent2 =
Ray Milkeyebc5d222015-03-18 15:45:36 -070086 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 Milkeyc8f481f2014-11-18 15:37:12 -080094
95 new EqualsTester()
Ray Milkey37f6a382014-11-25 14:54:42 -080096 .addEqualityGroup(collectionIntent1)
Ray Milkeyc8f481f2014-11-18 15:37:12 -080097 .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 Milkeyebc5d222015-03-18 15:45:36 -0700109 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 Milkeyc8f481f2014-11-18 15:37:12 -0800117
118 final Set<Link> createdLinks = collectionIntent.links();
119 assertThat(createdLinks, hasSize(1));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800120 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800121 assertThat(collectionIntent.treatment(), is(treatment));
122 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800123 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100124 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800125 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 SHIMIZU4e2149e2016-02-22 14:42:34 -0800139 constraints.add(new LatencyConstraint(Duration.ofMillis(100)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800140 final LinkCollectionIntent collectionIntent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700141 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 Milkeyc8f481f2014-11-18 15:37:12 -0800151
152 final Set<Link> createdLinks = collectionIntent.links();
153 assertThat(createdLinks, hasSize(1));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800154 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800155 assertThat(collectionIntent.treatment(), is(treatment));
156 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800157 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100158 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800159
160 final List<Constraint> createdConstraints = collectionIntent.constraints();
161 assertThat(createdConstraints, hasSize(1));
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -0800162 assertThat(createdConstraints.get(0), instanceOf(LatencyConstraint.class));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800163 }
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 SHIMIZUee2aa652015-02-25 18:56:43 -0800176 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800177 assertThat(collectionIntent.treatment(), nullValue());
178 assertThat(collectionIntent.selector(), nullValue());
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800179 assertThat(collectionIntent.ingressPoints(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100180 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800181
182 final List<Constraint> createdConstraints = collectionIntent.constraints();
183 assertThat(createdConstraints, hasSize(0));
184 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800185
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700186 /**
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'Connor520c0522014-11-23 23:50:47 -0800219 @Override
220 protected Intent createOne() {
221 HashSet<Link> links1 = new HashSet<>();
222 links1.add(link("src", 1, "dst", 2));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700223 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'Connor520c0522014-11-23 23:50:47 -0800231 }
232
233 @Override
234 protected Intent createAnother() {
235 HashSet<Link> links2 = new HashSet<>();
236 links2.add(link("src", 1, "dst", 3));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700237 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'Connor520c0522014-11-23 23:50:47 -0800245 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700246
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 Milkeyc8f481f2014-11-18 15:37:12 -0800275}