2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.routing.config.impl;
18 import com.google.common.collect.Maps;
19 import com.google.common.collect.Sets;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.onlab.packet.IpAddress;
23 import org.onlab.packet.IpPrefix;
24 import org.onlab.packet.MacAddress;
25 import org.onlab.packet.VlanId;
26 import org.onosproject.net.ConnectPoint;
27 import org.onosproject.net.DeviceId;
28 import org.onosproject.net.PortNumber;
29 import org.onosproject.net.host.HostService;
30 import org.onosproject.net.host.InterfaceIpAddress;
31 import org.onosproject.net.host.PortAddresses;
32 import org.onosproject.routing.config.Interface;
34 import java.util.Collections;
38 import static org.easymock.EasyMock.createMock;
39 import static org.easymock.EasyMock.expect;
40 import static org.easymock.EasyMock.replay;
41 import static org.easymock.EasyMock.reset;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNull;
44 import static org.junit.Assert.assertTrue;
47 * Unit tests for the HostToInterfaceAdaptor class.
49 public class HostToInterfaceAdaptorTest {
51 private HostService hostService;
52 private HostToInterfaceAdaptor adaptor;
54 private Set<PortAddresses> portAddresses;
55 private Map<ConnectPoint, Interface> interfaces;
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));
64 private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint(
65 DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1));
68 public void setUp() throws Exception {
69 hostService = createMock(HostService.class);
71 portAddresses = Sets.newHashSet();
72 interfaces = Maps.newHashMap();
74 InterfaceIpAddress ia11 =
75 new InterfaceIpAddress(IpAddress.valueOf("192.168.1.1"),
76 IpPrefix.valueOf("192.168.1.0/24"));
77 createPortAddressesAndInterface(CP1,
78 Sets.newHashSet(ia11),
79 MacAddress.valueOf("00:00:00:00:00:01"),
82 // Two addresses in the same subnet
83 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"));
89 createPortAddressesAndInterface(CP2,
90 Sets.newHashSet(ia21, ia22),
91 MacAddress.valueOf("00:00:00:00:00:02"),
92 VlanId.vlanId((short) 4));
94 // Two addresses in different subnets
95 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"));
101 createPortAddressesAndInterface(CP3,
102 Sets.newHashSet(ia31, ia41),
103 MacAddress.valueOf("00:00:00:00:00:03"),
106 expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
110 adaptor = new HostToInterfaceAdaptor(hostService);
114 * Creates both a PortAddresses and an Interface for the given inputs and
115 * places them in the correct global data stores.
117 * @param cp the connect point
118 * @param ipAddresses the set of interface IP addresses
119 * @param mac the MAC address
120 * @param vlan the VLAN ID
122 private void createPortAddressesAndInterface(
123 ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses,
124 MacAddress mac, VlanId vlan) {
125 PortAddresses pa = new PortAddresses(cp, ipAddresses, mac, vlan);
126 portAddresses.add(pa);
127 expect(hostService.getAddressBindingsForPort(cp)).andReturn(
128 Collections.singleton(pa)).anyTimes();
130 Interface intf = new Interface(cp, ipAddresses, mac, vlan);
131 interfaces.put(cp, intf);
135 * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
136 * Verifies that the set of interfaces returned matches what is expected
137 * based on the input PortAddresses data.
140 public void testGetInterfaces() {
141 Set<Interface> adaptorIntfs = adaptor.getInterfaces();
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)));
150 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
151 * Verifies that the correct interface is returned for a given connect
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));
160 // Try and get an interface for a connect point with no addresses
162 expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
163 .andReturn(Collections.<PortAddresses>emptySet()).anyTimes();
166 assertNull(adaptor.getInterface(NON_EXISTENT_CP));
170 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
171 * case that the input connect point is null.
172 * Verifies that a NullPointerException is thrown.
174 @Test(expected = NullPointerException.class)
175 public void testGetInterfaceNull() {
176 ConnectPoint c = null;
177 adaptor.getInterface(c);
181 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}.
182 * Verifies that the correct interface is returned based on the given IP
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")));
196 // Try and match an address we don't have subnet configured for
197 assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1")));
201 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the
202 * case that the input IP address is null.
203 * Verifies that a NullPointerException is thrown.
205 @Test(expected = NullPointerException.class)
206 public void testGetMatchingInterfaceNull() {
207 adaptor.getMatchingInterface(null);