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