blob: 7fd04128ac2af6d8aaa72b2c878bda90d494d352 [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;
19import com.google.common.collect.Multimap;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23
24import org.onosproject.cfg.ComponentConfigAdapter;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.cluster.ClusterService;
27import org.onosproject.cluster.ControllerNode;
28import org.onosproject.core.CoreServiceAdapter;
29import org.onosproject.mastership.MastershipServiceAdapter;
30import org.onosproject.net.device.DeviceServiceAdapter;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.MastershipRole;
33import org.onosproject.net.flow.FlowRuleOperation;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
36import org.onosproject.net.flow.DefaultFlowEntry;
37import org.onosproject.net.flow.FlowEntry;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
40import org.onosproject.net.intent.IntentTestsMocks;
41import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
42import org.onosproject.store.persistence.PersistenceServiceAdapter;
43import org.onosproject.store.service.TestStorageService;
44
45import org.onlab.packet.Ip4Address;
46import java.util.Iterator;
47import org.osgi.service.component.ComponentContext;
48
49import static org.easymock.EasyMock.createMock;
50import static org.easymock.EasyMock.expect;
51import static org.easymock.EasyMock.replay;
52import static org.hamcrest.MatcherAssert.assertThat;
53import static org.hamcrest.Matchers.emptyIterable;
54import static org.hamcrest.Matchers.is;
55import static org.hamcrest.Matchers.notNullValue;
56import static org.junit.Assert.assertEquals;
57import static org.onosproject.net.NetTestTools.APP_ID;
58import static org.onosproject.net.NetTestTools.did;
59
60/**
61 * Test class for ECFlowRuleStore.
62 */
63public class ECFlowRuleStoreTest {
64
65 ECFlowRuleStore flowStoreImpl;
66 ComponentContext context = null;
67 private ClusterService mockClusterService;
68 private ControllerNode mockControllerNode;
69
70 private NodeId nodeId;
71
72 private static final IntentTestsMocks.MockSelector SELECTOR =
73 new IntentTestsMocks.MockSelector();
74 private static final IntentTestsMocks.MockTreatment TREATMENT =
75 new IntentTestsMocks.MockTreatment();
76 DeviceId deviceId = did("device1");
77 FlowRule flowRule =
78 DefaultFlowRule.builder()
79 .forDevice(deviceId)
80 .withSelector(SELECTOR)
81 .withTreatment(TREATMENT)
82 .withPriority(22)
83 .makeTemporary(44)
84 .fromApp(APP_ID)
85 .build();
86 FlowRule flowRule1 =
87 DefaultFlowRule.builder()
88 .forDevice(deviceId)
89 .withSelector(SELECTOR)
90 .withTreatment(TREATMENT)
91 .withPriority(33)
92 .makeTemporary(44)
93 .fromApp(APP_ID)
94 .build();
95
96 static class MasterOfAll extends MastershipServiceAdapter {
97 @Override
98 public MastershipRole getLocalRole(DeviceId deviceId) {
99 return MastershipRole.MASTER;
100 }
101
102 @Override
103 public NodeId getMasterFor(DeviceId deviceId) {
104 return new NodeId("1");
105 }
106 }
107
108
109 private static class MockControllerNode implements ControllerNode {
110 final NodeId id;
111
112 public MockControllerNode(NodeId id) {
113 this.id = id;
114 }
115
116 @Override
117 public NodeId id() {
118 return this.id;
119 }
120
121 @Override
122 public Ip4Address ip() {
123 return Ip4Address.valueOf("127.0.0.1");
124 }
125
126 @Override
127 public int tcpPort() {
128 return 0;
129 }
130 }
131
132 @Before
133 public void setUp() throws Exception {
134 flowStoreImpl = new ECFlowRuleStore();
135 flowStoreImpl.storageService = new TestStorageService();
136 flowStoreImpl.replicaInfoManager = new ReplicaInfoManager();
137 mockClusterService = createMock(ClusterService.class);
138 flowStoreImpl.clusterService = mockClusterService;
139 nodeId = new NodeId("1");
140 mockControllerNode = new MockControllerNode(nodeId);
141
142 expect(mockClusterService.getLocalNode())
143 .andReturn(mockControllerNode).anyTimes();
144 replay(mockClusterService);
145
146 flowStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
147 flowStoreImpl.mastershipService = new MasterOfAll();
148 flowStoreImpl.deviceService = new DeviceServiceAdapter();
149 flowStoreImpl.coreService = new CoreServiceAdapter();
150 flowStoreImpl.configService = new ComponentConfigAdapter();
151 flowStoreImpl.persistenceService = new PersistenceServiceAdapter();
152 flowStoreImpl.activate(context);
153 }
154
155 @After
156 public void tearDown() throws Exception {
157 flowStoreImpl.deactivate(context);
158 }
159
160 /**
161 * Tests the initial state of the store.
162 */
163 @Test
164 public void testEmptyStore() {
165 assertThat(flowStoreImpl.getFlowRuleCount(), is(0));
166 assertThat(flowStoreImpl.getFlowEntries(deviceId), is(emptyIterable()));
167 }
168
169 /**
170 * Tests initial state of flowrule.
171 */
172 @Test
173 public void testStoreBatch() {
174 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
175 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
176 perDeviceBatches.put(op.rule().deviceId(),
177 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
178 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
179 deviceId, 1);
180 flowStoreImpl.storeBatch(b);
181 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
182 assertEquals("PENDING_ADD", flowEntry1.state().toString());
183 }
184
185 /**
186 * Tests adding a flowrule.
187 */
188 @Test
189 public void testAddFlow() {
190 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
191 FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD);
192 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
193 perDeviceBatches.put(op.rule().deviceId(),
194 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule()));
195 FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
196 deviceId, 1);
197 flowStoreImpl.storeBatch(b);
198 FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule);
199 assertEquals("PENDING_ADD", flowEntry1.state().toString());
200
201 flowStoreImpl.addOrUpdateFlowRule(flowEntry);
202 Iterable<FlowEntry> flows = flowStoreImpl.getFlowEntries(deviceId);
203 int sum = 0;
204 Iterator it = flows.iterator();
205 while (it.hasNext()) {
206 it.next();
207 sum++;
208 }
209 assertThat(sum, is(1));
210
211 FlowEntry flowEntry2 = flowStoreImpl.getFlowEntry(flowRule);
212 assertEquals("ADDED", flowEntry2.state().toString());
213 assertThat(flowStoreImpl.getTableStatistics(deviceId), notNullValue());
214 }
215
216 /**
217 * Tests flow removal.
218 */
219 @Test
220 public void testRemoveFlow() {
221 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
222 for (FlowEntry flow : flows1) {
223 flowStoreImpl.removeFlowRule(flow);
224 }
225
226 Iterable<FlowEntry> flows2 = flowStoreImpl.getFlowEntries(deviceId);
227 int sum = 0;
228 Iterator it = flows2.iterator();
229 while (it.hasNext()) {
230 it.next();
231 sum++;
232 }
233 assertThat(sum, is(0));
234 }
235
236 /**
237 * Tests purge flow for a device.
238 */
239 @Test
240 public void testPurgeFlow() {
241 FlowEntry flowEntry = new DefaultFlowEntry(flowRule);
242 flowStoreImpl.addOrUpdateFlowRule(flowEntry);
243
244 FlowEntry flowEntry1 = new DefaultFlowEntry(flowRule1);
245 flowStoreImpl.addOrUpdateFlowRule(flowEntry1);
246 Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId);
247 int sum2 = 0;
248 Iterator it1 = flows1.iterator();
249 while (it1.hasNext()) {
250 it1.next();
251 sum2++;
252 }
253 assertThat(sum2, is(2));
254 flowStoreImpl.purgeFlowRule(deviceId);
255
256 Iterable<FlowEntry> flows3 = flowStoreImpl.getFlowEntries(deviceId);
257 int sum3 = 0;
258 Iterator it3 = flows3.iterator();
259 while (it3.hasNext()) {
260 it3.next();
261 sum3++;
262 }
263 assertThat(sum3, is(0));
264 }
265}