yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl; |
| 18 | |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import com.google.common.util.concurrent.MoreExecutors; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.junit.TestUtils; |
| 28 | import org.onlab.osgi.ServiceDirectory; |
| 29 | import org.onlab.osgi.TestServiceDirectory; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 30 | import org.onosproject.TestApplicationId; |
| 31 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 34 | import org.onosproject.event.EventDeliveryService; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 35 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 36 | import org.onosproject.incubator.net.virtual.TenantId; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 38 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 39 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 40 | import org.onosproject.incubator.net.virtual.VirtualNetworkFlowRuleStore; |
| 41 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 42 | import org.onosproject.incubator.net.virtual.event.VirtualEvent; |
| 43 | import org.onosproject.incubator.net.virtual.event.VirtualListenerRegistryManager; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 44 | import org.onosproject.incubator.net.virtual.impl.provider.VirtualProviderManager; |
| 45 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider; |
| 46 | import org.onosproject.incubator.net.virtual.provider.VirtualFlowRuleProvider; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 47 | import org.onosproject.incubator.net.virtual.provider.VirtualFlowRuleProviderService; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 48 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
| 49 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 50 | import org.onosproject.incubator.store.virtual.impl.SimpleVirtualFlowRuleStore; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 51 | import org.onosproject.net.ConnectPoint; |
| 52 | import org.onosproject.net.DeviceId; |
| 53 | import org.onosproject.net.Link; |
| 54 | import org.onosproject.net.NetTestTools; |
| 55 | import org.onosproject.net.PortNumber; |
| 56 | import org.onosproject.net.TestDeviceParams; |
| 57 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 58 | import org.onosproject.net.flow.DefaultFlowRule; |
| 59 | import org.onosproject.net.flow.FlowEntry; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flow.FlowRule; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 61 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 62 | import org.onosproject.net.flow.FlowRuleEvent; |
| 63 | import org.onosproject.net.flow.FlowRuleListener; |
| 64 | import org.onosproject.net.flow.FlowRuleService; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 65 | import org.onosproject.net.flow.StoredFlowEntry; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 66 | import org.onosproject.net.flow.TrafficSelector; |
| 67 | import org.onosproject.net.flow.TrafficTreatment; |
| 68 | import org.onosproject.net.flow.criteria.Criterion; |
| 69 | import org.onosproject.net.flow.instructions.Instruction; |
| 70 | import org.onosproject.net.flow.instructions.Instructions; |
| 71 | import org.onosproject.net.intent.FakeIntentManager; |
| 72 | import org.onosproject.net.intent.TestableIntentService; |
| 73 | import org.onosproject.net.provider.ProviderId; |
| 74 | import org.onosproject.store.service.TestStorageService; |
| 75 | |
| 76 | import java.util.ArrayList; |
| 77 | import java.util.Collections; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 78 | import java.util.HashMap; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 79 | import java.util.List; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 80 | import java.util.Map; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 81 | import java.util.Set; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 82 | |
| 83 | import static org.junit.Assert.*; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 84 | import static org.onosproject.net.flow.FlowRuleEvent.Type.*; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 85 | |
| 86 | public class VirtualNetworkFlowRuleManagerTest extends TestDeviceParams { |
| 87 | private static final int TIMEOUT = 10; |
| 88 | |
| 89 | private VirtualNetworkManager manager; |
| 90 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 91 | private TestableIntentService intentService = new FakeIntentManager(); |
| 92 | private ServiceDirectory testDirectory; |
| 93 | private VirtualNetworkFlowRuleStore flowRuleStore; |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 94 | private VirtualProviderManager providerRegistryService; |
| 95 | |
| 96 | private EventDeliveryService eventDeliveryService; |
| 97 | VirtualListenerRegistryManager listenerRegistryManager = |
| 98 | VirtualListenerRegistryManager.getInstance(); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 99 | |
| 100 | private VirtualNetworkFlowRuleManager vnetFlowRuleService1; |
| 101 | private VirtualNetworkFlowRuleManager vnetFlowRuleService2; |
| 102 | |
| 103 | private VirtualFlowRuleProvider provider = new TestProvider(); |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 104 | private VirtualFlowRuleProviderService providerService1; |
| 105 | private VirtualFlowRuleProviderService providerService2; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 106 | |
| 107 | protected TestFlowRuleListener listener1 = new TestFlowRuleListener(); |
| 108 | protected TestFlowRuleListener listener2 = new TestFlowRuleListener(); |
| 109 | |
| 110 | private final TenantId tid1 = TenantId.tenantId("tid1"); |
| 111 | private final TenantId tid2 = TenantId.tenantId("tid2"); |
| 112 | |
| 113 | private VirtualNetwork vnet1; |
| 114 | private VirtualNetwork vnet2; |
| 115 | |
| 116 | private ApplicationId appId; |
| 117 | |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 118 | @Before |
| 119 | public void setUp() throws Exception { |
| 120 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 121 | |
| 122 | CoreService coreService = new TestCoreService(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 123 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 124 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 125 | virtualNetworkManagerStore.activate(); |
| 126 | |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 127 | flowRuleStore = new SimpleVirtualFlowRuleStore(); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 128 | |
| 129 | providerRegistryService = new VirtualProviderManager(); |
| 130 | providerRegistryService.registerProvider(provider); |
| 131 | |
| 132 | manager = new VirtualNetworkManager(); |
| 133 | manager.store = virtualNetworkManagerStore; |
| 134 | manager.intentService = intentService; |
| 135 | TestUtils.setField(manager, "coreService", coreService); |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 136 | |
| 137 | eventDeliveryService = new TestEventDispatcher(); |
| 138 | NetTestTools.injectEventDispatcher(manager, eventDeliveryService); |
| 139 | eventDeliveryService.addSink(VirtualEvent.class, listenerRegistryManager); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 140 | |
| 141 | appId = new TestApplicationId("FlowRuleManagerTest"); |
| 142 | |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 143 | testDirectory = new TestServiceDirectory() |
| 144 | .add(VirtualNetworkStore.class, virtualNetworkManagerStore) |
| 145 | .add(CoreService.class, coreService) |
| 146 | .add(VirtualProviderRegistryService.class, providerRegistryService) |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 147 | .add(EventDeliveryService.class, eventDeliveryService) |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 148 | .add(VirtualNetworkFlowRuleStore.class, flowRuleStore); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 149 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 150 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 151 | manager.activate(); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 152 | |
| 153 | vnet1 = setupVirtualNetworkTopology(tid1); |
| 154 | vnet2 = setupVirtualNetworkTopology(tid2); |
| 155 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 156 | vnetFlowRuleService1 = new VirtualNetworkFlowRuleManager(manager, vnet1.id()); |
| 157 | vnetFlowRuleService2 = new VirtualNetworkFlowRuleManager(manager, vnet2.id()); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 158 | vnetFlowRuleService1.addListener(listener1); |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 159 | vnetFlowRuleService2.addListener(listener2); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 160 | |
| 161 | vnetFlowRuleService1.operationsService = MoreExecutors.newDirectExecutorService(); |
| 162 | vnetFlowRuleService2.operationsService = MoreExecutors.newDirectExecutorService(); |
| 163 | vnetFlowRuleService1.deviceInstallers = MoreExecutors.newDirectExecutorService(); |
| 164 | vnetFlowRuleService2.deviceInstallers = MoreExecutors.newDirectExecutorService(); |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 165 | |
| 166 | providerService1 = (VirtualFlowRuleProviderService) |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 167 | providerRegistryService.getProviderService(vnet1.id(), VirtualFlowRuleProvider.class); |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 168 | providerService2 = (VirtualFlowRuleProviderService) |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 169 | providerRegistryService.getProviderService(vnet2.id(), VirtualFlowRuleProvider.class); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @After |
| 173 | public void tearDown() { |
| 174 | manager.deactivate(); |
| 175 | virtualNetworkManagerStore.deactivate(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Method to create the virtual network for further testing. |
| 180 | * |
| 181 | * @return virtual network |
| 182 | */ |
| 183 | private VirtualNetwork setupVirtualNetworkTopology(TenantId tenantId) { |
| 184 | manager.registerTenantId(tenantId); |
| 185 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(tenantId); |
| 186 | |
| 187 | VirtualDevice virtualDevice1 = |
| 188 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 189 | VirtualDevice virtualDevice2 = |
| 190 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
| 191 | VirtualDevice virtualDevice3 = |
| 192 | manager.createVirtualDevice(virtualNetwork.id(), DID3); |
| 193 | VirtualDevice virtualDevice4 = |
| 194 | manager.createVirtualDevice(virtualNetwork.id(), DID4); |
| 195 | |
| 196 | ConnectPoint cp1 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(1)); |
| 197 | manager.createVirtualPort(virtualNetwork.id(), cp1.deviceId(), cp1.port(), cp1); |
| 198 | |
| 199 | ConnectPoint cp2 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(2)); |
| 200 | manager.createVirtualPort(virtualNetwork.id(), cp2.deviceId(), cp2.port(), cp2); |
| 201 | |
| 202 | ConnectPoint cp3 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(3)); |
| 203 | manager.createVirtualPort(virtualNetwork.id(), cp3.deviceId(), cp3.port(), cp3); |
| 204 | |
| 205 | ConnectPoint cp4 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(4)); |
| 206 | manager.createVirtualPort(virtualNetwork.id(), cp4.deviceId(), cp4.port(), cp4); |
| 207 | |
| 208 | ConnectPoint cp5 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(5)); |
| 209 | manager.createVirtualPort(virtualNetwork.id(), cp5.deviceId(), cp5.port(), cp5); |
| 210 | |
| 211 | ConnectPoint cp6 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(6)); |
| 212 | manager.createVirtualPort(virtualNetwork.id(), cp6.deviceId(), cp6.port(), cp6); |
| 213 | |
| 214 | VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3); |
| 215 | virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE); |
| 216 | VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1); |
| 217 | virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE); |
| 218 | VirtualLink link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5); |
| 219 | virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE); |
| 220 | VirtualLink link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4); |
| 221 | virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE); |
| 222 | VirtualLink link5 = manager.createVirtualLink(virtualNetwork.id(), cp2, cp6); |
| 223 | virtualNetworkManagerStore.updateLink(link5, link5.tunnelId(), Link.State.ACTIVE); |
| 224 | VirtualLink link6 = manager.createVirtualLink(virtualNetwork.id(), cp6, cp2); |
| 225 | virtualNetworkManagerStore.updateLink(link6, link6.tunnelId(), Link.State.ACTIVE); |
| 226 | |
| 227 | return virtualNetwork; |
| 228 | } |
| 229 | |
| 230 | private FlowRule flowRule(int tsval, int trval) { |
| 231 | return flowRule(DID1, tsval, trval); |
| 232 | } |
| 233 | |
| 234 | private FlowRule flowRule(DeviceId did, int tsval, int trval) { |
| 235 | TestSelector ts = new TestSelector(tsval); |
| 236 | TestTreatment tr = new TestTreatment(trval); |
| 237 | return DefaultFlowRule.builder() |
| 238 | .forDevice(did) |
| 239 | .withSelector(ts) |
| 240 | .withTreatment(tr) |
| 241 | .withPriority(10) |
| 242 | .fromApp(appId) |
| 243 | .makeTemporary(TIMEOUT) |
| 244 | .build(); |
| 245 | } |
| 246 | |
| 247 | private FlowRule addFlowRule(int hval) { |
| 248 | FlowRule rule = flowRule(hval, hval); |
| 249 | vnetFlowRuleService1.applyFlowRules(rule); |
| 250 | |
| 251 | assertNotNull("rule should be found", vnetFlowRuleService1.getFlowEntries(DID1)); |
| 252 | return rule; |
| 253 | } |
| 254 | |
| 255 | private int flowCount(FlowRuleService service) { |
| 256 | List<FlowEntry> entries = Lists.newArrayList(); |
| 257 | service.getFlowEntries(DID1).forEach(entries::add); |
| 258 | return entries.size(); |
| 259 | } |
| 260 | |
| 261 | @Test |
| 262 | public void getFlowEntries() { |
| 263 | assertTrue("store should be empty", |
| 264 | Sets.newHashSet(vnetFlowRuleService1.getFlowEntries(DID1)).isEmpty()); |
| 265 | assertTrue("store should be empty", |
| 266 | Sets.newHashSet(vnetFlowRuleService2.getFlowEntries(DID1)).isEmpty()); |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 267 | |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 268 | FlowRule f1 = addFlowRule(1); |
| 269 | FlowRule f2 = addFlowRule(2); |
| 270 | |
| 271 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 272 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 273 | |
| 274 | assertEquals("2 rules should exist", 2, flowCount(vnetFlowRuleService1)); |
| 275 | assertEquals("0 rules should exist", 0, flowCount(vnetFlowRuleService2)); |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 276 | |
| 277 | providerService1.pushFlowMetrics(DID1, ImmutableList.of(fe1, fe2)); |
| 278 | validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 279 | RULE_ADDED, RULE_ADDED); |
| 280 | |
| 281 | addFlowRule(1); |
| 282 | assertEquals("should still be 2 rules", 2, flowCount(vnetFlowRuleService1)); |
| 283 | System.err.println("events :" + listener1.events); |
| 284 | assertEquals("0 rules should exist", 0, flowCount(vnetFlowRuleService2)); |
| 285 | |
| 286 | providerService1.pushFlowMetrics(DID1, ImmutableList.of(fe1)); |
| 287 | validateEvents(listener1, RULE_UPDATED, RULE_UPDATED); |
| 288 | } |
| 289 | |
| 290 | @Test |
| 291 | public void applyFlowRules() { |
| 292 | FlowRule r1 = flowRule(1, 1); |
| 293 | FlowRule r2 = flowRule(2, 2); |
| 294 | FlowRule r3 = flowRule(3, 3); |
| 295 | |
| 296 | assertTrue("store should be empty", |
| 297 | Sets.newHashSet(vnetFlowRuleService1.getFlowEntries(DID1)).isEmpty()); |
| 298 | vnetFlowRuleService1.applyFlowRules(r1, r2, r3); |
| 299 | assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1)); |
| 300 | assertTrue("Entries should be pending add.", |
| 301 | validateState(ImmutableMap.of( |
| 302 | r1, FlowEntry.FlowEntryState.PENDING_ADD, |
| 303 | r2, FlowEntry.FlowEntryState.PENDING_ADD, |
| 304 | r3, FlowEntry.FlowEntryState.PENDING_ADD))); |
| 305 | } |
| 306 | |
| 307 | @Test |
| 308 | public void purgeFlowRules() { |
| 309 | FlowRule f1 = addFlowRule(1); |
| 310 | FlowRule f2 = addFlowRule(2); |
| 311 | FlowRule f3 = addFlowRule(3); |
| 312 | assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1)); |
| 313 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 314 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 315 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
| 316 | providerService1.pushFlowMetrics(DID1, ImmutableList.of(fe1, fe2, fe3)); |
| 317 | validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 318 | RULE_ADDED, RULE_ADDED, RULE_ADDED); |
| 319 | vnetFlowRuleService1.purgeFlowRules(DID1); |
| 320 | assertEquals("0 rule should exist", 0, flowCount(vnetFlowRuleService1)); |
| 321 | } |
| 322 | |
| 323 | @Test |
| 324 | public void removeFlowRules() { |
| 325 | FlowRule f1 = addFlowRule(1); |
| 326 | FlowRule f2 = addFlowRule(2); |
| 327 | FlowRule f3 = addFlowRule(3); |
| 328 | assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1)); |
| 329 | |
| 330 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 331 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 332 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
| 333 | providerService1.pushFlowMetrics(DID1, ImmutableList.of(fe1, fe2, fe3)); |
| 334 | validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 335 | RULE_ADDED, RULE_ADDED, RULE_ADDED); |
| 336 | |
| 337 | vnetFlowRuleService1.removeFlowRules(f1, f2); |
| 338 | //removing from north, so no events generated |
| 339 | validateEvents(listener1, RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED); |
| 340 | assertEquals("3 rule should exist", 3, flowCount(vnetFlowRuleService1)); |
| 341 | assertTrue("Entries should be pending remove.", |
| 342 | validateState(ImmutableMap.of( |
| 343 | f1, FlowEntry.FlowEntryState.PENDING_REMOVE, |
| 344 | f2, FlowEntry.FlowEntryState.PENDING_REMOVE, |
| 345 | f3, FlowEntry.FlowEntryState.ADDED))); |
| 346 | |
| 347 | vnetFlowRuleService1.removeFlowRules(f1); |
| 348 | assertEquals("3 rule should still exist", 3, flowCount(vnetFlowRuleService1)); |
| 349 | } |
| 350 | |
| 351 | @Test |
| 352 | public void flowRemoved() { |
| 353 | FlowRule f1 = addFlowRule(1); |
| 354 | FlowRule f2 = addFlowRule(2); |
| 355 | StoredFlowEntry fe1 = new DefaultFlowEntry(f1); |
| 356 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 357 | |
| 358 | providerService1.pushFlowMetrics(DID1, ImmutableList.of(fe1, fe2)); |
| 359 | vnetFlowRuleService1.removeFlowRules(f1); |
| 360 | |
| 361 | //FIXME modification of "stored" flow entry outside of store |
| 362 | fe1.setState(FlowEntry.FlowEntryState.REMOVED); |
| 363 | |
| 364 | providerService1.flowRemoved(fe1); |
| 365 | |
| 366 | validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, |
| 367 | RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED); |
| 368 | |
| 369 | providerService1.flowRemoved(fe1); |
| 370 | validateEvents(listener1); |
| 371 | |
| 372 | FlowRule f3 = flowRule(3, 3); |
| 373 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
| 374 | vnetFlowRuleService1.applyFlowRules(f3); |
| 375 | |
| 376 | providerService1.pushFlowMetrics(DID1, Collections.singletonList(fe3)); |
| 377 | validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED); |
| 378 | |
| 379 | providerService1.flowRemoved(fe3); |
| 380 | validateEvents(listener1); |
| 381 | } |
| 382 | |
| 383 | @Test |
| 384 | public void extraneousFlow() { |
| 385 | FlowRule f1 = flowRule(1, 1); |
| 386 | FlowRule f2 = flowRule(2, 2); |
| 387 | FlowRule f3 = flowRule(3, 3); |
| 388 | vnetFlowRuleService1.applyFlowRules(f1, f2); |
| 389 | |
| 390 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 391 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 392 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
| 393 | |
| 394 | |
| 395 | providerService1.pushFlowMetrics(DID1, Lists.newArrayList(fe1, fe2, fe3)); |
| 396 | |
| 397 | validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 398 | RULE_ADDED, RULE_ADDED); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Tests whether a rule that was marked for removal but no flowRemoved was received |
| 403 | * is indeed removed at the next stats update. |
| 404 | */ |
| 405 | @Test |
| 406 | public void flowMissingRemove() { |
| 407 | FlowRule f1 = flowRule(1, 1); |
| 408 | FlowRule f2 = flowRule(2, 2); |
| 409 | FlowRule f3 = flowRule(3, 3); |
| 410 | |
| 411 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 412 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 413 | vnetFlowRuleService1.applyFlowRules(f1, f2, f3); |
| 414 | |
| 415 | vnetFlowRuleService1.removeFlowRules(f3); |
| 416 | |
| 417 | providerService1.pushFlowMetrics(DID1, Lists.newArrayList(fe1, fe2)); |
| 418 | |
| 419 | validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 420 | RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED); |
| 421 | } |
| 422 | |
| 423 | @Test |
| 424 | public void removeByAppId() { |
| 425 | FlowRule f1 = flowRule(1, 1); |
| 426 | FlowRule f2 = flowRule(2, 2); |
| 427 | vnetFlowRuleService1.applyFlowRules(f1, f2); |
| 428 | |
| 429 | vnetFlowRuleService1.removeFlowRulesById(appId); |
| 430 | |
| 431 | //only check that we are in pending remove. Events and actual remove state will |
| 432 | // be set by flowRemoved call. |
| 433 | validateState(ImmutableMap.of( |
| 434 | f1, FlowEntry.FlowEntryState.PENDING_REMOVE, |
| 435 | f2, FlowEntry.FlowEntryState.PENDING_REMOVE)); |
| 436 | } |
| 437 | |
| 438 | //TODO:Tests for fallback |
| 439 | |
| 440 | private boolean validateState(Map<FlowRule, FlowEntry.FlowEntryState> expected) { |
| 441 | Map<FlowRule, FlowEntry.FlowEntryState> expectedToCheck = new HashMap<>(expected); |
| 442 | Iterable<FlowEntry> rules = vnetFlowRuleService1.getFlowEntries(DID1); |
| 443 | for (FlowEntry f : rules) { |
| 444 | assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f)); |
| 445 | assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state()); |
| 446 | expectedToCheck.remove(f); |
| 447 | } |
| 448 | assertEquals(Collections.emptySet(), expectedToCheck.entrySet()); |
| 449 | return true; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | private class TestSelector implements TrafficSelector { |
| 453 | |
| 454 | //for controlling hashcode uniqueness; |
| 455 | private final int testval; |
| 456 | |
| 457 | public TestSelector(int val) { |
| 458 | testval = val; |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public Set<Criterion> criteria() { |
| 463 | return Collections.emptySet(); |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public Criterion getCriterion( |
| 468 | org.onosproject.net.flow.criteria.Criterion.Type type) { |
| 469 | return null; |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public int hashCode() { |
| 474 | return testval; |
| 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public boolean equals(Object o) { |
| 479 | if (o instanceof TestSelector) { |
| 480 | return this.testval == ((TestSelector) o).testval; |
| 481 | } |
| 482 | return false; |
| 483 | } |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | private class TestTreatment implements TrafficTreatment { |
| 487 | |
| 488 | //for controlling hashcode uniqueness; |
| 489 | private final int testval; |
| 490 | |
| 491 | public TestTreatment(int val) { |
| 492 | testval = val; |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public List<Instruction> deferred() { |
| 497 | return null; |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public List<Instruction> immediate() { |
| 502 | return null; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public List<Instruction> allInstructions() { |
| 507 | return null; |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public Instructions.TableTypeTransition tableTransition() { |
| 512 | return null; |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public boolean clearedDeferred() { |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public int hashCode() { |
| 522 | return testval; |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public boolean equals(Object o) { |
| 527 | if (o instanceof TestTreatment) { |
| 528 | return this.testval == ((TestTreatment) o).testval; |
| 529 | } |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | @Override |
| 534 | public Instructions.MetadataInstruction writeMetadata() { |
| 535 | return null; |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public Instructions.MeterInstruction metered() { |
| 540 | return null; |
| 541 | } |
| 542 | } |
| 543 | |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 544 | private void validateEvents(TestFlowRuleListener listener, FlowRuleEvent.Type... events) { |
| 545 | if (events == null) { |
| 546 | assertTrue("events generated", listener.events.isEmpty()); |
| 547 | } |
| 548 | |
| 549 | int i = 0; |
| 550 | System.err.println("events :" + listener.events); |
| 551 | for (FlowRuleEvent e : listener.events) { |
| 552 | assertEquals("unexpected event", events[i], e.type()); |
| 553 | i++; |
| 554 | } |
| 555 | |
| 556 | assertEquals("mispredicted number of events", |
| 557 | events.length, listener.events.size()); |
| 558 | |
| 559 | listener.events.clear(); |
| 560 | } |
| 561 | |
| 562 | private class TestFlowRuleListener implements FlowRuleListener { |
| 563 | |
| 564 | public final List<FlowRuleEvent> events = new ArrayList<>(); |
| 565 | |
| 566 | @Override |
| 567 | public void event(FlowRuleEvent event) { |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 568 | events.add(event); |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | private class TestProvider extends AbstractVirtualProvider |
| 573 | implements VirtualFlowRuleProvider { |
| 574 | |
| 575 | protected TestProvider() { |
| 576 | super(new ProviderId("test", "org.onosproject.virtual.testprovider")); |
| 577 | } |
| 578 | |
| 579 | @Override |
| 580 | public void applyFlowRule(NetworkId networkId, FlowRule... flowRules) { |
| 581 | |
| 582 | } |
| 583 | |
| 584 | @Override |
| 585 | public void removeFlowRule(NetworkId networkId, FlowRule... flowRules) { |
| 586 | |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public void executeBatch(NetworkId networkId, FlowRuleBatchOperation batch) { |
| 591 | |
| 592 | } |
| 593 | } |
| 594 | } |