blob: fe54a66d3350c18211d535376cc100496f3dccff [file] [log] [blame]
Pier Ventref8543d82016-09-28 19:49:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventref8543d82016-09-28 19:49:33 -07003 *
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 */
16
17package org.onosproject.net.resource.impl;
18
19import com.google.common.collect.ImmutableSet;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.MplsLabel;
24import org.onlab.packet.VlanId;
25import org.onlab.util.Identifier;
26import org.onosproject.core.IdGenerator;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DefaultLink;
29import org.onosproject.net.EncapsulationType;
30import org.onosproject.net.Link;
31import org.onosproject.net.LinkKey;
32import org.onosproject.net.intent.IntentId;
33import org.onosproject.net.intent.MockIdGenerator;
34import org.onosproject.net.resource.MockResourceService;
35import org.onosproject.net.resource.impl.LabelAllocator.FirstFitSelection;
36import org.onosproject.net.resource.impl.LabelAllocator.LabelSelection;
37import org.onosproject.net.resource.impl.LabelAllocator.RandomSelection;
38
Pier Luigi0b4222e2017-07-21 09:47:14 +020039
Pier Ventref8543d82016-09-28 19:49:33 -070040import java.util.Arrays;
41import java.util.List;
42import java.util.Map;
43
44import static org.hamcrest.CoreMatchers.instanceOf;
45import static org.hamcrest.MatcherAssert.assertThat;
46import static org.junit.Assert.assertEquals;
Pier Luigi0b4222e2017-07-21 09:47:14 +020047import static org.junit.Assert.assertNull;
Pier Ventref8543d82016-09-28 19:49:33 -070048import static org.junit.Assert.assertTrue;
49import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
50import static org.onosproject.net.Link.Type.DIRECT;
51import static org.onosproject.net.NetTestTools.PID;
52import static org.onosproject.net.NetTestTools.connectPoint;
53
54/**
55 * Unit tests for LabelAllocator.
56 */
57public class LabelAllocatorTest {
58
59 private LabelAllocator allocator;
60 private MockResourceService resourceService;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070061 private IdGenerator idGenerator = MockIdGenerator.INSTANCE;
Pier Ventref8543d82016-09-28 19:49:33 -070062
63 private final ConnectPoint d1p0 = connectPoint("s1", 0);
64 private final ConnectPoint d1p1 = connectPoint("s1", 1);
65 private final ConnectPoint d2p0 = connectPoint("s2", 0);
66 private final ConnectPoint d2p1 = connectPoint("s2", 1);
Pier Luigi0b4222e2017-07-21 09:47:14 +020067 private final ConnectPoint d3p0 = connectPoint("s3", 0);
68 private final ConnectPoint d3p1 = connectPoint("s3", 1);
69 private final ConnectPoint d4p0 = connectPoint("s4", 0);
70 private final ConnectPoint d4p1 = connectPoint("s4", 1);
Pier Ventref8543d82016-09-28 19:49:33 -070071
72 private final List<Link> links = Arrays.asList(
73 createEdgeLink(d1p0, true),
Pier Luigi0b4222e2017-07-21 09:47:14 +020074 DefaultLink.builder().providerId(PID).src(d1p1).dst(d3p1).type(DIRECT).build(),
75 DefaultLink.builder().providerId(PID).src(d3p0).dst(d2p1).type(DIRECT).build(),
Pier Ventref8543d82016-09-28 19:49:33 -070076 createEdgeLink(d2p0, false)
77 );
78
Pier Luigi0b4222e2017-07-21 09:47:14 +020079 private final List<Link> links2 = Arrays.asList(
80 createEdgeLink(d1p0, true),
81 DefaultLink.builder().providerId(PID).src(d1p1).dst(d3p1).type(DIRECT).build(),
82 DefaultLink.builder().providerId(PID).src(d3p0).dst(d4p1).type(DIRECT).build(),
83 DefaultLink.builder().providerId(PID).src(d4p0).dst(d2p1).type(DIRECT).build(),
84 createEdgeLink(d2p0, false)
85 );
86
87 // Selection behavior
Pier Ventref8543d82016-09-28 19:49:33 -070088 private final String firstFit = "FIRST_FIT";
89 private final String random = "RANDOM";
90 private final String wrong = "BLAHBLAHBLAH";
91
Pier Luigi0b4222e2017-07-21 09:47:14 +020092 // Optimization behavior
93 private final String none = "NONE";
94 private final String noswap = "NO_SWAP";
95 private final String minswap = "MIN_SWAP";
96
Pier Ventref8543d82016-09-28 19:49:33 -070097 @Before
98 public void setUp() {
99 this.resourceService = new MockResourceService();
100 this.allocator = new LabelAllocator(this.resourceService);
101 }
102
103 @After
104 public void tearDown() {
105
106 }
107
108 /**
109 * To test changes to the selection behavior.
110 */
111 @Test
Pier Luigi0b4222e2017-07-21 09:47:14 +0200112 public void testChangeSelBehavior() {
Pier Ventref8543d82016-09-28 19:49:33 -0700113 // It has to be an instance of LabelSelection
114 assertThat(this.allocator.getLabelSelection(), instanceOf(LabelSelection.class));
115 // By default we have Random Selection
116 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
117 // We change to FirstFit and we test the change
118 this.allocator.setLabelSelection(firstFit);
119 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
120 // We change to Random and we test the change
121 this.allocator.setLabelSelection(random);
122 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
123 // We put a wrong type and we should have a Random selection
124 this.allocator.setLabelSelection(wrong);
125 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
Pier Luigi0b4222e2017-07-21 09:47:14 +0200126 // We change to first_fit and we test the change
127 this.allocator.setLabelSelection("first_fit");
128 // The change does not happen
129 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
130 this.allocator.setLabelSelection(firstFit);
131 // We change to Random and we test the change
132 this.allocator.setLabelSelection("random");
133 // The change does not happen
134 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
Pier Ventref8543d82016-09-28 19:49:33 -0700135 }
136
137 /**
Pier Luigi0b4222e2017-07-21 09:47:14 +0200138 * To test changes to the optimization behavior.
Pier Ventref8543d82016-09-28 19:49:33 -0700139 */
140 @Test
Pier Luigi0b4222e2017-07-21 09:47:14 +0200141 public void testChangeOptBehavior() {
142 // It has to be an instance of NONE
143 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NONE);
144 // Change to MIN_SWAP
145 this.allocator.setOptLabelSelection(minswap);
146 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.MIN_SWAP);
147 // Change to NO_SWAP
148 this.allocator.setOptLabelSelection(noswap);
149 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NO_SWAP);
150 // Change to NONE
151 this.allocator.setOptLabelSelection(none);
152 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NONE);
153 // Change to MIN_SWAP
154 this.allocator.setOptLabelSelection("miN_swap");
155 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NONE);
156 this.allocator.setOptLabelSelection(minswap);
157 // Change to NO_SWAP
158 this.allocator.setOptLabelSelection("No_swap");
159 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.MIN_SWAP);
160 this.allocator.setOptLabelSelection(noswap);
161 // Change to NONE
162 this.allocator.setOptLabelSelection("none");
163 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NO_SWAP);
164 }
165
166 /**
167 * To test the first fit behavior. Using NONE optimization
168 */
169 @Test
170 public void testFirstFitBehaviorNone() {
Pier Ventref8543d82016-09-28 19:49:33 -0700171 // We change to FirstFit and we test the change
172 this.allocator.setLabelSelection(firstFit);
173 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
Pier Luigi0b4222e2017-07-21 09:47:14 +0200174 // It has to be an instance of NONE
175 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NONE);
176 // Filter reservations
177 this.resourceService.filterAssignment = true;
178 // We change the available Ids
179 this.resourceService.availableVlanLabels = ImmutableSet.of(
180 (short) 1,
181 (short) 20,
182 (short) 100
183 );
184 // First allocation on a subset of links
Pier Ventref8543d82016-09-28 19:49:33 -0700185 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
Pier Luigi0b4222e2017-07-21 09:47:14 +0200186 ImmutableSet.copyOf(links.subList(2, 3)),
Pier Ventref8543d82016-09-28 19:49:33 -0700187 IntentId.valueOf(idGenerator.getNewId()),
188 EncapsulationType.VLAN);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200189 Identifier<?> id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
190 // value has to be a Vlan Id
Pier Ventref8543d82016-09-28 19:49:33 -0700191 assertThat(id, instanceOf(VlanId.class));
192 // value should not be a forbidden value
193 VlanId vlanId = (VlanId) id;
194 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200195 // We test the behavior for VLAN
Pier Ventref8543d82016-09-28 19:49:33 -0700196 allocation = this.allocator.assignLabelToLinks(
Pier Luigi0b4222e2017-07-21 09:47:14 +0200197 ImmutableSet.copyOf(links.subList(1, 3)),
Pier Ventref8543d82016-09-28 19:49:33 -0700198 IntentId.valueOf(idGenerator.getNewId()),
199 EncapsulationType.VLAN);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200200 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
Pier Ventref8543d82016-09-28 19:49:33 -0700201 assertThat(id, instanceOf(VlanId.class));
Pier Ventref8543d82016-09-28 19:49:33 -0700202 vlanId = (VlanId) id;
203 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200204 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
205 assertThat(id, instanceOf(VlanId.class));
206 vlanId = (VlanId) id;
207 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
Pier Ventref8543d82016-09-28 19:49:33 -0700208 }
209
210 /**
Pier Luigi0b4222e2017-07-21 09:47:14 +0200211 * To test the first fit behavior. Using NO_SWAP optimization
Pier Ventref8543d82016-09-28 19:49:33 -0700212 */
213 @Test
Pier Luigi0b4222e2017-07-21 09:47:14 +0200214 public void testFirstFitBehaviorNoSwap() {
Pier Ventref8543d82016-09-28 19:49:33 -0700215 // We change to FirstFit and we test the change
216 this.allocator.setLabelSelection(firstFit);
217 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
Pier Luigi0b4222e2017-07-21 09:47:14 +0200218 /// Change to NO_SWAP
219 this.allocator.setOptLabelSelection(noswap);
220 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NO_SWAP);
221 // Filter reservations
222 this.resourceService.filterAssignment = true;
223 // We change the available Ids
224 this.resourceService.availableMplsLabels = ImmutableSet.of(
225 1,
226 100,
227 1000
228 );
229 // First allocation on a subset of links
Pier Ventref8543d82016-09-28 19:49:33 -0700230 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
Pier Luigi0b4222e2017-07-21 09:47:14 +0200231 ImmutableSet.copyOf(links.subList(2, 3)),
Pier Ventref8543d82016-09-28 19:49:33 -0700232 IntentId.valueOf(idGenerator.getNewId()),
233 EncapsulationType.MPLS);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200234 Identifier<?> id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
235 // value has to be a MPLS label
Pier Ventref8543d82016-09-28 19:49:33 -0700236 assertThat(id, instanceOf(MplsLabel.class));
237 // value should not be a forbidden value
238 MplsLabel mplsLabel = (MplsLabel) id;
239 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200240 // We test the behavior for MPLS
Pier Ventref8543d82016-09-28 19:49:33 -0700241 allocation = this.allocator.assignLabelToLinks(
Pier Luigi0b4222e2017-07-21 09:47:14 +0200242 ImmutableSet.copyOf(links.subList(1, 3)),
Pier Ventref8543d82016-09-28 19:49:33 -0700243 IntentId.valueOf(idGenerator.getNewId()),
244 EncapsulationType.MPLS);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200245 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
Pier Ventref8543d82016-09-28 19:49:33 -0700246 assertThat(id, instanceOf(MplsLabel.class));
Pier Ventref8543d82016-09-28 19:49:33 -0700247 mplsLabel = (MplsLabel) id;
248 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200249 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
250 assertThat(id, instanceOf(MplsLabel.class));
251 mplsLabel = (MplsLabel) id;
252 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
Pier Ventref8543d82016-09-28 19:49:33 -0700253 }
254
255 /**
Pier Luigi0b4222e2017-07-21 09:47:14 +0200256 * To test the first fit behavior. Using MIN_SWAP optimization
Pier Ventref8543d82016-09-28 19:49:33 -0700257 */
258 @Test
Pier Luigi0b4222e2017-07-21 09:47:14 +0200259 public void testFirstFitBehaviorMinSwap() {
260 // We change to FirstFit and we test the change
261 this.allocator.setLabelSelection(firstFit);
262 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
263 /// Change to MIN_SWAP
264 this.allocator.setOptLabelSelection(minswap);
265 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.MIN_SWAP);
266 // Filter reservations
267 this.resourceService.filterAssignment = true;
268 // We change the available Ids
269 this.resourceService.availableVlanLabels = ImmutableSet.of(
270 (short) 2,
271 (short) 20,
272 (short) 200
273 );
274 // First allocation on a subset of links
Pier Ventref8543d82016-09-28 19:49:33 -0700275 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
Pier Luigi0b4222e2017-07-21 09:47:14 +0200276 ImmutableSet.copyOf(links2.subList(2, 3)),
Pier Ventref8543d82016-09-28 19:49:33 -0700277 IntentId.valueOf(idGenerator.getNewId()),
278 EncapsulationType.VLAN);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200279 Identifier<?> id = allocation.get(LinkKey.linkKey(d3p0, d4p1));
280 // value has to be a VLAN id
Pier Ventref8543d82016-09-28 19:49:33 -0700281 assertThat(id, instanceOf(VlanId.class));
282 // value should not be a forbidden value
283 VlanId vlanId = (VlanId) id;
Pier Luigi0b4222e2017-07-21 09:47:14 +0200284 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
285 // We test the behavior for VLAN
286 allocation = this.allocator.assignLabelToLinks(
287 ImmutableSet.copyOf(links2.subList(1, 4)),
288 IntentId.valueOf(idGenerator.getNewId()),
289 EncapsulationType.VLAN);
290 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
291 assertThat(id, instanceOf(VlanId.class));
292 vlanId = (VlanId) id;
293 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
294 id = allocation.get(LinkKey.linkKey(d3p0, d4p1));
295 assertThat(id, instanceOf(VlanId.class));
296 vlanId = (VlanId) id;
297 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
298 id = allocation.get(LinkKey.linkKey(d4p0, d2p1));
299 assertThat(id, instanceOf(VlanId.class));
300 vlanId = (VlanId) id;
301 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
Pier Ventref8543d82016-09-28 19:49:33 -0700302 }
303
304 /**
Pier Luigi0b4222e2017-07-21 09:47:14 +0200305 * To test random behavior. Using NONE optimization
Pier Ventref8543d82016-09-28 19:49:33 -0700306 */
307 @Test
Pier Luigi0b4222e2017-07-21 09:47:14 +0200308 public void testRandomBehaviorNone() {
309 // By default Random is the selection behavior used
Pier Ventref8543d82016-09-28 19:49:33 -0700310 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
Pier Luigi0b4222e2017-07-21 09:47:14 +0200311 // It has to be an instance of NONE
312 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NONE);
313 // Filter reservations
314 this.resourceService.filterAssignment = true;
315 // We change the available Ids
316 this.resourceService.availableMplsLabels = ImmutableSet.of(
317 1,
318 2,
319 3,
320 4,
321 5,
322 6
323 );
324 // First allocation on a subset of links
Pier Ventref8543d82016-09-28 19:49:33 -0700325 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
Pier Luigi0b4222e2017-07-21 09:47:14 +0200326 ImmutableSet.copyOf(links.subList(2, 3)),
Pier Ventref8543d82016-09-28 19:49:33 -0700327 IntentId.valueOf(idGenerator.getNewId()),
328 EncapsulationType.MPLS);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200329 Identifier<?> id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
330 // value has to be a MPLS label
Pier Ventref8543d82016-09-28 19:49:33 -0700331 assertThat(id, instanceOf(MplsLabel.class));
332 // value should not be a forbidden value
Pier Luigi0b4222e2017-07-21 09:47:14 +0200333 MplsLabel mplsLabel = (MplsLabel) id;
334 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
335 // We test the behavior for MPLS
Pier Ventref8543d82016-09-28 19:49:33 -0700336 allocation = this.allocator.assignLabelToLinks(
Pier Luigi0b4222e2017-07-21 09:47:14 +0200337 ImmutableSet.copyOf(links.subList(1, 3)),
Pier Ventref8543d82016-09-28 19:49:33 -0700338 IntentId.valueOf(idGenerator.getNewId()),
339 EncapsulationType.MPLS);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200340 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
341 assertThat(id, instanceOf(MplsLabel.class));
342 mplsLabel = (MplsLabel) id;
343 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
344 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
345 assertThat(id, instanceOf(MplsLabel.class));
346 mplsLabel = (MplsLabel) id;
347 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
348 }
349
350 /**
351 * To test random behavior. Using NO_SWAP optimization
352 */
353 @Test
354 public void testRandomBehaviorNoSwap() {
355 // By default Random is the selection behavior used
356 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
357 // Change to NO_SWAP
358 this.allocator.setOptLabelSelection(noswap);
359 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NO_SWAP);
360 // Filter reservations
361 this.resourceService.filterAssignment = true;
362 // We change the available Ids
363 this.resourceService.availableVlanLabels = ImmutableSet.of(
364 (short) 1,
365 (short) 2,
366 (short) 3,
367 (short) 4,
368 (short) 5,
369 (short) 6
370 );
371 // First allocation on a subset of links
372 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
373 ImmutableSet.copyOf(links.subList(2, 3)),
374 IntentId.valueOf(idGenerator.getNewId()),
375 EncapsulationType.VLAN);
376 Identifier<?> id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
377 // value has to be a VLAN Id
378 assertThat(id, instanceOf(VlanId.class));
379 // value should not be a forbidden value
380 VlanId vlanId = (VlanId) id;
381 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
382 // We test the behavior for VLAN
383 allocation = this.allocator.assignLabelToLinks(
384 ImmutableSet.copyOf(links.subList(1, 3)),
385 IntentId.valueOf(idGenerator.getNewId()),
386 EncapsulationType.VLAN);
387 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
388 assertThat(id, instanceOf(VlanId.class));
389 vlanId = (VlanId) id;
390 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
391 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
392 assertThat(id, instanceOf(VlanId.class));
393 vlanId = (VlanId) id;
394 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
395 }
396
397 /**
398 * To test the random behavior. Using MIN_SWAP optimization
399 */
400 @Test
401 public void testRandomBehaviorMinSwap() {
402 // By default Random is the selection behavior used
403 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
404 // Change to MIN_SWAP
405 this.allocator.setOptLabelSelection(minswap);
406 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.MIN_SWAP);
407 // Filter reservations
408 this.resourceService.filterAssignment = true;
409 // We change the available Ids
410 this.resourceService.availableMplsLabels = ImmutableSet.of(
411 1,
412 2,
413 3,
414 4,
415 5,
416 6,
417 7,
418 8
419 );
420 // First allocation on a subset of links
421 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
422 ImmutableSet.copyOf(links2.subList(2, 3)),
423 IntentId.valueOf(idGenerator.getNewId()),
424 EncapsulationType.MPLS);
425 Identifier<?> id = allocation.get(LinkKey.linkKey(d3p0, d4p1));
426 // value has to be a MPLS label
Pier Ventref8543d82016-09-28 19:49:33 -0700427 assertThat(id, instanceOf(MplsLabel.class));
428 // value should not be a forbidden value
Pier Luigi0b4222e2017-07-21 09:47:14 +0200429 MplsLabel mplsLabel = (MplsLabel) id;
430 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
431 // We test the behavior for MPLS
432 allocation = this.allocator.assignLabelToLinks(
433 ImmutableSet.copyOf(links2.subList(1, 4)),
434 IntentId.valueOf(idGenerator.getNewId()),
435 EncapsulationType.MPLS);
436 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
437 assertThat(id, instanceOf(MplsLabel.class));
438 mplsLabel = (MplsLabel) id;
439 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
440 id = allocation.get(LinkKey.linkKey(d3p0, d4p1));
441 assertThat(id, instanceOf(MplsLabel.class));
442 mplsLabel = (MplsLabel) id;
443 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
444 id = allocation.get(LinkKey.linkKey(d4p0, d2p1));
445 assertThat(id, instanceOf(MplsLabel.class));
446 mplsLabel = (MplsLabel) id;
447 assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS);
Pier Ventref8543d82016-09-28 19:49:33 -0700448 }
449
Pier Luigi0b4222e2017-07-21 09:47:14 +0200450
Pier Ventref8543d82016-09-28 19:49:33 -0700451 /**
452 * To test the port key based API.
453 */
454 @Test
455 public void testPortKey() {
Pier Luigi0b4222e2017-07-21 09:47:14 +0200456 // Verify the first fit behavior
Pier Ventref8543d82016-09-28 19:49:33 -0700457 this.allocator.setLabelSelection(firstFit);
458 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
459 // We test the behavior for VLAN
460 Map<ConnectPoint, Identifier<?>> allocation = this.allocator.assignLabelToPorts(
461 ImmutableSet.copyOf(links.subList(1, 2)),
462 IntentId.valueOf(idGenerator.getNewId()),
463 EncapsulationType.VLAN);
464 Identifier<?> id = allocation.get(new ConnectPoint(d1p1.elementId(), d1p1.port()));
465 // value has to be a VlanId
466 assertThat(id, instanceOf(VlanId.class));
467 // value should not be a forbidden value
468 VlanId prevVlanId = (VlanId) id;
469 assertTrue(VlanId.NO_VID < prevVlanId.toShort() && prevVlanId.toShort() < VlanId.MAX_VLAN);
470 // value has to be 1
471 assertEquals(1, prevVlanId.toShort());
472 // verify same applies for d2p1
Pier Luigi0b4222e2017-07-21 09:47:14 +0200473 id = allocation.get(new ConnectPoint(d3p1.elementId(), d3p1.port()));
Pier Ventref8543d82016-09-28 19:49:33 -0700474 assertThat(id, instanceOf(VlanId.class));
475 // value should not be a forbidden value
476 VlanId vlanId = (VlanId) id;
477 assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN);
478 // value has to be 1
479 assertEquals(prevVlanId, vlanId);
480 }
481
Pier Luigi0b4222e2017-07-21 09:47:14 +0200482 /**
483 * To test the developed algorithms when there are no labels.
484 */
485 @Test
486 public void noLabelsTest() {
487 // Verify the first fit behavior with NONE optimization
488 this.allocator.setLabelSelection(firstFit);
489 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
490 // It has to be an instance of NONE
491 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NONE);
492 // We change the available Ids
493 this.resourceService.availableVlanLabels = ImmutableSet.of(
494 (short) 10
495 );
496 // Enable filtering of the reservation
497 this.resourceService.filterAssignment = true;
498 // We test the behavior for VLAN
499 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
500 ImmutableSet.copyOf(links.subList(1, 3)),
501 IntentId.valueOf(idGenerator.getNewId()),
502 EncapsulationType.VLAN);
503 Identifier<?> id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
504 // value has to be a VlanId
505 assertThat(id, instanceOf(VlanId.class));
506 // value should not be a forbidden value
507 VlanId label = (VlanId) id;
508 assertTrue(VlanId.NO_VID < label.toShort() && label.toShort() < VlanId.MAX_VLAN);
509 // Next hop
510 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
511 assertThat(id, instanceOf(VlanId.class));
512 label = (VlanId) id;
513 assertTrue(VlanId.NO_VID < label.toShort() && label.toShort() < VlanId.MAX_VLAN);
514 // No labels are available, reservation is not possible
515 allocation = this.allocator.assignLabelToLinks(
516 ImmutableSet.copyOf(links.subList(1, 3)),
517 IntentId.valueOf(idGenerator.getNewId()),
518 EncapsulationType.VLAN);
519 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
520 // value has to be null
521 assertNull(id);
522 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
523 // value has to be null
524 assertNull(id);
Pier Ventref8543d82016-09-28 19:49:33 -0700525
Pier Luigi0b4222e2017-07-21 09:47:14 +0200526 // Verify the random behavior with NONE_SWAP optimization
527 this.allocator.setLabelSelection(random);
528 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
529 // Change to NO_SWAP
530 this.allocator.setOptLabelSelection(noswap);
531 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NO_SWAP);
532 // We change the available Ids
533 this.resourceService.availableMplsLabels = ImmutableSet.of(
534 2000
535 );
536 // Enable filtering of the reservation
537 this.resourceService.filterAssignment = true;
538 // We test the behavior for MPLS
539 allocation = this.allocator.assignLabelToLinks(
540 ImmutableSet.copyOf(links.subList(1, 3)),
541 IntentId.valueOf(idGenerator.getNewId()),
542 EncapsulationType.MPLS);
543 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
544 // value has to be a Mplslabel
545 assertThat(id, instanceOf(MplsLabel.class));
546 // value should not be a forbidden value
547 MplsLabel mplsLabel = (MplsLabel) id;
548 assertTrue(0 <= mplsLabel.toInt() && mplsLabel.toInt() <= MplsLabel.MAX_MPLS);
549 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
550 assertThat(id, instanceOf(MplsLabel.class));
551 mplsLabel = (MplsLabel) id;
552 assertTrue(0 <= mplsLabel.toInt() && mplsLabel.toInt() <= MplsLabel.MAX_MPLS);
553 // No labels are available, reservation is not possible
554 allocation = this.allocator.assignLabelToLinks(
555 ImmutableSet.copyOf(links.subList(1, 3)),
556 IntentId.valueOf(idGenerator.getNewId()),
557 EncapsulationType.MPLS);
558 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
559 // value has to be null
560 assertNull(id);
561 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
562 // value has to be null
563 assertNull(id);
564
565 // Verify the first fit behavior with MIN optimization
566 this.allocator.setLabelSelection(firstFit);
567 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
568 // Change to MIN_SWAP
569 this.allocator.setOptLabelSelection(minswap);
570 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.MIN_SWAP);
571 // We change the available Ids
572 this.resourceService.availableVlanLabels = ImmutableSet.of(
573 (short) 11
574 );
575 // Enable filtering of the reservation
576 this.resourceService.filterAssignment = true;
577 // We test the behavior for VLAN
578 allocation = this.allocator.assignLabelToLinks(
579 ImmutableSet.copyOf(links.subList(1, 3)),
580 IntentId.valueOf(idGenerator.getNewId()),
581 EncapsulationType.VLAN);
582 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
583 // value has to be a VlanId
584 assertThat(id, instanceOf(VlanId.class));
585 // value should not be a forbidden value
586 label = (VlanId) id;
587 assertTrue(VlanId.NO_VID < label.toShort() && label.toShort() < VlanId.MAX_VLAN);
588 // Next hop
589 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
590 assertThat(id, instanceOf(VlanId.class));
591 label = (VlanId) id;
592 assertTrue(VlanId.NO_VID < label.toShort() && label.toShort() < VlanId.MAX_VLAN);
593 // No labels are available, reservation is not possible
594 allocation = this.allocator.assignLabelToLinks(
595 ImmutableSet.copyOf(links.subList(1, 3)),
596 IntentId.valueOf(idGenerator.getNewId()),
597 EncapsulationType.VLAN);
598 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
599 // value has to be null
600 assertNull(id);
601 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
602 // value has to be null
603 assertNull(id);
604 }
605
606 /**
607 * To test the developed algorithms when there are no labels on a specific link.
608 */
609 @Test
610 public void noLabelsOnLinkTest() {
611 // Verify the first fit behavior with NONE optimization
612 this.allocator.setLabelSelection(firstFit);
613 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
614 // It has to be an instance of NONE
615 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NONE);
616 // We change the available Ids
617 this.resourceService.availableVlanLabels = ImmutableSet.of(
618 (short) 10
619 );
620 // Enable filtering of the reservation
621 this.resourceService.filterAssignment = true;
622 // We test the behavior for VLAN
623 Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks(
624 ImmutableSet.copyOf(links.subList(2, 3)),
625 IntentId.valueOf(idGenerator.getNewId()),
626 EncapsulationType.VLAN);
627 Identifier<?> id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
628 assertThat(id, instanceOf(VlanId.class));
629 VlanId label = (VlanId) id;
630 assertTrue(VlanId.NO_VID < label.toShort() && label.toShort() < VlanId.MAX_VLAN);
631 // No labels are available, reservation is not possible
632 allocation = this.allocator.assignLabelToLinks(
633 ImmutableSet.copyOf(links.subList(1, 3)),
634 IntentId.valueOf(idGenerator.getNewId()),
635 EncapsulationType.VLAN);
636 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
637 // value has to be null
638 assertNull(id);
639 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
640 // value has to be null
641 assertNull(id);
642
643 // Verify the random behavior with NONE_SWAP optimization
644 this.allocator.setLabelSelection(random);
645 assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class));
646 // Change to NO_SWAP
647 this.allocator.setOptLabelSelection(noswap);
648 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.NO_SWAP);
649 // We change the available Ids
650 this.resourceService.availableMplsLabels = ImmutableSet.of(
651 2000
652 );
653 // Enable filtering of the reservation
654 this.resourceService.filterAssignment = true;
655 // We test the behavior for MPLS
656 allocation = this.allocator.assignLabelToLinks(
657 ImmutableSet.copyOf(links.subList(2, 3)),
658 IntentId.valueOf(idGenerator.getNewId()),
659 EncapsulationType.MPLS);
660 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
661 assertThat(id, instanceOf(MplsLabel.class));
662 MplsLabel mplsLabel = (MplsLabel) id;
663 assertTrue(0 <= mplsLabel.toInt() && mplsLabel.toInt() <= MplsLabel.MAX_MPLS);
664 // No labels are available, reservation is not possible
665 allocation = this.allocator.assignLabelToLinks(
666 ImmutableSet.copyOf(links.subList(1, 3)),
667 IntentId.valueOf(idGenerator.getNewId()),
668 EncapsulationType.MPLS);
669 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
670 // value has to be null
671 assertNull(id);
672 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
673 // value has to be null
674 assertNull(id);
675
676 // Verify the first fit behavior with MIN optimization
677 this.allocator.setLabelSelection(firstFit);
678 assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class));
679 // Change to MIN_SWAP
680 this.allocator.setOptLabelSelection(minswap);
681 assertEquals(this.allocator.getOptLabelSelection(), LabelAllocator.OptimizationBehavior.MIN_SWAP);
682 // We change the available Ids
683 this.resourceService.availableVlanLabels = ImmutableSet.of(
684 (short) 11
685 );
686 // Enable filtering of the reservation
687 this.resourceService.filterAssignment = true;
688 // We test the behavior for VLAN
689 allocation = this.allocator.assignLabelToLinks(
690 ImmutableSet.copyOf(links.subList(2, 3)),
691 IntentId.valueOf(idGenerator.getNewId()),
692 EncapsulationType.VLAN);
693 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
694 assertThat(id, instanceOf(VlanId.class));
695 label = (VlanId) id;
696 assertTrue(VlanId.NO_VID < label.toShort() && label.toShort() < VlanId.MAX_VLAN);
697 // No labels are available, reservation is not possible
698 allocation = this.allocator.assignLabelToLinks(
699 ImmutableSet.copyOf(links.subList(1, 3)),
700 IntentId.valueOf(idGenerator.getNewId()),
701 EncapsulationType.VLAN);
702 id = allocation.get(LinkKey.linkKey(d1p1, d3p1));
703 // value has to be null
704 assertNull(id);
705 id = allocation.get(LinkKey.linkKey(d3p0, d2p1));
706 // value has to be null
707 assertNull(id);
708 }
Pier Ventref8543d82016-09-28 19:49:33 -0700709
710}