Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.util; |
| 2 | |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| 5 | import java.io.IOException; |
| 6 | import java.util.EnumSet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 7 | |
| 8 | import net.floodlightcontroller.core.FloodlightContext; |
| 9 | |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | import org.openflow.protocol.OFEchoRequest; |
| 13 | import org.openflow.protocol.OFHello; |
| 14 | import org.openflow.protocol.OFMessage; |
| 15 | import org.openflow.protocol.OFType; |
| 16 | import org.openflow.protocol.factory.BasicFactory; |
| 17 | import org.openflow.protocol.factory.OFMessageFactory; |
| 18 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | public class OFMessageDamperTest { |
| 20 | OFMessageFactory factory; |
| 21 | OFMessageDamper damper; |
| 22 | FloodlightContext cntx; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 23 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | OFMessageDamperMockSwitch sw1; |
| 25 | OFMessageDamperMockSwitch sw2; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 26 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 27 | OFEchoRequest echoRequst1; |
| 28 | OFEchoRequest echoRequst1Clone; |
| 29 | OFEchoRequest echoRequst2; |
| 30 | OFHello hello1; |
| 31 | OFHello hello2; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 32 | |
| 33 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 34 | @Before |
| 35 | public void setUp() throws IOException { |
| 36 | factory = new BasicFactory(); |
| 37 | cntx = new FloodlightContext(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 38 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 39 | sw1 = new OFMessageDamperMockSwitch(); |
| 40 | sw2 = new OFMessageDamperMockSwitch(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 41 | |
| 42 | echoRequst1 = (OFEchoRequest) factory.getMessage(OFType.ECHO_REQUEST); |
| 43 | echoRequst1.setPayload(new byte[]{1}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 44 | echoRequst1Clone = (OFEchoRequest) |
| 45 | factory.getMessage(OFType.ECHO_REQUEST); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 46 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 51 | hello1.setXid(1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 52 | hello2 = (OFHello) factory.getMessage(OFType.HELLO); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 53 | hello2.setXid(2); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 54 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 56 | |
| 57 | protected void doWrite(boolean expectWrite, |
| 58 | OFMessageDamperMockSwitch sw, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 59 | OFMessage msg, |
| 60 | FloodlightContext cntx) throws IOException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 61 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 62 | boolean result; |
| 63 | sw.reset(); |
| 64 | result = damper.write(sw, msg, cntx); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 65 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 66 | if (expectWrite) { |
| 67 | assertEquals(true, result); |
| 68 | sw.assertMessageWasWritten(msg, cntx); |
| 69 | } else { |
| 70 | assertEquals(false, result); |
| 71 | sw.assertNoMessageWritten(); |
| 72 | } |
| 73 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 74 | |
| 75 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 76 | @Test |
| 77 | public void testOneMessageType() throws IOException, InterruptedException { |
| 78 | int timeout = 50; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 79 | int sleepTime = 60; |
| 80 | damper = new OFMessageDamper(100, |
| 81 | EnumSet.of(OFType.ECHO_REQUEST), |
| 82 | timeout); |
| 83 | |
| 84 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 85 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 91 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 92 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 96 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 97 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 101 | |
| 102 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 103 | Thread.sleep(sleepTime); |
| 104 | doWrite(true, sw1, echoRequst1, cntx); |
| 105 | doWrite(true, sw2, echoRequst1, cntx); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 106 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 108 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 109 | @Test |
| 110 | public void testTwoMessageTypes() throws IOException, InterruptedException { |
| 111 | int timeout = 50; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 112 | int sleepTime = 60; |
| 113 | damper = new OFMessageDamper(100, |
| 114 | EnumSet.of(OFType.ECHO_REQUEST, |
| 115 | OFType.HELLO), |
| 116 | timeout); |
| 117 | |
| 118 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 125 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 126 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 130 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | doWrite(true, sw1, hello2, cntx); |
| 132 | doWrite(false, sw1, hello2, cntx); |
| 133 | doWrite(false, sw1, hello2, cntx); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 134 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 135 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 139 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 140 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 146 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 147 | } |