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