Toshio Koide | d6cbec3 | 2014-08-27 15:23:44 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.flowmanager; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertNotNull; |
| 6 | import static org.junit.Assert.assertNull; |
| 7 | import static org.junit.Assert.assertTrue; |
| 8 | |
| 9 | import java.util.Arrays; |
| 10 | import java.util.List; |
| 11 | import java.util.Set; |
| 12 | |
| 13 | import net.floodlightcontroller.util.MACAddress; |
| 14 | import net.onrc.onos.api.flowmanager.FlowBatchId; |
| 15 | import net.onrc.onos.api.flowmanager.FlowBatchOperation; |
| 16 | import net.onrc.onos.api.flowmanager.FlowBatchState; |
| 17 | import net.onrc.onos.api.flowmanager.FlowId; |
| 18 | import net.onrc.onos.api.flowmanager.FlowLink; |
| 19 | import net.onrc.onos.api.flowmanager.PacketPathFlow; |
| 20 | import net.onrc.onos.api.flowmanager.Path; |
| 21 | import net.onrc.onos.core.datagrid.ISharedCollectionsService; |
| 22 | import net.onrc.onos.core.datastore.hazelcast.DummySharedCollectionsService; |
| 23 | import net.onrc.onos.core.matchaction.action.Action; |
| 24 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 25 | import net.onrc.onos.core.matchaction.action.OutputAction; |
| 26 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 27 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
| 28 | import net.onrc.onos.core.util.PortNumber; |
| 29 | import net.onrc.onos.core.util.SwitchPort; |
| 30 | |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | |
| 34 | public class SharedFlowBatchMapTest { |
| 35 | private ISharedCollectionsService scs; |
| 36 | private FlowBatchOperation flowOp; |
| 37 | |
| 38 | @Before |
| 39 | public void setUp() throws Exception { |
| 40 | scs = new DummySharedCollectionsService(); |
| 41 | |
| 42 | Path path = new Path(); |
| 43 | path.add(new FlowLink( |
| 44 | new SwitchPort(1, (short) 10), new SwitchPort(2, (short) 11))); |
| 45 | |
| 46 | PacketMatchBuilder builder = new PacketMatchBuilder(); |
| 47 | builder.setDstMac(MACAddress.valueOf(54321)); |
| 48 | PacketMatch match1 = builder.build(); |
| 49 | |
| 50 | builder = new PacketMatchBuilder(); |
| 51 | builder.setDstMac(MACAddress.valueOf(12345)); |
| 52 | PacketMatch match2 = builder.build(); |
| 53 | |
| 54 | List<Action> actions1 = Arrays.asList( |
| 55 | new ModifyDstMacAction(MACAddress.valueOf(12345)), |
| 56 | new OutputAction(PortNumber.uint16((short) 101))); |
| 57 | |
| 58 | List<Action> actions2 = Arrays.asList( |
| 59 | new ModifyDstMacAction(MACAddress.valueOf(54321)), |
| 60 | new OutputAction(PortNumber.uint16((short) 101))); |
| 61 | |
| 62 | PacketPathFlow flow1 = new PacketPathFlow(new FlowId(1L), match1, |
| 63 | PortNumber.uint32(12345), path, actions1, 0, 0); |
| 64 | |
| 65 | PacketPathFlow flow2 = new PacketPathFlow(new FlowId(2L), match2, |
| 66 | PortNumber.uint32(54321), path, actions2, 0, 0); |
| 67 | |
| 68 | flowOp = new FlowBatchOperation(); |
| 69 | flowOp.addAddFlowOperation(flow1); |
| 70 | flowOp.addAddFlowOperation(flow2); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests if the constructor initializes its field correctly. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testConstructor() { |
| 78 | SharedFlowBatchMap map = new SharedFlowBatchMap(scs); |
| 79 | Set<FlowBatchOperation> flowOps = map.getAll(); |
| 80 | assertNotNull(flowOps); |
| 81 | assertTrue(flowOps.isEmpty()); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests the basic functionality of the flow batch map. This test puts gets |
| 86 | * and removes a batch operation and checks these operations are executed |
| 87 | * properly. |
| 88 | */ |
| 89 | @Test |
| 90 | public void testAddGetRemoveFlowOp() { |
| 91 | SharedFlowBatchMap map = new SharedFlowBatchMap(scs); |
| 92 | final FlowBatchId id = new FlowBatchId(100L); |
| 93 | |
| 94 | // Check if the stored flow batch operation can be retrieved its ID |
| 95 | assertTrue(map.put(id, flowOp)); |
| 96 | final FlowBatchOperation obtainedFlowOp = map.get(id); |
| 97 | assertNotNull(obtainedFlowOp); |
| 98 | assertEquals(2, obtainedFlowOp.size()); |
| 99 | |
| 100 | // Check if it will not return flow with nonexistent ID |
| 101 | FlowBatchId nonexistentId = new FlowBatchId(99999L); |
| 102 | assertFalse(nonexistentId.equals(id)); |
| 103 | assertNull(map.get(nonexistentId)); |
| 104 | |
| 105 | // Check if it will remove the operation and it will not return the |
| 106 | // operation after the removal |
| 107 | final FlowBatchOperation removedFlowOp = map.remove(id); |
| 108 | assertNotNull(removedFlowOp); |
| 109 | assertEquals(2, removedFlowOp.size()); |
| 110 | final FlowBatchOperation nullFlowOp = map.get(id); |
| 111 | assertNull(nullFlowOp); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testChangeStateOfFlowOp() { |
| 116 | SharedFlowBatchMap map = new SharedFlowBatchMap(scs); |
| 117 | |
| 118 | final FlowBatchId id = new FlowBatchId(100L); |
| 119 | |
| 120 | // Check if the state of nonexistent flow is not exist |
| 121 | assertNull(map.getState(id)); |
| 122 | |
| 123 | // Check if it won't change the state of nonexistent flow |
| 124 | assertFalse(map.setState(id, FlowBatchState.EXECUTING, FlowBatchState.SUBMITTED)); |
| 125 | assertNull(map.getState(id)); |
| 126 | |
| 127 | // Check if the initial state is SUBMITTED |
| 128 | assertTrue(map.put(id, flowOp)); |
| 129 | assertEquals(FlowBatchState.SUBMITTED, map.getState(id)); |
| 130 | |
| 131 | // Check if it won't change the state if the expected state was wrong |
| 132 | assertFalse(map.setState(id, FlowBatchState.COMPLETED, FlowBatchState.EXECUTING)); |
| 133 | assertEquals(FlowBatchState.SUBMITTED, map.getState(id)); |
| 134 | |
| 135 | // Check if it changes the state if the expected state was correct |
| 136 | assertTrue(map.setState(id, FlowBatchState.EXECUTING, FlowBatchState.SUBMITTED)); |
| 137 | assertEquals(FlowBatchState.EXECUTING, map.getState(id)); |
| 138 | |
| 139 | // Check if it won't return the state if the flow was removed |
| 140 | assertEquals(flowOp, map.remove(id)); |
| 141 | assertNull(map.getState(id)); |
| 142 | } |
| 143 | } |