blob: 9112f23370d9959dc57c2e91eed0999854f6b754 [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
Gustavo Silva3041f1c2022-07-01 16:17:10 -0300372 Boolean removeOnDisable = Tools.isPropertyEnabled(properties, REMOVE_FLOWS_ON_DISABLE);
373 if (removeOnDisable != null) {
374 removeFlowsOnDisable = removeOnDisable;
375 }
376
yasin sapli0823c932022-01-26 11:26:09 +0000377 log.info("Modified. Values = enableDhcpOnNni: {}, enableDhcpV4: {}, " + "enableDhcpV6:{}, " +
Gustavo Silva3041f1c2022-07-01 16:17:10 -0300378 "enableIgmpOnNni:{}, enableEapol:{}, enablePppoeOnNni: {}, enablePppoe:{}, " +
379 "defaultTechProfileId:{}, waitForRemoval:{}, removeFlowsOnDisable:{}",
yasin sapli0823c932022-01-26 11:26:09 +0000380 enableDhcpOnNni, enableDhcpV4, enableDhcpV6, enableIgmpOnNni, enableEapol,
Gustavo Silva3041f1c2022-07-01 16:17:10 -0300381 enablePppoeOnNni, enablePppoe, defaultTechProfileId, waitForRemoval, removeOnDisable);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000382 }
383
384 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700385 public ImmutableMap<ServiceKey, OltPortStatus> getConnectPointStatus() {
386 try {
387 cpStatusReadLock.lock();
388 return ImmutableMap.copyOf(cpStatus);
389 } finally {
390 cpStatusReadLock.unlock();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000391 }
392 }
393
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700394 @Override
395 public ImmutableMap<ServiceKey, UniTagInformation> getProgrammedSubscribers() {
396 // NOTE we might want to remove this conversion and directly use cpStatus as it contains
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800397 // all the required information about subscribers
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700398 Map<ServiceKey, UniTagInformation> subscribers =
399 new HashMap<>();
400 try {
401 cpStatusReadLock.lock();
402
403 cpStatus.forEach((sk, status) -> {
404 if (
405 // not NNI Port
406 !oltDeviceService.isNniPort(deviceService.getDevice(sk.getPort().connectPoint().deviceId()),
407 sk.getPort().connectPoint().port()) &&
408 // not EAPOL flow
Andrea Campanella982fd332022-01-19 09:14:12 +0100409 !sk.getService().equals(defaultEapolUniTag) &&
410 !status.subscriberFlowsStatus.equals(OltFlowsStatus.PENDING_REMOVE)
411 && !status.subscriberFlowsStatus.equals(OltFlowsStatus.REMOVED)
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800412
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700413 ) {
414 subscribers.put(sk, sk.getService());
415 }
416 });
417
418 return ImmutableMap.copyOf(subscribers);
419 } finally {
420 cpStatusReadLock.unlock();
421 }
422 }
423
424 @Override
425 public Map<ServiceKey, Boolean> getRequestedSubscribers() {
426 try {
427 provisionedSubscribersReadLock.lock();
428 return ImmutableMap.copyOf(provisionedSubscribers);
429 } finally {
430 provisionedSubscribersReadLock.unlock();
431 }
432 }
433
434 @Override
435 public void handleNniFlows(Device device, Port port, FlowOperation action) {
436
437 // always handle the LLDP flow
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800438 log.debug("{} LLDP trap flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700439 processLldpFilteringObjective(device.id(), port, action);
440
441 if (enableDhcpOnNni) {
442 if (enableDhcpV4) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800443 log.debug("{} DHCPv4 trap flow on NNI {} for device {}", flowOpToString(action),
444 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700445 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
446 67, 68, EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP,
447 null, null, nniUniTag);
448 }
449 if (enableDhcpV6) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800450 log.debug("{} DHCPv6 trap flow on NNI {} for device {}", flowOpToString(action),
451 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700452 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
453 546, 547, EthType.EtherType.IPV6.ethType(), IPv6.PROTOCOL_UDP,
454 null, null, nniUniTag);
455 }
456 } else {
Matteo Scandolo1f8de332021-12-06 12:18:24 -0800457 log.debug("DHCP is not required on NNI {} for device {}", portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700458 }
459
460 if (enableIgmpOnNni) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800461 log.debug("{} IGMP flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700462 processIgmpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
463 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, -1);
464 }
465
yasin sapli0823c932022-01-26 11:26:09 +0000466 if (enablePppoeOnNni) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800467 log.debug("{} PPPoE flow on NNI {} for device {}", flowOpToString(action), port.number(), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700468 processPPPoEDFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
469 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, null);
470 }
471 }
472
473 @Override
474 public boolean handleBasicPortFlows(DiscoveredSubscriber sub, String bandwidthProfileId,
475 String oltBandwidthProfileId) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700476 // we only need to something if EAPOL is enabled
477 if (!enableEapol) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100478 log.debug("Eapol is disabled for {}", portWithName(sub.port));
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700479 return true;
480 }
481
482 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
483 return addDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
484 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
485 return removeDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
486 } else {
487 log.error("Unknown Status {} on DiscoveredSubscriber {}", sub.status, sub);
488 return false;
489 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700490 }
491
492 private boolean addDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfileId, String oltBandwidthProfileId) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100493 log.debug("Adding default flows for {}, status {}", portWithName(sub.port), sub.status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700494 if (!oltMeterService.createMeter(sub.device.id(), bandwidthProfileId)) {
495 if (log.isTraceEnabled()) {
496 log.trace("waiting on meter for bp {} and sub {}", bandwidthProfileId, sub);
497 }
498 return false;
499 }
500 if (hasDefaultEapol(sub.port)) {
Andrea Campanellaccb32862022-02-17 16:29:10 +0100501 OltPortStatus status = getOltPortStatus(sub.port, defaultEapolUniTag);
502 log.debug("Eapol is already present for {} with status {}", portWithName(sub.port), status);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700503 return true;
504 }
505 return handleEapolFlow(sub, bandwidthProfileId,
506 oltBandwidthProfileId, FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
507
508 }
509
510 private boolean removeDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfile, String oltBandwidthProfile) {
511 // NOTE that we are not checking for meters as they must have been created to install the flow in first place
512 return handleEapolFlow(sub, bandwidthProfile, oltBandwidthProfile,
513 FlowOperation.REMOVE, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
514 }
515
516 @Override
517 public boolean handleSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwidthProfile,
518 String multicastServiceName) {
519 // NOTE that we are taking defaultBandwithProfile as a parameter as that can be configured in the Olt component
520 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
521 return addSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200522 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED ||
523 sub.status == DiscoveredSubscriber.Status.ADMIN_REMOVED) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700524 return removeSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
525 } else {
526 log.error("don't know how to handle {}", sub);
527 return false;
528 }
529 }
530
531 private boolean addSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
532 String multicastServiceName) {
533 if (log.isTraceEnabled()) {
534 log.trace("Provisioning of subscriber on {} started", portWithName(sub.port));
535 }
536 if (enableEapol) {
537 if (hasDefaultEapol(sub.port)) {
538 // remove EAPOL flow and throw exception so that we'll retry later
539 if (!isDefaultEapolPendingRemoval(sub.port)) {
540 removeDefaultFlows(sub, defaultBandwithProfile, defaultBandwithProfile);
541 }
542
543 if (waitForRemoval) {
544 // NOTE wait for removal is a flag only needed to make sure VOLTHA
545 // does not explode with the flows remove/add in the same batch
546 log.debug("Awaiting for default flows removal for {}", portWithName(sub.port));
547 return false;
548 } else {
549 log.warn("continuing provisioning on {}", portWithName(sub.port));
550 }
551 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700552 }
553
554 // NOTE createMeters will return if the meters are not installed
555 if (!oltMeterService.createMeters(sub.device.id(),
Matteo Scandolo88df8ae2021-11-23 13:12:29 -0800556 sub.subscriberAndDeviceInformation, multicastServiceName)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700557 return false;
558 }
559
560 // NOTE we need to add the DHCP flow regardless so that the host can be discovered and the MacAddress added
561 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.ADD,
562 sub.subscriberAndDeviceInformation);
563
564 if (isMacLearningEnabled(sub.subscriberAndDeviceInformation)
565 && !isMacAddressAvailable(sub.device.id(), sub.port,
566 sub.subscriberAndDeviceInformation)) {
567 log.debug("Awaiting for macAddress on {}", portWithName(sub.port));
568 return false;
569 }
570
Matteo Scandolo8a91c0f2021-12-03 10:58:14 -0800571 // NOTE that the EAPOL flows handling is based on the data-plane flows status
572 // always process them before
573 handleSubscriberEapolFlows(sub, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
574
yasin sapli0823c932022-01-26 11:26:09 +0000575 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
576
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700577 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.ADD,
578 sub.subscriberAndDeviceInformation, multicastServiceName);
579
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700580 handleSubscriberIgmpFlows(sub, FlowOperation.ADD);
581
582 log.info("Provisioning of subscriber on {} completed", portWithName(sub.port));
583 return true;
584 }
585
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800586 protected boolean removeSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700587 String multicastServiceName) {
588
589 if (log.isTraceEnabled()) {
590 log.trace("Removal of subscriber on {} started",
591 portWithName(sub.port));
592 }
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800593 SubscriberAndDeviceInformation si = sub.subscriberAndDeviceInformation;
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200594 //If the port has been removed the device service will return null, while it will be true if it's just disabled
595 boolean isPortPresent = deviceService.getPort(new ConnectPoint(sub.device.id(), sub.port.number())) != null;
596 if (log.isTraceEnabled()) {
597 log.trace("Port {} present: ", portWithName(sub.port), isPortPresent);
598 }
599 // Always remove the EAPOL flow in case of port disable,remove or subscriber remove.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700600 if (enableEapol) {
601 // remove the tagged eapol
602 handleSubscriberEapolFlows(sub, FlowOperation.REMOVE, si);
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200603 log.info("Removal of eapol flow for subscriber on {} completed", portWithName(sub.port));
604
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800605 }
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200606 // If the port is gone entirely (onu delete) or it's enabled (subscriber remove request) remove all the flows
607 // In the case the port is just disabled we remove only the EAPOL flow because the ONU disable only represents
608 // the UNI side of the ONU going down, either for RG cable detach or for an administrative decision, but the PON
609 // side is still up as nothing changed, so no need to add/remove flows, when and if the UNI comes up
610 // we will re-push the EAPOL flow to require the subscriber to auth again.
611 // When the subscriber is admin removed from REST or CLI we ignore the port status.
Andrea Campanella7ef88992022-05-17 12:38:00 +0200612 // Check the admin Status of the port
Andrea Campanella833ce2b2022-06-28 16:36:23 +0200613 if ((!isPortPresent || sub.port.isEnabled() || sub.status == DiscoveredSubscriber.Status.ADMIN_REMOVED)
614 || removeFlowsOnDisable) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700615
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200616 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.REMOVE, si);
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800617
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200618 handleSubscriberPppoeFlows(sub.device.id(), sub.port, FlowOperation.REMOVE,
619 sub.subscriberAndDeviceInformation);
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800620
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200621 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.REMOVE, si, multicastServiceName);
622
623 handleSubscriberIgmpFlows(sub, FlowOperation.REMOVE);
624
625
626 if (enableEapol) {
627
628 // if any of the services still has flows, return false
629 Iterator<UniTagInformation> iter = sub.subscriberAndDeviceInformation.uniTagList().iterator();
630 while (iter.hasNext()) {
631 UniTagInformation entry = iter.next();
632 if (areSubscriberFlowsPendingRemoval(sub.port, entry, enableEapol)) {
633 log.info("Subscriber {} still have flows on service {}, postpone default EAPOL installation.",
634 portWithName(sub.port), entry.getServiceName());
635 return false;
636 }
637 }
638
639 // once the flows are removed add the default one back
640 // (only if the port is ENABLED and still present on the device)
641 if (sub.port.isEnabled() && deviceService.getPort(sub.device.id(), sub.port.number()) != null) {
642
643 // NOTE we remove the subscriber when the port goes down
644 // but in that case we don't need to add default eapol
645 handleEapolFlow(sub, defaultBandwithProfile, defaultBandwithProfile,
646 FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800647 }
648 }
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200649 // FIXME check the return status of the flow and return accordingly
650 log.info("Removal of subscriber on {} completed", portWithName(sub.port));
651 return true;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700652 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700653 return true;
654 }
655
656 @Override
657 public boolean hasDefaultEapol(Port port) {
658 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
659 // NOTE we consider ERROR as a present EAPOL flow as ONOS
660 // will keep trying to add it
661 return status != null && (status.defaultEapolStatus == OltFlowsStatus.ADDED ||
662 status.defaultEapolStatus == OltFlowsStatus.PENDING_ADD ||
663 status.defaultEapolStatus == OltFlowsStatus.ERROR);
664 }
665
666 private OltPortStatus getOltPortStatus(Port port, UniTagInformation uniTagInformation) {
667 try {
668 cpStatusReadLock.lock();
669 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uniTagInformation);
670 OltPortStatus status = cpStatus.get(sk);
671 return status;
672 } finally {
673 cpStatusReadLock.unlock();
674 }
675 }
676
677 public boolean isDefaultEapolPendingRemoval(Port port) {
678 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
679 if (log.isTraceEnabled()) {
680 log.trace("Status during EAPOL flow check {} for port {} and UniTagInformation {}",
681 status, portWithName(port), defaultEapolUniTag);
682 }
683 return status != null && status.defaultEapolStatus == OltFlowsStatus.PENDING_REMOVE;
684 }
685
686 @Override
687 public boolean hasDhcpFlows(Port port, UniTagInformation uti) {
688 OltPortStatus status = getOltPortStatus(port, uti);
689 if (log.isTraceEnabled()) {
690 log.trace("Status during DHCP flow check {} for port {} and service {}",
691 status, portWithName(port), uti.getServiceName());
692 }
693 return status != null &&
694 (status.dhcpStatus == OltFlowsStatus.ADDED ||
695 status.dhcpStatus == OltFlowsStatus.PENDING_ADD);
696 }
697
698 @Override
yasin sapli0823c932022-01-26 11:26:09 +0000699 public boolean hasPppoeFlows(Port port, UniTagInformation uti) {
700 OltPortStatus status = getOltPortStatus(port, uti);
701 if (log.isTraceEnabled()) {
702 log.trace("Status during PPPoE flow check {} for port {} and service {}",
703 status, portWithName(port), uti.getServiceName());
704 }
705 return status != null &&
706 (status.pppoeStatus == OltFlowsStatus.ADDED ||
707 status.pppoeStatus == OltFlowsStatus.PENDING_ADD);
708 }
709
710 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700711 public boolean hasSubscriberFlows(Port port, UniTagInformation uti) {
712
713 OltPortStatus status = getOltPortStatus(port, uti);
714 if (log.isTraceEnabled()) {
715 log.trace("Status during subscriber flow check {} for port {} and service {}",
716 status, portWithName(port), uti.getServiceName());
717 }
718 return status != null && (status.subscriberFlowsStatus == OltFlowsStatus.ADDED ||
719 status.subscriberFlowsStatus == OltFlowsStatus.PENDING_ADD);
720 }
721
Andrea Campanella87241ae2022-03-11 11:20:24 +0100722 public boolean areSubscriberFlowsPendingRemoval(Port port, UniTagInformation uti, boolean enableEapol) {
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800723 OltPortStatus status = getOltPortStatus(port, uti);
724 if (log.isTraceEnabled()) {
725 log.trace("Status during pending_remove flow check {} for port {} and UniTagInformation {}",
726 status, portWithName(port), uti);
727 }
Andrea Campanella87241ae2022-03-11 11:20:24 +0100728 return status != null && (status.subscriberFlowsStatus == OltFlowsStatus.PENDING_REMOVE ||
729 (enableEapol && status.subscriberEapolStatus == OltFlowsStatus.PENDING_REMOVE) ||
730 (uti.getIsDhcpRequired() && status.dhcpStatus == OltFlowsStatus.PENDING_REMOVE));
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800731 }
732
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700733 @Override
734 public void purgeDeviceFlows(DeviceId deviceId) {
735 log.debug("Purging flows on device {}", deviceId);
Matteo Scandolob6981dc2021-12-02 16:31:44 -0800736 flowRuleService.purgeFlowRules(deviceId);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700737
738 // removing the status from the cpStatus map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800739 if (log.isTraceEnabled()) {
740 log.trace("Clearing cp status from device {}", deviceId);
741 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700742 try {
743 cpStatusWriteLock.lock();
744 Iterator<Map.Entry<ServiceKey, OltPortStatus>> iter = cpStatus.entrySet().iterator();
745 while (iter.hasNext()) {
746 Map.Entry<ServiceKey, OltPortStatus> entry = iter.next();
747 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
748 cpStatus.remove(entry.getKey());
749 }
750 }
751 } finally {
752 cpStatusWriteLock.unlock();
753 }
754
755 // removing subscribers from the provisioned map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800756 if (log.isTraceEnabled()) {
757 log.trace("Clearing provisioned subscribers from device {}", deviceId);
758 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700759 try {
760 provisionedSubscribersWriteLock.lock();
761 Iterator<Map.Entry<ServiceKey, Boolean>> iter = provisionedSubscribers.entrySet().iterator();
762 while (iter.hasNext()) {
763 Map.Entry<ServiceKey, Boolean> entry = iter.next();
764 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
765 provisionedSubscribers.remove(entry.getKey());
766 }
767 }
768 } finally {
769 provisionedSubscribersWriteLock.unlock();
770 }
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800771 log.debug("Done clearing up device flows and subscribers");
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700772 }
773
774 @Override
775 public boolean isSubscriberServiceProvisioned(AccessDevicePort cp) {
Andrea Campanella61650a12022-01-24 18:09:44 -0800776 Set<Map.Entry<ServiceKey, Boolean>> subs;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700777 try {
778 provisionedSubscribersReadLock.lock();
Andrea Campanella61650a12022-01-24 18:09:44 -0800779 subs = new HashSet<>(provisionedSubscribers.entrySet());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700780 } finally {
781 provisionedSubscribersReadLock.unlock();
782 }
Andrea Campanella61650a12022-01-24 18:09:44 -0800783
784 for (Map.Entry<ServiceKey, Boolean> entry : subs) {
785 if (entry.getKey().getPort().equals(cp) && entry.getValue()) {
786 return true;
787 }
788 }
789 return false;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700790 }
791
792 @Override
793 public boolean isSubscriberServiceProvisioned(ServiceKey sk) {
794 try {
795 provisionedSubscribersReadLock.lock();
796 Boolean provisioned = provisionedSubscribers.get(sk);
797 if (provisioned == null || !provisioned) {
798 return false;
799 }
800 } finally {
801 provisionedSubscribersReadLock.unlock();
802 }
803 return true;
804 }
805
806 @Override
807 public void updateProvisionedSubscriberStatus(ServiceKey sk, Boolean status) {
808 try {
809 provisionedSubscribersWriteLock.lock();
810 provisionedSubscribers.put(sk, status);
811 } finally {
812 provisionedSubscribersWriteLock.unlock();
813 }
814 }
815
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800816 protected boolean handleEapolFlow(DiscoveredSubscriber sub, String bandwidthProfile,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700817 String oltBandwidthProfile, FlowOperation action, VlanId vlanId) {
818
819 // create a subscriberKey for the EAPOL flow
820 ServiceKey sk = new ServiceKey(new AccessDevicePort(sub.port), defaultEapolUniTag);
Andrea Campanella87241ae2022-03-11 11:20:24 +0100821 OltFlowsStatus status = action == FlowOperation.ADD ?
822 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700823 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
Andrea Campanella87241ae2022-03-11 11:20:24 +0100824 updateConnectPointStatus(sk, status, OltFlowsStatus.NONE, OltFlowsStatus.NONE,
825 OltFlowsStatus.NONE, OltFlowsStatus.NONE);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700826
Andrea Campanella87241ae2022-03-11 11:20:24 +0100827 } else {
828 updateConnectPointStatus(sk, OltFlowsStatus.NONE, status, OltFlowsStatus.NONE,
829 OltFlowsStatus.NONE, OltFlowsStatus.NONE);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700830 }
831
832 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
833 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
834
835 int techProfileId = getDefaultTechProfileId(sub.port);
836 MeterId meterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), bandwidthProfile);
837
838 // in the delete case the meter should still be there as we remove
839 // the meters only if no flows are pointing to them
840 if (meterId == null) {
841 log.debug("MeterId is null for BandwidthProfile {} on device {}",
842 bandwidthProfile, sub.device.id());
843 return false;
844 }
845
846 MeterId oltMeterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), oltBandwidthProfile);
847 if (oltMeterId == null) {
848 log.debug("MeterId is null for OltBandwidthProfile {} on device {}",
849 oltBandwidthProfile, sub.device.id());
850 return false;
851 }
852
853 log.info("{} EAPOL flow for {} with vlanId {} and BandwidthProfile {} (meterId {})",
854 flowOpToString(action), portWithName(sub.port), vlanId, bandwidthProfile, meterId);
855
856 FilteringObjective.Builder eapolAction;
857
858 if (action == FlowOperation.ADD) {
859 eapolAction = filterBuilder.permit();
860 } else if (action == FlowOperation.REMOVE) {
861 eapolAction = filterBuilder.deny();
862 } else {
863 log.error("Operation {} not supported", action);
864 return false;
865 }
866
867 FilteringObjective.Builder baseEapol = eapolAction
868 .withKey(Criteria.matchInPort(sub.port.number()))
869 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()));
870
871 // NOTE we only need to add the treatment to install the flow,
872 // we can remove it based in the match
873 FilteringObjective.Builder eapol;
874
Harsh Awasthi498b5c62022-03-21 23:19:46 +0530875 // Setting VlanId.NONE as cvlan in the metadata as the packet will be single tagged
876 // and cvlan should not be filled.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700877 TrafficTreatment treatment = treatmentBuilder
878 .meter(meterId)
Harsh Awasthic1e4bf52022-02-09 14:14:14 +0530879 .writeMetadata(OltFlowServiceUtils.createTechProfValueForWriteMetadata(
Harsh Awasthi498b5c62022-03-21 23:19:46 +0530880 VlanId.NONE,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700881 techProfileId, oltMeterId), 0)
882 .setOutput(PortNumber.CONTROLLER)
883 .pushVlan()
884 .setVlanId(vlanId)
885 .build();
886 eapol = baseEapol
887 .withMeta(treatment);
888
889 FilteringObjective eapolObjective = eapol
890 .fromApp(appId)
891 .withPriority(MAX_PRIORITY)
892 .add(new ObjectiveContext() {
893 @Override
894 public void onSuccess(Objective objective) {
895 log.info("EAPOL flow objective {} for {}",
896 completeFlowOpToString(action), portWithName(sub.port));
897 if (log.isTraceEnabled()) {
898 log.trace("EAPOL flow details for port {}: {}", portWithName(sub.port), objective);
899 }
900 }
901
902 @Override
903 public void onError(Objective objective, ObjectiveError error) {
904 log.error("Cannot {} eapol flow for {} : {}", action,
905 portWithName(sub.port), error);
906
907 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
908 updateConnectPointStatus(sk,
Andrea Campanella87241ae2022-03-11 11:20:24 +0100909 OltFlowsStatus.ERROR, null, null, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700910 }
911 }
912 });
913
914 flowObjectiveService.filter(sub.device.id(), eapolObjective);
915
916 log.info("{} EAPOL filter for {}", completeFlowOpToString(action), portWithName(sub.port));
917 return true;
918 }
919
920 // FIXME it's confusing that si is not necessarily inside the DiscoveredSubscriber
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800921 protected boolean handleSubscriberEapolFlows(DiscoveredSubscriber sub, FlowOperation action,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700922 SubscriberAndDeviceInformation si) {
923 if (!enableEapol) {
924 return true;
925 }
926 // TODO verify we need an EAPOL flow for EACH service
927 AtomicBoolean success = new AtomicBoolean(true);
928 si.uniTagList().forEach(u -> {
Andrea Campanella34ce61a2022-04-28 18:55:46 +0200929 //Always act on the eapol flow
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700930 log.info("{} EAPOL flows for subscriber {} on {} and service {}",
931 flowOpToString(action), si.id(), portWithName(sub.port), u.getServiceName());
932 if (!handleEapolFlow(sub, u.getUpstreamBandwidthProfile(),
933 u.getUpstreamOltBandwidthProfile(),
934 action, u.getPonCTag())) {
935 //
Andrea Campanella87241ae2022-03-11 11:20:24 +0100936 log.error("Failed to {} EAPOL with subscriber tags", action);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700937 //TODO this sets it for all services, maybe some services succeeded.
938 success.set(false);
939 }
940 });
941 return success.get();
942 }
943
Matteo Scandolo97449bb2021-12-09 15:33:46 -0800944 protected void handleSubscriberIgmpFlows(DiscoveredSubscriber sub, FlowOperation action) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700945 sub.subscriberAndDeviceInformation.uniTagList().forEach(uti -> {
946 if (uti.getIsIgmpRequired()) {
947 DeviceId deviceId = sub.device.id();
948 // if we reached here a meter already exists
949 MeterId meterId = oltMeterService
950 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
951 MeterId oltMeterId = oltMeterService
952 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
953
954 processIgmpFilteringObjectives(deviceId, sub.port,
955 action, FlowDirection.UPSTREAM, meterId, oltMeterId, uti.getTechnologyProfileId(),
956 uti.getPonCTag(), uti.getUniTagMatch(), uti.getUsPonCTagPriority());
957 }
958 });
959 }
960
961 private boolean checkSadisRunning() {
962 if (bpService == null) {
963 log.warn("Sadis is not running");
964 return false;
965 }
966 return true;
967 }
968
969 private int getDefaultTechProfileId(Port port) {
970 if (!checkSadisRunning()) {
971 return defaultTechProfileId;
972 }
973 if (port != null) {
974 SubscriberAndDeviceInformation info = subsService.get(getPortName(port));
975 if (info != null && info.uniTagList().size() == 1) {
976 return info.uniTagList().get(0).getTechnologyProfileId();
977 }
978 }
979 return defaultTechProfileId;
980 }
981
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700982 private void processLldpFilteringObjective(DeviceId deviceId, Port port, FlowOperation action) {
983 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
984
985 FilteringObjective lldp = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
986 .withKey(Criteria.matchInPort(port.number()))
987 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
988 .withMeta(DefaultTrafficTreatment.builder()
989 .setOutput(PortNumber.CONTROLLER).build())
990 .fromApp(appId)
991 .withPriority(MAX_PRIORITY)
992 .add(new ObjectiveContext() {
993 @Override
994 public void onSuccess(Objective objective) {
995 log.info("{} LLDP filter for {}.", completeFlowOpToString(action), portWithName(port));
996 }
997
998 @Override
999 public void onError(Objective objective, ObjectiveError error) {
1000 log.error("Falied to {} LLDP filter on {} because {}", action, portWithName(port),
1001 error);
1002 }
1003 });
1004
1005 flowObjectiveService.filter(deviceId, lldp);
1006 }
1007
1008 protected void handleSubscriberDhcpFlows(DeviceId deviceId, Port port,
1009 FlowOperation action,
1010 SubscriberAndDeviceInformation si) {
1011 si.uniTagList().forEach(uti -> {
1012
1013 if (!uti.getIsDhcpRequired()) {
1014 return;
1015 }
1016
1017 // if it's an ADD skip if flows are there,
1018 // if it's a DELETE skip if flows are not there
1019 boolean hasFlows = hasDhcpFlows(port, uti);
1020 if (action == FlowOperation.ADD && hasFlows ||
1021 action == FlowOperation.REMOVE && !hasFlows) {
1022 log.debug("Not dealing with DHCP {} on {} as DHCP flow status is {}", action,
1023 uti.getServiceName(), hasFlows);
1024 return;
1025 }
1026
1027 log.info("{} DHCP flows for subscriber on {} and service {}",
1028 flowOpToString(action), portWithName(port), uti.getServiceName());
1029
1030 // if we reached here a meter already exists
1031 MeterId meterId = oltMeterService
1032 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1033 MeterId oltMeterId = oltMeterService
1034 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1035
1036 if (enableDhcpV4) {
1037 processDhcpFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, 68, 67,
1038 EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP, meterId, oltMeterId,
1039 uti);
1040 }
1041 if (enableDhcpV6) {
1042 log.error("DHCP V6 not supported for subscribers");
1043 }
1044 });
1045 }
1046
yasin sapli0823c932022-01-26 11:26:09 +00001047 protected void handleSubscriberPppoeFlows(DeviceId deviceId, Port port,
1048 FlowOperation action,
1049 SubscriberAndDeviceInformation si) {
1050 si.uniTagList().forEach(uti -> {
1051
1052 if (!uti.getIsPppoeRequired()) {
1053 return;
1054 }
1055
1056 // if it's an ADD skip if flows are there,
1057 // if it's a DELETE skip if flows are not there
1058 boolean hasFlows = hasPppoeFlows(port, uti);
1059 if (action == FlowOperation.ADD && hasFlows ||
1060 action == FlowOperation.REMOVE && !hasFlows) {
1061 log.debug("Not dealing with PPPoE {} on {} as PPPoE flow status is {}", action,
1062 uti.getServiceName(), hasFlows);
1063 return;
1064 }
1065
1066 log.info("{} PPPoE flows for subscriber on {} and service {}",
1067 flowOpToString(action), portWithName(port), uti.getServiceName());
1068
1069 // if we reached here a meter already exists
1070 MeterId meterId = oltMeterService
1071 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
1072 MeterId oltMeterId = oltMeterService
1073 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
1074
1075 if (enablePppoe) {
1076 processPPPoEDFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, meterId, oltMeterId,
1077 uti.getTechnologyProfileId(), uti.getPonCTag(), uti.getUniTagMatch(),
1078 (byte) uti.getUsPonCTagPriority());
1079 }
1080 });
1081 }
1082
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001083 // FIXME return boolean, if this fails we need to retry
1084 protected void handleSubscriberDataFlows(Device device, Port port,
1085 FlowOperation action,
1086 SubscriberAndDeviceInformation si, String multicastServiceName) {
1087
1088 Optional<Port> nniPort = oltDeviceService.getNniPort(device);
Matteo Scandolo97449bb2021-12-09 15:33:46 -08001089 if (nniPort == null || nniPort.isEmpty()) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001090 log.error("Cannot configure DP flows as upstream port is not configured for subscriber {} on {}",
1091 si.id(), portWithName(port));
1092 return;
1093 }
1094 si.uniTagList().forEach(uti -> {
1095
1096 boolean hasFlows = hasSubscriberFlows(port, uti);
1097 if (action == FlowOperation.ADD && hasFlows ||
1098 action == FlowOperation.REMOVE && !hasFlows) {
1099 log.debug("Not dealing with DP flows {} on {} as subscriber flow status is {}", action,
1100 uti.getServiceName(), hasFlows);
1101 return;
1102 }
1103
1104 if (multicastServiceName.equals(uti.getServiceName())) {
1105 log.debug("This is the multicast service ({}) for subscriber {} on {}, " +
1106 "dataplane flows are not needed",
1107 uti.getServiceName(), si.id(), portWithName(port));
1108 return;
1109 }
1110
1111 log.info("{} Data plane flows for subscriber {} on {} and service {}",
1112 flowOpToString(action), si.id(), portWithName(port), uti.getServiceName());
Matteo Scandolo80f5e972021-12-02 14:59:15 -08001113 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1114 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1115 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
Andrea Campanella87241ae2022-03-11 11:20:24 +01001116 updateConnectPointStatus(sk, null, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001117
1118 // upstream flows
1119 MeterId usMeterId = oltMeterService
1120 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamBandwidthProfile());
1121 MeterId oltUsMeterId = oltMeterService
1122 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamOltBandwidthProfile());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301123
1124 if (FttbUtils.isFttbService(uti)) {
1125 processFttbUpstreamDataFilteringObjects(device.id(), port, nniPort.get(), action,
1126 usMeterId, oltUsMeterId, uti, si);
1127 } else {
1128 processUpstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, usMeterId,
1129 oltUsMeterId, uti);
1130 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001131
1132 // downstream flows
1133 MeterId dsMeterId = oltMeterService
1134 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamBandwidthProfile());
1135 MeterId oltDsMeterId = oltMeterService
1136 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamOltBandwidthProfile());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301137
1138 if (FttbUtils.isFttbService(uti)) {
1139 processFttbDownstreamDataFilteringObjects(device.id(), port, nniPort.get(),
1140 action, dsMeterId, oltDsMeterId, uti, si);
1141 } else {
1142 processDownstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, dsMeterId,
1143 oltDsMeterId, uti, getMacAddress(device.id(), port, uti));
1144 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001145 });
1146 }
1147
1148 private void processDhcpFilteringObjectives(DeviceId deviceId, Port port,
1149 FlowOperation action, FlowDirection direction,
1150 int udpSrc, int udpDst, EthType ethType, byte protocol,
1151 MeterId meterId, MeterId oltMeterId, UniTagInformation uti) {
1152 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1153 log.debug("{} DHCP filtering objectives on {}", flowOpToString(action), sk);
1154
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301155 String serviceName = uti.getServiceName();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001156
1157 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1158 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
Andrea Campanella87241ae2022-03-11 11:20:24 +01001159 updateConnectPointStatus(sk, null, null, null, status, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001160
1161 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1162 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1163
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001164 if (meterId != null) {
1165 treatmentBuilder.meter(meterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001166 }
1167
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001168 FilteringObjective.Builder dhcpBuilder = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001169 .withKey(Criteria.matchInPort(port.number()))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001170 .addCondition(Criteria.matchEthType(ethType))
1171 .addCondition(Criteria.matchIPProtocol(protocol))
1172 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1173 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001174 .fromApp(appId)
1175 .withPriority(MAX_PRIORITY);
1176
Andrea Campanella0e34f562020-06-11 10:47:10 +02001177 //VLAN changes and PCP matching need to happen only in the upstream directions
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001178 if (direction == FlowDirection.UPSTREAM) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301179 if (serviceName != null && serviceName.equals(FTTB_SERVICE_DPU_MGMT_TRAFFIC)) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301180 FttbUtils.addUpstreamDhcpCondition(dhcpBuilder, uti);
1181 FttbUtils.addUpstreamDhcpTreatment(treatmentBuilder, uti);
1182 } else {
1183 treatmentBuilder.setVlanId(uti.getPonCTag());
1184 if (!VlanId.vlanId(VlanId.NO_VID).equals(uti.getUniTagMatch())) {
1185 dhcpBuilder.addCondition(Criteria.matchVlanId(uti.getUniTagMatch()));
1186 }
1187 if (uti.getUsPonCTagPriority() != -1) {
1188 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
1189 }
Andrea Campanella0e34f562020-06-11 10:47:10 +02001190 }
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301191 } else if (direction == FlowDirection.DOWNSTREAM) {
1192 // Down stream DHCP vid to be matched if OLT Sadis info contains Vlan id in nniDhcpTrapVid.
1193 Device device = deviceService.getDevice(deviceId);
1194 SubscriberAndDeviceInformation oltSub = subsService.get(device.serialNumber());
1195 VlanId nniDhcpTrapVid = oltSub.nniDhcpTrapVid();
1196
1197 if (nniDhcpTrapVid != null && !VlanId.vlanId(VlanId.NO_VID).equals(nniDhcpTrapVid)) {
1198 dhcpBuilder.addCondition(Criteria.matchVlanId(nniDhcpTrapVid));
Andrea Campanella0e34f562020-06-11 10:47:10 +02001199 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001200 }
1201
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301202 if (uti.getTechnologyProfileId() != NONE_TP_ID) {
Harsh Awasthi498b5c62022-03-21 23:19:46 +05301203 // 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 +05301204 treatmentBuilder.writeMetadata(
Harsh Awasthi498b5c62022-03-21 23:19:46 +05301205 OltFlowServiceUtils.createTechProfValueForWriteMetadata(VlanId.NONE,
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301206 uti.getTechnologyProfileId(), oltMeterId), 0);
1207 }
1208
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001209 dhcpBuilder.withMeta(treatmentBuilder
1210 .setOutput(PortNumber.CONTROLLER).build());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001211
1212
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001213 FilteringObjective dhcpUpstream = dhcpBuilder.add(new ObjectiveContext() {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001214 @Override
1215 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001216 log.info("{} DHCP {} filter for {}.",
1217 completeFlowOpToString(action), (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1218 portWithName(port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001219 }
1220
1221 @Override
1222 public void onError(Objective objective, ObjectiveError error) {
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001223 log.error("DHCP {} filter for {} failed {} because {}",
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001224 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1225 portWithName(port),
1226 action,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001227 error);
Andrea Campanella87241ae2022-03-11 11:20:24 +01001228 updateConnectPointStatus(sk, null, null, null, OltFlowsStatus.ERROR, null);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001229 }
1230 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001231 flowObjectiveService.filter(deviceId, dhcpUpstream);
1232 }
1233
1234 private void processIgmpFilteringObjectives(DeviceId deviceId, Port port,
1235 FlowOperation action, FlowDirection direction,
1236 MeterId meterId, MeterId oltMeterId, int techProfileId,
1237 VlanId cTag, VlanId unitagMatch, int vlanPcp) {
1238
1239 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
1240 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1241 if (direction == FlowDirection.UPSTREAM) {
1242
1243 if (techProfileId != NONE_TP_ID) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301244 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createTechProfValueForWriteMetadata(null,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001245 techProfileId, oltMeterId), 0);
1246 }
1247
1248
1249 if (meterId != null) {
1250 treatmentBuilder.meter(meterId);
1251 }
1252
1253 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1254 filterBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1255 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001256 if (!VlanId.vlanId(VlanId.NO_VID).equals(cTag)) {
1257 treatmentBuilder.setVlanId(cTag);
1258 }
1259
1260 if (vlanPcp != -1) {
1261 treatmentBuilder.setVlanPcp((byte) vlanPcp);
1262 }
1263 }
1264
1265 filterBuilder = (action == FlowOperation.ADD) ? filterBuilder.permit() : filterBuilder.deny();
1266
1267 FilteringObjective igmp = filterBuilder
1268 .withKey(Criteria.matchInPort(port.number()))
1269 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1270 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
1271 .withMeta(treatmentBuilder
1272 .setOutput(PortNumber.CONTROLLER).build())
1273 .fromApp(appId)
1274 .withPriority(MAX_PRIORITY)
1275 .add(new ObjectiveContext() {
1276 @Override
1277 public void onSuccess(Objective objective) {
1278 log.info("Igmp filter for {} {}.", portWithName(port), action);
1279 }
1280
1281 @Override
1282 public void onError(Objective objective, ObjectiveError error) {
1283 log.error("Igmp filter for {} failed {} because {}.", portWithName(port), action,
1284 error);
1285 }
1286 });
1287
1288 flowObjectiveService.filter(deviceId, igmp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001289
1290 }
1291
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001292 private void processPPPoEDFilteringObjectives(DeviceId deviceId, Port port,
1293 FlowOperation action, FlowDirection direction,
1294 MeterId meterId, MeterId oltMeterId, int techProfileId,
1295 VlanId cTag, VlanId unitagMatch, Byte vlanPcp) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001296
1297 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1298 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001299
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001300 if (meterId != null) {
1301 treatmentBuilder.meter(meterId);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001302 }
1303
1304 if (techProfileId != NONE_TP_ID) {
Harsh Awasthi498b5c62022-03-21 23:19:46 +05301305 // Setting VlanId.NONE as cvlan as the packet will be single tagged and cvlan should not be filled.
1306 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createTechProfValueForWriteMetadata(VlanId.NONE,
1307 techProfileId, oltMeterId), 0);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001308 }
1309
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001310 DefaultFilteringObjective.Builder pppoedBuilder = ((action == FlowOperation.ADD)
1311 ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001312 .withKey(Criteria.matchInPort(port.number()))
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001313 .addCondition(Criteria.matchEthType(EthType.EtherType.PPPoED.ethType()))
1314 .fromApp(appId)
1315 .withPriority(10000);
1316
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001317 if (direction == FlowDirection.UPSTREAM) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001318 treatmentBuilder.setVlanId(cTag);
1319 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1320 pppoedBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1321 }
1322 if (vlanPcp != null) {
1323 treatmentBuilder.setVlanPcp(vlanPcp);
1324 }
1325 }
1326 pppoedBuilder = pppoedBuilder.withMeta(treatmentBuilder.setOutput(PortNumber.CONTROLLER).build());
1327
1328 FilteringObjective pppoed = pppoedBuilder
1329 .add(new ObjectiveContext() {
1330 @Override
1331 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001332 log.info("PPPoED filter for {} {}.", portWithName(port), action);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001333 }
1334
1335 @Override
1336 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001337 log.info("PPPoED filter for {} failed {} because {}", portWithName(port),
1338 action, error);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001339 }
1340 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001341 flowObjectiveService.filter(deviceId, pppoed);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001342 }
1343
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001344 private void processUpstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1345 FlowOperation action,
1346 MeterId upstreamMeterId,
1347 MeterId upstreamOltMeterId,
1348 UniTagInformation uti) {
1349 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001350 TrafficSelector selector = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001351 .matchInPort(port.number())
1352 .matchVlanId(uti.getUniTagMatch())
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001353 .build();
1354
Andrea Campanella327c5722020-01-30 11:34:13 +01001355 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1356 //if the subscriberVlan (cTag) is different than ANY it needs to set.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001357 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanella327c5722020-01-30 11:34:13 +01001358 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001359 .setVlanId(uti.getPonCTag());
Andrea Campanella327c5722020-01-30 11:34:13 +01001360 }
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001361 if (uti.getPonSTag().toShort() == VlanId.ANY_VALUE) {
1362 treatmentBuilder.popVlan();
1363 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001364
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001365 if (uti.getUsPonCTagPriority() != -1) {
1366 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001367
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001368 }
1369
1370 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001371 .setVlanId(uti.getPonSTag());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001372
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001373 if (uti.getUsPonSTagPriority() != -1) {
1374 treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001375 }
1376
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001377 treatmentBuilder.setOutput(nniPort.number())
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301378 .writeMetadata(OltFlowServiceUtils.createMetadata(uti.getPonCTag(),
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001379 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001380
yasin saplib4b8ee12021-06-13 18:25:20 +00001381 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1382
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001383 if (upstreamMeterId != null) {
1384 treatmentBuilder.meter(upstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001385 annotationBuilder.set(UPSTREAM_ONU, upstreamMeterId.toString());
1386 }
1387 if (upstreamOltMeterId != null) {
1388 treatmentBuilder.meter(upstreamOltMeterId);
1389 annotationBuilder.set(UPSTREAM_OLT, upstreamOltMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001390 }
1391
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001392 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selector,
1393 treatmentBuilder.build(), MIN_PRIORITY,
yasin saplib4b8ee12021-06-13 18:25:20 +00001394 annotationBuilder.build());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001395
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301396 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.UPSTREAM);
1397 processForwardingRule(action, flowBuilder, context, deviceId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001398 }
1399
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001400 private void processDownstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1401 FlowOperation action,
1402 MeterId downstreamMeterId,
1403 MeterId downstreamOltMeterId,
1404 UniTagInformation uti,
1405 MacAddress macAddress) {
1406 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanella327c5722020-01-30 11:34:13 +01001407 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001408 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001409 .matchVlanId(uti.getPonSTag())
1410 .matchInPort(nniPort.number())
1411 .matchInnerVlanId(uti.getPonCTag());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001412
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001413 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1414 selectorBuilder.matchMetadata(uti.getPonCTag().toShort());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001415 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001416
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001417 if (uti.getDsPonCTagPriority() != -1) {
1418 selectorBuilder.matchVlanPcp((byte) uti.getDsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001419 }
1420
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001421 if (macAddress != null) {
1422 selectorBuilder.matchEthDst(macAddress);
1423 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001424
1425 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
1426 .popVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001427 .setOutput(port.number());
Andrea Campanella327c5722020-01-30 11:34:13 +01001428
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301429 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(uti.getPonCTag(),
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001430 uti.getTechnologyProfileId(),
1431 port.number()), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001432
Andrea Campanella981e86c2021-03-12 11:35:33 +01001433 // Upstream pbit is used to remark inner vlan pbit.
1434 // Upstream is used to avoid trusting the BNG to send the packet with correct pbit.
1435 // this is done because ds mode 0 is used because ds mode 3 or 6 that allow for
1436 // all pbit acceptance are not widely supported by vendors even though present in
1437 // the OMCI spec.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001438 if (uti.getUsPonCTagPriority() != -1) {
1439 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001440 }
1441
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001442 if (!VlanId.NONE.equals(uti.getUniTagMatch()) &&
1443 uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1444 treatmentBuilder.setVlanId(uti.getUniTagMatch());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001445 }
1446
yasin saplib4b8ee12021-06-13 18:25:20 +00001447 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1448
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001449 if (downstreamMeterId != null) {
1450 treatmentBuilder.meter(downstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001451 annotationBuilder.set(DOWNSTREAM_ONU, downstreamMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001452 }
1453
yasin saplib4b8ee12021-06-13 18:25:20 +00001454 if (downstreamOltMeterId != null) {
1455 treatmentBuilder.meter(downstreamOltMeterId);
1456 annotationBuilder.set(DOWNSTREAM_OLT, downstreamOltMeterId.toString());
1457 }
1458
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001459 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1460 treatmentBuilder.build(), MIN_PRIORITY, annotationBuilder.build());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001461
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301462 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.DOWNSTREAM);
1463 processForwardingRule(action, flowBuilder, context, deviceId);
Andrea Campanella600d2e22020-06-22 11:00:31 +02001464 }
1465
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001466 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
1467 TrafficTreatment treatment,
yasin saplib4b8ee12021-06-13 18:25:20 +00001468 Integer priority,
1469 Annotations annotations) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001470 return DefaultForwardingObjective.builder()
1471 .withFlag(ForwardingObjective.Flag.VERSATILE)
1472 .withPriority(priority)
1473 .makePermanent()
1474 .withSelector(selector)
yasin saplib4b8ee12021-06-13 18:25:20 +00001475 .withAnnotations(annotations)
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001476 .fromApp(appId)
1477 .withTreatment(treatment);
1478 }
1479
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001480 private boolean isMacLearningEnabled(SubscriberAndDeviceInformation si) {
1481 AtomicBoolean requiresMacLearning = new AtomicBoolean();
1482 requiresMacLearning.set(false);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001483
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001484 si.uniTagList().forEach(uniTagInfo -> {
1485 if (uniTagInfo.getEnableMacLearning()) {
1486 requiresMacLearning.set(true);
1487 }
1488 });
1489
1490 return requiresMacLearning.get();
1491 }
1492
1493 /**
1494 * Checks whether the subscriber has the MacAddress configured or discovered.
1495 *
1496 * @param deviceId DeviceId for this subscriber
1497 * @param port Port for this subscriber
1498 * @param si SubscriberAndDeviceInformation
1499 * @return boolean
1500 */
1501 protected boolean isMacAddressAvailable(DeviceId deviceId, Port port, SubscriberAndDeviceInformation si) {
1502 AtomicBoolean isConfigured = new AtomicBoolean();
1503 isConfigured.set(true);
1504
1505 si.uniTagList().forEach(uniTagInfo -> {
1506 boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301507 boolean configureMac = OltFlowServiceUtils.isMacAddressValid(uniTagInfo);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001508 boolean discoveredMac = false;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301509
1510 final VlanId vlan;
1511
1512 if (FttbUtils.isFttbDpuOrAncpService(uniTagInfo)) {
1513 // Using S tag, as C tag is replaced by Stag by ONU.
1514 vlan = uniTagInfo.getPonSTag();
1515 } else {
1516 vlan = uniTagInfo.getPonCTag();
1517 }
1518
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001519 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301520 .stream().filter(host -> host.vlan().equals(vlan)).findFirst();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001521 if (optHost.isPresent() && optHost.get().mac() != null) {
1522 discoveredMac = true;
1523 }
1524 if (isMacLearningEnabled && !configureMac && !discoveredMac) {
1525 log.debug("Awaiting for macAddress on {} for service {}",
1526 portWithName(port), uniTagInfo.getServiceName());
1527 isConfigured.set(false);
1528 }
1529 });
1530
1531 return isConfigured.get();
1532 }
1533
1534 protected MacAddress getMacAddress(DeviceId deviceId, Port port, UniTagInformation uniTagInfo) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301535 boolean configuredMac = OltFlowServiceUtils.isMacAddressValid(uniTagInfo);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001536 if (configuredMac) {
1537 return MacAddress.valueOf(uniTagInfo.getConfiguredMacAddress());
1538 } else if (uniTagInfo.getEnableMacLearning()) {
1539 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1540 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1541 if (optHost.isPresent() && optHost.get().mac() != null) {
1542 return optHost.get().mac();
1543 }
1544 }
1545 return null;
1546 }
1547
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001548 protected void updateConnectPointStatus(ServiceKey key, OltFlowsStatus eapolStatus,
Andrea Campanella87241ae2022-03-11 11:20:24 +01001549 OltFlowsStatus subscriberEapolStatus,
yasin sapli0823c932022-01-26 11:26:09 +00001550 OltFlowsStatus subscriberFlowsStatus, OltFlowsStatus dhcpStatus,
1551 OltFlowsStatus pppoeStatus) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001552 if (log.isTraceEnabled()) {
Andrea Campanella87241ae2022-03-11 11:20:24 +01001553 log.trace("Updating cpStatus {} with values: eapolFlow={}, " +
1554 "subscriberEapolStatus={}, subscriberFlows={}, dhcpFlow={}",
1555 key, eapolStatus, subscriberEapolStatus, subscriberFlowsStatus, dhcpStatus);
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001556 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001557 try {
1558 cpStatusWriteLock.lock();
1559 OltPortStatus status = cpStatus.get(key);
1560
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001561
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001562 if (status == null) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001563 // if we don't have status for the connectPoint
1564 // and we're only updating status to PENDING_REMOVE or ERROR
1565 // do not create it. This is because this case will only happen when a device is removed
1566 // and it's status cleaned
1567 List<OltFlowsStatus> statusesToIgnore = new ArrayList<>();
1568 statusesToIgnore.add(OltFlowsStatus.PENDING_REMOVE);
1569 statusesToIgnore.add(OltFlowsStatus.ERROR);
1570
1571 if (
1572 (statusesToIgnore.contains(subscriberFlowsStatus) && dhcpStatus == null) ||
1573 (subscriberFlowsStatus == null && statusesToIgnore.contains(dhcpStatus))
1574 ) {
1575 if (log.isTraceEnabled()) {
1576 log.trace("Ignoring cpStatus update as status is meaningless");
1577 }
1578 return;
1579 }
1580
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001581 status = new OltPortStatus(
1582 eapolStatus != null ? eapolStatus : OltFlowsStatus.NONE,
Andrea Campanella87241ae2022-03-11 11:20:24 +01001583 subscriberEapolStatus != null ? subscriberEapolStatus : OltFlowsStatus.NONE,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001584 subscriberFlowsStatus != null ? subscriberFlowsStatus : OltFlowsStatus.NONE,
yasin sapli0823c932022-01-26 11:26:09 +00001585 dhcpStatus != null ? dhcpStatus : OltFlowsStatus.NONE,
1586 pppoeStatus != null ? pppoeStatus : OltFlowsStatus.NONE
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001587 );
1588 } else {
1589 if (eapolStatus != null) {
1590 status.defaultEapolStatus = eapolStatus;
1591 }
1592 if (subscriberFlowsStatus != null) {
1593 status.subscriberFlowsStatus = subscriberFlowsStatus;
1594 }
1595 if (dhcpStatus != null) {
1596 status.dhcpStatus = dhcpStatus;
1597 }
1598 }
1599
1600 cpStatus.put(key, status);
1601 } finally {
1602 cpStatusWriteLock.unlock();
1603 }
1604 }
1605
1606 protected class InternalFlowListener implements FlowRuleListener {
1607 @Override
1608 public void event(FlowRuleEvent event) {
1609 if (appId.id() != (event.subject().appId())) {
1610 return;
1611 }
1612
1613 if (!oltDeviceService.isLocalLeader(event.subject().deviceId())) {
1614 if (log.isTraceEnabled()) {
1615 log.trace("ignoring flow event {} " +
1616 "as not leader for {}", event, event.subject().deviceId());
1617 }
1618 return;
1619 }
1620
1621 switch (event.type()) {
1622 case RULE_ADDED:
1623 case RULE_REMOVED:
Andrea Campanella40d2b342022-02-04 18:13:37 +01001624 DeviceId deviceId = event.subject().deviceId();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001625 Port port = getCpFromFlowRule(event.subject());
1626 if (port == null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001627 log.warn("Port is gone in ONOS, " +
1628 "manually creating it {}", event.subject());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301629 PortNumber inPort = OltFlowServiceUtils.getPortNumberFromFlowRule(event.subject());
Andrea Campanella40d2b342022-02-04 18:13:37 +01001630 cpStatusReadLock.lock();
1631 Optional<ServiceKey> keyWithPort = cpStatus.keySet()
1632 .stream().filter(key -> key.getPort().connectPoint()
1633 .deviceId().equals(deviceId)
1634 && key.getPort().connectPoint().port()
1635 .equals(inPort)).findFirst();
1636 cpStatusReadLock.unlock();
1637 if (keyWithPort.isPresent()) {
1638 port = new DefaultPort(deviceService.getDevice(deviceId),
1639 inPort, false,
1640 DefaultAnnotations.builder()
1641 .set(AnnotationKeys.PORT_NAME,
1642 keyWithPort.get().getPort().name())
1643 .build());
1644 } else {
1645 log.warn("Can't find corresponding status for {}/{}", deviceId, inPort);
1646 return;
1647 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001648 }
1649 if (log.isTraceEnabled()) {
1650 log.trace("flow event {} on cp {}: {}", event.type(),
1651 portWithName(port), event.subject());
1652 }
1653 updateCpStatus(event.type(), port, event.subject());
1654 return;
1655 case RULE_ADD_REQUESTED:
1656 case RULE_REMOVE_REQUESTED:
1657 // NOTE that PENDING_ADD/REMOVE is set when we create the flowObjective
1658 return;
1659 default:
1660 return;
1661 }
1662 }
1663
1664 protected void updateCpStatus(FlowRuleEvent.Type type, Port port, FlowRule flowRule) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301665 OltFlowsStatus status = OltFlowServiceUtils.flowRuleStatusToOltFlowStatus(type);
1666 if (OltFlowServiceUtils.isDefaultEapolFlow(flowRule)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001667 ServiceKey sk = new ServiceKey(new AccessDevicePort(port),
1668 defaultEapolUniTag);
1669 if (log.isTraceEnabled()) {
1670 log.trace("update defaultEapolStatus {} on {}", status, sk);
1671 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001672 updateConnectPointStatus(sk, status, null, null, null, null);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301673 } else if (OltFlowServiceUtils.isSubscriberEapolFlow(flowRule)) {
Andrea Campanella87241ae2022-03-11 11:20:24 +01001674 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
1675 if (sk == null) {
1676 return;
1677 }
1678 if (log.isTraceEnabled()) {
1679 log.trace("update subscriberEapolStatus {} on {}", status, sk);
1680 }
Andrea Campanella34ce61a2022-04-28 18:55:46 +02001681 updateConnectPointStatus(sk, null, status, null, null, null);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301682 } else if (OltFlowServiceUtils.isDhcpFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001683 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001684 if (sk == null) {
1685 return;
1686 }
1687 if (log.isTraceEnabled()) {
1688 log.trace("update dhcpStatus {} on {}", status, sk);
1689 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001690 updateConnectPointStatus(sk, null, null, null, status, null);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301691 } else if (OltFlowServiceUtils.isPppoeFlow(flowRule)) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001692 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
yasin sapli0823c932022-01-26 11:26:09 +00001693 if (sk == null) {
1694 return;
1695 }
1696 if (log.isTraceEnabled()) {
1697 log.trace("update pppoeStatus {} on {}", status, sk);
1698 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001699 updateConnectPointStatus(sk, null, null, null, null, status);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301700 } else if (OltFlowServiceUtils.isDataFlow(flowRule)) {
1701 PortNumber number = OltFlowServiceUtils.getPortNumberFromFlowRule(flowRule);
Andrea Campanella40d2b342022-02-04 18:13:37 +01001702 if (number == null) {
1703 log.error("Can't capture the port number from flow {}", flowRule);
1704 return;
1705 }
1706 if (oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), number)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001707 // the NNI has data-plane for every subscriber, doesn't make sense to track them
1708 return;
1709 }
1710
Andrea Campanella40d2b342022-02-04 18:13:37 +01001711 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule, port);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001712 if (sk == null) {
1713 return;
1714 }
1715 if (log.isTraceEnabled()) {
1716 log.trace("update dataplaneStatus {} on {}", status, sk);
1717 }
Andrea Campanella87241ae2022-03-11 11:20:24 +01001718 updateConnectPointStatus(sk, null, null, status, null, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001719 }
1720 }
1721
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001722
Andrea Campanella87241ae2022-03-11 11:20:24 +01001723
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001724 private Port getCpFromFlowRule(FlowRule flowRule) {
1725 DeviceId deviceId = flowRule.deviceId();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301726 PortNumber inPort = OltFlowServiceUtils.getPortNumberFromFlowRule(flowRule);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001727 if (inPort != null) {
Andrea Campanella40d2b342022-02-04 18:13:37 +01001728 return deviceService.getPort(deviceId, inPort);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001729 }
1730 return null;
1731 }
1732
Andrea Campanella40d2b342022-02-04 18:13:37 +01001733 private ServiceKey getSubscriberKeyFromFlowRule(FlowRule flowRule, Port flowPort) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001734 SubscriberAndDeviceInformation si = subsService.get(getPortName(flowPort));
1735
1736 Boolean isNni = oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), flowPort.number());
1737 if (si == null && !isNni) {
1738 log.error("Subscriber information not found in sadis for port {}", portWithName(flowPort));
1739 return null;
1740 }
1741
1742 if (isNni) {
1743 return new ServiceKey(new AccessDevicePort(flowPort), nniUniTag);
1744 }
1745
1746 Optional<UniTagInformation> found = Optional.empty();
1747 VlanId flowVlan = null;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301748 if (OltFlowServiceUtils.isDhcpFlow(flowRule)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001749 // we need to make a special case for DHCP as in the ATT workflow DHCP flows don't match on tags
1750 L2ModificationInstruction.ModVlanIdInstruction instruction =
1751 (L2ModificationInstruction.ModVlanIdInstruction) flowRule.treatment().immediate().get(1);
1752 flowVlan = instruction.vlanId();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301753 } else if (OltFlowServiceUtils.isSubscriberEapolFlow(flowRule)) {
Andrea Campanella87241ae2022-03-11 11:20:24 +01001754 // we need to make a special case for EAPOL as in the ATT workflow EAPOL flows don't match on tags
1755 L2ModificationInstruction.ModVlanIdInstruction instruction =
1756 (L2ModificationInstruction.ModVlanIdInstruction) flowRule.treatment().immediate().get(2);
1757 flowVlan = instruction.vlanId();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001758 } else {
1759 // for now we assume that if it's not DHCP it's dataplane (or at least tagged)
1760 VlanIdCriterion vlanIdCriterion =
1761 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
1762 if (vlanIdCriterion == null) {
1763 log.warn("cannot match the flow to a subscriber service as it does not carry vlans");
1764 return null;
1765 }
1766 flowVlan = vlanIdCriterion.vlanId();
1767 }
1768
1769 VlanId finalFlowVlan = flowVlan;
1770 found = si.uniTagList().stream().filter(uti ->
1771 uti.getPonCTag().equals(finalFlowVlan) ||
1772 uti.getPonSTag().equals(finalFlowVlan) ||
1773 uti.getUniTagMatch().equals(finalFlowVlan)
1774 ).findFirst();
1775
1776
1777 if (found.isEmpty()) {
1778 log.warn("Cannot map flow rule {} to Service in {}", flowRule, si);
1779 }
1780
1781 return found.isPresent() ? new ServiceKey(new AccessDevicePort(flowPort), found.get()) : null;
1782
1783 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001784 }
1785
1786 protected void bindSadisService(SadisService service) {
1787 this.subsService = service.getSubscriberInfoService();
1788 this.bpService = service.getBandwidthProfileService();
1789 log.info("Sadis service is loaded");
1790 }
1791
1792 protected void unbindSadisService(SadisService service) {
1793 this.subsService = null;
1794 this.bpService = null;
1795 log.info("Sadis service is unloaded");
1796 }
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301797
1798 private void processFttbUpstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1799 FlowOperation action,
1800 MeterId upstreamMeterId,
1801 MeterId upstreamOltMeterId,
1802 UniTagInformation uti,
1803 SubscriberAndDeviceInformation si) {
1804 String serviceName = uti.getServiceName();
1805 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1806 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
1807 .matchInPort(port.number())
1808 .matchVlanId(uti.getPonCTag());
1809
1810 if (uti.getUsPonCTagPriority() != -1) {
1811 selectorBuilder.matchVlanPcp((byte) uti.getUsPonCTagPriority());
1812 }
1813
1814 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1815
1816 treatmentBuilder.setVlanId(uti.getPonSTag());
1817 if (uti.getUsPonSTagPriority() != -1) {
1818 treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority());
1819 }
1820
1821 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1822 annotationBuilder.set(FTTB_SERVICE_NAME, serviceName);
1823 annotationBuilder.set(FTTB_FLOW_DIRECTION, FTTB_FLOW_UPSTREAM);
1824
1825 if (upstreamMeterId != null) {
1826 treatmentBuilder.meter(upstreamMeterId);
1827 annotationBuilder.set(UPSTREAM_ONU, upstreamMeterId.toString());
1828 }
1829 if (upstreamOltMeterId != null) {
1830 treatmentBuilder.meter(upstreamOltMeterId);
1831 annotationBuilder.set(UPSTREAM_OLT, upstreamOltMeterId.toString());
1832 }
1833
1834 VlanId innerVlan = null;
Andrea Campanella7ef88992022-05-17 12:38:00 +02001835 treatmentBuilder.setOutput(nniPort.number());
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301836 if (serviceName.equals(FTTB_SERVICE_DPU_MGMT_TRAFFIC) || serviceName.equals(FTTB_SERVICE_DPU_ANCP_TRAFFIC)) {
amit.ghosh74a4bb22022-06-14 11:34:52 +02001837 fttbMacAddressesWriteLock.lock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301838 MacAddress mac = FttbUtils.getMacAddressFromDhcpEnabledUti(
amit.ghosh74a4bb22022-06-14 11:34:52 +02001839 hostService, si, deviceId, port, fttbMacAddresses);
1840 fttbMacAddressesWriteLock.unlock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301841
1842 if (mac == null) {
1843 log.error("Mac address not found port:{}, vlan:{}, service:{}",
1844 port, uti.getPonSTag(), serviceName);
1845 return;
1846 }
1847
1848 selectorBuilder.matchEthSrc(mac);
Andrea Campanella7ef88992022-05-17 12:38:00 +02001849
1850 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(VlanId.NONE,
1851 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301852
1853 } else if (serviceName.equals(FTTB_SERVICE_SUBSCRIBER_TRAFFIC)) {
Andrea Campanella7ef88992022-05-17 12:38:00 +02001854 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(VlanId.ANY,
1855 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301856 }
1857
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301858 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1859 treatmentBuilder.build(), MIN_PRIORITY,
1860 annotationBuilder.build());
1861
1862 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.UPSTREAM);
1863 processForwardingRule(action, flowBuilder, context, deviceId);
1864 }
1865
1866 private void processFttbDownstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1867 FlowOperation action,
1868 MeterId downstreamMeterId,
1869 MeterId downstreamOltMeterId,
1870 UniTagInformation uti, SubscriberAndDeviceInformation si) {
1871 String serviceName = uti.getServiceName();
1872 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1873 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
1874 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
1875 .matchVlanId(uti.getPonSTag())
1876 .matchInPort(nniPort.number());
1877
1878 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
1879 .setVlanId(uti.getPonCTag())
1880 .setOutput(port.number());
1881
1882 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1883 annotationBuilder.set(FTTB_SERVICE_NAME, uti.getServiceName());
1884 annotationBuilder.set(FTTB_FLOW_DIRECTION, FTTB_FLOW_DOWNSTREAM);
1885
1886 if (downstreamMeterId != null) {
1887 treatmentBuilder.meter(downstreamMeterId);
1888 annotationBuilder.set(DOWNSTREAM_ONU, downstreamMeterId.toString());
1889 }
1890
1891 if (downstreamOltMeterId != null) {
1892 treatmentBuilder.meter(downstreamOltMeterId);
1893 annotationBuilder.set(DOWNSTREAM_OLT, downstreamOltMeterId.toString());
1894 }
1895
1896 VlanId innerVlan = null;
1897
1898 if (serviceName.equals(FTTB_SERVICE_DPU_MGMT_TRAFFIC) || serviceName.equals(FTTB_SERVICE_DPU_ANCP_TRAFFIC)) {
amit.ghosh74a4bb22022-06-14 11:34:52 +02001899 fttbMacAddressesWriteLock.lock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301900 MacAddress mac = FttbUtils.getMacAddressFromDhcpEnabledUti(
amit.ghosh74a4bb22022-06-14 11:34:52 +02001901 hostService, si, deviceId, port, fttbMacAddresses);
1902 fttbMacAddressesWriteLock.unlock();
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301903
1904 if (mac == null) {
1905 log.error("Mac address not found port:{}, vlan:{}, service:{}",
1906 port, uti.getPonSTag(), serviceName);
1907 return;
1908 }
1909
1910 selectorBuilder.matchEthDst(mac);
1911 innerVlan = VlanId.NONE;
Andrea Campanella7ef88992022-05-17 12:38:00 +02001912 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(innerVlan,
1913 uti.getTechnologyProfileId(),
1914 port.number()), 0);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301915
1916 } else if (serviceName.equals(FTTB_SERVICE_SUBSCRIBER_TRAFFIC)) {
amit.ghosh4f0910e2022-06-20 15:53:21 +02001917 selectorBuilder.matchMetadata(uti.getPonSTag().toShort());
Andrea Campanella7ef88992022-05-17 12:38:00 +02001918 treatmentBuilder.writeMetadata(OltFlowServiceUtils.createMetadata(VlanId.ANY,
1919 uti.getTechnologyProfileId(),
1920 port.number()), 0);
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301921 }
1922
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301923 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1924 treatmentBuilder.build(), MIN_PRIORITY, annotationBuilder.build());
1925
1926 ObjectiveContext context = getSubscriberFlowBuilderContext(sk, action, FlowDirection.DOWNSTREAM);
1927 processForwardingRule(action, flowBuilder, context, deviceId);
1928 }
1929
1930 private ObjectiveContext getSubscriberFlowBuilderContext(ServiceKey sk, FlowOperation action,
1931 FlowDirection flowDirection) {
1932 ObjectiveContext context = new ObjectiveContext() {
1933 @Override
1934 public void onSuccess(Objective objective) {
1935 log.info("{} {} Data plane filter for {}.",
1936 completeFlowOpToString(action), flowDirection, sk);
1937 }
1938
1939 @Override
1940 public void onError(Objective objective, ObjectiveError error) {
1941 log.info("{} Data plane filter for {} failed {} because {}.",
1942 flowDirection, sk, action, error);
1943 updateConnectPointStatus(sk, null, null, OltFlowsStatus.ERROR, null, null);
1944 }
1945 };
1946
1947 return context;
1948 }
1949
1950 private void processForwardingRule(FlowOperation action, DefaultForwardingObjective.Builder flowBuilder,
1951 ObjectiveContext context, DeviceId deviceId) {
1952 ForwardingObjective flow = null;
1953 if (action == FlowOperation.ADD) {
1954 flow = flowBuilder.add(context);
1955 } else if (action == FlowOperation.REMOVE) {
1956 flow = flowBuilder.remove(context);
1957 } else {
1958 log.error("Flow action not supported: {}", action);
1959 }
1960
1961 if (flow != null) {
1962 if (log.isTraceEnabled()) {
1963 log.trace("Forwarding rule {}", flow);
1964 }
1965 flowObjectiveService.forward(deviceId, flow);
1966 }
1967 }
amit.ghosh74a4bb22022-06-14 11:34:52 +02001968}