blob: b4a7163a7e6369059815f50d8cd6239e4bbf7ddf [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(
Charles Chanee993b12017-08-14 13:32:49 -0700166 NetworkConfigEvent.Type.CONFIG_ADDED, cp, ic, null, CONFIG_CLASS);
Jonathan Hartb4ebbc32015-09-04 09:59:15 +0200167
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
Charles Chanee993b12017-08-14 13:32:49 -0700204 InterfaceConfig oldIc = new TestInterfaceConfig(cp, Sets.newHashSet(intf));
Jonathan Hartb4ebbc32015-09-04 09:59:15 +0200205 InterfaceConfig ic = new TestInterfaceConfig(cp, interfaces);
206
207 configs.put(cp, ic);
208
209 NetworkConfigEvent event = new NetworkConfigEvent(
Charles Chanee993b12017-08-14 13:32:49 -0700210 NetworkConfigEvent.Type.CONFIG_UPDATED, cp, ic, oldIc, CONFIG_CLASS);
Jonathan Hartb4ebbc32015-09-04 09:59:15 +0200211
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}