blob: 02cc535d44682c7267d6d1feaa8095e3c376d4d6 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.util;
2
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.util.EnumSet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08007
8import net.floodlightcontroller.core.FloodlightContext;
9
10import org.junit.Before;
11import org.junit.Test;
12import org.openflow.protocol.OFEchoRequest;
13import org.openflow.protocol.OFHello;
14import org.openflow.protocol.OFMessage;
15import org.openflow.protocol.OFType;
16import org.openflow.protocol.factory.BasicFactory;
17import org.openflow.protocol.factory.OFMessageFactory;
18
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019public class OFMessageDamperTest {
20 OFMessageFactory factory;
21 OFMessageDamper damper;
22 FloodlightContext cntx;
Ray Milkey269ffb92014-04-03 14:43:30 -070023
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024 OFMessageDamperMockSwitch sw1;
25 OFMessageDamperMockSwitch sw2;
Ray Milkey269ffb92014-04-03 14:43:30 -070026
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027 OFEchoRequest echoRequst1;
28 OFEchoRequest echoRequst1Clone;
29 OFEchoRequest echoRequst2;
30 OFHello hello1;
31 OFHello hello2;
Ray Milkey269ffb92014-04-03 14:43:30 -070032
33
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080034 @Before
35 public void setUp() throws IOException {
36 factory = new BasicFactory();
37 cntx = new FloodlightContext();
Ray Milkey269ffb92014-04-03 14:43:30 -070038
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039 sw1 = new OFMessageDamperMockSwitch();
40 sw2 = new OFMessageDamperMockSwitch();
Ray Milkey269ffb92014-04-03 14:43:30 -070041
42 echoRequst1 = (OFEchoRequest) factory.getMessage(OFType.ECHO_REQUEST);
43 echoRequst1.setPayload(new byte[]{1});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044 echoRequst1Clone = (OFEchoRequest)
45 factory.getMessage(OFType.ECHO_REQUEST);
Ray Milkey269ffb92014-04-03 14:43:30 -070046 echoRequst1Clone.setPayload(new byte[]{1});
47 echoRequst2 = (OFEchoRequest) factory.getMessage(OFType.ECHO_REQUEST);
48 echoRequst2.setPayload(new byte[]{2});
49
50 hello1 = (OFHello) factory.getMessage(OFType.HELLO);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 hello1.setXid(1);
Ray Milkey269ffb92014-04-03 14:43:30 -070052 hello2 = (OFHello) factory.getMessage(OFType.HELLO);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080053 hello2.setXid(2);
Ray Milkey269ffb92014-04-03 14:43:30 -070054
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055 }
Ray Milkey269ffb92014-04-03 14:43:30 -070056
57 protected void doWrite(boolean expectWrite,
58 OFMessageDamperMockSwitch sw,
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059 OFMessage msg,
60 FloodlightContext cntx) throws IOException {
Ray Milkey269ffb92014-04-03 14:43:30 -070061
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080062 boolean result;
63 sw.reset();
64 result = damper.write(sw, msg, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -070065
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080066 if (expectWrite) {
67 assertEquals(true, result);
68 sw.assertMessageWasWritten(msg, cntx);
69 } else {
70 assertEquals(false, result);
71 sw.assertNoMessageWritten();
72 }
73 }
Ray Milkey269ffb92014-04-03 14:43:30 -070074
75
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080076 @Test
77 public void testOneMessageType() throws IOException, InterruptedException {
78 int timeout = 50;
Ray Milkey269ffb92014-04-03 14:43:30 -070079 int sleepTime = 60;
80 damper = new OFMessageDamper(100,
81 EnumSet.of(OFType.ECHO_REQUEST),
82 timeout);
83
84
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085 // echo requests should be dampened
86 doWrite(true, sw1, echoRequst1, cntx);
87 doWrite(false, sw1, echoRequst1, cntx);
88 doWrite(false, sw1, echoRequst1Clone, cntx);
89 doWrite(true, sw1, echoRequst2, cntx);
90 doWrite(false, sw1, echoRequst2, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -070091
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080092 // we don't dampen hellos. All should succeed
93 doWrite(true, sw1, hello1, cntx);
94 doWrite(true, sw1, hello1, cntx);
95 doWrite(true, sw1, hello1, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -070096
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080097 // echo request should also be dampened on sw2
98 doWrite(true, sw2, echoRequst1, cntx);
99 doWrite(false, sw2, echoRequst1, cntx);
100 doWrite(true, sw2, echoRequst2, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700101
102
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800103 Thread.sleep(sleepTime);
104 doWrite(true, sw1, echoRequst1, cntx);
105 doWrite(true, sw2, echoRequst1, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700106
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700108
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800109 @Test
110 public void testTwoMessageTypes() throws IOException, InterruptedException {
111 int timeout = 50;
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 int sleepTime = 60;
113 damper = new OFMessageDamper(100,
114 EnumSet.of(OFType.ECHO_REQUEST,
115 OFType.HELLO),
116 timeout);
117
118
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800119 // echo requests should be dampened
120 doWrite(true, sw1, echoRequst1, cntx);
121 doWrite(false, sw1, echoRequst1, cntx);
122 doWrite(false, sw1, echoRequst1Clone, cntx);
123 doWrite(true, sw1, echoRequst2, cntx);
124 doWrite(false, sw1, echoRequst2, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700125
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800126 // hello should be dampened as well
127 doWrite(true, sw1, hello1, cntx);
128 doWrite(false, sw1, hello1, cntx);
129 doWrite(false, sw1, hello1, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700130
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 doWrite(true, sw1, hello2, cntx);
132 doWrite(false, sw1, hello2, cntx);
133 doWrite(false, sw1, hello2, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700134
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800135 // echo request should also be dampened on sw2
136 doWrite(true, sw2, echoRequst1, cntx);
137 doWrite(false, sw2, echoRequst1, cntx);
138 doWrite(true, sw2, echoRequst2, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700139
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800140 Thread.sleep(sleepTime);
141 doWrite(true, sw1, echoRequst1, cntx);
142 doWrite(true, sw2, echoRequst1, cntx);
143 doWrite(true, sw1, hello1, cntx);
144 doWrite(true, sw1, hello2, cntx);
145 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700146
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800147}