blob: a42cd871c95c5ceb10e95fb5225668211bf30387 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070019package org.onlab.onos.sdnip;
20
21import static org.easymock.EasyMock.createMock;
22import static org.easymock.EasyMock.expect;
23import static org.easymock.EasyMock.replay;
24import static org.easymock.EasyMock.reset;
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertNull;
27import static org.junit.Assert.assertTrue;
28
29import java.util.Map;
30import java.util.Set;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.onlab.onos.net.ConnectPoint;
35import org.onlab.onos.net.DeviceId;
36import org.onlab.onos.net.PortNumber;
37import org.onlab.onos.net.host.HostService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070038import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070039import org.onlab.onos.net.host.PortAddresses;
40import org.onlab.onos.sdnip.config.Interface;
41import org.onlab.packet.IpAddress;
42import org.onlab.packet.IpPrefix;
43import org.onlab.packet.MacAddress;
44
45import com.google.common.collect.Maps;
46import com.google.common.collect.Sets;
47
48/**
49 * Unit tests for the HostToInterfaceAdaptor class.
50 */
51public class HostToInterfaceAdaptorTest {
52
53 private HostService hostService;
54 private HostToInterfaceAdaptor adaptor;
55
56 private Set<PortAddresses> portAddresses;
57 private Map<ConnectPoint, Interface> interfaces;
58
59 private static final ConnectPoint CP1 = new ConnectPoint(
60 DeviceId.deviceId("of:1"), PortNumber.portNumber(1));
61 private static final ConnectPoint CP2 = new ConnectPoint(
62 DeviceId.deviceId("of:1"), PortNumber.portNumber(2));
63 private static final ConnectPoint CP3 = new ConnectPoint(
64 DeviceId.deviceId("of:2"), PortNumber.portNumber(1));
65
66 private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint(
67 DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1));
68
69 private static final PortAddresses DEFAULT_PA = new PortAddresses(
70 NON_EXISTENT_CP, null, null);
71
72
73 @Before
74 public void setUp() throws Exception {
75 hostService = createMock(HostService.class);
76
77 portAddresses = Sets.newHashSet();
78 interfaces = Maps.newHashMap();
79
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070080 InterfaceIpAddress ia11 =
81 new InterfaceIpAddress(IpAddress.valueOf("192.168.1.1"),
82 IpPrefix.valueOf("192.168.1.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070083 createPortAddressesAndInterface(CP1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070084 Sets.newHashSet(ia11),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070085 MacAddress.valueOf("00:00:00:00:00:01"));
86
87 // Two addresses in the same subnet
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070088 InterfaceIpAddress ia21 =
89 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.1"),
90 IpPrefix.valueOf("192.168.2.0/24"));
91 InterfaceIpAddress ia22 =
92 new InterfaceIpAddress(IpAddress.valueOf("192.168.2.2"),
93 IpPrefix.valueOf("192.168.2.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070094 createPortAddressesAndInterface(CP2,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070095 Sets.newHashSet(ia21, ia22),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070096 MacAddress.valueOf("00:00:00:00:00:02"));
97
98 // Two addresses in different subnets
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070099 InterfaceIpAddress ia31 =
100 new InterfaceIpAddress(IpAddress.valueOf("192.168.3.1"),
101 IpPrefix.valueOf("192.168.3.0/24"));
102 InterfaceIpAddress ia41 =
103 new InterfaceIpAddress(IpAddress.valueOf("192.168.4.1"),
104 IpPrefix.valueOf("192.168.4.0/24"));
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700105 createPortAddressesAndInterface(CP3,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700106 Sets.newHashSet(ia31, ia41),
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700107 MacAddress.valueOf("00:00:00:00:00:03"));
108
109 expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
110
111 replay(hostService);
112
113 adaptor = new HostToInterfaceAdaptor(hostService);
114 }
115
116 /**
117 * Creates both a PortAddresses and an Interface for the given inputs and
118 * places them in the correct global data stores.
119 *
120 * @param cp the connect point
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700121 * @param ipAddresses the set of interface IP addresses
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700122 * @param mac the MAC address
123 */
124 private void createPortAddressesAndInterface(
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700125 ConnectPoint cp, Set<InterfaceIpAddress> ipAddresses,
126 MacAddress mac) {
127 PortAddresses pa = new PortAddresses(cp, ipAddresses, mac);
Jonathan Hart4c2b15e2014-10-20 13:10:56 -0700128 portAddresses.add(pa);
129 expect(hostService.getAddressBindingsForPort(cp)).andReturn(pa).anyTimes();
130
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700131 Interface intf = new Interface(cp, ipAddresses, mac);
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))
164 .andReturn(DEFAULT_PA).anyTimes();
165 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}