blob: d567a94cf0f4218f0b85621d473069bb84341a61 [file] [log] [blame]
yoonseon86bebed2017-02-03 15:23:57 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
yoonseon86bebed2017-02-03 15:23:57 -08003 *
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
17package org.onosproject.incubator.net.virtual.impl;
18
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
yoonseon86bebed2017-02-03 15:23:57 -080022import org.onlab.junit.TestUtils;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onosproject.TestApplicationId;
26import org.onosproject.common.event.impl.TestEventDispatcher;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.event.EventDeliveryService;
30import org.onosproject.incubator.net.virtual.NetworkId;
31import org.onosproject.incubator.net.virtual.VirtualNetwork;
32import org.onosproject.incubator.net.virtual.VirtualNetworkFlowObjectiveStore;
33import org.onosproject.incubator.net.virtual.VirtualNetworkFlowRuleStore;
34import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
yoonseon86bebed2017-02-03 15:23:57 -080035import org.onosproject.incubator.net.virtual.impl.provider.VirtualProviderManager;
36import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider;
37import org.onosproject.incubator.net.virtual.provider.VirtualFlowRuleProvider;
38import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService;
39import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
40import org.onosproject.incubator.store.virtual.impl.SimpleVirtualFlowObjectiveStore;
41import org.onosproject.incubator.store.virtual.impl.SimpleVirtualFlowRuleStore;
42import org.onosproject.net.NetTestTools;
43import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
45import org.onosproject.net.flow.FlowRule;
Ray Milkey7bf273c2017-09-27 16:15:15 -070046import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
yoonseon86bebed2017-02-03 15:23:57 -080047import org.onosproject.net.flow.TrafficSelector;
48import org.onosproject.net.flow.TrafficTreatment;
Claudine Chiue502e9d2017-03-27 11:00:50 -040049import org.onosproject.net.flow.criteria.Criteria;
50import org.onosproject.net.flowobjective.DefaultFilteringObjective;
yoonseon86bebed2017-02-03 15:23:57 -080051import org.onosproject.net.flowobjective.DefaultForwardingObjective;
Claudine Chiue502e9d2017-03-27 11:00:50 -040052import org.onosproject.net.flowobjective.DefaultNextObjective;
53import org.onosproject.net.flowobjective.FilteringObjective;
yoonseon86bebed2017-02-03 15:23:57 -080054import org.onosproject.net.flowobjective.FlowObjectiveService;
55import org.onosproject.net.flowobjective.ForwardingObjective;
Claudine Chiue502e9d2017-03-27 11:00:50 -040056import org.onosproject.net.flowobjective.NextObjective;
Yoonseon Hane1a7e542017-05-03 15:57:58 -070057import org.onosproject.net.flowobjective.Objective;
58import org.onosproject.net.flowobjective.ObjectiveContext;
yoonseon86bebed2017-02-03 15:23:57 -080059import org.onosproject.net.provider.ProviderId;
60import org.onosproject.store.service.StorageService;
61import org.onosproject.store.service.TestStorageService;
62
Claudine Chiu465a2602017-03-17 18:33:36 -040063import static org.junit.Assert.assertEquals;
yoonseon86bebed2017-02-03 15:23:57 -080064
65/**
66 * Junit tests for VirtualNetworkFlowObjectiveManager.
67 */
68public class VirtualNetworkFlowObjectiveManagerTest
69 extends VirtualNetworkTestUtil {
70
yoonseon86bebed2017-02-03 15:23:57 -080071 private VirtualNetworkManager manager;
72 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
yoonseon86bebed2017-02-03 15:23:57 -080073 private ServiceDirectory testDirectory;
Claudine Chiucdc4b8c2017-03-30 00:34:39 -040074 protected SimpleVirtualFlowObjectiveStore flowObjectiveStore;
yoonseon86bebed2017-02-03 15:23:57 -080075
76 private VirtualProviderManager providerRegistryService;
77 private EventDeliveryService eventDeliveryService;
yoonseon86bebed2017-02-03 15:23:57 -080078
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 Chiucdc4b8c2017-03-30 00:34:39 -040090 protected StorageService storageService = new TestStorageService();
yoonseon86bebed2017-02-03 15:23:57 -080091
92 @Before
93 public void setUp() throws Exception {
94 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
95
96 CoreService coreService = new TestCoreService();
97 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
yoonseon86bebed2017-02-03 15:23:57 -080098 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;
yoonseon86bebed2017-02-03 15:23:57 -0800109 TestUtils.setField(manager, "coreService", coreService);
110
111 providerRegistryService = new VirtualProviderManager();
112 providerRegistryService.registerProvider(flowRuleProvider);
113
114 eventDeliveryService = new TestEventDispatcher();
115 NetTestTools.injectEventDispatcher(manager, eventDeliveryService);
yoonseon86bebed2017-02-03 15:23:57 -0800116
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 Hane1a7e542017-05-03 15:57:58 -0700157 .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 });
yoonseon86bebed2017-02-03 15:23:57 -0800166
167 service1.forward(VDID1, forward);
yoonseon86bebed2017-02-03 15:23:57 -0800168 }
169
Claudine Chiue502e9d2017-03-27 11:00:50 -0400170 /**
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 Hane1a7e542017-05-03 15:57:58 -0700182 .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 Chiue502e9d2017-03-27 11:00:50 -0400191
192 service1.next(VDID1, nextObjective);
Claudine Chiue502e9d2017-03-27 11:00:50 -0400193 }
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 Hane1a7e542017-05-03 15:57:58 -0700208 .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 Chiue502e9d2017-03-27 11:00:50 -0400220
221 service1.filter(VDID1, filter);
Claudine Chiue502e9d2017-03-27 11:00:50 -0400222 }
223
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700224 //TODO: More test cases for filter, forward, and next
yoonseon86bebed2017-02-03 15:23:57 -0800225
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 Chiue502e9d2017-03-27 11:00:50 -0400248}