blob: ae6d1039107e507ac83a4317e3d2e4516d779b35 [file] [log] [blame]
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.net.newresource.impl;
17
18import org.junit.Before;
19import org.junit.Test;
20import org.onlab.packet.VlanId;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.LinkKey;
24import org.onosproject.net.PortNumber;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070025import org.onosproject.net.newresource.ResourcePath;
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070026
27import java.util.function.Predicate;
28
29import static org.hamcrest.Matchers.is;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070030import static org.junit.Assert.assertThat;
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070031
32/**
33 * Unit tests for ResourceManager.
34 */
35public class ResourceManagerTest {
36
37 private static final DeviceId D1 = DeviceId.deviceId("of:001");
38 private static final DeviceId D2 = DeviceId.deviceId("of:002");
39 private static final PortNumber P1 = PortNumber.portNumber(1);
40 private static final ConnectPoint CP1_1 = new ConnectPoint(D1, P1);
41 private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1);
42 private static final short VLAN_LOWER_LIMIT = 0;
43 private static final short VLAN_UPPER_LIMIT = 1024;
44
45 private final Predicate<VlanId> vlanPredicate =
46 x -> x.toShort() >= VLAN_LOWER_LIMIT && x.toShort() < VLAN_UPPER_LIMIT;
47 private ResourceManager manager;
48
49 @Before
50 public void setUp() {
51 manager = new ResourceManager();
52 }
53
54 /**
55 * Tests resource boundaries.
56 */
57 @Test
58 public void testBoundary() {
59 manager.defineResourceBoundary(VlanId.class, vlanPredicate);
60
61 LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
62
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070063 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070064 is(false));
65
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070066 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070067 is(true));
68
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070069 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) 100))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070070 is(true));
71
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070072 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070073 is(true));
74
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070075 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070076 is(false));
77 }
78
79 /**
80 * Tests the case that a boundary is not set.
81 */
82 @Test
83 public void testWhenBoundaryNotSet() {
84 LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
85
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070086 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070087 is(true));
88
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070089 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070090 is(true));
91
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070092 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) 100))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070093 is(true));
94
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070095 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070096 is(true));
97
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070098 assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070099 is(true));
100 }
101}