blob: 4fb56df6f75bcd3c3eb5fe0f7f89aed0522b21c6 [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) {
483 return true;
484 }
485
486 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
487 return addDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
488 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
489 return removeDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
490 } else {
491 log.error("Unknown Status {} on DiscoveredSubscriber {}", sub.status, sub);
492 return false;
493 }
494
495 }
496
497 private boolean addDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfileId, String oltBandwidthProfileId) {
498
499 if (!oltMeterService.createMeter(sub.device.id(), bandwidthProfileId)) {
500 if (log.isTraceEnabled()) {
501 log.trace("waiting on meter for bp {} and sub {}", bandwidthProfileId, sub);
502 }
503 return false;
504 }
505 if (hasDefaultEapol(sub.port)) {
506 return true;
507 }
508 return handleEapolFlow(sub, bandwidthProfileId,
509 oltBandwidthProfileId, FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
510
511 }
512
513 private boolean removeDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfile, String oltBandwidthProfile) {
514 // NOTE that we are not checking for meters as they must have been created to install the flow in first place
515 return handleEapolFlow(sub, bandwidthProfile, oltBandwidthProfile,
516 FlowOperation.REMOVE, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
517 }
518
519 @Override
520 public boolean handleSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwidthProfile,
521 String multicastServiceName) {
522 // NOTE that we are taking defaultBandwithProfile as a parameter as that can be configured in the Olt component
523 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
524 return addSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
525 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
526 return removeSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
527 } else {
528 log.error("don't know how to handle {}", sub);
529 return false;
530 }
531 }
532
533 private boolean addSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
534 String multicastServiceName) {
535 if (log.isTraceEnabled()) {
536 log.trace("Provisioning of subscriber on {} started", portWithName(sub.port));
537 }
538 if (enableEapol) {
539 if (hasDefaultEapol(sub.port)) {
540 // remove EAPOL flow and throw exception so that we'll retry later
541 if (!isDefaultEapolPendingRemoval(sub.port)) {
542 removeDefaultFlows(sub, defaultBandwithProfile, defaultBandwithProfile);
543 }
544
545 if (waitForRemoval) {
546 // NOTE wait for removal is a flag only needed to make sure VOLTHA
547 // does not explode with the flows remove/add in the same batch
548 log.debug("Awaiting for default flows removal for {}", portWithName(sub.port));
549 return false;
550 } else {
551 log.warn("continuing provisioning on {}", portWithName(sub.port));
552 }
553 }
554
555 }
556
557 // NOTE createMeters will return if the meters are not installed
558 if (!oltMeterService.createMeters(sub.device.id(),
Matteo Scandolo88df8ae2021-11-23 13:12:29 -0800559 sub.subscriberAndDeviceInformation, multicastServiceName)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700560 return false;
561 }
562
563 // NOTE we need to add the DHCP flow regardless so that the host can be discovered and the MacAddress added
564 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.ADD,
565 sub.subscriberAndDeviceInformation);
566
567 if (isMacLearningEnabled(sub.subscriberAndDeviceInformation)
568 && !isMacAddressAvailable(sub.device.id(), sub.port,
569 sub.subscriberAndDeviceInformation)) {
570 log.debug("Awaiting for macAddress on {}", portWithName(sub.port));
571 return false;
572 }
573
Matteo Scandolo8a91c0f2021-12-03 10:58:14 -0800574 // NOTE that the EAPOL flows handling is based on the data-plane flows status
575 // always process them before
576 handleSubscriberEapolFlows(sub, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
577
yasin sapli0823c932022-01-26 11:26:09 +0000578 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
579
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700580 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.ADD,
581 sub.subscriberAndDeviceInformation, multicastServiceName);
582
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700583 handleSubscriberIgmpFlows(sub, FlowOperation.ADD);
584
585 log.info("Provisioning of subscriber on {} completed", portWithName(sub.port));
586 return true;
587 }
588
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800589 protected boolean removeSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700590 String multicastServiceName) {
591
592 if (log.isTraceEnabled()) {
593 log.trace("Removal of subscriber on {} started",
594 portWithName(sub.port));
595 }
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800596 SubscriberAndDeviceInformation si = sub.subscriberAndDeviceInformation;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700597
598 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.REMOVE, si);
599
yasin sapli0823c932022-01-26 11:26:09 +0000600 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.REMOVE, sub.subscriberAndDeviceInformation);
601
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700602 if (enableEapol) {
603 // remove the tagged eapol
604 handleSubscriberEapolFlows(sub, FlowOperation.REMOVE, si);
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800605 }
606 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.REMOVE, si, multicastServiceName);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700607
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800608 handleSubscriberIgmpFlows(sub, FlowOperation.REMOVE);
609
610 if (enableEapol) {
611
612 // if any of the services still has flows, return false
613 Iterator<UniTagInformation> iter = sub.subscriberAndDeviceInformation.uniTagList().iterator();
614 while (iter.hasNext()) {
615 UniTagInformation entry = iter.next();
616 if (areSubscriberFlowsPendingRemoval(sub.port, entry)) {
617 log.info("Subscriber {} still have flows on service {}, postpone default EAPOL installation.",
618 portWithName(sub.port), entry.getServiceName());
619 return false;
620 }
621 }
622
623 // once the flows are removed add the default one back
624 // (only if the port is ENABLED and still present on the device)
Matteo Scandolo49c42052021-11-23 13:12:29 -0800625 if (sub.port.isEnabled() && deviceService.getPort(sub.device.id(), sub.port.number()) != null) {
626
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700627 // NOTE we remove the subscriber when the port goes down
628 // but in that case we don't need to add default eapol
629 handleEapolFlow(sub, defaultBandwithProfile, defaultBandwithProfile,
630 FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
631 }
632 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700633 // FIXME check the return status of the flow and return accordingly
634 log.info("Removal of subscriber on {} completed", portWithName(sub.port));
635 return true;
636 }
637
638 @Override
639 public boolean hasDefaultEapol(Port port) {
640 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
641 // NOTE we consider ERROR as a present EAPOL flow as ONOS
642 // will keep trying to add it
643 return status != null && (status.defaultEapolStatus == OltFlowsStatus.ADDED ||
644 status.defaultEapolStatus == OltFlowsStatus.PENDING_ADD ||
645 status.defaultEapolStatus == OltFlowsStatus.ERROR);
646 }
647
648 private OltPortStatus getOltPortStatus(Port port, UniTagInformation uniTagInformation) {
649 try {
650 cpStatusReadLock.lock();
651 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uniTagInformation);
652 OltPortStatus status = cpStatus.get(sk);
653 return status;
654 } finally {
655 cpStatusReadLock.unlock();
656 }
657 }
658
659 public boolean isDefaultEapolPendingRemoval(Port port) {
660 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
661 if (log.isTraceEnabled()) {
662 log.trace("Status during EAPOL flow check {} for port {} and UniTagInformation {}",
663 status, portWithName(port), defaultEapolUniTag);
664 }
665 return status != null && status.defaultEapolStatus == OltFlowsStatus.PENDING_REMOVE;
666 }
667
668 @Override
669 public boolean hasDhcpFlows(Port port, UniTagInformation uti) {
670 OltPortStatus status = getOltPortStatus(port, uti);
671 if (log.isTraceEnabled()) {
672 log.trace("Status during DHCP flow check {} for port {} and service {}",
673 status, portWithName(port), uti.getServiceName());
674 }
675 return status != null &&
676 (status.dhcpStatus == OltFlowsStatus.ADDED ||
677 status.dhcpStatus == OltFlowsStatus.PENDING_ADD);
678 }
679
680 @Override
yasin sapli0823c932022-01-26 11:26:09 +0000681 public boolean hasPppoeFlows(Port port, UniTagInformation uti) {
682 OltPortStatus status = getOltPortStatus(port, uti);
683 if (log.isTraceEnabled()) {
684 log.trace("Status during PPPoE flow check {} for port {} and service {}",
685 status, portWithName(port), uti.getServiceName());
686 }
687 return status != null &&
688 (status.pppoeStatus == OltFlowsStatus.ADDED ||
689 status.pppoeStatus == OltFlowsStatus.PENDING_ADD);
690 }
691
692 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700693 public boolean hasSubscriberFlows(Port port, UniTagInformation uti) {
694
695 OltPortStatus status = getOltPortStatus(port, uti);
696 if (log.isTraceEnabled()) {
697 log.trace("Status during subscriber flow check {} for port {} and service {}",
698 status, portWithName(port), uti.getServiceName());
699 }
700 return status != null && (status.subscriberFlowsStatus == OltFlowsStatus.ADDED ||
701 status.subscriberFlowsStatus == OltFlowsStatus.PENDING_ADD);
702 }
703
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800704 public boolean areSubscriberFlowsPendingRemoval(Port port, UniTagInformation uti) {
705 OltPortStatus status = getOltPortStatus(port, uti);
706 if (log.isTraceEnabled()) {
707 log.trace("Status during pending_remove flow check {} for port {} and UniTagInformation {}",
708 status, portWithName(port), uti);
709 }
710 return status != null && status.subscriberFlowsStatus == OltFlowsStatus.PENDING_REMOVE;
711 }
712
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700713 @Override
714 public void purgeDeviceFlows(DeviceId deviceId) {
715 log.debug("Purging flows on device {}", deviceId);
Matteo Scandolob6981dc2021-12-02 16:31:44 -0800716 flowRuleService.purgeFlowRules(deviceId);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700717
718 // removing the status from the cpStatus map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800719 if (log.isTraceEnabled()) {
720 log.trace("Clearing cp status from device {}", deviceId);
721 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700722 try {
723 cpStatusWriteLock.lock();
724 Iterator<Map.Entry<ServiceKey, OltPortStatus>> iter = cpStatus.entrySet().iterator();
725 while (iter.hasNext()) {
726 Map.Entry<ServiceKey, OltPortStatus> entry = iter.next();
727 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
728 cpStatus.remove(entry.getKey());
729 }
730 }
731 } finally {
732 cpStatusWriteLock.unlock();
733 }
734
735 // removing subscribers from the provisioned map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800736 if (log.isTraceEnabled()) {
737 log.trace("Clearing provisioned subscribers from device {}", deviceId);
738 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700739 try {
740 provisionedSubscribersWriteLock.lock();
741 Iterator<Map.Entry<ServiceKey, Boolean>> iter = provisionedSubscribers.entrySet().iterator();
742 while (iter.hasNext()) {
743 Map.Entry<ServiceKey, Boolean> entry = iter.next();
744 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
745 provisionedSubscribers.remove(entry.getKey());
746 }
747 }
748 } finally {
749 provisionedSubscribersWriteLock.unlock();
750 }
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800751 log.debug("Done clearing up device flows and subscribers");
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700752 }
753
754 @Override
755 public boolean isSubscriberServiceProvisioned(AccessDevicePort cp) {
Andrea Campanella61650a12022-01-24 18:09:44 -0800756 Set<Map.Entry<ServiceKey, Boolean>> subs;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700757 try {
758 provisionedSubscribersReadLock.lock();
Andrea Campanella61650a12022-01-24 18:09:44 -0800759 subs = new HashSet<>(provisionedSubscribers.entrySet());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700760 } finally {
761 provisionedSubscribersReadLock.unlock();
762 }
Andrea Campanella61650a12022-01-24 18:09:44 -0800763
764 for (Map.Entry<ServiceKey, Boolean> entry : subs) {
765 if (entry.getKey().getPort().equals(cp) && entry.getValue()) {
766 return true;
767 }
768 }
769 return false;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700770 }
771
772 @Override
773 public boolean isSubscriberServiceProvisioned(ServiceKey sk) {
774 try {
775 provisionedSubscribersReadLock.lock();
776 Boolean provisioned = provisionedSubscribers.get(sk);
777 if (provisioned == null || !provisioned) {
778 return false;
779 }
780 } finally {
781 provisionedSubscribersReadLock.unlock();
782 }
783 return true;
784 }
785
786 @Override
787 public void updateProvisionedSubscriberStatus(ServiceKey sk, Boolean status) {
788 try {
789 provisionedSubscribersWriteLock.lock();
790 provisionedSubscribers.put(sk, status);
791 } finally {
792 provisionedSubscribersWriteLock.unlock();
793 }
794 }
795
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800796 protected boolean handleEapolFlow(DiscoveredSubscriber sub, String bandwidthProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700797 String oltBandwidthProfile, FlowOperation action, VlanId vlanId) {
798
799 // create a subscriberKey for the EAPOL flow
800 ServiceKey sk = new ServiceKey(new AccessDevicePort(sub.port), defaultEapolUniTag);
801
802 // NOTE we only need to keep track of the default EAPOL flow in the
803 // connectpoint status map
804 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
805 OltFlowsStatus status = action == FlowOperation.ADD ?
806 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
yasin sapli0823c932022-01-26 11:26:09 +0000807 updateConnectPointStatus(sk, status, OltFlowsStatus.NONE, OltFlowsStatus.NONE, OltFlowsStatus.NONE);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700808
809 }
810
811 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
812 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
813
814 int techProfileId = getDefaultTechProfileId(sub.port);
815 MeterId meterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), bandwidthProfile);
816
817 // in the delete case the meter should still be there as we remove
818 // the meters only if no flows are pointing to them
819 if (meterId == null) {
820 log.debug("MeterId is null for BandwidthProfile {} on device {}",
821 bandwidthProfile, sub.device.id());
822 return false;
823 }
824
825 MeterId oltMeterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), oltBandwidthProfile);
826 if (oltMeterId == null) {
827 log.debug("MeterId is null for OltBandwidthProfile {} on device {}",
828 oltBandwidthProfile, sub.device.id());
829 return false;
830 }
831
832 log.info("{} EAPOL flow for {} with vlanId {} and BandwidthProfile {} (meterId {})",
833 flowOpToString(action), portWithName(sub.port), vlanId, bandwidthProfile, meterId);
834
835 FilteringObjective.Builder eapolAction;
836
837 if (action == FlowOperation.ADD) {
838 eapolAction = filterBuilder.permit();
839 } else if (action == FlowOperation.REMOVE) {
840 eapolAction = filterBuilder.deny();
841 } else {
842 log.error("Operation {} not supported", action);
843 return false;
844 }
845
846 FilteringObjective.Builder baseEapol = eapolAction
847 .withKey(Criteria.matchInPort(sub.port.number()))
848 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()));
849
850 // NOTE we only need to add the treatment to install the flow,
851 // we can remove it based in the match
852 FilteringObjective.Builder eapol;
853
854 TrafficTreatment treatment = treatmentBuilder
855 .meter(meterId)
856 .writeMetadata(createTechProfValueForWriteMetadata(
857 vlanId,
858 techProfileId, oltMeterId), 0)
859 .setOutput(PortNumber.CONTROLLER)
860 .pushVlan()
861 .setVlanId(vlanId)
862 .build();
863 eapol = baseEapol
864 .withMeta(treatment);
865
866 FilteringObjective eapolObjective = eapol
867 .fromApp(appId)
868 .withPriority(MAX_PRIORITY)
869 .add(new ObjectiveContext() {
870 @Override
871 public void onSuccess(Objective objective) {
872 log.info("EAPOL flow objective {} for {}",
873 completeFlowOpToString(action), portWithName(sub.port));
874 if (log.isTraceEnabled()) {
875 log.trace("EAPOL flow details for port {}: {}", portWithName(sub.port), objective);
876 }
877 }
878
879 @Override
880 public void onError(Objective objective, ObjectiveError error) {
881 log.error("Cannot {} eapol flow for {} : {}", action,
882 portWithName(sub.port), error);
883
884 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
885 updateConnectPointStatus(sk,
yasin sapli0823c932022-01-26 11:26:09 +0000886 OltFlowsStatus.ERROR, null, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700887 }
888 }
889 });
890
891 flowObjectiveService.filter(sub.device.id(), eapolObjective);
892
893 log.info("{} EAPOL filter for {}", completeFlowOpToString(action), portWithName(sub.port));
894 return true;
895 }
896
897 // FIXME it's confusing that si is not necessarily inside the DiscoveredSubscriber
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800898 protected boolean handleSubscriberEapolFlows(DiscoveredSubscriber sub, FlowOperation action,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700899 SubscriberAndDeviceInformation si) {
900 if (!enableEapol) {
901 return true;
902 }
903 // TODO verify we need an EAPOL flow for EACH service
904 AtomicBoolean success = new AtomicBoolean(true);
905 si.uniTagList().forEach(u -> {
906 // we assume that if subscriber flows are installed, tagged EAPOL is installed as well
907 boolean hasFlows = hasSubscriberFlows(sub.port, u);
908
909 // if the subscriber flows are present the EAPOL flow is present thus we don't need to install it,
910 // if the subscriber does not have flows the EAPOL flow is not present thus we don't need to remove it
911 if (action == FlowOperation.ADD && hasFlows ||
912 action == FlowOperation.REMOVE && !hasFlows) {
913 log.debug("Not {} EAPOL on {}({}) as subscriber flow status is {}", flowOpToString(action),
914 portWithName(sub.port), u.getServiceName(), hasFlows);
915 return;
916 }
917 log.info("{} EAPOL flows for subscriber {} on {} and service {}",
918 flowOpToString(action), si.id(), portWithName(sub.port), u.getServiceName());
919 if (!handleEapolFlow(sub, u.getUpstreamBandwidthProfile(),
920 u.getUpstreamOltBandwidthProfile(),
921 action, u.getPonCTag())) {
922 //
923 log.error("Failed to {} EAPOL with suscriber tags", action);
924 //TODO this sets it for all services, maybe some services succeeded.
925 success.set(false);
926 }
927 });
928 return success.get();
929 }
930
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800931 protected void handleSubscriberIgmpFlows(DiscoveredSubscriber sub, FlowOperation action) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700932 sub.subscriberAndDeviceInformation.uniTagList().forEach(uti -> {
933 if (uti.getIsIgmpRequired()) {
934 DeviceId deviceId = sub.device.id();
935 // if we reached here a meter already exists
936 MeterId meterId = oltMeterService
937 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
938 MeterId oltMeterId = oltMeterService
939 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
940
941 processIgmpFilteringObjectives(deviceId, sub.port,
942 action, FlowDirection.UPSTREAM, meterId, oltMeterId, uti.getTechnologyProfileId(),
943 uti.getPonCTag(), uti.getUniTagMatch(), uti.getUsPonCTagPriority());
944 }
945 });
946 }
947
948 private boolean checkSadisRunning() {
949 if (bpService == null) {
950 log.warn("Sadis is not running");
951 return false;
952 }
953 return true;
954 }
955
956 private int getDefaultTechProfileId(Port port) {
957 if (!checkSadisRunning()) {
958 return defaultTechProfileId;
959 }
960 if (port != null) {
961 SubscriberAndDeviceInformation info = subsService.get(getPortName(port));
962 if (info != null && info.uniTagList().size() == 1) {
963 return info.uniTagList().get(0).getTechnologyProfileId();
964 }
965 }
966 return defaultTechProfileId;
967 }
968
969 protected Long createTechProfValueForWriteMetadata(VlanId cVlan, int techProfileId, MeterId upstreamOltMeterId) {
970 Long writeMetadata;
971
972 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
973 writeMetadata = (long) techProfileId << 32;
974 } else {
975 writeMetadata = ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
976 }
977 if (upstreamOltMeterId == null) {
978 return writeMetadata;
979 } else {
980 return writeMetadata | upstreamOltMeterId.id();
981 }
982 }
983
984 private void processLldpFilteringObjective(DeviceId deviceId, Port port, FlowOperation action) {
985 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
986
987 FilteringObjective lldp = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
988 .withKey(Criteria.matchInPort(port.number()))
989 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
990 .withMeta(DefaultTrafficTreatment.builder()
991 .setOutput(PortNumber.CONTROLLER).build())
992 .fromApp(appId)
993 .withPriority(MAX_PRIORITY)
994 .add(new ObjectiveContext() {
995 @Override
996 public void onSuccess(Objective objective) {
997 log.info("{} LLDP filter for {}.", completeFlowOpToString(action), portWithName(port));
998 }
999
1000 @Override
1001 public void onError(Objective objective, ObjectiveError error) {
1002 log.error("Falied to {} LLDP filter on {} because {}", action, portWithName(port),
1003 error);
1004 }
1005 });
1006
1007 flowObjectiveService.filter(deviceId, lldp);
1008 }
1009
1010 protected void handleSubscriberDhcpFlows(DeviceId deviceId, Port port,
1011 FlowOperation action,
1012 SubscriberAndDeviceInformation si) {
1013 si.uniTagList().forEach(uti -> {
1014
1015 if (!uti.getIsDhcpRequired()) {
1016 return;
1017 }
1018
1019 // if it's an ADD skip if flows are there,
1020 // if it's a DELETE skip if flows are not there
1021 boolean hasFlows = hasDhcpFlows(port, uti);
1022 if (action == FlowOperation.ADD && hasFlows ||
1023 action == FlowOperation.REMOVE && !hasFlows) {
1024 log.debug("Not dealing with DHCP {} on {} as DHCP flow status is {}", action,
1025 uti.getServiceName(), hasFlows);
1026 return;
1027 }
1028
1029 log.info("{} DHCP flows for subscriber on {} and service {}",
1030 flowOpToString(action), portWithName(port), uti.getServiceName());
1031
1032 // if we reached here a meter already exists
1033 MeterId meterId = oltMeterService
1034 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1035 MeterId oltMeterId = oltMeterService
1036 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1037
1038 if (enableDhcpV4) {
1039 processDhcpFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, 68, 67,
1040 EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP, meterId, oltMeterId,
1041 uti);
1042 }
1043 if (enableDhcpV6) {
1044 log.error("DHCP V6 not supported for subscribers");
1045 }
1046 });
1047 }
1048
yasin sapli0823c932022-01-26 11:26:09 +00001049 protected void handleSubscriberPppoeFlows(DeviceId deviceId, Port port,
1050 FlowOperation action,
1051 SubscriberAndDeviceInformation si) {
1052 si.uniTagList().forEach(uti -> {
1053
1054 if (!uti.getIsPppoeRequired()) {
1055 return;
1056 }
1057
1058 // if it's an ADD skip if flows are there,
1059 // if it's a DELETE skip if flows are not there
1060 boolean hasFlows = hasPppoeFlows(port, uti);
1061 if (action == FlowOperation.ADD && hasFlows ||
1062 action == FlowOperation.REMOVE && !hasFlows) {
1063 log.debug("Not dealing with PPPoE {} on {} as PPPoE flow status is {}", action,
1064 uti.getServiceName(), hasFlows);
1065 return;
1066 }
1067
1068 log.info("{} PPPoE flows for subscriber on {} and service {}",
1069 flowOpToString(action), portWithName(port), uti.getServiceName());
1070
1071 // if we reached here a meter already exists
1072 MeterId meterId = oltMeterService
1073 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1074 MeterId oltMeterId = oltMeterService
1075 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1076
1077 if (enablePppoe) {
1078 processPPPoEDFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, meterId, oltMeterId,
1079 uti.getTechnologyProfileId(), uti.getPonCTag(), uti.getUniTagMatch(),
1080 (byte) uti.getUsPonCTagPriority());
1081 }
1082 });
1083 }
1084
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001085 // FIXME return boolean, if this fails we need to retry
1086 protected void handleSubscriberDataFlows(Device device, Port port,
1087 FlowOperation action,
1088 SubscriberAndDeviceInformation si, String multicastServiceName) {
1089
1090 Optional<Port> nniPort = oltDeviceService.getNniPort(device);
Matteo Scandolo97449bb2021-12-09 15:33:46 -08001091 if (nniPort == null || nniPort.isEmpty()) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001092 log.error("Cannot configure DP flows as upstream port is not configured for subscriber {} on {}",
1093 si.id(), portWithName(port));
1094 return;
1095 }
1096 si.uniTagList().forEach(uti -> {
1097
1098 boolean hasFlows = hasSubscriberFlows(port, uti);
1099 if (action == FlowOperation.ADD && hasFlows ||
1100 action == FlowOperation.REMOVE && !hasFlows) {
1101 log.debug("Not dealing with DP flows {} on {} as subscriber flow status is {}", action,
1102 uti.getServiceName(), hasFlows);
1103 return;
1104 }
1105
1106 if (multicastServiceName.equals(uti.getServiceName())) {
1107 log.debug("This is the multicast service ({}) for subscriber {} on {}, " +
1108 "dataplane flows are not needed",
1109 uti.getServiceName(), si.id(), portWithName(port));
1110 return;
1111 }
1112
1113 log.info("{} Data plane flows for subscriber {} on {} and service {}",
1114 flowOpToString(action), si.id(), portWithName(port), uti.getServiceName());
Matteo Scandolo80f5e972021-12-02 14:59:15 -08001115 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1116 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1117 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
yasin sapli0823c932022-01-26 11:26:09 +00001118 updateConnectPointStatus(sk, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001119
1120 // upstream flows
1121 MeterId usMeterId = oltMeterService
1122 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamBandwidthProfile());
1123 MeterId oltUsMeterId = oltMeterService
1124 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamOltBandwidthProfile());
1125 processUpstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, usMeterId,
1126 oltUsMeterId, uti);
1127
1128 // downstream flows
1129 MeterId dsMeterId = oltMeterService
1130 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamBandwidthProfile());
1131 MeterId oltDsMeterId = oltMeterService
1132 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamOltBandwidthProfile());
1133 processDownstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, dsMeterId,
1134 oltDsMeterId, uti, getMacAddress(device.id(), port, uti));
1135 });
1136 }
1137
1138 private void processDhcpFilteringObjectives(DeviceId deviceId, Port port,
1139 FlowOperation action, FlowDirection direction,
1140 int udpSrc, int udpDst, EthType ethType, byte protocol,
1141 MeterId meterId, MeterId oltMeterId, UniTagInformation uti) {
1142 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1143 log.debug("{} DHCP filtering objectives on {}", flowOpToString(action), sk);
1144
1145
1146 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1147 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
yasin sapli0823c932022-01-26 11:26:09 +00001148 updateConnectPointStatus(sk, null, null, status, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001149
1150 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1151 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1152
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001153 if (meterId != null) {
1154 treatmentBuilder.meter(meterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001155 }
1156
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001157 if (uti.getTechnologyProfileId() != NONE_TP_ID) {
1158 treatmentBuilder.writeMetadata(
1159 createTechProfValueForWriteMetadata(uti.getUniTagMatch(),
1160 uti.getTechnologyProfileId(), oltMeterId), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001161 }
1162
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001163 FilteringObjective.Builder dhcpBuilder = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001164 .withKey(Criteria.matchInPort(port.number()))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001165 .addCondition(Criteria.matchEthType(ethType))
1166 .addCondition(Criteria.matchIPProtocol(protocol))
1167 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1168 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001169 .fromApp(appId)
1170 .withPriority(MAX_PRIORITY);
1171
Andrea Campanella0e34f562020-06-11 10:47:10 +02001172 //VLAN changes and PCP matching need to happen only in the upstream directions
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001173 if (direction == FlowDirection.UPSTREAM) {
1174 treatmentBuilder.setVlanId(uti.getPonCTag());
1175 if (!VlanId.vlanId(VlanId.NO_VID).equals(uti.getUniTagMatch())) {
1176 dhcpBuilder.addCondition(Criteria.matchVlanId(uti.getUniTagMatch()));
Andrea Campanella0e34f562020-06-11 10:47:10 +02001177 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001178 if (uti.getUsPonCTagPriority() != -1) {
1179 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001180 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001181 }
1182
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001183 dhcpBuilder.withMeta(treatmentBuilder
1184 .setOutput(PortNumber.CONTROLLER).build());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001185
1186
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001187 FilteringObjective dhcpUpstream = dhcpBuilder.add(new ObjectiveContext() {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001188 @Override
1189 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001190 log.info("{} DHCP {} filter for {}.",
1191 completeFlowOpToString(action), (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1192 portWithName(port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001193 }
1194
1195 @Override
1196 public void onError(Objective objective, ObjectiveError error) {
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001197 log.error("DHCP {} filter for {} failed {} because {}",
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001198 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1199 portWithName(port),
1200 action,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001201 error);
yasin sapli0823c932022-01-26 11:26:09 +00001202 updateConnectPointStatus(sk, null, null, OltFlowsStatus.ERROR, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001203 }
1204 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001205 flowObjectiveService.filter(deviceId, dhcpUpstream);
1206 }
1207
1208 private void processIgmpFilteringObjectives(DeviceId deviceId, Port port,
1209 FlowOperation action, FlowDirection direction,
1210 MeterId meterId, MeterId oltMeterId, int techProfileId,
1211 VlanId cTag, VlanId unitagMatch, int vlanPcp) {
1212
1213 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
1214 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1215 if (direction == FlowDirection.UPSTREAM) {
1216
1217 if (techProfileId != NONE_TP_ID) {
1218 treatmentBuilder.writeMetadata(createTechProfValueForWriteMetadata(null,
1219 techProfileId, oltMeterId), 0);
1220 }
1221
1222
1223 if (meterId != null) {
1224 treatmentBuilder.meter(meterId);
1225 }
1226
1227 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1228 filterBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1229 }
1230
1231 if (!VlanId.vlanId(VlanId.NO_VID).equals(cTag)) {
1232 treatmentBuilder.setVlanId(cTag);
1233 }
1234
1235 if (vlanPcp != -1) {
1236 treatmentBuilder.setVlanPcp((byte) vlanPcp);
1237 }
1238 }
1239
1240 filterBuilder = (action == FlowOperation.ADD) ? filterBuilder.permit() : filterBuilder.deny();
1241
1242 FilteringObjective igmp = filterBuilder
1243 .withKey(Criteria.matchInPort(port.number()))
1244 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1245 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
1246 .withMeta(treatmentBuilder
1247 .setOutput(PortNumber.CONTROLLER).build())
1248 .fromApp(appId)
1249 .withPriority(MAX_PRIORITY)
1250 .add(new ObjectiveContext() {
1251 @Override
1252 public void onSuccess(Objective objective) {
1253 log.info("Igmp filter for {} {}.", portWithName(port), action);
1254 }
1255
1256 @Override
1257 public void onError(Objective objective, ObjectiveError error) {
1258 log.error("Igmp filter for {} failed {} because {}.", portWithName(port), action,
1259 error);
1260 }
1261 });
1262
1263 flowObjectiveService.filter(deviceId, igmp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001264
1265 }
1266
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001267 private void processPPPoEDFilteringObjectives(DeviceId deviceId, Port port,
1268 FlowOperation action, FlowDirection direction,
1269 MeterId meterId, MeterId oltMeterId, int techProfileId,
1270 VlanId cTag, VlanId unitagMatch, Byte vlanPcp) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001271
1272 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1273 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001274
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001275 if (meterId != null) {
1276 treatmentBuilder.meter(meterId);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001277 }
1278
1279 if (techProfileId != NONE_TP_ID) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001280 treatmentBuilder.writeMetadata(createTechProfValueForWriteMetadata(cTag, techProfileId, oltMeterId), 0);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001281 }
1282
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001283 DefaultFilteringObjective.Builder pppoedBuilder = ((action == FlowOperation.ADD)
1284 ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001285 .withKey(Criteria.matchInPort(port.number()))
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001286 .addCondition(Criteria.matchEthType(EthType.EtherType.PPPoED.ethType()))
1287 .fromApp(appId)
1288 .withPriority(10000);
1289
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001290 if (direction == FlowDirection.UPSTREAM) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001291 treatmentBuilder.setVlanId(cTag);
1292 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1293 pppoedBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1294 }
1295 if (vlanPcp != null) {
1296 treatmentBuilder.setVlanPcp(vlanPcp);
1297 }
1298 }
1299 pppoedBuilder = pppoedBuilder.withMeta(treatmentBuilder.setOutput(PortNumber.CONTROLLER).build());
1300
1301 FilteringObjective pppoed = pppoedBuilder
1302 .add(new ObjectiveContext() {
1303 @Override
1304 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001305 log.info("PPPoED filter for {} {}.", portWithName(port), action);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001306 }
1307
1308 @Override
1309 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001310 log.info("PPPoED filter for {} failed {} because {}", portWithName(port),
1311 action, error);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001312 }
1313 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001314 flowObjectiveService.filter(deviceId, pppoed);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001315 }
1316
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001317 private void processUpstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1318 FlowOperation action,
1319 MeterId upstreamMeterId,
1320 MeterId upstreamOltMeterId,
1321 UniTagInformation uti) {
1322 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001323 TrafficSelector selector = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001324 .matchInPort(port.number())
1325 .matchVlanId(uti.getUniTagMatch())
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001326 .build();
1327
Andrea Campanella327c5722020-01-30 11:34:13 +01001328 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1329 //if the subscriberVlan (cTag) is different than ANY it needs to set.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001330 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanella327c5722020-01-30 11:34:13 +01001331 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001332 .setVlanId(uti.getPonCTag());
Andrea Campanella327c5722020-01-30 11:34:13 +01001333 }
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001334 if (uti.getPonSTag().toShort() == VlanId.ANY_VALUE) {
1335 treatmentBuilder.popVlan();
1336 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001337
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001338 if (uti.getUsPonCTagPriority() != -1) {
1339 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001340
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001341 }
1342
1343 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001344 .setVlanId(uti.getPonSTag());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001345
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001346 if (uti.getUsPonSTagPriority() != -1) {
1347 treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001348 }
1349
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001350 treatmentBuilder.setOutput(nniPort.number())
1351 .writeMetadata(createMetadata(uti.getPonCTag(),
1352 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001353
yasin saplib4b8ee12021-06-13 18:25:20 +00001354 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1355
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001356 if (upstreamMeterId != null) {
1357 treatmentBuilder.meter(upstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001358 annotationBuilder.set(UPSTREAM_ONU, upstreamMeterId.toString());
1359 }
1360 if (upstreamOltMeterId != null) {
1361 treatmentBuilder.meter(upstreamOltMeterId);
1362 annotationBuilder.set(UPSTREAM_OLT, upstreamOltMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001363 }
1364
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001365 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selector,
1366 treatmentBuilder.build(), MIN_PRIORITY,
yasin saplib4b8ee12021-06-13 18:25:20 +00001367 annotationBuilder.build());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001368
1369 ObjectiveContext context = new ObjectiveContext() {
1370 @Override
1371 public void onSuccess(Objective objective) {
1372 log.info("{} Upstream Data plane filter for {}.",
1373 completeFlowOpToString(action), sk);
1374 }
1375
1376 @Override
1377 public void onError(Objective objective, ObjectiveError error) {
1378 log.error("Upstream Data plane filter for {} failed {} because {}.",
1379 sk, action, error);
yasin sapli0823c932022-01-26 11:26:09 +00001380 updateConnectPointStatus(sk, null, OltFlowsStatus.ERROR, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001381 }
1382 };
1383
1384 ForwardingObjective flow = null;
1385 if (action == FlowOperation.ADD) {
1386 flow = flowBuilder.add(context);
1387 } else if (action == FlowOperation.REMOVE) {
1388 flow = flowBuilder.remove(context);
1389 } else {
1390 log.error("Flow action not supported: {}", action);
1391 }
1392
1393 if (flow != null) {
1394 flowObjectiveService.forward(deviceId, flow);
1395 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001396 }
1397
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001398 private void processDownstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1399 FlowOperation action,
1400 MeterId downstreamMeterId,
1401 MeterId downstreamOltMeterId,
1402 UniTagInformation uti,
1403 MacAddress macAddress) {
1404 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanella327c5722020-01-30 11:34:13 +01001405 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001406 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001407 .matchVlanId(uti.getPonSTag())
1408 .matchInPort(nniPort.number())
1409 .matchInnerVlanId(uti.getPonCTag());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001410
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001411 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1412 selectorBuilder.matchMetadata(uti.getPonCTag().toShort());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001413 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001414
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001415 if (uti.getDsPonCTagPriority() != -1) {
1416 selectorBuilder.matchVlanPcp((byte) uti.getDsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001417 }
1418
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001419 if (macAddress != null) {
1420 selectorBuilder.matchEthDst(macAddress);
1421 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001422
1423 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
1424 .popVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001425 .setOutput(port.number());
Andrea Campanella327c5722020-01-30 11:34:13 +01001426
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001427 treatmentBuilder.writeMetadata(createMetadata(uti.getPonCTag(),
1428 uti.getTechnologyProfileId(),
1429 port.number()), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001430
Andrea Campanella981e86c2021-03-12 11:35:33 +01001431 // Upstream pbit is used to remark inner vlan pbit.
1432 // Upstream is used to avoid trusting the BNG to send the packet with correct pbit.
1433 // this is done because ds mode 0 is used because ds mode 3 or 6 that allow for
1434 // all pbit acceptance are not widely supported by vendors even though present in
1435 // the OMCI spec.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001436 if (uti.getUsPonCTagPriority() != -1) {
1437 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001438 }
1439
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001440 if (!VlanId.NONE.equals(uti.getUniTagMatch()) &&
1441 uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1442 treatmentBuilder.setVlanId(uti.getUniTagMatch());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001443 }
1444
yasin saplib4b8ee12021-06-13 18:25:20 +00001445 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1446
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001447 if (downstreamMeterId != null) {
1448 treatmentBuilder.meter(downstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001449 annotationBuilder.set(DOWNSTREAM_ONU, downstreamMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001450 }
1451
yasin saplib4b8ee12021-06-13 18:25:20 +00001452 if (downstreamOltMeterId != null) {
1453 treatmentBuilder.meter(downstreamOltMeterId);
1454 annotationBuilder.set(DOWNSTREAM_OLT, downstreamOltMeterId.toString());
1455 }
1456
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001457 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1458 treatmentBuilder.build(), MIN_PRIORITY, annotationBuilder.build());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001459
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001460 ObjectiveContext context = new ObjectiveContext() {
1461 @Override
1462 public void onSuccess(Objective objective) {
1463 log.info("{} Downstream Data plane filter for {}.",
1464 completeFlowOpToString(action), sk);
1465 }
1466
1467 @Override
1468 public void onError(Objective objective, ObjectiveError error) {
1469 log.info("Downstream Data plane filter for {} failed {} because {}.",
1470 sk, action, error);
yasin sapli0823c932022-01-26 11:26:09 +00001471 updateConnectPointStatus(sk, null, OltFlowsStatus.ERROR, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001472 }
1473 };
1474
1475 ForwardingObjective flow = null;
1476 if (action == FlowOperation.ADD) {
1477 flow = flowBuilder.add(context);
1478 } else if (action == FlowOperation.REMOVE) {
1479 flow = flowBuilder.remove(context);
1480 } else {
1481 log.error("Flow action not supported: {}", action);
1482 }
1483
1484 if (flow != null) {
1485 if (log.isTraceEnabled()) {
1486 log.trace("Forwarding rule {}", flow);
1487 }
1488 flowObjectiveService.forward(deviceId, flow);
1489 }
Andrea Campanella600d2e22020-06-22 11:00:31 +02001490 }
1491
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001492 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
1493 TrafficTreatment treatment,
yasin saplib4b8ee12021-06-13 18:25:20 +00001494 Integer priority,
1495 Annotations annotations) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001496 return DefaultForwardingObjective.builder()
1497 .withFlag(ForwardingObjective.Flag.VERSATILE)
1498 .withPriority(priority)
1499 .makePermanent()
1500 .withSelector(selector)
yasin saplib4b8ee12021-06-13 18:25:20 +00001501 .withAnnotations(annotations)
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001502 .fromApp(appId)
1503 .withTreatment(treatment);
1504 }
1505
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001506 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
1507 if (techProfileId == NONE_TP_ID) {
Andrea Campanella7c49b792020-05-11 11:36:53 +02001508 techProfileId = DEFAULT_TP_ID_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001509 }
1510
1511 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
1512 }
1513
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001514 private boolean isMacLearningEnabled(SubscriberAndDeviceInformation si) {
1515 AtomicBoolean requiresMacLearning = new AtomicBoolean();
1516 requiresMacLearning.set(false);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001517
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001518 si.uniTagList().forEach(uniTagInfo -> {
1519 if (uniTagInfo.getEnableMacLearning()) {
1520 requiresMacLearning.set(true);
1521 }
1522 });
1523
1524 return requiresMacLearning.get();
1525 }
1526
1527 /**
1528 * Checks whether the subscriber has the MacAddress configured or discovered.
1529 *
1530 * @param deviceId DeviceId for this subscriber
1531 * @param port Port for this subscriber
1532 * @param si SubscriberAndDeviceInformation
1533 * @return boolean
1534 */
1535 protected boolean isMacAddressAvailable(DeviceId deviceId, Port port, SubscriberAndDeviceInformation si) {
1536 AtomicBoolean isConfigured = new AtomicBoolean();
1537 isConfigured.set(true);
1538
1539 si.uniTagList().forEach(uniTagInfo -> {
1540 boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning();
1541 boolean configureMac = isMacAddressValid(uniTagInfo);
1542 boolean discoveredMac = false;
1543 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1544 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1545 if (optHost.isPresent() && optHost.get().mac() != null) {
1546 discoveredMac = true;
1547 }
1548 if (isMacLearningEnabled && !configureMac && !discoveredMac) {
1549 log.debug("Awaiting for macAddress on {} for service {}",
1550 portWithName(port), uniTagInfo.getServiceName());
1551 isConfigured.set(false);
1552 }
1553 });
1554
1555 return isConfigured.get();
1556 }
1557
1558 protected MacAddress getMacAddress(DeviceId deviceId, Port port, UniTagInformation uniTagInfo) {
1559 boolean configuredMac = isMacAddressValid(uniTagInfo);
1560 if (configuredMac) {
1561 return MacAddress.valueOf(uniTagInfo.getConfiguredMacAddress());
1562 } else if (uniTagInfo.getEnableMacLearning()) {
1563 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1564 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1565 if (optHost.isPresent() && optHost.get().mac() != null) {
1566 return optHost.get().mac();
1567 }
1568 }
1569 return null;
1570 }
1571
1572 private boolean isMacAddressValid(UniTagInformation tagInformation) {
1573 return tagInformation.getConfiguredMacAddress() != null &&
1574 !tagInformation.getConfiguredMacAddress().trim().equals("") &&
1575 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
1576 }
1577
1578 protected void updateConnectPointStatus(ServiceKey key, OltFlowsStatus eapolStatus,
yasin sapli0823c932022-01-26 11:26:09 +00001579 OltFlowsStatus subscriberFlowsStatus, OltFlowsStatus dhcpStatus,
1580 OltFlowsStatus pppoeStatus) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001581 if (log.isTraceEnabled()) {
1582 log.trace("Updating cpStatus {} with values: eapolFlow={}, subscriberFlows={}, dhcpFlow={}",
1583 key, eapolStatus, subscriberFlowsStatus, dhcpStatus);
1584 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001585 try {
1586 cpStatusWriteLock.lock();
1587 OltPortStatus status = cpStatus.get(key);
1588
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001589
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001590 if (status == null) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001591 // if we don't have status for the connectPoint
1592 // and we're only updating status to PENDING_REMOVE or ERROR
1593 // do not create it. This is because this case will only happen when a device is removed
1594 // and it's status cleaned
1595 List<OltFlowsStatus> statusesToIgnore = new ArrayList<>();
1596 statusesToIgnore.add(OltFlowsStatus.PENDING_REMOVE);
1597 statusesToIgnore.add(OltFlowsStatus.ERROR);
1598
1599 if (
1600 (statusesToIgnore.contains(subscriberFlowsStatus) && dhcpStatus == null) ||
1601 (subscriberFlowsStatus == null && statusesToIgnore.contains(dhcpStatus))
1602 ) {
1603 if (log.isTraceEnabled()) {
1604 log.trace("Ignoring cpStatus update as status is meaningless");
1605 }
1606 return;
1607 }
1608
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001609 status = new OltPortStatus(
1610 eapolStatus != null ? eapolStatus : OltFlowsStatus.NONE,
1611 subscriberFlowsStatus != null ? subscriberFlowsStatus : OltFlowsStatus.NONE,
yasin sapli0823c932022-01-26 11:26:09 +00001612 dhcpStatus != null ? dhcpStatus : OltFlowsStatus.NONE,
1613 pppoeStatus != null ? pppoeStatus : OltFlowsStatus.NONE
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001614 );
1615 } else {
1616 if (eapolStatus != null) {
1617 status.defaultEapolStatus = eapolStatus;
1618 }
1619 if (subscriberFlowsStatus != null) {
1620 status.subscriberFlowsStatus = subscriberFlowsStatus;
1621 }
1622 if (dhcpStatus != null) {
1623 status.dhcpStatus = dhcpStatus;
1624 }
1625 }
1626
1627 cpStatus.put(key, status);
1628 } finally {
1629 cpStatusWriteLock.unlock();
1630 }
1631 }
1632
1633 protected class InternalFlowListener implements FlowRuleListener {
1634 @Override
1635 public void event(FlowRuleEvent event) {
1636 if (appId.id() != (event.subject().appId())) {
1637 return;
1638 }
1639
1640 if (!oltDeviceService.isLocalLeader(event.subject().deviceId())) {
1641 if (log.isTraceEnabled()) {
1642 log.trace("ignoring flow event {} " +
1643 "as not leader for {}", event, event.subject().deviceId());
1644 }
1645 return;
1646 }
1647
1648 switch (event.type()) {
1649 case RULE_ADDED:
1650 case RULE_REMOVED:
Andrea Campanella40d2b342022-02-04 18:13:37 +01001651 DeviceId deviceId = event.subject().deviceId();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001652 Port port = getCpFromFlowRule(event.subject());
1653 if (port == null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001654 log.warn("Port is gone in ONOS, " +
1655 "manually creating it {}", event.subject());
1656 PortNumber inPort = getPortNumberFromFlowRule(event.subject());
1657 cpStatusReadLock.lock();
1658 Optional<ServiceKey> keyWithPort = cpStatus.keySet()
1659 .stream().filter(key -> key.getPort().connectPoint()
1660 .deviceId().equals(deviceId)
1661 && key.getPort().connectPoint().port()
1662 .equals(inPort)).findFirst();
1663 cpStatusReadLock.unlock();
1664 if (keyWithPort.isPresent()) {
1665 port = new DefaultPort(deviceService.getDevice(deviceId),
1666 inPort, false,
1667 DefaultAnnotations.builder()
1668 .set(AnnotationKeys.PORT_NAME,
1669 keyWithPort.get().getPort().name())
1670 .build());
1671 } else {
1672 log.warn("Can't find corresponding status for {}/{}", deviceId, inPort);
1673 return;
1674 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001675 }
1676 if (log.isTraceEnabled()) {
1677 log.trace("flow event {} on cp {}: {}", event.type(),
1678 portWithName(port), event.subject());
1679 }
1680 updateCpStatus(event.type(), port, event.subject());
1681 return;
1682 case RULE_ADD_REQUESTED:
1683 case RULE_REMOVE_REQUESTED:
1684 // NOTE that PENDING_ADD/REMOVE is set when we create the flowObjective
1685 return;
1686 default:
1687 return;
1688 }
1689 }
1690
1691 protected void updateCpStatus(FlowRuleEvent.Type type, Port port, FlowRule flowRule) {
1692 OltFlowsStatus status = flowRuleStatusToOltFlowStatus(type);
1693 if (isDefaultEapolFlow(flowRule)) {
1694 ServiceKey sk = new ServiceKey(new AccessDevicePort(port),
1695 defaultEapolUniTag);
1696 if (log.isTraceEnabled()) {
1697 log.trace("update defaultEapolStatus {} on {}", status, sk);
1698 }
yasin sapli0823c932022-01-26 11:26:09 +00001699 updateConnectPointStatus(sk, status, null, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001700 } else if (isDhcpFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001701 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001702 if (sk == null) {
1703 return;
1704 }
1705 if (log.isTraceEnabled()) {
1706 log.trace("update dhcpStatus {} on {}", status, sk);
1707 }
yasin sapli0823c932022-01-26 11:26:09 +00001708 updateConnectPointStatus(sk, null, null, status, null);
1709 } else if (isPppoeFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001710 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
yasin sapli0823c932022-01-26 11:26:09 +00001711 if (sk == null) {
1712 return;
1713 }
1714 if (log.isTraceEnabled()) {
1715 log.trace("update pppoeStatus {} on {}", status, sk);
1716 }
1717 updateConnectPointStatus(sk, null, null, null, status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001718 } else if (isDataFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001719 PortNumber number = getPortNumberFromFlowRule(flowRule);
1720 if (number == null) {
1721 log.error("Can't capture the port number from flow {}", flowRule);
1722 return;
1723 }
1724 if (oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), number)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001725 // the NNI has data-plane for every subscriber, doesn't make sense to track them
1726 return;
1727 }
1728
Andrea Campanella40d2b342022-02-04 18:13:37 +01001729 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001730 if (sk == null) {
1731 return;
1732 }
1733 if (log.isTraceEnabled()) {
1734 log.trace("update dataplaneStatus {} on {}", status, sk);
1735 }
yasin sapli0823c932022-01-26 11:26:09 +00001736 updateConnectPointStatus(sk, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001737 }
1738 }
1739
1740 private boolean isDefaultEapolFlow(FlowRule flowRule) {
1741 EthTypeCriterion c = (EthTypeCriterion) flowRule.selector().getCriterion(Criterion.Type.ETH_TYPE);
1742 if (c == null) {
1743 return false;
1744 }
1745 if (c.ethType().equals(EthType.EtherType.EAPOL.ethType())) {
1746 AtomicBoolean isDefault = new AtomicBoolean(false);
1747 flowRule.treatment().allInstructions().forEach(instruction -> {
1748 if (instruction.type() == L2MODIFICATION) {
1749 L2ModificationInstruction modificationInstruction = (L2ModificationInstruction) instruction;
1750 if (modificationInstruction.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
1751 L2ModificationInstruction.ModVlanIdInstruction vlanInstruction =
1752 (L2ModificationInstruction.ModVlanIdInstruction) modificationInstruction;
1753 if (vlanInstruction.vlanId().id().equals(EAPOL_DEFAULT_VLAN)) {
1754 isDefault.set(true);
1755 return;
1756 }
1757 }
1758 }
1759 });
1760 return isDefault.get();
1761 }
1762 return false;
1763 }
1764
1765 /**
1766 * Returns true if the flow is a DHCP flow.
1767 * Matches both upstream and downstream flows.
1768 *
1769 * @param flowRule The FlowRule to evaluate
1770 * @return boolean
1771 */
1772 private boolean isDhcpFlow(FlowRule flowRule) {
1773 IPProtocolCriterion ipCriterion = (IPProtocolCriterion) flowRule.selector()
1774 .getCriterion(Criterion.Type.IP_PROTO);
1775 if (ipCriterion == null) {
1776 return false;
1777 }
1778
1779 UdpPortCriterion src = (UdpPortCriterion) flowRule.selector().getCriterion(Criterion.Type.UDP_SRC);
1780
1781 if (src == null) {
1782 return false;
1783 }
1784 return ipCriterion.protocol() == IPv4.PROTOCOL_UDP &&
1785 (src.udpPort().toInt() == 68 || src.udpPort().toInt() == 67);
1786 }
1787
yasin sapli0823c932022-01-26 11:26:09 +00001788 private boolean isPppoeFlow(FlowRule flowRule) {
1789 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) flowRule.selector()
1790 .getCriterion(Criterion.Type.ETH_TYPE);
1791
1792 if (ethTypeCriterion == null) {
1793 return false;
1794 }
1795 return EthType.EtherType.PPPoED.ethType().equals(ethTypeCriterion.ethType());
1796 }
1797
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001798 private boolean isDataFlow(FlowRule flowRule) {
1799 // we consider subscriber flows the one that matches on VLAN_VID
1800 // method is valid only because it's the last check after EAPOL and DHCP.
1801 // this matches mcast flows as well, if we want to avoid that we can
1802 // filter out the elements that have groups in the treatment or
1803 // mcastIp in the selector
1804 // IPV4_DST:224.0.0.22/32
1805 // treatment=[immediate=[GROUP:0x1]]
1806
1807 return flowRule.selector().getCriterion(Criterion.Type.VLAN_VID) != null;
1808 }
1809
1810 private Port getCpFromFlowRule(FlowRule flowRule) {
1811 DeviceId deviceId = flowRule.deviceId();
Andrea Campanella40d2b342022-02-04 18:13:37 +01001812 PortNumber inPort = getPortNumberFromFlowRule(flowRule);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001813 if (inPort != null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001814 return deviceService.getPort(deviceId, inPort);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001815 }
1816 return null;
1817 }
1818
Andrea Campanella40d2b342022-02-04 18:13:37 +01001819 private PortNumber getPortNumberFromFlowRule(FlowRule flowRule) {
1820 PortCriterion inPort = (PortCriterion) flowRule.selector().getCriterion(Criterion.Type.IN_PORT);
1821 if (inPort != null) {
1822 return inPort.port();
1823 }
1824 return null;
1825 }
1826
1827 private ServiceKey getSubscriberKeyFromFlowRule(FlowRule flowRule, Port flowPort) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001828 SubscriberAndDeviceInformation si = subsService.get(getPortName(flowPort));
1829
1830 Boolean isNni = oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), flowPort.number());
1831 if (si == null && !isNni) {
1832 log.error("Subscriber information not found in sadis for port {}", portWithName(flowPort));
1833 return null;
1834 }
1835
1836 if (isNni) {
1837 return new ServiceKey(new AccessDevicePort(flowPort), nniUniTag);
1838 }
1839
1840 Optional<UniTagInformation> found = Optional.empty();
1841 VlanId flowVlan = null;
1842 if (isDhcpFlow(flowRule)) {
1843 // we need to make a special case for DHCP as in the ATT workflow DHCP flows don't match on tags
1844 L2ModificationInstruction.ModVlanIdInstruction instruction =
1845 (L2ModificationInstruction.ModVlanIdInstruction) flowRule.treatment().immediate().get(1);
1846 flowVlan = instruction.vlanId();
1847 } else {
1848 // for now we assume that if it's not DHCP it's dataplane (or at least tagged)
1849 VlanIdCriterion vlanIdCriterion =
1850 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
1851 if (vlanIdCriterion == null) {
1852 log.warn("cannot match the flow to a subscriber service as it does not carry vlans");
1853 return null;
1854 }
1855 flowVlan = vlanIdCriterion.vlanId();
1856 }
1857
1858 VlanId finalFlowVlan = flowVlan;
1859 found = si.uniTagList().stream().filter(uti ->
1860 uti.getPonCTag().equals(finalFlowVlan) ||
1861 uti.getPonSTag().equals(finalFlowVlan) ||
1862 uti.getUniTagMatch().equals(finalFlowVlan)
1863 ).findFirst();
1864
1865
1866 if (found.isEmpty()) {
1867 log.warn("Cannot map flow rule {} to Service in {}", flowRule, si);
1868 }
1869
1870 return found.isPresent() ? new ServiceKey(new AccessDevicePort(flowPort), found.get()) : null;
1871
1872 }
1873
1874 private OltFlowsStatus flowRuleStatusToOltFlowStatus(FlowRuleEvent.Type type) {
1875 switch (type) {
1876 case RULE_ADD_REQUESTED:
1877 return OltFlowsStatus.PENDING_ADD;
1878 case RULE_ADDED:
1879 return OltFlowsStatus.ADDED;
1880 case RULE_REMOVE_REQUESTED:
1881 return OltFlowsStatus.PENDING_REMOVE;
1882 case RULE_REMOVED:
1883 return OltFlowsStatus.REMOVED;
1884 default:
1885 return OltFlowsStatus.NONE;
1886 }
1887 }
1888 }
1889
1890 protected void bindSadisService(SadisService service) {
1891 this.subsService = service.getSubscriberInfoService();
1892 this.bpService = service.getBandwidthProfileService();
1893 log.info("Sadis service is loaded");
1894 }
1895
1896 protected void unbindSadisService(SadisService service) {
1897 this.subsService = null;
1898 this.bpService = null;
1899 log.info("Sadis service is unloaded");
1900 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001901}