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