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