Jonathan Hart | b4ebbc3 | 2015-09-04 09:59:15 +0200 | [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 | |
| 17 | package org.onosproject.incubator.net.intf.impl; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 28 | import org.onosproject.incubator.net.config.basics.InterfaceConfig; |
| 29 | import org.onosproject.incubator.net.intf.Interface; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.config.Config; |
| 32 | import org.onosproject.net.config.NetworkConfigEvent; |
| 33 | import org.onosproject.net.config.NetworkConfigListener; |
| 34 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
| 35 | import org.onosproject.net.host.InterfaceIpAddress; |
| 36 | |
| 37 | import java.util.Collections; |
| 38 | import java.util.Map; |
| 39 | import java.util.Set; |
| 40 | |
| 41 | import static junit.framework.TestCase.assertEquals; |
| 42 | import static org.junit.Assert.assertNull; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for InterfaceManager. |
| 46 | */ |
| 47 | public class InterfaceManagerTest { |
| 48 | private static final Class<InterfaceConfig> CONFIG_CLASS = InterfaceConfig.class; |
| 49 | |
| 50 | private static final int NUM_INTERFACES = 4; |
| 51 | |
| 52 | private Set<ConnectPoint> subjects = Sets.newHashSet(); |
| 53 | private Map<ConnectPoint, InterfaceConfig> configs = Maps.newHashMap(); |
| 54 | |
| 55 | private Set<Interface> interfaces = Sets.newHashSet(); |
| 56 | |
| 57 | private NetworkConfigListener listener; |
| 58 | |
| 59 | private InterfaceManager interfaceManager; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() throws Exception { |
| 63 | for (int i = 0; i < NUM_INTERFACES; i++) { |
| 64 | ConnectPoint cp = createConnectPoint(i); |
| 65 | subjects.add(cp); |
| 66 | |
| 67 | Interface intf = createInterface(i); |
| 68 | |
| 69 | interfaces.add(intf); |
| 70 | |
| 71 | InterfaceConfig ic = new TestInterfaceConfig(cp, Sets.newHashSet(intf)); |
| 72 | |
| 73 | configs.put(cp, ic); |
| 74 | } |
| 75 | |
| 76 | TestNetworkConfigService configService = |
| 77 | new TestNetworkConfigService(subjects, configs); |
| 78 | |
| 79 | interfaceManager = new InterfaceManager(); |
| 80 | interfaceManager.configService = configService; |
| 81 | interfaceManager.activate(); |
| 82 | } |
| 83 | |
| 84 | private Interface createInterface(int i) { |
| 85 | ConnectPoint cp = createConnectPoint(i); |
| 86 | |
| 87 | InterfaceIpAddress ia = InterfaceIpAddress.valueOf("192.168." + i + ".1/24"); |
| 88 | |
| 89 | Interface intf = new Interface(cp, |
| 90 | Sets.newHashSet(ia), |
| 91 | MacAddress.valueOf(i), |
| 92 | VlanId.vlanId((short) i)); |
| 93 | |
| 94 | return intf; |
| 95 | } |
| 96 | |
| 97 | private ConnectPoint createConnectPoint(int i) { |
| 98 | return ConnectPoint.deviceConnectPoint("of:000000000000000" + i + "/1"); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testGetInterfaces() throws Exception { |
| 103 | assertEquals(interfaces, interfaceManager.getInterfaces()); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testGetInterfacesByPort() throws Exception { |
| 108 | ConnectPoint cp = ConnectPoint.deviceConnectPoint("of:0000000000000001/1"); |
| 109 | |
| 110 | Set<Interface> byPort = Collections.singleton(createInterface(1)); |
| 111 | |
| 112 | assertEquals(byPort, interfaceManager.getInterfacesByPort(cp)); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testGetInterfacesByIp() throws Exception { |
| 117 | IpAddress ip = Ip4Address.valueOf("192.168.2.1"); |
| 118 | |
| 119 | Set<Interface> byIp = Collections.singleton(createInterface(2)); |
| 120 | |
| 121 | assertEquals(byIp, interfaceManager.getInterfacesByIp(ip)); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testGetMatchingInterface() throws Exception { |
| 126 | IpAddress ip = Ip4Address.valueOf("192.168.1.100"); |
| 127 | |
| 128 | Interface matchingIntf = createInterface(1); |
| 129 | |
| 130 | assertEquals(matchingIntf, interfaceManager.getMatchingInterface(ip)); |
| 131 | |
| 132 | // Searching for an IP with no match should return null |
| 133 | ip = Ip4Address.valueOf("1.1.1.1"); |
| 134 | |
| 135 | assertNull(interfaceManager.getMatchingInterface(ip)); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testGetInterfacesByVlan() throws Exception { |
| 140 | VlanId vlanId = VlanId.vlanId((short) 1); |
| 141 | |
| 142 | Set<Interface> byVlan = Collections.singleton(createInterface(1)); |
| 143 | |
| 144 | assertEquals(byVlan, interfaceManager.getInterfacesByVlan(vlanId)); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testAddInterface() throws Exception { |
| 149 | // Create a new InterfaceConfig which will get added |
| 150 | VlanId vlanId = VlanId.vlanId((short) 1); |
| 151 | ConnectPoint cp = ConnectPoint.deviceConnectPoint("of:0000000000000001/2"); |
| 152 | Interface newIntf = new Interface(cp, |
| 153 | Collections.emptySet(), |
| 154 | MacAddress.valueOf(100), |
| 155 | vlanId); |
| 156 | |
| 157 | InterfaceConfig ic = new TestInterfaceConfig(cp, Collections.singleton(newIntf)); |
| 158 | |
| 159 | subjects.add(cp); |
| 160 | configs.put(cp, ic); |
| 161 | interfaces.add(newIntf); |
| 162 | |
| 163 | NetworkConfigEvent event = new NetworkConfigEvent( |
| 164 | NetworkConfigEvent.Type.CONFIG_ADDED, cp, CONFIG_CLASS); |
| 165 | |
| 166 | assertEquals(NUM_INTERFACES, interfaceManager.getInterfaces().size()); |
| 167 | |
| 168 | // Send in a config event containing a new interface config |
| 169 | listener.event(event); |
| 170 | |
| 171 | // Check the new interface exists in the InterfaceManager's inventory |
| 172 | assertEquals(interfaces, interfaceManager.getInterfaces()); |
| 173 | assertEquals(NUM_INTERFACES + 1, interfaceManager.getInterfaces().size()); |
| 174 | |
| 175 | // There are now two interfaces with vlan ID 1 |
| 176 | Set<Interface> byVlan = Sets.newHashSet(createInterface(1), newIntf); |
| 177 | assertEquals(byVlan, interfaceManager.getInterfacesByVlan(vlanId)); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testUpdateInterface() throws Exception { |
| 182 | ConnectPoint cp = createConnectPoint(1); |
| 183 | |
| 184 | // Create an interface that is the same as the existing one, but adds a |
| 185 | // new IP address |
| 186 | Interface intf = createInterface(1); |
| 187 | Set<InterfaceIpAddress> addresses = Sets.newHashSet(intf.ipAddresses()); |
| 188 | addresses.add(InterfaceIpAddress.valueOf("192.168.100.1/24")); |
| 189 | intf = new Interface(intf.connectPoint(), addresses, intf.mac(), intf.vlan()); |
| 190 | |
| 191 | // Create a new interface on the same connect point as the existing one |
| 192 | InterfaceIpAddress newAddr = InterfaceIpAddress.valueOf("192.168.101.1/24"); |
| 193 | Interface newIntf = new Interface(cp, |
| 194 | Collections.singleton(newAddr), |
| 195 | MacAddress.valueOf(101), |
| 196 | VlanId.vlanId((short) 101)); |
| 197 | |
| 198 | Set<Interface> interfaces = Sets.newHashSet(intf, newIntf); |
| 199 | |
| 200 | // New interface config updates the existing interface and adds a new |
| 201 | // interface to the same connect point |
| 202 | InterfaceConfig ic = new TestInterfaceConfig(cp, interfaces); |
| 203 | |
| 204 | configs.put(cp, ic); |
| 205 | |
| 206 | NetworkConfigEvent event = new NetworkConfigEvent( |
| 207 | NetworkConfigEvent.Type.CONFIG_UPDATED, cp, CONFIG_CLASS); |
| 208 | |
| 209 | // Send in the event signalling the interfaces for this connect point |
| 210 | // have been updated |
| 211 | listener.event(event); |
| 212 | |
| 213 | assertEquals(NUM_INTERFACES + 1, interfaceManager.getInterfaces().size()); |
| 214 | assertEquals(interfaces, interfaceManager.getInterfacesByPort(cp)); |
| 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void testRemoveInterface() throws Exception { |
| 219 | ConnectPoint cp = createConnectPoint(1); |
| 220 | |
| 221 | NetworkConfigEvent event = new NetworkConfigEvent( |
| 222 | NetworkConfigEvent.Type.CONFIG_REMOVED, cp, CONFIG_CLASS); |
| 223 | |
| 224 | assertEquals(NUM_INTERFACES, interfaceManager.getInterfaces().size()); |
| 225 | |
| 226 | // Send in a config event removing an interface config |
| 227 | listener.event(event); |
| 228 | |
| 229 | assertEquals(NUM_INTERFACES - 1, interfaceManager.getInterfaces().size()); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Test version of NetworkConfigService which allows us to pass in subjects |
| 234 | * and InterfaceConfigs directly. |
| 235 | */ |
| 236 | private class TestNetworkConfigService extends NetworkConfigServiceAdapter { |
| 237 | private final Set<ConnectPoint> subjects; |
| 238 | private final Map<ConnectPoint, InterfaceConfig> configs; |
| 239 | |
| 240 | public TestNetworkConfigService(Set<ConnectPoint> subjects, |
| 241 | Map<ConnectPoint, InterfaceConfig> configs) { |
| 242 | this.subjects = subjects; |
| 243 | this.configs = configs; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, |
| 248 | Class<C> configClass) { |
| 249 | return (Set<S>) subjects; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 254 | return (C) configs.get(subject); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void addListener(NetworkConfigListener listener) { |
| 259 | InterfaceManagerTest.this.listener = listener; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Test version of InterfaceConfig where we can inject interfaces directly, |
| 265 | * rather than parsing them from JSON. |
| 266 | */ |
| 267 | private class TestInterfaceConfig extends InterfaceConfig { |
| 268 | private final ConnectPoint subject; |
| 269 | private final Set<Interface> interfaces; |
| 270 | |
| 271 | @Override |
| 272 | public ConnectPoint subject() { |
| 273 | return subject; |
| 274 | } |
| 275 | |
| 276 | public TestInterfaceConfig(ConnectPoint subject, Set<Interface> interfaces) { |
| 277 | this.subject = subject; |
| 278 | this.interfaces = interfaces; |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public Set<Interface> getInterfaces() throws ConfigException { |
| 283 | return interfaces; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | } |