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