Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.flow.impl; |
| 17 | |
| 18 | import com.google.common.collect.ArrayListMultimap; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame^] | 19 | import com.google.common.collect.ImmutableMap; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Multimap; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | |
| 25 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.cluster.ClusterService; |
| 28 | import org.onosproject.cluster.ControllerNode; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame^] | 30 | import org.onosproject.mastership.MastershipInfo; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 31 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 32 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.MastershipRole; |
| 35 | import org.onosproject.net.flow.FlowRuleOperation; |
| 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 38 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 39 | import org.onosproject.net.flow.FlowEntry; |
| 40 | import org.onosproject.net.flow.DefaultFlowRule; |
| 41 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 42 | import org.onosproject.net.intent.IntentTestsMocks; |
| 43 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
| 44 | import org.onosproject.store.persistence.PersistenceServiceAdapter; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame^] | 45 | import org.onosproject.store.service.AsyncConsistentMap; |
| 46 | import org.onosproject.store.service.AsyncConsistentMapAdapter; |
| 47 | import org.onosproject.store.service.ConsistentMap; |
| 48 | import org.onosproject.store.service.ConsistentMapBuilder; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.TestStorageService; |
| 50 | |
| 51 | import org.onlab.packet.Ip4Address; |
| 52 | import java.util.Iterator; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame^] | 53 | import java.util.Optional; |
| 54 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 55 | import org.osgi.service.component.ComponentContext; |
| 56 | |
| 57 | import static org.easymock.EasyMock.createMock; |
| 58 | import static org.easymock.EasyMock.expect; |
| 59 | import static org.easymock.EasyMock.replay; |
| 60 | import static org.hamcrest.MatcherAssert.assertThat; |
| 61 | import static org.hamcrest.Matchers.emptyIterable; |
| 62 | import static org.hamcrest.Matchers.is; |
| 63 | import static org.hamcrest.Matchers.notNullValue; |
| 64 | import static org.junit.Assert.assertEquals; |
| 65 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 66 | import static org.onosproject.net.NetTestTools.did; |
| 67 | |
| 68 | /** |
| 69 | * Test class for ECFlowRuleStore. |
| 70 | */ |
| 71 | public class ECFlowRuleStoreTest { |
| 72 | |
| 73 | ECFlowRuleStore flowStoreImpl; |
| 74 | ComponentContext context = null; |
| 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 | } |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame^] | 114 | |
| 115 | @Override |
| 116 | public MastershipInfo getMastershipFor(DeviceId deviceId) { |
| 117 | return new MastershipInfo( |
| 118 | 1, |
| 119 | Optional.of(NodeId.nodeId("1")), |
| 120 | ImmutableMap.<NodeId, MastershipRole>builder() |
| 121 | .put(NodeId.nodeId("1"), MastershipRole.MASTER) |
| 122 | .build()); |
| 123 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | private static class MockControllerNode implements ControllerNode { |
| 128 | final NodeId id; |
| 129 | |
| 130 | public MockControllerNode(NodeId id) { |
| 131 | this.id = id; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public NodeId id() { |
| 136 | return this.id; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public Ip4Address ip() { |
| 141 | return Ip4Address.valueOf("127.0.0.1"); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public int tcpPort() { |
| 146 | return 0; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Before |
| 151 | public void setUp() throws Exception { |
| 152 | flowStoreImpl = new ECFlowRuleStore(); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame^] | 153 | flowStoreImpl.storageService = new TestStorageService() { |
| 154 | @Override |
| 155 | public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() { |
| 156 | return new ConsistentMapBuilder<K, V>() { |
| 157 | @Override |
| 158 | public AsyncConsistentMap<K, V> buildAsyncMap() { |
| 159 | return new AsyncConsistentMapAdapter<K, V>(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public ConsistentMap<K, V> build() { |
| 164 | return null; |
| 165 | } |
| 166 | }; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | ReplicaInfoManager replicaInfoManager = new ReplicaInfoManager(); |
| 171 | replicaInfoManager.mastershipService = new MasterOfAll(); |
| 172 | |
| 173 | flowStoreImpl.replicaInfoManager = replicaInfoManager; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 174 | mockClusterService = createMock(ClusterService.class); |
| 175 | flowStoreImpl.clusterService = mockClusterService; |
| 176 | nodeId = new NodeId("1"); |
| 177 | mockControllerNode = new MockControllerNode(nodeId); |
| 178 | |
| 179 | expect(mockClusterService.getLocalNode()) |
| 180 | .andReturn(mockControllerNode).anyTimes(); |
| 181 | replay(mockClusterService); |
| 182 | |
| 183 | flowStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter(); |
| 184 | flowStoreImpl.mastershipService = new MasterOfAll(); |
| 185 | flowStoreImpl.deviceService = new DeviceServiceAdapter(); |
| 186 | flowStoreImpl.coreService = new CoreServiceAdapter(); |
| 187 | flowStoreImpl.configService = new ComponentConfigAdapter(); |
| 188 | flowStoreImpl.persistenceService = new PersistenceServiceAdapter(); |
| 189 | flowStoreImpl.activate(context); |
| 190 | } |
| 191 | |
| 192 | @After |
| 193 | public void tearDown() throws Exception { |
| 194 | flowStoreImpl.deactivate(context); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Tests the initial state of the store. |
| 199 | */ |
| 200 | @Test |
| 201 | public void testEmptyStore() { |
| 202 | assertThat(flowStoreImpl.getFlowRuleCount(), is(0)); |
| 203 | assertThat(flowStoreImpl.getFlowEntries(deviceId), is(emptyIterable())); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Tests initial state of flowrule. |
| 208 | */ |
| 209 | @Test |
| 210 | public void testStoreBatch() { |
| 211 | FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD); |
| 212 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create(); |
| 213 | perDeviceBatches.put(op.rule().deviceId(), |
| 214 | new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule())); |
| 215 | FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId), |
| 216 | deviceId, 1); |
| 217 | flowStoreImpl.storeBatch(b); |
| 218 | FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule); |
| 219 | assertEquals("PENDING_ADD", flowEntry1.state().toString()); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Tests adding a flowrule. |
| 224 | */ |
| 225 | @Test |
| 226 | public void testAddFlow() { |
| 227 | FlowEntry flowEntry = new DefaultFlowEntry(flowRule); |
| 228 | FlowRuleOperation op = new FlowRuleOperation(flowRule, FlowRuleOperation.Type.ADD); |
| 229 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create(); |
| 230 | perDeviceBatches.put(op.rule().deviceId(), |
| 231 | new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, op.rule())); |
| 232 | FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId), |
| 233 | deviceId, 1); |
| 234 | flowStoreImpl.storeBatch(b); |
| 235 | FlowEntry flowEntry1 = flowStoreImpl.getFlowEntry(flowRule); |
| 236 | assertEquals("PENDING_ADD", flowEntry1.state().toString()); |
| 237 | |
| 238 | flowStoreImpl.addOrUpdateFlowRule(flowEntry); |
| 239 | Iterable<FlowEntry> flows = flowStoreImpl.getFlowEntries(deviceId); |
| 240 | int sum = 0; |
| 241 | Iterator it = flows.iterator(); |
| 242 | while (it.hasNext()) { |
| 243 | it.next(); |
| 244 | sum++; |
| 245 | } |
| 246 | assertThat(sum, is(1)); |
| 247 | |
| 248 | FlowEntry flowEntry2 = flowStoreImpl.getFlowEntry(flowRule); |
| 249 | assertEquals("ADDED", flowEntry2.state().toString()); |
| 250 | assertThat(flowStoreImpl.getTableStatistics(deviceId), notNullValue()); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Tests flow removal. |
| 255 | */ |
| 256 | @Test |
| 257 | public void testRemoveFlow() { |
| 258 | Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId); |
| 259 | for (FlowEntry flow : flows1) { |
| 260 | flowStoreImpl.removeFlowRule(flow); |
| 261 | } |
| 262 | |
| 263 | Iterable<FlowEntry> flows2 = flowStoreImpl.getFlowEntries(deviceId); |
| 264 | int sum = 0; |
| 265 | Iterator it = flows2.iterator(); |
| 266 | while (it.hasNext()) { |
| 267 | it.next(); |
| 268 | sum++; |
| 269 | } |
| 270 | assertThat(sum, is(0)); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Tests purge flow for a device. |
| 275 | */ |
| 276 | @Test |
| 277 | public void testPurgeFlow() { |
| 278 | FlowEntry flowEntry = new DefaultFlowEntry(flowRule); |
| 279 | flowStoreImpl.addOrUpdateFlowRule(flowEntry); |
| 280 | |
| 281 | FlowEntry flowEntry1 = new DefaultFlowEntry(flowRule1); |
| 282 | flowStoreImpl.addOrUpdateFlowRule(flowEntry1); |
| 283 | Iterable<FlowEntry> flows1 = flowStoreImpl.getFlowEntries(deviceId); |
| 284 | int sum2 = 0; |
| 285 | Iterator it1 = flows1.iterator(); |
| 286 | while (it1.hasNext()) { |
| 287 | it1.next(); |
| 288 | sum2++; |
| 289 | } |
| 290 | assertThat(sum2, is(2)); |
| 291 | flowStoreImpl.purgeFlowRule(deviceId); |
| 292 | |
| 293 | Iterable<FlowEntry> flows3 = flowStoreImpl.getFlowEntries(deviceId); |
| 294 | int sum3 = 0; |
| 295 | Iterator it3 = flows3.iterator(); |
| 296 | while (it3.hasNext()) { |
| 297 | it3.next(); |
| 298 | sum3++; |
| 299 | } |
| 300 | assertThat(sum3, is(0)); |
| 301 | } |
| 302 | } |