blob: 3d41a5b6ae3bdf8b01e26f3eeaf0e00c5f40ffbc [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
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000222 int techProfileId = tagInformation != null ? tagInformation.getTechnologyProfileId() : NONE_TP_ID;
223 VlanId cTag = tagInformation != null ? tagInformation.getPonCTag() : VlanId.NONE;
224 VlanId unitagMatch = tagInformation != null ? tagInformation.getUniTagMatch() : VlanId.ANY;
225
226 if (enableDhcpV4) {
227 int udpSrc = (upstream) ? 68 : 67;
228 int udpDst = (upstream) ? 67 : 68;
229
230 EthType ethType = EthType.EtherType.IPV4.ethType();
231 byte protocol = IPv4.PROTOCOL_UDP;
232
233 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
234 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
235 }
236
237 if (enableDhcpV6) {
238 int udpSrc = (upstream) ? 547 : 546;
239 int udpDst = (upstream) ? 546 : 547;
240
241 EthType ethType = EthType.EtherType.IPV6.ethType();
242 byte protocol = IPv6.PROTOCOL_UDP;
243
244 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
245 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
246 }
247 }
248
249 private void addDhcpFilteringObjectives(DeviceId devId, PortNumber port, int udpSrc, int udpDst,
250 EthType ethType, MeterId upstreamMeterId, int techProfileId, byte protocol,
251 VlanId cTag, VlanId unitagMatch, boolean install) {
252
253 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
254 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
255
256 if (upstreamMeterId != null) {
257 treatmentBuilder.meter(upstreamMeterId);
258 }
259
260 if (techProfileId != NONE_TP_ID) {
261 treatmentBuilder.writeMetadata(createTechProfValueForWm(unitagMatch, techProfileId), 0);
262 }
263
264 FilteringObjective.Builder dhcpUpstreamBuilder = (install ? builder.permit() : builder.deny())
265 .withKey(Criteria.matchInPort(port))
266 .addCondition(Criteria.matchEthType(ethType))
267 .addCondition(Criteria.matchIPProtocol(protocol))
268 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
269 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
270 .withMeta(treatmentBuilder
271 .setOutput(PortNumber.CONTROLLER).build())
272 .fromApp(appId)
273 .withPriority(MAX_PRIORITY);
274
275 if (!VlanId.NONE.equals(cTag)) {
276 dhcpUpstreamBuilder.addCondition(Criteria.matchVlanId(cTag));
277 }
278
279 FilteringObjective dhcpUpstream = dhcpUpstreamBuilder.add(new ObjectiveContext() {
280 @Override
281 public void onSuccess(Objective objective) {
282 log.info("DHCP {} filter for device {} on port {} {}.",
283 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
284 devId, port, (install) ? INSTALLED : REMOVED);
285 }
286
287 @Override
288 public void onError(Objective objective, ObjectiveError error) {
289 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
290 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
291 devId, port, (install) ? INSTALLATION : REMOVAL,
292 error);
293 }
294 });
295
296 flowObjectiveService.filter(devId, dhcpUpstream);
297
298 }
299
300 @Override
301 public void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
302 MeterId upstreamMeterId,
303 UniTagInformation tagInformation,
304 boolean install,
305 boolean upstream) {
306 if (!enableIgmpOnProvisioning && !upstream) {
307 log.debug("Igmp provisioning is disabled.");
308 return;
309 }
310
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000311 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
312 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
313 if (upstream) {
314
315 if (tagInformation.getTechnologyProfileId() != NONE_TP_ID) {
316 treatmentBuilder.writeMetadata(createTechProfValueForWm(null,
317 tagInformation.getTechnologyProfileId()), 0);
318 }
319
320
321 if (upstreamMeterId != null) {
322 treatmentBuilder.meter(upstreamMeterId);
323 }
324
325 if (!VlanId.NONE.equals(tagInformation.getPonCTag())) {
326 builder.addCondition(Criteria.matchVlanId(tagInformation.getPonCTag()));
327 }
328
329 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
330 builder.addCondition(Criteria.matchVlanPcp((byte) tagInformation.getUsPonCTagPriority()));
331 }
332 }
333
334 builder = install ? builder.permit() : builder.deny();
335
336 FilteringObjective igmp = builder
337 .withKey(Criteria.matchInPort(port))
338 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
339 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
340 .withMeta(treatmentBuilder
341 .setOutput(PortNumber.CONTROLLER).build())
342 .fromApp(appId)
343 .withPriority(MAX_PRIORITY)
344 .add(new ObjectiveContext() {
345 @Override
346 public void onSuccess(Objective objective) {
347 log.info("Igmp filter for {} on {} {}.",
348 devId, port, (install) ? INSTALLED : REMOVED);
349 }
350
351 @Override
352 public void onError(Objective objective, ObjectiveError error) {
353 log.info("Igmp filter for {} on {} failed {} because {}.",
354 devId, port, (install) ? INSTALLATION : REMOVAL,
355 error);
356 }
357 });
358
359 flowObjectiveService.filter(devId, igmp);
360 }
361
362 @Override
363 public void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
364 CompletableFuture<ObjectiveError> filterFuture,
365 VlanId vlanId, boolean install) {
366
367 if (!enableEapol) {
368 log.debug("Eapol filtering is disabled. Completing filteringFuture immediately for the device {}", devId);
369 if (filterFuture != null) {
370 filterFuture.complete(null);
371 }
372 return;
373 }
374
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000375 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
376 if (bpInfo == null) {
377 log.warn("Bandwidth profile {} is not found. Authentication flow"
378 + " will not be installed", bpId);
379 if (filterFuture != null) {
380 filterFuture.complete(ObjectiveError.BADPARAMS);
381 }
382 return;
383 }
384
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700385 ConnectPoint cp = new ConnectPoint(devId, portNumber);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000386 if (install) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700387 boolean added = pendingAddEapol.add(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000388 if (!added) {
389 if (filterFuture != null) {
390 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
391 filterFuture.complete(null);
392 }
393 return;
394 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700395 log.info("connectPoint added to pendingAddEapol map {}", cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000396 }
397
398 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
399 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
400 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
401
402 // check if meter exists and create it only for an install
403 MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
404 if (meterId == null) {
405 if (install) {
406 meterId = oltMeterService.createMeter(devId, bpInfo, meterFuture);
407 treatmentBuilder.meter(meterId);
408 } else {
409 // this case should not happen as the request to remove an eapol
410 // flow should mean that the flow points to a meter that exists.
411 // Nevertheless we can still delete the flow as we only need the
412 // correct 'match' to do so.
413 log.warn("Unknown meter id for bp {}, still proceeding with "
414 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
415 meterFuture.complete(null);
416 }
417 } else {
418 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
419 treatmentBuilder.meter(meterId);
420 meterFuture.complete(null);
421 }
422
423 final MeterId mId = meterId;
424 meterFuture.thenAcceptAsync(result -> {
425 if (result == null) {
426 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
427 mId, bpId, devId, portNumber,
428 (install) ? "Installing" : "Removing");
429 int techProfileId = getDefaultTechProfileId(devId, portNumber);
430
431 //Authentication trap flow uses only tech profile id as write metadata value
432 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
433 .withKey(Criteria.matchInPort(portNumber))
434 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
435 .addCondition(Criteria.matchVlanId(vlanId))
436 .withMeta(treatmentBuilder
437 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
438 .setOutput(PortNumber.CONTROLLER).build())
439 .fromApp(appId)
440 .withPriority(MAX_PRIORITY)
441 .add(new ObjectiveContext() {
442 @Override
443 public void onSuccess(Objective objective) {
444 log.info("Eapol filter for {} on {} {} with meter {}.",
445 devId, portNumber, (install) ? INSTALLED : REMOVED, mId);
446 if (filterFuture != null) {
447 filterFuture.complete(null);
448 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700449 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000450 }
451
452 @Override
453 public void onError(Objective objective, ObjectiveError error) {
454 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
455 devId, portNumber, mId, (install) ? INSTALLATION : REMOVAL,
456 error);
457 if (filterFuture != null) {
458 filterFuture.complete(error);
459 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700460 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000461 }
462 });
463
464 flowObjectiveService.filter(devId, eapol);
465 } else {
466 log.warn("Meter installation error while sending eapol trap flow. " +
467 "Result {} and MeterId {}", result, mId);
468 }
469 });
470 }
471
472 /**
473 * Installs trap filtering objectives for particular traffic types on an
474 * NNI port.
475 *
476 * @param devId device ID
477 * @param port port number
478 * @param install true to install, false to remove
479 */
480 @Override
481 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
482 log.info("Sending flows for NNI port {} of the device {}", port, devId);
483 processLldpFilteringObjective(devId, port, install);
484 processDhcpFilteringObjectives(devId, port, null, null, install, false);
485 processIgmpFilteringObjectives(devId, port, null, null, install, false);
486 }
487
488
489 @Override
490 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000491 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
492
493 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
494 .withKey(Criteria.matchInPort(port))
495 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
496 .withMeta(DefaultTrafficTreatment.builder()
497 .setOutput(PortNumber.CONTROLLER).build())
498 .fromApp(appId)
499 .withPriority(MAX_PRIORITY)
500 .add(new ObjectiveContext() {
501 @Override
502 public void onSuccess(Objective objective) {
503 log.info("LLDP filter for device {} on port {} {}.",
504 devId, port, (install) ? INSTALLED : REMOVED);
505 }
506
507 @Override
508 public void onError(Objective objective, ObjectiveError error) {
509 log.info("LLDP filter for device {} on port {} failed {} because {}",
510 devId, port, (install) ? INSTALLATION : REMOVAL,
511 error);
512 }
513 });
514
515 flowObjectiveService.filter(devId, lldp);
516 }
517
518 @Override
519 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
520 PortNumber subscriberPort,
521 MeterId meterId,
522 UniTagInformation tagInfo,
523 boolean upstream) {
524
525 TrafficSelector selector = DefaultTrafficSelector.builder()
526 .matchVlanId(tagInfo.getPonSTag())
527 .matchInPort(upstream ? subscriberPort : uplinkPort)
528 .matchInnerVlanId(tagInfo.getPonCTag())
529 .build();
530
531 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
532 if (meterId != null) {
533 tBuilder.meter(meterId);
534 }
535
536 TrafficTreatment treatment = tBuilder
537 .setOutput(upstream ? uplinkPort : subscriberPort)
538 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
539 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
540 .build();
541
542 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
543 }
544
545 @Override
546 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
547 PortNumber subscriberPort,
548 MeterId upstreamMeterId,
549 UniTagInformation uniTagInformation) {
550
551 TrafficSelector selector = DefaultTrafficSelector.builder()
552 .matchInPort(subscriberPort)
553 .matchVlanId(uniTagInformation.getUniTagMatch())
554 .build();
555
Andrea Campanella327c5722020-01-30 11:34:13 +0100556 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
557 //if the subscriberVlan (cTag) is different than ANY it needs to set.
558 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
559 treatmentBuilder.pushVlan()
560 .setVlanId(uniTagInformation.getPonCTag());
561 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000562
563 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
564 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
565 }
566
567 treatmentBuilder.pushVlan()
568 .setVlanId(uniTagInformation.getPonSTag());
569
570 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
571 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
572 }
573
574 treatmentBuilder.setOutput(uplinkPort)
575 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
576 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
577
578 if (upstreamMeterId != null) {
579 treatmentBuilder.meter(upstreamMeterId);
580 }
581
582 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
583 }
584
585 @Override
586 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
587 PortNumber subscriberPort,
588 MeterId downstreamMeterId,
589 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100590
591 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000592 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
593 .matchVlanId(tagInformation.getPonSTag())
594 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100595 .matchInnerVlanId(tagInformation.getPonCTag());
596
597
598 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
599 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
600 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000601
602 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
603 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
604 }
605
606 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800607 !tagInformation.getConfiguredMacAddress().equals("") &&
608 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000609 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
610 }
611
612 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
613 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100614 .setOutput(subscriberPort);
615
Andrea Campanella327c5722020-01-30 11:34:13 +0100616 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
617 tagInformation.getTechnologyProfileId(),
618 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000619
620 // to remark inner vlan header
621 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
622 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
623 }
624
Andrea Campanella9a779292020-02-03 19:19:09 +0100625 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
626 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000627 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
628 }
629
630 if (downstreamMeterId != null) {
631 treatmentBuilder.meter(downstreamMeterId);
632 }
633
634 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
635 }
636
637 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
638 TrafficTreatment treatment,
639 Integer priority) {
640 return DefaultForwardingObjective.builder()
641 .withFlag(ForwardingObjective.Flag.VERSATILE)
642 .withPriority(priority)
643 .makePermanent()
644 .withSelector(selector)
645 .fromApp(appId)
646 .withTreatment(treatment);
647 }
648
649 /**
650 * Returns the write metadata value including tech profile reference and innerVlan.
651 * For param cVlan, null can be sent
652 *
653 * @param cVlan c (customer) tag of one subscriber
654 * @param techProfileId tech profile id of one subscriber
655 * @return the write metadata value including tech profile reference and innerVlan
656 */
657 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
658 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
659 return (long) techProfileId << 32;
660 }
661 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
662 }
663
664 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
665 if (bandwidthProfile == null) {
666 return null;
667 }
668 return bpService.get(bandwidthProfile);
669 }
670
671 /**
672 * It will be used to support AT&T use case (for EAPOL flows).
673 * If multiple services are found in uniServiceList, returns default tech profile id
674 * If one service is found, returns the found one
675 *
676 * @param devId
677 * @param portNumber
678 * @return the default technology profile id
679 */
680 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
681 Port port = deviceService.getPort(devId, portNumber);
682 if (port != null) {
683 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
684 if (info != null && info.uniTagList().size() == 1) {
685 return info.uniTagList().get(0).getTechnologyProfileId();
686 }
687 }
688 return defaultTechProfileId;
689 }
690
691 /**
692 * Write metadata instruction value (metadata) is 8 bytes.
693 * <p>
694 * MS 2 bytes: C Tag
695 * Next 2 bytes: Technology Profile Id
696 * Next 4 bytes: Port number (uni or nni)
697 */
698 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
699 if (techProfileId == NONE_TP_ID) {
700 techProfileId = DEFAULT_TP_ID;
701 }
702
703 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
704 }
705
706
707}