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