yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
yoonseon | 86bebed | 2017-02-03 15:23:57 -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 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 22 | import org.onlab.junit.TestUtils; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onlab.osgi.TestServiceDirectory; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.event.EventDeliveryService; |
| 30 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualNetworkFlowObjectiveStore; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualNetworkFlowRuleStore; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 35 | import org.onosproject.incubator.net.virtual.impl.provider.VirtualProviderManager; |
| 36 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider; |
| 37 | import org.onosproject.incubator.net.virtual.provider.VirtualFlowRuleProvider; |
| 38 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
| 39 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 40 | import org.onosproject.incubator.store.virtual.impl.SimpleVirtualFlowObjectiveStore; |
| 41 | import org.onosproject.incubator.store.virtual.impl.SimpleVirtualFlowRuleStore; |
| 42 | import org.onosproject.net.NetTestTools; |
| 43 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 45 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 7bf273c | 2017-09-27 16:15:15 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 49 | import org.onosproject.net.flow.criteria.Criteria; |
| 50 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 53 | import org.onosproject.net.flowobjective.FilteringObjective; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 55 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 56 | import org.onosproject.net.flowobjective.NextObjective; |
Yoonseon Han | e1a7e54 | 2017-05-03 15:57:58 -0700 | [diff] [blame] | 57 | import org.onosproject.net.flowobjective.Objective; |
| 58 | import org.onosproject.net.flowobjective.ObjectiveContext; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 59 | import org.onosproject.net.provider.ProviderId; |
| 60 | import org.onosproject.store.service.StorageService; |
| 61 | import org.onosproject.store.service.TestStorageService; |
| 62 | |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 63 | import static org.junit.Assert.assertEquals; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Junit tests for VirtualNetworkFlowObjectiveManager. |
| 67 | */ |
| 68 | public class VirtualNetworkFlowObjectiveManagerTest |
| 69 | extends VirtualNetworkTestUtil { |
| 70 | |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 71 | private VirtualNetworkManager manager; |
| 72 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 73 | private ServiceDirectory testDirectory; |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 74 | protected SimpleVirtualFlowObjectiveStore flowObjectiveStore; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 75 | |
| 76 | private VirtualProviderManager providerRegistryService; |
| 77 | private EventDeliveryService eventDeliveryService; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 78 | |
| 79 | private ApplicationId appId; |
| 80 | |
| 81 | private VirtualNetwork vnet1; |
| 82 | private VirtualNetwork vnet2; |
| 83 | |
| 84 | private FlowObjectiveService service1; |
| 85 | private FlowObjectiveService service2; |
| 86 | |
| 87 | //FIXME: referring flowrule service, store, and provider shouldn't be here |
| 88 | private VirtualFlowRuleProvider flowRuleProvider = new TestProvider(); |
| 89 | private SimpleVirtualFlowRuleStore flowRuleStore; |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 90 | protected StorageService storageService = new TestStorageService(); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 91 | |
| 92 | @Before |
| 93 | public void setUp() throws Exception { |
| 94 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 95 | |
| 96 | CoreService coreService = new TestCoreService(); |
| 97 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 98 | TestUtils.setField(virtualNetworkManagerStore, "storageService", storageService); |
| 99 | virtualNetworkManagerStore.activate(); |
| 100 | |
| 101 | flowObjectiveStore = new SimpleVirtualFlowObjectiveStore(); |
| 102 | TestUtils.setField(flowObjectiveStore, "storageService", storageService); |
| 103 | flowObjectiveStore.activate(); |
| 104 | flowRuleStore = new SimpleVirtualFlowRuleStore(); |
| 105 | flowRuleStore.activate(); |
| 106 | |
| 107 | manager = new VirtualNetworkManager(); |
| 108 | manager.store = virtualNetworkManagerStore; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 109 | TestUtils.setField(manager, "coreService", coreService); |
| 110 | |
| 111 | providerRegistryService = new VirtualProviderManager(); |
| 112 | providerRegistryService.registerProvider(flowRuleProvider); |
| 113 | |
| 114 | eventDeliveryService = new TestEventDispatcher(); |
| 115 | NetTestTools.injectEventDispatcher(manager, eventDeliveryService); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 116 | |
| 117 | appId = new TestApplicationId("FlowRuleManagerTest"); |
| 118 | |
| 119 | testDirectory = new TestServiceDirectory() |
| 120 | .add(VirtualNetworkStore.class, virtualNetworkManagerStore) |
| 121 | .add(CoreService.class, coreService) |
| 122 | .add(EventDeliveryService.class, eventDeliveryService) |
| 123 | .add(VirtualProviderRegistryService.class, providerRegistryService) |
| 124 | .add(VirtualNetworkFlowRuleStore.class, flowRuleStore) |
| 125 | .add(VirtualNetworkFlowObjectiveStore.class, flowObjectiveStore); |
| 126 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
| 127 | |
| 128 | manager.activate(); |
| 129 | |
| 130 | vnet1 = setupVirtualNetworkTopology(manager, TID1); |
| 131 | vnet2 = setupVirtualNetworkTopology(manager, TID2); |
| 132 | |
| 133 | service1 = new VirtualNetworkFlowObjectiveManager(manager, vnet1.id()); |
| 134 | service2 = new VirtualNetworkFlowObjectiveManager(manager, vnet2.id()); |
| 135 | } |
| 136 | |
| 137 | @After |
| 138 | public void tearDownTest() { |
| 139 | manager.deactivate(); |
| 140 | virtualNetworkManagerStore.deactivate(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests adding a forwarding objective. |
| 145 | */ |
| 146 | @Test |
| 147 | public void forwardingObjective() { |
| 148 | TrafficSelector selector = DefaultTrafficSelector.emptySelector(); |
| 149 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 150 | ForwardingObjective forward = |
| 151 | DefaultForwardingObjective.builder() |
| 152 | .fromApp(NetTestTools.APP_ID) |
| 153 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 154 | .withSelector(selector) |
| 155 | .withTreatment(treatment) |
| 156 | .makePermanent() |
Yoonseon Han | e1a7e54 | 2017-05-03 15:57:58 -0700 | [diff] [blame] | 157 | .add(new ObjectiveContext() { |
| 158 | @Override |
| 159 | public void onSuccess(Objective objective) { |
| 160 | assertEquals("1 flowrule entry expected", |
| 161 | 1, flowRuleStore.getFlowRuleCount(vnet1.id())); |
| 162 | assertEquals("0 flowrule entry expected", |
| 163 | 0, flowRuleStore.getFlowRuleCount(vnet2.id())); |
| 164 | } |
| 165 | }); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 166 | |
| 167 | service1.forward(VDID1, forward); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 170 | /** |
| 171 | * Tests adding a next objective. |
| 172 | */ |
| 173 | @Test |
| 174 | public void nextObjective() { |
| 175 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 176 | NextObjective nextObjective = DefaultNextObjective.builder() |
| 177 | .withId(service1.allocateNextId()) |
| 178 | .fromApp(NetTestTools.APP_ID) |
| 179 | .addTreatment(treatment) |
| 180 | .withType(NextObjective.Type.BROADCAST) |
| 181 | .makePermanent() |
Yoonseon Han | e1a7e54 | 2017-05-03 15:57:58 -0700 | [diff] [blame] | 182 | .add(new ObjectiveContext() { |
| 183 | @Override |
| 184 | public void onSuccess(Objective objective) { |
| 185 | assertEquals("1 next map entry expected", |
| 186 | 1, service1.getNextMappings().size()); |
| 187 | assertEquals("0 next map entry expected", |
| 188 | 0, service2.getNextMappings().size()); |
| 189 | } |
| 190 | }); |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 191 | |
| 192 | service1.next(VDID1, nextObjective); |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Tests adding a filtering objective. |
| 197 | */ |
| 198 | @Test |
| 199 | public void filteringObjective() { |
| 200 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 201 | FilteringObjective filter = |
| 202 | DefaultFilteringObjective.builder() |
| 203 | .fromApp(NetTestTools.APP_ID) |
| 204 | .withMeta(treatment) |
| 205 | .makePermanent() |
| 206 | .deny() |
| 207 | .addCondition(Criteria.matchEthType(12)) |
Yoonseon Han | e1a7e54 | 2017-05-03 15:57:58 -0700 | [diff] [blame] | 208 | .add(new ObjectiveContext() { |
| 209 | @Override |
| 210 | public void onSuccess(Objective objective) { |
| 211 | assertEquals("1 flowrule entry expected", |
| 212 | 1, |
| 213 | flowRuleStore.getFlowRuleCount(vnet1.id())); |
| 214 | assertEquals("0 flowrule entry expected", |
| 215 | 0, |
| 216 | flowRuleStore.getFlowRuleCount(vnet2.id())); |
| 217 | |
| 218 | } |
| 219 | }); |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 220 | |
| 221 | service1.filter(VDID1, filter); |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 224 | //TODO: More test cases for filter, forward, and next |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 225 | |
| 226 | private class TestProvider extends AbstractVirtualProvider |
| 227 | implements VirtualFlowRuleProvider { |
| 228 | |
| 229 | protected TestProvider() { |
| 230 | super(new ProviderId("test", "org.onosproject.virtual.testprovider")); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void applyFlowRule(NetworkId networkId, FlowRule... flowRules) { |
| 235 | |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void removeFlowRule(NetworkId networkId, FlowRule... flowRules) { |
| 240 | |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void executeBatch(NetworkId networkId, FlowRuleBatchOperation batch) { |
| 245 | |
| 246 | } |
| 247 | } |
Claudine Chiu | e502e9d | 2017-03-27 11:00:50 -0400 | [diff] [blame] | 248 | } |