blob: 5a3dc51fb03613874cca969f2fe2e96433f036e3 [file] [log] [blame]
Jonathan Hart4c2b15e2014-10-20 13:10:56 -07001package org.onlab.onos.sdnip;
2
3import static org.easymock.EasyMock.createMock;
4import static org.easymock.EasyMock.expect;
5import static org.easymock.EasyMock.replay;
6import static org.easymock.EasyMock.reset;
7import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertNull;
9import static org.junit.Assert.assertTrue;
10
11import java.util.Map;
12import java.util.Set;
13
14import org.junit.Before;
15import org.junit.Test;
16import org.onlab.onos.net.ConnectPoint;
17import org.onlab.onos.net.DeviceId;
18import org.onlab.onos.net.PortNumber;
19import org.onlab.onos.net.host.HostService;
20import org.onlab.onos.net.host.PortAddresses;
21import org.onlab.onos.sdnip.config.Interface;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
25
26import com.google.common.collect.Maps;
27import com.google.common.collect.Sets;
28
29/**
30 * Unit tests for the HostToInterfaceAdaptor class.
31 */
32public class HostToInterfaceAdaptorTest {
33
34 private HostService hostService;
35 private HostToInterfaceAdaptor adaptor;
36
37 private Set<PortAddresses> portAddresses;
38 private Map<ConnectPoint, Interface> interfaces;
39
40 private static final ConnectPoint CP1 = new ConnectPoint(
41 DeviceId.deviceId("of:1"), PortNumber.portNumber(1));
42 private static final ConnectPoint CP2 = new ConnectPoint(
43 DeviceId.deviceId("of:1"), PortNumber.portNumber(2));
44 private static final ConnectPoint CP3 = new ConnectPoint(
45 DeviceId.deviceId("of:2"), PortNumber.portNumber(1));
46
47 private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint(
48 DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1));
49
50 private static final PortAddresses DEFAULT_PA = new PortAddresses(
51 NON_EXISTENT_CP, null, null);
52
53
54 @Before
55 public void setUp() throws Exception {
56 hostService = createMock(HostService.class);
57
58 portAddresses = Sets.newHashSet();
59 interfaces = Maps.newHashMap();
60
61 createPortAddressesAndInterface(CP1,
62 Sets.newHashSet(IpPrefix.valueOf("192.168.1.1/24")),
63 MacAddress.valueOf("00:00:00:00:00:01"));
64
65 // Two addresses in the same subnet
66 createPortAddressesAndInterface(CP2,
67 Sets.newHashSet(IpPrefix.valueOf("192.168.2.1/24"),
68 IpPrefix.valueOf("192.168.2.2/24")),
69 MacAddress.valueOf("00:00:00:00:00:02"));
70
71 // Two addresses in different subnets
72 createPortAddressesAndInterface(CP3,
73 Sets.newHashSet(IpPrefix.valueOf("192.168.3.1/24"),
74 IpPrefix.valueOf("192.168.4.1/24")),
75 MacAddress.valueOf("00:00:00:00:00:03"));
76
77 expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
78
79 replay(hostService);
80
81 adaptor = new HostToInterfaceAdaptor(hostService);
82 }
83
84 /**
85 * Creates both a PortAddresses and an Interface for the given inputs and
86 * places them in the correct global data stores.
87 *
88 * @param cp the connect point
89 * @param ips the set of IP addresses
90 * @param mac the MAC address
91 */
92 private void createPortAddressesAndInterface(
93 ConnectPoint cp, Set<IpPrefix> ips, MacAddress mac) {
94 PortAddresses pa = new PortAddresses(cp, ips, mac);
95 portAddresses.add(pa);
96 expect(hostService.getAddressBindingsForPort(cp)).andReturn(pa).anyTimes();
97
98 Interface intf = new Interface(cp, ips, mac);
99 interfaces.put(cp, intf);
100 }
101
102 /**
103 * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
104 * Verifies that the set of interfaces returned matches what is expected
105 * based on the input PortAddresses data.
106 */
107 @Test
108 public void testGetInterfaces() {
109 Set<Interface> adaptorIntfs = adaptor.getInterfaces();
110
111 assertEquals(3, adaptorIntfs.size());
112 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1)));
113 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2)));
114 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3)));
115 }
116
117 /**
118 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
119 * Verifies that the correct interface is returned for a given connect
120 * point.
121 */
122 @Test
123 public void testGetInterface() {
124 assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1));
125 assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2));
126 assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3));
127
128 // Try and get an interface for a connect point with no addresses
129 reset(hostService);
130 expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
131 .andReturn(DEFAULT_PA).anyTimes();
132 replay(hostService);
133
134 assertNull(adaptor.getInterface(NON_EXISTENT_CP));
135 }
136
137 /**
138 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
139 * case that the input connect point is null.
140 * Verifies that a NullPointerException is thrown.
141 */
142 @Test(expected = NullPointerException.class)
143 public void testGetInterfaceNull() {
144 adaptor.getInterface(null);
145 }
146
147 /**
148 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}.
149 * Verifies that the correct interface is returned based on the given IP
150 * address.
151 */
152 @Test
153 public void testGetMatchingInterface() {
154 assertEquals(this.interfaces.get(CP1),
155 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.1.100")));
156 assertEquals(this.interfaces.get(CP2),
157 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.2.100")));
158 assertEquals(this.interfaces.get(CP3),
159 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.3.100")));
160 assertEquals(this.interfaces.get(CP3),
161 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.4.100")));
162
163 // Try and match an address we don't have subnet configured for
164 assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1")));
165 }
166
167 /**
168 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the
169 * case that the input IP address is null.
170 * Verifies that a NullPointerException is thrown.
171 */
172 @Test(expected = NullPointerException.class)
173 public void testGetMatchingInterfaceNull() {
174 adaptor.getMatchingInterface(null);
175 }
176
177}