blob: 625214442f0165e16e9a9c95eaef34715388f222 [file] [log] [blame]
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001/*
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07002 * Copyright 2021-present Open Networking Foundation
Andrea Campanellacbbb7952019-11-25 06:38:41 +00003 *
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 */
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070016
Andrea Campanellacbbb7952019-11-25 06:38:41 +000017package org.opencord.olt.impl;
18
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070019import com.google.common.collect.ImmutableMap;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000020import org.onlab.packet.EthType;
21import org.onlab.packet.IPv4;
22import org.onlab.packet.IPv6;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.TpPort;
25import org.onlab.packet.VlanId;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000026import org.onlab.util.KryoNamespace;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000027import org.onlab.util.Tools;
28import org.onosproject.cfg.ComponentConfigService;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Andrea Campanella40d2b342022-02-04 18:13:37 +010031import org.onosproject.net.AnnotationKeys;
yasin saplib4b8ee12021-06-13 18:25:20 +000032import org.onosproject.net.Annotations;
Matteo Scandolo3a037a32020-04-01 12:17:50 -070033import org.onosproject.net.ConnectPoint;
yasin saplib4b8ee12021-06-13 18:25:20 +000034import org.onosproject.net.DefaultAnnotations;
Andrea Campanella40d2b342022-02-04 18:13:37 +010035import org.onosproject.net.DefaultPort;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070036import org.onosproject.net.Device;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000037import org.onosproject.net.DeviceId;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070038import org.onosproject.net.Host;
39import org.onosproject.net.Port;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000040import org.onosproject.net.PortNumber;
41import org.onosproject.net.device.DeviceService;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070044import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.FlowRuleEvent;
46import org.onosproject.net.flow.FlowRuleListener;
47import org.onosproject.net.flow.FlowRuleService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000048import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.flow.criteria.Criteria;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070051import org.onosproject.net.flow.criteria.Criterion;
52import org.onosproject.net.flow.criteria.EthTypeCriterion;
53import org.onosproject.net.flow.criteria.IPProtocolCriterion;
54import org.onosproject.net.flow.criteria.PortCriterion;
55import org.onosproject.net.flow.criteria.UdpPortCriterion;
56import org.onosproject.net.flow.criteria.VlanIdCriterion;
57import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000058import org.onosproject.net.flowobjective.DefaultFilteringObjective;
59import org.onosproject.net.flowobjective.DefaultForwardingObjective;
60import org.onosproject.net.flowobjective.FilteringObjective;
61import org.onosproject.net.flowobjective.FlowObjectiveService;
62import org.onosproject.net.flowobjective.ForwardingObjective;
63import org.onosproject.net.flowobjective.Objective;
64import org.onosproject.net.flowobjective.ObjectiveContext;
65import org.onosproject.net.flowobjective.ObjectiveError;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070066import org.onosproject.net.host.HostService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000067import org.onosproject.net.meter.MeterId;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000068import org.onosproject.store.serializers.KryoNamespaces;
69import org.onosproject.store.service.Serializer;
70import org.onosproject.store.service.StorageService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000071import org.opencord.sadis.BandwidthProfileInformation;
72import org.opencord.sadis.BaseInformationService;
73import org.opencord.sadis.SadisService;
74import org.opencord.sadis.SubscriberAndDeviceInformation;
75import org.opencord.sadis.UniTagInformation;
76import org.osgi.service.component.ComponentContext;
77import org.osgi.service.component.annotations.Activate;
78import org.osgi.service.component.annotations.Component;
79import org.osgi.service.component.annotations.Deactivate;
80import org.osgi.service.component.annotations.Modified;
81import org.osgi.service.component.annotations.Reference;
82import org.osgi.service.component.annotations.ReferenceCardinality;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000083import org.osgi.service.component.annotations.ReferencePolicy;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000084import org.slf4j.Logger;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070085import org.slf4j.LoggerFactory;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000086
Matteo Scandolo2542e5d2021-12-01 16:53:41 -080087import java.util.ArrayList;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +010088import java.util.Dictionary;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070089import java.util.HashMap;
Andrea Campanella61650a12022-01-24 18:09:44 -080090import java.util.HashSet;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070091import java.util.Iterator;
Matteo Scandolo2542e5d2021-12-01 16:53:41 -080092import java.util.List;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000093import java.util.Map;
Andrea Campanellabfb47af2021-06-03 11:09:45 +020094import java.util.Optional;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +010095import java.util.Properties;
Andrea Campanella61650a12022-01-24 18:09:44 -080096import java.util.Set;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070097import java.util.concurrent.atomic.AtomicBoolean;
98import java.util.concurrent.locks.Lock;
99import java.util.concurrent.locks.ReentrantReadWriteLock;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +0100100
101import static com.google.common.base.Strings.isNullOrEmpty;
102import static org.onlab.util.Tools.get;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700103import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION;
104import static org.opencord.olt.impl.OltUtils.completeFlowOpToString;
105import static org.opencord.olt.impl.OltUtils.flowOpToString;
106import static org.opencord.olt.impl.OltUtils.getPortName;
107import static org.opencord.olt.impl.OltUtils.portWithName;
108import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID;
109import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID_DEFAULT;
110import static org.opencord.olt.impl.OsgiPropertyConstants.DOWNSTREAM_OLT;
111import static org.opencord.olt.impl.OsgiPropertyConstants.DOWNSTREAM_ONU;
112import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_ON_NNI;
113import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_ON_NNI_DEFAULT;
114import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V4;
115import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V4_DEFAULT;
116import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V6;
117import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V6_DEFAULT;
118import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_EAPOL;
119import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_EAPOL_DEFAULT;
120import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_IGMP_ON_NNI;
121import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_IGMP_ON_NNI_DEFAULT;
yasin sapli0823c932022-01-26 11:26:09 +0000122import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_PPPOE_ON_NNI;
123import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_PPPOE_ON_NNI_DEFAULT;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700124import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_PPPOE;
125import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_PPPOE_DEFAULT;
126import static org.opencord.olt.impl.OsgiPropertyConstants.UPSTREAM_OLT;
127import static org.opencord.olt.impl.OsgiPropertyConstants.UPSTREAM_ONU;
128import static org.opencord.olt.impl.OsgiPropertyConstants.WAIT_FOR_REMOVAL;
129import static org.opencord.olt.impl.OsgiPropertyConstants.WAIT_FOR_REMOVAL_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000130
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000131@Component(immediate = true, property = {
Saurav Dasf62cea82020-08-26 17:43:04 -0700132 ENABLE_DHCP_ON_NNI + ":Boolean=" + ENABLE_DHCP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000133 ENABLE_DHCP_V4 + ":Boolean=" + ENABLE_DHCP_V4_DEFAULT,
134 ENABLE_DHCP_V6 + ":Boolean=" + ENABLE_DHCP_V6_DEFAULT,
Saurav Dasf62cea82020-08-26 17:43:04 -0700135 ENABLE_IGMP_ON_NNI + ":Boolean=" + ENABLE_IGMP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000136 ENABLE_EAPOL + ":Boolean=" + ENABLE_EAPOL_DEFAULT,
yasin sapli0823c932022-01-26 11:26:09 +0000137 ENABLE_PPPOE_ON_NNI + ":Boolean=" + ENABLE_PPPOE_ON_NNI_DEFAULT,
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300138 ENABLE_PPPOE + ":Boolean=" + ENABLE_PPPOE_DEFAULT,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700139 DEFAULT_TP_ID + ":Integer=" + DEFAULT_TP_ID_DEFAULT,
140 // FIXME remove this option as potentially dangerous in production
141 WAIT_FOR_REMOVAL + ":Boolean=" + WAIT_FOR_REMOVAL_DEFAULT
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000142})
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700143public class OltFlowService implements OltFlowServiceInterface {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
146 protected CoreService coreService;
147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700149 protected ComponentConfigService cfgService;
150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
152 protected FlowObjectiveService flowObjectiveService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000153
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000154 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
155 bind = "bindSadisService",
156 unbind = "unbindSadisService",
157 policy = ReferencePolicy.DYNAMIC)
158 protected volatile SadisService sadisService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700161 protected OltMeterServiceInterface oltMeterService;
162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY)
164 protected OltDeviceServiceInterface oltDeviceService;
165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY)
167 protected FlowRuleService flowRuleService;
168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY)
170 protected HostService hostService;
171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000173 protected DeviceService deviceService;
174
175 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000176 protected StorageService storageService;
177
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700178 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
179 protected BaseInformationService<BandwidthProfileInformation> bpService;
180
181 private static final String APP_NAME = "org.opencord.olt";
182 protected ApplicationId appId;
183 private static final Integer MAX_PRIORITY = 10000;
184 private static final Integer MIN_PRIORITY = 1000;
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800185 protected static final short EAPOL_DEFAULT_VLAN = 4091;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700186 private static final int NONE_TP_ID = -1;
187 private static final String V4 = "V4";
188 private static final String V6 = "V6";
189 private final Logger log = LoggerFactory.getLogger(getClass());
190
191 protected UniTagInformation defaultEapolUniTag = new UniTagInformation.Builder()
192 .setServiceName("defaultEapol").build();
193 protected UniTagInformation nniUniTag = new UniTagInformation.Builder()
194 .setServiceName("nni")
195 .setTechnologyProfileId(NONE_TP_ID)
196 .setPonCTag(VlanId.NONE)
197 .setUniTagMatch(VlanId.ANY)
198 .setUsPonCTagPriority(-1)
199 .build();
200
201 /**
202 * Connect Point status map.
203 * Used to keep track of which cp has flows that needs to be removed when the status changes.
204 */
205 protected Map<ServiceKey, OltPortStatus> cpStatus;
206 private final ReentrantReadWriteLock cpStatusLock = new ReentrantReadWriteLock();
207 private final Lock cpStatusWriteLock = cpStatusLock.writeLock();
208 private final Lock cpStatusReadLock = cpStatusLock.readLock();
209
210 /**
211 * This map contains the subscriber that have been provisioned by the operator.
212 * They may or may not have flows, depending on the port status.
213 * The map is used to define whether flows need to be provisioned when a port comes up.
214 */
215 protected Map<ServiceKey, Boolean> provisionedSubscribers;
216 private final ReentrantReadWriteLock provisionedSubscribersLock = new ReentrantReadWriteLock();
217 private final Lock provisionedSubscribersWriteLock = provisionedSubscribersLock.writeLock();
218 private final Lock provisionedSubscribersReadLock = provisionedSubscribersLock.readLock();
219
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000220 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700221 * Create DHCP trap flow on NNI port(s).
222 */
223 protected boolean enableDhcpOnNni = ENABLE_DHCP_ON_NNI_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000224
225 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700226 * Enable flows for DHCP v4 if dhcp is required in sadis config.
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000227 **/
228 protected boolean enableDhcpV4 = ENABLE_DHCP_V4_DEFAULT;
229
230 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700231 * Enable flows for DHCP v6 if dhcp is required in sadis config.
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000232 **/
233 protected boolean enableDhcpV6 = ENABLE_DHCP_V6_DEFAULT;
234
235 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700236 * Create IGMP trap flow on NNI port(s).
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000237 **/
Saurav Dasf62cea82020-08-26 17:43:04 -0700238 protected boolean enableIgmpOnNni = ENABLE_IGMP_ON_NNI_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000239
240 /**
241 * Send EAPOL authentication trap flows before subscriber provisioning.
242 **/
243 protected boolean enableEapol = ENABLE_EAPOL_DEFAULT;
244
245 /**
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300246 * Send PPPoED authentication trap flows before subscriber provisioning.
247 **/
yasin sapli0823c932022-01-26 11:26:09 +0000248 protected boolean enablePppoeOnNni = ENABLE_PPPOE_ON_NNI_DEFAULT;
249
250 /**
251 * Enable flows for PPPoE if it is required in sadis config.
252 **/
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300253 protected boolean enablePppoe = ENABLE_PPPOE_DEFAULT;
254
255 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000256 * Default technology profile id that is used for authentication trap flows.
257 **/
258 protected int defaultTechProfileId = DEFAULT_TP_ID_DEFAULT;
259
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700260 protected boolean waitForRemoval = WAIT_FOR_REMOVAL_DEFAULT;
261
262 public enum FlowOperation {
263 ADD,
264 REMOVE;
265
266
267 @Override
268 public String toString() {
269 return super.toString().toLowerCase();
270 }
271 }
272
273 public enum FlowDirection {
274 UPSTREAM,
275 DOWNSTREAM,
276 }
277
278 public enum OltFlowsStatus {
279 NONE,
280 PENDING_ADD,
281 ADDED,
282 PENDING_REMOVE,
283 REMOVED,
284 ERROR
285 }
286
287 protected InternalFlowListener internalFlowListener;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000288
289 @Activate
290 public void activate(ComponentContext context) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700291 cfgService.registerProperties(getClass());
292 appId = coreService.registerApplication(APP_NAME);
293 internalFlowListener = new InternalFlowListener();
294
295 modified(context);
296
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000297 KryoNamespace serializer = KryoNamespace.newBuilder()
298 .register(KryoNamespaces.API)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700299 .register(OltFlowsStatus.class)
300 .register(FlowDirection.class)
301 .register(OltPortStatus.class)
302 .register(OltFlowsStatus.class)
Tunahan Sezenf0843b92021-04-30 07:13:16 +0000303 .register(AccessDevicePort.class)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700304 .register(new ServiceKeySerializer(), ServiceKey.class)
305 .register(UniTagInformation.class)
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000306 .build();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000307
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700308 cpStatus = storageService.<ServiceKey, OltPortStatus>consistentMapBuilder()
309 .withName("volt-cp-status")
310 .withApplicationId(appId)
311 .withSerializer(Serializer.using(serializer))
312 .build().asJavaMap();
313
314 provisionedSubscribers = storageService.<ServiceKey, Boolean>consistentMapBuilder()
315 .withName("volt-provisioned-subscriber")
316 .withApplicationId(appId)
317 .withSerializer(Serializer.using(serializer))
318 .build().asJavaMap();
319
320 flowRuleService.addListener(internalFlowListener);
321
322 log.info("Started");
323 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000324
325 @Deactivate
326 public void deactivate(ComponentContext context) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700327 cfgService.unregisterProperties(getClass(), false);
328 flowRuleService.removeListener(internalFlowListener);
329 log.info("Stopped");
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000330 }
331
332 @Modified
333 public void modified(ComponentContext context) {
334
335 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
336
Saurav Dasf62cea82020-08-26 17:43:04 -0700337 Boolean o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000338 if (o != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700339 enableDhcpOnNni = o;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000340 }
341
Andrea Campanella7c49b792020-05-11 11:36:53 +0200342 Boolean v4 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V4);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000343 if (v4 != null) {
344 enableDhcpV4 = v4;
345 }
346
Andrea Campanella7c49b792020-05-11 11:36:53 +0200347 Boolean v6 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V6);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000348 if (v6 != null) {
349 enableDhcpV6 = v6;
350 }
351
Saurav Dasf62cea82020-08-26 17:43:04 -0700352 Boolean p = Tools.isPropertyEnabled(properties, ENABLE_IGMP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000353 if (p != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700354 enableIgmpOnNni = p;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000355 }
356
Andrea Campanella7c49b792020-05-11 11:36:53 +0200357 Boolean eap = Tools.isPropertyEnabled(properties, ENABLE_EAPOL);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000358 if (eap != null) {
359 enableEapol = eap;
360 }
361
yasin sapli0823c932022-01-26 11:26:09 +0000362 Boolean pppoeInNni = Tools.isPropertyEnabled(properties, ENABLE_PPPOE_ON_NNI);
363 if (pppoeInNni != null) {
364 enablePppoeOnNni = pppoeInNni;
365 }
366
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300367 Boolean pppoe = Tools.isPropertyEnabled(properties, ENABLE_PPPOE);
368 if (pppoe != null) {
369 enablePppoe = pppoe;
370 }
371
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700372 Boolean wait = Tools.isPropertyEnabled(properties, WAIT_FOR_REMOVAL);
373 if (wait != null) {
374 waitForRemoval = wait;
375 }
376
Andrea Campanella7c49b792020-05-11 11:36:53 +0200377 String tpId = get(properties, DEFAULT_TP_ID);
378 defaultTechProfileId = isNullOrEmpty(tpId) ? DEFAULT_TP_ID_DEFAULT : Integer.parseInt(tpId.trim());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000379
yasin sapli0823c932022-01-26 11:26:09 +0000380 log.info("Modified. Values = enableDhcpOnNni: {}, enableDhcpV4: {}, " + "enableDhcpV6:{}, " +
381 "enableIgmpOnNni:{}, " + "enableEapol:{}, enablePppoeOnNni: {}, enablePppoe:{}, " +
382 "defaultTechProfileId:{}," + "waitForRemoval:{}",
383 enableDhcpOnNni, enableDhcpV4, enableDhcpV6, enableIgmpOnNni, enableEapol,
384 enablePppoeOnNni, enablePppoe, defaultTechProfileId, waitForRemoval);
Andrea Campanellafee86422020-06-04 16:01:27 +0200385
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000386 }
387
388 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700389 public ImmutableMap<ServiceKey, OltPortStatus> getConnectPointStatus() {
390 try {
391 cpStatusReadLock.lock();
392 return ImmutableMap.copyOf(cpStatus);
393 } finally {
394 cpStatusReadLock.unlock();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000395 }
396 }
397
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700398 @Override
399 public ImmutableMap<ServiceKey, UniTagInformation> getProgrammedSubscribers() {
400 // NOTE we might want to remove this conversion and directly use cpStatus as it contains
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800401 // all the required information about subscribers
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700402 Map<ServiceKey, UniTagInformation> subscribers =
403 new HashMap<>();
404 try {
405 cpStatusReadLock.lock();
406
407 cpStatus.forEach((sk, status) -> {
408 if (
409 // not NNI Port
410 !oltDeviceService.isNniPort(deviceService.getDevice(sk.getPort().connectPoint().deviceId()),
411 sk.getPort().connectPoint().port()) &&
412 // not EAPOL flow
Andrea Campanella982fd332022-01-19 09:14:12 +0100413 !sk.getService().equals(defaultEapolUniTag) &&
414 !status.subscriberFlowsStatus.equals(OltFlowsStatus.PENDING_REMOVE)
415 && !status.subscriberFlowsStatus.equals(OltFlowsStatus.REMOVED)
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800416
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700417 ) {
418 subscribers.put(sk, sk.getService());
419 }
420 });
421
422 return ImmutableMap.copyOf(subscribers);
423 } finally {
424 cpStatusReadLock.unlock();
425 }
426 }
427
428 @Override
429 public Map<ServiceKey, Boolean> getRequestedSubscribers() {
430 try {
431 provisionedSubscribersReadLock.lock();
432 return ImmutableMap.copyOf(provisionedSubscribers);
433 } finally {
434 provisionedSubscribersReadLock.unlock();
435 }
436 }
437
438 @Override
439 public void handleNniFlows(Device device, Port port, FlowOperation action) {
440
441 // always handle the LLDP flow
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800442 log.debug("{} LLDP trap flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700443 processLldpFilteringObjective(device.id(), port, action);
444
445 if (enableDhcpOnNni) {
446 if (enableDhcpV4) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800447 log.debug("{} DHCPv4 trap flow on NNI {} for device {}", flowOpToString(action),
448 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700449 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
450 67, 68, EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP,
451 null, null, nniUniTag);
452 }
453 if (enableDhcpV6) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800454 log.debug("{} DHCPv6 trap flow on NNI {} for device {}", flowOpToString(action),
455 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700456 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
457 546, 547, EthType.EtherType.IPV6.ethType(), IPv6.PROTOCOL_UDP,
458 null, null, nniUniTag);
459 }
460 } else {
Matteo Scandolo1f8de332021-12-06 12:18:24 -0800461 log.debug("DHCP is not required on NNI {} for device {}", portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700462 }
463
464 if (enableIgmpOnNni) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800465 log.debug("{} IGMP flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700466 processIgmpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
467 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, -1);
468 }
469
yasin sapli0823c932022-01-26 11:26:09 +0000470 if (enablePppoeOnNni) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800471 log.debug("{} PPPoE flow on NNI {} for device {}", flowOpToString(action), port.number(), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700472 processPPPoEDFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
473 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, null);
474 }
475 }
476
477 @Override
478 public boolean handleBasicPortFlows(DiscoveredSubscriber sub, String bandwidthProfileId,
479 String oltBandwidthProfileId) {
480
481 // we only need to something if EAPOL is enabled
482 if (!enableEapol) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100483 log.debug("Eapol is disabled for {}", portWithName(sub.port));
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700484 return true;
485 }
486
487 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
488 return addDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
489 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
490 return removeDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
491 } else {
492 log.error("Unknown Status {} on DiscoveredSubscriber {}", sub.status, sub);
493 return false;
494 }
495
496 }
497
498 private boolean addDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfileId, String oltBandwidthProfileId) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100499 log.debug("Adding default flows for {}, status {}", portWithName(sub.port), sub.status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700500 if (!oltMeterService.createMeter(sub.device.id(), bandwidthProfileId)) {
501 if (log.isTraceEnabled()) {
502 log.trace("waiting on meter for bp {} and sub {}", bandwidthProfileId, sub);
503 }
504 return false;
505 }
506 if (hasDefaultEapol(sub.port)) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100507 OltPortStatus status = getOltPortStatus(sub.port, defaultEapolUniTag);
508 log.debug("Eapol is already present for {} with status {}", portWithName(sub.port), status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700509 return true;
510 }
511 return handleEapolFlow(sub, bandwidthProfileId,
512 oltBandwidthProfileId, FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
513
514 }
515
516 private boolean removeDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfile, String oltBandwidthProfile) {
517 // NOTE that we are not checking for meters as they must have been created to install the flow in first place
518 return handleEapolFlow(sub, bandwidthProfile, oltBandwidthProfile,
519 FlowOperation.REMOVE, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
520 }
521
522 @Override
523 public boolean handleSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwidthProfile,
524 String multicastServiceName) {
525 // NOTE that we are taking defaultBandwithProfile as a parameter as that can be configured in the Olt component
526 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
527 return addSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
528 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
529 return removeSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
530 } else {
531 log.error("don't know how to handle {}", sub);
532 return false;
533 }
534 }
535
536 private boolean addSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
537 String multicastServiceName) {
538 if (log.isTraceEnabled()) {
539 log.trace("Provisioning of subscriber on {} started", portWithName(sub.port));
540 }
541 if (enableEapol) {
542 if (hasDefaultEapol(sub.port)) {
543 // remove EAPOL flow and throw exception so that we'll retry later
544 if (!isDefaultEapolPendingRemoval(sub.port)) {
545 removeDefaultFlows(sub, defaultBandwithProfile, defaultBandwithProfile);
546 }
547
548 if (waitForRemoval) {
549 // NOTE wait for removal is a flag only needed to make sure VOLTHA
550 // does not explode with the flows remove/add in the same batch
551 log.debug("Awaiting for default flows removal for {}", portWithName(sub.port));
552 return false;
553 } else {
554 log.warn("continuing provisioning on {}", portWithName(sub.port));
555 }
556 }
557
558 }
559
560 // NOTE createMeters will return if the meters are not installed
561 if (!oltMeterService.createMeters(sub.device.id(),
Matteo Scandolo88df8ae2021-11-23 13:12:29 -0800562 sub.subscriberAndDeviceInformation, multicastServiceName)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700563 return false;
564 }
565
566 // NOTE we need to add the DHCP flow regardless so that the host can be discovered and the MacAddress added
567 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.ADD,
568 sub.subscriberAndDeviceInformation);
569
570 if (isMacLearningEnabled(sub.subscriberAndDeviceInformation)
571 && !isMacAddressAvailable(sub.device.id(), sub.port,
572 sub.subscriberAndDeviceInformation)) {
573 log.debug("Awaiting for macAddress on {}", portWithName(sub.port));
574 return false;
575 }
576
Matteo Scandolo8a91c0f2021-12-03 10:58:14 -0800577 // NOTE that the EAPOL flows handling is based on the data-plane flows status
578 // always process them before
579 handleSubscriberEapolFlows(sub, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
580
yasin sapli0823c932022-01-26 11:26:09 +0000581 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
582
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700583 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.ADD,
584 sub.subscriberAndDeviceInformation, multicastServiceName);
585
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700586 handleSubscriberIgmpFlows(sub, FlowOperation.ADD);
587
588 log.info("Provisioning of subscriber on {} completed", portWithName(sub.port));
589 return true;
590 }
591
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800592 protected boolean removeSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700593 String multicastServiceName) {
594
595 if (log.isTraceEnabled()) {
596 log.trace("Removal of subscriber on {} started",
597 portWithName(sub.port));
598 }
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800599 SubscriberAndDeviceInformation si = sub.subscriberAndDeviceInformation;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700600
601 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.REMOVE, si);
602
yasin sapli0823c932022-01-26 11:26:09 +0000603 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.REMOVE, sub.subscriberAndDeviceInformation);
604
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700605 if (enableEapol) {
606 // remove the tagged eapol
607 handleSubscriberEapolFlows(sub, FlowOperation.REMOVE, si);
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800608 }
609 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.REMOVE, si, multicastServiceName);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700610
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800611 handleSubscriberIgmpFlows(sub, FlowOperation.REMOVE);
612
613 if (enableEapol) {
614
615 // if any of the services still has flows, return false
616 Iterator<UniTagInformation> iter = sub.subscriberAndDeviceInformation.uniTagList().iterator();
617 while (iter.hasNext()) {
618 UniTagInformation entry = iter.next();
619 if (areSubscriberFlowsPendingRemoval(sub.port, entry)) {
620 log.info("Subscriber {} still have flows on service {}, postpone default EAPOL installation.",
621 portWithName(sub.port), entry.getServiceName());
622 return false;
623 }
624 }
625
626 // once the flows are removed add the default one back
627 // (only if the port is ENABLED and still present on the device)
Matteo Scandolo49c42052021-11-23 13:12:29 -0800628 if (sub.port.isEnabled() && deviceService.getPort(sub.device.id(), sub.port.number()) != null) {
629
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700630 // NOTE we remove the subscriber when the port goes down
631 // but in that case we don't need to add default eapol
632 handleEapolFlow(sub, defaultBandwithProfile, defaultBandwithProfile,
633 FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
634 }
635 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700636 // FIXME check the return status of the flow and return accordingly
637 log.info("Removal of subscriber on {} completed", portWithName(sub.port));
638 return true;
639 }
640
641 @Override
642 public boolean hasDefaultEapol(Port port) {
643 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
644 // NOTE we consider ERROR as a present EAPOL flow as ONOS
645 // will keep trying to add it
646 return status != null && (status.defaultEapolStatus == OltFlowsStatus.ADDED ||
647 status.defaultEapolStatus == OltFlowsStatus.PENDING_ADD ||
648 status.defaultEapolStatus == OltFlowsStatus.ERROR);
649 }
650
651 private OltPortStatus getOltPortStatus(Port port, UniTagInformation uniTagInformation) {
652 try {
653 cpStatusReadLock.lock();
654 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uniTagInformation);
655 OltPortStatus status = cpStatus.get(sk);
656 return status;
657 } finally {
658 cpStatusReadLock.unlock();
659 }
660 }
661
662 public boolean isDefaultEapolPendingRemoval(Port port) {
663 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
664 if (log.isTraceEnabled()) {
665 log.trace("Status during EAPOL flow check {} for port {} and UniTagInformation {}",
666 status, portWithName(port), defaultEapolUniTag);
667 }
668 return status != null && status.defaultEapolStatus == OltFlowsStatus.PENDING_REMOVE;
669 }
670
671 @Override
672 public boolean hasDhcpFlows(Port port, UniTagInformation uti) {
673 OltPortStatus status = getOltPortStatus(port, uti);
674 if (log.isTraceEnabled()) {
675 log.trace("Status during DHCP flow check {} for port {} and service {}",
676 status, portWithName(port), uti.getServiceName());
677 }
678 return status != null &&
679 (status.dhcpStatus == OltFlowsStatus.ADDED ||
680 status.dhcpStatus == OltFlowsStatus.PENDING_ADD);
681 }
682
683 @Override
yasin sapli0823c932022-01-26 11:26:09 +0000684 public boolean hasPppoeFlows(Port port, UniTagInformation uti) {
685 OltPortStatus status = getOltPortStatus(port, uti);
686 if (log.isTraceEnabled()) {
687 log.trace("Status during PPPoE flow check {} for port {} and service {}",
688 status, portWithName(port), uti.getServiceName());
689 }
690 return status != null &&
691 (status.pppoeStatus == OltFlowsStatus.ADDED ||
692 status.pppoeStatus == OltFlowsStatus.PENDING_ADD);
693 }
694
695 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700696 public boolean hasSubscriberFlows(Port port, UniTagInformation uti) {
697
698 OltPortStatus status = getOltPortStatus(port, uti);
699 if (log.isTraceEnabled()) {
700 log.trace("Status during subscriber flow check {} for port {} and service {}",
701 status, portWithName(port), uti.getServiceName());
702 }
703 return status != null && (status.subscriberFlowsStatus == OltFlowsStatus.ADDED ||
704 status.subscriberFlowsStatus == OltFlowsStatus.PENDING_ADD);
705 }
706
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800707 public boolean areSubscriberFlowsPendingRemoval(Port port, UniTagInformation uti) {
708 OltPortStatus status = getOltPortStatus(port, uti);
709 if (log.isTraceEnabled()) {
710 log.trace("Status during pending_remove flow check {} for port {} and UniTagInformation {}",
711 status, portWithName(port), uti);
712 }
713 return status != null && status.subscriberFlowsStatus == OltFlowsStatus.PENDING_REMOVE;
714 }
715
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700716 @Override
717 public void purgeDeviceFlows(DeviceId deviceId) {
718 log.debug("Purging flows on device {}", deviceId);
Matteo Scandolob6981dc2021-12-02 16:31:44 -0800719 flowRuleService.purgeFlowRules(deviceId);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700720
721 // removing the status from the cpStatus map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800722 if (log.isTraceEnabled()) {
723 log.trace("Clearing cp status from device {}", deviceId);
724 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700725 try {
726 cpStatusWriteLock.lock();
727 Iterator<Map.Entry<ServiceKey, OltPortStatus>> iter = cpStatus.entrySet().iterator();
728 while (iter.hasNext()) {
729 Map.Entry<ServiceKey, OltPortStatus> entry = iter.next();
730 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
731 cpStatus.remove(entry.getKey());
732 }
733 }
734 } finally {
735 cpStatusWriteLock.unlock();
736 }
737
738 // removing subscribers from the provisioned map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800739 if (log.isTraceEnabled()) {
740 log.trace("Clearing provisioned subscribers from device {}", deviceId);
741 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700742 try {
743 provisionedSubscribersWriteLock.lock();
744 Iterator<Map.Entry<ServiceKey, Boolean>> iter = provisionedSubscribers.entrySet().iterator();
745 while (iter.hasNext()) {
746 Map.Entry<ServiceKey, Boolean> entry = iter.next();
747 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
748 provisionedSubscribers.remove(entry.getKey());
749 }
750 }
751 } finally {
752 provisionedSubscribersWriteLock.unlock();
753 }
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800754 log.debug("Done clearing up device flows and subscribers");
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700755 }
756
757 @Override
758 public boolean isSubscriberServiceProvisioned(AccessDevicePort cp) {
Andrea Campanella61650a12022-01-24 18:09:44 -0800759 Set<Map.Entry<ServiceKey, Boolean>> subs;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700760 try {
761 provisionedSubscribersReadLock.lock();
Andrea Campanella61650a12022-01-24 18:09:44 -0800762 subs = new HashSet<>(provisionedSubscribers.entrySet());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700763 } finally {
764 provisionedSubscribersReadLock.unlock();
765 }
Andrea Campanella61650a12022-01-24 18:09:44 -0800766
767 for (Map.Entry<ServiceKey, Boolean> entry : subs) {
768 if (entry.getKey().getPort().equals(cp) && entry.getValue()) {
769 return true;
770 }
771 }
772 return false;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700773 }
774
775 @Override
776 public boolean isSubscriberServiceProvisioned(ServiceKey sk) {
777 try {
778 provisionedSubscribersReadLock.lock();
779 Boolean provisioned = provisionedSubscribers.get(sk);
780 if (provisioned == null || !provisioned) {
781 return false;
782 }
783 } finally {
784 provisionedSubscribersReadLock.unlock();
785 }
786 return true;
787 }
788
789 @Override
790 public void updateProvisionedSubscriberStatus(ServiceKey sk, Boolean status) {
791 try {
792 provisionedSubscribersWriteLock.lock();
793 provisionedSubscribers.put(sk, status);
794 } finally {
795 provisionedSubscribersWriteLock.unlock();
796 }
797 }
798
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800799 protected boolean handleEapolFlow(DiscoveredSubscriber sub, String bandwidthProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700800 String oltBandwidthProfile, FlowOperation action, VlanId vlanId) {
801
802 // create a subscriberKey for the EAPOL flow
803 ServiceKey sk = new ServiceKey(new AccessDevicePort(sub.port), defaultEapolUniTag);
804
805 // NOTE we only need to keep track of the default EAPOL flow in the
806 // connectpoint status map
807 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
808 OltFlowsStatus status = action == FlowOperation.ADD ?
809 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
yasin sapli0823c932022-01-26 11:26:09 +0000810 updateConnectPointStatus(sk, status, OltFlowsStatus.NONE, OltFlowsStatus.NONE, OltFlowsStatus.NONE);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700811
812 }
813
814 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
815 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
816
817 int techProfileId = getDefaultTechProfileId(sub.port);
818 MeterId meterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), bandwidthProfile);
819
820 // in the delete case the meter should still be there as we remove
821 // the meters only if no flows are pointing to them
822 if (meterId == null) {
823 log.debug("MeterId is null for BandwidthProfile {} on device {}",
824 bandwidthProfile, sub.device.id());
825 return false;
826 }
827
828 MeterId oltMeterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), oltBandwidthProfile);
829 if (oltMeterId == null) {
830 log.debug("MeterId is null for OltBandwidthProfile {} on device {}",
831 oltBandwidthProfile, sub.device.id());
832 return false;
833 }
834
835 log.info("{} EAPOL flow for {} with vlanId {} and BandwidthProfile {} (meterId {})",
836 flowOpToString(action), portWithName(sub.port), vlanId, bandwidthProfile, meterId);
837
838 FilteringObjective.Builder eapolAction;
839
840 if (action == FlowOperation.ADD) {
841 eapolAction = filterBuilder.permit();
842 } else if (action == FlowOperation.REMOVE) {
843 eapolAction = filterBuilder.deny();
844 } else {
845 log.error("Operation {} not supported", action);
846 return false;
847 }
848
849 FilteringObjective.Builder baseEapol = eapolAction
850 .withKey(Criteria.matchInPort(sub.port.number()))
851 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()));
852
853 // NOTE we only need to add the treatment to install the flow,
854 // we can remove it based in the match
855 FilteringObjective.Builder eapol;
856
857 TrafficTreatment treatment = treatmentBuilder
858 .meter(meterId)
859 .writeMetadata(createTechProfValueForWriteMetadata(
860 vlanId,
861 techProfileId, oltMeterId), 0)
862 .setOutput(PortNumber.CONTROLLER)
863 .pushVlan()
864 .setVlanId(vlanId)
865 .build();
866 eapol = baseEapol
867 .withMeta(treatment);
868
869 FilteringObjective eapolObjective = eapol
870 .fromApp(appId)
871 .withPriority(MAX_PRIORITY)
872 .add(new ObjectiveContext() {
873 @Override
874 public void onSuccess(Objective objective) {
875 log.info("EAPOL flow objective {} for {}",
876 completeFlowOpToString(action), portWithName(sub.port));
877 if (log.isTraceEnabled()) {
878 log.trace("EAPOL flow details for port {}: {}", portWithName(sub.port), objective);
879 }
880 }
881
882 @Override
883 public void onError(Objective objective, ObjectiveError error) {
884 log.error("Cannot {} eapol flow for {} : {}", action,
885 portWithName(sub.port), error);
886
887 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
888 updateConnectPointStatus(sk,
yasin sapli0823c932022-01-26 11:26:09 +0000889 OltFlowsStatus.ERROR, null, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700890 }
891 }
892 });
893
894 flowObjectiveService.filter(sub.device.id(), eapolObjective);
895
896 log.info("{} EAPOL filter for {}", completeFlowOpToString(action), portWithName(sub.port));
897 return true;
898 }
899
900 // FIXME it's confusing that si is not necessarily inside the DiscoveredSubscriber
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800901 protected boolean handleSubscriberEapolFlows(DiscoveredSubscriber sub, FlowOperation action,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700902 SubscriberAndDeviceInformation si) {
903 if (!enableEapol) {
904 return true;
905 }
906 // TODO verify we need an EAPOL flow for EACH service
907 AtomicBoolean success = new AtomicBoolean(true);
908 si.uniTagList().forEach(u -> {
909 // we assume that if subscriber flows are installed, tagged EAPOL is installed as well
910 boolean hasFlows = hasSubscriberFlows(sub.port, u);
911
912 // if the subscriber flows are present the EAPOL flow is present thus we don't need to install it,
913 // if the subscriber does not have flows the EAPOL flow is not present thus we don't need to remove it
914 if (action == FlowOperation.ADD && hasFlows ||
915 action == FlowOperation.REMOVE && !hasFlows) {
916 log.debug("Not {} EAPOL on {}({}) as subscriber flow status is {}", flowOpToString(action),
917 portWithName(sub.port), u.getServiceName(), hasFlows);
918 return;
919 }
920 log.info("{} EAPOL flows for subscriber {} on {} and service {}",
921 flowOpToString(action), si.id(), portWithName(sub.port), u.getServiceName());
922 if (!handleEapolFlow(sub, u.getUpstreamBandwidthProfile(),
923 u.getUpstreamOltBandwidthProfile(),
924 action, u.getPonCTag())) {
925 //
926 log.error("Failed to {} EAPOL with suscriber tags", action);
927 //TODO this sets it for all services, maybe some services succeeded.
928 success.set(false);
929 }
930 });
931 return success.get();
932 }
933
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800934 protected void handleSubscriberIgmpFlows(DiscoveredSubscriber sub, FlowOperation action) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700935 sub.subscriberAndDeviceInformation.uniTagList().forEach(uti -> {
936 if (uti.getIsIgmpRequired()) {
937 DeviceId deviceId = sub.device.id();
938 // if we reached here a meter already exists
939 MeterId meterId = oltMeterService
940 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
941 MeterId oltMeterId = oltMeterService
942 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
943
944 processIgmpFilteringObjectives(deviceId, sub.port,
945 action, FlowDirection.UPSTREAM, meterId, oltMeterId, uti.getTechnologyProfileId(),
946 uti.getPonCTag(), uti.getUniTagMatch(), uti.getUsPonCTagPriority());
947 }
948 });
949 }
950
951 private boolean checkSadisRunning() {
952 if (bpService == null) {
953 log.warn("Sadis is not running");
954 return false;
955 }
956 return true;
957 }
958
959 private int getDefaultTechProfileId(Port port) {
960 if (!checkSadisRunning()) {
961 return defaultTechProfileId;
962 }
963 if (port != null) {
964 SubscriberAndDeviceInformation info = subsService.get(getPortName(port));
965 if (info != null && info.uniTagList().size() == 1) {
966 return info.uniTagList().get(0).getTechnologyProfileId();
967 }
968 }
969 return defaultTechProfileId;
970 }
971
972 protected Long createTechProfValueForWriteMetadata(VlanId cVlan, int techProfileId, MeterId upstreamOltMeterId) {
973 Long writeMetadata;
974
975 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
976 writeMetadata = (long) techProfileId << 32;
977 } else {
978 writeMetadata = ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
979 }
980 if (upstreamOltMeterId == null) {
981 return writeMetadata;
982 } else {
983 return writeMetadata | upstreamOltMeterId.id();
984 }
985 }
986
987 private void processLldpFilteringObjective(DeviceId deviceId, Port port, FlowOperation action) {
988 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
989
990 FilteringObjective lldp = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
991 .withKey(Criteria.matchInPort(port.number()))
992 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
993 .withMeta(DefaultTrafficTreatment.builder()
994 .setOutput(PortNumber.CONTROLLER).build())
995 .fromApp(appId)
996 .withPriority(MAX_PRIORITY)
997 .add(new ObjectiveContext() {
998 @Override
999 public void onSuccess(Objective objective) {
1000 log.info("{} LLDP filter for {}.", completeFlowOpToString(action), portWithName(port));
1001 }
1002
1003 @Override
1004 public void onError(Objective objective, ObjectiveError error) {
1005 log.error("Falied to {} LLDP filter on {} because {}", action, portWithName(port),
1006 error);
1007 }
1008 });
1009
1010 flowObjectiveService.filter(deviceId, lldp);
1011 }
1012
1013 protected void handleSubscriberDhcpFlows(DeviceId deviceId, Port port,
1014 FlowOperation action,
1015 SubscriberAndDeviceInformation si) {
1016 si.uniTagList().forEach(uti -> {
1017
1018 if (!uti.getIsDhcpRequired()) {
1019 return;
1020 }
1021
1022 // if it's an ADD skip if flows are there,
1023 // if it's a DELETE skip if flows are not there
1024 boolean hasFlows = hasDhcpFlows(port, uti);
1025 if (action == FlowOperation.ADD && hasFlows ||
1026 action == FlowOperation.REMOVE && !hasFlows) {
1027 log.debug("Not dealing with DHCP {} on {} as DHCP flow status is {}", action,
1028 uti.getServiceName(), hasFlows);
1029 return;
1030 }
1031
1032 log.info("{} DHCP flows for subscriber on {} and service {}",
1033 flowOpToString(action), portWithName(port), uti.getServiceName());
1034
1035 // if we reached here a meter already exists
1036 MeterId meterId = oltMeterService
1037 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1038 MeterId oltMeterId = oltMeterService
1039 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1040
1041 if (enableDhcpV4) {
1042 processDhcpFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, 68, 67,
1043 EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP, meterId, oltMeterId,
1044 uti);
1045 }
1046 if (enableDhcpV6) {
1047 log.error("DHCP V6 not supported for subscribers");
1048 }
1049 });
1050 }
1051
yasin sapli0823c932022-01-26 11:26:09 +00001052 protected void handleSubscriberPppoeFlows(DeviceId deviceId, Port port,
1053 FlowOperation action,
1054 SubscriberAndDeviceInformation si) {
1055 si.uniTagList().forEach(uti -> {
1056
1057 if (!uti.getIsPppoeRequired()) {
1058 return;
1059 }
1060
1061 // if it's an ADD skip if flows are there,
1062 // if it's a DELETE skip if flows are not there
1063 boolean hasFlows = hasPppoeFlows(port, uti);
1064 if (action == FlowOperation.ADD && hasFlows ||
1065 action == FlowOperation.REMOVE && !hasFlows) {
1066 log.debug("Not dealing with PPPoE {} on {} as PPPoE flow status is {}", action,
1067 uti.getServiceName(), hasFlows);
1068 return;
1069 }
1070
1071 log.info("{} PPPoE flows for subscriber on {} and service {}",
1072 flowOpToString(action), portWithName(port), uti.getServiceName());
1073
1074 // if we reached here a meter already exists
1075 MeterId meterId = oltMeterService
1076 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1077 MeterId oltMeterId = oltMeterService
1078 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1079
1080 if (enablePppoe) {
1081 processPPPoEDFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, meterId, oltMeterId,
1082 uti.getTechnologyProfileId(), uti.getPonCTag(), uti.getUniTagMatch(),
1083 (byte) uti.getUsPonCTagPriority());
1084 }
1085 });
1086 }
1087
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001088 // FIXME return boolean, if this fails we need to retry
1089 protected void handleSubscriberDataFlows(Device device, Port port,
1090 FlowOperation action,
1091 SubscriberAndDeviceInformation si, String multicastServiceName) {
1092
1093 Optional<Port> nniPort = oltDeviceService.getNniPort(device);
Matteo Scandolo97449bb2021-12-09 15:33:46 -08001094 if (nniPort == null || nniPort.isEmpty()) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001095 log.error("Cannot configure DP flows as upstream port is not configured for subscriber {} on {}",
1096 si.id(), portWithName(port));
1097 return;
1098 }
1099 si.uniTagList().forEach(uti -> {
1100
1101 boolean hasFlows = hasSubscriberFlows(port, uti);
1102 if (action == FlowOperation.ADD && hasFlows ||
1103 action == FlowOperation.REMOVE && !hasFlows) {
1104 log.debug("Not dealing with DP flows {} on {} as subscriber flow status is {}", action,
1105 uti.getServiceName(), hasFlows);
1106 return;
1107 }
1108
1109 if (multicastServiceName.equals(uti.getServiceName())) {
1110 log.debug("This is the multicast service ({}) for subscriber {} on {}, " +
1111 "dataplane flows are not needed",
1112 uti.getServiceName(), si.id(), portWithName(port));
1113 return;
1114 }
1115
1116 log.info("{} Data plane flows for subscriber {} on {} and service {}",
1117 flowOpToString(action), si.id(), portWithName(port), uti.getServiceName());
Matteo Scandolo80f5e972021-12-02 14:59:15 -08001118 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1119 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1120 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
yasin sapli0823c932022-01-26 11:26:09 +00001121 updateConnectPointStatus(sk, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001122
1123 // upstream flows
1124 MeterId usMeterId = oltMeterService
1125 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamBandwidthProfile());
1126 MeterId oltUsMeterId = oltMeterService
1127 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamOltBandwidthProfile());
1128 processUpstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, usMeterId,
1129 oltUsMeterId, uti);
1130
1131 // downstream flows
1132 MeterId dsMeterId = oltMeterService
1133 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamBandwidthProfile());
1134 MeterId oltDsMeterId = oltMeterService
1135 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamOltBandwidthProfile());
1136 processDownstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, dsMeterId,
1137 oltDsMeterId, uti, getMacAddress(device.id(), port, uti));
1138 });
1139 }
1140
1141 private void processDhcpFilteringObjectives(DeviceId deviceId, Port port,
1142 FlowOperation action, FlowDirection direction,
1143 int udpSrc, int udpDst, EthType ethType, byte protocol,
1144 MeterId meterId, MeterId oltMeterId, UniTagInformation uti) {
1145 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1146 log.debug("{} DHCP filtering objectives on {}", flowOpToString(action), sk);
1147
1148
1149 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1150 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
yasin sapli0823c932022-01-26 11:26:09 +00001151 updateConnectPointStatus(sk, null, null, status, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001152
1153 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1154 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1155
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001156 if (meterId != null) {
1157 treatmentBuilder.meter(meterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001158 }
1159
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001160 if (uti.getTechnologyProfileId() != NONE_TP_ID) {
1161 treatmentBuilder.writeMetadata(
1162 createTechProfValueForWriteMetadata(uti.getUniTagMatch(),
1163 uti.getTechnologyProfileId(), oltMeterId), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001164 }
1165
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001166 FilteringObjective.Builder dhcpBuilder = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001167 .withKey(Criteria.matchInPort(port.number()))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001168 .addCondition(Criteria.matchEthType(ethType))
1169 .addCondition(Criteria.matchIPProtocol(protocol))
1170 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1171 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001172 .fromApp(appId)
1173 .withPriority(MAX_PRIORITY);
1174
Andrea Campanella0e34f562020-06-11 10:47:10 +02001175 //VLAN changes and PCP matching need to happen only in the upstream directions
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001176 if (direction == FlowDirection.UPSTREAM) {
1177 treatmentBuilder.setVlanId(uti.getPonCTag());
1178 if (!VlanId.vlanId(VlanId.NO_VID).equals(uti.getUniTagMatch())) {
1179 dhcpBuilder.addCondition(Criteria.matchVlanId(uti.getUniTagMatch()));
Andrea Campanella0e34f562020-06-11 10:47:10 +02001180 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001181 if (uti.getUsPonCTagPriority() != -1) {
1182 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001183 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001184 }
1185
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001186 dhcpBuilder.withMeta(treatmentBuilder
1187 .setOutput(PortNumber.CONTROLLER).build());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001188
1189
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001190 FilteringObjective dhcpUpstream = dhcpBuilder.add(new ObjectiveContext() {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001191 @Override
1192 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001193 log.info("{} DHCP {} filter for {}.",
1194 completeFlowOpToString(action), (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1195 portWithName(port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001196 }
1197
1198 @Override
1199 public void onError(Objective objective, ObjectiveError error) {
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001200 log.error("DHCP {} filter for {} failed {} because {}",
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001201 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1202 portWithName(port),
1203 action,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001204 error);
yasin sapli0823c932022-01-26 11:26:09 +00001205 updateConnectPointStatus(sk, null, null, OltFlowsStatus.ERROR, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001206 }
1207 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001208 flowObjectiveService.filter(deviceId, dhcpUpstream);
1209 }
1210
1211 private void processIgmpFilteringObjectives(DeviceId deviceId, Port port,
1212 FlowOperation action, FlowDirection direction,
1213 MeterId meterId, MeterId oltMeterId, int techProfileId,
1214 VlanId cTag, VlanId unitagMatch, int vlanPcp) {
1215
1216 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
1217 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1218 if (direction == FlowDirection.UPSTREAM) {
1219
1220 if (techProfileId != NONE_TP_ID) {
1221 treatmentBuilder.writeMetadata(createTechProfValueForWriteMetadata(null,
1222 techProfileId, oltMeterId), 0);
1223 }
1224
1225
1226 if (meterId != null) {
1227 treatmentBuilder.meter(meterId);
1228 }
1229
1230 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1231 filterBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1232 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001233 if (!VlanId.vlanId(VlanId.NO_VID).equals(cTag)) {
1234 treatmentBuilder.setVlanId(cTag);
1235 }
1236
1237 if (vlanPcp != -1) {
1238 treatmentBuilder.setVlanPcp((byte) vlanPcp);
1239 }
1240 }
1241
1242 filterBuilder = (action == FlowOperation.ADD) ? filterBuilder.permit() : filterBuilder.deny();
1243
1244 FilteringObjective igmp = filterBuilder
1245 .withKey(Criteria.matchInPort(port.number()))
1246 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1247 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
1248 .withMeta(treatmentBuilder
1249 .setOutput(PortNumber.CONTROLLER).build())
1250 .fromApp(appId)
1251 .withPriority(MAX_PRIORITY)
1252 .add(new ObjectiveContext() {
1253 @Override
1254 public void onSuccess(Objective objective) {
1255 log.info("Igmp filter for {} {}.", portWithName(port), action);
1256 }
1257
1258 @Override
1259 public void onError(Objective objective, ObjectiveError error) {
1260 log.error("Igmp filter for {} failed {} because {}.", portWithName(port), action,
1261 error);
1262 }
1263 });
1264
1265 flowObjectiveService.filter(deviceId, igmp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001266
1267 }
1268
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001269 private void processPPPoEDFilteringObjectives(DeviceId deviceId, Port port,
1270 FlowOperation action, FlowDirection direction,
1271 MeterId meterId, MeterId oltMeterId, int techProfileId,
1272 VlanId cTag, VlanId unitagMatch, Byte vlanPcp) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001273
1274 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1275 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001276
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001277 if (meterId != null) {
1278 treatmentBuilder.meter(meterId);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001279 }
1280
1281 if (techProfileId != NONE_TP_ID) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001282 treatmentBuilder.writeMetadata(createTechProfValueForWriteMetadata(cTag, techProfileId, oltMeterId), 0);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001283 }
1284
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001285 DefaultFilteringObjective.Builder pppoedBuilder = ((action == FlowOperation.ADD)
1286 ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001287 .withKey(Criteria.matchInPort(port.number()))
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001288 .addCondition(Criteria.matchEthType(EthType.EtherType.PPPoED.ethType()))
1289 .fromApp(appId)
1290 .withPriority(10000);
1291
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001292 if (direction == FlowDirection.UPSTREAM) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001293 treatmentBuilder.setVlanId(cTag);
1294 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1295 pppoedBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1296 }
1297 if (vlanPcp != null) {
1298 treatmentBuilder.setVlanPcp(vlanPcp);
1299 }
1300 }
1301 pppoedBuilder = pppoedBuilder.withMeta(treatmentBuilder.setOutput(PortNumber.CONTROLLER).build());
1302
1303 FilteringObjective pppoed = pppoedBuilder
1304 .add(new ObjectiveContext() {
1305 @Override
1306 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001307 log.info("PPPoED filter for {} {}.", portWithName(port), action);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001308 }
1309
1310 @Override
1311 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001312 log.info("PPPoED filter for {} failed {} because {}", portWithName(port),
1313 action, error);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001314 }
1315 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001316 flowObjectiveService.filter(deviceId, pppoed);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001317 }
1318
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001319 private void processUpstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1320 FlowOperation action,
1321 MeterId upstreamMeterId,
1322 MeterId upstreamOltMeterId,
1323 UniTagInformation uti) {
1324 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001325 TrafficSelector selector = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001326 .matchInPort(port.number())
1327 .matchVlanId(uti.getUniTagMatch())
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001328 .build();
1329
Andrea Campanella327c5722020-01-30 11:34:13 +01001330 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1331 //if the subscriberVlan (cTag) is different than ANY it needs to set.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001332 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanella327c5722020-01-30 11:34:13 +01001333 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001334 .setVlanId(uti.getPonCTag());
Andrea Campanella327c5722020-01-30 11:34:13 +01001335 }
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001336 if (uti.getPonSTag().toShort() == VlanId.ANY_VALUE) {
1337 treatmentBuilder.popVlan();
1338 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001339
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001340 if (uti.getUsPonCTagPriority() != -1) {
1341 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001342
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001343 }
1344
1345 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001346 .setVlanId(uti.getPonSTag());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001347
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001348 if (uti.getUsPonSTagPriority() != -1) {
1349 treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001350 }
1351
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001352 treatmentBuilder.setOutput(nniPort.number())
1353 .writeMetadata(createMetadata(uti.getPonCTag(),
1354 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001355
yasin saplib4b8ee12021-06-13 18:25:20 +00001356 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1357
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001358 if (upstreamMeterId != null) {
1359 treatmentBuilder.meter(upstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001360 annotationBuilder.set(UPSTREAM_ONU, upstreamMeterId.toString());
1361 }
1362 if (upstreamOltMeterId != null) {
1363 treatmentBuilder.meter(upstreamOltMeterId);
1364 annotationBuilder.set(UPSTREAM_OLT, upstreamOltMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001365 }
1366
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001367 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selector,
1368 treatmentBuilder.build(), MIN_PRIORITY,
yasin saplib4b8ee12021-06-13 18:25:20 +00001369 annotationBuilder.build());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001370
1371 ObjectiveContext context = new ObjectiveContext() {
1372 @Override
1373 public void onSuccess(Objective objective) {
1374 log.info("{} Upstream Data plane filter for {}.",
1375 completeFlowOpToString(action), sk);
1376 }
1377
1378 @Override
1379 public void onError(Objective objective, ObjectiveError error) {
1380 log.error("Upstream Data plane filter for {} failed {} because {}.",
1381 sk, action, error);
yasin sapli0823c932022-01-26 11:26:09 +00001382 updateConnectPointStatus(sk, null, OltFlowsStatus.ERROR, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001383 }
1384 };
1385
1386 ForwardingObjective flow = null;
1387 if (action == FlowOperation.ADD) {
1388 flow = flowBuilder.add(context);
1389 } else if (action == FlowOperation.REMOVE) {
1390 flow = flowBuilder.remove(context);
1391 } else {
1392 log.error("Flow action not supported: {}", action);
1393 }
1394
1395 if (flow != null) {
1396 flowObjectiveService.forward(deviceId, flow);
1397 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001398 }
1399
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001400 private void processDownstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1401 FlowOperation action,
1402 MeterId downstreamMeterId,
1403 MeterId downstreamOltMeterId,
1404 UniTagInformation uti,
1405 MacAddress macAddress) {
1406 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanella327c5722020-01-30 11:34:13 +01001407 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001408 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001409 .matchVlanId(uti.getPonSTag())
1410 .matchInPort(nniPort.number())
1411 .matchInnerVlanId(uti.getPonCTag());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001412
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001413 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1414 selectorBuilder.matchMetadata(uti.getPonCTag().toShort());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001415 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001416
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001417 if (uti.getDsPonCTagPriority() != -1) {
1418 selectorBuilder.matchVlanPcp((byte) uti.getDsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001419 }
1420
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001421 if (macAddress != null) {
1422 selectorBuilder.matchEthDst(macAddress);
1423 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001424
1425 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
1426 .popVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001427 .setOutput(port.number());
Andrea Campanella327c5722020-01-30 11:34:13 +01001428
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001429 treatmentBuilder.writeMetadata(createMetadata(uti.getPonCTag(),
1430 uti.getTechnologyProfileId(),
1431 port.number()), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001432
Andrea Campanella981e86c2021-03-12 11:35:33 +01001433 // Upstream pbit is used to remark inner vlan pbit.
1434 // Upstream is used to avoid trusting the BNG to send the packet with correct pbit.
1435 // this is done because ds mode 0 is used because ds mode 3 or 6 that allow for
1436 // all pbit acceptance are not widely supported by vendors even though present in
1437 // the OMCI spec.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001438 if (uti.getUsPonCTagPriority() != -1) {
1439 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001440 }
1441
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001442 if (!VlanId.NONE.equals(uti.getUniTagMatch()) &&
1443 uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1444 treatmentBuilder.setVlanId(uti.getUniTagMatch());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001445 }
1446
yasin saplib4b8ee12021-06-13 18:25:20 +00001447 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1448
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001449 if (downstreamMeterId != null) {
1450 treatmentBuilder.meter(downstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001451 annotationBuilder.set(DOWNSTREAM_ONU, downstreamMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001452 }
1453
yasin saplib4b8ee12021-06-13 18:25:20 +00001454 if (downstreamOltMeterId != null) {
1455 treatmentBuilder.meter(downstreamOltMeterId);
1456 annotationBuilder.set(DOWNSTREAM_OLT, downstreamOltMeterId.toString());
1457 }
1458
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001459 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1460 treatmentBuilder.build(), MIN_PRIORITY, annotationBuilder.build());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001461
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001462 ObjectiveContext context = new ObjectiveContext() {
1463 @Override
1464 public void onSuccess(Objective objective) {
1465 log.info("{} Downstream Data plane filter for {}.",
1466 completeFlowOpToString(action), sk);
1467 }
1468
1469 @Override
1470 public void onError(Objective objective, ObjectiveError error) {
1471 log.info("Downstream Data plane filter for {} failed {} because {}.",
1472 sk, action, error);
yasin sapli0823c932022-01-26 11:26:09 +00001473 updateConnectPointStatus(sk, null, OltFlowsStatus.ERROR, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001474 }
1475 };
1476
1477 ForwardingObjective flow = null;
1478 if (action == FlowOperation.ADD) {
1479 flow = flowBuilder.add(context);
1480 } else if (action == FlowOperation.REMOVE) {
1481 flow = flowBuilder.remove(context);
1482 } else {
1483 log.error("Flow action not supported: {}", action);
1484 }
1485
1486 if (flow != null) {
1487 if (log.isTraceEnabled()) {
1488 log.trace("Forwarding rule {}", flow);
1489 }
1490 flowObjectiveService.forward(deviceId, flow);
1491 }
Andrea Campanella600d2e22020-06-22 11:00:31 +02001492 }
1493
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001494 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
1495 TrafficTreatment treatment,
yasin saplib4b8ee12021-06-13 18:25:20 +00001496 Integer priority,
1497 Annotations annotations) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001498 return DefaultForwardingObjective.builder()
1499 .withFlag(ForwardingObjective.Flag.VERSATILE)
1500 .withPriority(priority)
1501 .makePermanent()
1502 .withSelector(selector)
yasin saplib4b8ee12021-06-13 18:25:20 +00001503 .withAnnotations(annotations)
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001504 .fromApp(appId)
1505 .withTreatment(treatment);
1506 }
1507
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001508 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
1509 if (techProfileId == NONE_TP_ID) {
Andrea Campanella7c49b792020-05-11 11:36:53 +02001510 techProfileId = DEFAULT_TP_ID_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001511 }
1512
1513 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
1514 }
1515
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001516 private boolean isMacLearningEnabled(SubscriberAndDeviceInformation si) {
1517 AtomicBoolean requiresMacLearning = new AtomicBoolean();
1518 requiresMacLearning.set(false);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001519
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001520 si.uniTagList().forEach(uniTagInfo -> {
1521 if (uniTagInfo.getEnableMacLearning()) {
1522 requiresMacLearning.set(true);
1523 }
1524 });
1525
1526 return requiresMacLearning.get();
1527 }
1528
1529 /**
1530 * Checks whether the subscriber has the MacAddress configured or discovered.
1531 *
1532 * @param deviceId DeviceId for this subscriber
1533 * @param port Port for this subscriber
1534 * @param si SubscriberAndDeviceInformation
1535 * @return boolean
1536 */
1537 protected boolean isMacAddressAvailable(DeviceId deviceId, Port port, SubscriberAndDeviceInformation si) {
1538 AtomicBoolean isConfigured = new AtomicBoolean();
1539 isConfigured.set(true);
1540
1541 si.uniTagList().forEach(uniTagInfo -> {
1542 boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning();
1543 boolean configureMac = isMacAddressValid(uniTagInfo);
1544 boolean discoveredMac = false;
1545 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1546 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1547 if (optHost.isPresent() && optHost.get().mac() != null) {
1548 discoveredMac = true;
1549 }
1550 if (isMacLearningEnabled && !configureMac && !discoveredMac) {
1551 log.debug("Awaiting for macAddress on {} for service {}",
1552 portWithName(port), uniTagInfo.getServiceName());
1553 isConfigured.set(false);
1554 }
1555 });
1556
1557 return isConfigured.get();
1558 }
1559
1560 protected MacAddress getMacAddress(DeviceId deviceId, Port port, UniTagInformation uniTagInfo) {
1561 boolean configuredMac = isMacAddressValid(uniTagInfo);
1562 if (configuredMac) {
1563 return MacAddress.valueOf(uniTagInfo.getConfiguredMacAddress());
1564 } else if (uniTagInfo.getEnableMacLearning()) {
1565 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1566 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1567 if (optHost.isPresent() && optHost.get().mac() != null) {
1568 return optHost.get().mac();
1569 }
1570 }
1571 return null;
1572 }
1573
1574 private boolean isMacAddressValid(UniTagInformation tagInformation) {
1575 return tagInformation.getConfiguredMacAddress() != null &&
1576 !tagInformation.getConfiguredMacAddress().trim().equals("") &&
1577 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
1578 }
1579
1580 protected void updateConnectPointStatus(ServiceKey key, OltFlowsStatus eapolStatus,
yasin sapli0823c932022-01-26 11:26:09 +00001581 OltFlowsStatus subscriberFlowsStatus, OltFlowsStatus dhcpStatus,
1582 OltFlowsStatus pppoeStatus) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001583 if (log.isTraceEnabled()) {
1584 log.trace("Updating cpStatus {} with values: eapolFlow={}, subscriberFlows={}, dhcpFlow={}",
1585 key, eapolStatus, subscriberFlowsStatus, dhcpStatus);
1586 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001587 try {
1588 cpStatusWriteLock.lock();
1589 OltPortStatus status = cpStatus.get(key);
1590
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001591
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001592 if (status == null) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001593 // if we don't have status for the connectPoint
1594 // and we're only updating status to PENDING_REMOVE or ERROR
1595 // do not create it. This is because this case will only happen when a device is removed
1596 // and it's status cleaned
1597 List<OltFlowsStatus> statusesToIgnore = new ArrayList<>();
1598 statusesToIgnore.add(OltFlowsStatus.PENDING_REMOVE);
1599 statusesToIgnore.add(OltFlowsStatus.ERROR);
1600
1601 if (
1602 (statusesToIgnore.contains(subscriberFlowsStatus) && dhcpStatus == null) ||
1603 (subscriberFlowsStatus == null && statusesToIgnore.contains(dhcpStatus))
1604 ) {
1605 if (log.isTraceEnabled()) {
1606 log.trace("Ignoring cpStatus update as status is meaningless");
1607 }
1608 return;
1609 }
1610
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001611 status = new OltPortStatus(
1612 eapolStatus != null ? eapolStatus : OltFlowsStatus.NONE,
1613 subscriberFlowsStatus != null ? subscriberFlowsStatus : OltFlowsStatus.NONE,
yasin sapli0823c932022-01-26 11:26:09 +00001614 dhcpStatus != null ? dhcpStatus : OltFlowsStatus.NONE,
1615 pppoeStatus != null ? pppoeStatus : OltFlowsStatus.NONE
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001616 );
1617 } else {
1618 if (eapolStatus != null) {
1619 status.defaultEapolStatus = eapolStatus;
1620 }
1621 if (subscriberFlowsStatus != null) {
1622 status.subscriberFlowsStatus = subscriberFlowsStatus;
1623 }
1624 if (dhcpStatus != null) {
1625 status.dhcpStatus = dhcpStatus;
1626 }
1627 }
1628
1629 cpStatus.put(key, status);
1630 } finally {
1631 cpStatusWriteLock.unlock();
1632 }
1633 }
1634
1635 protected class InternalFlowListener implements FlowRuleListener {
1636 @Override
1637 public void event(FlowRuleEvent event) {
1638 if (appId.id() != (event.subject().appId())) {
1639 return;
1640 }
1641
1642 if (!oltDeviceService.isLocalLeader(event.subject().deviceId())) {
1643 if (log.isTraceEnabled()) {
1644 log.trace("ignoring flow event {} " +
1645 "as not leader for {}", event, event.subject().deviceId());
1646 }
1647 return;
1648 }
1649
1650 switch (event.type()) {
1651 case RULE_ADDED:
1652 case RULE_REMOVED:
Andrea Campanella40d2b342022-02-04 18:13:37 +01001653 DeviceId deviceId = event.subject().deviceId();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001654 Port port = getCpFromFlowRule(event.subject());
1655 if (port == null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001656 log.warn("Port is gone in ONOS, " +
1657 "manually creating it {}", event.subject());
1658 PortNumber inPort = getPortNumberFromFlowRule(event.subject());
1659 cpStatusReadLock.lock();
1660 Optional<ServiceKey> keyWithPort = cpStatus.keySet()
1661 .stream().filter(key -> key.getPort().connectPoint()
1662 .deviceId().equals(deviceId)
1663 && key.getPort().connectPoint().port()
1664 .equals(inPort)).findFirst();
1665 cpStatusReadLock.unlock();
1666 if (keyWithPort.isPresent()) {
1667 port = new DefaultPort(deviceService.getDevice(deviceId),
1668 inPort, false,
1669 DefaultAnnotations.builder()
1670 .set(AnnotationKeys.PORT_NAME,
1671 keyWithPort.get().getPort().name())
1672 .build());
1673 } else {
1674 log.warn("Can't find corresponding status for {}/{}", deviceId, inPort);
1675 return;
1676 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001677 }
1678 if (log.isTraceEnabled()) {
1679 log.trace("flow event {} on cp {}: {}", event.type(),
1680 portWithName(port), event.subject());
1681 }
1682 updateCpStatus(event.type(), port, event.subject());
1683 return;
1684 case RULE_ADD_REQUESTED:
1685 case RULE_REMOVE_REQUESTED:
1686 // NOTE that PENDING_ADD/REMOVE is set when we create the flowObjective
1687 return;
1688 default:
1689 return;
1690 }
1691 }
1692
1693 protected void updateCpStatus(FlowRuleEvent.Type type, Port port, FlowRule flowRule) {
1694 OltFlowsStatus status = flowRuleStatusToOltFlowStatus(type);
1695 if (isDefaultEapolFlow(flowRule)) {
1696 ServiceKey sk = new ServiceKey(new AccessDevicePort(port),
1697 defaultEapolUniTag);
1698 if (log.isTraceEnabled()) {
1699 log.trace("update defaultEapolStatus {} on {}", status, sk);
1700 }
yasin sapli0823c932022-01-26 11:26:09 +00001701 updateConnectPointStatus(sk, status, null, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001702 } else if (isDhcpFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001703 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001704 if (sk == null) {
1705 return;
1706 }
1707 if (log.isTraceEnabled()) {
1708 log.trace("update dhcpStatus {} on {}", status, sk);
1709 }
yasin sapli0823c932022-01-26 11:26:09 +00001710 updateConnectPointStatus(sk, null, null, status, null);
1711 } else if (isPppoeFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001712 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
yasin sapli0823c932022-01-26 11:26:09 +00001713 if (sk == null) {
1714 return;
1715 }
1716 if (log.isTraceEnabled()) {
1717 log.trace("update pppoeStatus {} on {}", status, sk);
1718 }
1719 updateConnectPointStatus(sk, null, null, null, status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001720 } else if (isDataFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001721 PortNumber number = getPortNumberFromFlowRule(flowRule);
1722 if (number == null) {
1723 log.error("Can't capture the port number from flow {}", flowRule);
1724 return;
1725 }
1726 if (oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), number)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001727 // the NNI has data-plane for every subscriber, doesn't make sense to track them
1728 return;
1729 }
1730
Andrea Campanella40d2b342022-02-04 18:13:37 +01001731 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001732 if (sk == null) {
1733 return;
1734 }
1735 if (log.isTraceEnabled()) {
1736 log.trace("update dataplaneStatus {} on {}", status, sk);
1737 }
yasin sapli0823c932022-01-26 11:26:09 +00001738 updateConnectPointStatus(sk, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001739 }
1740 }
1741
1742 private boolean isDefaultEapolFlow(FlowRule flowRule) {
1743 EthTypeCriterion c = (EthTypeCriterion) flowRule.selector().getCriterion(Criterion.Type.ETH_TYPE);
1744 if (c == null) {
1745 return false;
1746 }
1747 if (c.ethType().equals(EthType.EtherType.EAPOL.ethType())) {
1748 AtomicBoolean isDefault = new AtomicBoolean(false);
1749 flowRule.treatment().allInstructions().forEach(instruction -> {
1750 if (instruction.type() == L2MODIFICATION) {
1751 L2ModificationInstruction modificationInstruction = (L2ModificationInstruction) instruction;
1752 if (modificationInstruction.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
1753 L2ModificationInstruction.ModVlanIdInstruction vlanInstruction =
1754 (L2ModificationInstruction.ModVlanIdInstruction) modificationInstruction;
1755 if (vlanInstruction.vlanId().id().equals(EAPOL_DEFAULT_VLAN)) {
1756 isDefault.set(true);
1757 return;
1758 }
1759 }
1760 }
1761 });
1762 return isDefault.get();
1763 }
1764 return false;
1765 }
1766
1767 /**
1768 * Returns true if the flow is a DHCP flow.
1769 * Matches both upstream and downstream flows.
1770 *
1771 * @param flowRule The FlowRule to evaluate
1772 * @return boolean
1773 */
1774 private boolean isDhcpFlow(FlowRule flowRule) {
1775 IPProtocolCriterion ipCriterion = (IPProtocolCriterion) flowRule.selector()
1776 .getCriterion(Criterion.Type.IP_PROTO);
1777 if (ipCriterion == null) {
1778 return false;
1779 }
1780
1781 UdpPortCriterion src = (UdpPortCriterion) flowRule.selector().getCriterion(Criterion.Type.UDP_SRC);
1782
1783 if (src == null) {
1784 return false;
1785 }
1786 return ipCriterion.protocol() == IPv4.PROTOCOL_UDP &&
1787 (src.udpPort().toInt() == 68 || src.udpPort().toInt() == 67);
1788 }
1789
yasin sapli0823c932022-01-26 11:26:09 +00001790 private boolean isPppoeFlow(FlowRule flowRule) {
1791 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) flowRule.selector()
1792 .getCriterion(Criterion.Type.ETH_TYPE);
1793
1794 if (ethTypeCriterion == null) {
1795 return false;
1796 }
1797 return EthType.EtherType.PPPoED.ethType().equals(ethTypeCriterion.ethType());
1798 }
1799
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001800 private boolean isDataFlow(FlowRule flowRule) {
1801 // we consider subscriber flows the one that matches on VLAN_VID
1802 // method is valid only because it's the last check after EAPOL and DHCP.
1803 // this matches mcast flows as well, if we want to avoid that we can
1804 // filter out the elements that have groups in the treatment or
1805 // mcastIp in the selector
1806 // IPV4_DST:224.0.0.22/32
1807 // treatment=[immediate=[GROUP:0x1]]
1808
1809 return flowRule.selector().getCriterion(Criterion.Type.VLAN_VID) != null;
1810 }
1811
1812 private Port getCpFromFlowRule(FlowRule flowRule) {
1813 DeviceId deviceId = flowRule.deviceId();
Andrea Campanella40d2b342022-02-04 18:13:37 +01001814 PortNumber inPort = getPortNumberFromFlowRule(flowRule);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001815 if (inPort != null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001816 return deviceService.getPort(deviceId, inPort);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001817 }
1818 return null;
1819 }
1820
Andrea Campanella40d2b342022-02-04 18:13:37 +01001821 private PortNumber getPortNumberFromFlowRule(FlowRule flowRule) {
1822 PortCriterion inPort = (PortCriterion) flowRule.selector().getCriterion(Criterion.Type.IN_PORT);
1823 if (inPort != null) {
1824 return inPort.port();
1825 }
1826 return null;
1827 }
1828
1829 private ServiceKey getSubscriberKeyFromFlowRule(FlowRule flowRule, Port flowPort) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001830 SubscriberAndDeviceInformation si = subsService.get(getPortName(flowPort));
1831
1832 Boolean isNni = oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), flowPort.number());
1833 if (si == null && !isNni) {
1834 log.error("Subscriber information not found in sadis for port {}", portWithName(flowPort));
1835 return null;
1836 }
1837
1838 if (isNni) {
1839 return new ServiceKey(new AccessDevicePort(flowPort), nniUniTag);
1840 }
1841
1842 Optional<UniTagInformation> found = Optional.empty();
1843 VlanId flowVlan = null;
1844 if (isDhcpFlow(flowRule)) {
1845 // we need to make a special case for DHCP as in the ATT workflow DHCP flows don't match on tags
1846 L2ModificationInstruction.ModVlanIdInstruction instruction =
1847 (L2ModificationInstruction.ModVlanIdInstruction) flowRule.treatment().immediate().get(1);
1848 flowVlan = instruction.vlanId();
1849 } else {
1850 // for now we assume that if it's not DHCP it's dataplane (or at least tagged)
1851 VlanIdCriterion vlanIdCriterion =
1852 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
1853 if (vlanIdCriterion == null) {
1854 log.warn("cannot match the flow to a subscriber service as it does not carry vlans");
1855 return null;
1856 }
1857 flowVlan = vlanIdCriterion.vlanId();
1858 }
1859
1860 VlanId finalFlowVlan = flowVlan;
1861 found = si.uniTagList().stream().filter(uti ->
1862 uti.getPonCTag().equals(finalFlowVlan) ||
1863 uti.getPonSTag().equals(finalFlowVlan) ||
1864 uti.getUniTagMatch().equals(finalFlowVlan)
1865 ).findFirst();
1866
1867
1868 if (found.isEmpty()) {
1869 log.warn("Cannot map flow rule {} to Service in {}", flowRule, si);
1870 }
1871
1872 return found.isPresent() ? new ServiceKey(new AccessDevicePort(flowPort), found.get()) : null;
1873
1874 }
1875
1876 private OltFlowsStatus flowRuleStatusToOltFlowStatus(FlowRuleEvent.Type type) {
1877 switch (type) {
1878 case RULE_ADD_REQUESTED:
1879 return OltFlowsStatus.PENDING_ADD;
1880 case RULE_ADDED:
1881 return OltFlowsStatus.ADDED;
1882 case RULE_REMOVE_REQUESTED:
1883 return OltFlowsStatus.PENDING_REMOVE;
1884 case RULE_REMOVED:
1885 return OltFlowsStatus.REMOVED;
1886 default:
1887 return OltFlowsStatus.NONE;
1888 }
1889 }
1890 }
1891
1892 protected void bindSadisService(SadisService service) {
1893 this.subsService = service.getSubscriberInfoService();
1894 this.bpService = service.getBandwidthProfileService();
1895 log.info("Sadis service is loaded");
1896 }
1897
1898 protected void unbindSadisService(SadisService service) {
1899 this.subsService = null;
1900 this.bpService = null;
1901 log.info("Sadis service is unloaded");
1902 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001903}