blob: 9b9c4dba80727f7e1f2b57800afc3513a33216fb [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;
Jordan Haltermane458f002018-09-18 17:42:05 -070028import org.onlab.packet.IpAddress;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053029import org.onosproject.cluster.ClusterService;
30import org.onosproject.cluster.ControllerNode;
Jordan Haltermanf7554092017-07-30 15:05:51 -070031import org.onosproject.cluster.NodeId;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053032import org.onosproject.core.CoreServiceAdapter;
33import org.onosproject.mastership.MastershipServiceAdapter;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053034import org.onosproject.net.DeviceId;
35import org.onosproject.net.MastershipRole;
Jordan Haltermanf7554092017-07-30 15:05:51 -070036import org.onosproject.net.device.DeviceServiceAdapter;
37import org.onosproject.net.flow.DefaultFlowEntry;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.FlowEntry;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053040import org.onosproject.net.flow.FlowRule;
Ray Milkey7bf273c2017-09-27 16:15:15 -070041import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
42import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
Jordan Haltermanf7554092017-07-30 15:05:51 -070043import org.onosproject.net.flow.FlowRuleOperation;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053044import org.onosproject.net.intent.IntentTestsMocks;
45import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Jordan Haltermanf7554092017-07-30 15:05:51 -070046import org.onosproject.store.service.AsyncDocumentTree;
Jordan Haltermanf7554092017-07-30 15:05:51 -070047import org.onosproject.store.service.DocumentTreeBuilder;
48import org.onosproject.store.service.Serializer;
Jordan Halterman9b3a7ce2017-08-25 15:12:54 -070049import org.onosproject.store.service.TestAsyncDocumentTree;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053050import org.onosproject.store.service.TestStorageService;
Jordan Haltermanf7554092017-07-30 15:05:51 -070051import org.onosproject.store.service.TestTopic;
52import org.onosproject.store.service.Topic;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053053
54import static org.easymock.EasyMock.createMock;
55import static org.easymock.EasyMock.expect;
56import static org.easymock.EasyMock.replay;
57import static org.hamcrest.MatcherAssert.assertThat;
58import static org.hamcrest.Matchers.emptyIterable;
59import static org.hamcrest.Matchers.is;
60import static org.hamcrest.Matchers.notNullValue;
61import static org.junit.Assert.assertEquals;
62import static org.onosproject.net.NetTestTools.APP_ID;
63import static org.onosproject.net.NetTestTools.did;
64
65/**
66 * Test class for DistributedFlowRuleStore.
Ray Milkeyed9bace2018-03-20 10:05:56 -070067 *
68 * @deprecated in Nightingale Release (1.13)
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053069 */
Ray Milkeyed9bace2018-03-20 10:05:56 -070070@Deprecated
71@Ignore("The distributed flow rule store has been deprecated")
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053072public class DistributedFlowRuleStoreTest {
73
74 DistributedFlowRuleStore flowStoreImpl;
sivachidambaram subramanian5168f612017-03-08 13:53:30 +053075 private ClusterService mockClusterService;
76 private ControllerNode mockControllerNode;
77
78 private NodeId nodeId;
79
80 private static final IntentTestsMocks.MockSelector SELECTOR =
81 new IntentTestsMocks.MockSelector();
82 private static final IntentTestsMocks.MockTreatment TREATMENT =
83 new IntentTestsMocks.MockTreatment();
84 DeviceId deviceId = did("device1");
85 FlowRule flowRule =
86 DefaultFlowRule.builder()
87 .forDevice(deviceId)
88 .withSelector(SELECTOR)
89 .withTreatment(TREATMENT)
90 .withPriority(22)
91 .makeTemporary(44)
92 .fromApp(APP_ID)
93 .build();
94 FlowRule flowRule1 =
95 DefaultFlowRule.builder()
96 .forDevice(deviceId)
97 .withSelector(SELECTOR)
98 .withTreatment(TREATMENT)
99 .withPriority(33)
100 .makeTemporary(44)
101 .fromApp(APP_ID)
102 .build();
103
104 static class MasterOfAll extends MastershipServiceAdapter {
105 @Override
106 public MastershipRole getLocalRole(DeviceId deviceId) {
107 return MastershipRole.MASTER;
108 }
109
110 @Override
111 public NodeId getMasterFor(DeviceId deviceId) {
112 return new NodeId("1");
113 }
114 }
115
116
117 private static class MockControllerNode implements ControllerNode {
118 final NodeId id;
119
120 public MockControllerNode(NodeId id) {
121 this.id = id;
122 }
123
124 @Override
125 public NodeId id() {
126 return this.id;
127 }
128
129 @Override
Jordan Haltermane458f002018-09-18 17:42:05 -0700130 public String host() {
131 return "127.0.0.1";
132 }
133
134 @Override
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530135 public Ip4Address ip() {
136 return Ip4Address.valueOf("127.0.0.1");
137 }
138
139 @Override
Jordan Haltermane458f002018-09-18 17:42:05 -0700140 public IpAddress ip(boolean resolve) {
141 return ip();
142 }
143
144 @Override
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530145 public int tcpPort() {
146 return 0;
147 }
148 }
149
Jordan Haltermanf7554092017-07-30 15:05:51 -0700150 private static class MockStorageService extends TestStorageService {
151 @Override
152 public <V> DocumentTreeBuilder<V> documentTreeBuilder() {
153 return new DocumentTreeBuilder<V>() {
154 @Override
155 public AsyncDocumentTree<V> buildDocumentTree() {
156 return build();
157 }
158
159 @Override
160 @SuppressWarnings("unchecked")
161 public AsyncDocumentTree<V> build() {
Jordan Halterman9b3a7ce2017-08-25 15:12:54 -0700162 return new TestAsyncDocumentTree<>(name());
Jordan Haltermanf7554092017-07-30 15:05:51 -0700163 }
164 };
165 }
166
167 @Override
168 public <T> Topic<T> getTopic(String name, Serializer serializer) {
169 return new TestTopic<>(name);
170 }
171 }
172
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530173 @Before
174 public void setUp() throws Exception {
175 flowStoreImpl = new DistributedFlowRuleStore();
Jordan Haltermanf7554092017-07-30 15:05:51 -0700176 flowStoreImpl.storageService = new MockStorageService();
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530177 mockClusterService = createMock(ClusterService.class);
178 flowStoreImpl.clusterService = mockClusterService;
179 nodeId = new NodeId("1");
180 mockControllerNode = new MockControllerNode(nodeId);
181
182 expect(mockClusterService.getLocalNode())
183 .andReturn(mockControllerNode).anyTimes();
184 replay(mockClusterService);
185
186 flowStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
187 flowStoreImpl.mastershipService = new MasterOfAll();
188 flowStoreImpl.deviceService = new DeviceServiceAdapter();
189 flowStoreImpl.coreService = new CoreServiceAdapter();
Jordan Haltermanf7554092017-07-30 15:05:51 -0700190 flowStoreImpl.activate();
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530191 }
192
193 @After
194 public void tearDown() throws Exception {
Jordan Haltermanf7554092017-07-30 15:05:51 -0700195 flowStoreImpl.deactivate();
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530196 }
197
198 /**
199 * Tests the initial state of the store.
200 */
201 @Test
202 public void testEmptyStore() {
203 assertThat(flowStoreImpl.getFlowRuleCount(), is(0));
204 assertThat(flowStoreImpl.getFlowEntries(deviceId), is(emptyIterable()));
205 }
206
207 /**
208 * Tests initial state of flowrule.
209 */
210 @Test
211 public void testStoreBatch() {
212 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
213 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
214 perDeviceBatches.put(op.rule().deviceId(),
215 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
216 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
217 deviceId, 1);
218 flowStoreImpl.storeBatch(b);
219 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
220 assertEquals("PENDING_ADD", flowEntry1.state().toString());
221 }
222
223 /**
224 * Tests adding a flowrule.
225 */
226 @Test
227 public void testAddFlow() {
228 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
229 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
230 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
231 perDeviceBatches.put(op.rule().deviceId(),
232 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
233 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
234 deviceId, 1);
235 flowStoreImpl.storeBatch(b);
236 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
237 assertEquals("PENDING_ADD", flowEntry1.state().toString());
238
239 flowStoreImpl.addOrUpdateFlowRule(flowEntry);
240 Iterable<FlowEntry> flows = flowStoreImpl.getFlowEntries(deviceId);
241 int sum = 0;
242 Iterator it = flows.iterator();
243 while (it.hasNext()) {
244 it.next();
245 sum++;
246 }
247 assertThat(sum, is(1));
248
249 FlowEntry flowEntry2 = flowStoreImpl.getFlowEntry(flowRule);
250 assertEquals("ADDED", flowEntry2.state().toString());
251 assertThat(flowStoreImpl.getTableStatistics(deviceId), notNullValue());
252 }
253
254 /**
255 * Tests flow removal.
256 */
257 @Test
258 public void testRemoveFlow() {
259 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
260 for (FlowEntry flow : flows1) {
261 flowStoreImpl.removeFlowRule(flow);
262 }
263
264 Iterable<FlowEntry> flows2 = flowStoreImpl.getFlowEntries(deviceId);
265 int sum = 0;
266 Iterator it = flows2.iterator();
267 while (it.hasNext()) {
268 it.next();
269 sum++;
270 }
271 assertThat(sum, is(0));
272 }
273
274 /**
275 * Tests purge flow for a device.
276 */
277 @Test
278 public void testPurgeFlow() {
279 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
Jordan Haltermanf7554092017-07-30 15:05:51 -0700280 flowStoreImpl.storeBatch(new FlowRuleBatchOperation(
281 Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry)),
282 flowEntry.deviceId(), 1));
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530283
284 FlowEntry flowEntry1 = new DefaultFlowEntry(flowRule1);
Jordan Haltermanf7554092017-07-30 15:05:51 -0700285 flowStoreImpl.storeBatch(new FlowRuleBatchOperation(
286 Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry1)),
287 flowEntry1.deviceId(), 2));
288
sivachidambaram subramanian5168f612017-03-08 13:53:30 +0530289 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
290 int sum2 = 0;
291 Iterator it1 = flows1.iterator();
292 while (it1.hasNext()) {
293 it1.next();
294 sum2++;
295 }
296 assertThat(sum2, is(2));
297 flowStoreImpl.purgeFlowRule(deviceId);
298
299 Iterable<FlowEntry> flows3 = flowStoreImpl.getFlowEntries(deviceId);
300 int sum3 = 0;
301 Iterator it3 = flows3.iterator();
302 while (it3.hasNext()) {
303 it3.next();
304 sum3++;
305 }
306 assertThat(sum3, is(0));
307 }
308}