blob: 080aa349a17d2274d5a49037924ac0f77ee3fb7f [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;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070052import org.onosproject.net.flow.criteria.VlanIdCriterion;
53import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000054import org.onosproject.net.flowobjective.DefaultFilteringObjective;
55import org.onosproject.net.flowobjective.DefaultForwardingObjective;
56import org.onosproject.net.flowobjective.FilteringObjective;
57import org.onosproject.net.flowobjective.FlowObjectiveService;
58import org.onosproject.net.flowobjective.ForwardingObjective;
59import org.onosproject.net.flowobjective.Objective;
60import org.onosproject.net.flowobjective.ObjectiveContext;
61import org.onosproject.net.flowobjective.ObjectiveError;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070062import org.onosproject.net.host.HostService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000063import org.onosproject.net.meter.MeterId;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000064import org.onosproject.store.serializers.KryoNamespaces;
65import org.onosproject.store.service.Serializer;
66import org.onosproject.store.service.StorageService;
Gustavo Silva29fb20e2022-05-26 09:59:54 -030067import org.opencord.olt.AccessDevicePort;
68import org.opencord.olt.DiscoveredSubscriber;
69import org.opencord.olt.OltDeviceServiceInterface;
70import org.opencord.olt.OltFlowServiceInterface;
71import org.opencord.olt.OltMeterServiceInterface;
72import org.opencord.olt.OltPortStatus;
73import org.opencord.olt.ServiceKey;
74import org.opencord.olt.OltFlowsStatus;
75import org.opencord.olt.FlowDirection;
76import org.opencord.olt.FlowOperation;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +053077import org.opencord.olt.impl.fttb.FttbUtils;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000078import org.opencord.sadis.BandwidthProfileInformation;
79import org.opencord.sadis.BaseInformationService;
80import org.opencord.sadis.SadisService;
81import org.opencord.sadis.SubscriberAndDeviceInformation;
82import org.opencord.sadis.UniTagInformation;
83import org.osgi.service.component.ComponentContext;
84import org.osgi.service.component.annotations.Activate;
85import org.osgi.service.component.annotations.Component;
86import org.osgi.service.component.annotations.Deactivate;
87import org.osgi.service.component.annotations.Modified;
88import org.osgi.service.component.annotations.Reference;
89import org.osgi.service.component.annotations.ReferenceCardinality;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000090import org.osgi.service.component.annotations.ReferencePolicy;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000091import org.slf4j.Logger;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070092import org.slf4j.LoggerFactory;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000093
Matteo Scandolo2542e5d2021-12-01 16:53:41 -080094import java.util.ArrayList;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +010095import java.util.Dictionary;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070096import java.util.HashMap;
Andrea Campanella61650a12022-01-24 18:09:44 -080097import java.util.HashSet;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070098import java.util.Iterator;
Matteo Scandolo2542e5d2021-12-01 16:53:41 -080099import java.util.List;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000100import java.util.Map;
Andrea Campanellabfb47af2021-06-03 11:09:45 +0200101import java.util.Optional;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +0100102import java.util.Properties;
Andrea Campanella61650a12022-01-24 18:09:44 -0800103import java.util.Set;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700104import java.util.concurrent.atomic.AtomicBoolean;
105import java.util.concurrent.locks.Lock;
106import java.util.concurrent.locks.ReentrantReadWriteLock;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +0100107
108import static com.google.common.base.Strings.isNullOrEmpty;
109import static org.onlab.util.Tools.get;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700110import static org.opencord.olt.impl.OltUtils.completeFlowOpToString;
111import static org.opencord.olt.impl.OltUtils.flowOpToString;
112import static org.opencord.olt.impl.OltUtils.getPortName;
113import static org.opencord.olt.impl.OltUtils.portWithName;
Andrea Campanella833ce2b2022-06-28 16:36:23 +0200114import static org.opencord.olt.impl.OsgiPropertyConstants.*;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +0530115import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_FLOW_DIRECTION;
116import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_FLOW_DOWNSTREAM;
117import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_FLOW_UPSTREAM;
118import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_SERVICE_DPU_ANCP_TRAFFIC;
119import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_SERVICE_DPU_MGMT_TRAFFIC;
120import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_SERVICE_NAME;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +0530121import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_SERVICE_SUBSCRIBER_TRAFFIC;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000122
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000123@Component(immediate = true, property = {
Saurav Dasf62cea82020-08-26 17:43:04 -0700124 ENABLE_DHCP_ON_NNI + ":Boolean=" + ENABLE_DHCP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000125 ENABLE_DHCP_V4 + ":Boolean=" + ENABLE_DHCP_V4_DEFAULT,
126 ENABLE_DHCP_V6 + ":Boolean=" + ENABLE_DHCP_V6_DEFAULT,
Saurav Dasf62cea82020-08-26 17:43:04 -0700127 ENABLE_IGMP_ON_NNI + ":Boolean=" + ENABLE_IGMP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000128 ENABLE_EAPOL + ":Boolean=" + ENABLE_EAPOL_DEFAULT,
yasin sapli0823c932022-01-26 11:26:09 +0000129 ENABLE_PPPOE_ON_NNI + ":Boolean=" + ENABLE_PPPOE_ON_NNI_DEFAULT,
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300130 ENABLE_PPPOE + ":Boolean=" + ENABLE_PPPOE_DEFAULT,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700131 DEFAULT_TP_ID + ":Integer=" + DEFAULT_TP_ID_DEFAULT,
132 // FIXME remove this option as potentially dangerous in production
Andrea Campanella833ce2b2022-06-28 16:36:23 +0200133 WAIT_FOR_REMOVAL + ":Boolean=" + WAIT_FOR_REMOVAL_DEFAULT,
134 REMOVE_FLOWS_ON_DISABLE + ":Boolean=" + REMOVE_FLOWS_ON_DISABLE_DEFAULT
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000135})
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700136public class OltFlowService implements OltFlowServiceInterface {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
139 protected CoreService coreService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700142 protected ComponentConfigService cfgService;
143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
145 protected FlowObjectiveService flowObjectiveService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000146
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000147 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
148 bind = "bindSadisService",
149 unbind = "unbindSadisService",
150 policy = ReferencePolicy.DYNAMIC)
151 protected volatile SadisService sadisService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700154 protected OltMeterServiceInterface oltMeterService;
155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY)
157 protected OltDeviceServiceInterface oltDeviceService;
158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY)
160 protected FlowRuleService flowRuleService;
161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY)
163 protected HostService hostService;
164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000166 protected DeviceService deviceService;
167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000169 protected StorageService storageService;
170
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700171 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
172 protected BaseInformationService<BandwidthProfileInformation> bpService;
173
174 private static final String APP_NAME = "org.opencord.olt";
175 protected ApplicationId appId;
176 private static final Integer MAX_PRIORITY = 10000;
177 private static final Integer MIN_PRIORITY = 1000;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +0530178 public static final short EAPOL_DEFAULT_VLAN = 4091;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700179 private static final int NONE_TP_ID = -1;
180 private static final String V4 = "V4";
181 private static final String V6 = "V6";
182 private final Logger log = LoggerFactory.getLogger(getClass());
183
184 protected UniTagInformation defaultEapolUniTag = new UniTagInformation.Builder()
185 .setServiceName("defaultEapol").build();
186 protected UniTagInformation nniUniTag = new UniTagInformation.Builder()
187 .setServiceName("nni")
188 .setTechnologyProfileId(NONE_TP_ID)
189 .setPonCTag(VlanId.NONE)
190 .setUniTagMatch(VlanId.ANY)
191 .setUsPonCTagPriority(-1)
192 .build();
193
194 /**
195 * Connect Point status map.
196 * Used to keep track of which cp has flows that needs to be removed when the status changes.
197 */
198 protected Map<ServiceKey, OltPortStatus> cpStatus;
199 private final ReentrantReadWriteLock cpStatusLock = new ReentrantReadWriteLock();
200 private final Lock cpStatusWriteLock = cpStatusLock.writeLock();
201 private final Lock cpStatusReadLock = cpStatusLock.readLock();
202
203 /**
204 * This map contains the subscriber that have been provisioned by the operator.
205 * They may or may not have flows, depending on the port status.
206 * The map is used to define whether flows need to be provisioned when a port comes up.
207 */
208 protected Map<ServiceKey, Boolean> provisionedSubscribers;
209 private final ReentrantReadWriteLock provisionedSubscribersLock = new ReentrantReadWriteLock();
210 private final Lock provisionedSubscribersWriteLock = provisionedSubscribersLock.writeLock();
211 private final Lock provisionedSubscribersReadLock = provisionedSubscribersLock.readLock();
212
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000213 /**
amit.ghosh74a4bb22022-06-14 11:34:52 +0200214 * For storing the mapping of ConnectPoints to FTTB DPU MAC addresses.
215 */
216 protected Map<ConnectPoint, MacAddress> fttbMacAddresses;
217 private final ReentrantReadWriteLock fttbMacAddressesLock = new ReentrantReadWriteLock();
218 private final Lock fttbMacAddressesWriteLock = fttbMacAddressesLock.writeLock();
219
220 /**
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
Andrea Campanella833ce2b2022-06-28 16:36:23 +0200262 /**
263 * Removes all the flows on an ONU disable.
264 **/
265 protected boolean removeFlowsOnDisable = REMOVE_FLOWS_ON_DISABLE_DEFAULT;
266
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700267 protected InternalFlowListener internalFlowListener;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000268
269 @Activate
270 public void activate(ComponentContext context) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700271 cfgService.registerProperties(getClass());
272 appId = coreService.registerApplication(APP_NAME);
273 internalFlowListener = new InternalFlowListener();
274
275 modified(context);
276
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000277 KryoNamespace serializer = KryoNamespace.newBuilder()
278 .register(KryoNamespaces.API)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700279 .register(OltFlowsStatus.class)
280 .register(FlowDirection.class)
281 .register(OltPortStatus.class)
282 .register(OltFlowsStatus.class)
Tunahan Sezenf0843b92021-04-30 07:13:16 +0000283 .register(AccessDevicePort.class)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700284 .register(new ServiceKeySerializer(), ServiceKey.class)
285 .register(UniTagInformation.class)
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000286 .build();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000287
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700288 cpStatus = storageService.<ServiceKey, OltPortStatus>consistentMapBuilder()
289 .withName("volt-cp-status")
290 .withApplicationId(appId)
291 .withSerializer(Serializer.using(serializer))
292 .build().asJavaMap();
293
294 provisionedSubscribers = storageService.<ServiceKey, Boolean>consistentMapBuilder()
295 .withName("volt-provisioned-subscriber")
296 .withApplicationId(appId)
297 .withSerializer(Serializer.using(serializer))
298 .build().asJavaMap();
299
amit.ghosh74a4bb22022-06-14 11:34:52 +0200300 KryoNamespace fttbMacSerializer = KryoNamespace.newBuilder()
301 .register(KryoNamespaces.API)
302 .register(ConnectPoint.class)
303 .register(MacAddress.class)
304 .build();
305
306 fttbMacAddresses = storageService.<ConnectPoint, MacAddress>consistentMapBuilder()
307 .withName("fttb-mac-addresses")
308 .withApplicationId(appId)
309 .withSerializer(Serializer.using(fttbMacSerializer))
310 .build().asJavaMap();
311
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700312 flowRuleService.addListener(internalFlowListener);
313
314 log.info("Started");
315 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000316
317 @Deactivate
318 public void deactivate(ComponentContext context) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700319 cfgService.unregisterProperties(getClass(), false);
320 flowRuleService.removeListener(internalFlowListener);
321 log.info("Stopped");
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000322 }
323
324 @Modified
325 public void modified(ComponentContext context) {
326
327 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
328
Saurav Dasf62cea82020-08-26 17:43:04 -0700329 Boolean o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000330 if (o != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700331 enableDhcpOnNni = o;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000332 }
333
Andrea Campanella7c49b792020-05-11 11:36:53 +0200334 Boolean v4 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V4);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000335 if (v4 != null) {
336 enableDhcpV4 = v4;
337 }
338
Andrea Campanella7c49b792020-05-11 11:36:53 +0200339 Boolean v6 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V6);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000340 if (v6 != null) {
341 enableDhcpV6 = v6;
342 }
343
Saurav Dasf62cea82020-08-26 17:43:04 -0700344 Boolean p = Tools.isPropertyEnabled(properties, ENABLE_IGMP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000345 if (p != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700346 enableIgmpOnNni = p;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000347 }
348
Andrea Campanella7c49b792020-05-11 11:36:53 +0200349 Boolean eap = Tools.isPropertyEnabled(properties, ENABLE_EAPOL);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000350 if (eap != null) {
351 enableEapol = eap;
352 }
353
yasin sapli0823c932022-01-26 11:26:09 +0000354 Boolean pppoeInNni = Tools.isPropertyEnabled(properties, ENABLE_PPPOE_ON_NNI);
355 if (pppoeInNni != null) {
356 enablePppoeOnNni = pppoeInNni;
357 }
358
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300359 Boolean pppoe = Tools.isPropertyEnabled(properties, ENABLE_PPPOE);
360 if (pppoe != null) {
361 enablePppoe = pppoe;
362 }
363
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700364 Boolean wait = Tools.isPropertyEnabled(properties, WAIT_FOR_REMOVAL);
365 if (wait != null) {
366 waitForRemoval = wait;
367 }
368
Andrea Campanella7c49b792020-05-11 11:36:53 +0200369 String tpId = get(properties, DEFAULT_TP_ID);
370 defaultTechProfileId = isNullOrEmpty(tpId) ? DEFAULT_TP_ID_DEFAULT : Integer.parseInt(tpId.trim());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000371
yasin sapli0823c932022-01-26 11:26:09 +0000372 log.info("Modified. Values = enableDhcpOnNni: {}, enableDhcpV4: {}, " + "enableDhcpV6:{}, " +
373 "enableIgmpOnNni:{}, " + "enableEapol:{}, enablePppoeOnNni: {}, enablePppoe:{}, " +
374 "defaultTechProfileId:{}," + "waitForRemoval:{}",
375 enableDhcpOnNni, enableDhcpV4, enableDhcpV6, enableIgmpOnNni, enableEapol,
376 enablePppoeOnNni, enablePppoe, defaultTechProfileId, waitForRemoval);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000377 }
378
379 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700380 public ImmutableMap<ServiceKey, OltPortStatus> getConnectPointStatus() {
381 try {
382 cpStatusReadLock.lock();
383 return ImmutableMap.copyOf(cpStatus);
384 } finally {
385 cpStatusReadLock.unlock();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000386 }
387 }
388
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700389 @Override
390 public ImmutableMap<ServiceKey, UniTagInformation> getProgrammedSubscribers() {
391 // NOTE we might want to remove this conversion and directly use cpStatus as it contains
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800392 // all the required information about subscribers
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700393 Map<ServiceKey, UniTagInformation> subscribers =
394 new HashMap<>();
395 try {
396 cpStatusReadLock.lock();
397
398 cpStatus.forEach((sk, status) -> {
399 if (
400 // not NNI Port
401 !oltDeviceService.isNniPort(deviceService.getDevice(sk.getPort().connectPoint().deviceId()),
402 sk.getPort().connectPoint().port()) &&
403 // not EAPOL flow
Andrea Campanella982fd332022-01-19 09:14:12 +0100404 !sk.getService().equals(defaultEapolUniTag) &&
405 !status.subscriberFlowsStatus.equals(OltFlowsStatus.PENDING_REMOVE)
406 && !status.subscriberFlowsStatus.equals(OltFlowsStatus.REMOVED)
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800407
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700408 ) {
409 subscribers.put(sk, sk.getService());
410 }
411 });
412
413 return ImmutableMap.copyOf(subscribers);
414 } finally {
415 cpStatusReadLock.unlock();
416 }
417 }
418
419 @Override
420 public Map<ServiceKey, Boolean> getRequestedSubscribers() {
421 try {
422 provisionedSubscribersReadLock.lock();
423 return ImmutableMap.copyOf(provisionedSubscribers);
424 } finally {
425 provisionedSubscribersReadLock.unlock();
426 }
427 }
428
429 @Override
430 public void handleNniFlows(Device device, Port port, FlowOperation action) {
431
432 // always handle the LLDP flow
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800433 log.debug("{} LLDP trap flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700434 processLldpFilteringObjective(device.id(), port, action);
435
436 if (enableDhcpOnNni) {
437 if (enableDhcpV4) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800438 log.debug("{} DHCPv4 trap flow on NNI {} for device {}", flowOpToString(action),
439 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700440 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
441 67, 68, EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP,
442 null, null, nniUniTag);
443 }
444 if (enableDhcpV6) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800445 log.debug("{} DHCPv6 trap flow on NNI {} for device {}", flowOpToString(action),
446 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700447 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
448 546, 547, EthType.EtherType.IPV6.ethType(), IPv6.PROTOCOL_UDP,
449 null, null, nniUniTag);
450 }
451 } else {
Matteo Scandolo1f8de332021-12-06 12:18:24 -0800452 log.debug("DHCP is not required on NNI {} for device {}", portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700453 }
454
455 if (enableIgmpOnNni) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800456 log.debug("{} IGMP flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700457 processIgmpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
458 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, -1);
459 }
460
yasin sapli0823c932022-01-26 11:26:09 +0000461 if (enablePppoeOnNni) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800462 log.debug("{} PPPoE flow on NNI {} for device {}", flowOpToString(action), port.number(), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700463 processPPPoEDFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
464 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, null);
465 }
466 }
467
468 @Override
469 public boolean handleBasicPortFlows(DiscoveredSubscriber sub, String bandwidthProfileId,
470 String oltBandwidthProfileId) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700471 // we only need to something if EAPOL is enabled
472 if (!enableEapol) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100473 log.debug("Eapol is disabled for {}", portWithName(sub.port));
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700474 return true;
475 }
476
477 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
478 return addDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
479 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
480 return removeDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
481 } else {
482 log.error("Unknown Status {} on DiscoveredSubscriber {}", sub.status, sub);
483 return false;
484 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700485 }
486
487 private boolean addDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfileId, String oltBandwidthProfileId) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100488 log.debug("Adding default flows for {}, status {}", portWithName(sub.port), sub.status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700489 if (!oltMeterService.createMeter(sub.device.id(), bandwidthProfileId)) {
490 if (log.isTraceEnabled()) {
491 log.trace("waiting on meter for bp {} and sub {}", bandwidthProfileId, sub);
492 }
493 return false;
494 }
495 if (hasDefaultEapol(sub.port)) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100496 OltPortStatus status = getOltPortStatus(sub.port, defaultEapolUniTag);
497 log.debug("Eapol is already present for {} with status {}", portWithName(sub.port), status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700498 return true;
499 }
500 return handleEapolFlow(sub, bandwidthProfileId,
501 oltBandwidthProfileId, FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
502
503 }
504
505 private boolean removeDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfile, String oltBandwidthProfile) {
506 // NOTE that we are not checking for meters as they must have been created to install the flow in first place
507 return handleEapolFlow(sub, bandwidthProfile, oltBandwidthProfile,
508 FlowOperation.REMOVE, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
509 }
510
511 @Override
512 public boolean handleSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwidthProfile,
513 String multicastServiceName) {
514 // NOTE that we are taking defaultBandwithProfile as a parameter as that can be configured in the Olt component
515 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
516 return addSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200517 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED ||
518 sub.status == DiscoveredSubscriber.Status.ADMIN_REMOVED) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700519 return removeSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
520 } else {
521 log.error("don't know how to handle {}", sub);
522 return false;
523 }
524 }
525
526 private boolean addSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
527 String multicastServiceName) {
528 if (log.isTraceEnabled()) {
529 log.trace("Provisioning of subscriber on {} started", portWithName(sub.port));
530 }
531 if (enableEapol) {
532 if (hasDefaultEapol(sub.port)) {
533 // remove EAPOL flow and throw exception so that we'll retry later
534 if (!isDefaultEapolPendingRemoval(sub.port)) {
535 removeDefaultFlows(sub, defaultBandwithProfile, defaultBandwithProfile);
536 }
537
538 if (waitForRemoval) {
539 // NOTE wait for removal is a flag only needed to make sure VOLTHA
540 // does not explode with the flows remove/add in the same batch
541 log.debug("Awaiting for default flows removal for {}", portWithName(sub.port));
542 return false;
543 } else {
544 log.warn("continuing provisioning on {}", portWithName(sub.port));
545 }
546 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700547 }
548
549 // NOTE createMeters will return if the meters are not installed
550 if (!oltMeterService.createMeters(sub.device.id(),
Matteo Scandolo88df8ae2021-11-23 13:12:29 -0800551 sub.subscriberAndDeviceInformation, multicastServiceName)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700552 return false;
553 }
554
555 // NOTE we need to add the DHCP flow regardless so that the host can be discovered and the MacAddress added
556 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.ADD,
557 sub.subscriberAndDeviceInformation);
558
559 if (isMacLearningEnabled(sub.subscriberAndDeviceInformation)
560 && !isMacAddressAvailable(sub.device.id(), sub.port,
561 sub.subscriberAndDeviceInformation)) {
562 log.debug("Awaiting for macAddress on {}", portWithName(sub.port));
563 return false;
564 }
565
Matteo Scandolo8a91c0f2021-12-03 10:58:14 -0800566 // NOTE that the EAPOL flows handling is based on the data-plane flows status
567 // always process them before
568 handleSubscriberEapolFlows(sub, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
569
yasin sapli0823c932022-01-26 11:26:09 +0000570 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
571
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700572 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.ADD,
573 sub.subscriberAndDeviceInformation, multicastServiceName);
574
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700575 handleSubscriberIgmpFlows(sub, FlowOperation.ADD);
576
577 log.info("Provisioning of subscriber on {} completed", portWithName(sub.port));
578 return true;
579 }
580
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800581 protected boolean removeSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700582 String multicastServiceName) {
583
584 if (log.isTraceEnabled()) {
585 log.trace("Removal of subscriber on {} started",
586 portWithName(sub.port));
587 }
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800588 SubscriberAndDeviceInformation si = sub.subscriberAndDeviceInformation;
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200589 //If the port has been removed the device service will return null, while it will be true if it's just disabled
590 boolean isPortPresent = deviceService.getPort(new ConnectPoint(sub.device.id(), sub.port.number())) != null;
591 if (log.isTraceEnabled()) {
592 log.trace("Port {} present: ", portWithName(sub.port), isPortPresent);
593 }
594 // Always remove the EAPOL flow in case of port disable,remove or subscriber remove.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700595 if (enableEapol) {
596 // remove the tagged eapol
597 handleSubscriberEapolFlows(sub, FlowOperation.REMOVE, si);
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200598 log.info("Removal of eapol flow for subscriber on {} completed", portWithName(sub.port));
599
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800600 }
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200601 // If the port is gone entirely (onu delete) or it's enabled (subscriber remove request) remove all the flows
602 // In the case the port is just disabled we remove only the EAPOL flow because the ONU disable only represents
603 // the UNI side of the ONU going down, either for RG cable detach or for an administrative decision, but the PON
604 // side is still up as nothing changed, so no need to add/remove flows, when and if the UNI comes up
605 // we will re-push the EAPOL flow to require the subscriber to auth again.
606 // When the subscriber is admin removed from REST or CLI we ignore the port status.
Andrea Campanella7ef88992022-05-17 12:38:00 +0200607 // Check the admin Status of the port
Andrea Campanella833ce2b2022-06-28 16:36:23 +0200608 if ((!isPortPresent || sub.port.isEnabled() || sub.status == DiscoveredSubscriber.Status.ADMIN_REMOVED)
609 || removeFlowsOnDisable) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700610
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200611 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.REMOVE, si);
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800612
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200613 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.REMOVE,
614 sub.subscriberAndDeviceInformation);
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800615
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200616 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.REMOVE, si, multicastServiceName);
617
618 handleSubscriberIgmpFlows(sub, FlowOperation.REMOVE);
619
620
621 if (enableEapol) {
622
623 // if any of the services still has flows, return false
624 Iterator<UniTagInformation> iter = sub.subscriberAndDeviceInformation.uniTagList().iterator();
625 while (iter.hasNext()) {
626 UniTagInformation entry = iter.next();
627 if (areSubscriberFlowsPendingRemoval(sub.port, entry, enableEapol)) {
628 log.info("Subscriber {} still have flows on service {}, postpone default EAPOL installation.",
629 portWithName(sub.port), entry.getServiceName());
630 return false;
631 }
632 }
633
634 // once the flows are removed add the default one back
635 // (only if the port is ENABLED and still present on the device)
636 if (sub.port.isEnabled() && deviceService.getPort(sub.device.id(), sub.port.number()) != null) {
637
638 // NOTE we remove the subscriber when the port goes down
639 // but in that case we don't need to add default eapol
640 handleEapolFlow(sub, defaultBandwithProfile, defaultBandwithProfile,
641 FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800642 }
643 }
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200644 // FIXME check the return status of the flow and return accordingly
645 log.info("Removal of subscriber on {} completed", portWithName(sub.port));
646 return true;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700647 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700648 return true;
649 }
650
651 @Override
652 public boolean hasDefaultEapol(Port port) {
653 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
654 // NOTE we consider ERROR as a present EAPOL flow as ONOS
655 // will keep trying to add it
656 return status != null && (status.defaultEapolStatus == OltFlowsStatus.ADDED ||
657 status.defaultEapolStatus == OltFlowsStatus.PENDING_ADD ||
658 status.defaultEapolStatus == OltFlowsStatus.ERROR);
659 }
660
661 private OltPortStatus getOltPortStatus(Port port, UniTagInformation uniTagInformation) {
662 try {
663 cpStatusReadLock.lock();
664 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uniTagInformation);
665 OltPortStatus status = cpStatus.get(sk);
666 return status;
667 } finally {
668 cpStatusReadLock.unlock();
669 }
670 }
671
672 public boolean isDefaultEapolPendingRemoval(Port port) {
673 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
674 if (log.isTraceEnabled()) {
675 log.trace("Status during EAPOL flow check {} for port {} and UniTagInformation {}",
676 status, portWithName(port), defaultEapolUniTag);
677 }
678 return status != null && status.defaultEapolStatus == OltFlowsStatus.PENDING_REMOVE;
679 }
680
681 @Override
682 public boolean hasDhcpFlows(Port port, UniTagInformation uti) {
683 OltPortStatus status = getOltPortStatus(port, uti);
684 if (log.isTraceEnabled()) {
685 log.trace("Status during DHCP flow check {} for port {} and service {}",
686 status, portWithName(port), uti.getServiceName());
687 }
688 return status != null &&
689 (status.dhcpStatus == OltFlowsStatus.ADDED ||
690 status.dhcpStatus == OltFlowsStatus.PENDING_ADD);
691 }
692
693 @Override
yasin sapli0823c932022-01-26 11:26:09 +0000694 public boolean hasPppoeFlows(Port port, UniTagInformation uti) {
695 OltPortStatus status = getOltPortStatus(port, uti);
696 if (log.isTraceEnabled()) {
697 log.trace("Status during PPPoE flow check {} for port {} and service {}",
698 status, portWithName(port), uti.getServiceName());
699 }
700 return status != null &&
701 (status.pppoeStatus == OltFlowsStatus.ADDED ||
702 status.pppoeStatus == OltFlowsStatus.PENDING_ADD);
703 }
704
705 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700706 public boolean hasSubscriberFlows(Port port, UniTagInformation uti) {
707
708 OltPortStatus status = getOltPortStatus(port, uti);
709 if (log.isTraceEnabled()) {
710 log.trace("Status during subscriber flow check {} for port {} and service {}",
711 status, portWithName(port), uti.getServiceName());
712 }
713 return status != null && (status.subscriberFlowsStatus == OltFlowsStatus.ADDED ||
714 status.subscriberFlowsStatus == OltFlowsStatus.PENDING_ADD);
715 }
716
Andrea Campanella87241ae2022-03-11 11:20:24 +0100717 public boolean areSubscriberFlowsPendingRemoval(Port port, UniTagInformation uti, boolean enableEapol) {
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800718 OltPortStatus status = getOltPortStatus(port, uti);
719 if (log.isTraceEnabled()) {
720 log.trace("Status during pending_remove flow check {} for port {} and UniTagInformation {}",
721 status, portWithName(port), uti);
722 }
Andrea Campanella87241ae2022-03-11 11:20:24 +0100723 return status != null && (status.subscriberFlowsStatus == OltFlowsStatus.PENDING_REMOVE ||
724 (enableEapol && status.subscriberEapolStatus == OltFlowsStatus.PENDING_REMOVE) ||
725 (uti.getIsDhcpRequired() && status.dhcpStatus == OltFlowsStatus.PENDING_REMOVE));
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800726 }
727
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700728 @Override
729 public void purgeDeviceFlows(DeviceId deviceId) {
730 log.debug("Purging flows on device {}", deviceId);
Matteo Scandolob6981dc2021-12-02 16:31:44 -0800731 flowRuleService.purgeFlowRules(deviceId);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700732
733 // removing the status from the cpStatus map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800734 if (log.isTraceEnabled()) {
735 log.trace("Clearing cp status from device {}", deviceId);
736 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700737 try {
738 cpStatusWriteLock.lock();
739 Iterator<Map.Entry<ServiceKey, OltPortStatus>> iter = cpStatus.entrySet().iterator();
740 while (iter.hasNext()) {
741 Map.Entry<ServiceKey, OltPortStatus> entry = iter.next();
742 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
743 cpStatus.remove(entry.getKey());
744 }
745 }
746 } finally {
747 cpStatusWriteLock.unlock();
748 }
749
750 // removing subscribers from the provisioned map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800751 if (log.isTraceEnabled()) {
752 log.trace("Clearing provisioned subscribers from device {}", deviceId);
753 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700754 try {
755 provisionedSubscribersWriteLock.lock();
756 Iterator<Map.Entry<ServiceKey, Boolean>> iter = provisionedSubscribers.entrySet().iterator();
757 while (iter.hasNext()) {
758 Map.Entry<ServiceKey, Boolean> entry = iter.next();
759 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
760 provisionedSubscribers.remove(entry.getKey());
761 }
762 }
763 } finally {
764 provisionedSubscribersWriteLock.unlock();
765 }
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800766 log.debug("Done clearing up device flows and subscribers");
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700767 }
768
769 @Override
770 public boolean isSubscriberServiceProvisioned(AccessDevicePort cp) {
Andrea Campanella61650a12022-01-24 18:09:44 -0800771 Set<Map.Entry<ServiceKey, Boolean>> subs;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700772 try {
773 provisionedSubscribersReadLock.lock();
Andrea Campanella61650a12022-01-24 18:09:44 -0800774 subs = new HashSet<>(provisionedSubscribers.entrySet());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700775 } finally {
776 provisionedSubscribersReadLock.unlock();
777 }
Andrea Campanella61650a12022-01-24 18:09:44 -0800778
779 for (Map.Entry<ServiceKey, Boolean> entry : subs) {
780 if (entry.getKey().getPort().equals(cp) && entry.getValue()) {
781 return true;
782 }
783 }
784 return false;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700785 }
786
787 @Override
788 public boolean isSubscriberServiceProvisioned(ServiceKey sk) {
789 try {
790 provisionedSubscribersReadLock.lock();
791 Boolean provisioned = provisionedSubscribers.get(sk);
792 if (provisioned == null || !provisioned) {
793 return false;
794 }
795 } finally {
796 provisionedSubscribersReadLock.unlock();
797 }
798 return true;
799 }
800
801 @Override
802 public void updateProvisionedSubscriberStatus(ServiceKey sk, Boolean status) {
803 try {
804 provisionedSubscribersWriteLock.lock();
805 provisionedSubscribers.put(sk, status);
806 } finally {
807 provisionedSubscribersWriteLock.unlock();
808 }
809 }
810
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800811 protected boolean handleEapolFlow(DiscoveredSubscriber sub, String bandwidthProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700812 String oltBandwidthProfile, FlowOperation action, VlanId vlanId) {
813
814 // create a subscriberKey for the EAPOL flow
815 ServiceKey sk = new ServiceKey(new AccessDevicePort(sub.port), defaultEapolUniTag);
Andrea Campanella87241ae2022-03-11 11:20:24 +0100816 OltFlowsStatus status = action == FlowOperation.ADD ?
817 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700818 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
Andrea Campanella87241ae2022-03-11 11:20:24 +0100819 updateConnectPointStatus(sk, status, OltFlowsStatus.NONE, OltFlowsStatus.NONE,
820 OltFlowsStatus.NONE, OltFlowsStatus.NONE);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700821
Andrea Campanella87241ae2022-03-11 11:20:24 +0100822 } else {
823 updateConnectPointStatus(sk, OltFlowsStatus.NONE, status, OltFlowsStatus.NONE,
824 OltFlowsStatus.NONE, OltFlowsStatus.NONE);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700825 }
826
827 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
828 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
829
830 int techProfileId = getDefaultTechProfileId(sub.port);
831 MeterId meterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), bandwidthProfile);
832
833 // in the delete case the meter should still be there as we remove
834 // the meters only if no flows are pointing to them
835 if (meterId == null) {
836 log.debug("MeterId is null for BandwidthProfile {} on device {}",
837 bandwidthProfile, sub.device.id());
838 return false;
839 }
840
841 MeterId oltMeterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), oltBandwidthProfile);
842 if (oltMeterId == null) {
843 log.debug("MeterId is null for OltBandwidthProfile {} on device {}",
844 oltBandwidthProfile, sub.device.id());
845 return false;
846 }
847
848 log.info("{} EAPOL flow for {} with vlanId {} and BandwidthProfile {} (meterId {})",
849 flowOpToString(action), portWithName(sub.port), vlanId, bandwidthProfile, meterId);
850
851 FilteringObjective.Builder eapolAction;
852
853 if (action == FlowOperation.ADD) {
854 eapolAction = filterBuilder.permit();
855 } else if (action == FlowOperation.REMOVE) {
856 eapolAction = filterBuilder.deny();
857 } else {
858 log.error("Operation {} not supported", action);
859 return false;
860 }
861
862 FilteringObjective.Builder baseEapol = eapolAction
863 .withKey(Criteria.matchInPort(sub.port.number()))
864 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()));
865
866 // NOTE we only need to add the treatment to install the flow,
867 // we can remove it based in the match
868 FilteringObjective.Builder eapol;
869
Harsh Awasthi498b5c62022-03-21 23:19:46 +0530870 // Setting VlanId.NONE as cvlan in the metadata as the packet will be single tagged
871 // and cvlan should not be filled.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700872 TrafficTreatment treatment = treatmentBuilder
873 .meter(meterId)
Harsh Awasthic1e4bf52022-02-09 14:14:14 +0530874 .writeMetadata(OltFlowServiceUtils.createTechProfValueForWriteMetadata(
Harsh Awasthi498b5c62022-03-21 23:19:46 +0530875 VlanId.NONE,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700876 techProfileId, oltMeterId), 0)
877 .setOutput(PortNumber.CONTROLLER)
878 .pushVlan()
879 .setVlanId(vlanId)
880 .build();
881 eapol = baseEapol
882 .withMeta(treatment);
883
884 FilteringObjective eapolObjective = eapol
885 .fromApp(appId)
886 .withPriority(MAX_PRIORITY)
887 .add(new ObjectiveContext() {
888 @Override
889 public void onSuccess(Objective objective) {
890 log.info("EAPOL flow objective {} for {}",
891 completeFlowOpToString(action), portWithName(sub.port));
892 if (log.isTraceEnabled()) {
893 log.trace("EAPOL flow details for port {}: {}", portWithName(sub.port), objective);
894 }
895 }
896
897 @Override
898 public void onError(Objective objective, ObjectiveError error) {
899 log.error("Cannot {} eapol flow for {} : {}", action,
900 portWithName(sub.port), error);
901
902 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
903 updateConnectPointStatus(sk,
Andrea Campanella87241ae2022-03-11 11:20:24 +0100904 OltFlowsStatus.ERROR, null, null, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700905 }
906 }
907 });
908
909 flowObjectiveService.filter(sub.device.id(), eapolObjective);
910
911 log.info("{} EAPOL filter for {}", completeFlowOpToString(action), portWithName(sub.port));
912 return true;
913 }
914
915 // FIXME it's confusing that si is not necessarily inside the DiscoveredSubscriber
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800916 protected boolean handleSubscriberEapolFlows(DiscoveredSubscriber sub, FlowOperation action,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700917 SubscriberAndDeviceInformation si) {
918 if (!enableEapol) {
919 return true;
920 }
921 // TODO verify we need an EAPOL flow for EACH service
922 AtomicBoolean success = new AtomicBoolean(true);
923 si.uniTagList().forEach(u -> {
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200924 //Always act on the eapol flow
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700925 log.info("{} EAPOL flows for subscriber {} on {} and service {}",
926 flowOpToString(action), si.id(), portWithName(sub.port), u.getServiceName());
927 if (!handleEapolFlow(sub, u.getUpstreamBandwidthProfile(),
928 u.getUpstreamOltBandwidthProfile(),
929 action, u.getPonCTag())) {
930 //
Andrea Campanella87241ae2022-03-11 11:20:24 +0100931 log.error("Failed to {} EAPOL with subscriber tags", action);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700932 //TODO this sets it for all services, maybe some services succeeded.
933 success.set(false);
934 }
935 });
936 return success.get();
937 }
938
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800939 protected void handleSubscriberIgmpFlows(DiscoveredSubscriber sub, FlowOperation action) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700940 sub.subscriberAndDeviceInformation.uniTagList().forEach(uti -> {
941 if (uti.getIsIgmpRequired()) {
942 DeviceId deviceId = sub.device.id();
943 // if we reached here a meter already exists
944 MeterId meterId = oltMeterService
945 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
946 MeterId oltMeterId = oltMeterService
947 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
948
949 processIgmpFilteringObjectives(deviceId, sub.port,
950 action, FlowDirection.UPSTREAM, meterId, oltMeterId, uti.getTechnologyProfileId(),
951 uti.getPonCTag(), uti.getUniTagMatch(), uti.getUsPonCTagPriority());
952 }
953 });
954 }
955
956 private boolean checkSadisRunning() {
957 if (bpService == null) {
958 log.warn("Sadis is not running");
959 return false;
960 }
961 return true;
962 }
963
964 private int getDefaultTechProfileId(Port port) {
965 if (!checkSadisRunning()) {
966 return defaultTechProfileId;
967 }
968 if (port != null) {
969 SubscriberAndDeviceInformation info = subsService.get(getPortName(port));
970 if (info != null && info.uniTagList().size() == 1) {
971 return info.uniTagList().get(0).getTechnologyProfileId();
972 }
973 }
974 return defaultTechProfileId;
975 }
976
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700977 private void processLldpFilteringObjective(DeviceId deviceId, Port port, FlowOperation action) {
978 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
979
980 FilteringObjective lldp = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
981 .withKey(Criteria.matchInPort(port.number()))
982 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
983 .withMeta(DefaultTrafficTreatment.builder()
984 .setOutput(PortNumber.CONTROLLER).build())
985 .fromApp(appId)
986 .withPriority(MAX_PRIORITY)
987 .add(new ObjectiveContext() {
988 @Override
989 public void onSuccess(Objective objective) {
990 log.info("{} LLDP filter for {}.", completeFlowOpToString(action), portWithName(port));
991 }
992
993 @Override
994 public void onError(Objective objective, ObjectiveError error) {
995 log.error("Falied to {} LLDP filter on {} because {}", action, portWithName(port),
996 error);
997 }
998 });
999
1000 flowObjectiveService.filter(deviceId, lldp);
1001 }
1002
1003 protected void handleSubscriberDhcpFlows(DeviceId deviceId, Port port,
1004 FlowOperation action,
1005 SubscriberAndDeviceInformation si) {
1006 si.uniTagList().forEach(uti -> {
1007
1008 if (!uti.getIsDhcpRequired()) {
1009 return;
1010 }
1011
1012 // if it's an ADD skip if flows are there,
1013 // if it's a DELETE skip if flows are not there
1014 boolean hasFlows = hasDhcpFlows(port, uti);
1015 if (action == FlowOperation.ADD && hasFlows ||
1016 action == FlowOperation.REMOVE && !hasFlows) {
1017 log.debug("Not dealing with DHCP {} on {} as DHCP flow status is {}", action,
1018 uti.getServiceName(), hasFlows);
1019 return;
1020 }
1021
1022 log.info("{} DHCP flows for subscriber on {} and service {}",
1023 flowOpToString(action), portWithName(port), uti.getServiceName());
1024
1025 // if we reached here a meter already exists
1026 MeterId meterId = oltMeterService
1027 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1028 MeterId oltMeterId = oltMeterService
1029 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1030
1031 if (enableDhcpV4) {
1032 processDhcpFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, 68, 67,
1033 EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP, meterId, oltMeterId,
1034 uti);
1035 }
1036 if (enableDhcpV6) {
1037 log.error("DHCP V6 not supported for subscribers");
1038 }
1039 });
1040 }
1041
yasin sapli0823c932022-01-26 11:26:09 +00001042 protected void handleSubscriberPppoeFlows(DeviceId deviceId, Port port,
1043 FlowOperation action,
1044 SubscriberAndDeviceInformation si) {
1045 si.uniTagList().forEach(uti -> {
1046
1047 if (!uti.getIsPppoeRequired()) {
1048 return;
1049 }
1050
1051 // if it's an ADD skip if flows are there,
1052 // if it's a DELETE skip if flows are not there
1053 boolean hasFlows = hasPppoeFlows(port, uti);
1054 if (action == FlowOperation.ADD && hasFlows ||
1055 action == FlowOperation.REMOVE && !hasFlows) {
1056 log.debug("Not dealing with PPPoE {} on {} as PPPoE flow status is {}", action,
1057 uti.getServiceName(), hasFlows);
1058 return;
1059 }
1060
1061 log.info("{} PPPoE flows for subscriber on {} and service {}",
1062 flowOpToString(action), portWithName(port), uti.getServiceName());
1063
1064 // if we reached here a meter already exists
1065 MeterId meterId = oltMeterService
1066 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1067 MeterId oltMeterId = oltMeterService
1068 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1069
1070 if (enablePppoe) {
1071 processPPPoEDFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, meterId, oltMeterId,
1072 uti.getTechnologyProfileId(), uti.getPonCTag(), uti.getUniTagMatch(),
1073 (byte) uti.getUsPonCTagPriority());
1074 }
1075 });
1076 }
1077
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001078 // FIXME return boolean, if this fails we need to retry
1079 protected void handleSubscriberDataFlows(Device device, Port port,
1080 FlowOperation action,
1081 SubscriberAndDeviceInformation si, String multicastServiceName) {
1082
1083 Optional<Port> nniPort = oltDeviceService.getNniPort(device);
Matteo Scandolo97449bb2021-12-09 15:33:46 -08001084 if (nniPort == null || nniPort.isEmpty()) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001085 log.error("Cannot configure DP flows as upstream port is not configured for subscriber {} on {}",
1086 si.id(), portWithName(port));
1087 return;
1088 }
1089 si.uniTagList().forEach(uti -> {
1090
1091 boolean hasFlows = hasSubscriberFlows(port, uti);
1092 if (action == FlowOperation.ADD && hasFlows ||
1093 action == FlowOperation.REMOVE && !hasFlows) {
1094 log.debug("Not dealing with DP flows {} on {} as subscriber flow status is {}", action,
1095 uti.getServiceName(), hasFlows);
1096 return;
1097 }
1098
1099 if (multicastServiceName.equals(uti.getServiceName())) {
1100 log.debug("This is the multicast service ({}) for subscriber {} on {}, " +
1101 "dataplane flows are not needed",
1102 uti.getServiceName(), si.id(), portWithName(port));
1103 return;
1104 }
1105
1106 log.info("{} Data plane flows for subscriber {} on {} and service {}",
1107 flowOpToString(action), si.id(), portWithName(port), uti.getServiceName());
Matteo Scandolo80f5e972021-12-02 14:59:15 -08001108 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1109 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1110 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
Andrea Campanella87241ae2022-03-11 11:20:24 +01001111 updateConnectPointStatus(sk, null, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001112
1113 // upstream flows
1114 MeterId usMeterId = oltMeterService
1115 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamBandwidthProfile());
1116 MeterId oltUsMeterId = oltMeterService
1117 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamOltBandwidthProfile());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301118
1119 if (FttbUtils.isFttbService(uti)) {
1120 processFttbUpstreamDataFilteringObjects(device.id(), port, nniPort.get(), action,
1121 usMeterId, oltUsMeterId, uti, si);
1122 } else {
1123 processUpstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, usMeterId,
1124 oltUsMeterId, uti);
1125 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001126
1127 // downstream flows
1128 MeterId dsMeterId = oltMeterService
1129 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamBandwidthProfile());
1130 MeterId oltDsMeterId = oltMeterService
1131 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamOltBandwidthProfile());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301132
1133 if (FttbUtils.isFttbService(uti)) {
1134 processFttbDownstreamDataFilteringObjects(device.id(), port, nniPort.get(),
1135 action, dsMeterId, oltDsMeterId, uti, si);
1136 } else {
1137 processDownstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, dsMeterId,
1138 oltDsMeterId, uti, getMacAddress(device.id(), port, uti));
1139 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001140 });
1141 }
1142
1143 private void processDhcpFilteringObjectives(DeviceId deviceId, Port port,
1144 FlowOperation action, FlowDirection direction,
1145 int udpSrc, int udpDst, EthType ethType, byte protocol,
1146 MeterId meterId, MeterId oltMeterId, UniTagInformation uti) {
1147 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1148 log.debug("{} DHCP filtering objectives on {}", flowOpToString(action), sk);
1149
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301150 String serviceName = uti.getServiceName();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001151
1152 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1153 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
Andrea Campanella87241ae2022-03-11 11:20:24 +01001154 updateConnectPointStatus(sk, null, null, null, status, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001155
1156 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1157 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1158
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001159 if (meterId != null) {
1160 treatmentBuilder.meter(meterId);
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) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301174 if (serviceName != null && serviceName.equals(FTTB_SERVICE_DPU_MGMT_TRAFFIC)) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301175 FttbUtils.addUpstreamDhcpCondition(dhcpBuilder, uti);
1176 FttbUtils.addUpstreamDhcpTreatment(treatmentBuilder, uti);
1177 } else {
1178 treatmentBuilder.setVlanId(uti.getPonCTag());
1179 if (!VlanId.vlanId(VlanId.NO_VID).equals(uti.getUniTagMatch())) {
1180 dhcpBuilder.addCondition(Criteria.matchVlanId(uti.getUniTagMatch()));
1181 }
1182 if (uti.getUsPonCTagPriority() != -1) {
1183 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
1184 }
Andrea Campanella0e34f562020-06-11 10:47:10 +02001185 }
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301186 } else if (direction == FlowDirection.DOWNSTREAM) {
1187 // Down stream DHCP vid to be matched if OLT Sadis info contains Vlan id in nniDhcpTrapVid.
1188 Device device = deviceService.getDevice(deviceId);
1189 SubscriberAndDeviceInformation oltSub = subsService.get(device.serialNumber());
1190 VlanId nniDhcpTrapVid = oltSub.nniDhcpTrapVid();
1191
1192 if (nniDhcpTrapVid != null && !VlanId.vlanId(VlanId.NO_VID).equals(nniDhcpTrapVid)) {
1193 dhcpBuilder.addCondition(Criteria.matchVlanId(nniDhcpTrapVid));
Andrea Campanella0e34f562020-06-11 10:47:10 +02001194 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001195 }
1196
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301197 if (uti.getTechnologyProfileId() != NONE_TP_ID) {
Harsh Awasthi498b5c62022-03-21 23:19:46 +05301198 // Setting VlanId.NONE as cvlan, as the packet will be single tagged and cvlan should not be filled.
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301199 treatmentBuilder.writeMetadata(
Harsh Awasthi498b5c62022-03-21 23:19:46 +05301200 OltFlowServiceUtils.createTechProfValueForWriteMetadata(VlanId.NONE,
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301201 uti.getTechnologyProfileId(), oltMeterId), 0);
1202 }
1203
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001204 dhcpBuilder.withMeta(treatmentBuilder
1205 .setOutput(PortNumber.CONTROLLER).build());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001206
1207
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001208 FilteringObjective dhcpUpstream = dhcpBuilder.add(new ObjectiveContext() {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001209 @Override
1210 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001211 log.info("{} DHCP {} filter for {}.",
1212 completeFlowOpToString(action), (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1213 portWithName(port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001214 }
1215
1216 @Override
1217 public void onError(Objective objective, ObjectiveError error) {
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001218 log.error("DHCP {} filter for {} failed {} because {}",
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001219 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1220 portWithName(port),
1221 action,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001222 error);
Andrea Campanella87241ae2022-03-11 11:20:24 +01001223 updateConnectPointStatus(sk, null, null, null, OltFlowsStatus.ERROR, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001224 }
1225 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001226 flowObjectiveService.filter(deviceId, dhcpUpstream);
1227 }
1228
1229 private void processIgmpFilteringObjectives(DeviceId deviceId, Port port,
1230 FlowOperation action, FlowDirection direction,
1231 MeterId meterId, MeterId oltMeterId, int techProfileId,
1232 VlanId cTag, VlanId unitagMatch, int vlanPcp) {
1233
1234 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
1235 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1236 if (direction == FlowDirection.UPSTREAM) {
1237
1238 if (techProfileId != NONE_TP_ID) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301239 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createTechProfValueForWriteMetadata(null,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001240 techProfileId, oltMeterId), 0);
1241 }
1242
1243
1244 if (meterId != null) {
1245 treatmentBuilder.meter(meterId);
1246 }
1247
1248 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1249 filterBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1250 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001251 if (!VlanId.vlanId(VlanId.NO_VID).equals(cTag)) {
1252 treatmentBuilder.setVlanId(cTag);
1253 }
1254
1255 if (vlanPcp != -1) {
1256 treatmentBuilder.setVlanPcp((byte) vlanPcp);
1257 }
1258 }
1259
1260 filterBuilder = (action == FlowOperation.ADD) ? filterBuilder.permit() : filterBuilder.deny();
1261
1262 FilteringObjective igmp = filterBuilder
1263 .withKey(Criteria.matchInPort(port.number()))
1264 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1265 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
1266 .withMeta(treatmentBuilder
1267 .setOutput(PortNumber.CONTROLLER).build())
1268 .fromApp(appId)
1269 .withPriority(MAX_PRIORITY)
1270 .add(new ObjectiveContext() {
1271 @Override
1272 public void onSuccess(Objective objective) {
1273 log.info("Igmp filter for {} {}.", portWithName(port), action);
1274 }
1275
1276 @Override
1277 public void onError(Objective objective, ObjectiveError error) {
1278 log.error("Igmp filter for {} failed {} because {}.", portWithName(port), action,
1279 error);
1280 }
1281 });
1282
1283 flowObjectiveService.filter(deviceId, igmp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001284
1285 }
1286
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001287 private void processPPPoEDFilteringObjectives(DeviceId deviceId, Port port,
1288 FlowOperation action, FlowDirection direction,
1289 MeterId meterId, MeterId oltMeterId, int techProfileId,
1290 VlanId cTag, VlanId unitagMatch, Byte vlanPcp) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001291
1292 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1293 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001294
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001295 if (meterId != null) {
1296 treatmentBuilder.meter(meterId);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001297 }
1298
1299 if (techProfileId != NONE_TP_ID) {
Harsh Awasthi498b5c62022-03-21 23:19:46 +05301300 // Setting VlanId.NONE as cvlan as the packet will be single tagged and cvlan should not be filled.
1301 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createTechProfValueForWriteMetadata(VlanId.NONE,
1302 techProfileId, oltMeterId), 0);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001303 }
1304
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001305 DefaultFilteringObjective.Builder pppoedBuilder = ((action == FlowOperation.ADD)
1306 ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001307 .withKey(Criteria.matchInPort(port.number()))
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001308 .addCondition(Criteria.matchEthType(EthType.EtherType.PPPoED.ethType()))
1309 .fromApp(appId)
1310 .withPriority(10000);
1311
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001312 if (direction == FlowDirection.UPSTREAM) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001313 treatmentBuilder.setVlanId(cTag);
1314 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1315 pppoedBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1316 }
1317 if (vlanPcp != null) {
1318 treatmentBuilder.setVlanPcp(vlanPcp);
1319 }
1320 }
1321 pppoedBuilder = pppoedBuilder.withMeta(treatmentBuilder.setOutput(PortNumber.CONTROLLER).build());
1322
1323 FilteringObjective pppoed = pppoedBuilder
1324 .add(new ObjectiveContext() {
1325 @Override
1326 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001327 log.info("PPPoED filter for {} {}.", portWithName(port), action);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001328 }
1329
1330 @Override
1331 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001332 log.info("PPPoED filter for {} failed {} because {}", portWithName(port),
1333 action, error);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001334 }
1335 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001336 flowObjectiveService.filter(deviceId, pppoed);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001337 }
1338
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001339 private void processUpstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1340 FlowOperation action,
1341 MeterId upstreamMeterId,
1342 MeterId upstreamOltMeterId,
1343 UniTagInformation uti) {
1344 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001345 TrafficSelector selector = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001346 .matchInPort(port.number())
1347 .matchVlanId(uti.getUniTagMatch())
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001348 .build();
1349
Andrea Campanella327c5722020-01-30 11:34:13 +01001350 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1351 //if the subscriberVlan (cTag) is different than ANY it needs to set.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001352 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanella327c5722020-01-30 11:34:13 +01001353 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001354 .setVlanId(uti.getPonCTag());
Andrea Campanella327c5722020-01-30 11:34:13 +01001355 }
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001356 if (uti.getPonSTag().toShort() == VlanId.ANY_VALUE) {
1357 treatmentBuilder.popVlan();
1358 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001359
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001360 if (uti.getUsPonCTagPriority() != -1) {
1361 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001362
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001363 }
1364
1365 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001366 .setVlanId(uti.getPonSTag());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001367
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001368 if (uti.getUsPonSTagPriority() != -1) {
1369 treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001370 }
1371
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001372 treatmentBuilder.setOutput(nniPort.number())
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301373 .writeMetadata(OltFlowServiceUtils.createMetadata(uti.getPonCTag(),
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001374 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001375
yasin saplib4b8ee12021-06-13 18:25:20 +00001376 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1377
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001378 if (upstreamMeterId != null) {
1379 treatmentBuilder.meter(upstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001380 annotationBuilder.set(UPSTREAM_ONU, upstreamMeterId.toString());
1381 }
1382 if (upstreamOltMeterId != null) {
1383 treatmentBuilder.meter(upstreamOltMeterId);
1384 annotationBuilder.set(UPSTREAM_OLT, upstreamOltMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001385 }
1386
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001387 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selector,
1388 treatmentBuilder.build(), MIN_PRIORITY,
yasin saplib4b8ee12021-06-13 18:25:20 +00001389 annotationBuilder.build());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001390
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301391 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.UPSTREAM);
1392 processForwardingRule(action, flowBuilder, context, deviceId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001393 }
1394
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001395 private void processDownstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1396 FlowOperation action,
1397 MeterId downstreamMeterId,
1398 MeterId downstreamOltMeterId,
1399 UniTagInformation uti,
1400 MacAddress macAddress) {
1401 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanella327c5722020-01-30 11:34:13 +01001402 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001403 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001404 .matchVlanId(uti.getPonSTag())
1405 .matchInPort(nniPort.number())
1406 .matchInnerVlanId(uti.getPonCTag());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001407
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001408 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1409 selectorBuilder.matchMetadata(uti.getPonCTag().toShort());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001410 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001411
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001412 if (uti.getDsPonCTagPriority() != -1) {
1413 selectorBuilder.matchVlanPcp((byte) uti.getDsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001414 }
1415
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001416 if (macAddress != null) {
1417 selectorBuilder.matchEthDst(macAddress);
1418 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001419
1420 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
1421 .popVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001422 .setOutput(port.number());
Andrea Campanella327c5722020-01-30 11:34:13 +01001423
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301424 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(uti.getPonCTag(),
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001425 uti.getTechnologyProfileId(),
1426 port.number()), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001427
Andrea Campanella981e86c2021-03-12 11:35:33 +01001428 // Upstream pbit is used to remark inner vlan pbit.
1429 // Upstream is used to avoid trusting the BNG to send the packet with correct pbit.
1430 // this is done because ds mode 0 is used because ds mode 3 or 6 that allow for
1431 // all pbit acceptance are not widely supported by vendors even though present in
1432 // the OMCI spec.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001433 if (uti.getUsPonCTagPriority() != -1) {
1434 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001435 }
1436
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001437 if (!VlanId.NONE.equals(uti.getUniTagMatch()) &&
1438 uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1439 treatmentBuilder.setVlanId(uti.getUniTagMatch());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001440 }
1441
yasin saplib4b8ee12021-06-13 18:25:20 +00001442 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1443
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001444 if (downstreamMeterId != null) {
1445 treatmentBuilder.meter(downstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001446 annotationBuilder.set(DOWNSTREAM_ONU, downstreamMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001447 }
1448
yasin saplib4b8ee12021-06-13 18:25:20 +00001449 if (downstreamOltMeterId != null) {
1450 treatmentBuilder.meter(downstreamOltMeterId);
1451 annotationBuilder.set(DOWNSTREAM_OLT, downstreamOltMeterId.toString());
1452 }
1453
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001454 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1455 treatmentBuilder.build(), MIN_PRIORITY, annotationBuilder.build());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001456
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301457 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.DOWNSTREAM);
1458 processForwardingRule(action, flowBuilder, context, deviceId);
Andrea Campanella600d2e22020-06-22 11:00:31 +02001459 }
1460
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001461 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
1462 TrafficTreatment treatment,
yasin saplib4b8ee12021-06-13 18:25:20 +00001463 Integer priority,
1464 Annotations annotations) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001465 return DefaultForwardingObjective.builder()
1466 .withFlag(ForwardingObjective.Flag.VERSATILE)
1467 .withPriority(priority)
1468 .makePermanent()
1469 .withSelector(selector)
yasin saplib4b8ee12021-06-13 18:25:20 +00001470 .withAnnotations(annotations)
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001471 .fromApp(appId)
1472 .withTreatment(treatment);
1473 }
1474
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001475 private boolean isMacLearningEnabled(SubscriberAndDeviceInformation si) {
1476 AtomicBoolean requiresMacLearning = new AtomicBoolean();
1477 requiresMacLearning.set(false);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001478
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001479 si.uniTagList().forEach(uniTagInfo -> {
1480 if (uniTagInfo.getEnableMacLearning()) {
1481 requiresMacLearning.set(true);
1482 }
1483 });
1484
1485 return requiresMacLearning.get();
1486 }
1487
1488 /**
1489 * Checks whether the subscriber has the MacAddress configured or discovered.
1490 *
1491 * @param deviceId DeviceId for this subscriber
1492 * @param port Port for this subscriber
1493 * @param si SubscriberAndDeviceInformation
1494 * @return boolean
1495 */
1496 protected boolean isMacAddressAvailable(DeviceId deviceId, Port port, SubscriberAndDeviceInformation si) {
1497 AtomicBoolean isConfigured = new AtomicBoolean();
1498 isConfigured.set(true);
1499
1500 si.uniTagList().forEach(uniTagInfo -> {
1501 boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301502 boolean configureMac = OltFlowServiceUtils.isMacAddressValid(uniTagInfo);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001503 boolean discoveredMac = false;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301504
1505 final VlanId vlan;
1506
1507 if (FttbUtils.isFttbDpuOrAncpService(uniTagInfo)) {
1508 // Using S tag, as C tag is replaced by Stag by ONU.
1509 vlan = uniTagInfo.getPonSTag();
1510 } else {
1511 vlan = uniTagInfo.getPonCTag();
1512 }
1513
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001514 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301515 .stream().filter(host -> host.vlan().equals(vlan)).findFirst();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001516 if (optHost.isPresent() && optHost.get().mac() != null) {
1517 discoveredMac = true;
1518 }
1519 if (isMacLearningEnabled && !configureMac && !discoveredMac) {
1520 log.debug("Awaiting for macAddress on {} for service {}",
1521 portWithName(port), uniTagInfo.getServiceName());
1522 isConfigured.set(false);
1523 }
1524 });
1525
1526 return isConfigured.get();
1527 }
1528
1529 protected MacAddress getMacAddress(DeviceId deviceId, Port port, UniTagInformation uniTagInfo) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301530 boolean configuredMac = OltFlowServiceUtils.isMacAddressValid(uniTagInfo);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001531 if (configuredMac) {
1532 return MacAddress.valueOf(uniTagInfo.getConfiguredMacAddress());
1533 } else if (uniTagInfo.getEnableMacLearning()) {
1534 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1535 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1536 if (optHost.isPresent() && optHost.get().mac() != null) {
1537 return optHost.get().mac();
1538 }
1539 }
1540 return null;
1541 }
1542
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001543 protected void updateConnectPointStatus(ServiceKey key, OltFlowsStatus eapolStatus,
Andrea Campanella87241ae2022-03-11 11:20:24 +01001544 OltFlowsStatus subscriberEapolStatus,
yasin sapli0823c932022-01-26 11:26:09 +00001545 OltFlowsStatus subscriberFlowsStatus, OltFlowsStatus dhcpStatus,
1546 OltFlowsStatus pppoeStatus) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001547 if (log.isTraceEnabled()) {
Andrea Campanella87241ae2022-03-11 11:20:24 +01001548 log.trace("Updating cpStatus {} with values: eapolFlow={}, " +
1549 "subscriberEapolStatus={}, subscriberFlows={}, dhcpFlow={}",
1550 key, eapolStatus, subscriberEapolStatus, subscriberFlowsStatus, dhcpStatus);
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001551 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001552 try {
1553 cpStatusWriteLock.lock();
1554 OltPortStatus status = cpStatus.get(key);
1555
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001556
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001557 if (status == null) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001558 // if we don't have status for the connectPoint
1559 // and we're only updating status to PENDING_REMOVE or ERROR
1560 // do not create it. This is because this case will only happen when a device is removed
1561 // and it's status cleaned
1562 List<OltFlowsStatus> statusesToIgnore = new ArrayList<>();
1563 statusesToIgnore.add(OltFlowsStatus.PENDING_REMOVE);
1564 statusesToIgnore.add(OltFlowsStatus.ERROR);
1565
1566 if (
1567 (statusesToIgnore.contains(subscriberFlowsStatus) && dhcpStatus == null) ||
1568 (subscriberFlowsStatus == null && statusesToIgnore.contains(dhcpStatus))
1569 ) {
1570 if (log.isTraceEnabled()) {
1571 log.trace("Ignoring cpStatus update as status is meaningless");
1572 }
1573 return;
1574 }
1575
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001576 status = new OltPortStatus(
1577 eapolStatus != null ? eapolStatus : OltFlowsStatus.NONE,
Andrea Campanella87241ae2022-03-11 11:20:24 +01001578 subscriberEapolStatus != null ? subscriberEapolStatus : OltFlowsStatus.NONE,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001579 subscriberFlowsStatus != null ? subscriberFlowsStatus : OltFlowsStatus.NONE,
yasin sapli0823c932022-01-26 11:26:09 +00001580 dhcpStatus != null ? dhcpStatus : OltFlowsStatus.NONE,
1581 pppoeStatus != null ? pppoeStatus : OltFlowsStatus.NONE
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001582 );
1583 } else {
1584 if (eapolStatus != null) {
1585 status.defaultEapolStatus = eapolStatus;
1586 }
1587 if (subscriberFlowsStatus != null) {
1588 status.subscriberFlowsStatus = subscriberFlowsStatus;
1589 }
1590 if (dhcpStatus != null) {
1591 status.dhcpStatus = dhcpStatus;
1592 }
1593 }
1594
1595 cpStatus.put(key, status);
1596 } finally {
1597 cpStatusWriteLock.unlock();
1598 }
1599 }
1600
1601 protected class InternalFlowListener implements FlowRuleListener {
1602 @Override
1603 public void event(FlowRuleEvent event) {
1604 if (appId.id() != (event.subject().appId())) {
1605 return;
1606 }
1607
1608 if (!oltDeviceService.isLocalLeader(event.subject().deviceId())) {
1609 if (log.isTraceEnabled()) {
1610 log.trace("ignoring flow event {} " +
1611 "as not leader for {}", event, event.subject().deviceId());
1612 }
1613 return;
1614 }
1615
1616 switch (event.type()) {
1617 case RULE_ADDED:
1618 case RULE_REMOVED:
Andrea Campanella40d2b342022-02-04 18:13:37 +01001619 DeviceId deviceId = event.subject().deviceId();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001620 Port port = getCpFromFlowRule(event.subject());
1621 if (port == null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001622 log.warn("Port is gone in ONOS, " +
1623 "manually creating it {}", event.subject());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301624 PortNumber inPort = OltFlowServiceUtils.getPortNumberFromFlowRule(event.subject());
Andrea Campanella40d2b342022-02-04 18:13:37 +01001625 cpStatusReadLock.lock();
1626 Optional<ServiceKey> keyWithPort = cpStatus.keySet()
1627 .stream().filter(key -> key.getPort().connectPoint()
1628 .deviceId().equals(deviceId)
1629 && key.getPort().connectPoint().port()
1630 .equals(inPort)).findFirst();
1631 cpStatusReadLock.unlock();
1632 if (keyWithPort.isPresent()) {
1633 port = new DefaultPort(deviceService.getDevice(deviceId),
1634 inPort, false,
1635 DefaultAnnotations.builder()
1636 .set(AnnotationKeys.PORT_NAME,
1637 keyWithPort.get().getPort().name())
1638 .build());
1639 } else {
1640 log.warn("Can't find corresponding status for {}/{}", deviceId, inPort);
1641 return;
1642 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001643 }
1644 if (log.isTraceEnabled()) {
1645 log.trace("flow event {} on cp {}: {}", event.type(),
1646 portWithName(port), event.subject());
1647 }
1648 updateCpStatus(event.type(), port, event.subject());
1649 return;
1650 case RULE_ADD_REQUESTED:
1651 case RULE_REMOVE_REQUESTED:
1652 // NOTE that PENDING_ADD/REMOVE is set when we create the flowObjective
1653 return;
1654 default:
1655 return;
1656 }
1657 }
1658
1659 protected void updateCpStatus(FlowRuleEvent.Type type, Port port, FlowRule flowRule) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301660 OltFlowsStatus status = OltFlowServiceUtils.flowRuleStatusToOltFlowStatus(type);
1661 if (OltFlowServiceUtils.isDefaultEapolFlow(flowRule)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001662 ServiceKey sk = new ServiceKey(new AccessDevicePort(port),
1663 defaultEapolUniTag);
1664 if (log.isTraceEnabled()) {
1665 log.trace("update defaultEapolStatus {} on {}", status, sk);
1666 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001667 updateConnectPointStatus(sk, status, null, null, null, null);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301668 } else if (OltFlowServiceUtils.isSubscriberEapolFlow(flowRule)) {
Andrea Campanella87241ae2022-03-11 11:20:24 +01001669 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
1670 if (sk == null) {
1671 return;
1672 }
1673 if (log.isTraceEnabled()) {
1674 log.trace("update subscriberEapolStatus {} on {}", status, sk);
1675 }
Andrea Campanella34ce61a2022-04-28 18:55:46 +02001676 updateConnectPointStatus(sk, null, status, null, null, null);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301677 } else if (OltFlowServiceUtils.isDhcpFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001678 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001679 if (sk == null) {
1680 return;
1681 }
1682 if (log.isTraceEnabled()) {
1683 log.trace("update dhcpStatus {} on {}", status, sk);
1684 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001685 updateConnectPointStatus(sk, null, null, null, status, null);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301686 } else if (OltFlowServiceUtils.isPppoeFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001687 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
yasin sapli0823c932022-01-26 11:26:09 +00001688 if (sk == null) {
1689 return;
1690 }
1691 if (log.isTraceEnabled()) {
1692 log.trace("update pppoeStatus {} on {}", status, sk);
1693 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001694 updateConnectPointStatus(sk, null, null, null, null, status);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301695 } else if (OltFlowServiceUtils.isDataFlow(flowRule)) {
1696 PortNumber number = OltFlowServiceUtils.getPortNumberFromFlowRule(flowRule);
Andrea Campanella40d2b342022-02-04 18:13:37 +01001697 if (number == null) {
1698 log.error("Can't capture the port number from flow {}", flowRule);
1699 return;
1700 }
1701 if (oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), number)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001702 // the NNI has data-plane for every subscriber, doesn't make sense to track them
1703 return;
1704 }
1705
Andrea Campanella40d2b342022-02-04 18:13:37 +01001706 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001707 if (sk == null) {
1708 return;
1709 }
1710 if (log.isTraceEnabled()) {
1711 log.trace("update dataplaneStatus {} on {}", status, sk);
1712 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001713 updateConnectPointStatus(sk, null, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001714 }
1715 }
1716
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001717
Andrea Campanella87241ae2022-03-11 11:20:24 +01001718
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001719 private Port getCpFromFlowRule(FlowRule flowRule) {
1720 DeviceId deviceId = flowRule.deviceId();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301721 PortNumber inPort = OltFlowServiceUtils.getPortNumberFromFlowRule(flowRule);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001722 if (inPort != null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001723 return deviceService.getPort(deviceId, inPort);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001724 }
1725 return null;
1726 }
1727
Andrea Campanella40d2b342022-02-04 18:13:37 +01001728 private ServiceKey getSubscriberKeyFromFlowRule(FlowRule flowRule, Port flowPort) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001729 SubscriberAndDeviceInformation si = subsService.get(getPortName(flowPort));
1730
1731 Boolean isNni = oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), flowPort.number());
1732 if (si == null && !isNni) {
1733 log.error("Subscriber information not found in sadis for port {}", portWithName(flowPort));
1734 return null;
1735 }
1736
1737 if (isNni) {
1738 return new ServiceKey(new AccessDevicePort(flowPort), nniUniTag);
1739 }
1740
1741 Optional<UniTagInformation> found = Optional.empty();
1742 VlanId flowVlan = null;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301743 if (OltFlowServiceUtils.isDhcpFlow(flowRule)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001744 // we need to make a special case for DHCP as in the ATT workflow DHCP flows don't match on tags
1745 L2ModificationInstruction.ModVlanIdInstruction instruction =
1746 (L2ModificationInstruction.ModVlanIdInstruction) flowRule.treatment().immediate().get(1);
1747 flowVlan = instruction.vlanId();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301748 } else if (OltFlowServiceUtils.isSubscriberEapolFlow(flowRule)) {
Andrea Campanella87241ae2022-03-11 11:20:24 +01001749 // we need to make a special case for EAPOL as in the ATT workflow EAPOL flows don't match on tags
1750 L2ModificationInstruction.ModVlanIdInstruction instruction =
1751 (L2ModificationInstruction.ModVlanIdInstruction) flowRule.treatment().immediate().get(2);
1752 flowVlan = instruction.vlanId();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001753 } else {
1754 // for now we assume that if it's not DHCP it's dataplane (or at least tagged)
1755 VlanIdCriterion vlanIdCriterion =
1756 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
1757 if (vlanIdCriterion == null) {
1758 log.warn("cannot match the flow to a subscriber service as it does not carry vlans");
1759 return null;
1760 }
1761 flowVlan = vlanIdCriterion.vlanId();
1762 }
1763
1764 VlanId finalFlowVlan = flowVlan;
1765 found = si.uniTagList().stream().filter(uti ->
1766 uti.getPonCTag().equals(finalFlowVlan) ||
1767 uti.getPonSTag().equals(finalFlowVlan) ||
1768 uti.getUniTagMatch().equals(finalFlowVlan)
1769 ).findFirst();
1770
1771
1772 if (found.isEmpty()) {
1773 log.warn("Cannot map flow rule {} to Service in {}", flowRule, si);
1774 }
1775
1776 return found.isPresent() ? new ServiceKey(new AccessDevicePort(flowPort), found.get()) : null;
1777
1778 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001779 }
1780
1781 protected void bindSadisService(SadisService service) {
1782 this.subsService = service.getSubscriberInfoService();
1783 this.bpService = service.getBandwidthProfileService();
1784 log.info("Sadis service is loaded");
1785 }
1786
1787 protected void unbindSadisService(SadisService service) {
1788 this.subsService = null;
1789 this.bpService = null;
1790 log.info("Sadis service is unloaded");
1791 }
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301792
1793 private void processFttbUpstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1794 FlowOperation action,
1795 MeterId upstreamMeterId,
1796 MeterId upstreamOltMeterId,
1797 UniTagInformation uti,
1798 SubscriberAndDeviceInformation si) {
1799 String serviceName = uti.getServiceName();
1800 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1801 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
1802 .matchInPort(port.number())
1803 .matchVlanId(uti.getPonCTag());
1804
1805 if (uti.getUsPonCTagPriority() != -1) {
1806 selectorBuilder.matchVlanPcp((byte) uti.getUsPonCTagPriority());
1807 }
1808
1809 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1810
1811 treatmentBuilder.setVlanId(uti.getPonSTag());
1812 if (uti.getUsPonSTagPriority() != -1) {
1813 treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority());
1814 }
1815
1816 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1817 annotationBuilder.set(FTTB_SERVICE_NAME, serviceName);
1818 annotationBuilder.set(FTTB_FLOW_DIRECTION, FTTB_FLOW_UPSTREAM);
1819
1820 if (upstreamMeterId != null) {
1821 treatmentBuilder.meter(upstreamMeterId);
1822 annotationBuilder.set(UPSTREAM_ONU, upstreamMeterId.toString());
1823 }
1824 if (upstreamOltMeterId != null) {
1825 treatmentBuilder.meter(upstreamOltMeterId);
1826 annotationBuilder.set(UPSTREAM_OLT, upstreamOltMeterId.toString());
1827 }
1828
1829 VlanId innerVlan = null;
Andrea Campanella7ef88992022-05-17 12:38:00 +02001830 treatmentBuilder.setOutput(nniPort.number());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301831 if (serviceName.equals(FTTB_SERVICE_DPU_MGMT_TRAFFIC) || serviceName.equals(FTTB_SERVICE_DPU_ANCP_TRAFFIC)) {
amit.ghosh74a4bb22022-06-14 11:34:52 +02001832 fttbMacAddressesWriteLock.lock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301833 MacAddress mac = FttbUtils.getMacAddressFromDhcpEnabledUti(
amit.ghosh74a4bb22022-06-14 11:34:52 +02001834 hostService, si, deviceId, port, fttbMacAddresses);
1835 fttbMacAddressesWriteLock.unlock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301836
1837 if (mac == null) {
1838 log.error("Mac address not found port:{}, vlan:{}, service:{}",
1839 port, uti.getPonSTag(), serviceName);
1840 return;
1841 }
1842
1843 selectorBuilder.matchEthSrc(mac);
Andrea Campanella7ef88992022-05-17 12:38:00 +02001844
1845 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(VlanId.NONE,
1846 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301847
1848 } else if (serviceName.equals(FTTB_SERVICE_SUBSCRIBER_TRAFFIC)) {
Andrea Campanella7ef88992022-05-17 12:38:00 +02001849 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(VlanId.ANY,
1850 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301851 }
1852
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301853 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1854 treatmentBuilder.build(), MIN_PRIORITY,
1855 annotationBuilder.build());
1856
1857 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.UPSTREAM);
1858 processForwardingRule(action, flowBuilder, context, deviceId);
1859 }
1860
1861 private void processFttbDownstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1862 FlowOperation action,
1863 MeterId downstreamMeterId,
1864 MeterId downstreamOltMeterId,
1865 UniTagInformation uti, SubscriberAndDeviceInformation si) {
1866 String serviceName = uti.getServiceName();
1867 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1868 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
1869 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
1870 .matchVlanId(uti.getPonSTag())
1871 .matchInPort(nniPort.number());
1872
1873 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
1874 .setVlanId(uti.getPonCTag())
1875 .setOutput(port.number());
1876
1877 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1878 annotationBuilder.set(FTTB_SERVICE_NAME, uti.getServiceName());
1879 annotationBuilder.set(FTTB_FLOW_DIRECTION, FTTB_FLOW_DOWNSTREAM);
1880
1881 if (downstreamMeterId != null) {
1882 treatmentBuilder.meter(downstreamMeterId);
1883 annotationBuilder.set(DOWNSTREAM_ONU, downstreamMeterId.toString());
1884 }
1885
1886 if (downstreamOltMeterId != null) {
1887 treatmentBuilder.meter(downstreamOltMeterId);
1888 annotationBuilder.set(DOWNSTREAM_OLT, downstreamOltMeterId.toString());
1889 }
1890
1891 VlanId innerVlan = null;
1892
1893 if (serviceName.equals(FTTB_SERVICE_DPU_MGMT_TRAFFIC) || serviceName.equals(FTTB_SERVICE_DPU_ANCP_TRAFFIC)) {
amit.ghosh74a4bb22022-06-14 11:34:52 +02001894 fttbMacAddressesWriteLock.lock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301895 MacAddress mac = FttbUtils.getMacAddressFromDhcpEnabledUti(
amit.ghosh74a4bb22022-06-14 11:34:52 +02001896 hostService, si, deviceId, port, fttbMacAddresses);
1897 fttbMacAddressesWriteLock.unlock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301898
1899 if (mac == null) {
1900 log.error("Mac address not found port:{}, vlan:{}, service:{}",
1901 port, uti.getPonSTag(), serviceName);
1902 return;
1903 }
1904
1905 selectorBuilder.matchEthDst(mac);
1906 innerVlan = VlanId.NONE;
Andrea Campanella7ef88992022-05-17 12:38:00 +02001907 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(innerVlan,
1908 uti.getTechnologyProfileId(),
1909 port.number()), 0);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301910
1911 } else if (serviceName.equals(FTTB_SERVICE_SUBSCRIBER_TRAFFIC)) {
amit.ghosh4f0910e2022-06-20 15:53:21 +02001912 selectorBuilder.matchMetadata(uti.getPonSTag().toShort());
Andrea Campanella7ef88992022-05-17 12:38:00 +02001913 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(VlanId.ANY,
1914 uti.getTechnologyProfileId(),
1915 port.number()), 0);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301916 }
1917
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301918 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1919 treatmentBuilder.build(), MIN_PRIORITY, annotationBuilder.build());
1920
1921 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.DOWNSTREAM);
1922 processForwardingRule(action, flowBuilder, context, deviceId);
1923 }
1924
1925 private ObjectiveContext getSubscriberFlowBuilderContext(ServiceKey sk, FlowOperation action,
1926 FlowDirection flowDirection) {
1927 ObjectiveContext context = new ObjectiveContext() {
1928 @Override
1929 public void onSuccess(Objective objective) {
1930 log.info("{} {} Data plane filter for {}.",
1931 completeFlowOpToString(action), flowDirection, sk);
1932 }
1933
1934 @Override
1935 public void onError(Objective objective, ObjectiveError error) {
1936 log.info("{} Data plane filter for {} failed {} because {}.",
1937 flowDirection, sk, action, error);
1938 updateConnectPointStatus(sk, null, null, OltFlowsStatus.ERROR, null, null);
1939 }
1940 };
1941
1942 return context;
1943 }
1944
1945 private void processForwardingRule(FlowOperation action, DefaultForwardingObjective.Builder flowBuilder,
1946 ObjectiveContext context, DeviceId deviceId) {
1947 ForwardingObjective flow = null;
1948 if (action == FlowOperation.ADD) {
1949 flow = flowBuilder.add(context);
1950 } else if (action == FlowOperation.REMOVE) {
1951 flow = flowBuilder.remove(context);
1952 } else {
1953 log.error("Flow action not supported: {}", action);
1954 }
1955
1956 if (flow != null) {
1957 if (log.isTraceEnabled()) {
1958 log.trace("Forwarding rule {}", flow);
1959 }
1960 flowObjectiveService.forward(deviceId, flow);
1961 }
1962 }
amit.ghosh74a4bb22022-06-14 11:34:52 +02001963}