Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.newresource.impl; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.LinkKey; |
| 24 | import org.onosproject.net.PortNumber; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 25 | import org.onosproject.net.newresource.ResourcePath; |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.function.Predicate; |
| 28 | |
| 29 | import static org.hamcrest.Matchers.is; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 30 | import static org.junit.Assert.assertThat; |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Unit tests for ResourceManager. |
| 34 | */ |
| 35 | public 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 SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 63 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 64 | is(false)); |
| 65 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 66 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 67 | is(true)); |
| 68 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 69 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) 100))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 70 | is(true)); |
| 71 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 72 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 73 | is(true)); |
| 74 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 75 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 76 | 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 SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 86 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 87 | is(true)); |
| 88 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 89 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 90 | is(true)); |
| 91 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 92 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) 100))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 93 | is(true)); |
| 94 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 95 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 96 | is(true)); |
| 97 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 98 | assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))), |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 99 | is(true)); |
| 100 | } |
| 101 | } |