0347fc5f8516c9188e8a6307483d6700d3625bbc
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
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
7  *
8  *     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.
15  */
16 package org.onosproject.routing.config.impl;
17
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;
33
34 import java.util.Collections;
35 import java.util.Map;
36 import java.util.Set;
37
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;
45
46 /**
47  * Unit tests for the HostToInterfaceAdaptor class.
48  */
49 public 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
67     @Before
68     public void setUp() throws Exception {
69         hostService = createMock(HostService.class);
70
71         portAddresses = Sets.newHashSet();
72         interfaces = Maps.newHashMap();
73
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"),
80                 VlanId.NONE);
81
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));
93
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"),
104                 VlanId.NONE);
105
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
118      * @param ipAddresses the set of interface IP addresses
119      * @param mac the MAC address
120      * @param vlan the VLAN ID
121      */
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();
129
130         Interface intf = new Interface(cp, ipAddresses, mac, vlan);
131         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))
163                 .andReturn(Collections.<PortAddresses>emptySet()).anyTimes();
164         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() {
176         ConnectPoint c = null;
177         adaptor.getInterface(c);
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 }