Jonathan Hart

Implemented getMatchingInterface and added unit tests for

HostToInterfaceAdaptor.

Change-Id: Ibb56902e13f26dbd619d1ef87e283e8e4c5317db
...@@ -4,19 +4,17 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -4,19 +4,17 @@ import static com.google.common.base.Preconditions.checkNotNull;
4 4
5 import java.util.Set; 5 import java.util.Set;
6 6
7 -import org.apache.commons.lang.NotImplementedException;
8 import org.onlab.onos.net.ConnectPoint; 7 import org.onlab.onos.net.ConnectPoint;
9 import org.onlab.onos.net.host.HostService; 8 import org.onlab.onos.net.host.HostService;
10 import org.onlab.onos.net.host.PortAddresses; 9 import org.onlab.onos.net.host.PortAddresses;
11 import org.onlab.onos.sdnip.config.Interface; 10 import org.onlab.onos.sdnip.config.Interface;
12 import org.onlab.packet.IpAddress; 11 import org.onlab.packet.IpAddress;
12 +import org.onlab.packet.IpPrefix;
13 13
14 import com.google.common.collect.Sets; 14 import com.google.common.collect.Sets;
15 15
16 -
17 -
18 /** 16 /**
19 - * Provides IntefaceService using PortAddresses data from the HostService. 17 + * Provides InterfaceService using PortAddresses data from the HostService.
20 */ 18 */
21 public class HostToInterfaceAdaptor implements InterfaceService { 19 public class HostToInterfaceAdaptor implements InterfaceService {
22 20
...@@ -52,8 +50,17 @@ public class HostToInterfaceAdaptor implements InterfaceService { ...@@ -52,8 +50,17 @@ public class HostToInterfaceAdaptor implements InterfaceService {
52 50
53 @Override 51 @Override
54 public Interface getMatchingInterface(IpAddress ipAddress) { 52 public Interface getMatchingInterface(IpAddress ipAddress) {
55 - // TODO implement 53 + checkNotNull(ipAddress);
56 - throw new NotImplementedException("getMatchingInteface is not yet implemented"); 54 +
55 + for (PortAddresses portAddresses : hostService.getAddressBindings()) {
56 + for (IpPrefix p : portAddresses.ips()) {
57 + if (p.contains(ipAddress)) {
58 + return new Interface(portAddresses);
59 + }
60 + }
61 + }
62 +
63 + return null;
57 } 64 }
58 65
59 } 66 }
......
...@@ -64,6 +64,7 @@ public class SdnIp implements SdnIpService { ...@@ -64,6 +64,7 @@ public class SdnIp implements SdnIpService {
64 bgpSessionManager.startUp(2000); // TODO 64 bgpSessionManager.startUp(2000); // TODO
65 65
66 // TODO need to disable link discovery on external ports 66 // TODO need to disable link discovery on external ports
67 +
67 } 68 }
68 69
69 @Deactivate 70 @Deactivate
......
1 +package org.onlab.onos.sdnip;
2 +
3 +import static org.easymock.EasyMock.createMock;
4 +import static org.easymock.EasyMock.expect;
5 +import static org.easymock.EasyMock.replay;
6 +import static org.easymock.EasyMock.reset;
7 +import static org.junit.Assert.assertEquals;
8 +import static org.junit.Assert.assertNull;
9 +import static org.junit.Assert.assertTrue;
10 +
11 +import java.util.Map;
12 +import java.util.Set;
13 +
14 +import org.junit.Before;
15 +import org.junit.Test;
16 +import org.onlab.onos.net.ConnectPoint;
17 +import org.onlab.onos.net.DeviceId;
18 +import org.onlab.onos.net.PortNumber;
19 +import org.onlab.onos.net.host.HostService;
20 +import org.onlab.onos.net.host.PortAddresses;
21 +import org.onlab.onos.sdnip.config.Interface;
22 +import org.onlab.packet.IpAddress;
23 +import org.onlab.packet.IpPrefix;
24 +import org.onlab.packet.MacAddress;
25 +
26 +import com.google.common.collect.Maps;
27 +import com.google.common.collect.Sets;
28 +
29 +/**
30 + * Unit tests for the HostToInterfaceAdaptor class.
31 + */
32 +public class HostToInterfaceAdaptorTest {
33 +
34 + private HostService hostService;
35 + private HostToInterfaceAdaptor adaptor;
36 +
37 + private Set<PortAddresses> portAddresses;
38 + private Map<ConnectPoint, Interface> interfaces;
39 +
40 + private static final ConnectPoint CP1 = new ConnectPoint(
41 + DeviceId.deviceId("of:1"), PortNumber.portNumber(1));
42 + private static final ConnectPoint CP2 = new ConnectPoint(
43 + DeviceId.deviceId("of:1"), PortNumber.portNumber(2));
44 + private static final ConnectPoint CP3 = new ConnectPoint(
45 + DeviceId.deviceId("of:2"), PortNumber.portNumber(1));
46 +
47 + private static final ConnectPoint NON_EXISTENT_CP = new ConnectPoint(
48 + DeviceId.deviceId("doesnotexist"), PortNumber.portNumber(1));
49 +
50 + private static final PortAddresses DEFAULT_PA = new PortAddresses(
51 + NON_EXISTENT_CP, null, null);
52 +
53 +
54 + @Before
55 + public void setUp() throws Exception {
56 + hostService = createMock(HostService.class);
57 +
58 + portAddresses = Sets.newHashSet();
59 + interfaces = Maps.newHashMap();
60 +
61 + createPortAddressesAndInterface(CP1,
62 + Sets.newHashSet(IpPrefix.valueOf("192.168.1.1/24")),
63 + MacAddress.valueOf("00:00:00:00:00:01"));
64 +
65 + // Two addresses in the same subnet
66 + createPortAddressesAndInterface(CP2,
67 + Sets.newHashSet(IpPrefix.valueOf("192.168.2.1/24"),
68 + IpPrefix.valueOf("192.168.2.2/24")),
69 + MacAddress.valueOf("00:00:00:00:00:02"));
70 +
71 + // Two addresses in different subnets
72 + createPortAddressesAndInterface(CP3,
73 + Sets.newHashSet(IpPrefix.valueOf("192.168.3.1/24"),
74 + IpPrefix.valueOf("192.168.4.1/24")),
75 + MacAddress.valueOf("00:00:00:00:00:03"));
76 +
77 + expect(hostService.getAddressBindings()).andReturn(portAddresses).anyTimes();
78 +
79 + replay(hostService);
80 +
81 + adaptor = new HostToInterfaceAdaptor(hostService);
82 + }
83 +
84 + /**
85 + * Creates both a PortAddresses and an Interface for the given inputs and
86 + * places them in the correct global data stores.
87 + *
88 + * @param cp the connect point
89 + * @param ips the set of IP addresses
90 + * @param mac the MAC address
91 + */
92 + private void createPortAddressesAndInterface(
93 + ConnectPoint cp, Set<IpPrefix> ips, MacAddress mac) {
94 + PortAddresses pa = new PortAddresses(cp, ips, mac);
95 + portAddresses.add(pa);
96 + expect(hostService.getAddressBindingsForPort(cp)).andReturn(pa).anyTimes();
97 +
98 + Interface intf = new Interface(cp, ips, mac);
99 + interfaces.put(cp, intf);
100 + }
101 +
102 + /**
103 + * Tests {@link HostToInterfaceAdaptor#getInterfaces()}.
104 + * Verifies that the set of interfaces returned matches what is expected
105 + * based on the input PortAddresses data.
106 + */
107 + @Test
108 + public void testGetInterfaces() {
109 + Set<Interface> adaptorIntfs = adaptor.getInterfaces();
110 +
111 + assertEquals(3, adaptorIntfs.size());
112 + assertTrue(adaptorIntfs.contains(this.interfaces.get(CP1)));
113 + assertTrue(adaptorIntfs.contains(this.interfaces.get(CP2)));
114 + assertTrue(adaptorIntfs.contains(this.interfaces.get(CP3)));
115 + }
116 +
117 + /**
118 + * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)}.
119 + * Verifies that the correct interface is returned for a given connect
120 + * point.
121 + */
122 + @Test
123 + public void testGetInterface() {
124 + assertEquals(this.interfaces.get(CP1), adaptor.getInterface(CP1));
125 + assertEquals(this.interfaces.get(CP2), adaptor.getInterface(CP2));
126 + assertEquals(this.interfaces.get(CP3), adaptor.getInterface(CP3));
127 +
128 + // Try and get an interface for a connect point with no addresses
129 + reset(hostService);
130 + expect(hostService.getAddressBindingsForPort(NON_EXISTENT_CP))
131 + .andReturn(DEFAULT_PA).anyTimes();
132 + replay(hostService);
133 +
134 + assertNull(adaptor.getInterface(NON_EXISTENT_CP));
135 + }
136 +
137 + /**
138 + * Tests {@link HostToInterfaceAdaptor#getInterface(ConnectPoint)} in the
139 + * case that the input connect point is null.
140 + * Verifies that a NullPointerException is thrown.
141 + */
142 + @Test(expected = NullPointerException.class)
143 + public void testGetInterfaceNull() {
144 + adaptor.getInterface(null);
145 + }
146 +
147 + /**
148 + * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)}.
149 + * Verifies that the correct interface is returned based on the given IP
150 + * address.
151 + */
152 + @Test
153 + public void testGetMatchingInterface() {
154 + assertEquals(this.interfaces.get(CP1),
155 + adaptor.getMatchingInterface(IpAddress.valueOf("192.168.1.100")));
156 + assertEquals(this.interfaces.get(CP2),
157 + adaptor.getMatchingInterface(IpAddress.valueOf("192.168.2.100")));
158 + assertEquals(this.interfaces.get(CP3),
159 + adaptor.getMatchingInterface(IpAddress.valueOf("192.168.3.100")));
160 + assertEquals(this.interfaces.get(CP3),
161 + adaptor.getMatchingInterface(IpAddress.valueOf("192.168.4.100")));
162 +
163 + // Try and match an address we don't have subnet configured for
164 + assertNull(adaptor.getMatchingInterface(IpAddress.valueOf("1.1.1.1")));
165 + }
166 +
167 + /**
168 + * Tests {@link HostToInterfaceAdaptor#getMatchingInterface(IpAddress)} in the
169 + * case that the input IP address is null.
170 + * Verifies that a NullPointerException is thrown.
171 + */
172 + @Test(expected = NullPointerException.class)
173 + public void testGetMatchingInterfaceNull() {
174 + adaptor.getMatchingInterface(null);
175 + }
176 +
177 +}