blob: b4a215cd066a7772fc669683dba47aedeb8e7bca [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2011, Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package net.floodlightcontroller.hub;
19
20import static org.easymock.EasyMock.createMock;
21import static org.easymock.EasyMock.replay;
22import static org.easymock.EasyMock.verify;
23import static org.easymock.EasyMock.capture;
24
25import java.util.Arrays;
26
27import net.floodlightcontroller.core.FloodlightContext;
28import net.floodlightcontroller.core.IOFMessageListener;
29import net.floodlightcontroller.core.IOFSwitch;
30import net.floodlightcontroller.core.test.MockFloodlightProvider;
31import net.floodlightcontroller.packet.Data;
32import net.floodlightcontroller.packet.Ethernet;
33import net.floodlightcontroller.packet.IPacket;
34import net.floodlightcontroller.packet.IPv4;
35import net.floodlightcontroller.packet.UDP;
36import net.floodlightcontroller.test.FloodlightTestCase;
37
38import org.easymock.Capture;
39import org.easymock.CaptureType;
40import org.junit.Before;
41import org.junit.Test;
42import org.openflow.protocol.OFPacketIn;
43import org.openflow.protocol.OFPacketIn.OFPacketInReason;
44import org.openflow.protocol.OFMessage;
45import org.openflow.protocol.OFPacketOut;
46import org.openflow.protocol.OFPort;
47import org.openflow.protocol.OFType;
48import org.openflow.protocol.action.OFAction;
49import org.openflow.protocol.action.OFActionOutput;
50
51/**
52 *
53 * @author David Erickson (daviderickson@cs.stanford.edu)
54 */
55public class HubTest extends FloodlightTestCase {
56 protected OFPacketIn packetIn;
57 protected IPacket testPacket;
58 protected byte[] testPacketSerialized;
59 private MockFloodlightProvider mockFloodlightProvider;
60 private Hub hub;
61
62 @Before
63 public void setUp() throws Exception {
64 super.setUp();
65
66 mockFloodlightProvider = getMockFloodlightProvider();
67 hub = new Hub();
68 mockFloodlightProvider.addOFMessageListener(OFType.PACKET_IN, hub);
69 hub.setFloodlightProvider(mockFloodlightProvider);
70
71 // Build our test packet
72 this.testPacket = new Ethernet()
73 .setDestinationMACAddress("00:11:22:33:44:55")
74 .setSourceMACAddress("00:44:33:22:11:00")
75 .setEtherType(Ethernet.TYPE_IPv4)
76 .setPayload(
77 new IPv4()
78 .setTtl((byte) 128)
79 .setSourceAddress("192.168.1.1")
80 .setDestinationAddress("192.168.1.2")
81 .setPayload(new UDP()
82 .setSourcePort((short) 5000)
83 .setDestinationPort((short) 5001)
84 .setPayload(new Data(new byte[] {0x01}))));
85 this.testPacketSerialized = testPacket.serialize();
86
87 // Build the PacketIn
88 this.packetIn = ((OFPacketIn) mockFloodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_IN))
89 .setBufferId(-1)
90 .setInPort((short) 1)
91 .setPacketData(this.testPacketSerialized)
92 .setReason(OFPacketInReason.NO_MATCH)
93 .setTotalLength((short) this.testPacketSerialized.length);
94 }
95
96 @Test
97 public void testFloodNoBufferId() throws Exception {
98 // build our expected flooded packetOut
99 OFPacketOut po = ((OFPacketOut) mockFloodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT))
100 .setActions(Arrays.asList(new OFAction[] {new OFActionOutput().setPort(OFPort.OFPP_FLOOD.getValue())}))
101 .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH)
102 .setBufferId(-1)
103 .setInPort((short) 1)
104 .setPacketData(this.testPacketSerialized);
105 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + po.getActionsLengthU()
106 + this.testPacketSerialized.length);
107
108 // Mock up our expected behavior
109 IOFSwitch mockSwitch = createMock(IOFSwitch.class);
110
111 Capture<OFMessage> wc1 = new Capture<OFMessage>(CaptureType.ALL);
112 Capture<FloodlightContext> bc1 = new Capture<FloodlightContext>(CaptureType.ALL);
113
114 mockSwitch.write(capture(wc1), capture(bc1));
115
116 // Start recording the replay on the mocks
117 replay(mockSwitch);
118 // Get the listener and trigger the packet in
119 IOFMessageListener listener = mockFloodlightProvider.getListeners().get(
120 OFType.PACKET_IN).get(0);
121 listener.receive(mockSwitch, this.packetIn,
122 parseAndAnnotate(this.packetIn));
123
124 // Verify the replay matched our expectations
125 verify(mockSwitch);
126
127 assertTrue(wc1.hasCaptured());
128 OFMessage m = wc1.getValue();
129 assert(m.equals(po));
130 }
131
132 @Test
133 public void testFloodBufferId() throws Exception {
134 MockFloodlightProvider mockFloodlightProvider = getMockFloodlightProvider();
135 this.packetIn.setBufferId(10);
136
137 // build our expected flooded packetOut
138 OFPacketOut po = ((OFPacketOut) mockFloodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT))
139 .setActions(Arrays.asList(new OFAction[] {new OFActionOutput().setPort(OFPort.OFPP_FLOOD.getValue())}))
140 .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH)
141 .setBufferId(10)
142 .setInPort((short) 1);
143 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + po.getActionsLengthU());
144
145 // Mock up our expected behavior
146 IOFSwitch mockSwitch = createMock(IOFSwitch.class);
147 Capture<OFMessage> wc1 = new Capture<OFMessage>(CaptureType.ALL);
148 Capture<FloodlightContext> bc1 = new Capture<FloodlightContext>(CaptureType.ALL);
149
150 mockSwitch.write(capture(wc1), capture(bc1));
151
152 // Start recording the replay on the mocks
153 replay(mockSwitch);
154 // Get the listener and trigger the packet in
155 IOFMessageListener listener = mockFloodlightProvider.getListeners().get(
156 OFType.PACKET_IN).get(0);
157 listener.receive(mockSwitch, this.packetIn,
158 parseAndAnnotate(this.packetIn));
159
160 // Verify the replay matched our expectations
161 verify(mockSwitch);
162
163 assertTrue(wc1.hasCaptured());
164 OFMessage m = wc1.getValue();
165 assert(m.equals(po));
166 }
167}