sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.flow.impl; |
| 17 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
| 19 | import java.util.Iterator; |
| 20 | |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 21 | import com.google.common.collect.ArrayListMultimap; |
| 22 | import com.google.common.collect.Multimap; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
Ray Milkey | ed9bace | 2018-03-20 10:05:56 -0700 | [diff] [blame] | 25 | import org.junit.Ignore; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 26 | import org.junit.Test; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 28 | import org.onosproject.cluster.ClusterService; |
| 29 | import org.onosproject.cluster.ControllerNode; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.NodeId; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 31 | import org.onosproject.core.CoreServiceAdapter; |
| 32 | import org.onosproject.mastership.MastershipServiceAdapter; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.MastershipRole; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 35 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 36 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 37 | import org.onosproject.net.flow.DefaultFlowRule; |
| 38 | import org.onosproject.net.flow.FlowEntry; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 7bf273c | 2017-09-27 16:15:15 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 41 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flow.FlowRuleOperation; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 43 | import org.onosproject.net.intent.IntentTestsMocks; |
| 44 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 45 | import org.onosproject.store.service.AsyncDocumentTree; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 46 | import org.onosproject.store.service.DocumentTreeBuilder; |
| 47 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 48 | import org.onosproject.store.service.TestAsyncDocumentTree; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 49 | import org.onosproject.store.service.TestStorageService; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.TestTopic; |
| 51 | import org.onosproject.store.service.Topic; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 52 | |
| 53 | import static org.easymock.EasyMock.createMock; |
| 54 | import static org.easymock.EasyMock.expect; |
| 55 | import static org.easymock.EasyMock.replay; |
| 56 | import static org.hamcrest.MatcherAssert.assertThat; |
| 57 | import static org.hamcrest.Matchers.emptyIterable; |
| 58 | import static org.hamcrest.Matchers.is; |
| 59 | import static org.hamcrest.Matchers.notNullValue; |
| 60 | import static org.junit.Assert.assertEquals; |
| 61 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 62 | import static org.onosproject.net.NetTestTools.did; |
| 63 | |
| 64 | /** |
| 65 | * Test class for DistributedFlowRuleStore. |
Ray Milkey | ed9bace | 2018-03-20 10:05:56 -0700 | [diff] [blame] | 66 | * |
| 67 | * @deprecated in Nightingale Release (1.13) |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 68 | */ |
Ray Milkey | ed9bace | 2018-03-20 10:05:56 -0700 | [diff] [blame] | 69 | @Deprecated |
| 70 | @Ignore("The distributed flow rule store has been deprecated") |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 71 | public class DistributedFlowRuleStoreTest { |
| 72 | |
| 73 | DistributedFlowRuleStore flowStoreImpl; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 74 | 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 Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 139 | 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 Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 151 | return new TestAsyncDocumentTree<>(name()); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 152 | } |
| 153 | }; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public <T> Topic<T> getTopic(String name, Serializer serializer) { |
| 158 | return new TestTopic<>(name); |
| 159 | } |
| 160 | } |
| 161 | |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 162 | @Before |
| 163 | public void setUp() throws Exception { |
| 164 | flowStoreImpl = new DistributedFlowRuleStore(); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 165 | flowStoreImpl.storageService = new MockStorageService(); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 166 | 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 Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 179 | flowStoreImpl.activate(); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @After |
| 183 | public void tearDown() throws Exception { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 184 | flowStoreImpl.deactivate(); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 185 | } |
| 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 Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 269 | flowStoreImpl.storeBatch(new FlowRuleBatchOperation( |
| 270 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry)), |
| 271 | flowEntry.deviceId(), 1)); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 272 | |
| 273 | FlowEntry flowEntry1 = new DefaultFlowEntry(flowRule1); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 274 | flowStoreImpl.storeBatch(new FlowRuleBatchOperation( |
| 275 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry1)), |
| 276 | flowEntry1.deviceId(), 2)); |
| 277 | |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 278 | 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 | } |