blob: a4f2b5f00d7d50f696f0b0b7a67b9021f3a5040a [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;
yasin saplib4b8ee12021-06-13 18:25:20 +000031import org.onosproject.net.Annotations;
Matteo Scandolo3a037a32020-04-01 12:17:50 -070032import org.onosproject.net.ConnectPoint;
yasin saplib4b8ee12021-06-13 18:25:20 +000033import org.onosproject.net.DefaultAnnotations;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070034import org.onosproject.net.Device;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000035import org.onosproject.net.DeviceId;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070036import org.onosproject.net.Host;
37import org.onosproject.net.Port;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000038import org.onosproject.net.PortNumber;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070042import org.onosproject.net.flow.FlowRule;
43import org.onosproject.net.flow.FlowRuleEvent;
44import org.onosproject.net.flow.FlowRuleListener;
45import org.onosproject.net.flow.FlowRuleService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000046import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
48import org.onosproject.net.flow.criteria.Criteria;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070049import org.onosproject.net.flow.criteria.Criterion;
50import org.onosproject.net.flow.criteria.EthTypeCriterion;
51import org.onosproject.net.flow.criteria.IPProtocolCriterion;
52import org.onosproject.net.flow.criteria.PortCriterion;
53import org.onosproject.net.flow.criteria.UdpPortCriterion;
54import org.onosproject.net.flow.criteria.VlanIdCriterion;
55import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000056import org.onosproject.net.flowobjective.DefaultFilteringObjective;
57import org.onosproject.net.flowobjective.DefaultForwardingObjective;
58import org.onosproject.net.flowobjective.FilteringObjective;
59import org.onosproject.net.flowobjective.FlowObjectiveService;
60import org.onosproject.net.flowobjective.ForwardingObjective;
61import org.onosproject.net.flowobjective.Objective;
62import org.onosproject.net.flowobjective.ObjectiveContext;
63import org.onosproject.net.flowobjective.ObjectiveError;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070064import org.onosproject.net.host.HostService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000065import org.onosproject.net.meter.MeterId;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000066import org.onosproject.store.serializers.KryoNamespaces;
67import org.onosproject.store.service.Serializer;
68import org.onosproject.store.service.StorageService;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000069import org.opencord.sadis.BandwidthProfileInformation;
70import org.opencord.sadis.BaseInformationService;
71import org.opencord.sadis.SadisService;
72import org.opencord.sadis.SubscriberAndDeviceInformation;
73import org.opencord.sadis.UniTagInformation;
74import org.osgi.service.component.ComponentContext;
75import org.osgi.service.component.annotations.Activate;
76import org.osgi.service.component.annotations.Component;
77import org.osgi.service.component.annotations.Deactivate;
78import org.osgi.service.component.annotations.Modified;
79import org.osgi.service.component.annotations.Reference;
80import org.osgi.service.component.annotations.ReferenceCardinality;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000081import org.osgi.service.component.annotations.ReferencePolicy;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000082import org.slf4j.Logger;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070083import org.slf4j.LoggerFactory;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000084
Matteo Scandolo2542e5d2021-12-01 16:53:41 -080085import java.util.ArrayList;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +010086import java.util.Dictionary;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070087import java.util.HashMap;
88import java.util.Iterator;
Matteo Scandolo2542e5d2021-12-01 16:53:41 -080089import java.util.List;
Ilayda Ozdemir90a93622021-02-25 09:40:58 +000090import java.util.Map;
Andrea Campanellabfb47af2021-06-03 11:09:45 +020091import java.util.Optional;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +010092import java.util.Properties;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070093import java.util.concurrent.atomic.AtomicBoolean;
94import java.util.concurrent.locks.Lock;
95import java.util.concurrent.locks.ReentrantReadWriteLock;
Andrea Campanella7a1d7e72020-11-05 10:40:10 +010096
97import static com.google.common.base.Strings.isNullOrEmpty;
98import static org.onlab.util.Tools.get;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070099import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION;
100import static org.opencord.olt.impl.OltUtils.completeFlowOpToString;
101import static org.opencord.olt.impl.OltUtils.flowOpToString;
102import static org.opencord.olt.impl.OltUtils.getPortName;
103import static org.opencord.olt.impl.OltUtils.portWithName;
104import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID;
105import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID_DEFAULT;
106import static org.opencord.olt.impl.OsgiPropertyConstants.DOWNSTREAM_OLT;
107import static org.opencord.olt.impl.OsgiPropertyConstants.DOWNSTREAM_ONU;
108import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_ON_NNI;
109import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_ON_NNI_DEFAULT;
110import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V4;
111import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V4_DEFAULT;
112import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V6;
113import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V6_DEFAULT;
114import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_EAPOL;
115import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_EAPOL_DEFAULT;
116import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_IGMP_ON_NNI;
117import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_IGMP_ON_NNI_DEFAULT;
118import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_PPPOE;
119import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_PPPOE_DEFAULT;
120import static org.opencord.olt.impl.OsgiPropertyConstants.UPSTREAM_OLT;
121import static org.opencord.olt.impl.OsgiPropertyConstants.UPSTREAM_ONU;
122import static org.opencord.olt.impl.OsgiPropertyConstants.WAIT_FOR_REMOVAL;
123import static org.opencord.olt.impl.OsgiPropertyConstants.WAIT_FOR_REMOVAL_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000124
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000125@Component(immediate = true, property = {
Saurav Dasf62cea82020-08-26 17:43:04 -0700126 ENABLE_DHCP_ON_NNI + ":Boolean=" + ENABLE_DHCP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000127 ENABLE_DHCP_V4 + ":Boolean=" + ENABLE_DHCP_V4_DEFAULT,
128 ENABLE_DHCP_V6 + ":Boolean=" + ENABLE_DHCP_V6_DEFAULT,
Saurav Dasf62cea82020-08-26 17:43:04 -0700129 ENABLE_IGMP_ON_NNI + ":Boolean=" + ENABLE_IGMP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000130 ENABLE_EAPOL + ":Boolean=" + ENABLE_EAPOL_DEFAULT,
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300131 ENABLE_PPPOE + ":Boolean=" + ENABLE_PPPOE_DEFAULT,
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700132 DEFAULT_TP_ID + ":Integer=" + DEFAULT_TP_ID_DEFAULT,
133 // FIXME remove this option as potentially dangerous in production
134 WAIT_FOR_REMOVAL + ":Boolean=" + WAIT_FOR_REMOVAL_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;
178 private static final short EAPOL_DEFAULT_VLAN = 4091;
179 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 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700214 * Create DHCP trap flow on NNI port(s).
215 */
216 protected boolean enableDhcpOnNni = ENABLE_DHCP_ON_NNI_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000217
218 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700219 * Enable flows for DHCP v4 if dhcp is required in sadis config.
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000220 **/
221 protected boolean enableDhcpV4 = ENABLE_DHCP_V4_DEFAULT;
222
223 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700224 * Enable flows for DHCP v6 if dhcp is required in sadis config.
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000225 **/
226 protected boolean enableDhcpV6 = ENABLE_DHCP_V6_DEFAULT;
227
228 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700229 * Create IGMP trap flow on NNI port(s).
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000230 **/
Saurav Dasf62cea82020-08-26 17:43:04 -0700231 protected boolean enableIgmpOnNni = ENABLE_IGMP_ON_NNI_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000232
233 /**
234 * Send EAPOL authentication trap flows before subscriber provisioning.
235 **/
236 protected boolean enableEapol = ENABLE_EAPOL_DEFAULT;
237
238 /**
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300239 * Send PPPoED authentication trap flows before subscriber provisioning.
240 **/
241 protected boolean enablePppoe = ENABLE_PPPOE_DEFAULT;
242
243 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000244 * Default technology profile id that is used for authentication trap flows.
245 **/
246 protected int defaultTechProfileId = DEFAULT_TP_ID_DEFAULT;
247
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700248 protected boolean waitForRemoval = WAIT_FOR_REMOVAL_DEFAULT;
249
250 public enum FlowOperation {
251 ADD,
252 REMOVE;
253
254
255 @Override
256 public String toString() {
257 return super.toString().toLowerCase();
258 }
259 }
260
261 public enum FlowDirection {
262 UPSTREAM,
263 DOWNSTREAM,
264 }
265
266 public enum OltFlowsStatus {
267 NONE,
268 PENDING_ADD,
269 ADDED,
270 PENDING_REMOVE,
271 REMOVED,
272 ERROR
273 }
274
275 protected InternalFlowListener internalFlowListener;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000276
277 @Activate
278 public void activate(ComponentContext context) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700279 cfgService.registerProperties(getClass());
280 appId = coreService.registerApplication(APP_NAME);
281 internalFlowListener = new InternalFlowListener();
282
283 modified(context);
284
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000285 KryoNamespace serializer = KryoNamespace.newBuilder()
286 .register(KryoNamespaces.API)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700287 .register(OltFlowsStatus.class)
288 .register(FlowDirection.class)
289 .register(OltPortStatus.class)
290 .register(OltFlowsStatus.class)
Tunahan Sezenf0843b92021-04-30 07:13:16 +0000291 .register(AccessDevicePort.class)
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700292 .register(new ServiceKeySerializer(), ServiceKey.class)
293 .register(UniTagInformation.class)
Ilayda Ozdemir90a93622021-02-25 09:40:58 +0000294 .build();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000295
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700296 cpStatus = storageService.<ServiceKey, OltPortStatus>consistentMapBuilder()
297 .withName("volt-cp-status")
298 .withApplicationId(appId)
299 .withSerializer(Serializer.using(serializer))
300 .build().asJavaMap();
301
302 provisionedSubscribers = storageService.<ServiceKey, Boolean>consistentMapBuilder()
303 .withName("volt-provisioned-subscriber")
304 .withApplicationId(appId)
305 .withSerializer(Serializer.using(serializer))
306 .build().asJavaMap();
307
308 flowRuleService.addListener(internalFlowListener);
309
310 log.info("Started");
311 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000312
313 @Deactivate
314 public void deactivate(ComponentContext context) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700315 cfgService.unregisterProperties(getClass(), false);
316 flowRuleService.removeListener(internalFlowListener);
317 log.info("Stopped");
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000318 }
319
320 @Modified
321 public void modified(ComponentContext context) {
322
323 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
324
Saurav Dasf62cea82020-08-26 17:43:04 -0700325 Boolean o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000326 if (o != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700327 enableDhcpOnNni = o;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000328 }
329
Andrea Campanella7c49b792020-05-11 11:36:53 +0200330 Boolean v4 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V4);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000331 if (v4 != null) {
332 enableDhcpV4 = v4;
333 }
334
Andrea Campanella7c49b792020-05-11 11:36:53 +0200335 Boolean v6 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V6);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000336 if (v6 != null) {
337 enableDhcpV6 = v6;
338 }
339
Saurav Dasf62cea82020-08-26 17:43:04 -0700340 Boolean p = Tools.isPropertyEnabled(properties, ENABLE_IGMP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000341 if (p != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700342 enableIgmpOnNni = p;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000343 }
344
Andrea Campanella7c49b792020-05-11 11:36:53 +0200345 Boolean eap = Tools.isPropertyEnabled(properties, ENABLE_EAPOL);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000346 if (eap != null) {
347 enableEapol = eap;
348 }
349
Gustavo Silva5c492dd2021-02-12 10:21:11 -0300350 Boolean pppoe = Tools.isPropertyEnabled(properties, ENABLE_PPPOE);
351 if (pppoe != null) {
352 enablePppoe = pppoe;
353 }
354
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700355 Boolean wait = Tools.isPropertyEnabled(properties, WAIT_FOR_REMOVAL);
356 if (wait != null) {
357 waitForRemoval = wait;
358 }
359
Andrea Campanella7c49b792020-05-11 11:36:53 +0200360 String tpId = get(properties, DEFAULT_TP_ID);
361 defaultTechProfileId = isNullOrEmpty(tpId) ? DEFAULT_TP_ID_DEFAULT : Integer.parseInt(tpId.trim());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000362
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700363 log.info("Modified. Values = enableDhcpOnNni: {}, enableDhcpV4: {}, " +
364 "enableDhcpV6:{}, enableIgmpOnNni:{}, " +
365 "enableEapol:{}, enablePppoe:{}, defaultTechProfileId:{}," +
366 "waitForRemoval:{}",
367 enableDhcpOnNni, enableDhcpV4, enableDhcpV6,
368 enableIgmpOnNni, enableEapol, enablePppoe,
369 defaultTechProfileId, waitForRemoval);
Andrea Campanellafee86422020-06-04 16:01:27 +0200370
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000371 }
372
373 @Override
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700374 public ImmutableMap<ServiceKey, OltPortStatus> getConnectPointStatus() {
375 try {
376 cpStatusReadLock.lock();
377 return ImmutableMap.copyOf(cpStatus);
378 } finally {
379 cpStatusReadLock.unlock();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000380 }
381 }
382
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700383 @Override
384 public ImmutableMap<ServiceKey, UniTagInformation> getProgrammedSubscribers() {
385 // NOTE we might want to remove this conversion and directly use cpStatus as it contains
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800386 // all the required information about subscribers
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700387 Map<ServiceKey, UniTagInformation> subscribers =
388 new HashMap<>();
389 try {
390 cpStatusReadLock.lock();
391
392 cpStatus.forEach((sk, status) -> {
393 if (
394 // not NNI Port
395 !oltDeviceService.isNniPort(deviceService.getDevice(sk.getPort().connectPoint().deviceId()),
396 sk.getPort().connectPoint().port()) &&
397 // not EAPOL flow
398 !sk.getService().equals(defaultEapolUniTag)
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800399
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700400 ) {
401 subscribers.put(sk, sk.getService());
402 }
403 });
404
405 return ImmutableMap.copyOf(subscribers);
406 } finally {
407 cpStatusReadLock.unlock();
408 }
409 }
410
411 @Override
412 public Map<ServiceKey, Boolean> getRequestedSubscribers() {
413 try {
414 provisionedSubscribersReadLock.lock();
415 return ImmutableMap.copyOf(provisionedSubscribers);
416 } finally {
417 provisionedSubscribersReadLock.unlock();
418 }
419 }
420
421 @Override
422 public void handleNniFlows(Device device, Port port, FlowOperation action) {
423
424 // always handle the LLDP flow
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800425 log.debug("{} LLDP trap flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700426 processLldpFilteringObjective(device.id(), port, action);
427
428 if (enableDhcpOnNni) {
429 if (enableDhcpV4) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800430 log.debug("{} DHCPv4 trap flow on NNI {} for device {}", flowOpToString(action),
431 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700432 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
433 67, 68, EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP,
434 null, null, nniUniTag);
435 }
436 if (enableDhcpV6) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800437 log.debug("{} DHCPv6 trap flow on NNI {} for device {}", flowOpToString(action),
438 portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700439 processDhcpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
440 546, 547, EthType.EtherType.IPV6.ethType(), IPv6.PROTOCOL_UDP,
441 null, null, nniUniTag);
442 }
443 } else {
Matteo Scandolo1f8de332021-12-06 12:18:24 -0800444 log.debug("DHCP is not required on NNI {} for device {}", portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700445 }
446
447 if (enableIgmpOnNni) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800448 log.debug("{} IGMP flow on NNI {} for device {}", flowOpToString(action), portWithName(port), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700449 processIgmpFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
450 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, -1);
451 }
452
453 if (enablePppoe) {
Matteo Scandolod7aa89c2021-12-07 10:21:34 -0800454 log.debug("{} PPPoE flow on NNI {} for device {}", flowOpToString(action), port.number(), device.id());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700455 processPPPoEDFilteringObjectives(device.id(), port, action, FlowDirection.DOWNSTREAM,
456 null, null, NONE_TP_ID, VlanId.NONE, VlanId.ANY, null);
457 }
458 }
459
460 @Override
461 public boolean handleBasicPortFlows(DiscoveredSubscriber sub, String bandwidthProfileId,
462 String oltBandwidthProfileId) {
463
464 // we only need to something if EAPOL is enabled
465 if (!enableEapol) {
466 return true;
467 }
468
469 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
470 return addDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
471 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
472 return removeDefaultFlows(sub, bandwidthProfileId, oltBandwidthProfileId);
473 } else {
474 log.error("Unknown Status {} on DiscoveredSubscriber {}", sub.status, sub);
475 return false;
476 }
477
478 }
479
480 private boolean addDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfileId, String oltBandwidthProfileId) {
481
482 if (!oltMeterService.createMeter(sub.device.id(), bandwidthProfileId)) {
483 if (log.isTraceEnabled()) {
484 log.trace("waiting on meter for bp {} and sub {}", bandwidthProfileId, sub);
485 }
486 return false;
487 }
488 if (hasDefaultEapol(sub.port)) {
489 return true;
490 }
491 return handleEapolFlow(sub, bandwidthProfileId,
492 oltBandwidthProfileId, FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
493
494 }
495
496 private boolean removeDefaultFlows(DiscoveredSubscriber sub, String bandwidthProfile, String oltBandwidthProfile) {
497 // NOTE that we are not checking for meters as they must have been created to install the flow in first place
498 return handleEapolFlow(sub, bandwidthProfile, oltBandwidthProfile,
499 FlowOperation.REMOVE, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
500 }
501
502 @Override
503 public boolean handleSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwidthProfile,
504 String multicastServiceName) {
505 // NOTE that we are taking defaultBandwithProfile as a parameter as that can be configured in the Olt component
506 if (sub.status == DiscoveredSubscriber.Status.ADDED) {
507 return addSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
508 } else if (sub.status == DiscoveredSubscriber.Status.REMOVED) {
509 return removeSubscriberFlows(sub, defaultBandwidthProfile, multicastServiceName);
510 } else {
511 log.error("don't know how to handle {}", sub);
512 return false;
513 }
514 }
515
516 private boolean addSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
517 String multicastServiceName) {
518 if (log.isTraceEnabled()) {
519 log.trace("Provisioning of subscriber on {} started", portWithName(sub.port));
520 }
521 if (enableEapol) {
522 if (hasDefaultEapol(sub.port)) {
523 // remove EAPOL flow and throw exception so that we'll retry later
524 if (!isDefaultEapolPendingRemoval(sub.port)) {
525 removeDefaultFlows(sub, defaultBandwithProfile, defaultBandwithProfile);
526 }
527
528 if (waitForRemoval) {
529 // NOTE wait for removal is a flag only needed to make sure VOLTHA
530 // does not explode with the flows remove/add in the same batch
531 log.debug("Awaiting for default flows removal for {}", portWithName(sub.port));
532 return false;
533 } else {
534 log.warn("continuing provisioning on {}", portWithName(sub.port));
535 }
536 }
537
538 }
539
540 // NOTE createMeters will return if the meters are not installed
541 if (!oltMeterService.createMeters(sub.device.id(),
Matteo Scandolo88df8ae2021-11-23 13:12:29 -0800542 sub.subscriberAndDeviceInformation, multicastServiceName)) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700543 return false;
544 }
545
546 // NOTE we need to add the DHCP flow regardless so that the host can be discovered and the MacAddress added
547 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.ADD,
548 sub.subscriberAndDeviceInformation);
549
550 if (isMacLearningEnabled(sub.subscriberAndDeviceInformation)
551 && !isMacAddressAvailable(sub.device.id(), sub.port,
552 sub.subscriberAndDeviceInformation)) {
553 log.debug("Awaiting for macAddress on {}", portWithName(sub.port));
554 return false;
555 }
556
Matteo Scandolo8a91c0f2021-12-03 10:58:14 -0800557 // NOTE that the EAPOL flows handling is based on the data-plane flows status
558 // always process them before
559 handleSubscriberEapolFlows(sub, FlowOperation.ADD, sub.subscriberAndDeviceInformation);
560
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700561 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.ADD,
562 sub.subscriberAndDeviceInformation, multicastServiceName);
563
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700564 handleSubscriberIgmpFlows(sub, FlowOperation.ADD);
565
566 log.info("Provisioning of subscriber on {} completed", portWithName(sub.port));
567 return true;
568 }
569
570 private boolean removeSubscriberFlows(DiscoveredSubscriber sub, String defaultBandwithProfile,
571 String multicastServiceName) {
572
573 if (log.isTraceEnabled()) {
574 log.trace("Removal of subscriber on {} started",
575 portWithName(sub.port));
576 }
577 SubscriberAndDeviceInformation si = subsService.get(sub.portName());
578 if (si == null) {
579 log.error("Subscriber information not found in sadis for port {} during subscriber removal",
580 portWithName(sub.port));
581 // NOTE that we are returning true so that the subscriber is removed from the queue
582 // and we can move on provisioning others
583 return true;
584 }
585
586 handleSubscriberDhcpFlows(sub.device.id(), sub.port, FlowOperation.REMOVE, si);
587
588 if (enableEapol) {
589 // remove the tagged eapol
590 handleSubscriberEapolFlows(sub, FlowOperation.REMOVE, si);
591
Matteo Scandolo49c42052021-11-23 13:12:29 -0800592 // and add the default one back (only if the port is ENABLED and still present on the device)
593 if (sub.port.isEnabled() && deviceService.getPort(sub.device.id(), sub.port.number()) != null) {
594
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700595 // NOTE we remove the subscriber when the port goes down
596 // but in that case we don't need to add default eapol
597 handleEapolFlow(sub, defaultBandwithProfile, defaultBandwithProfile,
598 FlowOperation.ADD, VlanId.vlanId(EAPOL_DEFAULT_VLAN));
599 }
600 }
601 handleSubscriberDataFlows(sub.device, sub.port, FlowOperation.REMOVE, si, multicastServiceName);
602
603 handleSubscriberIgmpFlows(sub, FlowOperation.REMOVE);
604
605 // FIXME check the return status of the flow and return accordingly
606 log.info("Removal of subscriber on {} completed", portWithName(sub.port));
607 return true;
608 }
609
610 @Override
611 public boolean hasDefaultEapol(Port port) {
612 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
613 // NOTE we consider ERROR as a present EAPOL flow as ONOS
614 // will keep trying to add it
615 return status != null && (status.defaultEapolStatus == OltFlowsStatus.ADDED ||
616 status.defaultEapolStatus == OltFlowsStatus.PENDING_ADD ||
617 status.defaultEapolStatus == OltFlowsStatus.ERROR);
618 }
619
620 private OltPortStatus getOltPortStatus(Port port, UniTagInformation uniTagInformation) {
621 try {
622 cpStatusReadLock.lock();
623 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uniTagInformation);
624 OltPortStatus status = cpStatus.get(sk);
625 return status;
626 } finally {
627 cpStatusReadLock.unlock();
628 }
629 }
630
631 public boolean isDefaultEapolPendingRemoval(Port port) {
632 OltPortStatus status = getOltPortStatus(port, defaultEapolUniTag);
633 if (log.isTraceEnabled()) {
634 log.trace("Status during EAPOL flow check {} for port {} and UniTagInformation {}",
635 status, portWithName(port), defaultEapolUniTag);
636 }
637 return status != null && status.defaultEapolStatus == OltFlowsStatus.PENDING_REMOVE;
638 }
639
640 @Override
641 public boolean hasDhcpFlows(Port port, UniTagInformation uti) {
642 OltPortStatus status = getOltPortStatus(port, uti);
643 if (log.isTraceEnabled()) {
644 log.trace("Status during DHCP flow check {} for port {} and service {}",
645 status, portWithName(port), uti.getServiceName());
646 }
647 return status != null &&
648 (status.dhcpStatus == OltFlowsStatus.ADDED ||
649 status.dhcpStatus == OltFlowsStatus.PENDING_ADD);
650 }
651
652 @Override
653 public boolean hasSubscriberFlows(Port port, UniTagInformation uti) {
654
655 OltPortStatus status = getOltPortStatus(port, uti);
656 if (log.isTraceEnabled()) {
657 log.trace("Status during subscriber flow check {} for port {} and service {}",
658 status, portWithName(port), uti.getServiceName());
659 }
660 return status != null && (status.subscriberFlowsStatus == OltFlowsStatus.ADDED ||
661 status.subscriberFlowsStatus == OltFlowsStatus.PENDING_ADD);
662 }
663
664 @Override
665 public void purgeDeviceFlows(DeviceId deviceId) {
666 log.debug("Purging flows on device {}", deviceId);
Matteo Scandolob6981dc2021-12-02 16:31:44 -0800667 flowRuleService.purgeFlowRules(deviceId);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700668
669 // removing the status from the cpStatus map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800670 if (log.isTraceEnabled()) {
671 log.trace("Clearing cp status from device {}", deviceId);
672 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700673 try {
674 cpStatusWriteLock.lock();
675 Iterator<Map.Entry<ServiceKey, OltPortStatus>> iter = cpStatus.entrySet().iterator();
676 while (iter.hasNext()) {
677 Map.Entry<ServiceKey, OltPortStatus> entry = iter.next();
678 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
679 cpStatus.remove(entry.getKey());
680 }
681 }
682 } finally {
683 cpStatusWriteLock.unlock();
684 }
685
686 // removing subscribers from the provisioned map
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800687 if (log.isTraceEnabled()) {
688 log.trace("Clearing provisioned subscribers from device {}", deviceId);
689 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700690 try {
691 provisionedSubscribersWriteLock.lock();
692 Iterator<Map.Entry<ServiceKey, Boolean>> iter = provisionedSubscribers.entrySet().iterator();
693 while (iter.hasNext()) {
694 Map.Entry<ServiceKey, Boolean> entry = iter.next();
695 if (entry.getKey().getPort().connectPoint().deviceId().equals(deviceId)) {
696 provisionedSubscribers.remove(entry.getKey());
697 }
698 }
699 } finally {
700 provisionedSubscribersWriteLock.unlock();
701 }
Matteo Scandolo2542e5d2021-12-01 16:53:41 -0800702 log.debug("Done clearing up device flows and subscribers");
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700703 }
704
705 @Override
706 public boolean isSubscriberServiceProvisioned(AccessDevicePort cp) {
707 // if any service is programmed on this port, returns true
708 AtomicBoolean provisioned = new AtomicBoolean(false);
709 try {
710 provisionedSubscribersReadLock.lock();
711 for (Map.Entry<ServiceKey, Boolean> entry : provisionedSubscribers.entrySet()) {
712 if (entry.getKey().getPort().equals(cp) && entry.getValue()) {
713 provisioned.set(true);
714 break;
715 }
716 }
717 } finally {
718 provisionedSubscribersReadLock.unlock();
719 }
720 return provisioned.get();
721 }
722
723 @Override
724 public boolean isSubscriberServiceProvisioned(ServiceKey sk) {
725 try {
726 provisionedSubscribersReadLock.lock();
727 Boolean provisioned = provisionedSubscribers.get(sk);
728 if (provisioned == null || !provisioned) {
729 return false;
730 }
731 } finally {
732 provisionedSubscribersReadLock.unlock();
733 }
734 return true;
735 }
736
737 @Override
738 public void updateProvisionedSubscriberStatus(ServiceKey sk, Boolean status) {
739 try {
740 provisionedSubscribersWriteLock.lock();
741 provisionedSubscribers.put(sk, status);
742 } finally {
743 provisionedSubscribersWriteLock.unlock();
744 }
745 }
746
747 private boolean handleEapolFlow(DiscoveredSubscriber sub, String bandwidthProfile,
748 String oltBandwidthProfile, FlowOperation action, VlanId vlanId) {
749
750 // create a subscriberKey for the EAPOL flow
751 ServiceKey sk = new ServiceKey(new AccessDevicePort(sub.port), defaultEapolUniTag);
752
753 // NOTE we only need to keep track of the default EAPOL flow in the
754 // connectpoint status map
755 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
756 OltFlowsStatus status = action == FlowOperation.ADD ?
757 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
758 updateConnectPointStatus(sk, status, OltFlowsStatus.NONE, OltFlowsStatus.NONE);
759
760 }
761
762 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
763 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
764
765 int techProfileId = getDefaultTechProfileId(sub.port);
766 MeterId meterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), bandwidthProfile);
767
768 // in the delete case the meter should still be there as we remove
769 // the meters only if no flows are pointing to them
770 if (meterId == null) {
771 log.debug("MeterId is null for BandwidthProfile {} on device {}",
772 bandwidthProfile, sub.device.id());
773 return false;
774 }
775
776 MeterId oltMeterId = oltMeterService.getMeterIdForBandwidthProfile(sub.device.id(), oltBandwidthProfile);
777 if (oltMeterId == null) {
778 log.debug("MeterId is null for OltBandwidthProfile {} on device {}",
779 oltBandwidthProfile, sub.device.id());
780 return false;
781 }
782
783 log.info("{} EAPOL flow for {} with vlanId {} and BandwidthProfile {} (meterId {})",
784 flowOpToString(action), portWithName(sub.port), vlanId, bandwidthProfile, meterId);
785
786 FilteringObjective.Builder eapolAction;
787
788 if (action == FlowOperation.ADD) {
789 eapolAction = filterBuilder.permit();
790 } else if (action == FlowOperation.REMOVE) {
791 eapolAction = filterBuilder.deny();
792 } else {
793 log.error("Operation {} not supported", action);
794 return false;
795 }
796
797 FilteringObjective.Builder baseEapol = eapolAction
798 .withKey(Criteria.matchInPort(sub.port.number()))
799 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()));
800
801 // NOTE we only need to add the treatment to install the flow,
802 // we can remove it based in the match
803 FilteringObjective.Builder eapol;
804
805 TrafficTreatment treatment = treatmentBuilder
806 .meter(meterId)
807 .writeMetadata(createTechProfValueForWriteMetadata(
808 vlanId,
809 techProfileId, oltMeterId), 0)
810 .setOutput(PortNumber.CONTROLLER)
811 .pushVlan()
812 .setVlanId(vlanId)
813 .build();
814 eapol = baseEapol
815 .withMeta(treatment);
816
817 FilteringObjective eapolObjective = eapol
818 .fromApp(appId)
819 .withPriority(MAX_PRIORITY)
820 .add(new ObjectiveContext() {
821 @Override
822 public void onSuccess(Objective objective) {
823 log.info("EAPOL flow objective {} for {}",
824 completeFlowOpToString(action), portWithName(sub.port));
825 if (log.isTraceEnabled()) {
826 log.trace("EAPOL flow details for port {}: {}", portWithName(sub.port), objective);
827 }
828 }
829
830 @Override
831 public void onError(Objective objective, ObjectiveError error) {
832 log.error("Cannot {} eapol flow for {} : {}", action,
833 portWithName(sub.port), error);
834
835 if (vlanId.id().equals(EAPOL_DEFAULT_VLAN)) {
836 updateConnectPointStatus(sk,
837 OltFlowsStatus.ERROR, null, null);
838 }
839 }
840 });
841
842 flowObjectiveService.filter(sub.device.id(), eapolObjective);
843
844 log.info("{} EAPOL filter for {}", completeFlowOpToString(action), portWithName(sub.port));
845 return true;
846 }
847
848 // FIXME it's confusing that si is not necessarily inside the DiscoveredSubscriber
849 private boolean handleSubscriberEapolFlows(DiscoveredSubscriber sub, FlowOperation action,
850 SubscriberAndDeviceInformation si) {
851 if (!enableEapol) {
852 return true;
853 }
854 // TODO verify we need an EAPOL flow for EACH service
855 AtomicBoolean success = new AtomicBoolean(true);
856 si.uniTagList().forEach(u -> {
857 // we assume that if subscriber flows are installed, tagged EAPOL is installed as well
858 boolean hasFlows = hasSubscriberFlows(sub.port, u);
859
860 // if the subscriber flows are present the EAPOL flow is present thus we don't need to install it,
861 // if the subscriber does not have flows the EAPOL flow is not present thus we don't need to remove it
862 if (action == FlowOperation.ADD && hasFlows ||
863 action == FlowOperation.REMOVE && !hasFlows) {
864 log.debug("Not {} EAPOL on {}({}) as subscriber flow status is {}", flowOpToString(action),
865 portWithName(sub.port), u.getServiceName(), hasFlows);
866 return;
867 }
868 log.info("{} EAPOL flows for subscriber {} on {} and service {}",
869 flowOpToString(action), si.id(), portWithName(sub.port), u.getServiceName());
870 if (!handleEapolFlow(sub, u.getUpstreamBandwidthProfile(),
871 u.getUpstreamOltBandwidthProfile(),
872 action, u.getPonCTag())) {
873 //
874 log.error("Failed to {} EAPOL with suscriber tags", action);
875 //TODO this sets it for all services, maybe some services succeeded.
876 success.set(false);
877 }
878 });
879 return success.get();
880 }
881
882 private void handleSubscriberIgmpFlows(DiscoveredSubscriber sub, FlowOperation action) {
883 sub.subscriberAndDeviceInformation.uniTagList().forEach(uti -> {
884 if (uti.getIsIgmpRequired()) {
885 DeviceId deviceId = sub.device.id();
886 // if we reached here a meter already exists
887 MeterId meterId = oltMeterService
888 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
889 MeterId oltMeterId = oltMeterService
890 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
891
892 processIgmpFilteringObjectives(deviceId, sub.port,
893 action, FlowDirection.UPSTREAM, meterId, oltMeterId, uti.getTechnologyProfileId(),
894 uti.getPonCTag(), uti.getUniTagMatch(), uti.getUsPonCTagPriority());
895 }
896 });
897 }
898
899 private boolean checkSadisRunning() {
900 if (bpService == null) {
901 log.warn("Sadis is not running");
902 return false;
903 }
904 return true;
905 }
906
907 private int getDefaultTechProfileId(Port port) {
908 if (!checkSadisRunning()) {
909 return defaultTechProfileId;
910 }
911 if (port != null) {
912 SubscriberAndDeviceInformation info = subsService.get(getPortName(port));
913 if (info != null && info.uniTagList().size() == 1) {
914 return info.uniTagList().get(0).getTechnologyProfileId();
915 }
916 }
917 return defaultTechProfileId;
918 }
919
920 protected Long createTechProfValueForWriteMetadata(VlanId cVlan, int techProfileId, MeterId upstreamOltMeterId) {
921 Long writeMetadata;
922
923 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
924 writeMetadata = (long) techProfileId << 32;
925 } else {
926 writeMetadata = ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
927 }
928 if (upstreamOltMeterId == null) {
929 return writeMetadata;
930 } else {
931 return writeMetadata | upstreamOltMeterId.id();
932 }
933 }
934
935 private void processLldpFilteringObjective(DeviceId deviceId, Port port, FlowOperation action) {
936 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
937
938 FilteringObjective lldp = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
939 .withKey(Criteria.matchInPort(port.number()))
940 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
941 .withMeta(DefaultTrafficTreatment.builder()
942 .setOutput(PortNumber.CONTROLLER).build())
943 .fromApp(appId)
944 .withPriority(MAX_PRIORITY)
945 .add(new ObjectiveContext() {
946 @Override
947 public void onSuccess(Objective objective) {
948 log.info("{} LLDP filter for {}.", completeFlowOpToString(action), portWithName(port));
949 }
950
951 @Override
952 public void onError(Objective objective, ObjectiveError error) {
953 log.error("Falied to {} LLDP filter on {} because {}", action, portWithName(port),
954 error);
955 }
956 });
957
958 flowObjectiveService.filter(deviceId, lldp);
959 }
960
961 protected void handleSubscriberDhcpFlows(DeviceId deviceId, Port port,
962 FlowOperation action,
963 SubscriberAndDeviceInformation si) {
964 si.uniTagList().forEach(uti -> {
965
966 if (!uti.getIsDhcpRequired()) {
967 return;
968 }
969
970 // if it's an ADD skip if flows are there,
971 // if it's a DELETE skip if flows are not there
972 boolean hasFlows = hasDhcpFlows(port, uti);
973 if (action == FlowOperation.ADD && hasFlows ||
974 action == FlowOperation.REMOVE && !hasFlows) {
975 log.debug("Not dealing with DHCP {} on {} as DHCP flow status is {}", action,
976 uti.getServiceName(), hasFlows);
977 return;
978 }
979
980 log.info("{} DHCP flows for subscriber on {} and service {}",
981 flowOpToString(action), portWithName(port), uti.getServiceName());
982
983 // if we reached here a meter already exists
984 MeterId meterId = oltMeterService
985 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamBandwidthProfile());
986 MeterId oltMeterId = oltMeterService
987 .getMeterIdForBandwidthProfile(deviceId, uti.getUpstreamOltBandwidthProfile());
988
989 if (enableDhcpV4) {
990 processDhcpFilteringObjectives(deviceId, port, action, FlowDirection.UPSTREAM, 68, 67,
991 EthType.EtherType.IPV4.ethType(), IPv4.PROTOCOL_UDP, meterId, oltMeterId,
992 uti);
993 }
994 if (enableDhcpV6) {
995 log.error("DHCP V6 not supported for subscribers");
996 }
997 });
998 }
999
1000 // FIXME return boolean, if this fails we need to retry
1001 protected void handleSubscriberDataFlows(Device device, Port port,
1002 FlowOperation action,
1003 SubscriberAndDeviceInformation si, String multicastServiceName) {
1004
1005 Optional<Port> nniPort = oltDeviceService.getNniPort(device);
1006 if (nniPort.isEmpty()) {
1007 log.error("Cannot configure DP flows as upstream port is not configured for subscriber {} on {}",
1008 si.id(), portWithName(port));
1009 return;
1010 }
1011 si.uniTagList().forEach(uti -> {
1012
1013 boolean hasFlows = hasSubscriberFlows(port, uti);
1014 if (action == FlowOperation.ADD && hasFlows ||
1015 action == FlowOperation.REMOVE && !hasFlows) {
1016 log.debug("Not dealing with DP flows {} on {} as subscriber flow status is {}", action,
1017 uti.getServiceName(), hasFlows);
1018 return;
1019 }
1020
1021 if (multicastServiceName.equals(uti.getServiceName())) {
1022 log.debug("This is the multicast service ({}) for subscriber {} on {}, " +
1023 "dataplane flows are not needed",
1024 uti.getServiceName(), si.id(), portWithName(port));
1025 return;
1026 }
1027
1028 log.info("{} Data plane flows for subscriber {} on {} and service {}",
1029 flowOpToString(action), si.id(), portWithName(port), uti.getServiceName());
Matteo Scandolo80f5e972021-12-02 14:59:15 -08001030 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1031 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1032 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
1033 updateConnectPointStatus(sk, null, status, null);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001034
1035 // upstream flows
1036 MeterId usMeterId = oltMeterService
1037 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamBandwidthProfile());
1038 MeterId oltUsMeterId = oltMeterService
1039 .getMeterIdForBandwidthProfile(device.id(), uti.getUpstreamOltBandwidthProfile());
1040 processUpstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, usMeterId,
1041 oltUsMeterId, uti);
1042
1043 // downstream flows
1044 MeterId dsMeterId = oltMeterService
1045 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamBandwidthProfile());
1046 MeterId oltDsMeterId = oltMeterService
1047 .getMeterIdForBandwidthProfile(device.id(), uti.getDownstreamOltBandwidthProfile());
1048 processDownstreamDataFilteringObjects(device.id(), port, nniPort.get(), action, dsMeterId,
1049 oltDsMeterId, uti, getMacAddress(device.id(), port, uti));
1050 });
1051 }
1052
1053 private void processDhcpFilteringObjectives(DeviceId deviceId, Port port,
1054 FlowOperation action, FlowDirection direction,
1055 int udpSrc, int udpDst, EthType ethType, byte protocol,
1056 MeterId meterId, MeterId oltMeterId, UniTagInformation uti) {
1057 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
1058 log.debug("{} DHCP filtering objectives on {}", flowOpToString(action), sk);
1059
1060
1061 OltFlowsStatus status = action.equals(FlowOperation.ADD) ?
1062 OltFlowsStatus.PENDING_ADD : OltFlowsStatus.PENDING_REMOVE;
1063 updateConnectPointStatus(sk, null, null, status);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001064
1065 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1066 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1067
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001068 if (meterId != null) {
1069 treatmentBuilder.meter(meterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001070 }
1071
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001072 if (uti.getTechnologyProfileId() != NONE_TP_ID) {
1073 treatmentBuilder.writeMetadata(
1074 createTechProfValueForWriteMetadata(uti.getUniTagMatch(),
1075 uti.getTechnologyProfileId(), oltMeterId), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001076 }
1077
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001078 FilteringObjective.Builder dhcpBuilder = (action == FlowOperation.ADD ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001079 .withKey(Criteria.matchInPort(port.number()))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001080 .addCondition(Criteria.matchEthType(ethType))
1081 .addCondition(Criteria.matchIPProtocol(protocol))
1082 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1083 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001084 .fromApp(appId)
1085 .withPriority(MAX_PRIORITY);
1086
Andrea Campanella0e34f562020-06-11 10:47:10 +02001087 //VLAN changes and PCP matching need to happen only in the upstream directions
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001088 if (direction == FlowDirection.UPSTREAM) {
1089 treatmentBuilder.setVlanId(uti.getPonCTag());
1090 if (!VlanId.vlanId(VlanId.NO_VID).equals(uti.getUniTagMatch())) {
1091 dhcpBuilder.addCondition(Criteria.matchVlanId(uti.getUniTagMatch()));
Andrea Campanella0e34f562020-06-11 10:47:10 +02001092 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001093 if (uti.getUsPonCTagPriority() != -1) {
1094 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001095 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001096 }
1097
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001098 dhcpBuilder.withMeta(treatmentBuilder
1099 .setOutput(PortNumber.CONTROLLER).build());
Andrea Campanella0e34f562020-06-11 10:47:10 +02001100
1101
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001102 FilteringObjective dhcpUpstream = dhcpBuilder.add(new ObjectiveContext() {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001103 @Override
1104 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001105 log.info("{} DHCP {} filter for {}.",
1106 completeFlowOpToString(action), (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1107 portWithName(port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001108 }
1109
1110 @Override
1111 public void onError(Objective objective, ObjectiveError error) {
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001112 log.error("DHCP {} filter for {} failed {} because {}",
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001113 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
1114 portWithName(port),
1115 action,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001116 error);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001117 updateConnectPointStatus(sk, null, null, OltFlowsStatus.ERROR);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001118 }
1119 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001120 flowObjectiveService.filter(deviceId, dhcpUpstream);
1121 }
1122
1123 private void processIgmpFilteringObjectives(DeviceId deviceId, Port port,
1124 FlowOperation action, FlowDirection direction,
1125 MeterId meterId, MeterId oltMeterId, int techProfileId,
1126 VlanId cTag, VlanId unitagMatch, int vlanPcp) {
1127
1128 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
1129 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1130 if (direction == FlowDirection.UPSTREAM) {
1131
1132 if (techProfileId != NONE_TP_ID) {
1133 treatmentBuilder.writeMetadata(createTechProfValueForWriteMetadata(null,
1134 techProfileId, oltMeterId), 0);
1135 }
1136
1137
1138 if (meterId != null) {
1139 treatmentBuilder.meter(meterId);
1140 }
1141
1142 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1143 filterBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1144 }
1145
1146 if (!VlanId.vlanId(VlanId.NO_VID).equals(cTag)) {
1147 treatmentBuilder.setVlanId(cTag);
1148 }
1149
1150 if (vlanPcp != -1) {
1151 treatmentBuilder.setVlanPcp((byte) vlanPcp);
1152 }
1153 }
1154
1155 filterBuilder = (action == FlowOperation.ADD) ? filterBuilder.permit() : filterBuilder.deny();
1156
1157 FilteringObjective igmp = filterBuilder
1158 .withKey(Criteria.matchInPort(port.number()))
1159 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1160 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
1161 .withMeta(treatmentBuilder
1162 .setOutput(PortNumber.CONTROLLER).build())
1163 .fromApp(appId)
1164 .withPriority(MAX_PRIORITY)
1165 .add(new ObjectiveContext() {
1166 @Override
1167 public void onSuccess(Objective objective) {
1168 log.info("Igmp filter for {} {}.", portWithName(port), action);
1169 }
1170
1171 @Override
1172 public void onError(Objective objective, ObjectiveError error) {
1173 log.error("Igmp filter for {} failed {} because {}.", portWithName(port), action,
1174 error);
1175 }
1176 });
1177
1178 flowObjectiveService.filter(deviceId, igmp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001179
1180 }
1181
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001182 private void processPPPoEDFilteringObjectives(DeviceId deviceId, Port port,
1183 FlowOperation action, FlowDirection direction,
1184 MeterId meterId, MeterId oltMeterId, int techProfileId,
1185 VlanId cTag, VlanId unitagMatch, Byte vlanPcp) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001186
1187 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1188 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001189
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001190 if (meterId != null) {
1191 treatmentBuilder.meter(meterId);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001192 }
1193
1194 if (techProfileId != NONE_TP_ID) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001195 treatmentBuilder.writeMetadata(createTechProfValueForWriteMetadata(cTag, techProfileId, oltMeterId), 0);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001196 }
1197
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001198 DefaultFilteringObjective.Builder pppoedBuilder = ((action == FlowOperation.ADD)
1199 ? builder.permit() : builder.deny())
Tunahan Sezenf0843b92021-04-30 07:13:16 +00001200 .withKey(Criteria.matchInPort(port.number()))
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001201 .addCondition(Criteria.matchEthType(EthType.EtherType.PPPoED.ethType()))
1202 .fromApp(appId)
1203 .withPriority(10000);
1204
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001205 if (direction == FlowDirection.UPSTREAM) {
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001206 treatmentBuilder.setVlanId(cTag);
1207 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
1208 pppoedBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
1209 }
1210 if (vlanPcp != null) {
1211 treatmentBuilder.setVlanPcp(vlanPcp);
1212 }
1213 }
1214 pppoedBuilder = pppoedBuilder.withMeta(treatmentBuilder.setOutput(PortNumber.CONTROLLER).build());
1215
1216 FilteringObjective pppoed = pppoedBuilder
1217 .add(new ObjectiveContext() {
1218 @Override
1219 public void onSuccess(Objective objective) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001220 log.info("PPPoED filter for {} {}.", portWithName(port), action);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001221 }
1222
1223 @Override
1224 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001225 log.info("PPPoED filter for {} failed {} because {}", portWithName(port),
1226 action, error);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001227 }
1228 });
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001229 flowObjectiveService.filter(deviceId, pppoed);
Gustavo Silva5c492dd2021-02-12 10:21:11 -03001230 }
1231
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001232 private void processUpstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1233 FlowOperation action,
1234 MeterId upstreamMeterId,
1235 MeterId upstreamOltMeterId,
1236 UniTagInformation uti) {
1237 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001238 TrafficSelector selector = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001239 .matchInPort(port.number())
1240 .matchVlanId(uti.getUniTagMatch())
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001241 .build();
1242
Andrea Campanella327c5722020-01-30 11:34:13 +01001243 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1244 //if the subscriberVlan (cTag) is different than ANY it needs to set.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001245 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanella327c5722020-01-30 11:34:13 +01001246 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001247 .setVlanId(uti.getPonCTag());
Andrea Campanella327c5722020-01-30 11:34:13 +01001248 }
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001249 if (uti.getPonSTag().toShort() == VlanId.ANY_VALUE) {
1250 treatmentBuilder.popVlan();
1251 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001252
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001253 if (uti.getUsPonCTagPriority() != -1) {
1254 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Maria Carmela Cascino067ee4d2021-11-02 13:14:43 +01001255
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001256 }
1257
1258 treatmentBuilder.pushVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001259 .setVlanId(uti.getPonSTag());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001260
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001261 if (uti.getUsPonSTagPriority() != -1) {
1262 treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001263 }
1264
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001265 treatmentBuilder.setOutput(nniPort.number())
1266 .writeMetadata(createMetadata(uti.getPonCTag(),
1267 uti.getTechnologyProfileId(), nniPort.number()), 0L);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001268
yasin saplib4b8ee12021-06-13 18:25:20 +00001269 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1270
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001271 if (upstreamMeterId != null) {
1272 treatmentBuilder.meter(upstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001273 annotationBuilder.set(UPSTREAM_ONU, upstreamMeterId.toString());
1274 }
1275 if (upstreamOltMeterId != null) {
1276 treatmentBuilder.meter(upstreamOltMeterId);
1277 annotationBuilder.set(UPSTREAM_OLT, upstreamOltMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001278 }
1279
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001280 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selector,
1281 treatmentBuilder.build(), MIN_PRIORITY,
yasin saplib4b8ee12021-06-13 18:25:20 +00001282 annotationBuilder.build());
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001283
1284 ObjectiveContext context = new ObjectiveContext() {
1285 @Override
1286 public void onSuccess(Objective objective) {
1287 log.info("{} Upstream Data plane filter for {}.",
1288 completeFlowOpToString(action), sk);
1289 }
1290
1291 @Override
1292 public void onError(Objective objective, ObjectiveError error) {
1293 log.error("Upstream Data plane filter for {} failed {} because {}.",
1294 sk, action, error);
1295 updateConnectPointStatus(sk, null, OltFlowsStatus.ERROR, null);
1296 }
1297 };
1298
1299 ForwardingObjective flow = null;
1300 if (action == FlowOperation.ADD) {
1301 flow = flowBuilder.add(context);
1302 } else if (action == FlowOperation.REMOVE) {
1303 flow = flowBuilder.remove(context);
1304 } else {
1305 log.error("Flow action not supported: {}", action);
1306 }
1307
1308 if (flow != null) {
1309 flowObjectiveService.forward(deviceId, flow);
1310 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001311 }
1312
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001313 private void processDownstreamDataFilteringObjects(DeviceId deviceId, Port port, Port nniPort,
1314 FlowOperation action,
1315 MeterId downstreamMeterId,
1316 MeterId downstreamOltMeterId,
1317 UniTagInformation uti,
1318 MacAddress macAddress) {
1319 ServiceKey sk = new ServiceKey(new AccessDevicePort(port), uti);
Andrea Campanella327c5722020-01-30 11:34:13 +01001320 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001321 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001322 .matchVlanId(uti.getPonSTag())
1323 .matchInPort(nniPort.number())
1324 .matchInnerVlanId(uti.getPonCTag());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001325
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001326 if (uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1327 selectorBuilder.matchMetadata(uti.getPonCTag().toShort());
Andrea Campanella090e4a02020-02-05 13:53:55 +01001328 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001329
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001330 if (uti.getDsPonCTagPriority() != -1) {
1331 selectorBuilder.matchVlanPcp((byte) uti.getDsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001332 }
1333
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001334 if (macAddress != null) {
1335 selectorBuilder.matchEthDst(macAddress);
1336 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001337
1338 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
1339 .popVlan()
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001340 .setOutput(port.number());
Andrea Campanella327c5722020-01-30 11:34:13 +01001341
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001342 treatmentBuilder.writeMetadata(createMetadata(uti.getPonCTag(),
1343 uti.getTechnologyProfileId(),
1344 port.number()), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001345
Andrea Campanella981e86c2021-03-12 11:35:33 +01001346 // Upstream pbit is used to remark inner vlan pbit.
1347 // Upstream is used to avoid trusting the BNG to send the packet with correct pbit.
1348 // this is done because ds mode 0 is used because ds mode 3 or 6 that allow for
1349 // all pbit acceptance are not widely supported by vendors even though present in
1350 // the OMCI spec.
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001351 if (uti.getUsPonCTagPriority() != -1) {
1352 treatmentBuilder.setVlanPcp((byte) uti.getUsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001353 }
1354
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001355 if (!VlanId.NONE.equals(uti.getUniTagMatch()) &&
1356 uti.getPonCTag().toShort() != VlanId.ANY_VALUE) {
1357 treatmentBuilder.setVlanId(uti.getUniTagMatch());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001358 }
1359
yasin saplib4b8ee12021-06-13 18:25:20 +00001360 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
1361
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001362 if (downstreamMeterId != null) {
1363 treatmentBuilder.meter(downstreamMeterId);
yasin saplib4b8ee12021-06-13 18:25:20 +00001364 annotationBuilder.set(DOWNSTREAM_ONU, downstreamMeterId.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001365 }
1366
yasin saplib4b8ee12021-06-13 18:25:20 +00001367 if (downstreamOltMeterId != null) {
1368 treatmentBuilder.meter(downstreamOltMeterId);
1369 annotationBuilder.set(DOWNSTREAM_OLT, downstreamOltMeterId.toString());
1370 }
1371
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001372 DefaultForwardingObjective.Builder flowBuilder = createForwardingObjectiveBuilder(selectorBuilder.build(),
1373 treatmentBuilder.build(), MIN_PRIORITY, annotationBuilder.build());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001374
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001375 ObjectiveContext context = new ObjectiveContext() {
1376 @Override
1377 public void onSuccess(Objective objective) {
1378 log.info("{} Downstream Data plane filter for {}.",
1379 completeFlowOpToString(action), sk);
1380 }
1381
1382 @Override
1383 public void onError(Objective objective, ObjectiveError error) {
1384 log.info("Downstream Data plane filter for {} failed {} because {}.",
1385 sk, action, error);
1386 updateConnectPointStatus(sk, null, OltFlowsStatus.ERROR, null);
1387 }
1388 };
1389
1390 ForwardingObjective flow = null;
1391 if (action == FlowOperation.ADD) {
1392 flow = flowBuilder.add(context);
1393 } else if (action == FlowOperation.REMOVE) {
1394 flow = flowBuilder.remove(context);
1395 } else {
1396 log.error("Flow action not supported: {}", action);
1397 }
1398
1399 if (flow != null) {
1400 if (log.isTraceEnabled()) {
1401 log.trace("Forwarding rule {}", flow);
1402 }
1403 flowObjectiveService.forward(deviceId, flow);
1404 }
Andrea Campanella600d2e22020-06-22 11:00:31 +02001405 }
1406
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001407 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
1408 TrafficTreatment treatment,
yasin saplib4b8ee12021-06-13 18:25:20 +00001409 Integer priority,
1410 Annotations annotations) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001411 return DefaultForwardingObjective.builder()
1412 .withFlag(ForwardingObjective.Flag.VERSATILE)
1413 .withPriority(priority)
1414 .makePermanent()
1415 .withSelector(selector)
yasin saplib4b8ee12021-06-13 18:25:20 +00001416 .withAnnotations(annotations)
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001417 .fromApp(appId)
1418 .withTreatment(treatment);
1419 }
1420
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001421 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
1422 if (techProfileId == NONE_TP_ID) {
Andrea Campanella7c49b792020-05-11 11:36:53 +02001423 techProfileId = DEFAULT_TP_ID_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001424 }
1425
1426 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
1427 }
1428
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001429 private boolean isMacLearningEnabled(SubscriberAndDeviceInformation si) {
1430 AtomicBoolean requiresMacLearning = new AtomicBoolean();
1431 requiresMacLearning.set(false);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001432
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001433 si.uniTagList().forEach(uniTagInfo -> {
1434 if (uniTagInfo.getEnableMacLearning()) {
1435 requiresMacLearning.set(true);
1436 }
1437 });
1438
1439 return requiresMacLearning.get();
1440 }
1441
1442 /**
1443 * Checks whether the subscriber has the MacAddress configured or discovered.
1444 *
1445 * @param deviceId DeviceId for this subscriber
1446 * @param port Port for this subscriber
1447 * @param si SubscriberAndDeviceInformation
1448 * @return boolean
1449 */
1450 protected boolean isMacAddressAvailable(DeviceId deviceId, Port port, SubscriberAndDeviceInformation si) {
1451 AtomicBoolean isConfigured = new AtomicBoolean();
1452 isConfigured.set(true);
1453
1454 si.uniTagList().forEach(uniTagInfo -> {
1455 boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning();
1456 boolean configureMac = isMacAddressValid(uniTagInfo);
1457 boolean discoveredMac = false;
1458 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1459 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1460 if (optHost.isPresent() && optHost.get().mac() != null) {
1461 discoveredMac = true;
1462 }
1463 if (isMacLearningEnabled && !configureMac && !discoveredMac) {
1464 log.debug("Awaiting for macAddress on {} for service {}",
1465 portWithName(port), uniTagInfo.getServiceName());
1466 isConfigured.set(false);
1467 }
1468 });
1469
1470 return isConfigured.get();
1471 }
1472
1473 protected MacAddress getMacAddress(DeviceId deviceId, Port port, UniTagInformation uniTagInfo) {
1474 boolean configuredMac = isMacAddressValid(uniTagInfo);
1475 if (configuredMac) {
1476 return MacAddress.valueOf(uniTagInfo.getConfiguredMacAddress());
1477 } else if (uniTagInfo.getEnableMacLearning()) {
1478 Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number()))
1479 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonCTag())).findFirst();
1480 if (optHost.isPresent() && optHost.get().mac() != null) {
1481 return optHost.get().mac();
1482 }
1483 }
1484 return null;
1485 }
1486
1487 private boolean isMacAddressValid(UniTagInformation tagInformation) {
1488 return tagInformation.getConfiguredMacAddress() != null &&
1489 !tagInformation.getConfiguredMacAddress().trim().equals("") &&
1490 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
1491 }
1492
1493 protected void updateConnectPointStatus(ServiceKey key, OltFlowsStatus eapolStatus,
1494 OltFlowsStatus subscriberFlowsStatus, OltFlowsStatus dhcpStatus) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001495 if (log.isTraceEnabled()) {
1496 log.trace("Updating cpStatus {} with values: eapolFlow={}, subscriberFlows={}, dhcpFlow={}",
1497 key, eapolStatus, subscriberFlowsStatus, dhcpStatus);
1498 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001499 try {
1500 cpStatusWriteLock.lock();
1501 OltPortStatus status = cpStatus.get(key);
1502
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001503
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001504 if (status == null) {
Matteo Scandolo2542e5d2021-12-01 16:53:41 -08001505 // if we don't have status for the connectPoint
1506 // and we're only updating status to PENDING_REMOVE or ERROR
1507 // do not create it. This is because this case will only happen when a device is removed
1508 // and it's status cleaned
1509 List<OltFlowsStatus> statusesToIgnore = new ArrayList<>();
1510 statusesToIgnore.add(OltFlowsStatus.PENDING_REMOVE);
1511 statusesToIgnore.add(OltFlowsStatus.ERROR);
1512
1513 if (
1514 (statusesToIgnore.contains(subscriberFlowsStatus) && dhcpStatus == null) ||
1515 (subscriberFlowsStatus == null && statusesToIgnore.contains(dhcpStatus))
1516 ) {
1517 if (log.isTraceEnabled()) {
1518 log.trace("Ignoring cpStatus update as status is meaningless");
1519 }
1520 return;
1521 }
1522
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001523 status = new OltPortStatus(
1524 eapolStatus != null ? eapolStatus : OltFlowsStatus.NONE,
1525 subscriberFlowsStatus != null ? subscriberFlowsStatus : OltFlowsStatus.NONE,
1526 dhcpStatus != null ? dhcpStatus : OltFlowsStatus.NONE
1527 );
1528 } else {
1529 if (eapolStatus != null) {
1530 status.defaultEapolStatus = eapolStatus;
1531 }
1532 if (subscriberFlowsStatus != null) {
1533 status.subscriberFlowsStatus = subscriberFlowsStatus;
1534 }
1535 if (dhcpStatus != null) {
1536 status.dhcpStatus = dhcpStatus;
1537 }
1538 }
1539
1540 cpStatus.put(key, status);
1541 } finally {
1542 cpStatusWriteLock.unlock();
1543 }
1544 }
1545
1546 protected class InternalFlowListener implements FlowRuleListener {
1547 @Override
1548 public void event(FlowRuleEvent event) {
1549 if (appId.id() != (event.subject().appId())) {
1550 return;
1551 }
1552
1553 if (!oltDeviceService.isLocalLeader(event.subject().deviceId())) {
1554 if (log.isTraceEnabled()) {
1555 log.trace("ignoring flow event {} " +
1556 "as not leader for {}", event, event.subject().deviceId());
1557 }
1558 return;
1559 }
1560
1561 switch (event.type()) {
1562 case RULE_ADDED:
1563 case RULE_REMOVED:
1564 Port port = getCpFromFlowRule(event.subject());
1565 if (port == null) {
1566 log.error("Can't find port in flow {}", event.subject());
1567 return;
1568 }
1569 if (log.isTraceEnabled()) {
1570 log.trace("flow event {} on cp {}: {}", event.type(),
1571 portWithName(port), event.subject());
1572 }
1573 updateCpStatus(event.type(), port, event.subject());
1574 return;
1575 case RULE_ADD_REQUESTED:
1576 case RULE_REMOVE_REQUESTED:
1577 // NOTE that PENDING_ADD/REMOVE is set when we create the flowObjective
1578 return;
1579 default:
1580 return;
1581 }
1582 }
1583
1584 protected void updateCpStatus(FlowRuleEvent.Type type, Port port, FlowRule flowRule) {
1585 OltFlowsStatus status = flowRuleStatusToOltFlowStatus(type);
1586 if (isDefaultEapolFlow(flowRule)) {
1587 ServiceKey sk = new ServiceKey(new AccessDevicePort(port),
1588 defaultEapolUniTag);
1589 if (log.isTraceEnabled()) {
1590 log.trace("update defaultEapolStatus {} on {}", status, sk);
1591 }
1592 updateConnectPointStatus(sk, status, null, null);
1593 } else if (isDhcpFlow(flowRule)) {
1594 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule);
1595 if (sk == null) {
1596 return;
1597 }
1598 if (log.isTraceEnabled()) {
1599 log.trace("update dhcpStatus {} on {}", status, sk);
1600 }
1601 updateConnectPointStatus(sk, null, null, status);
1602 } else if (isDataFlow(flowRule)) {
1603
1604 if (oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()),
1605 getCpFromFlowRule(flowRule).number())) {
1606 // the NNI has data-plane for every subscriber, doesn't make sense to track them
1607 return;
1608 }
1609
1610 ServiceKey sk = getSubscriberKeyFromFlowRule(flowRule);
1611 if (sk == null) {
1612 return;
1613 }
1614 if (log.isTraceEnabled()) {
1615 log.trace("update dataplaneStatus {} on {}", status, sk);
1616 }
1617 updateConnectPointStatus(sk, null, status, null);
1618 }
1619 }
1620
1621 private boolean isDefaultEapolFlow(FlowRule flowRule) {
1622 EthTypeCriterion c = (EthTypeCriterion) flowRule.selector().getCriterion(Criterion.Type.ETH_TYPE);
1623 if (c == null) {
1624 return false;
1625 }
1626 if (c.ethType().equals(EthType.EtherType.EAPOL.ethType())) {
1627 AtomicBoolean isDefault = new AtomicBoolean(false);
1628 flowRule.treatment().allInstructions().forEach(instruction -> {
1629 if (instruction.type() == L2MODIFICATION) {
1630 L2ModificationInstruction modificationInstruction = (L2ModificationInstruction) instruction;
1631 if (modificationInstruction.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
1632 L2ModificationInstruction.ModVlanIdInstruction vlanInstruction =
1633 (L2ModificationInstruction.ModVlanIdInstruction) modificationInstruction;
1634 if (vlanInstruction.vlanId().id().equals(EAPOL_DEFAULT_VLAN)) {
1635 isDefault.set(true);
1636 return;
1637 }
1638 }
1639 }
1640 });
1641 return isDefault.get();
1642 }
1643 return false;
1644 }
1645
1646 /**
1647 * Returns true if the flow is a DHCP flow.
1648 * Matches both upstream and downstream flows.
1649 *
1650 * @param flowRule The FlowRule to evaluate
1651 * @return boolean
1652 */
1653 private boolean isDhcpFlow(FlowRule flowRule) {
1654 IPProtocolCriterion ipCriterion = (IPProtocolCriterion) flowRule.selector()
1655 .getCriterion(Criterion.Type.IP_PROTO);
1656 if (ipCriterion == null) {
1657 return false;
1658 }
1659
1660 UdpPortCriterion src = (UdpPortCriterion) flowRule.selector().getCriterion(Criterion.Type.UDP_SRC);
1661
1662 if (src == null) {
1663 return false;
1664 }
1665 return ipCriterion.protocol() == IPv4.PROTOCOL_UDP &&
1666 (src.udpPort().toInt() == 68 || src.udpPort().toInt() == 67);
1667 }
1668
1669 private boolean isDataFlow(FlowRule flowRule) {
1670 // we consider subscriber flows the one that matches on VLAN_VID
1671 // method is valid only because it's the last check after EAPOL and DHCP.
1672 // this matches mcast flows as well, if we want to avoid that we can
1673 // filter out the elements that have groups in the treatment or
1674 // mcastIp in the selector
1675 // IPV4_DST:224.0.0.22/32
1676 // treatment=[immediate=[GROUP:0x1]]
1677
1678 return flowRule.selector().getCriterion(Criterion.Type.VLAN_VID) != null;
1679 }
1680
1681 private Port getCpFromFlowRule(FlowRule flowRule) {
1682 DeviceId deviceId = flowRule.deviceId();
1683 PortCriterion inPort = (PortCriterion) flowRule.selector().getCriterion(Criterion.Type.IN_PORT);
1684 if (inPort != null) {
1685 PortNumber port = inPort.port();
1686 return deviceService.getPort(deviceId, port);
1687 }
1688 return null;
1689 }
1690
1691 private ServiceKey getSubscriberKeyFromFlowRule(FlowRule flowRule) {
1692 Port flowPort = getCpFromFlowRule(flowRule);
1693 SubscriberAndDeviceInformation si = subsService.get(getPortName(flowPort));
1694
1695 Boolean isNni = oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), flowPort.number());
1696 if (si == null && !isNni) {
1697 log.error("Subscriber information not found in sadis for port {}", portWithName(flowPort));
1698 return null;
1699 }
1700
1701 if (isNni) {
1702 return new ServiceKey(new AccessDevicePort(flowPort), nniUniTag);
1703 }
1704
1705 Optional<UniTagInformation> found = Optional.empty();
1706 VlanId flowVlan = null;
1707 if (isDhcpFlow(flowRule)) {
1708 // we need to make a special case for DHCP as in the ATT workflow DHCP flows don't match on tags
1709 L2ModificationInstruction.ModVlanIdInstruction instruction =
1710 (L2ModificationInstruction.ModVlanIdInstruction) flowRule.treatment().immediate().get(1);
1711 flowVlan = instruction.vlanId();
1712 } else {
1713 // for now we assume that if it's not DHCP it's dataplane (or at least tagged)
1714 VlanIdCriterion vlanIdCriterion =
1715 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
1716 if (vlanIdCriterion == null) {
1717 log.warn("cannot match the flow to a subscriber service as it does not carry vlans");
1718 return null;
1719 }
1720 flowVlan = vlanIdCriterion.vlanId();
1721 }
1722
1723 VlanId finalFlowVlan = flowVlan;
1724 found = si.uniTagList().stream().filter(uti ->
1725 uti.getPonCTag().equals(finalFlowVlan) ||
1726 uti.getPonSTag().equals(finalFlowVlan) ||
1727 uti.getUniTagMatch().equals(finalFlowVlan)
1728 ).findFirst();
1729
1730
1731 if (found.isEmpty()) {
1732 log.warn("Cannot map flow rule {} to Service in {}", flowRule, si);
1733 }
1734
1735 return found.isPresent() ? new ServiceKey(new AccessDevicePort(flowPort), found.get()) : null;
1736
1737 }
1738
1739 private OltFlowsStatus flowRuleStatusToOltFlowStatus(FlowRuleEvent.Type type) {
1740 switch (type) {
1741 case RULE_ADD_REQUESTED:
1742 return OltFlowsStatus.PENDING_ADD;
1743 case RULE_ADDED:
1744 return OltFlowsStatus.ADDED;
1745 case RULE_REMOVE_REQUESTED:
1746 return OltFlowsStatus.PENDING_REMOVE;
1747 case RULE_REMOVED:
1748 return OltFlowsStatus.REMOVED;
1749 default:
1750 return OltFlowsStatus.NONE;
1751 }
1752 }
1753 }
1754
1755 protected void bindSadisService(SadisService service) {
1756 this.subsService = service.getSubscriberInfoService();
1757 this.bpService = service.getBandwidthProfileService();
1758 log.info("Sadis service is loaded");
1759 }
1760
1761 protected void unbindSadisService(SadisService service) {
1762 this.subsService = null;
1763 this.bpService = null;
1764 log.info("Sadis service is unloaded");
1765 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001766}