Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.flowmanager; |
| 2 | |
| 3 | import static org.easymock.EasyMock.createMock; |
| 4 | import static org.easymock.EasyMock.expect; |
| 5 | import static org.easymock.EasyMock.replay; |
| 6 | import static org.junit.Assert.assertEquals; |
| 7 | import static org.junit.Assert.assertNotNull; |
| 8 | |
| 9 | import java.util.Arrays; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import net.floodlightcontroller.util.MACAddress; |
| 13 | import net.onrc.onos.core.matchaction.MatchAction; |
| 14 | import net.onrc.onos.core.matchaction.MatchActionIdGeneratorWithIdBlockAllocator; |
| 15 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
| 16 | import net.onrc.onos.core.matchaction.MatchActionOperations; |
| 17 | import net.onrc.onos.core.matchaction.MatchActionOperations.Operator; |
| 18 | import net.onrc.onos.core.matchaction.MatchActionOperationsIdGeneratorWithIdBlockAllocator; |
| 19 | import net.onrc.onos.core.matchaction.action.Action; |
| 20 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 21 | import net.onrc.onos.core.matchaction.action.OutputAction; |
| 22 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 23 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
| 24 | import net.onrc.onos.core.util.IdBlock; |
| 25 | import net.onrc.onos.core.util.IdBlockAllocator; |
| 26 | import net.onrc.onos.core.util.PortNumber; |
| 27 | import net.onrc.onos.core.util.SwitchPort; |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.serializers.KryoFactory; |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame] | 29 | |
| 30 | import org.junit.After; |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for {@link PacketPathFlow} class. |
| 36 | */ |
| 37 | public class PacketPathFlowTest { |
| 38 | private Path pathWith4Switches; |
| 39 | private Path pathWith2Switches; |
| 40 | private PacketMatch match; |
| 41 | private List<Action> actions; |
| 42 | private IdBlockAllocator allocator; |
| 43 | |
| 44 | @Before |
| 45 | public void setUp() throws Exception { |
| 46 | allocator = createMock(IdBlockAllocator.class); |
| 47 | expect(allocator.allocateUniqueIdBlock()) |
| 48 | .andReturn(new IdBlock(0, 99)) |
| 49 | .andReturn(new IdBlock(100, 199)); |
| 50 | replay(allocator); |
| 51 | |
| 52 | pathWith4Switches = new Path(); |
| 53 | pathWith4Switches.add(new FlowLink( |
| 54 | new SwitchPort(1, (short) 10), new SwitchPort(2, (short) 11))); |
| 55 | pathWith4Switches.add(new FlowLink( |
| 56 | new SwitchPort(2, (short) 12), new SwitchPort(3, (short) 13))); |
| 57 | pathWith4Switches.add(new FlowLink( |
| 58 | new SwitchPort(3, (short) 14), new SwitchPort(4, (short) 15))); |
| 59 | |
| 60 | pathWith2Switches = new Path(); |
| 61 | pathWith2Switches.add(new FlowLink( |
| 62 | new SwitchPort(1, (short) 10), new SwitchPort(2, (short) 11))); |
| 63 | |
| 64 | PacketMatchBuilder builder = new PacketMatchBuilder(); |
| 65 | builder.setDstMac(MACAddress.valueOf(54321)); |
| 66 | match = builder.build(); |
| 67 | |
| 68 | actions = Arrays.asList( |
| 69 | new ModifyDstMacAction(MACAddress.valueOf(12345)), |
| 70 | new OutputAction(PortNumber.uint32(101))); |
| 71 | } |
| 72 | |
| 73 | @After |
| 74 | public void tearDown() throws Exception { |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks the constructor initializes fields properly. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testConstructor() { |
| 82 | PacketPathFlow flow = new PacketPathFlow( |
| 83 | new FlowId(1L), match, PortNumber.uint32(100), |
| 84 | pathWith4Switches, actions, 0, 0); |
| 85 | |
| 86 | assertNotNull(flow); |
| 87 | assertEquals(new FlowId(1L), flow.getId()); |
| 88 | assertEquals(match, flow.getMatch()); |
| 89 | assertEquals(PortNumber.uint32(100), flow.getIngressPortNumber()); |
| 90 | assertEquals(pathWith4Switches, flow.getPath()); |
| 91 | assertEquals(actions, flow.getEgressActions()); |
| 92 | assertEquals(0, flow.getHardTimeout()); |
| 93 | assertEquals(0, flow.getIdleTimeout()); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Checks the compile method with add-operation. This test creates a flow |
| 98 | * object using the path with 2 switches, then checks if the compiled the |
| 99 | * list of MatchActionOperations objects are generated properly. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testCompileWithAddOperationFor2Switches() { |
| 103 | PacketPathFlow flow = new PacketPathFlow( |
| 104 | new FlowId(1L), match, PortNumber.uint32(100), |
| 105 | pathWith2Switches, actions, 0, 0); |
| 106 | |
| 107 | List<MatchActionOperations> maOpsList = |
| 108 | flow.compile(FlowBatchOperation.Operator.ADD, |
| 109 | new MatchActionIdGeneratorWithIdBlockAllocator(allocator), |
| 110 | new MatchActionOperationsIdGeneratorWithIdBlockAllocator( |
| 111 | allocator) |
| 112 | ); |
| 113 | |
| 114 | assertEquals(2, maOpsList.size()); |
| 115 | |
| 116 | MatchActionOperations firstOp = maOpsList.get(0); |
| 117 | assertEquals(1, firstOp.size()); |
| 118 | assertEquals(1, firstOp.getOperations().size()); |
| 119 | |
| 120 | MatchActionOperations secondOp = maOpsList.get(1); |
| 121 | assertEquals(1, secondOp.size()); |
| 122 | assertEquals(1, secondOp.getOperations().size()); |
| 123 | |
| 124 | MatchActionOperationEntry entry1 = secondOp.getOperations().get(0); |
| 125 | MatchActionOperationEntry entry2 = firstOp.getOperations().get(0); |
| 126 | |
| 127 | assertEquals(Operator.ADD, entry1.getOperator()); |
| 128 | assertEquals(Operator.ADD, entry2.getOperator()); |
| 129 | |
| 130 | MatchAction ma1 = entry1.getTarget(); |
| 131 | MatchAction ma2 = entry2.getTarget(); |
| 132 | |
| 133 | assertNotNull(ma1); |
| 134 | assertNotNull(ma2); |
| 135 | |
| 136 | assertEquals(new SwitchPort(1, (short) 100), ma1.getSwitchPort()); |
| 137 | assertEquals(new SwitchPort(2, (short) 11), ma2.getSwitchPort()); |
| 138 | |
| 139 | assertEquals(match, ma1.getMatch()); |
| 140 | assertEquals(match, ma2.getMatch()); |
| 141 | |
| 142 | assertNotNull(ma1.getActions()); |
| 143 | assertNotNull(ma2.getActions()); |
| 144 | |
| 145 | assertEquals(1, ma1.getActions().size()); |
| 146 | assertEquals(2, ma2.getActions().size()); |
| 147 | |
| 148 | assertEquals(new OutputAction(PortNumber.uint32(10)), |
| 149 | ma1.getActions().get(0)); |
| 150 | assertEquals(new ModifyDstMacAction(MACAddress.valueOf(12345)), |
| 151 | ma2.getActions().get(0)); |
| 152 | assertEquals(new OutputAction(PortNumber.uint32(101)), |
| 153 | ma2.getActions().get(1)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Checks the compile method with add-operation. This test creates a flow |
| 158 | * object using the path with 4 switches, then checks if the compiled the |
| 159 | * list of MatchActionOperations objects are generated properly. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testCompileWithAddOperationFor4Switches() { |
| 163 | PacketPathFlow flow = new PacketPathFlow( |
| 164 | new FlowId(1L), match, PortNumber.uint32(100), |
| 165 | pathWith4Switches, actions, 0, 0); |
| 166 | |
| 167 | List<MatchActionOperations> maOpsList = |
| 168 | flow.compile(FlowBatchOperation.Operator.ADD, |
| 169 | new MatchActionIdGeneratorWithIdBlockAllocator(allocator), |
| 170 | new MatchActionOperationsIdGeneratorWithIdBlockAllocator( |
| 171 | allocator) |
| 172 | ); |
| 173 | |
| 174 | assertEquals(2, maOpsList.size()); |
| 175 | |
| 176 | MatchActionOperations firstOp = maOpsList.get(0); |
| 177 | assertEquals(3, firstOp.size()); |
| 178 | assertEquals(3, firstOp.getOperations().size()); |
| 179 | |
| 180 | MatchActionOperations secondOp = maOpsList.get(1); |
| 181 | assertEquals(1, secondOp.size()); |
| 182 | assertEquals(1, secondOp.getOperations().size()); |
| 183 | |
| 184 | MatchActionOperationEntry entry1 = secondOp.getOperations().get(0); |
| 185 | MatchActionOperationEntry entry2 = firstOp.getOperations().get(0); |
| 186 | MatchActionOperationEntry entry3 = firstOp.getOperations().get(1); |
| 187 | MatchActionOperationEntry entry4 = firstOp.getOperations().get(2); |
| 188 | |
| 189 | assertEquals(Operator.ADD, entry1.getOperator()); |
| 190 | assertEquals(Operator.ADD, entry2.getOperator()); |
| 191 | assertEquals(Operator.ADD, entry3.getOperator()); |
| 192 | assertEquals(Operator.ADD, entry4.getOperator()); |
| 193 | |
| 194 | MatchAction ma1 = entry1.getTarget(); |
| 195 | MatchAction ma2 = entry2.getTarget(); |
| 196 | MatchAction ma3 = entry3.getTarget(); |
| 197 | MatchAction ma4 = entry4.getTarget(); |
| 198 | |
| 199 | assertNotNull(ma1); |
| 200 | assertNotNull(ma2); |
| 201 | assertNotNull(ma3); |
| 202 | assertNotNull(ma4); |
| 203 | |
| 204 | assertEquals(new SwitchPort(1, (short) 100), ma1.getSwitchPort()); |
| 205 | assertEquals(new SwitchPort(2, (short) 11), ma2.getSwitchPort()); |
| 206 | assertEquals(new SwitchPort(3, (short) 13), ma3.getSwitchPort()); |
| 207 | assertEquals(new SwitchPort(4, (short) 15), ma4.getSwitchPort()); |
| 208 | |
| 209 | assertEquals(match, ma1.getMatch()); |
| 210 | assertEquals(match, ma2.getMatch()); |
| 211 | assertEquals(match, ma3.getMatch()); |
| 212 | assertEquals(match, ma4.getMatch()); |
| 213 | |
| 214 | assertNotNull(ma1.getActions()); |
| 215 | assertNotNull(ma2.getActions()); |
| 216 | assertNotNull(ma3.getActions()); |
| 217 | assertNotNull(ma4.getActions()); |
| 218 | |
| 219 | assertEquals(1, ma1.getActions().size()); |
| 220 | assertEquals(1, ma2.getActions().size()); |
| 221 | assertEquals(1, ma3.getActions().size()); |
| 222 | assertEquals(2, ma4.getActions().size()); |
| 223 | |
| 224 | assertEquals(new OutputAction(PortNumber.uint32(10)), |
| 225 | ma1.getActions().get(0)); |
| 226 | assertEquals(new OutputAction(PortNumber.uint32(12)), |
| 227 | ma2.getActions().get(0)); |
| 228 | assertEquals(new OutputAction(PortNumber.uint32(14)), |
| 229 | ma3.getActions().get(0)); |
| 230 | assertEquals(new ModifyDstMacAction(MACAddress.valueOf(12345)), |
| 231 | ma4.getActions().get(0)); |
| 232 | assertEquals(new OutputAction(PortNumber.uint32(101)), |
| 233 | ma4.getActions().get(1)); |
| 234 | } |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 235 | |
| 236 | /** |
| 237 | * Tests if the object can be serialized and deserialized properly with |
| 238 | * Kryo. |
| 239 | */ |
| 240 | @Test |
| 241 | public void testKryo() { |
| 242 | final FlowId id = new FlowId(1); |
| 243 | final PortNumber ingressPort = PortNumber.uint32(12345); |
| 244 | |
| 245 | final PacketPathFlow originalFlow = |
| 246 | new PacketPathFlow(id, match, ingressPort, pathWith4Switches, |
| 247 | actions, 1000, 100); |
| 248 | |
| 249 | assertNotNull(originalFlow); |
| 250 | byte[] buf = KryoFactory.serialize(originalFlow); |
| 251 | |
| 252 | final PacketPathFlow obtainedFlow = KryoFactory.deserialize(buf); |
| 253 | |
| 254 | assertEquals(id, obtainedFlow.getId()); |
| 255 | assertEquals(match, obtainedFlow.getMatch()); |
| 256 | assertEquals(ingressPort, obtainedFlow.getIngressPortNumber()); |
| 257 | assertEquals(pathWith4Switches, obtainedFlow.getPath()); |
| 258 | assertEquals(actions, obtainedFlow.getEgressActions()); |
| 259 | assertEquals(1000, obtainedFlow.getHardTimeout()); |
| 260 | assertEquals(100, obtainedFlow.getIdleTimeout()); |
| 261 | } |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame] | 262 | } |