Ayaka Koshibe

test for OFHostProvider

Change-Id: Ib5385244f9ace79c59d97fb6759419a7cceaca1b
...@@ -59,7 +59,7 @@ public class DefaultTopologyProviderTest { ...@@ -59,7 +59,7 @@ public class DefaultTopologyProviderTest {
59 59
60 private void validateSubmission() { 60 private void validateSubmission() {
61 assertNotNull("registration expected", providerService); 61 assertNotNull("registration expected", providerService);
62 - assertEquals("incorrect providert", provider, providerService.provider()); 62 + assertEquals("incorrect provider", provider, providerService.provider());
63 assertNotNull("topo change should be submitted", providerService.graphDesc); 63 assertNotNull("topo change should be submitted", providerService.graphDesc);
64 assertEquals("incorrect vertex count", 6, providerService.graphDesc.vertexes().size()); 64 assertEquals("incorrect vertex count", 6, providerService.graphDesc.vertexes().size());
65 assertEquals("incorrect edge count", 10, providerService.graphDesc.edges().size()); 65 assertEquals("incorrect edge count", 10, providerService.graphDesc.edges().size());
......
1 +package org.onlab.onos.provider.of.host.impl;
2 +
3 +import static org.junit.Assert.assertEquals;
4 +import static org.junit.Assert.assertNotNull;
5 +import static org.junit.Assert.assertNull;
6 +
7 +import java.util.Set;
8 +
9 +import org.junit.After;
10 +import org.junit.Before;
11 +import org.junit.Test;
12 +import org.onlab.onos.net.ConnectPoint;
13 +import org.onlab.onos.net.DeviceId;
14 +import org.onlab.onos.net.HostId;
15 +import org.onlab.onos.net.Link;
16 +import org.onlab.onos.net.Path;
17 +import org.onlab.onos.net.host.HostDescription;
18 +import org.onlab.onos.net.host.HostProvider;
19 +import org.onlab.onos.net.host.HostProviderRegistry;
20 +import org.onlab.onos.net.host.HostProviderService;
21 +import org.onlab.onos.net.provider.AbstractProviderService;
22 +import org.onlab.onos.net.provider.ProviderId;
23 +import org.onlab.onos.net.topology.ClusterId;
24 +import org.onlab.onos.net.topology.LinkWeight;
25 +import org.onlab.onos.net.topology.Topology;
26 +import org.onlab.onos.net.topology.TopologyCluster;
27 +import org.onlab.onos.net.topology.TopologyGraph;
28 +import org.onlab.onos.net.topology.TopologyListener;
29 +import org.onlab.onos.net.topology.TopologyService;
30 +import org.onlab.onos.of.controller.Dpid;
31 +import org.onlab.onos.of.controller.OpenFlowController;
32 +import org.onlab.onos.of.controller.OpenFlowPacketContext;
33 +import org.onlab.onos.of.controller.OpenFlowSwitch;
34 +import org.onlab.onos.of.controller.OpenFlowSwitchListener;
35 +import org.onlab.onos.of.controller.PacketListener;
36 +import org.onlab.onos.of.controller.RoleState;
37 +import org.onlab.packet.ARP;
38 +import org.onlab.packet.Ethernet;
39 +import org.onlab.packet.MACAddress;
40 +import org.onlab.packet.VLANID;
41 +import org.projectfloodlight.openflow.protocol.OFMessage;
42 +import org.projectfloodlight.openflow.types.OFPort;
43 +
44 +public class OpenFlowHostProviderTest {
45 +
46 + private static final Integer INPORT = 10;
47 + private static final Dpid DPID1 = new Dpid(100);
48 + private static final Dpid DPID2 = new Dpid(200);
49 + private static final Dpid DPID3 = new Dpid(300);
50 +
51 + private static final VLANID VLAN = VLANID.vlanId();
52 + private static final MACAddress MAC = MACAddress.valueOf("00:00:11:00:00:01");
53 + private static final MACAddress BCMAC = MACAddress.valueOf("ff:ff:ff:ff:ff:ff");
54 + private static byte [] IP = new byte [] { 10,0,0,1 };
55 +
56 + private OpenFlowHostProvider provider = new OpenFlowHostProvider();
57 + private TestHostRegistry hostService = new TestHostRegistry();
58 + private TestController controller = new TestController();
59 + private TestTopologyService topoService = new TestTopologyService();
60 + private TestHostProviderService providerService;
61 +
62 + @Before
63 + public void setUp() {
64 + provider.providerRegistry = hostService;
65 + provider.controller = controller;
66 + provider.topologyService = topoService;
67 + provider.activate();
68 + }
69 +
70 + @Test
71 + public void basics() {
72 + assertNotNull("registration expected", providerService);
73 + assertEquals("incorrect provider", provider, providerService.provider());
74 + }
75 +
76 + @Test
77 + public void events() {
78 + // new host
79 + controller.processPacket(DPID1, null);
80 + assertNotNull("new host expected", providerService.added);
81 + assertNull("host motion unexpected", providerService.moved);
82 +
83 + // the host moved to new switch
84 + controller.processPacket(DPID2, null);
85 + assertNotNull("host motion expected", providerService.moved);
86 +
87 + // the host was misheard on a spine
88 + controller.processPacket(DPID3, null);
89 + assertNull("host misheard on spine switch", providerService.spine);
90 + }
91 +
92 + @After
93 + public void tearDown() {
94 + provider.deactivate();
95 + provider.providerRegistry = null;
96 + provider.controller = null;
97 + }
98 +
99 + private class TestHostRegistry implements HostProviderRegistry {
100 +
101 + @Override
102 + public HostProviderService register(HostProvider provider) {
103 + providerService = new TestHostProviderService(provider);
104 + return providerService;
105 + }
106 +
107 + @Override
108 + public void unregister(HostProvider provider) {
109 + }
110 +
111 + @Override
112 + public Set<ProviderId> getProviders() {
113 + return null;
114 + }
115 +
116 + }
117 +
118 + private class TestHostProviderService
119 + extends AbstractProviderService<HostProvider>
120 + implements HostProviderService {
121 +
122 + Dpid added = null;
123 + Dpid moved = null;
124 + Dpid spine = null;
125 +
126 + protected TestHostProviderService(HostProvider provider) {
127 + super(provider);
128 + }
129 +
130 + @Override
131 + public void hostDetected(HostId hostId, HostDescription hostDescription) {
132 + Dpid descr = Dpid.dpid(hostDescription.location().deviceId().uri());
133 + if (added == null) {
134 + added = descr;
135 + } else if ((moved == null) && !descr.equals(added)) {
136 + moved = descr;
137 + } else {
138 + spine = descr;
139 + }
140 + }
141 +
142 + @Override
143 + public void hostVanished(HostId hostId) {
144 + }
145 +
146 + }
147 +
148 + private class TestController implements OpenFlowController {
149 +
150 + PacketListener pktListener;
151 +
152 + @Override
153 + public Iterable<OpenFlowSwitch> getSwitches() {
154 + return null;
155 + }
156 +
157 + @Override
158 + public Iterable<OpenFlowSwitch> getMasterSwitches() {
159 + return null;
160 + }
161 +
162 + @Override
163 + public Iterable<OpenFlowSwitch> getEqualSwitches() {
164 + return null;
165 + }
166 +
167 + @Override
168 + public OpenFlowSwitch getSwitch(Dpid dpid) {
169 + return null;
170 + }
171 +
172 + @Override
173 + public OpenFlowSwitch getMasterSwitch(Dpid dpid) {
174 + return null;
175 + }
176 +
177 + @Override
178 + public OpenFlowSwitch getEqualSwitch(Dpid dpid) {
179 + return null;
180 + }
181 +
182 + @Override
183 + public void addListener(OpenFlowSwitchListener listener) {
184 + }
185 +
186 + @Override
187 + public void removeListener(OpenFlowSwitchListener listener) {
188 + }
189 +
190 + @Override
191 + public void addPacketListener(int priority, PacketListener listener) {
192 + pktListener = listener;
193 + }
194 +
195 + @Override
196 + public void removePacketListener(PacketListener listener) {
197 + }
198 +
199 + @Override
200 + public void write(Dpid dpid, OFMessage msg) {
201 + }
202 +
203 + @Override
204 + public void processPacket(Dpid dpid, OFMessage msg) {
205 + OpenFlowPacketContext ctx =
206 + new TestPacketContext(dpid);
207 +
208 + pktListener.handlePacket(ctx);
209 + }
210 +
211 + @Override
212 + public void setRole(Dpid dpid, RoleState role) {
213 + }
214 + }
215 +
216 + private class TestTopologyService implements TopologyService {
217 +
218 + @Override
219 + public Topology currentTopology() {
220 + return null;
221 + }
222 +
223 + @Override
224 + public boolean isLatest(Topology topology) {
225 + return false;
226 + }
227 +
228 + @Override
229 + public TopologyGraph getGraph(Topology topology) {
230 + return null;
231 + }
232 +
233 + @Override
234 + public Set<TopologyCluster> getClusters(Topology topology) {
235 + return null;
236 + }
237 +
238 + @Override
239 + public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
240 + return null;
241 + }
242 +
243 + @Override
244 + public Set<DeviceId> getClusterDevices(Topology topology,
245 + TopologyCluster cluster) {
246 + return null;
247 + }
248 +
249 + @Override
250 + public Set<Link> getClusterLinks(Topology topology,
251 + TopologyCluster cluster) {
252 + return null;
253 + }
254 +
255 + @Override
256 + public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
257 + return null;
258 + }
259 +
260 + @Override
261 + public Set<Path> getPaths(Topology topology, DeviceId src,
262 + DeviceId dst, LinkWeight weight) {
263 + return null;
264 + }
265 +
266 + @Override
267 + public boolean isInfrastructure(Topology topology,
268 + ConnectPoint connectPoint) {
269 + //simulate DPID3 as an infrastructure switch
270 + if (Dpid.dpid(connectPoint.deviceId().uri()).equals(DPID3)) {
271 + return true;
272 + }
273 + return false;
274 + }
275 +
276 + @Override
277 + public boolean isBroadcastPoint(Topology topology,
278 + ConnectPoint connectPoint) {
279 + return false;
280 + }
281 +
282 + @Override
283 + public void addListener(TopologyListener listener) {
284 + }
285 +
286 + @Override
287 + public void removeListener(TopologyListener listener) {
288 + }
289 +
290 + }
291 +
292 + private class TestPacketContext implements OpenFlowPacketContext {
293 +
294 + protected Dpid swid;
295 +
296 + public TestPacketContext(Dpid dpid) {
297 + swid = dpid;
298 + }
299 +
300 + @Override
301 + public boolean blocked() {
302 + return false;
303 + }
304 +
305 + @Override
306 + public void send() {
307 + }
308 +
309 + @Override
310 + public void build(OFPort outPort) {
311 + }
312 +
313 + @Override
314 + public void build(Ethernet ethFrame, OFPort outPort) {
315 + }
316 +
317 + @Override
318 + public Ethernet parsed() {
319 + // just things we (and serializers) need
320 + ARP arp = new ARP();
321 + arp.setSenderProtocolAddress(IP)
322 + .setSenderHardwareAddress(MAC.toBytes())
323 + .setTargetHardwareAddress(BCMAC.toBytes())
324 + .setTargetProtocolAddress(IP);
325 +
326 + Ethernet eth = new Ethernet();
327 + eth.setEtherType(Ethernet.TYPE_ARP)
328 + .setVlanID(VLAN.toShort())
329 + .setSourceMACAddress(MAC.toBytes())
330 + .setDestinationMACAddress(BCMAC.getAddress())
331 + .setPayload(arp);
332 +
333 + return eth;
334 + }
335 +
336 + @Override
337 + public byte[] unparsed() {
338 + return null;
339 + }
340 +
341 + @Override
342 + public Dpid dpid() {
343 + return swid;
344 + }
345 +
346 + @Override
347 + public Integer inPort() {
348 + return INPORT;
349 + }
350 +
351 + }
352 +}