blob: ceac0c28e53309f5352eadd85fb2886868ac7fd1 [file] [log] [blame]
sivachidambaram subramanian5168f612017-03-08 13:53:30 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sivachidambaram subramanian5168f612017-03-08 13:53:30 +05303 *
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
Jordan Haltermanf7554092017-07-30 15:05:51 -070018import java.util.Collections;
19import java.util.Iterator;
20
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053021import com.google.common.collect.ArrayListMultimap;
22import com.google.common.collect.Multimap;
23import org.junit.After;
24import org.junit.Before;
Ray Milkeyed9bace2018-03-20 10:05:56 -070025import org.junit.Ignore;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053026import org.junit.Test;
Jordan Haltermanf7554092017-07-30 15:05:51 -070027import org.onlab.packet.Ip4Address;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053028import org.onosproject.cluster.ClusterService;
29import org.onosproject.cluster.ControllerNode;
Jordan Haltermanf7554092017-07-30 15:05:51 -070030import org.onosproject.cluster.NodeId;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053031import org.onosproject.core.CoreServiceAdapter;
32import org.onosproject.mastership.MastershipServiceAdapter;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053033import org.onosproject.net.DeviceId;
34import org.onosproject.net.MastershipRole;
Jordan Haltermanf7554092017-07-30 15:05:51 -070035import org.onosproject.net.device.DeviceServiceAdapter;
36import org.onosproject.net.flow.DefaultFlowEntry;
37import org.onosproject.net.flow.DefaultFlowRule;
38import org.onosproject.net.flow.FlowEntry;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053039import org.onosproject.net.flow.FlowRule;
Ray Milkey7bf273c2017-09-27 16:15:15 -070040import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
41import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
Jordan Haltermanf7554092017-07-30 15:05:51 -070042import org.onosproject.net.flow.FlowRuleOperation;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053043import org.onosproject.net.intent.IntentTestsMocks;
44import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Jordan Haltermanf7554092017-07-30 15:05:51 -070045import org.onosproject.store.service.AsyncDocumentTree;
Jordan Haltermanf7554092017-07-30 15:05:51 -070046import org.onosproject.store.service.DocumentTreeBuilder;
47import org.onosproject.store.service.Serializer;
Jordan Halterman9b3a7ce2017-08-25 15:12:54 -070048import org.onosproject.store.service.TestAsyncDocumentTree;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053049import org.onosproject.store.service.TestStorageService;
Jordan Haltermanf7554092017-07-30 15:05:51 -070050import org.onosproject.store.service.TestTopic;
51import org.onosproject.store.service.Topic;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053052
53import static org.easymock.EasyMock.createMock;
54import static org.easymock.EasyMock.expect;
55import static org.easymock.EasyMock.replay;
56import static org.hamcrest.MatcherAssert.assertThat;
57import static org.hamcrest.Matchers.emptyIterable;
58import static org.hamcrest.Matchers.is;
59import static org.hamcrest.Matchers.notNullValue;
60import static org.junit.Assert.assertEquals;
61import static org.onosproject.net.NetTestTools.APP_ID;
62import static org.onosproject.net.NetTestTools.did;
63
64/**
65 * Test class for DistributedFlowRuleStore.
Ray Milkeyed9bace2018-03-20 10:05:56 -070066 *
67 * @deprecated in Nightingale Release (1.13)
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053068 */
Ray Milkeyed9bace2018-03-20 10:05:56 -070069@Deprecated
70@Ignore("The distributed flow rule store has been deprecated")
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053071public class DistributedFlowRuleStoreTest {
72
73 DistributedFlowRuleStore flowStoreImpl;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053074 private ClusterService mockClusterService;
75 private ControllerNode mockControllerNode;
76
77 private NodeId nodeId;
78
79 private static final IntentTestsMocks.MockSelector SELECTOR =
80 new IntentTestsMocks.MockSelector();
81 private static final IntentTestsMocks.MockTreatment TREATMENT =
82 new IntentTestsMocks.MockTreatment();
83 DeviceId deviceId = did("device1");
84 FlowRule flowRule =
85 DefaultFlowRule.builder()
86 .forDevice(deviceId)
87 .withSelector(SELECTOR)
88 .withTreatment(TREATMENT)
89 .withPriority(22)
90 .makeTemporary(44)
91 .fromApp(APP_ID)
92 .build();
93 FlowRule flowRule1 =
94 DefaultFlowRule.builder()
95 .forDevice(deviceId)
96 .withSelector(SELECTOR)
97 .withTreatment(TREATMENT)
98 .withPriority(33)
99 .makeTemporary(44)
100 .fromApp(APP_ID)
101 .build();
102
103 static class MasterOfAll extends MastershipServiceAdapter {
104 @Override
105 public MastershipRole getLocalRole(DeviceId deviceId) {
106 return MastershipRole.MASTER;
107 }
108
109 @Override
110 public NodeId getMasterFor(DeviceId deviceId) {
111 return new NodeId("1");
112 }
113 }
114
115
116 private static class MockControllerNode implements ControllerNode {
117 final NodeId id;
118
119 public MockControllerNode(NodeId id) {
120 this.id = id;
121 }
122
123 @Override
124 public NodeId id() {
125 return this.id;
126 }
127
128 @Override
129 public Ip4Address ip() {
130 return Ip4Address.valueOf("127.0.0.1");
131 }
132
133 @Override
134 public int tcpPort() {
135 return 0;
136 }
137 }
138
Jordan Haltermanf7554092017-07-30 15:05:51 -0700139 private static class MockStorageService extends TestStorageService {
140 @Override
141 public <V> DocumentTreeBuilder<V> documentTreeBuilder() {
142 return new DocumentTreeBuilder<V>() {
143 @Override
144 public AsyncDocumentTree<V> buildDocumentTree() {
145 return build();
146 }
147
148 @Override
149 @SuppressWarnings("unchecked")
150 public AsyncDocumentTree<V> build() {
Jordan Halterman9b3a7ce2017-08-25 15:12:54 -0700151 return new TestAsyncDocumentTree<>(name());
Jordan Haltermanf7554092017-07-30 15:05:51 -0700152 }
153 };
154 }
155
156 @Override
157 public <T> Topic<T> getTopic(String name, Serializer serializer) {
158 return new TestTopic<>(name);
159 }
160 }
161
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530162 @Before
163 public void setUp() throws Exception {
164 flowStoreImpl = new DistributedFlowRuleStore();
Jordan Haltermanf7554092017-07-30 15:05:51 -0700165 flowStoreImpl.storageService = new MockStorageService();
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530166 mockClusterService = createMock(ClusterService.class);
167 flowStoreImpl.clusterService = mockClusterService;
168 nodeId = new NodeId("1");
169 mockControllerNode = new MockControllerNode(nodeId);
170
171 expect(mockClusterService.getLocalNode())
172 .andReturn(mockControllerNode).anyTimes();
173 replay(mockClusterService);
174
175 flowStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
176 flowStoreImpl.mastershipService = new MasterOfAll();
177 flowStoreImpl.deviceService = new DeviceServiceAdapter();
178 flowStoreImpl.coreService = new CoreServiceAdapter();
Jordan Haltermanf7554092017-07-30 15:05:51 -0700179 flowStoreImpl.activate();
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530180 }
181
182 @After
183 public void tearDown() throws Exception {
Jordan Haltermanf7554092017-07-30 15:05:51 -0700184 flowStoreImpl.deactivate();
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530185 }
186
187 /**
188 * Tests the initial state of the store.
189 */
190 @Test
191 public void testEmptyStore() {
192 assertThat(flowStoreImpl.getFlowRuleCount(), is(0));
193 assertThat(flowStoreImpl.getFlowEntries(deviceId), is(emptyIterable()));
194 }
195
196 /**
197 * Tests initial state of flowrule.
198 */
199 @Test
200 public void testStoreBatch() {
201 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
202 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
203 perDeviceBatches.put(op.rule().deviceId(),
204 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
205 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
206 deviceId, 1);
207 flowStoreImpl.storeBatch(b);
208 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
209 assertEquals("PENDING_ADD", flowEntry1.state().toString());
210 }
211
212 /**
213 * Tests adding a flowrule.
214 */
215 @Test
216 public void testAddFlow() {
217 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
218 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
219 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
220 perDeviceBatches.put(op.rule().deviceId(),
221 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
222 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
223 deviceId, 1);
224 flowStoreImpl.storeBatch(b);
225 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
226 assertEquals("PENDING_ADD", flowEntry1.state().toString());
227
228 flowStoreImpl.addOrUpdateFlowRule(flowEntry);
229 Iterable<FlowEntry> flows = flowStoreImpl.getFlowEntries(deviceId);
230 int sum = 0;
231 Iterator it = flows.iterator();
232 while (it.hasNext()) {
233 it.next();
234 sum++;
235 }
236 assertThat(sum, is(1));
237
238 FlowEntry flowEntry2 = flowStoreImpl.getFlowEntry(flowRule);
239 assertEquals("ADDED", flowEntry2.state().toString());
240 assertThat(flowStoreImpl.getTableStatistics(deviceId), notNullValue());
241 }
242
243 /**
244 * Tests flow removal.
245 */
246 @Test
247 public void testRemoveFlow() {
248 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
249 for (FlowEntry flow : flows1) {
250 flowStoreImpl.removeFlowRule(flow);
251 }
252
253 Iterable<FlowEntry> flows2 = flowStoreImpl.getFlowEntries(deviceId);
254 int sum = 0;
255 Iterator it = flows2.iterator();
256 while (it.hasNext()) {
257 it.next();
258 sum++;
259 }
260 assertThat(sum, is(0));
261 }
262
263 /**
264 * Tests purge flow for a device.
265 */
266 @Test
267 public void testPurgeFlow() {
268 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
Jordan Haltermanf7554092017-07-30 15:05:51 -0700269 flowStoreImpl.storeBatch(new FlowRuleBatchOperation(
270 Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry)),
271 flowEntry.deviceId(), 1));
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530272
273 FlowEntry flowEntry1 = new DefaultFlowEntry(flowRule1);
Jordan Haltermanf7554092017-07-30 15:05:51 -0700274 flowStoreImpl.storeBatch(new FlowRuleBatchOperation(
275 Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry1)),
276 flowEntry1.deviceId(), 2));
277
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530278 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
279 int sum2 = 0;
280 Iterator it1 = flows1.iterator();
281 while (it1.hasNext()) {
282 it1.next();
283 sum2++;
284 }
285 assertThat(sum2, is(2));
286 flowStoreImpl.purgeFlowRule(deviceId);
287
288 Iterable<FlowEntry> flows3 = flowStoreImpl.getFlowEntries(deviceId);
289 int sum3 = 0;
290 Iterator it3 = flows3.iterator();
291 while (it3.hasNext()) {
292 it3.next();
293 sum3++;
294 }
295 assertThat(sum3, is(0));
296 }
297}