blob: af8ba4ac6dd7f1d9bfc7cac5d772eec635ab5bd5 [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
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000383 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
384 if (bpInfo == null) {
385 log.warn("Bandwidth profile {} is not found. Authentication flow"
386 + " will not be installed", bpId);
387 if (filterFuture != null) {
388 filterFuture.complete(ObjectiveError.BADPARAMS);
389 }
390 return;
391 }
392
393 if (install) {
394 boolean added = pendingAddEapol.add(portNumber);
395 if (!added) {
396 if (filterFuture != null) {
397 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
398 filterFuture.complete(null);
399 }
400 return;
401 }
402 log.info("portNumber added to pendingAddEapol map {}", portNumber);
403 }
404
405 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
406 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
407 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
408
409 // check if meter exists and create it only for an install
410 MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
411 if (meterId == null) {
412 if (install) {
413 meterId = oltMeterService.createMeter(devId, bpInfo, meterFuture);
414 treatmentBuilder.meter(meterId);
415 } else {
416 // this case should not happen as the request to remove an eapol
417 // flow should mean that the flow points to a meter that exists.
418 // Nevertheless we can still delete the flow as we only need the
419 // correct 'match' to do so.
420 log.warn("Unknown meter id for bp {}, still proceeding with "
421 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
422 meterFuture.complete(null);
423 }
424 } else {
425 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
426 treatmentBuilder.meter(meterId);
427 meterFuture.complete(null);
428 }
429
430 final MeterId mId = meterId;
431 meterFuture.thenAcceptAsync(result -> {
432 if (result == null) {
433 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
434 mId, bpId, devId, portNumber,
435 (install) ? "Installing" : "Removing");
436 int techProfileId = getDefaultTechProfileId(devId, portNumber);
437
438 //Authentication trap flow uses only tech profile id as write metadata value
439 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
440 .withKey(Criteria.matchInPort(portNumber))
441 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
442 .addCondition(Criteria.matchVlanId(vlanId))
443 .withMeta(treatmentBuilder
444 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
445 .setOutput(PortNumber.CONTROLLER).build())
446 .fromApp(appId)
447 .withPriority(MAX_PRIORITY)
448 .add(new ObjectiveContext() {
449 @Override
450 public void onSuccess(Objective objective) {
451 log.info("Eapol filter for {} on {} {} with meter {}.",
452 devId, portNumber, (install) ? INSTALLED : REMOVED, mId);
453 if (filterFuture != null) {
454 filterFuture.complete(null);
455 }
456 pendingAddEapol.remove(portNumber);
457 }
458
459 @Override
460 public void onError(Objective objective, ObjectiveError error) {
461 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
462 devId, portNumber, mId, (install) ? INSTALLATION : REMOVAL,
463 error);
464 if (filterFuture != null) {
465 filterFuture.complete(error);
466 }
467 pendingAddEapol.remove(portNumber);
468 }
469 });
470
471 flowObjectiveService.filter(devId, eapol);
472 } else {
473 log.warn("Meter installation error while sending eapol trap flow. " +
474 "Result {} and MeterId {}", result, mId);
475 }
476 });
477 }
478
479 /**
480 * Installs trap filtering objectives for particular traffic types on an
481 * NNI port.
482 *
483 * @param devId device ID
484 * @param port port number
485 * @param install true to install, false to remove
486 */
487 @Override
488 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
489 log.info("Sending flows for NNI port {} of the device {}", port, devId);
490 processLldpFilteringObjective(devId, port, install);
491 processDhcpFilteringObjectives(devId, port, null, null, install, false);
492 processIgmpFilteringObjectives(devId, port, null, null, install, false);
493 }
494
495
496 @Override
497 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
498 if (!mastershipService.isLocalMaster(devId)) {
499 return;
500 }
501 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
502
503 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
504 .withKey(Criteria.matchInPort(port))
505 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
506 .withMeta(DefaultTrafficTreatment.builder()
507 .setOutput(PortNumber.CONTROLLER).build())
508 .fromApp(appId)
509 .withPriority(MAX_PRIORITY)
510 .add(new ObjectiveContext() {
511 @Override
512 public void onSuccess(Objective objective) {
513 log.info("LLDP filter for device {} on port {} {}.",
514 devId, port, (install) ? INSTALLED : REMOVED);
515 }
516
517 @Override
518 public void onError(Objective objective, ObjectiveError error) {
519 log.info("LLDP filter for device {} on port {} failed {} because {}",
520 devId, port, (install) ? INSTALLATION : REMOVAL,
521 error);
522 }
523 });
524
525 flowObjectiveService.filter(devId, lldp);
526 }
527
528 @Override
529 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
530 PortNumber subscriberPort,
531 MeterId meterId,
532 UniTagInformation tagInfo,
533 boolean upstream) {
534
535 TrafficSelector selector = DefaultTrafficSelector.builder()
536 .matchVlanId(tagInfo.getPonSTag())
537 .matchInPort(upstream ? subscriberPort : uplinkPort)
538 .matchInnerVlanId(tagInfo.getPonCTag())
539 .build();
540
541 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
542 if (meterId != null) {
543 tBuilder.meter(meterId);
544 }
545
546 TrafficTreatment treatment = tBuilder
547 .setOutput(upstream ? uplinkPort : subscriberPort)
548 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
549 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
550 .build();
551
552 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
553 }
554
555 @Override
556 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
557 PortNumber subscriberPort,
558 MeterId upstreamMeterId,
559 UniTagInformation uniTagInformation) {
560
561 TrafficSelector selector = DefaultTrafficSelector.builder()
562 .matchInPort(subscriberPort)
563 .matchVlanId(uniTagInformation.getUniTagMatch())
564 .build();
565
Andrea Campanella327c5722020-01-30 11:34:13 +0100566 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
567 //if the subscriberVlan (cTag) is different than ANY it needs to set.
568 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
569 treatmentBuilder.pushVlan()
570 .setVlanId(uniTagInformation.getPonCTag());
571 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000572
573 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
574 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
575 }
576
577 treatmentBuilder.pushVlan()
578 .setVlanId(uniTagInformation.getPonSTag());
579
580 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
581 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
582 }
583
584 treatmentBuilder.setOutput(uplinkPort)
585 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
586 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
587
588 if (upstreamMeterId != null) {
589 treatmentBuilder.meter(upstreamMeterId);
590 }
591
592 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
593 }
594
595 @Override
596 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
597 PortNumber subscriberPort,
598 MeterId downstreamMeterId,
599 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100600
601 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000602 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
603 .matchVlanId(tagInformation.getPonSTag())
604 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100605 .matchInnerVlanId(tagInformation.getPonCTag());
606
607
608 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
609 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
610 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000611
612 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
613 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
614 }
615
616 if (tagInformation.getConfiguredMacAddress() != null &&
617 !NO_MAC.equals(tagInformation.getConfiguredMacAddress())) {
618 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}