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