Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.easymock.EasyMock.createMock; |
| 19 | import static org.easymock.EasyMock.expect; |
| 20 | import static org.easymock.EasyMock.replay; |
| 21 | import static org.easymock.EasyMock.reset; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertNull; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 26 | import java.util.Collections; |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
| 33 | import org.onlab.packet.IpPrefix; |
| 34 | import org.onlab.packet.MacAddress; |
| 35 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.host.HostService; |
| 40 | import org.onosproject.net.host.InterfaceIpAddress; |
| 41 | import org.onosproject.net.host.PortAddresses; |
| 42 | import org.onosproject.sdnip.config.Interface; |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 43 | |
| 44 | import com.google.common.collect.Maps; |
| 45 | import com.google.common.collect.Sets; |
| 46 | |
| 47 | /** |
| 48 | * Unit tests for the HostToInterfaceAdaptor class. |
| 49 | */ |
| 50 | public class HostToInterfaceAdaptorTest { |
| 51 | |
| 52 | private HostService hostService; |
| 53 | private HostToInterfaceAdaptor adaptor; |
| 54 | |
| 55 | private Set<PortAddresses> portAddresses; |
| 56 | private Map<ConnectPoint, Interface> interfaces; |
| 57 | |
| 58 | private static final ConnectPoint CP1 = new ConnectPoint( |
| 59 | DeviceId.deviceId("of:1"), PortNumber.portNumber(1)); |
| 60 | private static final ConnectPoint CP2 = new ConnectPoint( |
| 61 | DeviceId.deviceId("of:1"), PortNumber.portNumber(2)); |
| 62 | private static final ConnectPoint CP3 = new ConnectPoint( |
| 63 | DeviceId.deviceId("of:2"), PortNumber.portNumber(1)); |
| 64 | |
| 65 | private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint( |
| 66 | DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1)); |
| 67 | |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 68 | @Before |
| 69 | public void setUp() throws Exception { |
| 70 | hostService = createMock(HostService.class); |
| 71 | |
| 72 | portAddresses = Sets.newHashSet(); |
| 73 | interfaces = Maps.newHashMap(); |
| 74 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 75 | InterfaceIpAddress ia11 = |
| 76 | new InterfaceIpAddress(IpAddress.valueOf("192.168.1.1"), |
| 77 | IpPrefix.valueOf("192.168.1.0/24")); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 78 | createPortAddressesAndInterface(CP1, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 79 | Sets.newHashSet(ia11), |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 80 | MacAddress.valueOf("00:00:00:00:00:01"), |
| 81 | VlanId.NONE); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 82 | |
| 83 | // Two addresses in the same subnet |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 84 | InterfaceIpAddress ia21 = |
| 85 | new InterfaceIpAddress(IpAddress.valueOf("192.168.2.1"), |
| 86 | IpPrefix.valueOf("192.168.2.0/24")); |
| 87 | InterfaceIpAddress ia22 = |
| 88 | new InterfaceIpAddress(IpAddress.valueOf("192.168.2.2"), |
| 89 | IpPrefix.valueOf("192.168.2.0/24")); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 90 | createPortAddressesAndInterface(CP2, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 91 | Sets.newHashSet(ia21, ia22), |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 92 | MacAddress.valueOf("00:00:00:00:00:02"), |
| 93 | VlanId.vlanId((short) 4)); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 94 | |
| 95 | // Two addresses in different subnets |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 96 | InterfaceIpAddress ia31 = |
| 97 | new InterfaceIpAddress(IpAddress.valueOf("192.168.3.1"), |
| 98 | IpPrefix.valueOf("192.168.3.0/24")); |
| 99 | InterfaceIpAddress ia41 = |
| 100 | new InterfaceIpAddress(IpAddress.valueOf("192.168.4.1"), |
| 101 | IpPrefix.valueOf("192.168.4.0/24")); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 102 | createPortAddressesAndInterface(CP3, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 103 | Sets.newHashSet(ia31, ia41), |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 104 | MacAddress.valueOf("00:00:00:00:00:03"), |
| 105 | VlanId.NONE); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 106 | |
| 107 | expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes(); |
| 108 | |
| 109 | replay(hostService); |
| 110 | |
| 111 | adaptor = new HostToInterfaceAdaptor(hostService); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Creates both a PortAddresses and an Interface for the given inputs and |
| 116 | * places them in the correct global data stores. |
| 117 | * |
| 118 | * @param cp the connect point |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 119 | * @param ipAddresses the set of interface IP addresses |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 120 | * @param mac the MAC address |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 121 | * @param vlan the VLAN ID |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 122 | */ |
| 123 | private void createPortAddressesAndInterface( |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 124 | ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 125 | MacAddress mac, VlanId vlan) { |
| 126 | PortAddresses pa = new PortAddresses(cp, ipAddresses, mac, vlan); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 127 | portAddresses.add(pa); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 128 | expect(hostService.getAddressBindingsForPort(cp)).andReturn( |
| 129 | Collections.singleton(pa)).anyTimes(); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 130 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 131 | Interface intf = new Interface(cp, ipAddresses, mac, vlan); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 132 | interfaces.put(cp, intf); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Tests {@link HostToInterfaceAdaptor#getInterfaces()}. |
| 137 | * Verifies that the set of interfaces returned matches what is expected |
| 138 | * based on the input PortAddresses data. |
| 139 | */ |
| 140 | @Test |
| 141 | public void testGetInterfaces() { |
| 142 | Set<Interface> adaptorIntfs = adaptor.getInterfaces(); |
| 143 | |
| 144 | assertEquals(3, adaptorIntfs.size()); |
| 145 | assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1))); |
| 146 | assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2))); |
| 147 | assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3))); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}. |
| 152 | * Verifies that the correct interface is returned for a given connect |
| 153 | * point. |
| 154 | */ |
| 155 | @Test |
| 156 | public void testGetInterface() { |
| 157 | assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1)); |
| 158 | assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2)); |
| 159 | assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3)); |
| 160 | |
| 161 | // Try and get an interface for a connect point with no addresses |
| 162 | reset(hostService); |
| 163 | expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP)) |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 164 | .andReturn(Collections.<PortAddresses>emptySet()).anyTimes(); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 165 | replay(hostService); |
| 166 | |
| 167 | assertNull(adaptor.getInterface(NON_EXISTENT_CP)); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the |
| 172 | * case that the input connect point is null. |
| 173 | * Verifies that a NullPointerException is thrown. |
| 174 | */ |
| 175 | @Test(expected = NullPointerException.class) |
| 176 | public void testGetInterfaceNull() { |
| 177 | adaptor.getInterface(null); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}. |
| 182 | * Verifies that the correct interface is returned based on the given IP |
| 183 | * address. |
| 184 | */ |
| 185 | @Test |
| 186 | public void testGetMatchingInterface() { |
| 187 | assertEquals(this.interfaces.get(CP1), |
| 188 | adaptor.getMatchingInterface(IpAddress.valueOf("192.168.1.100"))); |
| 189 | assertEquals(this.interfaces.get(CP2), |
| 190 | adaptor.getMatchingInterface(IpAddress.valueOf("192.168.2.100"))); |
| 191 | assertEquals(this.interfaces.get(CP3), |
| 192 | adaptor.getMatchingInterface(IpAddress.valueOf("192.168.3.100"))); |
| 193 | assertEquals(this.interfaces.get(CP3), |
| 194 | adaptor.getMatchingInterface(IpAddress.valueOf("192.168.4.100"))); |
| 195 | |
| 196 | // Try and match an address we don't have subnet configured for |
| 197 | assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1"))); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the |
| 202 | * case that the input IP address is null. |
| 203 | * Verifies that a NullPointerException is thrown. |
| 204 | */ |
| 205 | @Test(expected = NullPointerException.class) |
| 206 | public void testGetMatchingInterfaceNull() { |
| 207 | adaptor.getMatchingInterface(null); |
| 208 | } |
| 209 | |
| 210 | } |