blob: f259e6f079d7e17df72b13ce1f0aa3ca1bb43add [file] [log] [blame]
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001/*
2 * Copyright 2016-present Open Networking Foundation
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 */
16package org.opencord.olt.impl;
17
18import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.ListMultimap;
21import org.apache.commons.lang3.tuple.Pair;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.EthType;
25import org.onlab.packet.VlanId;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.cluster.RoleInfo;
28import org.onosproject.mastership.MastershipInfo;
29import org.onosproject.mastership.MastershipListener;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.MastershipRole;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.flow.criteria.Criterion;
36import org.onosproject.net.flow.criteria.EthTypeCriterion;
37import org.onosproject.net.flow.criteria.PortCriterion;
38import org.onosproject.net.flow.criteria.VlanIdCriterion;
39import org.onosproject.net.flow.instructions.Instruction;
40import org.onosproject.net.flow.instructions.Instructions;
41import org.onosproject.net.flow.instructions.L2ModificationInstruction;
42import org.onosproject.net.flowobjective.FilteringObjQueueKey;
43import org.onosproject.net.flowobjective.FilteringObjective;
44import org.onosproject.net.flowobjective.ForwardingObjQueueKey;
45import org.onosproject.net.flowobjective.ForwardingObjective;
46import org.onosproject.net.flowobjective.NextObjQueueKey;
47import org.onosproject.net.flowobjective.NextObjective;
48import org.onosproject.net.flowobjective.Objective;
49import org.onosproject.net.meter.MeterId;
50import org.onosproject.net.meter.MeterKey;
Andrea Campanella3ce4d282020-06-09 13:46:58 +020051import org.opencord.olt.internalapi.DeviceBandwidthProfile;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000052import org.opencord.sadis.BandwidthProfileInformation;
53import org.opencord.sadis.UniTagInformation;
54
55import java.util.Collection;
56import java.util.List;
57import java.util.Map;
58import java.util.Optional;
59import java.util.Set;
60import java.util.concurrent.CompletableFuture;
61
62public class OltFlowTest extends TestBase {
63 private OltFlowService oltFlowService;
64
65 PortNumber uniPortNumber = PortNumber.portNumber(1);
66 PortNumber nniPortNumber = PortNumber.portNumber(65535);
67
68 UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder();
69 UniTagInformation uniTagInfo = tagInfoBuilder.setUniTagMatch(VlanId.vlanId((short) 35))
70 .setPonCTag(VlanId.vlanId((short) 33))
71 .setPonSTag(VlanId.vlanId((short) 7))
72 .setDsPonCTagPriority(0)
73 .setUsPonSTagPriority(0)
74 .setTechnologyProfileId(64)
75 .setDownstreamBandwidthProfile(dsBpId)
76 .setUpstreamBandwidthProfile(usBpId)
77 .build();
78
79 @Before
80 public void setUp() {
81 oltFlowService = new OltFlowService();
82 oltFlowService.oltMeterService = new MockOltMeterService();
83 oltFlowService.flowObjectiveService = new MockOltFlowObjectiveService();
84 oltFlowService.mastershipService = new MockMastershipService();
85 oltFlowService.sadisService = new MockSadisService();
86 oltFlowService.bpService = oltFlowService.sadisService.getBandwidthProfileService();
87 oltFlowService.appId = appId;
88 }
89
90 @Test
91 public void testDhcpFiltering() {
92 oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo,
93 true, true);
94 oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo,
95 false, true);
96 oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, dsMeterId, uniTagInfo,
97 true, false);
98 oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo,
99 false, false);
100 }
101
102 @Test
103 public void testIgmpFiltering() {
104 oltFlowService.processIgmpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo,
105 true, true);
106 oltFlowService.processIgmpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo,
107 false, true);
108 }
109
110 @Test
111 public void testEapolFiltering() {
112 addBandwidthProfile(uniTagInfo.getUpstreamBandwidthProfile());
113 oltFlowService.enableEapol = true;
114
115 //will install
116 oltFlowService.processEapolFilteringObjectives(DEVICE_ID_1, uniPortNumber,
117 uniTagInfo.getUpstreamBandwidthProfile(), new CompletableFuture<>(),
118 uniTagInfo.getUniTagMatch(), true);
119
120 //bp profile doesn't exist
121 oltFlowService.processEapolFilteringObjectives(DEVICE_ID_1, uniPortNumber,
122 uniTagInfo.getDownstreamBandwidthProfile(), new CompletableFuture<>(),
123 uniTagInfo.getUniTagMatch(), true);
124 }
125
126 @Test
127 public void testLldpFiltering() {
128 oltFlowService.processLldpFilteringObjective(DEVICE_ID_1, nniPortNumber, true);
129 oltFlowService.processLldpFilteringObjective(DEVICE_ID_1, nniPortNumber, false);
130 }
131
132 @Test
133 public void testNniFiltering() {
134 oltFlowService.enableDhcpOnProvisioning = true;
135 oltFlowService.enableIgmpOnProvisioning = true;
136 oltFlowService.processNniFilteringObjectives(DEVICE_ID_1, nniPortNumber, true);
137 oltFlowService.processNniFilteringObjectives(DEVICE_ID_1, nniPortNumber, false);
138 }
139
140 @Test
141 public void testUpBuilder() {
142 ForwardingObjective objective =
143 oltFlowService.createUpBuilder(nniPortNumber, uniPortNumber, usMeterId, uniTagInfo).add();
144 checkObjective(objective, true);
145 }
146
147 @Test
148 public void testDownBuilder() {
149 ForwardingObjective objective =
150 oltFlowService.createDownBuilder(nniPortNumber, uniPortNumber, dsMeterId, uniTagInfo).remove();
151 checkObjective(objective, false);
152 }
153
154 private void checkObjective(ForwardingObjective fwd, boolean upstream) {
155 TrafficTreatment treatment = fwd.treatment();
156
157 //check instructions
158 Set<Instructions.MeterInstruction> meters = treatment.meters();
159 assert !meters.isEmpty();
160
161 Instructions.MetadataInstruction writeMetadata = treatment.writeMetadata();
162 assert writeMetadata != null;
163
164 List<Instruction> immediateInstructions = treatment.immediate();
165 Optional<Instruction> vlanInstruction = immediateInstructions.stream()
166 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
167 .filter(i -> ((L2ModificationInstruction) i).subtype() ==
168 L2ModificationInstruction.L2SubType.VLAN_PUSH ||
169 ((L2ModificationInstruction) i).subtype() ==
170 L2ModificationInstruction.L2SubType.VLAN_POP)
171 .findAny();
172
173 assert vlanInstruction.isPresent();
174
175 //check match criteria
176 TrafficSelector selector = fwd.selector();
177 assert selector.getCriterion(Criterion.Type.IN_PORT) != null;
178 assert selector.getCriterion(Criterion.Type.VLAN_VID) != null;
179
180 if (!upstream) {
181 assert selector.getCriterion(Criterion.Type.METADATA) != null;
182 }
183 }
184
185 private class MockOltMeterService implements org.opencord.olt.internalapi.AccessDeviceMeterService {
186 @Override
187 public ImmutableMap<String, Collection<MeterKey>> getBpMeterMappings() {
188 return null;
189 }
190
191 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000192 public MeterId getMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfile) {
193 return null;
194 }
195
196
197 @Override
198 public ImmutableSet<MeterKey> getProgMeters() {
199 return null;
200 }
201
202 @Override
203 public MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo,
204 CompletableFuture<Object> meterFuture) {
205 return usMeterId;
206 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800207
208 @Override
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200209 public void addToPendingMeters(DeviceBandwidthProfile deviceBandwidthProfile) {
210
211 }
212
213 @Override
214 public void removeFromPendingMeters(DeviceBandwidthProfile deviceBandwidthProfile) {
215 }
216
217 @Override
218 public boolean isMeterPending(DeviceBandwidthProfile deviceBandwidthProfile) {
219 return false;
220 }
221
222 @Override
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800223 public void clearMeters(DeviceId deviceId) {
224 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000225 }
226
227 private class MockOltFlowObjectiveService implements org.onosproject.net.flowobjective.FlowObjectiveService {
228 @Override
229 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
230
231 EthTypeCriterion ethType = (EthTypeCriterion)
232 filterForCriterion(filteringObjective.conditions(), Criterion.Type.ETH_TYPE);
233
234 Instructions.MeterInstruction meter = filteringObjective.meta().metered();
235 Instruction writeMetadata = filteringObjective.meta().writeMetadata();
236 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion)
237 filterForCriterion(filteringObjective.conditions(), Criterion.Type.VLAN_VID);
238 PortCriterion portCriterion = (PortCriterion) filteringObjective.key();
239
240
241 if (ethType.ethType().equals(EthType.EtherType.LLDP.ethType()) ||
242 portCriterion.port().equals(nniPortNumber)) {
243 assert meter == null;
244 assert writeMetadata == null;
245 assert vlanIdCriterion == null;
246 } else {
247 assert meter.meterId().equals(usMeterId) || meter.meterId().equals(dsMeterId);
248 assert writeMetadata != null;
249 assert vlanIdCriterion.vlanId() == uniTagInfo.getPonCTag();
250 }
251
252 }
253
254 @Override
255 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
256
257 }
258
259 @Override
260 public void next(DeviceId deviceId, NextObjective nextObjective) {
261
262 }
263
264 @Override
265 public int allocateNextId() {
266 return 0;
267 }
268
269 @Override
270 public void initPolicy(String s) {
271
272 }
273
274 @Override
275 public void apply(DeviceId deviceId, Objective objective) {
276
277 }
278
279 @Override
280 public Map<Pair<Integer, DeviceId>, List<String>> getNextMappingsChain() {
281 return null;
282 }
283
284 @Override
285 public List<String> getNextMappings() {
286 return null;
287 }
288
289 @Override
290 public List<String> getPendingFlowObjectives() {
291 return null;
292 }
293
294 @Override
295 public ListMultimap<FilteringObjQueueKey, Objective> getFilteringObjQueue() {
296 return null;
297 }
298
299 @Override
300 public ListMultimap<ForwardingObjQueueKey, Objective> getForwardingObjQueue() {
301 return null;
302 }
303
304 @Override
305 public ListMultimap<NextObjQueueKey, Objective> getNextObjQueue() {
306 return null;
307 }
308
309 @Override
310 public Map<FilteringObjQueueKey, Objective> getFilteringObjQueueHead() {
311 return null;
312 }
313
314 @Override
315 public Map<ForwardingObjQueueKey, Objective> getForwardingObjQueueHead() {
316 return null;
317 }
318
319 @Override
320 public Map<NextObjQueueKey, Objective> getNextObjQueueHead() {
321 return null;
322 }
323
324 @Override
325 public void clearQueue() {
326
327 }
328
329 private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) {
330 return criteria.stream()
331 .filter(c -> c.type().equals(type))
332 .limit(1)
333 .findFirst().orElse(null);
334 }
335 }
336
337 private class MockMastershipService implements org.onosproject.mastership.MastershipService {
338 @Override
339 public MastershipRole getLocalRole(DeviceId deviceId) {
340 return null;
341 }
342
343 @Override
344 public boolean isLocalMaster(DeviceId deviceId) {
345 return true;
346 }
347
348 @Override
349 public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) {
350 return null;
351 }
352
353 @Override
354 public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
355 return null;
356 }
357
358 @Override
359 public NodeId getMasterFor(DeviceId deviceId) {
360 return null;
361 }
362
363 @Override
364 public RoleInfo getNodesFor(DeviceId deviceId) {
365 return null;
366 }
367
368 @Override
369 public MastershipInfo getMastershipFor(DeviceId deviceId) {
370 return null;
371 }
372
373 @Override
374 public Set<DeviceId> getDevicesOf(NodeId nodeId) {
375 return null;
376 }
377
378 @Override
379 public void addListener(MastershipListener mastershipListener) {
380
381 }
382
383 @Override
384 public void removeListener(MastershipListener mastershipListener) {
385
386 }
387 }
388}