Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 19 | package org.onlab.onos.sdnip; |
| 20 | |
| 21 | import static org.easymock.EasyMock.createMock; |
| 22 | import static org.easymock.EasyMock.expect; |
| 23 | import static org.easymock.EasyMock.replay; |
| 24 | import static org.easymock.EasyMock.reset; |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertNull; |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.onlab.onos.net.ConnectPoint; |
| 35 | import org.onlab.onos.net.DeviceId; |
| 36 | import org.onlab.onos.net.PortNumber; |
| 37 | import org.onlab.onos.net.host.HostService; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.host.PortAddresses; |
| 40 | import org.onlab.onos.sdnip.config.Interface; |
| 41 | import org.onlab.packet.IpAddress; |
| 42 | import org.onlab.packet.IpPrefix; |
| 43 | import org.onlab.packet.MacAddress; |
| 44 | |
| 45 | import com.google.common.collect.Maps; |
| 46 | import com.google.common.collect.Sets; |
| 47 | |
| 48 | /** |
| 49 | * Unit tests for the HostToInterfaceAdaptor class. |
| 50 | */ |
| 51 | public class HostToInterfaceAdaptorTest { |
| 52 | |
| 53 | private HostService hostService; |
| 54 | private HostToInterfaceAdaptor adaptor; |
| 55 | |
| 56 | private Set<PortAddresses> portAddresses; |
| 57 | private Map<ConnectPoint, Interface> interfaces; |
| 58 | |
| 59 | private static final ConnectPoint CP1 = new ConnectPoint( |
| 60 | DeviceId.deviceId("of:1"), PortNumber.portNumber(1)); |
| 61 | private static final ConnectPoint CP2 = new ConnectPoint( |
| 62 | DeviceId.deviceId("of:1"), PortNumber.portNumber(2)); |
| 63 | private static final ConnectPoint CP3 = new ConnectPoint( |
| 64 | DeviceId.deviceId("of:2"), PortNumber.portNumber(1)); |
| 65 | |
| 66 | private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint( |
| 67 | DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1)); |
| 68 | |
| 69 | private static final PortAddresses DEFAULT_PA = new PortAddresses( |
| 70 | NON_EXISTENT_CP, null, null); |
| 71 | |
| 72 | |
| 73 | @Before |
| 74 | public void setUp() throws Exception { |
| 75 | hostService = createMock(HostService.class); |
| 76 | |
| 77 | portAddresses = Sets.newHashSet(); |
| 78 | interfaces = Maps.newHashMap(); |
| 79 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 80 | InterfaceIpAddress ia11 = |
| 81 | new InterfaceIpAddress(IpAddress.valueOf("192.168.1.1"), |
| 82 | IpPrefix.valueOf("192.168.1.0/24")); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 83 | createPortAddressesAndInterface(CP1, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 84 | Sets.newHashSet(ia11), |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 85 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 86 | |
| 87 | // Two addresses in the same subnet |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 88 | InterfaceIpAddress ia21 = |
| 89 | new InterfaceIpAddress(IpAddress.valueOf("192.168.2.1"), |
| 90 | IpPrefix.valueOf("192.168.2.0/24")); |
| 91 | InterfaceIpAddress ia22 = |
| 92 | new InterfaceIpAddress(IpAddress.valueOf("192.168.2.2"), |
| 93 | IpPrefix.valueOf("192.168.2.0/24")); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 94 | createPortAddressesAndInterface(CP2, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 95 | Sets.newHashSet(ia21, ia22), |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 96 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 97 | |
| 98 | // Two addresses in different subnets |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 99 | InterfaceIpAddress ia31 = |
| 100 | new InterfaceIpAddress(IpAddress.valueOf("192.168.3.1"), |
| 101 | IpPrefix.valueOf("192.168.3.0/24")); |
| 102 | InterfaceIpAddress ia41 = |
| 103 | new InterfaceIpAddress(IpAddress.valueOf("192.168.4.1"), |
| 104 | IpPrefix.valueOf("192.168.4.0/24")); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 105 | createPortAddressesAndInterface(CP3, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 106 | Sets.newHashSet(ia31, ia41), |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 107 | MacAddress.valueOf("00:00:00:00:00:03")); |
| 108 | |
| 109 | expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes(); |
| 110 | |
| 111 | replay(hostService); |
| 112 | |
| 113 | adaptor = new HostToInterfaceAdaptor(hostService); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Creates both a PortAddresses and an Interface for the given inputs and |
| 118 | * places them in the correct global data stores. |
| 119 | * |
| 120 | * @param cp the connect point |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 121 | * @param ipAddresses the set of interface IP addresses |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 122 | * @param mac the MAC address |
| 123 | */ |
| 124 | private void createPortAddressesAndInterface( |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 125 | ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses, |
| 126 | MacAddress mac) { |
| 127 | PortAddresses pa = new PortAddresses(cp, ipAddresses, mac); |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 128 | portAddresses.add(pa); |
| 129 | expect(hostService.getAddressBindingsForPort(cp)).andReturn(pa).anyTimes(); |
| 130 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 131 | Interface intf = new Interface(cp, ipAddresses, mac); |
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)) |
| 164 | .andReturn(DEFAULT_PA).anyTimes(); |
| 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 | } |