blob: 0347fc5f8516c9188e8a6307483d6700d3625bbc [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing.config.impl;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070017
Jonathan Hart90a02c22015-02-13 11:52:07 -080018import com.google.common.collect.Maps;
19import com.google.common.collect.Sets;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070020import org.junit.Before;
21import org.junit.Test;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080022import org.onlab.packet.IpAddress;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.host.HostService;
30import org.onosproject.net.host.InterfaceIpAddress;
31import org.onosproject.net.host.PortAddresses;
Jonathan Hart2da1e602015-02-18 19:09:24 -080032import org.onosproject.routing.config.Interface;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070033
Jonathan Hart90a02c22015-02-13 11:52:07 -080034import java.util.Collections;
35import java.util.Map;
36import java.util.Set;
37
38import static org.easymock.EasyMock.createMock;
39import static org.easymock.EasyMock.expect;
40import static org.easymock.EasyMock.replay;
41import static org.easymock.EasyMock.reset;
42import static org.junit.Assert.assertEquals;
43import static org.junit.Assert.assertNull;
44import static org.junit.Assert.assertTrue;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070045
46/**
47 * Unit tests for the HostToInterfaceAdaptor class.
48 */
49public class HostToInterfaceAdaptorTest {
50
51 private HostService hostService;
52 private HostToInterfaceAdaptor adaptor;
53
54 private Set<PortAddresses> portAddresses;
55 private Map<ConnectPoint, Interface> interfaces;
56
57 private static final ConnectPoint CP1 = new ConnectPoint(
58 DeviceId.deviceId("of:1"), PortNumber.portNumber(1));
59 private static final ConnectPoint CP2 = new ConnectPoint(
60 DeviceId.deviceId("of:1"), PortNumber.portNumber(2));
61 private static final ConnectPoint CP3 = new ConnectPoint(
62 DeviceId.deviceId("of:2"), PortNumber.portNumber(1));
63
64 private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint(
65 DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1));
66
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070067 @Before
68 public void setUp() throws Exception {
69 hostService = createMock(HostService.class);
70
71 portAddresses = Sets.newHashSet();
72 interfaces = Maps.newHashMap();
73
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070074 InterfaceIpAddress ia11 =
75 new InterfaceIpAddress(IpAddress.valueOf("192.168.1.1"),
76 IpPrefix.valueOf("192.168.1.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070077 createPortAddressesAndInterface(CP1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070078 Sets.newHashSet(ia11),
Jonathan Hart6cd2f352015-01-13 17:44:45 -080079 MacAddress.valueOf("00:00:00:00:00:01"),
80 VlanId.NONE);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070081
82 // Two addresses in the same subnet
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070083 InterfaceIpAddress ia21 =
84 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.1"),
85 IpPrefix.valueOf("192.168.2.0/24"));
86 InterfaceIpAddress ia22 =
87 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.2"),
88 IpPrefix.valueOf("192.168.2.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070089 createPortAddressesAndInterface(CP2,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070090 Sets.newHashSet(ia21, ia22),
Jonathan Hart6cd2f352015-01-13 17:44:45 -080091 MacAddress.valueOf("00:00:00:00:00:02"),
92 VlanId.vlanId((short) 4));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070093
94 // Two addresses in different subnets
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070095 InterfaceIpAddress ia31 =
96 new InterfaceIpAddress(IpAddress.valueOf("192.168.3.1"),
97 IpPrefix.valueOf("192.168.3.0/24"));
98 InterfaceIpAddress ia41 =
99 new InterfaceIpAddress(IpAddress.valueOf("192.168.4.1"),
100 IpPrefix.valueOf("192.168.4.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700101 createPortAddressesAndInterface(CP3,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700102 Sets.newHashSet(ia31, ia41),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800103 MacAddress.valueOf("00:00:00:00:00:03"),
104 VlanId.NONE);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700105
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
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800120 * @param vlan the VLAN ID
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700121 */
122 private void createPortAddressesAndInterface(
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700123 ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800124 MacAddress mac, VlanId vlan) {
125 PortAddresses pa = new PortAddresses(cp, ipAddresses, mac, vlan);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700126 portAddresses.add(pa);
Jonathan Harta887ba82014-11-03 15:20:52 -0800127 expect(hostService.getAddressBindingsForPort(cp)).andReturn(
128 Collections.singleton(pa)).anyTimes();
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700129
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800130 Interface intf = new Interface(cp, ipAddresses, mac, vlan);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700131 interfaces.put(cp, intf);
132 }
133
134 /**
135 * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
136 * Verifies that the set of interfaces returned matches what is expected
137 * based on the input PortAddresses data.
138 */
139 @Test
140 public void testGetInterfaces() {
141 Set<Interface> adaptorIntfs = adaptor.getInterfaces();
142
143 assertEquals(3, adaptorIntfs.size());
144 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1)));
145 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2)));
146 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3)));
147 }
148
149 /**
150 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
151 * Verifies that the correct interface is returned for a given connect
152 * point.
153 */
154 @Test
155 public void testGetInterface() {
156 assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1));
157 assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2));
158 assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3));
159
160 // Try and get an interface for a connect point with no addresses
161 reset(hostService);
162 expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
Jonathan Harta887ba82014-11-03 15:20:52 -0800163 .andReturn(Collections.<PortAddresses>emptySet()).anyTimes();
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700164 replay(hostService);
165
166 assertNull(adaptor.getInterface(NON_EXISTENT_CP));
167 }
168
169 /**
170 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
171 * case that the input connect point is null.
172 * Verifies that a NullPointerException is thrown.
173 */
174 @Test(expected = NullPointerException.class)
175 public void testGetInterfaceNull() {
Jonathan Harte30fcda2015-08-06 16:22:34 -0700176 ConnectPoint c = null;
177 adaptor.getInterface(c);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700178 }
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}