blob: 096e63cc7892c5ac072bbb2c1e44f2a1c23caa4d [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070017
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
Jonathan Harta887ba82014-11-03 15:20:52 -080026import java.util.Collections;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070027import java.util.Map;
28import java.util.Set;
29
30import org.junit.Before;
31import org.junit.Test;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080032import org.onlab.packet.IpAddress;
33import org.onlab.packet.IpPrefix;
34import org.onlab.packet.MacAddress;
35import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.host.HostService;
40import org.onosproject.net.host.InterfaceIpAddress;
41import org.onosproject.net.host.PortAddresses;
42import org.onosproject.sdnip.config.Interface;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070043
44import com.google.common.collect.Maps;
45import com.google.common.collect.Sets;
46
47/**
48 * Unit tests for the HostToInterfaceAdaptor class.
49 */
50public class HostToInterfaceAdaptorTest {
51
52 private HostService hostService;
53 private HostToInterfaceAdaptor adaptor;
54
55 private Set<PortAddresses> portAddresses;
56 private Map<ConnectPoint, Interface> interfaces;
57
58 private static final ConnectPoint CP1 = new ConnectPoint(
59 DeviceId.deviceId("of:1"), PortNumber.portNumber(1));
60 private static final ConnectPoint CP2 = new ConnectPoint(
61 DeviceId.deviceId("of:1"), PortNumber.portNumber(2));
62 private static final ConnectPoint CP3 = new ConnectPoint(
63 DeviceId.deviceId("of:2"), PortNumber.portNumber(1));
64
65 private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint(
66 DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1));
67
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070068 @Before
69 public void setUp() throws Exception {
70 hostService = createMock(HostService.class);
71
72 portAddresses = Sets.newHashSet();
73 interfaces = Maps.newHashMap();
74
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070075 InterfaceIpAddress ia11 =
76 new InterfaceIpAddress(IpAddress.valueOf("192.168.1.1"),
77 IpPrefix.valueOf("192.168.1.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070078 createPortAddressesAndInterface(CP1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070079 Sets.newHashSet(ia11),
Jonathan Hart6cd2f352015-01-13 17:44:45 -080080 MacAddress.valueOf("00:00:00:00:00:01"),
81 VlanId.NONE);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070082
83 // Two addresses in the same subnet
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070084 InterfaceIpAddress ia21 =
85 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.1"),
86 IpPrefix.valueOf("192.168.2.0/24"));
87 InterfaceIpAddress ia22 =
88 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.2"),
89 IpPrefix.valueOf("192.168.2.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070090 createPortAddressesAndInterface(CP2,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070091 Sets.newHashSet(ia21, ia22),
Jonathan Hart6cd2f352015-01-13 17:44:45 -080092 MacAddress.valueOf("00:00:00:00:00:02"),
93 VlanId.vlanId((short) 4));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070094
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 Hart6cd2f352015-01-13 17:44:45 -0800104 MacAddress.valueOf("00:00:00:00:00:03"),
105 VlanId.NONE);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700106
107 expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
108
109 replay(hostService);
110
111 adaptor = new HostToInterfaceAdaptor(hostService);
112 }
113
114 /**
115 * Creates both a PortAddresses and an Interface for the given inputs and
116 * places them in the correct global data stores.
117 *
118 * @param cp the connect point
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700119 * @param ipAddresses the set of interface IP addresses
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700120 * @param mac the MAC address
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800121 * @param vlan the VLAN ID
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700122 */
123 private void createPortAddressesAndInterface(
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700124 ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800125 MacAddress mac, VlanId vlan) {
126 PortAddresses pa = new PortAddresses(cp, ipAddresses, mac, vlan);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700127 portAddresses.add(pa);
Jonathan Harta887ba82014-11-03 15:20:52 -0800128 expect(hostService.getAddressBindingsForPort(cp)).andReturn(
129 Collections.singleton(pa)).anyTimes();
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700130
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800131 Interface intf = new Interface(cp, ipAddresses, mac, vlan);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700132 interfaces.put(cp, intf);
133 }
134
135 /**
136 * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
137 * Verifies that the set of interfaces returned matches what is expected
138 * based on the input PortAddresses data.
139 */
140 @Test
141 public void testGetInterfaces() {
142 Set<Interface> adaptorIntfs = adaptor.getInterfaces();
143
144 assertEquals(3, adaptorIntfs.size());
145 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1)));
146 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2)));
147 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3)));
148 }
149
150 /**
151 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
152 * Verifies that the correct interface is returned for a given connect
153 * point.
154 */
155 @Test
156 public void testGetInterface() {
157 assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1));
158 assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2));
159 assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3));
160
161 // Try and get an interface for a connect point with no addresses
162 reset(hostService);
163 expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
Jonathan Harta887ba82014-11-03 15:20:52 -0800164 .andReturn(Collections.<PortAddresses>emptySet()).anyTimes();
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700165 replay(hostService);
166
167 assertNull(adaptor.getInterface(NON_EXISTENT_CP));
168 }
169
170 /**
171 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
172 * case that the input connect point is null.
173 * Verifies that a NullPointerException is thrown.
174 */
175 @Test(expected = NullPointerException.class)
176 public void testGetInterfaceNull() {
177 adaptor.getInterface(null);
178 }
179
180 /**
181 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}.
182 * Verifies that the correct interface is returned based on the given IP
183 * address.
184 */
185 @Test
186 public void testGetMatchingInterface() {
187 assertEquals(this.interfaces.get(CP1),
188 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.1.100")));
189 assertEquals(this.interfaces.get(CP2),
190 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.2.100")));
191 assertEquals(this.interfaces.get(CP3),
192 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.3.100")));
193 assertEquals(this.interfaces.get(CP3),
194 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.4.100")));
195
196 // Try and match an address we don't have subnet configured for
197 assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1")));
198 }
199
200 /**
201 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the
202 * case that the input IP address is null.
203 * Verifies that a NullPointerException is thrown.
204 */
205 @Test(expected = NullPointerException.class)
206 public void testGetMatchingInterfaceNull() {
207 adaptor.getMatchingInterface(null);
208 }
209
210}