Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.util; |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
| 5 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame^] | 6 | import net.onrc.onos.core.util.Dpid; |
| 7 | import net.onrc.onos.core.util.FlowEntry; |
| 8 | import net.onrc.onos.core.util.FlowEntryAction; |
| 9 | import net.onrc.onos.core.util.FlowEntryActions; |
| 10 | import net.onrc.onos.core.util.FlowEntryErrorState; |
| 11 | import net.onrc.onos.core.util.FlowEntryId; |
| 12 | import net.onrc.onos.core.util.FlowEntryMatch; |
| 13 | import net.onrc.onos.core.util.FlowEntrySwitchState; |
| 14 | import net.onrc.onos.core.util.FlowEntryUserState; |
| 15 | import net.onrc.onos.core.util.FlowId; |
| 16 | import net.onrc.onos.core.util.IPv4; |
| 17 | import net.onrc.onos.core.util.IPv4Net; |
| 18 | import net.onrc.onos.core.util.Port; |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 19 | |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | public class FlowEntryTest { |
| 24 | |
| 25 | FlowEntry entry; |
| 26 | |
| 27 | FlowId flowId = new FlowId(0x1234); |
| 28 | FlowEntryId flowEntryId = new FlowEntryId(0x5678); |
Pavlin Radoslavov | 5139c0b | 2013-12-09 18:04:53 -0800 | [diff] [blame] | 29 | int idleTimeout = 5; |
| 30 | int hardTimeout = 10; |
Pavlin Radoslavov | afbf103 | 2014-02-04 10:37:52 -0800 | [diff] [blame] | 31 | int priority = 15; |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 32 | FlowEntryMatch match; |
| 33 | FlowEntryActions actions; |
| 34 | |
| 35 | Dpid dpid = new Dpid(0xCAFE); |
| 36 | |
| 37 | Port inport = new Port((short)1); |
| 38 | byte[] byte1 = { 1, 2, 3, 4, 5, 6 }; |
| 39 | byte[] byte2 = { 6, 5, 4, 3, 2, 1 }; |
| 40 | MACAddress mac1 = new MACAddress(byte1); |
| 41 | MACAddress mac2 = new MACAddress(byte2); |
| 42 | Short ether = Short.valueOf((short)2); |
| 43 | Short vlanid = Short.valueOf((short)3); |
| 44 | Byte vlanprio = Byte.valueOf((byte)4); |
| 45 | IPv4Net ip1 = new IPv4Net("127.0.0.1/32"); |
| 46 | IPv4Net ip2 = new IPv4Net( new IPv4("127.0.0.2"), (short)32); |
| 47 | IPv4 ipaddr1 = new IPv4("127.0.0.3"); |
| 48 | IPv4 ipaddr2 = new IPv4("127.0.0.4"); |
| 49 | Byte ipproto = Byte.valueOf((byte)5); |
| 50 | Byte ipToS = Byte.valueOf((byte)6); |
| 51 | Short tport1 = Short.valueOf((short)7); |
| 52 | Short tport2 = Short.valueOf((short)8); |
| 53 | Port outport = new Port((short)9); |
| 54 | Port queueport = new Port((short)10); |
| 55 | int queueId = 11; |
| 56 | |
| 57 | FlowEntryErrorState errorState = new FlowEntryErrorState( (short)12, (short)13); |
| 58 | |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() throws Exception{ |
| 62 | entry = new FlowEntry(); |
| 63 | |
| 64 | flowId = new FlowId("0x1234"); |
| 65 | entry.setFlowId( flowId ); |
| 66 | |
| 67 | flowEntryId = new FlowEntryId("0x5678"); |
| 68 | entry.setFlowEntryId(flowEntryId); |
Pavlin Radoslavov | 5139c0b | 2013-12-09 18:04:53 -0800 | [diff] [blame] | 69 | |
| 70 | entry.setIdleTimeout(5); |
| 71 | entry.setHardTimeout(10); |
Pavlin Radoslavov | afbf103 | 2014-02-04 10:37:52 -0800 | [diff] [blame] | 72 | entry.setPriority(15); |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 73 | |
| 74 | dpid = new Dpid("CA:FE"); |
| 75 | entry.setDpid( dpid ); |
| 76 | |
| 77 | entry.setInPort( inport ); |
| 78 | entry.setOutPort( outport ); |
| 79 | |
| 80 | match = new FlowEntryMatch(); |
| 81 | match.enableInPort( inport); |
| 82 | match.enableSrcMac( mac1 ); |
| 83 | match.enableDstMac( mac2 ); |
| 84 | match.enableEthernetFrameType( ether ); |
| 85 | match.enableVlanId( vlanid ); |
| 86 | match.enableVlanPriority( vlanprio ); |
| 87 | match.enableSrcIPv4Net( ip1 ); |
| 88 | match.enableDstIPv4Net( ip2 ); |
| 89 | match.enableIpProto( ipproto ); |
| 90 | match.enableIpToS( ipToS ); |
| 91 | match.enableSrcTcpUdpPort( tport1 ); |
| 92 | match.enableDstTcpUdpPort( tport2 ); |
| 93 | |
| 94 | entry.setFlowEntryMatch( match ); |
| 95 | |
| 96 | FlowEntryAction action = null; |
| 97 | actions = entry.flowEntryActions(); |
| 98 | |
| 99 | action = new FlowEntryAction(); |
| 100 | action.setActionOutput(outport); |
| 101 | actions.addAction(action); |
| 102 | |
| 103 | action = new FlowEntryAction(); |
| 104 | action.setActionOutputToController((short)0); |
| 105 | actions.addAction(action); |
| 106 | |
| 107 | action = new FlowEntryAction(); |
| 108 | action.setActionSetVlanId(vlanid); |
| 109 | actions.addAction(action); |
| 110 | |
| 111 | action = new FlowEntryAction(); |
| 112 | action.setActionSetVlanPriority(vlanprio); |
| 113 | actions.addAction(action); |
| 114 | |
| 115 | action = new FlowEntryAction(); |
| 116 | action.setActionStripVlan(true); |
| 117 | actions.addAction(action); |
| 118 | |
| 119 | action = new FlowEntryAction(); |
| 120 | action.setActionSetEthernetSrcAddr(mac1); |
| 121 | actions.addAction(action); |
| 122 | |
| 123 | action = new FlowEntryAction(); |
| 124 | action.setActionSetEthernetDstAddr(mac2); |
| 125 | actions.addAction(action); |
| 126 | |
| 127 | action = new FlowEntryAction(); |
| 128 | action.setActionSetIPv4SrcAddr(ipaddr1); |
| 129 | actions.addAction(action); |
| 130 | |
| 131 | action = new FlowEntryAction(); |
| 132 | action.setActionSetIPv4DstAddr(ipaddr2); |
| 133 | actions.addAction(action); |
| 134 | |
| 135 | action = new FlowEntryAction(); |
| 136 | action.setActionSetIpToS(ipToS); |
| 137 | actions.addAction(action); |
| 138 | |
| 139 | action = new FlowEntryAction(); |
| 140 | action.setActionSetTcpUdpSrcPort(tport1); |
| 141 | actions.addAction(action); |
| 142 | |
| 143 | action = new FlowEntryAction(); |
| 144 | action.setActionSetTcpUdpDstPort(tport2); |
| 145 | actions.addAction(action); |
| 146 | |
| 147 | action = new FlowEntryAction(); |
| 148 | action.setActionEnqueue(queueport, queueId); |
| 149 | actions.addAction(action); |
| 150 | |
| 151 | entry.setFlowEntryUserState( FlowEntryUserState.FE_USER_ADD ); |
| 152 | entry.setFlowEntrySwitchState( FlowEntrySwitchState.FE_SWITCH_UPDATED ); |
| 153 | entry.setFlowEntryErrorState( errorState ); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testFlowEntry(){ |
| 159 | FlowEntry e = new FlowEntry(); |
| 160 | |
| 161 | assertTrue( e.flowEntryActions().isEmpty() ); |
| 162 | assertEquals("flowEntryUserState", FlowEntryUserState.FE_USER_UNKNOWN, e.flowEntryUserState() ); |
| 163 | assertEquals("flowEntrySwitchState", FlowEntrySwitchState.FE_SWITCH_UNKNOWN, e.flowEntrySwitchState() ); |
| 164 | } |
| 165 | |
| 166 | @Test |
Pavlin Radoslavov | b1309e8 | 2013-10-30 12:18:28 -0700 | [diff] [blame] | 167 | public void testFlowId(){ |
| 168 | assertEquals("flowId", flowId, entry.flowId() ); |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
Pavlin Radoslavov | 062c72d | 2013-10-30 12:42:15 -0700 | [diff] [blame] | 172 | public void testIsValidFlowId(){ |
| 173 | FlowEntry e = new FlowEntry(); |
| 174 | |
| 175 | // Test a Flow Entry with empty Flow ID |
| 176 | assertEquals("isValidFlowId", false, e.isValidFlowId() ); |
| 177 | |
| 178 | // Test a Flow Entry with invalid Flow ID |
| 179 | e.setFlowId(new FlowId()); |
| 180 | assertEquals("isValidFlowId", false, e.isValidFlowId() ); |
| 181 | |
| 182 | // Test a Flow Entry with valid Flow ID |
| 183 | e.setFlowId(new FlowId(0x1)); |
| 184 | assertEquals("isValidFlowId", true, e.isValidFlowId() ); |
| 185 | assertEquals("isValidFlowId", true, entry.isValidFlowId() ); |
| 186 | } |
| 187 | |
| 188 | @Test |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 189 | public void testFlowEntryId(){ |
| 190 | assertEquals("flowEntryId", flowEntryId, entry.flowEntryId() ); |
| 191 | } |
| 192 | |
| 193 | @Test |
Pavlin Radoslavov | b1309e8 | 2013-10-30 12:18:28 -0700 | [diff] [blame] | 194 | public void testIsValidFlowEntryId(){ |
| 195 | FlowEntry e = new FlowEntry(); |
| 196 | |
| 197 | // Test a Flow Entry with empty Flow Entry ID |
| 198 | assertEquals("isValidFlowEntryId", false, e.isValidFlowEntryId() ); |
| 199 | |
| 200 | // Test a Flow Entry with invalid Flow Entry ID |
| 201 | e.setFlowEntryId(new FlowEntryId()); |
| 202 | assertEquals("isValidFlowEntryId", false, e.isValidFlowEntryId() ); |
| 203 | |
| 204 | // Test a Flow Entry with valid Flow Entry ID |
| 205 | e.setFlowEntryId(new FlowEntryId(0x1)); |
| 206 | assertEquals("isValidFlowEntryId", true, e.isValidFlowEntryId() ); |
| 207 | assertEquals("isValidFlowEntryId", true, entry.isValidFlowEntryId() ); |
| 208 | } |
| 209 | |
| 210 | @Test |
Pavlin Radoslavov | 5139c0b | 2013-12-09 18:04:53 -0800 | [diff] [blame] | 211 | public void testIdleTimeout(){ |
| 212 | assertEquals("idleTimeout", idleTimeout, entry.idleTimeout() ); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testHardTimeout(){ |
| 217 | assertEquals("hardTimeout", hardTimeout, entry.hardTimeout() ); |
| 218 | } |
| 219 | |
| 220 | @Test |
Pavlin Radoslavov | afbf103 | 2014-02-04 10:37:52 -0800 | [diff] [blame] | 221 | public void testPriority(){ |
| 222 | assertEquals("priority", priority, entry.priority() ); |
| 223 | } |
| 224 | |
| 225 | @Test |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 226 | public void testFlowEntryMatch(){ |
| 227 | assertEquals("flowEntryMatch", match, entry.flowEntryMatch() ); |
| 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testFlowEntryActions(){ |
| 232 | assertEquals("flowEntryActions", actions, entry.flowEntryActions() ); |
| 233 | } |
| 234 | |
| 235 | @Test |
| 236 | public void testSetFlowEntryActions(){ |
| 237 | FlowEntryActions actions = new FlowEntryActions(); |
| 238 | entry.setFlowEntryActions( actions ); |
| 239 | assertEquals("flowEntryActions", actions, entry.flowEntryActions() ); |
| 240 | } |
| 241 | |
| 242 | @Test |
| 243 | public void testDpid(){ |
| 244 | assertEquals("dpid", dpid, entry.dpid() ); |
| 245 | } |
| 246 | |
| 247 | @Test |
| 248 | public void testInPort(){ |
| 249 | assertEquals("inPort", inport, entry.inPort() ); |
| 250 | } |
| 251 | |
| 252 | @Test |
| 253 | public void testOutPort(){ |
| 254 | assertEquals("outPort", outport, entry.outPort() ); |
| 255 | } |
| 256 | |
| 257 | @Test |
| 258 | public void testFlowEntryUserState(){ |
| 259 | assertEquals("flowEntryUserState", FlowEntryUserState.FE_USER_ADD, entry.flowEntryUserState() ); |
| 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void testFlowEntrySwitchState(){ |
| 264 | assertEquals("flowEntrySwitchState", FlowEntrySwitchState.FE_SWITCH_UPDATED, entry.flowEntrySwitchState() ); |
| 265 | } |
| 266 | |
| 267 | @Test |
| 268 | public void testFlowEntryErrorState(){ |
| 269 | assertEquals("flowEntryErrorState", errorState, entry.flowEntryErrorState() ); |
| 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testToString(){ |
| 274 | FlowEntry def = new FlowEntry(); |
Pavlin Radoslavov | afbf103 | 2014-02-04 10:37:52 -0800 | [diff] [blame] | 275 | assertEquals("toString", def.toString(), "[ idleTimeout=0 hardTimeout=0 priority=32768 flowEntryActions=[] flowEntryUserState=FE_USER_UNKNOWN flowEntrySwitchState=FE_SWITCH_UNKNOWN]" ); |
| 276 | assertEquals("toString", entry.toString(), "[flowEntryId=0x5678 flowId=0x1234 idleTimeout=5 hardTimeout=10 priority=15 flowEntryMatch=[inPort=1 srcMac=01:02:03:04:05:06 dstMac=06:05:04:03:02:01 ethernetFrameType=2 vlanId=3 vlanPriority=4 srcIPv4Net=127.0.0.1/32 dstIPv4Net=127.0.0.2/32 ipProto=5 ipToS=6 srcTcpUdpPort=7 dstTcpUdpPort=8] flowEntryActions=[[type=ACTION_OUTPUT action=[port=9 maxLen=0]];[type=ACTION_OUTPUT action=[port=-3 maxLen=0]];[type=ACTION_SET_VLAN_VID action=[vlanId=3]];[type=ACTION_SET_VLAN_PCP action=[vlanPriority=4]];[type=ACTION_STRIP_VLAN action=[stripVlan=true]];[type=ACTION_SET_DL_SRC action=[addr=01:02:03:04:05:06]];[type=ACTION_SET_DL_DST action=[addr=06:05:04:03:02:01]];[type=ACTION_SET_NW_SRC action=[addr=127.0.0.3]];[type=ACTION_SET_NW_DST action=[addr=127.0.0.4]];[type=ACTION_SET_NW_TOS action=[ipToS=6]];[type=ACTION_SET_TP_SRC action=[port=7]];[type=ACTION_SET_TP_DST action=[port=8]];[type=ACTION_ENQUEUE action=[port=10 queueId=11]];] dpid=00:00:00:00:00:00:ca:fe inPort=1 outPort=9 flowEntryUserState=FE_USER_ADD flowEntrySwitchState=FE_SWITCH_UPDATED flowEntryErrorState=[type=12 code=13]]" ); |
HIGUCHI Yuta | d8dc9c0 | 2013-08-04 06:16:30 +0900 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | } |