blob: d63581fe36f2a72c2a63ab17a86ef688c8163b5f [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.host.HostService;
36import org.onosproject.net.host.InterfaceIpAddress;
37import org.onosproject.net.host.PortAddresses;
38import org.onosproject.sdnip.config.Interface;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070039import org.onlab.packet.IpAddress;
40import org.onlab.packet.IpPrefix;
41import org.onlab.packet.MacAddress;
42
43import com.google.common.collect.Maps;
44import com.google.common.collect.Sets;
45
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 Hart4c2b15e2014-10-20 13:10:56 -070079 MacAddress.valueOf("00:00:00:00:00:01"));
80
81 // Two addresses in the same subnet
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070082 InterfaceIpAddress ia21 =
83 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.1"),
84 IpPrefix.valueOf("192.168.2.0/24"));
85 InterfaceIpAddress ia22 =
86 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.2"),
87 IpPrefix.valueOf("192.168.2.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070088 createPortAddressesAndInterface(CP2,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070089 Sets.newHashSet(ia21, ia22),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070090 MacAddress.valueOf("00:00:00:00:00:02"));
91
92 // Two addresses in different subnets
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070093 InterfaceIpAddress ia31 =
94 new InterfaceIpAddress(IpAddress.valueOf("192.168.3.1"),
95 IpPrefix.valueOf("192.168.3.0/24"));
96 InterfaceIpAddress ia41 =
97 new InterfaceIpAddress(IpAddress.valueOf("192.168.4.1"),
98 IpPrefix.valueOf("192.168.4.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070099 createPortAddressesAndInterface(CP3,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700100 Sets.newHashSet(ia31, ia41),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700101 MacAddress.valueOf("00:00:00:00:00:03"));
102
103 expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
104
105 replay(hostService);
106
107 adaptor = new HostToInterfaceAdaptor(hostService);
108 }
109
110 /**
111 * Creates both a PortAddresses and an Interface for the given inputs and
112 * places them in the correct global data stores.
113 *
114 * @param cp the connect point
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700115 * @param ipAddresses the set of interface IP addresses
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700116 * @param mac the MAC address
117 */
118 private void createPortAddressesAndInterface(
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700119 ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses,
120 MacAddress mac) {
121 PortAddresses pa = new PortAddresses(cp, ipAddresses, mac);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700122 portAddresses.add(pa);
Jonathan Harta887ba82014-11-03 15:20:52 -0800123 expect(hostService.getAddressBindingsForPort(cp)).andReturn(
124 Collections.singleton(pa)).anyTimes();
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700125
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700126 Interface intf = new Interface(cp, ipAddresses, mac);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700127 interfaces.put(cp, intf);
128 }
129
130 /**
131 * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
132 * Verifies that the set of interfaces returned matches what is expected
133 * based on the input PortAddresses data.
134 */
135 @Test
136 public void testGetInterfaces() {
137 Set<Interface> adaptorIntfs = adaptor.getInterfaces();
138
139 assertEquals(3, adaptorIntfs.size());
140 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1)));
141 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2)));
142 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3)));
143 }
144
145 /**
146 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
147 * Verifies that the correct interface is returned for a given connect
148 * point.
149 */
150 @Test
151 public void testGetInterface() {
152 assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1));
153 assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2));
154 assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3));
155
156 // Try and get an interface for a connect point with no addresses
157 reset(hostService);
158 expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
Jonathan Harta887ba82014-11-03 15:20:52 -0800159 .andReturn(Collections.<PortAddresses>emptySet()).anyTimes();
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700160 replay(hostService);
161
162 assertNull(adaptor.getInterface(NON_EXISTENT_CP));
163 }
164
165 /**
166 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
167 * case that the input connect point is null.
168 * Verifies that a NullPointerException is thrown.
169 */
170 @Test(expected = NullPointerException.class)
171 public void testGetInterfaceNull() {
172 adaptor.getInterface(null);
173 }
174
175 /**
176 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}.
177 * Verifies that the correct interface is returned based on the given IP
178 * address.
179 */
180 @Test
181 public void testGetMatchingInterface() {
182 assertEquals(this.interfaces.get(CP1),
183 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.1.100")));
184 assertEquals(this.interfaces.get(CP2),
185 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.2.100")));
186 assertEquals(this.interfaces.get(CP3),
187 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.3.100")));
188 assertEquals(this.interfaces.get(CP3),
189 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.4.100")));
190
191 // Try and match an address we don't have subnet configured for
192 assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1")));
193 }
194
195 /**
196 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the
197 * case that the input IP address is null.
198 * Verifies that a NullPointerException is thrown.
199 */
200 @Test(expected = NullPointerException.class)
201 public void testGetMatchingInterfaceNull() {
202 adaptor.getMatchingInterface(null);
203 }
204
205}