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