blob: 09746ccabcec21b81f6dbc0d71024f647a57d728 [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;
Matteo Scandolo3a037a32020-04-01 12:17:50 -070031import org.onosproject.net.ConnectPoint;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000032import org.onosproject.net.DeviceId;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criteria;
41import org.onosproject.net.flowobjective.DefaultFilteringObjective;
42import org.onosproject.net.flowobjective.DefaultForwardingObjective;
43import org.onosproject.net.flowobjective.FilteringObjective;
44import org.onosproject.net.flowobjective.FlowObjectiveService;
45import org.onosproject.net.flowobjective.ForwardingObjective;
46import org.onosproject.net.flowobjective.Objective;
47import org.onosproject.net.flowobjective.ObjectiveContext;
48import org.onosproject.net.flowobjective.ObjectiveError;
49import org.onosproject.net.meter.MeterId;
50import org.opencord.olt.internalapi.AccessDeviceFlowService;
51import org.opencord.olt.internalapi.AccessDeviceMeterService;
52import org.opencord.sadis.BandwidthProfileInformation;
53import org.opencord.sadis.BaseInformationService;
54import org.opencord.sadis.SadisService;
55import org.opencord.sadis.SubscriberAndDeviceInformation;
56import org.opencord.sadis.UniTagInformation;
57import org.osgi.service.component.ComponentContext;
58import org.osgi.service.component.annotations.Activate;
59import org.osgi.service.component.annotations.Component;
60import org.osgi.service.component.annotations.Deactivate;
61import org.osgi.service.component.annotations.Modified;
62import org.osgi.service.component.annotations.Reference;
63import org.osgi.service.component.annotations.ReferenceCardinality;
64import org.slf4j.Logger;
65
66import java.util.Dictionary;
67import java.util.Properties;
68import java.util.Set;
69import java.util.concurrent.CompletableFuture;
70
71import static com.google.common.base.Strings.isNullOrEmpty;
72import static org.onlab.util.Tools.get;
73import static org.opencord.olt.impl.OsgiPropertyConstants.*;
74import static org.slf4j.LoggerFactory.getLogger;
75
76/**
77 * Provisions flow rules on access devices.
78 */
79@Component(immediate = true, property = {
80 ENABLE_DHCP_ON_PROVISIONING + ":Boolean=" + ENABLE_DHCP_ON_PROVISIONING_DEFAULT,
81 ENABLE_DHCP_V4 + ":Boolean=" + ENABLE_DHCP_V4_DEFAULT,
82 ENABLE_DHCP_V6 + ":Boolean=" + ENABLE_DHCP_V6_DEFAULT,
83 ENABLE_IGMP_ON_PROVISIONING + ":Boolean=" + ENABLE_IGMP_ON_PROVISIONING_DEFAULT,
84 ENABLE_EAPOL + ":Boolean=" + ENABLE_EAPOL_DEFAULT,
85 DEFAULT_TP_ID + ":Integer=" + DEFAULT_TP_ID_DEFAULT
86})
87public class OltFlowService implements AccessDeviceFlowService {
88
89 private static final String APP_NAME = "org.opencord.olt";
90 private static final int NONE_TP_ID = -1;
91 private static final int NO_PCP = -1;
92 private static final Integer MAX_PRIORITY = 10000;
93 private static final Integer MIN_PRIORITY = 1000;
94 private static final int DEFAULT_TP_ID = 64;
95 private static final String INSTALLED = "installed";
96 private static final String REMOVED = "removed";
97 private static final String INSTALLATION = "installation";
98 private static final String REMOVAL = "removal";
99 private static final String V4 = "V4";
100 private static final String V6 = "V6";
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000101
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;
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700158 private Set<ConnectPoint> pendingAddEapol = Sets.newConcurrentHashSet();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000159
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
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700393 ConnectPoint cp = new ConnectPoint(devId, portNumber);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000394 if (install) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700395 boolean added = pendingAddEapol.add(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000396 if (!added) {
397 if (filterFuture != null) {
398 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
399 filterFuture.complete(null);
400 }
401 return;
402 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700403 log.info("connectPoint added to pendingAddEapol map {}", cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000404 }
405
406 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
407 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
408 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
409
410 // check if meter exists and create it only for an install
411 MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
412 if (meterId == null) {
413 if (install) {
414 meterId = oltMeterService.createMeter(devId, bpInfo, meterFuture);
415 treatmentBuilder.meter(meterId);
416 } else {
417 // this case should not happen as the request to remove an eapol
418 // flow should mean that the flow points to a meter that exists.
419 // Nevertheless we can still delete the flow as we only need the
420 // correct 'match' to do so.
421 log.warn("Unknown meter id for bp {}, still proceeding with "
422 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
423 meterFuture.complete(null);
424 }
425 } else {
426 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
427 treatmentBuilder.meter(meterId);
428 meterFuture.complete(null);
429 }
430
431 final MeterId mId = meterId;
432 meterFuture.thenAcceptAsync(result -> {
433 if (result == null) {
434 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
435 mId, bpId, devId, portNumber,
436 (install) ? "Installing" : "Removing");
437 int techProfileId = getDefaultTechProfileId(devId, portNumber);
438
439 //Authentication trap flow uses only tech profile id as write metadata value
440 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
441 .withKey(Criteria.matchInPort(portNumber))
442 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
443 .addCondition(Criteria.matchVlanId(vlanId))
444 .withMeta(treatmentBuilder
445 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
446 .setOutput(PortNumber.CONTROLLER).build())
447 .fromApp(appId)
448 .withPriority(MAX_PRIORITY)
449 .add(new ObjectiveContext() {
450 @Override
451 public void onSuccess(Objective objective) {
452 log.info("Eapol filter for {} on {} {} with meter {}.",
453 devId, portNumber, (install) ? INSTALLED : REMOVED, mId);
454 if (filterFuture != null) {
455 filterFuture.complete(null);
456 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700457 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000458 }
459
460 @Override
461 public void onError(Objective objective, ObjectiveError error) {
462 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
463 devId, portNumber, mId, (install) ? INSTALLATION : REMOVAL,
464 error);
465 if (filterFuture != null) {
466 filterFuture.complete(error);
467 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700468 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000469 }
470 });
471
472 flowObjectiveService.filter(devId, eapol);
473 } else {
474 log.warn("Meter installation error while sending eapol trap flow. " +
475 "Result {} and MeterId {}", result, mId);
476 }
477 });
478 }
479
480 /**
481 * Installs trap filtering objectives for particular traffic types on an
482 * NNI port.
483 *
484 * @param devId device ID
485 * @param port port number
486 * @param install true to install, false to remove
487 */
488 @Override
489 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
490 log.info("Sending flows for NNI port {} of the device {}", port, devId);
491 processLldpFilteringObjective(devId, port, install);
492 processDhcpFilteringObjectives(devId, port, null, null, install, false);
493 processIgmpFilteringObjectives(devId, port, null, null, install, false);
494 }
495
496
497 @Override
498 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
499 if (!mastershipService.isLocalMaster(devId)) {
500 return;
501 }
502 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
503
504 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
505 .withKey(Criteria.matchInPort(port))
506 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
507 .withMeta(DefaultTrafficTreatment.builder()
508 .setOutput(PortNumber.CONTROLLER).build())
509 .fromApp(appId)
510 .withPriority(MAX_PRIORITY)
511 .add(new ObjectiveContext() {
512 @Override
513 public void onSuccess(Objective objective) {
514 log.info("LLDP filter for device {} on port {} {}.",
515 devId, port, (install) ? INSTALLED : REMOVED);
516 }
517
518 @Override
519 public void onError(Objective objective, ObjectiveError error) {
520 log.info("LLDP filter for device {} on port {} failed {} because {}",
521 devId, port, (install) ? INSTALLATION : REMOVAL,
522 error);
523 }
524 });
525
526 flowObjectiveService.filter(devId, lldp);
527 }
528
529 @Override
530 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
531 PortNumber subscriberPort,
532 MeterId meterId,
533 UniTagInformation tagInfo,
534 boolean upstream) {
535
536 TrafficSelector selector = DefaultTrafficSelector.builder()
537 .matchVlanId(tagInfo.getPonSTag())
538 .matchInPort(upstream ? subscriberPort : uplinkPort)
539 .matchInnerVlanId(tagInfo.getPonCTag())
540 .build();
541
542 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
543 if (meterId != null) {
544 tBuilder.meter(meterId);
545 }
546
547 TrafficTreatment treatment = tBuilder
548 .setOutput(upstream ? uplinkPort : subscriberPort)
549 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
550 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
551 .build();
552
553 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
554 }
555
556 @Override
557 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
558 PortNumber subscriberPort,
559 MeterId upstreamMeterId,
560 UniTagInformation uniTagInformation) {
561
562 TrafficSelector selector = DefaultTrafficSelector.builder()
563 .matchInPort(subscriberPort)
564 .matchVlanId(uniTagInformation.getUniTagMatch())
565 .build();
566
Andrea Campanella327c5722020-01-30 11:34:13 +0100567 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
568 //if the subscriberVlan (cTag) is different than ANY it needs to set.
569 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
570 treatmentBuilder.pushVlan()
571 .setVlanId(uniTagInformation.getPonCTag());
572 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000573
574 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
575 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
576 }
577
578 treatmentBuilder.pushVlan()
579 .setVlanId(uniTagInformation.getPonSTag());
580
581 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
582 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
583 }
584
585 treatmentBuilder.setOutput(uplinkPort)
586 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
587 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
588
589 if (upstreamMeterId != null) {
590 treatmentBuilder.meter(upstreamMeterId);
591 }
592
593 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
594 }
595
596 @Override
597 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
598 PortNumber subscriberPort,
599 MeterId downstreamMeterId,
600 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100601
602 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000603 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
604 .matchVlanId(tagInformation.getPonSTag())
605 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100606 .matchInnerVlanId(tagInformation.getPonCTag());
607
608
609 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
610 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
611 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000612
613 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
614 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
615 }
616
617 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800618 !tagInformation.getConfiguredMacAddress().equals("") &&
619 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000620 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
621 }
622
623 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
624 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100625 .setOutput(subscriberPort);
626
Andrea Campanella327c5722020-01-30 11:34:13 +0100627 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
628 tagInformation.getTechnologyProfileId(),
629 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000630
631 // to remark inner vlan header
632 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
633 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
634 }
635
Andrea Campanella9a779292020-02-03 19:19:09 +0100636 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
637 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000638 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
639 }
640
641 if (downstreamMeterId != null) {
642 treatmentBuilder.meter(downstreamMeterId);
643 }
644
645 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
646 }
647
648 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
649 TrafficTreatment treatment,
650 Integer priority) {
651 return DefaultForwardingObjective.builder()
652 .withFlag(ForwardingObjective.Flag.VERSATILE)
653 .withPriority(priority)
654 .makePermanent()
655 .withSelector(selector)
656 .fromApp(appId)
657 .withTreatment(treatment);
658 }
659
660 /**
661 * Returns the write metadata value including tech profile reference and innerVlan.
662 * For param cVlan, null can be sent
663 *
664 * @param cVlan c (customer) tag of one subscriber
665 * @param techProfileId tech profile id of one subscriber
666 * @return the write metadata value including tech profile reference and innerVlan
667 */
668 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
669 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
670 return (long) techProfileId << 32;
671 }
672 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
673 }
674
675 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
676 if (bandwidthProfile == null) {
677 return null;
678 }
679 return bpService.get(bandwidthProfile);
680 }
681
682 /**
683 * It will be used to support AT&T use case (for EAPOL flows).
684 * If multiple services are found in uniServiceList, returns default tech profile id
685 * If one service is found, returns the found one
686 *
687 * @param devId
688 * @param portNumber
689 * @return the default technology profile id
690 */
691 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
692 Port port = deviceService.getPort(devId, portNumber);
693 if (port != null) {
694 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
695 if (info != null && info.uniTagList().size() == 1) {
696 return info.uniTagList().get(0).getTechnologyProfileId();
697 }
698 }
699 return defaultTechProfileId;
700 }
701
702 /**
703 * Write metadata instruction value (metadata) is 8 bytes.
704 * <p>
705 * MS 2 bytes: C Tag
706 * Next 2 bytes: Technology Profile Id
707 * Next 4 bytes: Port number (uni or nni)
708 */
709 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
710 if (techProfileId == NONE_TP_ID) {
711 techProfileId = DEFAULT_TP_ID;
712 }
713
714 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
715 }
716
717
718}