blob: cebf01cc92a2583e7737838bcc6ecb4f8a5d5922 [file] [log] [blame]
Ray Milkeyc127a5a2014-08-20 11:22:12 -07001package net.onrc.onos.core.matchaction;
2
Ray Milkey18b44ac2014-08-22 08:29:47 -07003import net.floodlightcontroller.core.module.FloodlightModuleContext;
4import net.onrc.onos.core.datagrid.IDatagridService;
5import net.onrc.onos.core.datagrid.IEventChannel;
6import net.onrc.onos.core.datagrid.IEventChannelListener;
7import org.junit.Before;
Ray Milkeyc127a5a2014-08-20 11:22:12 -07008
9import java.util.ArrayList;
10import java.util.Set;
11
Ray Milkey18b44ac2014-08-22 08:29:47 -070012import static org.easymock.EasyMock.anyObject;
13import static org.easymock.EasyMock.createMock;
14import static org.easymock.EasyMock.eq;
15import static org.easymock.EasyMock.expect;
16import static org.easymock.EasyMock.replay;
Ray Milkeyc127a5a2014-08-20 11:22:12 -070017import static org.hamcrest.MatcherAssert.assertThat;
18import static org.hamcrest.Matchers.containsInAnyOrder;
19import static org.hamcrest.Matchers.hasSize;
20import static org.hamcrest.Matchers.is;
21import static org.hamcrest.Matchers.notNullValue;
Ray Milkey18b44ac2014-08-22 08:29:47 -070022import static org.easymock.EasyMock.createNiceMock;
Ray Milkeyc127a5a2014-08-20 11:22:12 -070023
24/**
25 * Unit tests for the MatchActionModule.
26 */
27public class MatchActionModuleTest {
28
Ray Milkey18b44ac2014-08-22 08:29:47 -070029 private IDatagridService datagridService;
30 private FloodlightModuleContext modContext;
31
32 @Before
33 @SuppressWarnings("unchecked")
34 public void setUpMocks() {
35 final IEventChannel<String, MatchActionOperations> installSetChannel =
36 createMock(IEventChannel.class);
37 final IEventChannel<String, SwitchResultList> installSetReplyChannel =
38 createMock(IEventChannel.class);
39
40 datagridService = createNiceMock(IDatagridService.class);
41 modContext = createMock(FloodlightModuleContext.class);
42
43 expect(modContext.getServiceImpl(IDatagridService.class))
44 .andReturn(datagridService).once();
45
46 expect(datagridService.createChannel("onos.matchaction.installSetChannel",
47 String.class,
48 MatchActionOperations.class))
49 .andReturn(installSetChannel).once();
50
51 expect(datagridService.addListener(
52 eq("onos.matchaction.installSetChannel"),
53 anyObject(IEventChannelListener.class),
54 eq(String.class),
55 eq(MatchActionOperations.class)))
56 .andReturn(installSetChannel).once();
57
58 expect(datagridService.createChannel("onos.matchaction.installSetReplyChannel",
59 String.class,
60 SwitchResultList.class))
61 .andReturn(installSetReplyChannel).once();
62
63 expect(datagridService.addListener(
64 eq("onos.matchaction.installSetReplyChannel"),
65 anyObject(IEventChannelListener.class),
66 eq(String.class),
67 eq(SwitchResultList.class)))
68 .andReturn(installSetReplyChannel).once();
69
70 replay(datagridService);
71 }
72
Ray Milkeyc127a5a2014-08-20 11:22:12 -070073 /**
74 * Tests that MatchAction objects added by the executeOperations()
75 * method are properly returned by the getMatchActions() method.
76 */
Ray Milkey18b44ac2014-08-22 08:29:47 -070077 //@Test
Ray Milkeyc127a5a2014-08-20 11:22:12 -070078 public void testMatchActionModuleGlobalEntriesSet() {
79
80 final int iterations = 5;
Ray Milkey18b44ac2014-08-22 08:29:47 -070081 final MatchActionComponent matchActionComponent =
82 new MatchActionComponent(datagridService, null, null);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070083 final ArrayList<MatchAction> generatedMatchActions = new ArrayList<>();
84
85 // Add some test MatchAction objects. 25 will be added, in 5 blocks
86 // of 5.
87 for (int operationsIteration = 1;
88 operationsIteration <= iterations;
89 operationsIteration++) {
90 final MatchActionOperationsId id =
Ray Milkey9ed4b962014-08-20 15:43:40 -070091 new MatchActionOperationsId(1L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070092 assertThat(id, is(notNullValue()));
93 final MatchActionOperations operations =
Ray Milkey9ed4b962014-08-20 15:43:40 -070094 new MatchActionOperations(id);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070095 assertThat(operations, is(notNullValue()));
96
97 for (int entriesIteration = 1;
98 entriesIteration <= iterations;
99 entriesIteration++) {
100
Ray Milkey9ed4b962014-08-20 15:43:40 -0700101 final MatchActionId entryId =
102 new MatchActionId(
103 (operationsIteration * 10) +
104 entriesIteration);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700105 final MatchAction matchAction =
106 new MatchAction(entryId, null, null, null);
107 final MatchActionOperationEntry entry =
108 new MatchActionOperationEntry(MatchActionOperations.Operator.ADD,
109 matchAction);
110 operations.addOperation(entry);
111 generatedMatchActions.add(matchAction);
112 }
113
114 // Add the MatchActions generated by this iteration
Ray Milkey18b44ac2014-08-22 08:29:47 -0700115 final boolean result = matchActionComponent.executeOperations(operations);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700116 assertThat(result, is(true));
117 }
118
119 // Get the list of generated MatchAction objects and make sure its
120 // length is correct.
121 final int generatedCount = generatedMatchActions.size();
Ray Milkey18b44ac2014-08-22 08:29:47 -0700122 final Set<MatchAction> matchActions = matchActionComponent.getMatchActions();
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700123 assertThat(matchActions, hasSize(generatedCount));
124
125 // Make sure that all the created items are in the list
126 final MatchAction[] generatedArray =
127 generatedMatchActions.toArray(new MatchAction[generatedCount]);
128 assertThat(matchActions, containsInAnyOrder(generatedArray));
129
130 // Make sure that the returned list cannot be modified
131 Throwable errorThrown = null;
132 try {
Ray Milkey9ed4b962014-08-20 15:43:40 -0700133 matchActions.add(new MatchAction(new MatchActionId(1L), null, null, null));
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700134 } catch (UnsupportedOperationException e) {
135 errorThrown = e;
136 }
137 assertThat(errorThrown, is(notNullValue()));
138 }
139
140 /**
141 * Tests that adding a duplicate MatchAction via executeOperations()
142 * returns an error.
143 */
Ray Milkey18b44ac2014-08-22 08:29:47 -0700144 //@Test
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700145 public void testAddingDuplicateMatchAction() {
146
147 // Create two MatchAction objects using the same ID
148 final MatchAction matchAction =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700149 new MatchAction(new MatchActionId(111L), null, null, null);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700150 final MatchAction duplicateMatchAction =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700151 new MatchAction(new MatchActionId(111L), null, null, null);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700152
153 // create Operation Entries for the two MatchAction objects
154 final MatchActionOperationEntry entry =
155 new MatchActionOperationEntry(MatchActionOperations.Operator.ADD,
156 matchAction);
157 final MatchActionOperationEntry duplicateEntry =
158 new MatchActionOperationEntry(MatchActionOperations.Operator.ADD,
159 duplicateMatchAction);
160
161 // Create an Operations object to execute the first MatchAction
162 final MatchActionOperationsId id =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700163 new MatchActionOperationsId(11L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700164 assertThat(id, is(notNullValue()));
165 final MatchActionOperations operations =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700166 new MatchActionOperations(id);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700167 operations.addOperation(entry);
168
169 // Create a module to use to execute the Operations.
Ray Milkey18b44ac2014-08-22 08:29:47 -0700170 final MatchActionComponent matchActionComponent = new MatchActionComponent(null, null, null);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700171
172 // Execute the first set of Operations. This
173 // should succeed.
Ray Milkey18b44ac2014-08-22 08:29:47 -0700174 final boolean result = matchActionComponent.executeOperations(operations);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700175 assertThat(result, is(true));
176
177 // Now add the duplicate entry. This should fail.
178 final MatchActionOperationsId idForDuplicate =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700179 new MatchActionOperationsId(22L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700180 assertThat(idForDuplicate, is(notNullValue()));
181 final MatchActionOperations operationsForDuplicate =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700182 new MatchActionOperations(idForDuplicate);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700183 operationsForDuplicate.addOperation(duplicateEntry);
184
185 final boolean resultForDuplicate =
Ray Milkey18b44ac2014-08-22 08:29:47 -0700186 matchActionComponent.executeOperations(operationsForDuplicate);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700187 assertThat(resultForDuplicate, is(false));
188
189 // Now add the original entry again. This should fail.
Ray Milkey18b44ac2014-08-22 08:29:47 -0700190 final boolean resultForAddAgain = matchActionComponent.executeOperations(operations);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700191 assertThat(resultForAddAgain, is(false));
192 }
193}