blob: 8e6ad0a9cc0603c42ccd546b489274b83fad99a6 [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.Sets;
19import org.onlab.packet.EthType;
20import org.onlab.packet.IPv4;
21import org.onlab.packet.IPv6;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.TpPort;
24import org.onlab.packet.VlanId;
25import org.onlab.util.Tools;
26import org.onosproject.cfg.ComponentConfigService;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.mastership.MastershipService;
30import org.onosproject.net.AnnotationKeys;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.device.DeviceService;
35import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.flow.criteria.Criteria;
40import org.onosproject.net.flowobjective.DefaultFilteringObjective;
41import org.onosproject.net.flowobjective.DefaultForwardingObjective;
42import org.onosproject.net.flowobjective.FilteringObjective;
43import org.onosproject.net.flowobjective.FlowObjectiveService;
44import org.onosproject.net.flowobjective.ForwardingObjective;
45import org.onosproject.net.flowobjective.Objective;
46import org.onosproject.net.flowobjective.ObjectiveContext;
47import org.onosproject.net.flowobjective.ObjectiveError;
48import org.onosproject.net.meter.MeterId;
49import org.opencord.olt.internalapi.AccessDeviceFlowService;
50import org.opencord.olt.internalapi.AccessDeviceMeterService;
51import org.opencord.sadis.BandwidthProfileInformation;
52import org.opencord.sadis.BaseInformationService;
53import org.opencord.sadis.SadisService;
54import org.opencord.sadis.SubscriberAndDeviceInformation;
55import org.opencord.sadis.UniTagInformation;
56import org.osgi.service.component.ComponentContext;
57import org.osgi.service.component.annotations.Activate;
58import org.osgi.service.component.annotations.Component;
59import org.osgi.service.component.annotations.Deactivate;
60import org.osgi.service.component.annotations.Modified;
61import org.osgi.service.component.annotations.Reference;
62import org.osgi.service.component.annotations.ReferenceCardinality;
63import org.slf4j.Logger;
64
65import java.util.Dictionary;
66import java.util.Properties;
67import java.util.Set;
68import java.util.concurrent.CompletableFuture;
69
70import static com.google.common.base.Strings.isNullOrEmpty;
71import static org.onlab.util.Tools.get;
72import static org.opencord.olt.impl.OsgiPropertyConstants.*;
73import static org.slf4j.LoggerFactory.getLogger;
74
75/**
76 * Provisions flow rules on access devices.
77 */
78@Component(immediate = true, property = {
79 ENABLE_DHCP_ON_PROVISIONING + ":Boolean=" + ENABLE_DHCP_ON_PROVISIONING_DEFAULT,
80 ENABLE_DHCP_V4 + ":Boolean=" + ENABLE_DHCP_V4_DEFAULT,
81 ENABLE_DHCP_V6 + ":Boolean=" + ENABLE_DHCP_V6_DEFAULT,
82 ENABLE_IGMP_ON_PROVISIONING + ":Boolean=" + ENABLE_IGMP_ON_PROVISIONING_DEFAULT,
83 ENABLE_EAPOL + ":Boolean=" + ENABLE_EAPOL_DEFAULT,
84 DEFAULT_TP_ID + ":Integer=" + DEFAULT_TP_ID_DEFAULT
85})
86public class OltFlowService implements AccessDeviceFlowService {
87
88 private static final String APP_NAME = "org.opencord.olt";
89 private static final int NONE_TP_ID = -1;
90 private static final int NO_PCP = -1;
91 private static final Integer MAX_PRIORITY = 10000;
92 private static final Integer MIN_PRIORITY = 1000;
93 private static final int DEFAULT_TP_ID = 64;
94 private static final String INSTALLED = "installed";
95 private static final String REMOVED = "removed";
96 private static final String INSTALLATION = "installation";
97 private static final String REMOVAL = "removal";
98 private static final String V4 = "V4";
99 private static final String V6 = "V6";
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000100
101 private final Logger log = getLogger(getClass());
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY)
104 protected FlowObjectiveService flowObjectiveService;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY)
107 protected CoreService coreService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY)
110 protected MastershipService mastershipService;
111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY)
113 protected SadisService sadisService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY)
116 protected DeviceService deviceService;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY)
119 protected AccessDeviceMeterService oltMeterService;
120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY)
122 protected ComponentConfigService componentConfigService;
123
124 /**
125 * Create the DHCP Flow rules when a subscriber is provisioned.
126 **/
127 protected boolean enableDhcpOnProvisioning = ENABLE_DHCP_ON_PROVISIONING_DEFAULT;
128
129 /**
130 * Enable flows for DHCP v4.
131 **/
132 protected boolean enableDhcpV4 = ENABLE_DHCP_V4_DEFAULT;
133
134 /**
135 * Enable flows for DHCP v6.
136 **/
137 protected boolean enableDhcpV6 = ENABLE_DHCP_V6_DEFAULT;
138
139 /**
140 * Create IGMP Flow rules when a subscriber is provisioned.
141 **/
142 protected boolean enableIgmpOnProvisioning = ENABLE_IGMP_ON_PROVISIONING_DEFAULT;
143
144 /**
145 * Send EAPOL authentication trap flows before subscriber provisioning.
146 **/
147 protected boolean enableEapol = ENABLE_EAPOL_DEFAULT;
148
149 /**
150 * Default technology profile id that is used for authentication trap flows.
151 **/
152 protected int defaultTechProfileId = DEFAULT_TP_ID_DEFAULT;
153
154 protected ApplicationId appId;
155 protected BaseInformationService<BandwidthProfileInformation> bpService;
156 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
157 private Set<PortNumber> pendingAddEapol = Sets.newConcurrentHashSet();
158
159 @Activate
160 public void activate(ComponentContext context) {
161 bpService = sadisService.getBandwidthProfileService();
162 subsService = sadisService.getSubscriberInfoService();
163 componentConfigService.registerProperties(getClass());
164 appId = coreService.getAppId(APP_NAME);
165 log.info("Olt Flow Service started");
166 }
167
168
169 @Deactivate
170 public void deactivate(ComponentContext context) {
171 componentConfigService.unregisterProperties(getClass(), false);
172 log.info("Olt flow service stopped");
173 }
174
175 @Modified
176 public void modified(ComponentContext context) {
177
178 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
179
180 Boolean o = Tools.isPropertyEnabled(properties, "enableDhcpOnProvisioning");
181 if (o != null) {
182 enableDhcpOnProvisioning = o;
183 }
184
185 Boolean v4 = Tools.isPropertyEnabled(properties, "enableDhcpV4");
186 if (v4 != null) {
187 enableDhcpV4 = v4;
188 }
189
190 Boolean v6 = Tools.isPropertyEnabled(properties, "enableDhcpV6");
191 if (v6 != null) {
192 enableDhcpV6 = v6;
193 }
194
195 Boolean p = Tools.isPropertyEnabled(properties, "enableIgmpOnProvisioning");
196 if (p != null) {
197 enableIgmpOnProvisioning = p;
198 }
199
200 Boolean eap = Tools.isPropertyEnabled(properties, "enableEapol");
201 if (eap != null) {
202 enableEapol = eap;
203 }
204
205 String tpId = get(properties, "defaultTechProfileId");
206 defaultTechProfileId = isNullOrEmpty(tpId) ? DEFAULT_TP_ID : Integer.parseInt(tpId.trim());
207
208 }
209
210 @Override
211 public void processDhcpFilteringObjectives(DeviceId devId, PortNumber port,
212 MeterId upstreamMeterId,
213 UniTagInformation tagInformation,
214 boolean install,
215 boolean upstream) {
216 if (!enableDhcpOnProvisioning && !upstream) {
217 log.debug("Dhcp provisioning is disabled.");
218 return;
219 }
220
221 if (!mastershipService.isLocalMaster(devId)) {
222 return;
223 }
224
225 int techProfileId = tagInformation != null ? tagInformation.getTechnologyProfileId() : NONE_TP_ID;
226 VlanId cTag = tagInformation != null ? tagInformation.getPonCTag() : VlanId.NONE;
227 VlanId unitagMatch = tagInformation != null ? tagInformation.getUniTagMatch() : VlanId.ANY;
228
229 if (enableDhcpV4) {
230 int udpSrc = (upstream) ? 68 : 67;
231 int udpDst = (upstream) ? 67 : 68;
232
233 EthType ethType = EthType.EtherType.IPV4.ethType();
234 byte protocol = IPv4.PROTOCOL_UDP;
235
236 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
237 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
238 }
239
240 if (enableDhcpV6) {
241 int udpSrc = (upstream) ? 547 : 546;
242 int udpDst = (upstream) ? 546 : 547;
243
244 EthType ethType = EthType.EtherType.IPV6.ethType();
245 byte protocol = IPv6.PROTOCOL_UDP;
246
247 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
248 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
249 }
250 }
251
252 private void addDhcpFilteringObjectives(DeviceId devId, PortNumber port, int udpSrc, int udpDst,
253 EthType ethType, MeterId upstreamMeterId, int techProfileId, byte protocol,
254 VlanId cTag, VlanId unitagMatch, boolean install) {
255
256 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
257 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
258
259 if (upstreamMeterId != null) {
260 treatmentBuilder.meter(upstreamMeterId);
261 }
262
263 if (techProfileId != NONE_TP_ID) {
264 treatmentBuilder.writeMetadata(createTechProfValueForWm(unitagMatch, techProfileId), 0);
265 }
266
267 FilteringObjective.Builder dhcpUpstreamBuilder = (install ? builder.permit() : builder.deny())
268 .withKey(Criteria.matchInPort(port))
269 .addCondition(Criteria.matchEthType(ethType))
270 .addCondition(Criteria.matchIPProtocol(protocol))
271 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
272 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
273 .withMeta(treatmentBuilder
274 .setOutput(PortNumber.CONTROLLER).build())
275 .fromApp(appId)
276 .withPriority(MAX_PRIORITY);
277
278 if (!VlanId.NONE.equals(cTag)) {
279 dhcpUpstreamBuilder.addCondition(Criteria.matchVlanId(cTag));
280 }
281
282 FilteringObjective dhcpUpstream = dhcpUpstreamBuilder.add(new ObjectiveContext() {
283 @Override
284 public void onSuccess(Objective objective) {
285 log.info("DHCP {} filter for device {} on port {} {}.",
286 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
287 devId, port, (install) ? INSTALLED : REMOVED);
288 }
289
290 @Override
291 public void onError(Objective objective, ObjectiveError error) {
292 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
293 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
294 devId, port, (install) ? INSTALLATION : REMOVAL,
295 error);
296 }
297 });
298
299 flowObjectiveService.filter(devId, dhcpUpstream);
300
301 }
302
303 @Override
304 public void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
305 MeterId upstreamMeterId,
306 UniTagInformation tagInformation,
307 boolean install,
308 boolean upstream) {
309 if (!enableIgmpOnProvisioning && !upstream) {
310 log.debug("Igmp provisioning is disabled.");
311 return;
312 }
313
314 if (!mastershipService.isLocalMaster(devId)) {
315 return;
316 }
317
318 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
319 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
320 if (upstream) {
321
322 if (tagInformation.getTechnologyProfileId() != NONE_TP_ID) {
323 treatmentBuilder.writeMetadata(createTechProfValueForWm(null,
324 tagInformation.getTechnologyProfileId()), 0);
325 }
326
327
328 if (upstreamMeterId != null) {
329 treatmentBuilder.meter(upstreamMeterId);
330 }
331
332 if (!VlanId.NONE.equals(tagInformation.getPonCTag())) {
333 builder.addCondition(Criteria.matchVlanId(tagInformation.getPonCTag()));
334 }
335
336 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
337 builder.addCondition(Criteria.matchVlanPcp((byte) tagInformation.getUsPonCTagPriority()));
338 }
339 }
340
341 builder = install ? builder.permit() : builder.deny();
342
343 FilteringObjective igmp = builder
344 .withKey(Criteria.matchInPort(port))
345 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
346 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
347 .withMeta(treatmentBuilder
348 .setOutput(PortNumber.CONTROLLER).build())
349 .fromApp(appId)
350 .withPriority(MAX_PRIORITY)
351 .add(new ObjectiveContext() {
352 @Override
353 public void onSuccess(Objective objective) {
354 log.info("Igmp filter for {} on {} {}.",
355 devId, port, (install) ? INSTALLED : REMOVED);
356 }
357
358 @Override
359 public void onError(Objective objective, ObjectiveError error) {
360 log.info("Igmp filter for {} on {} failed {} because {}.",
361 devId, port, (install) ? INSTALLATION : REMOVAL,
362 error);
363 }
364 });
365
366 flowObjectiveService.filter(devId, igmp);
367 }
368
369 @Override
370 public void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
371 CompletableFuture<ObjectiveError> filterFuture,
372 VlanId vlanId, boolean install) {
373
374 if (!enableEapol) {
375 log.debug("Eapol filtering is disabled. Completing filteringFuture immediately for the device {}", devId);
376 if (filterFuture != null) {
377 filterFuture.complete(null);
378 }
379 return;
380 }
381
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000382 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
383 if (bpInfo == null) {
384 log.warn("Bandwidth profile {} is not found. Authentication flow"
385 + " will not be installed", bpId);
386 if (filterFuture != null) {
387 filterFuture.complete(ObjectiveError.BADPARAMS);
388 }
389 return;
390 }
391
392 if (install) {
393 boolean added = pendingAddEapol.add(portNumber);
394 if (!added) {
395 if (filterFuture != null) {
396 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
397 filterFuture.complete(null);
398 }
399 return;
400 }
401 log.info("portNumber added to pendingAddEapol map {}", portNumber);
402 }
403
404 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
405 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
406 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
407
408 // check if meter exists and create it only for an install
409 MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
410 if (meterId == null) {
411 if (install) {
412 meterId = oltMeterService.createMeter(devId, bpInfo, meterFuture);
413 treatmentBuilder.meter(meterId);
414 } else {
415 // this case should not happen as the request to remove an eapol
416 // flow should mean that the flow points to a meter that exists.
417 // Nevertheless we can still delete the flow as we only need the
418 // correct 'match' to do so.
419 log.warn("Unknown meter id for bp {}, still proceeding with "
420 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
421 meterFuture.complete(null);
422 }
423 } else {
424 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
425 treatmentBuilder.meter(meterId);
426 meterFuture.complete(null);
427 }
428
429 final MeterId mId = meterId;
430 meterFuture.thenAcceptAsync(result -> {
431 if (result == null) {
432 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
433 mId, bpId, devId, portNumber,
434 (install) ? "Installing" : "Removing");
435 int techProfileId = getDefaultTechProfileId(devId, portNumber);
436
437 //Authentication trap flow uses only tech profile id as write metadata value
438 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
439 .withKey(Criteria.matchInPort(portNumber))
440 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
441 .addCondition(Criteria.matchVlanId(vlanId))
442 .withMeta(treatmentBuilder
443 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
444 .setOutput(PortNumber.CONTROLLER).build())
445 .fromApp(appId)
446 .withPriority(MAX_PRIORITY)
447 .add(new ObjectiveContext() {
448 @Override
449 public void onSuccess(Objective objective) {
450 log.info("Eapol filter for {} on {} {} with meter {}.",
451 devId, portNumber, (install) ? INSTALLED : REMOVED, mId);
452 if (filterFuture != null) {
453 filterFuture.complete(null);
454 }
455 pendingAddEapol.remove(portNumber);
456 }
457
458 @Override
459 public void onError(Objective objective, ObjectiveError error) {
460 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
461 devId, portNumber, mId, (install) ? INSTALLATION : REMOVAL,
462 error);
463 if (filterFuture != null) {
464 filterFuture.complete(error);
465 }
466 pendingAddEapol.remove(portNumber);
467 }
468 });
469
470 flowObjectiveService.filter(devId, eapol);
471 } else {
472 log.warn("Meter installation error while sending eapol trap flow. " +
473 "Result {} and MeterId {}", result, mId);
474 }
475 });
476 }
477
478 /**
479 * Installs trap filtering objectives for particular traffic types on an
480 * NNI port.
481 *
482 * @param devId device ID
483 * @param port port number
484 * @param install true to install, false to remove
485 */
486 @Override
487 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
488 log.info("Sending flows for NNI port {} of the device {}", port, devId);
489 processLldpFilteringObjective(devId, port, install);
490 processDhcpFilteringObjectives(devId, port, null, null, install, false);
491 processIgmpFilteringObjectives(devId, port, null, null, install, false);
492 }
493
494
495 @Override
496 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
497 if (!mastershipService.isLocalMaster(devId)) {
498 return;
499 }
500 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
501
502 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
503 .withKey(Criteria.matchInPort(port))
504 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
505 .withMeta(DefaultTrafficTreatment.builder()
506 .setOutput(PortNumber.CONTROLLER).build())
507 .fromApp(appId)
508 .withPriority(MAX_PRIORITY)
509 .add(new ObjectiveContext() {
510 @Override
511 public void onSuccess(Objective objective) {
512 log.info("LLDP filter for device {} on port {} {}.",
513 devId, port, (install) ? INSTALLED : REMOVED);
514 }
515
516 @Override
517 public void onError(Objective objective, ObjectiveError error) {
518 log.info("LLDP filter for device {} on port {} failed {} because {}",
519 devId, port, (install) ? INSTALLATION : REMOVAL,
520 error);
521 }
522 });
523
524 flowObjectiveService.filter(devId, lldp);
525 }
526
527 @Override
528 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
529 PortNumber subscriberPort,
530 MeterId meterId,
531 UniTagInformation tagInfo,
532 boolean upstream) {
533
534 TrafficSelector selector = DefaultTrafficSelector.builder()
535 .matchVlanId(tagInfo.getPonSTag())
536 .matchInPort(upstream ? subscriberPort : uplinkPort)
537 .matchInnerVlanId(tagInfo.getPonCTag())
538 .build();
539
540 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
541 if (meterId != null) {
542 tBuilder.meter(meterId);
543 }
544
545 TrafficTreatment treatment = tBuilder
546 .setOutput(upstream ? uplinkPort : subscriberPort)
547 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
548 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
549 .build();
550
551 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
552 }
553
554 @Override
555 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
556 PortNumber subscriberPort,
557 MeterId upstreamMeterId,
558 UniTagInformation uniTagInformation) {
559
560 TrafficSelector selector = DefaultTrafficSelector.builder()
561 .matchInPort(subscriberPort)
562 .matchVlanId(uniTagInformation.getUniTagMatch())
563 .build();
564
Andrea Campanella327c5722020-01-30 11:34:13 +0100565 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
566 //if the subscriberVlan (cTag) is different than ANY it needs to set.
567 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
568 treatmentBuilder.pushVlan()
569 .setVlanId(uniTagInformation.getPonCTag());
570 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000571
572 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
573 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
574 }
575
576 treatmentBuilder.pushVlan()
577 .setVlanId(uniTagInformation.getPonSTag());
578
579 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
580 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
581 }
582
583 treatmentBuilder.setOutput(uplinkPort)
584 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
585 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
586
587 if (upstreamMeterId != null) {
588 treatmentBuilder.meter(upstreamMeterId);
589 }
590
591 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
592 }
593
594 @Override
595 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
596 PortNumber subscriberPort,
597 MeterId downstreamMeterId,
598 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100599
600 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000601 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
602 .matchVlanId(tagInformation.getPonSTag())
603 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100604 .matchInnerVlanId(tagInformation.getPonCTag());
605
606
607 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
608 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
609 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000610
611 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
612 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
613 }
614
615 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800616 !tagInformation.getConfiguredMacAddress().equals("") &&
617 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000618 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
619 }
620
621 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
622 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100623 .setOutput(subscriberPort);
624
Andrea Campanella327c5722020-01-30 11:34:13 +0100625 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
626 tagInformation.getTechnologyProfileId(),
627 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000628
629 // to remark inner vlan header
630 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
631 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
632 }
633
Andrea Campanella9a779292020-02-03 19:19:09 +0100634 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
635 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000636 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
637 }
638
639 if (downstreamMeterId != null) {
640 treatmentBuilder.meter(downstreamMeterId);
641 }
642
643 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
644 }
645
646 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
647 TrafficTreatment treatment,
648 Integer priority) {
649 return DefaultForwardingObjective.builder()
650 .withFlag(ForwardingObjective.Flag.VERSATILE)
651 .withPriority(priority)
652 .makePermanent()
653 .withSelector(selector)
654 .fromApp(appId)
655 .withTreatment(treatment);
656 }
657
658 /**
659 * Returns the write metadata value including tech profile reference and innerVlan.
660 * For param cVlan, null can be sent
661 *
662 * @param cVlan c (customer) tag of one subscriber
663 * @param techProfileId tech profile id of one subscriber
664 * @return the write metadata value including tech profile reference and innerVlan
665 */
666 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
667 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
668 return (long) techProfileId << 32;
669 }
670 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
671 }
672
673 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
674 if (bandwidthProfile == null) {
675 return null;
676 }
677 return bpService.get(bandwidthProfile);
678 }
679
680 /**
681 * It will be used to support AT&T use case (for EAPOL flows).
682 * If multiple services are found in uniServiceList, returns default tech profile id
683 * If one service is found, returns the found one
684 *
685 * @param devId
686 * @param portNumber
687 * @return the default technology profile id
688 */
689 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
690 Port port = deviceService.getPort(devId, portNumber);
691 if (port != null) {
692 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
693 if (info != null && info.uniTagList().size() == 1) {
694 return info.uniTagList().get(0).getTechnologyProfileId();
695 }
696 }
697 return defaultTechProfileId;
698 }
699
700 /**
701 * Write metadata instruction value (metadata) is 8 bytes.
702 * <p>
703 * MS 2 bytes: C Tag
704 * Next 2 bytes: Technology Profile Id
705 * Next 4 bytes: Port number (uni or nni)
706 */
707 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
708 if (techProfileId == NONE_TP_ID) {
709 techProfileId = DEFAULT_TP_ID;
710 }
711
712 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
713 }
714
715
716}