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; |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
| 30 | import org.onosproject.cluster.ControllerNode; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 31 | import org.onosproject.cluster.NodeId; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 32 | import org.onosproject.core.CoreServiceAdapter; |
| 33 | import org.onosproject.mastership.MastershipServiceAdapter; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.MastershipRole; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 37 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 38 | import org.onosproject.net.flow.DefaultFlowRule; |
| 39 | import org.onosproject.net.flow.FlowEntry; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 7bf273c | 2017-09-27 16:15:15 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 42 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.FlowRuleOperation; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 44 | import org.onosproject.net.intent.IntentTestsMocks; |
| 45 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 46 | import org.onosproject.store.service.AsyncDocumentTree; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 47 | import org.onosproject.store.service.DocumentTreeBuilder; |
| 48 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.TestAsyncDocumentTree; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 50 | import org.onosproject.store.service.TestStorageService; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 51 | import org.onosproject.store.service.TestTopic; |
| 52 | import org.onosproject.store.service.Topic; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 53 | |
| 54 | import static org.easymock.EasyMock.createMock; |
| 55 | import static org.easymock.EasyMock.expect; |
| 56 | import static org.easymock.EasyMock.replay; |
| 57 | import static org.hamcrest.MatcherAssert.assertThat; |
| 58 | import static org.hamcrest.Matchers.emptyIterable; |
| 59 | import static org.hamcrest.Matchers.is; |
| 60 | import static org.hamcrest.Matchers.notNullValue; |
| 61 | import static org.junit.Assert.assertEquals; |
| 62 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 63 | import static org.onosproject.net.NetTestTools.did; |
| 64 | |
| 65 | /** |
| 66 | * Test class for DistributedFlowRuleStore. |
Ray Milkey | ed9bace | 2018-03-20 10:05:56 -0700 | [diff] [blame] | 67 | * |
| 68 | * @deprecated in Nightingale Release (1.13) |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 69 | */ |
Ray Milkey | ed9bace | 2018-03-20 10:05:56 -0700 | [diff] [blame] | 70 | @Deprecated |
| 71 | @Ignore("The distributed flow rule store has been deprecated") |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 72 | public class DistributedFlowRuleStoreTest { |
| 73 | |
| 74 | DistributedFlowRuleStore flowStoreImpl; |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 75 | 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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 130 | public String host() { |
| 131 | return "127.0.0.1"; |
| 132 | } |
| 133 | |
| 134 | @Override |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 135 | public Ip4Address ip() { |
| 136 | return Ip4Address.valueOf("127.0.0.1"); |
| 137 | } |
| 138 | |
| 139 | @Override |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 140 | public IpAddress ip(boolean resolve) { |
| 141 | return ip(); |
| 142 | } |
| 143 | |
| 144 | @Override |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 145 | public int tcpPort() { |
| 146 | return 0; |
| 147 | } |
| 148 | } |
| 149 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 150 | 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 Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 162 | return new TestAsyncDocumentTree<>(name()); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 163 | } |
| 164 | }; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public <T> Topic<T> getTopic(String name, Serializer serializer) { |
| 169 | return new TestTopic<>(name); |
| 170 | } |
| 171 | } |
| 172 | |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 173 | @Before |
| 174 | public void setUp() throws Exception { |
| 175 | flowStoreImpl = new DistributedFlowRuleStore(); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 176 | flowStoreImpl.storageService = new MockStorageService(); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 177 | 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 Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 190 | flowStoreImpl.activate(); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @After |
| 194 | public void tearDown() throws Exception { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 195 | flowStoreImpl.deactivate(); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 196 | } |
| 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 Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 280 | flowStoreImpl.storeBatch(new FlowRuleBatchOperation( |
| 281 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry)), |
| 282 | flowEntry.deviceId(), 1)); |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 283 | |
| 284 | FlowEntry flowEntry1 = new DefaultFlowEntry(flowRule1); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 285 | flowStoreImpl.storeBatch(new FlowRuleBatchOperation( |
| 286 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, flowEntry1)), |
| 287 | flowEntry1.deviceId(), 2)); |
| 288 | |
sivachidambaram subramanian | 5168f61 | 2017-03-08 13:53:30 +0530 | [diff] [blame] | 289 | 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 | } |