blob: 2a65616dd2c96b33669932f56a8c68d0351fe1bd [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070016package org.onlab.onos.sdnip;
17
18import static org.easymock.EasyMock.createMock;
19import static org.easymock.EasyMock.expect;
20import static org.easymock.EasyMock.replay;
21import static org.easymock.EasyMock.reset;
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertTrue;
25
26import java.util.Map;
27import java.util.Set;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.onlab.onos.net.ConnectPoint;
32import org.onlab.onos.net.DeviceId;
33import org.onlab.onos.net.PortNumber;
34import org.onlab.onos.net.host.HostService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070035import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070036import org.onlab.onos.net.host.PortAddresses;
37import org.onlab.onos.sdnip.config.Interface;
38import org.onlab.packet.IpAddress;
39import org.onlab.packet.IpPrefix;
40import org.onlab.packet.MacAddress;
41
42import com.google.common.collect.Maps;
43import com.google.common.collect.Sets;
44
45/**
46 * Unit tests for the HostToInterfaceAdaptor class.
47 */
48public class HostToInterfaceAdaptorTest {
49
50 private HostService hostService;
51 private HostToInterfaceAdaptor adaptor;
52
53 private Set<PortAddresses> portAddresses;
54 private Map<ConnectPoint, Interface> interfaces;
55
56 private static final ConnectPoint CP1 = new ConnectPoint(
57 DeviceId.deviceId("of:1"), PortNumber.portNumber(1));
58 private static final ConnectPoint CP2 = new ConnectPoint(
59 DeviceId.deviceId("of:1"), PortNumber.portNumber(2));
60 private static final ConnectPoint CP3 = new ConnectPoint(
61 DeviceId.deviceId("of:2"), PortNumber.portNumber(1));
62
63 private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint(
64 DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1));
65
66 private static final PortAddresses DEFAULT_PA = new PortAddresses(
67 NON_EXISTENT_CP, null, null);
68
69
70 @Before
71 public void setUp() throws Exception {
72 hostService = createMock(HostService.class);
73
74 portAddresses = Sets.newHashSet();
75 interfaces = Maps.newHashMap();
76
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070077 InterfaceIpAddress ia11 =
78 new InterfaceIpAddress(IpAddress.valueOf("192.168.1.1"),
79 IpPrefix.valueOf("192.168.1.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070080 createPortAddressesAndInterface(CP1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070081 Sets.newHashSet(ia11),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070082 MacAddress.valueOf("00:00:00:00:00:01"));
83
84 // Two addresses in the same subnet
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070085 InterfaceIpAddress ia21 =
86 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.1"),
87 IpPrefix.valueOf("192.168.2.0/24"));
88 InterfaceIpAddress ia22 =
89 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.2"),
90 IpPrefix.valueOf("192.168.2.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070091 createPortAddressesAndInterface(CP2,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070092 Sets.newHashSet(ia21, ia22),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070093 MacAddress.valueOf("00:00:00:00:00:02"));
94
95 // Two addresses in different subnets
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070096 InterfaceIpAddress ia31 =
97 new InterfaceIpAddress(IpAddress.valueOf("192.168.3.1"),
98 IpPrefix.valueOf("192.168.3.0/24"));
99 InterfaceIpAddress ia41 =
100 new InterfaceIpAddress(IpAddress.valueOf("192.168.4.1"),
101 IpPrefix.valueOf("192.168.4.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700102 createPortAddressesAndInterface(CP3,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700103 Sets.newHashSet(ia31, ia41),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700104 MacAddress.valueOf("00:00:00:00:00:03"));
105
106 expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
107
108 replay(hostService);
109
110 adaptor = new HostToInterfaceAdaptor(hostService);
111 }
112
113 /**
114 * Creates both a PortAddresses and an Interface for the given inputs and
115 * places them in the correct global data stores.
116 *
117 * @param cp the connect point
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700118 * @param ipAddresses the set of interface IP addresses
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700119 * @param mac the MAC address
120 */
121 private void createPortAddressesAndInterface(
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700122 ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses,
123 MacAddress mac) {
124 PortAddresses pa = new PortAddresses(cp, ipAddresses, mac);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700125 portAddresses.add(pa);
126 expect(hostService.getAddressBindingsForPort(cp)).andReturn(pa).anyTimes();
127
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700128 Interface intf = new Interface(cp, ipAddresses, mac);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700129 interfaces.put(cp, intf);
130 }
131
132 /**
133 * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
134 * Verifies that the set of interfaces returned matches what is expected
135 * based on the input PortAddresses data.
136 */
137 @Test
138 public void testGetInterfaces() {
139 Set<Interface> adaptorIntfs = adaptor.getInterfaces();
140
141 assertEquals(3, adaptorIntfs.size());
142 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1)));
143 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2)));
144 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3)));
145 }
146
147 /**
148 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
149 * Verifies that the correct interface is returned for a given connect
150 * point.
151 */
152 @Test
153 public void testGetInterface() {
154 assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1));
155 assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2));
156 assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3));
157
158 // Try and get an interface for a connect point with no addresses
159 reset(hostService);
160 expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
161 .andReturn(DEFAULT_PA).anyTimes();
162 replay(hostService);
163
164 assertNull(adaptor.getInterface(NON_EXISTENT_CP));
165 }
166
167 /**
168 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
169 * case that the input connect point is null.
170 * Verifies that a NullPointerException is thrown.
171 */
172 @Test(expected = NullPointerException.class)
173 public void testGetInterfaceNull() {
174 adaptor.getInterface(null);
175 }
176
177 /**
178 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}.
179 * Verifies that the correct interface is returned based on the given IP
180 * address.
181 */
182 @Test
183 public void testGetMatchingInterface() {
184 assertEquals(this.interfaces.get(CP1),
185 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.1.100")));
186 assertEquals(this.interfaces.get(CP2),
187 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.2.100")));
188 assertEquals(this.interfaces.get(CP3),
189 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.3.100")));
190 assertEquals(this.interfaces.get(CP3),
191 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.4.100")));
192
193 // Try and match an address we don't have subnet configured for
194 assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1")));
195 }
196
197 /**
198 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the
199 * case that the input IP address is null.
200 * Verifies that a NullPointerException is thrown.
201 */
202 @Test(expected = NullPointerException.class)
203 public void testGetMatchingInterfaceNull() {
204 adaptor.getMatchingInterface(null);
205 }
206
207}