blob: 0dfc2a2b0ff90d9e1d04b6e935a561d9aaa06b9b [file] [log] [blame]
Jon Hallfa132292017-10-24 11:11:24 -07001/*
2 * Copyright 2016-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.store.flow.impl;
17
18import com.google.common.collect.ArrayListMultimap;
Jordan Halterman281dbf32018-06-15 17:46:28 -070019import com.google.common.collect.ImmutableMap;
Jon Hallfa132292017-10-24 11:11:24 -070020import com.google.common.collect.Multimap;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24
Jordan Haltermane458f002018-09-18 17:42:05 -070025import org.onlab.packet.IpAddress;
Jon Hallfa132292017-10-24 11:11:24 -070026import org.onosproject.cfg.ComponentConfigAdapter;
27import org.onosproject.cluster.NodeId;
28import org.onosproject.cluster.ClusterService;
29import org.onosproject.cluster.ControllerNode;
30import org.onosproject.core.CoreServiceAdapter;
Jordan Halterman281dbf32018-06-15 17:46:28 -070031import org.onosproject.mastership.MastershipInfo;
Jon Hallfa132292017-10-24 11:11:24 -070032import org.onosproject.mastership.MastershipServiceAdapter;
33import org.onosproject.net.device.DeviceServiceAdapter;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.MastershipRole;
36import org.onosproject.net.flow.FlowRuleOperation;
37import org.onosproject.net.flow.FlowRule;
38import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
39import org.onosproject.net.flow.DefaultFlowEntry;
40import org.onosproject.net.flow.FlowEntry;
41import org.onosproject.net.flow.DefaultFlowRule;
42import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
43import org.onosproject.net.intent.IntentTestsMocks;
44import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
45import org.onosproject.store.persistence.PersistenceServiceAdapter;
Jordan Halterman281dbf32018-06-15 17:46:28 -070046import org.onosproject.store.service.AsyncConsistentMap;
47import org.onosproject.store.service.AsyncConsistentMapAdapter;
48import org.onosproject.store.service.ConsistentMap;
49import org.onosproject.store.service.ConsistentMapBuilder;
Jon Hallfa132292017-10-24 11:11:24 -070050import org.onosproject.store.service.TestStorageService;
51
52import org.onlab.packet.Ip4Address;
53import java.util.Iterator;
Jordan Halterman281dbf32018-06-15 17:46:28 -070054import java.util.Optional;
55
Jon Hallfa132292017-10-24 11:11:24 -070056import org.osgi.service.component.ComponentContext;
57
58import static org.easymock.EasyMock.createMock;
59import static org.easymock.EasyMock.expect;
60import static org.easymock.EasyMock.replay;
61import static org.hamcrest.MatcherAssert.assertThat;
62import static org.hamcrest.Matchers.emptyIterable;
63import static org.hamcrest.Matchers.is;
64import static org.hamcrest.Matchers.notNullValue;
65import static org.junit.Assert.assertEquals;
66import static org.onosproject.net.NetTestTools.APP_ID;
67import static org.onosproject.net.NetTestTools.did;
68
69/**
70 * Test class for ECFlowRuleStore.
71 */
72public class ECFlowRuleStoreTest {
73
74 ECFlowRuleStore flowStoreImpl;
75 ComponentContext context = null;
76 private ClusterService mockClusterService;
77 private ControllerNode mockControllerNode;
78
79 private NodeId nodeId;
80
81 private static final IntentTestsMocks.MockSelector SELECTOR =
82 new IntentTestsMocks.MockSelector();
83 private static final IntentTestsMocks.MockTreatment TREATMENT =
84 new IntentTestsMocks.MockTreatment();
85 DeviceId deviceId = did("device1");
86 FlowRule flowRule =
87 DefaultFlowRule.builder()
88 .forDevice(deviceId)
89 .withSelector(SELECTOR)
90 .withTreatment(TREATMENT)
91 .withPriority(22)
92 .makeTemporary(44)
93 .fromApp(APP_ID)
94 .build();
95 FlowRule flowRule1 =
96 DefaultFlowRule.builder()
97 .forDevice(deviceId)
98 .withSelector(SELECTOR)
99 .withTreatment(TREATMENT)
100 .withPriority(33)
101 .makeTemporary(44)
102 .fromApp(APP_ID)
103 .build();
104
105 static class MasterOfAll extends MastershipServiceAdapter {
106 @Override
107 public MastershipRole getLocalRole(DeviceId deviceId) {
108 return MastershipRole.MASTER;
109 }
110
111 @Override
112 public NodeId getMasterFor(DeviceId deviceId) {
113 return new NodeId("1");
114 }
Jordan Halterman281dbf32018-06-15 17:46:28 -0700115
116 @Override
117 public MastershipInfo getMastershipFor(DeviceId deviceId) {
118 return new MastershipInfo(
119 1,
120 Optional.of(NodeId.nodeId("1")),
121 ImmutableMap.<NodeId, MastershipRole>builder()
122 .put(NodeId.nodeId("1"), MastershipRole.MASTER)
123 .build());
124 }
Jon Hallfa132292017-10-24 11:11:24 -0700125 }
126
127
128 private static class MockControllerNode implements ControllerNode {
129 final NodeId id;
130
131 public MockControllerNode(NodeId id) {
132 this.id = id;
133 }
134
135 @Override
136 public NodeId id() {
137 return this.id;
138 }
139
140 @Override
Jordan Haltermane458f002018-09-18 17:42:05 -0700141 public String host() {
142 return "127.0.0.1";
143 }
144
145 @Override
Jon Hallfa132292017-10-24 11:11:24 -0700146 public Ip4Address ip() {
147 return Ip4Address.valueOf("127.0.0.1");
148 }
149
150 @Override
Jordan Haltermane458f002018-09-18 17:42:05 -0700151 public IpAddress ip(boolean resolve) {
152 return ip();
153 }
154
155 @Override
Jon Hallfa132292017-10-24 11:11:24 -0700156 public int tcpPort() {
157 return 0;
158 }
159 }
160
161 @Before
162 public void setUp() throws Exception {
163 flowStoreImpl = new ECFlowRuleStore();
Jordan Halterman281dbf32018-06-15 17:46:28 -0700164 flowStoreImpl.storageService = new TestStorageService() {
165 @Override
166 public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
167 return new ConsistentMapBuilder<K, V>() {
168 @Override
169 public AsyncConsistentMap<K, V> buildAsyncMap() {
170 return new AsyncConsistentMapAdapter<K, V>();
171 }
172
173 @Override
174 public ConsistentMap<K, V> build() {
175 return null;
176 }
177 };
178 }
179 };
180
181 ReplicaInfoManager replicaInfoManager = new ReplicaInfoManager();
182 replicaInfoManager.mastershipService = new MasterOfAll();
183
184 flowStoreImpl.replicaInfoManager = replicaInfoManager;
Jon Hallfa132292017-10-24 11:11:24 -0700185 mockClusterService = createMock(ClusterService.class);
186 flowStoreImpl.clusterService = mockClusterService;
187 nodeId = new NodeId("1");
188 mockControllerNode = new MockControllerNode(nodeId);
189
190 expect(mockClusterService.getLocalNode())
191 .andReturn(mockControllerNode).anyTimes();
192 replay(mockClusterService);
193
194 flowStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
195 flowStoreImpl.mastershipService = new MasterOfAll();
196 flowStoreImpl.deviceService = new DeviceServiceAdapter();
197 flowStoreImpl.coreService = new CoreServiceAdapter();
198 flowStoreImpl.configService = new ComponentConfigAdapter();
199 flowStoreImpl.persistenceService = new PersistenceServiceAdapter();
200 flowStoreImpl.activate(context);
201 }
202
203 @After
204 public void tearDown() throws Exception {
205 flowStoreImpl.deactivate(context);
206 }
207
208 /**
209 * Tests the initial state of the store.
210 */
211 @Test
212 public void testEmptyStore() {
213 assertThat(flowStoreImpl.getFlowRuleCount(), is(0));
214 assertThat(flowStoreImpl.getFlowEntries(deviceId), is(emptyIterable()));
215 }
216
217 /**
218 * Tests initial state of flowrule.
219 */
220 @Test
221 public void testStoreBatch() {
222 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
223 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
224 perDeviceBatches.put(op.rule().deviceId(),
225 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
226 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
227 deviceId, 1);
228 flowStoreImpl.storeBatch(b);
229 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
230 assertEquals("PENDING_ADD", flowEntry1.state().toString());
231 }
232
233 /**
234 * Tests adding a flowrule.
235 */
236 @Test
237 public void testAddFlow() {
238 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
239 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
240 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
241 perDeviceBatches.put(op.rule().deviceId(),
242 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
243 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
244 deviceId, 1);
245 flowStoreImpl.storeBatch(b);
246 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
247 assertEquals("PENDING_ADD", flowEntry1.state().toString());
248
249 flowStoreImpl.addOrUpdateFlowRule(flowEntry);
250 Iterable<FlowEntry> flows = flowStoreImpl.getFlowEntries(deviceId);
251 int sum = 0;
252 Iterator it = flows.iterator();
253 while (it.hasNext()) {
254 it.next();
255 sum++;
256 }
257 assertThat(sum, is(1));
258
259 FlowEntry flowEntry2 = flowStoreImpl.getFlowEntry(flowRule);
260 assertEquals("ADDED", flowEntry2.state().toString());
261 assertThat(flowStoreImpl.getTableStatistics(deviceId), notNullValue());
262 }
263
264 /**
265 * Tests flow removal.
266 */
267 @Test
268 public void testRemoveFlow() {
269 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
270 for (FlowEntry flow : flows1) {
271 flowStoreImpl.removeFlowRule(flow);
272 }
273
274 Iterable<FlowEntry> flows2 = flowStoreImpl.getFlowEntries(deviceId);
275 int sum = 0;
276 Iterator it = flows2.iterator();
277 while (it.hasNext()) {
278 it.next();
279 sum++;
280 }
281 assertThat(sum, is(0));
282 }
283
284 /**
285 * Tests purge flow for a device.
286 */
287 @Test
288 public void testPurgeFlow() {
289 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
290 flowStoreImpl.addOrUpdateFlowRule(flowEntry);
291
292 FlowEntry flowEntry1 = new DefaultFlowEntry(flowRule1);
293 flowStoreImpl.addOrUpdateFlowRule(flowEntry1);
294 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
295 int sum2 = 0;
296 Iterator it1 = flows1.iterator();
297 while (it1.hasNext()) {
298 it1.next();
299 sum2++;
300 }
301 assertThat(sum2, is(2));
302 flowStoreImpl.purgeFlowRule(deviceId);
303
304 Iterable<FlowEntry> flows3 = flowStoreImpl.getFlowEntries(deviceId);
305 int sum3 = 0;
306 Iterator it3 = flows3.iterator();
307 while (it3.hasNext()) {
308 it3.next();
309 sum3++;
310 }
311 assertThat(sum3, is(0));
312 }
313}