blob: e9a84207ff097cdff8ec183183639a8cf54259ec [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;
38import org.onlab.onos.net.host.PortAddresses;
39import org.onlab.onos.sdnip.config.Interface;
40import org.onlab.packet.IpAddress;
41import org.onlab.packet.IpPrefix;
42import org.onlab.packet.MacAddress;
43
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
68 private static final PortAddresses DEFAULT_PA = new PortAddresses(
69 NON_EXISTENT_CP, null, null);
70
71
72 @Before
73 public void setUp() throws Exception {
74 hostService = createMock(HostService.class);
75
76 portAddresses = Sets.newHashSet();
77 interfaces = Maps.newHashMap();
78
79 createPortAddressesAndInterface(CP1,
80 Sets.newHashSet(IpPrefix.valueOf("192.168.1.1/24")),
81 MacAddress.valueOf("00:00:00:00:00:01"));
82
83 // Two addresses in the same subnet
84 createPortAddressesAndInterface(CP2,
85 Sets.newHashSet(IpPrefix.valueOf("192.168.2.1/24"),
86 IpPrefix.valueOf("192.168.2.2/24")),
87 MacAddress.valueOf("00:00:00:00:00:02"));
88
89 // Two addresses in different subnets
90 createPortAddressesAndInterface(CP3,
91 Sets.newHashSet(IpPrefix.valueOf("192.168.3.1/24"),
92 IpPrefix.valueOf("192.168.4.1/24")),
93 MacAddress.valueOf("00:00:00:00:00:03"));
94
95 expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
96
97 replay(hostService);
98
99 adaptor = new HostToInterfaceAdaptor(hostService);
100 }
101
102 /**
103 * Creates both a PortAddresses and an Interface for the given inputs and
104 * places them in the correct global data stores.
105 *
106 * @param cp the connect point
107 * @param ips the set of IP addresses
108 * @param mac the MAC address
109 */
110 private void createPortAddressesAndInterface(
111 ConnectPoint cp, Set<IpPrefix> ips, MacAddress mac) {
112 PortAddresses pa = new PortAddresses(cp, ips, mac);
113 portAddresses.add(pa);
114 expect(hostService.getAddressBindingsForPort(cp)).andReturn(pa).anyTimes();
115
116 Interface intf = new Interface(cp, ips, mac);
117 interfaces.put(cp, intf);
118 }
119
120 /**
121 * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
122 * Verifies that the set of interfaces returned matches what is expected
123 * based on the input PortAddresses data.
124 */
125 @Test
126 public void testGetInterfaces() {
127 Set<Interface> adaptorIntfs = adaptor.getInterfaces();
128
129 assertEquals(3, adaptorIntfs.size());
130 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1)));
131 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2)));
132 assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3)));
133 }
134
135 /**
136 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
137 * Verifies that the correct interface is returned for a given connect
138 * point.
139 */
140 @Test
141 public void testGetInterface() {
142 assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1));
143 assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2));
144 assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3));
145
146 // Try and get an interface for a connect point with no addresses
147 reset(hostService);
148 expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
149 .andReturn(DEFAULT_PA).anyTimes();
150 replay(hostService);
151
152 assertNull(adaptor.getInterface(NON_EXISTENT_CP));
153 }
154
155 /**
156 * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
157 * case that the input connect point is null.
158 * Verifies that a NullPointerException is thrown.
159 */
160 @Test(expected = NullPointerException.class)
161 public void testGetInterfaceNull() {
162 adaptor.getInterface(null);
163 }
164
165 /**
166 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}.
167 * Verifies that the correct interface is returned based on the given IP
168 * address.
169 */
170 @Test
171 public void testGetMatchingInterface() {
172 assertEquals(this.interfaces.get(CP1),
173 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.1.100")));
174 assertEquals(this.interfaces.get(CP2),
175 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.2.100")));
176 assertEquals(this.interfaces.get(CP3),
177 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.3.100")));
178 assertEquals(this.interfaces.get(CP3),
179 adaptor.getMatchingInterface(IpAddress.valueOf("192.168.4.100")));
180
181 // Try and match an address we don't have subnet configured for
182 assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1")));
183 }
184
185 /**
186 * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the
187 * case that the input IP address is null.
188 * Verifies that a NullPointerException is thrown.
189 */
190 @Test(expected = NullPointerException.class)
191 public void testGetMatchingInterfaceNull() {
192 adaptor.getMatchingInterface(null);
193 }
194
195}