Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -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.HashMap; |
| 11 | import java.util.List; |
| 12 | import java.util.Map; |
| 13 | import java.util.Set; |
| 14 | |
| 15 | import net.floodlightcontroller.util.MACAddress; |
| 16 | import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator; |
| 17 | import net.onrc.onos.core.matchaction.MatchAction; |
| 18 | import net.onrc.onos.core.matchaction.MatchActionIdGeneratorWithIdBlockAllocator; |
| 19 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
| 20 | import net.onrc.onos.core.matchaction.MatchActionOperations; |
| 21 | import net.onrc.onos.core.matchaction.MatchActionOperationsIdGeneratorWithIdBlockAllocator; |
| 22 | import net.onrc.onos.core.matchaction.action.Action; |
| 23 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 24 | import net.onrc.onos.core.matchaction.action.OutputAction; |
| 25 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 26 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
| 27 | import net.onrc.onos.core.util.IdBlock; |
| 28 | import net.onrc.onos.core.util.IdBlockAllocator; |
| 29 | import net.onrc.onos.core.util.PortNumber; |
| 30 | import net.onrc.onos.core.util.SwitchPort; |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.util.serializers.KryoFactory; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 32 | |
| 33 | import org.junit.After; |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | |
| 37 | import com.google.common.collect.Sets; |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for {@link SingleDstTreeFlow} class. |
| 41 | */ |
| 42 | public class SingleDstTreeFlowTest { |
| 43 | private PacketMatch match; |
| 44 | private Set<SwitchPort> ingressPorts; |
| 45 | private Tree tree; |
| 46 | private List<Action> egressActions; |
| 47 | private IdBlockAllocator allocator; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
| 51 | allocator = createMock(IdBlockAllocator.class); |
| 52 | expect(allocator.allocateUniqueIdBlock()) |
| 53 | .andReturn(new IdBlock(0, 100)) |
| 54 | .andReturn(new IdBlock(100, 100)); |
| 55 | replay(allocator); |
| 56 | |
| 57 | PacketMatchBuilder builder = new PacketMatchBuilder(); |
| 58 | builder.setDstMac(MACAddress.valueOf(54321)); |
| 59 | match = builder.build(); |
| 60 | |
| 61 | ingressPorts = Sets.newHashSet( |
| 62 | new SwitchPort(1L, (short) 101), |
| 63 | new SwitchPort(1L, (short) 102), |
| 64 | new SwitchPort(2L, (short) 103), |
| 65 | new SwitchPort(3L, (short) 104), |
| 66 | new SwitchPort(5L, (short) 105)); |
| 67 | |
| 68 | tree = new Tree(); |
| 69 | tree.addLink(new FlowLink( |
| 70 | new SwitchPort(1L, (short) 10), |
| 71 | new SwitchPort(3L, (short) 12))); |
| 72 | tree.addLink(new FlowLink( |
| 73 | new SwitchPort(2L, (short) 11), |
| 74 | new SwitchPort(3L, (short) 13))); |
| 75 | tree.addLink(new FlowLink( |
| 76 | new SwitchPort(3L, (short) 14), |
| 77 | new SwitchPort(5L, (short) 15))); |
| 78 | tree.addLink(new FlowLink( |
| 79 | new SwitchPort(4L, (short) 16), |
| 80 | new SwitchPort(5L, (short) 17))); |
| 81 | |
| 82 | egressActions = Arrays.asList( |
| 83 | new ModifyDstMacAction(MACAddress.valueOf(12345)), |
| 84 | new OutputAction(PortNumber.uint32(100))); |
| 85 | } |
| 86 | |
| 87 | @After |
| 88 | public void tearDown() throws Exception { |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tests if constructor initialized fields properly. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testConstructor() { |
| 96 | SingleDstTreeFlow treeFlow = new SingleDstTreeFlow( |
| 97 | new FlowId(1L), match, ingressPorts, tree, egressActions); |
| 98 | |
| 99 | assertNotNull(treeFlow); |
| 100 | assertEquals(new FlowId(1L), treeFlow.getId()); |
| 101 | assertEquals(match, treeFlow.getMatch()); |
| 102 | assertEquals(ingressPorts, treeFlow.getIngressPorts()); |
| 103 | assertEquals(tree, treeFlow.getTree()); |
| 104 | assertEquals(egressActions, treeFlow.getEgressActions()); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Generates a list of {@link Action} contains {@link OutputAction} with |
| 109 | * specified output port. |
| 110 | * |
| 111 | * @param outputPort the output port number |
| 112 | * @return a list of {@link Action} contains one {@link OutputAction} |
| 113 | */ |
| 114 | private List<Action> outputAction(int outputPort) { |
| 115 | return Arrays.asList( |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 116 | (Action) new OutputAction(PortNumber.uint16((short) outputPort))); |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests if compile method with {@link Operator}.ADD generates two |
| 121 | * {@link MatchActionOperations} objects and they have |
| 122 | * {@link MatchActionOperationEntry} properly based on specified match, |
| 123 | * ingress ports, tree, and egress actinos. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testCompileWithAddOperation() { |
| 127 | SingleDstTreeFlow treeFlow = new SingleDstTreeFlow( |
| 128 | new FlowId(1L), match, ingressPorts, tree, egressActions); |
| 129 | |
| 130 | List<MatchActionOperations> maOpsList = |
| 131 | treeFlow.compile(Operator.ADD, |
| 132 | new MatchActionIdGeneratorWithIdBlockAllocator(allocator), |
| 133 | new MatchActionOperationsIdGeneratorWithIdBlockAllocator( |
| 134 | allocator)); |
| 135 | |
| 136 | assertEquals(2, maOpsList.size()); |
| 137 | |
| 138 | MatchActionOperations firstOp = maOpsList.get(0); |
| 139 | MatchActionOperations secondOp = maOpsList.get(1); |
| 140 | |
| 141 | assertEquals(4, firstOp.size()); |
| 142 | Map<SwitchPort, MatchAction> maMap1 = new HashMap<>(); |
| 143 | for (MatchActionOperationEntry entry : firstOp.getOperations()) { |
| 144 | assertEquals(MatchActionOperations.Operator.ADD, entry.getOperator()); |
| 145 | MatchAction ma = entry.getTarget(); |
| 146 | maMap1.put(ma.getSwitchPort(), ma); |
| 147 | } |
| 148 | assertEquals(4, maMap1.size()); |
| 149 | |
| 150 | assertEquals(5, secondOp.size()); |
| 151 | Map<SwitchPort, MatchAction> maMap2 = new HashMap<>(); |
| 152 | for (MatchActionOperationEntry entry : secondOp.getOperations()) { |
| 153 | assertEquals(MatchActionOperations.Operator.ADD, entry.getOperator()); |
| 154 | MatchAction ma = entry.getTarget(); |
| 155 | maMap2.put(ma.getSwitchPort(), ma); |
| 156 | } |
| 157 | assertEquals(5, maMap2.size()); |
| 158 | |
| 159 | assertEquals(Sets.newHashSet( |
| 160 | new SwitchPort(3L, (short) 12), |
| 161 | new SwitchPort(3L, (short) 13), |
| 162 | new SwitchPort(5L, (short) 15), |
| 163 | new SwitchPort(5L, (short) 17) |
| 164 | ), maMap1.keySet()); |
| 165 | |
| 166 | MatchAction ma11 = maMap1.get(new SwitchPort(3L, (short) 12)); |
| 167 | assertEquals(match, ma11.getMatch()); |
| 168 | assertEquals(outputAction(14), ma11.getActions()); |
| 169 | |
| 170 | MatchAction ma12 = maMap1.get(new SwitchPort(3L, (short) 13)); |
| 171 | assertEquals(match, ma12.getMatch()); |
| 172 | assertEquals(outputAction(14), ma12.getActions()); |
| 173 | |
| 174 | MatchAction ma13 = maMap1.get(new SwitchPort(5L, (short) 15)); |
| 175 | assertEquals(match, ma13.getMatch()); |
| 176 | assertEquals(egressActions, ma13.getActions()); |
| 177 | |
| 178 | MatchAction ma14 = maMap1.get(new SwitchPort(5L, (short) 17)); |
| 179 | assertEquals(match, ma14.getMatch()); |
| 180 | assertEquals(egressActions, ma14.getActions()); |
| 181 | |
| 182 | assertEquals(Sets.newHashSet( |
| 183 | new SwitchPort(1L, (short) 101), |
| 184 | new SwitchPort(1L, (short) 102), |
| 185 | new SwitchPort(2L, (short) 103), |
| 186 | new SwitchPort(3L, (short) 104), |
| 187 | new SwitchPort(5L, (short) 105) |
| 188 | ), maMap2.keySet()); |
| 189 | |
| 190 | MatchAction ma21 = maMap2.get(new SwitchPort(1L, (short) 101)); |
| 191 | assertEquals(match, ma21.getMatch()); |
| 192 | assertEquals(outputAction(10), ma21.getActions()); |
| 193 | |
| 194 | MatchAction ma22 = maMap2.get(new SwitchPort(1L, (short) 102)); |
| 195 | assertEquals(match, ma22.getMatch()); |
| 196 | assertEquals(outputAction(10), ma22.getActions()); |
| 197 | |
| 198 | MatchAction ma23 = maMap2.get(new SwitchPort(2L, (short) 103)); |
| 199 | assertEquals(match, ma23.getMatch()); |
| 200 | assertEquals(outputAction(11), ma23.getActions()); |
| 201 | |
| 202 | MatchAction ma24 = maMap2.get(new SwitchPort(3L, (short) 104)); |
| 203 | assertEquals(match, ma24.getMatch()); |
| 204 | assertEquals(outputAction(14), ma24.getActions()); |
| 205 | |
| 206 | MatchAction ma25 = maMap2.get(new SwitchPort(5L, (short) 105)); |
| 207 | assertEquals(match, ma25.getMatch()); |
| 208 | assertEquals(egressActions, ma25.getActions()); |
| 209 | } |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * Tests if the object can be serialized and deserialized properly with |
| 213 | * Kryo. |
| 214 | */ |
| 215 | @Test |
| 216 | public void testKryo() { |
| 217 | SingleDstTreeFlow originalFlow = new SingleDstTreeFlow( |
| 218 | new FlowId(1L), match, ingressPorts, tree, egressActions); |
| 219 | |
| 220 | assertNotNull(originalFlow); |
| 221 | byte[] buf = KryoFactory.serialize(originalFlow); |
| 222 | |
| 223 | final SingleDstTreeFlow obtainedFlow = KryoFactory.deserialize(buf); |
| 224 | |
| 225 | assertEquals(new FlowId(1L), obtainedFlow.getId()); |
| 226 | assertEquals(match, obtainedFlow.getMatch()); |
| 227 | assertEquals(ingressPorts, obtainedFlow.getIngressPorts()); |
| 228 | assertEquals(tree, obtainedFlow.getTree()); |
| 229 | assertEquals(egressActions, obtainedFlow.getEgressActions()); |
| 230 | } |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 231 | } |