blob: 89ca4ee9aa9df300a577517590d7e5feb231a320 [file] [log] [blame]
Jonathan Hartb4ebbc32015-09-04 09:59:15 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hartb4ebbc32015-09-04 09:59:15 +02003 *
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 Milkeyfacf2862017-08-03 11:58:29 -070017package org.onosproject.net.intf.impl;
Jonathan Hartb4ebbc32015-09-04 09:59:15 +020018
Jonathan Hart00cddda2016-02-16 10:30:37 -080019import com.google.common.collect.Lists;
Jonathan Hartb4ebbc32015-09-04 09:59:15 +020020import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.Ip4Address;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
28import org.onosproject.incubator.net.config.basics.ConfigException;
29import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Ray Milkeyfacf2862017-08-03 11:58:29 -070030import org.onosproject.net.intf.Interface;
Jonathan Hartb4ebbc32015-09-04 09:59:15 +020031import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.config.Config;
33import org.onosproject.net.config.NetworkConfigEvent;
34import org.onosproject.net.config.NetworkConfigListener;
35import org.onosproject.net.config.NetworkConfigServiceAdapter;
36import org.onosproject.net.host.InterfaceIpAddress;
37
38import java.util.Collections;
Jonathan Hart00cddda2016-02-16 10:30:37 -080039import java.util.List;
Jonathan Hartb4ebbc32015-09-04 09:59:15 +020040import java.util.Map;
41import java.util.Set;
42
43import static junit.framework.TestCase.assertEquals;
44import static org.junit.Assert.assertNull;
45
46/**
47 * Unit tests for InterfaceManager.
48 */
49public 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 SHIMIZU891162b2016-06-06 16:32:01 -070091 Interface intf = new Interface(Interface.NO_INTERFACE_NAME, cp,
Jonathan Hart00cddda2016-02-16 10:30:37 -080092 Collections.singletonList(ia),
Jonathan Hartb4ebbc32015-09-04 09:59:15 +020093 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 SHIMIZU891162b2016-06-06 16:32:01 -0700154 Interface newIntf = new Interface(Interface.NO_INTERFACE_NAME, cp,
Jonathan Hart00cddda2016-02-16 10:30:37 -0800155 Collections.emptyList(),
Jonathan Hartb4ebbc32015-09-04 09:59:15 +0200156 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(
166 NetworkConfigEvent.Type.CONFIG_ADDED, cp, CONFIG_CLASS);
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 Milkey048bf9a2017-05-12 14:31:50 -0700189 List<InterfaceIpAddress> addresses = Lists.newArrayList(intf.ipAddressesList());
Jonathan Hartb4ebbc32015-09-04 09:59:15 +0200190 addresses.add(InterfaceIpAddress.valueOf("192.168.100.1/24"));
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700191 intf = new Interface(Interface.NO_INTERFACE_NAME, intf.connectPoint(), addresses, intf.mac(), intf.vlan());
Jonathan Hartb4ebbc32015-09-04 09:59:15 +0200192
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 SHIMIZU891162b2016-06-06 16:32:01 -0700195 Interface newIntf = new Interface(Interface.NO_INTERFACE_NAME, cp,
Jonathan Hart00cddda2016-02-16 10:30:37 -0800196 Collections.singletonList(newAddr),
Jonathan Hartb4ebbc32015-09-04 09:59:15 +0200197 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
204 InterfaceConfig ic = new TestInterfaceConfig(cp, interfaces);
205
206 configs.put(cp, ic);
207
208 NetworkConfigEvent event = new NetworkConfigEvent(
209 NetworkConfigEvent.Type.CONFIG_UPDATED, cp, CONFIG_CLASS);
210
211 // Send in the event signalling the interfaces for this connect point
212 // have been updated
213 listener.event(event);
214
215 assertEquals(NUM_INTERFACES + 1, interfaceManager.getInterfaces().size());
216 assertEquals(interfaces, interfaceManager.getInterfacesByPort(cp));
217 }
218
219 @Test
220 public void testRemoveInterface() throws Exception {
221 ConnectPoint cp = createConnectPoint(1);
222
223 NetworkConfigEvent event = new NetworkConfigEvent(
224 NetworkConfigEvent.Type.CONFIG_REMOVED, cp, CONFIG_CLASS);
225
226 assertEquals(NUM_INTERFACES, interfaceManager.getInterfaces().size());
227
228 // Send in a config event removing an interface config
229 listener.event(event);
230
231 assertEquals(NUM_INTERFACES - 1, interfaceManager.getInterfaces().size());
232 }
233
234 /**
235 * Test version of NetworkConfigService which allows us to pass in subjects
236 * and InterfaceConfigs directly.
237 */
238 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
239 private final Set<ConnectPoint> subjects;
240 private final Map<ConnectPoint, InterfaceConfig> configs;
241
242 public TestNetworkConfigService(Set<ConnectPoint> subjects,
243 Map<ConnectPoint, InterfaceConfig> configs) {
244 this.subjects = subjects;
245 this.configs = configs;
246 }
247
248 @Override
249 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass,
250 Class<C> configClass) {
251 return (Set<S>) subjects;
252 }
253
254 @Override
255 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
256 return (C) configs.get(subject);
257 }
258
259 @Override
260 public void addListener(NetworkConfigListener listener) {
261 InterfaceManagerTest.this.listener = listener;
262 }
263 }
264
265 /**
266 * Test version of InterfaceConfig where we can inject interfaces directly,
267 * rather than parsing them from JSON.
268 */
269 private class TestInterfaceConfig extends InterfaceConfig {
270 private final ConnectPoint subject;
271 private final Set<Interface> interfaces;
272
273 @Override
274 public ConnectPoint subject() {
275 return subject;
276 }
277
278 public TestInterfaceConfig(ConnectPoint subject, Set<Interface> interfaces) {
279 this.subject = subject;
280 this.interfaces = interfaces;
281 }
282
283 @Override
284 public Set<Interface> getInterfaces() throws ConfigException {
285 return interfaces;
286 }
287 }
288
289}