blob: 9db1b406a411baab6b1586d1db5e3455791108cd [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) {
Matteo Scandolo34556e52020-05-08 12:34:13 -0700306 if (!enableIgmpOnProvisioning) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000307 log.debug("Igmp provisioning is disabled.");
308 return;
309 }
310
Matteo Scandolo34556e52020-05-08 12:34:13 -0700311 if (!upstream) {
312 log.debug("Direction is not Upstream, ignoring Igmp request");
313 return;
314 }
315
316 if (!mastershipService.isLocalMaster(devId)) {
317 return;
318 }
319
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000320 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
321 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
322 if (upstream) {
323
324 if (tagInformation.getTechnologyProfileId() != NONE_TP_ID) {
325 treatmentBuilder.writeMetadata(createTechProfValueForWm(null,
326 tagInformation.getTechnologyProfileId()), 0);
327 }
328
329
330 if (upstreamMeterId != null) {
331 treatmentBuilder.meter(upstreamMeterId);
332 }
333
334 if (!VlanId.NONE.equals(tagInformation.getPonCTag())) {
335 builder.addCondition(Criteria.matchVlanId(tagInformation.getPonCTag()));
336 }
337
338 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
339 builder.addCondition(Criteria.matchVlanPcp((byte) tagInformation.getUsPonCTagPriority()));
340 }
341 }
342
343 builder = install ? builder.permit() : builder.deny();
344
345 FilteringObjective igmp = builder
346 .withKey(Criteria.matchInPort(port))
347 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
348 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
349 .withMeta(treatmentBuilder
350 .setOutput(PortNumber.CONTROLLER).build())
351 .fromApp(appId)
352 .withPriority(MAX_PRIORITY)
353 .add(new ObjectiveContext() {
354 @Override
355 public void onSuccess(Objective objective) {
356 log.info("Igmp filter for {} on {} {}.",
357 devId, port, (install) ? INSTALLED : REMOVED);
358 }
359
360 @Override
361 public void onError(Objective objective, ObjectiveError error) {
362 log.info("Igmp filter for {} on {} failed {} because {}.",
363 devId, port, (install) ? INSTALLATION : REMOVAL,
364 error);
365 }
366 });
367
368 flowObjectiveService.filter(devId, igmp);
369 }
370
371 @Override
372 public void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
373 CompletableFuture<ObjectiveError> filterFuture,
374 VlanId vlanId, boolean install) {
375
376 if (!enableEapol) {
377 log.debug("Eapol filtering is disabled. Completing filteringFuture immediately for the device {}", devId);
378 if (filterFuture != null) {
379 filterFuture.complete(null);
380 }
381 return;
382 }
383
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000384 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
385 if (bpInfo == null) {
386 log.warn("Bandwidth profile {} is not found. Authentication flow"
387 + " will not be installed", bpId);
388 if (filterFuture != null) {
389 filterFuture.complete(ObjectiveError.BADPARAMS);
390 }
391 return;
392 }
393
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700394 ConnectPoint cp = new ConnectPoint(devId, portNumber);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000395 if (install) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700396 boolean added = pendingAddEapol.add(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000397 if (!added) {
398 if (filterFuture != null) {
399 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
400 filterFuture.complete(null);
401 }
402 return;
403 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700404 log.info("connectPoint added to pendingAddEapol map {}", cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000405 }
406
407 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
408 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
409 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
410
411 // check if meter exists and create it only for an install
412 MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
413 if (meterId == null) {
414 if (install) {
415 meterId = oltMeterService.createMeter(devId, bpInfo, meterFuture);
416 treatmentBuilder.meter(meterId);
417 } else {
418 // this case should not happen as the request to remove an eapol
419 // flow should mean that the flow points to a meter that exists.
420 // Nevertheless we can still delete the flow as we only need the
421 // correct 'match' to do so.
422 log.warn("Unknown meter id for bp {}, still proceeding with "
423 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
424 meterFuture.complete(null);
425 }
426 } else {
427 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
428 treatmentBuilder.meter(meterId);
429 meterFuture.complete(null);
430 }
431
432 final MeterId mId = meterId;
433 meterFuture.thenAcceptAsync(result -> {
434 if (result == null) {
435 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
436 mId, bpId, devId, portNumber,
437 (install) ? "Installing" : "Removing");
438 int techProfileId = getDefaultTechProfileId(devId, portNumber);
439
440 //Authentication trap flow uses only tech profile id as write metadata value
441 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
442 .withKey(Criteria.matchInPort(portNumber))
443 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
444 .addCondition(Criteria.matchVlanId(vlanId))
445 .withMeta(treatmentBuilder
446 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
447 .setOutput(PortNumber.CONTROLLER).build())
448 .fromApp(appId)
449 .withPriority(MAX_PRIORITY)
450 .add(new ObjectiveContext() {
451 @Override
452 public void onSuccess(Objective objective) {
453 log.info("Eapol filter for {} on {} {} with meter {}.",
454 devId, portNumber, (install) ? INSTALLED : REMOVED, mId);
455 if (filterFuture != null) {
456 filterFuture.complete(null);
457 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700458 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000459 }
460
461 @Override
462 public void onError(Objective objective, ObjectiveError error) {
463 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
464 devId, portNumber, mId, (install) ? INSTALLATION : REMOVAL,
465 error);
466 if (filterFuture != null) {
467 filterFuture.complete(error);
468 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700469 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000470 }
471 });
472
473 flowObjectiveService.filter(devId, eapol);
474 } else {
475 log.warn("Meter installation error while sending eapol trap flow. " +
476 "Result {} and MeterId {}", result, mId);
477 }
478 });
479 }
480
481 /**
482 * Installs trap filtering objectives for particular traffic types on an
483 * NNI port.
484 *
485 * @param devId device ID
486 * @param port port number
487 * @param install true to install, false to remove
488 */
489 @Override
490 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
491 log.info("Sending flows for NNI port {} of the device {}", port, devId);
492 processLldpFilteringObjective(devId, port, install);
493 processDhcpFilteringObjectives(devId, port, null, null, install, false);
494 processIgmpFilteringObjectives(devId, port, null, null, install, false);
495 }
496
497
498 @Override
499 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000500 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
501
502 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
503 .withKey(Criteria.matchInPort(port))
504 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
505 .withMeta(DefaultTrafficTreatment.builder()
506 .setOutput(PortNumber.CONTROLLER).build())
507 .fromApp(appId)
508 .withPriority(MAX_PRIORITY)
509 .add(new ObjectiveContext() {
510 @Override
511 public void onSuccess(Objective objective) {
512 log.info("LLDP filter for device {} on port {} {}.",
513 devId, port, (install) ? INSTALLED : REMOVED);
514 }
515
516 @Override
517 public void onError(Objective objective, ObjectiveError error) {
518 log.info("LLDP filter for device {} on port {} failed {} because {}",
519 devId, port, (install) ? INSTALLATION : REMOVAL,
520 error);
521 }
522 });
523
524 flowObjectiveService.filter(devId, lldp);
525 }
526
527 @Override
528 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
529 PortNumber subscriberPort,
530 MeterId meterId,
531 UniTagInformation tagInfo,
532 boolean upstream) {
533
534 TrafficSelector selector = DefaultTrafficSelector.builder()
535 .matchVlanId(tagInfo.getPonSTag())
536 .matchInPort(upstream ? subscriberPort : uplinkPort)
537 .matchInnerVlanId(tagInfo.getPonCTag())
538 .build();
539
540 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
541 if (meterId != null) {
542 tBuilder.meter(meterId);
543 }
544
545 TrafficTreatment treatment = tBuilder
546 .setOutput(upstream ? uplinkPort : subscriberPort)
547 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
548 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
549 .build();
550
551 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
552 }
553
554 @Override
555 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
556 PortNumber subscriberPort,
557 MeterId upstreamMeterId,
558 UniTagInformation uniTagInformation) {
559
560 TrafficSelector selector = DefaultTrafficSelector.builder()
561 .matchInPort(subscriberPort)
562 .matchVlanId(uniTagInformation.getUniTagMatch())
563 .build();
564
Andrea Campanella327c5722020-01-30 11:34:13 +0100565 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
566 //if the subscriberVlan (cTag) is different than ANY it needs to set.
567 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
568 treatmentBuilder.pushVlan()
569 .setVlanId(uniTagInformation.getPonCTag());
570 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000571
572 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
573 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
574 }
575
576 treatmentBuilder.pushVlan()
577 .setVlanId(uniTagInformation.getPonSTag());
578
579 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
580 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
581 }
582
583 treatmentBuilder.setOutput(uplinkPort)
584 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
585 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
586
587 if (upstreamMeterId != null) {
588 treatmentBuilder.meter(upstreamMeterId);
589 }
590
591 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
592 }
593
594 @Override
595 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
596 PortNumber subscriberPort,
597 MeterId downstreamMeterId,
598 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100599
600 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000601 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
602 .matchVlanId(tagInformation.getPonSTag())
603 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100604 .matchInnerVlanId(tagInformation.getPonCTag());
605
606
607 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
608 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
609 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000610
611 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
612 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
613 }
614
615 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800616 !tagInformation.getConfiguredMacAddress().equals("") &&
617 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000618 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
619 }
620
621 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
622 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100623 .setOutput(subscriberPort);
624
Andrea Campanella327c5722020-01-30 11:34:13 +0100625 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
626 tagInformation.getTechnologyProfileId(),
627 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000628
629 // to remark inner vlan header
630 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
631 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
632 }
633
Andrea Campanella9a779292020-02-03 19:19:09 +0100634 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
635 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000636 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
637 }
638
639 if (downstreamMeterId != null) {
640 treatmentBuilder.meter(downstreamMeterId);
641 }
642
643 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
644 }
645
646 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
647 TrafficTreatment treatment,
648 Integer priority) {
649 return DefaultForwardingObjective.builder()
650 .withFlag(ForwardingObjective.Flag.VERSATILE)
651 .withPriority(priority)
652 .makePermanent()
653 .withSelector(selector)
654 .fromApp(appId)
655 .withTreatment(treatment);
656 }
657
658 /**
659 * Returns the write metadata value including tech profile reference and innerVlan.
660 * For param cVlan, null can be sent
661 *
662 * @param cVlan c (customer) tag of one subscriber
663 * @param techProfileId tech profile id of one subscriber
664 * @return the write metadata value including tech profile reference and innerVlan
665 */
666 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
667 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
668 return (long) techProfileId << 32;
669 }
670 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
671 }
672
673 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
674 if (bandwidthProfile == null) {
675 return null;
676 }
677 return bpService.get(bandwidthProfile);
678 }
679
680 /**
681 * It will be used to support AT&T use case (for EAPOL flows).
682 * If multiple services are found in uniServiceList, returns default tech profile id
683 * If one service is found, returns the found one
684 *
685 * @param devId
686 * @param portNumber
687 * @return the default technology profile id
688 */
689 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
690 Port port = deviceService.getPort(devId, portNumber);
691 if (port != null) {
692 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
693 if (info != null && info.uniTagList().size() == 1) {
694 return info.uniTagList().get(0).getTechnologyProfileId();
695 }
696 }
697 return defaultTechProfileId;
698 }
699
700 /**
701 * Write metadata instruction value (metadata) is 8 bytes.
702 * <p>
703 * MS 2 bytes: C Tag
704 * Next 2 bytes: Technology Profile Id
705 * Next 4 bytes: Port number (uni or nni)
706 */
707 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
708 if (techProfileId == NONE_TP_ID) {
709 techProfileId = DEFAULT_TP_ID;
710 }
711
712 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
713 }
714
715
716}