blob: 8cad7568617d39a6db111c7dcb8f6e66e77f7d25 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.util;
2
3import static org.junit.Assert.*;
4
5import net.floodlightcontroller.core.FloodlightContext;
6
7import org.junit.Before;
8import org.junit.Test;
9import org.openflow.protocol.OFEchoRequest;
10import org.openflow.protocol.OFHello;
11import org.openflow.protocol.OFMessage;
12import org.openflow.protocol.OFType;
13import org.openflow.protocol.factory.BasicFactory;
14import org.openflow.protocol.factory.OFMessageFactory;
15
16import java.io.IOException;
17import java.util.EnumSet;
18
19public class OFMessageDamperTest {
20 OFMessageFactory factory;
21 OFMessageDamper damper;
22 FloodlightContext cntx;
23
24 OFMessageDamperMockSwitch sw1;
25 OFMessageDamperMockSwitch sw2;
26
27 OFEchoRequest echoRequst1;
28 OFEchoRequest echoRequst1Clone;
29 OFEchoRequest echoRequst2;
30 OFHello hello1;
31 OFHello hello2;
32
33
34
35 @Before
36 public void setUp() throws IOException {
37 factory = new BasicFactory();
38 cntx = new FloodlightContext();
39
40 sw1 = new OFMessageDamperMockSwitch();
41 sw2 = new OFMessageDamperMockSwitch();
42
43 echoRequst1 = (OFEchoRequest)factory.getMessage(OFType.ECHO_REQUEST);
44 echoRequst1.setPayload(new byte[] { 1 });
45 echoRequst1Clone = (OFEchoRequest)
46 factory.getMessage(OFType.ECHO_REQUEST);
47 echoRequst1Clone.setPayload(new byte[] { 1 });
48 echoRequst2 = (OFEchoRequest)factory.getMessage(OFType.ECHO_REQUEST);
49 echoRequst2.setPayload(new byte[] { 2 });
50
51 hello1 = (OFHello)factory.getMessage(OFType.HELLO);
52 hello1.setXid(1);
53 hello2 = (OFHello)factory.getMessage(OFType.HELLO);
54 hello2.setXid(2);
55
56 }
57
58 protected void doWrite(boolean expectWrite,
59 OFMessageDamperMockSwitch sw,
60 OFMessage msg,
61 FloodlightContext cntx) throws IOException {
62
63 boolean result;
64 sw.reset();
65 result = damper.write(sw, msg, cntx);
66
67 if (expectWrite) {
68 assertEquals(true, result);
69 sw.assertMessageWasWritten(msg, cntx);
70 } else {
71 assertEquals(false, result);
72 sw.assertNoMessageWritten();
73 }
74 }
75
76
77 @Test
78 public void testOneMessageType() throws IOException, InterruptedException {
79 int timeout = 50;
80 int sleepTime = 60;
81 damper = new OFMessageDamper(100,
82 EnumSet.of(OFType.ECHO_REQUEST),
83 timeout);
84
85
86
87 // echo requests should be dampened
88 doWrite(true, sw1, echoRequst1, cntx);
89 doWrite(false, sw1, echoRequst1, cntx);
90 doWrite(false, sw1, echoRequst1Clone, cntx);
91 doWrite(true, sw1, echoRequst2, cntx);
92 doWrite(false, sw1, echoRequst2, cntx);
93
94 // we don't dampen hellos. All should succeed
95 doWrite(true, sw1, hello1, cntx);
96 doWrite(true, sw1, hello1, cntx);
97 doWrite(true, sw1, hello1, cntx);
98
99 // echo request should also be dampened on sw2
100 doWrite(true, sw2, echoRequst1, cntx);
101 doWrite(false, sw2, echoRequst1, cntx);
102 doWrite(true, sw2, echoRequst2, cntx);
103
104
105 Thread.sleep(sleepTime);
106 doWrite(true, sw1, echoRequst1, cntx);
107 doWrite(true, sw2, echoRequst1, cntx);
108
109 }
110
111 @Test
112 public void testTwoMessageTypes() throws IOException, InterruptedException {
113 int timeout = 50;
114 int sleepTime = 60;
115 damper = new OFMessageDamper(100,
116 EnumSet.of(OFType.ECHO_REQUEST,
117 OFType.HELLO),
118 timeout);
119
120
121
122 // echo requests should be dampened
123 doWrite(true, sw1, echoRequst1, cntx);
124 doWrite(false, sw1, echoRequst1, cntx);
125 doWrite(false, sw1, echoRequst1Clone, cntx);
126 doWrite(true, sw1, echoRequst2, cntx);
127 doWrite(false, sw1, echoRequst2, cntx);
128
129 // hello should be dampened as well
130 doWrite(true, sw1, hello1, cntx);
131 doWrite(false, sw1, hello1, cntx);
132 doWrite(false, sw1, hello1, cntx);
133
134 doWrite(true, sw1, hello2, cntx);
135 doWrite(false, sw1, hello2, cntx);
136 doWrite(false, sw1, hello2, cntx);
137
138 // echo request should also be dampened on sw2
139 doWrite(true, sw2, echoRequst1, cntx);
140 doWrite(false, sw2, echoRequst1, cntx);
141 doWrite(true, sw2, echoRequst2, cntx);
142
143 Thread.sleep(sleepTime);
144 doWrite(true, sw1, echoRequst1, cntx);
145 doWrite(true, sw2, echoRequst1, cntx);
146 doWrite(true, sw1, hello1, cntx);
147 doWrite(true, sw1, hello2, cntx);
148 }
149
150}