blob: 2a8c6e84d39fb34dedd4619bacbc05cdade96eaa [file] [log] [blame]
alshabibf0e7e702015-05-30 18:22:36 -07001/*
Brian O'Connord6a135a2017-08-03 22:46:05 -07002 * Copyright 2016-present Open Networking Foundation
alshabibf0e7e702015-05-30 18:22:36 -07003 *
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 */
alshabib36a4d732016-06-01 16:03:59 -070016package org.opencord.olt.impl;
alshabibf0e7e702015-05-30 18:22:36 -070017
Saurav Das9da7d522020-03-23 19:14:35 -070018import static com.google.common.base.Preconditions.checkNotNull;
Andrea Campanella971d5b92020-05-07 11:20:43 +020019import static com.google.common.base.Strings.isNullOrEmpty;
Saurav Das9da7d522020-03-23 19:14:35 -070020import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
21import static java.util.stream.Collectors.collectingAndThen;
22import static java.util.stream.Collectors.groupingBy;
23import static java.util.stream.Collectors.mapping;
24import static java.util.stream.Collectors.toList;
25import static java.util.stream.Collectors.toSet;
26import static org.onlab.util.Tools.get;
27import static org.onlab.util.Tools.groupedThreads;
Matteo Scandoloa4aaa972020-10-23 15:24:38 -070028import static org.opencord.olt.impl.OsgiPropertyConstants.*;
Saurav Das9da7d522020-03-23 19:14:35 -070029import static org.slf4j.LoggerFactory.getLogger;
30
31import java.util.ArrayList;
32import java.util.Collection;
33import java.util.Dictionary;
Andrea Campanella0c3309d2020-05-29 01:51:18 -070034import java.util.Iterator;
Saurav Das9da7d522020-03-23 19:14:35 -070035import java.util.List;
36import java.util.Map;
37import java.util.Optional;
38import java.util.Properties;
39import java.util.Set;
40import java.util.concurrent.CompletableFuture;
41import java.util.concurrent.ExecutorService;
42import java.util.concurrent.Executors;
Matteo Scandoloa4aaa972020-10-23 15:24:38 -070043import java.util.concurrent.ScheduledExecutorService;
44import java.util.concurrent.TimeUnit;
Saurav Das9da7d522020-03-23 19:14:35 -070045
alshabibf0e7e702015-05-30 18:22:36 -070046import org.onlab.packet.VlanId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080047import org.onlab.util.KryoNamespace;
Gamze Abaka1f62dd92020-05-07 08:58:13 +000048import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080049import org.onosproject.cluster.ClusterEvent;
50import org.onosproject.cluster.ClusterEventListener;
51import org.onosproject.cluster.ClusterService;
52import org.onosproject.cluster.ControllerNode;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020053import org.onosproject.cluster.LeadershipService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080054import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onosproject.core.ApplicationId;
56import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080057import org.onosproject.event.AbstractListenerManager;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020058import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010059import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070060import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010061import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070062import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080063import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070064import org.onosproject.net.PortNumber;
65import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053068import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
70import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080071import org.onosproject.net.flowobjective.Objective;
72import org.onosproject.net.flowobjective.ObjectiveContext;
73import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070074import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080075import org.onosproject.store.serializers.KryoNamespaces;
76import org.onosproject.store.service.ConsistentMultimap;
77import org.onosproject.store.service.Serializer;
78import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070079import org.opencord.olt.AccessDeviceEvent;
80import org.opencord.olt.AccessDeviceListener;
81import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010082import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000083import org.opencord.olt.internalapi.AccessDeviceFlowService;
84import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000085import org.opencord.sadis.BandwidthProfileInformation;
86import org.opencord.sadis.BaseInformationService;
87import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010088import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000089import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080090import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070091import org.osgi.service.component.annotations.Activate;
92import org.osgi.service.component.annotations.Component;
93import org.osgi.service.component.annotations.Deactivate;
94import org.osgi.service.component.annotations.Modified;
95import org.osgi.service.component.annotations.Reference;
96import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070097import org.slf4j.Logger;
98
Saurav Das9da7d522020-03-23 19:14:35 -070099import com.google.common.collect.ImmutableMap;
100import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700101
102/**
Jonathan Harte533a422015-10-20 17:31:24 -0700103 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700104 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700105@Component(immediate = true,
106 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700107 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000108 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Saurav Das2d3777a2020-08-07 18:48:51 -0700109 EAPOL_DELETE_RETRY_MAX_ATTEMPS + ":Integer=" +
110 EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT,
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700111 PROVISION_DELAY + ":Integer=" + PROVISION_DELAY_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700112 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800113public class Olt
114 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
115 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800116 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800117
Gamze Abakada282b42019-03-11 13:16:48 +0000118 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000119 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800120
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800121 public static final int HASH_WEIGHT = 10;
122
alshabibf0e7e702015-05-30 18:22:36 -0700123 private final Logger log = getLogger(getClass());
124
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530125 private static final String NNI = "nni-";
126
Carmelo Casconeca931162019-07-15 18:22:24 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700128 protected FlowObjectiveService flowObjectiveService;
129
Carmelo Casconeca931162019-07-15 18:22:24 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700131 protected DeviceService deviceService;
132
Carmelo Casconeca931162019-07-15 18:22:24 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700134 protected CoreService coreService;
135
Carmelo Casconeca931162019-07-15 18:22:24 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000137 protected SadisService sadisService;
138
Carmelo Casconeca931162019-07-15 18:22:24 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000140 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800141
Carmelo Casconeca931162019-07-15 18:22:24 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000143 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000144
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
146 protected StorageService storageService;
147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
149 protected ClusterService clusterService;
150
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200152 protected MastershipService mastershipService;
153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY)
155 protected LeadershipService leadershipService;
156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530158 protected FlowRuleService flowRuleService;
159
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000160 @Reference(cardinality = ReferenceCardinality.MANDATORY)
161 protected ComponentConfigService componentConfigService;
162
Carmelo Casconeca931162019-07-15 18:22:24 -0700163 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800164 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700165 **/
166 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000167
Carmelo Casconeca931162019-07-15 18:22:24 -0700168 /**
Gamze Abaka51a34e82020-05-08 13:03:14 +0000169 * Default multicast service name.
Carmelo Casconeca931162019-07-15 18:22:24 -0700170 **/
Gamze Abaka51a34e82020-05-08 13:03:14 +0000171 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Gamze Abaka33feef52019-02-27 08:16:47 +0000172
Saurav Das2d3777a2020-08-07 18:48:51 -0700173 /**
174 * Default amounts of eapol retry.
175 **/
176 protected int eapolDeleteRetryMaxAttempts = EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT;
177
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700178 /**
179 * Delay between EAPOL removal and data plane flows provisioning.
180 */
181 protected int provisionDelay = PROVISION_DELAY_DEFAULT;
182
alshabibf0e7e702015-05-30 18:22:36 -0700183 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800184 private final ClusterEventListener clusterListener = new InternalClusterListener();
185
186 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700187
Gamze Abaka641fc072018-09-04 09:16:27 +0000188 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
189 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700190
Gamze Abaka641fc072018-09-04 09:16:27 +0000191 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000192 groupedThreads("onos/olt-service",
193 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100194
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700195 protected ExecutorService eventExecutor;
Saurav Das2d3777a2020-08-07 18:48:51 -0700196 protected ExecutorService retryExecutor;
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700197 protected ScheduledExecutorService provisionExecutor;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700198
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800199 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Das2d3777a2020-08-07 18:48:51 -0700200 private ConsistentMultimap<ConnectPoint, UniTagInformation> failedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800201
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700202 private Set<SubscriberFlowInfo> pendingSubscribers;
203
alshabibf0e7e702015-05-30 18:22:36 -0700204 @Activate
alshabibe0559672016-02-21 14:49:51 -0800205 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000206 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
207 "events-%d", log));
Saurav Das2d3777a2020-08-07 18:48:51 -0700208 retryExecutor = Executors.newCachedThreadPool();
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700209 provisionExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
210 "provision-%d", log));
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700211
alshabibe0559672016-02-21 14:49:51 -0800212 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000213 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000214 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800215
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800216 KryoNamespace serializer = KryoNamespace.newBuilder()
217 .register(KryoNamespaces.API)
218 .register(UniTagInformation.class)
219 .build();
220
221 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
222 .withName("volt-programmed-subs")
223 .withSerializer(Serializer.using(serializer))
224 .withApplicationId(appId)
225 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700226
Saurav Das2d3777a2020-08-07 18:48:51 -0700227 failedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
228 .withName("volt-failed-subs")
229 .withSerializer(Serializer.using(serializer))
230 .withApplicationId(appId)
231 .build();
232
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700233 pendingSubscribers = Sets.newConcurrentHashSet();
alshabib8e4fd2f2016-01-12 15:55:53 -0800234 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
235
Gamze Abaka641fc072018-09-04 09:16:27 +0000236 subsService = sadisService.getSubscriberInfoService();
237 bpService = sadisService.getBandwidthProfileService();
238
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800239 List<NodeId> readyNodes = clusterService.getNodes().stream()
240 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
241 .map(ControllerNode::id)
242 .collect(toList());
243 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
244 clusterService.addListener(clusterListener);
245
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100246 // look for all provisioned devices in Sadis and create EAPOL flows for the
247 // UNI ports
248 Iterable<Device> devices = deviceService.getDevices();
249 for (Device d : devices) {
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200250 if (isLocalLeader(d.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800251 checkAndCreateDeviceFlows(d);
252 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100253 }
alshabib4ceaed32016-03-03 18:00:58 -0800254
alshabibba357492016-01-27 13:49:46 -0800255 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700256 log.info("Started with Application ID {}", appId.id());
257 }
258
259 @Deactivate
260 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000261 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800262 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800263 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700264 eventDispatcher.removeSink(AccessDeviceEvent.class);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700265 eventExecutor.shutdown();
Saurav Das2d3777a2020-08-07 18:48:51 -0700266 retryExecutor.shutdown();
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700267 provisionExecutor.shutdown();
alshabibf0e7e702015-05-30 18:22:36 -0700268 log.info("Stopped");
269 }
270
alshabibe0559672016-02-21 14:49:51 -0800271 @Modified
272 public void modified(ComponentContext context) {
273 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
274
275 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200276 String bpId = get(properties, DEFAULT_BP_ID);
277 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000278
Andrea Campanella971d5b92020-05-07 11:20:43 +0200279 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
280 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000281
Saurav Das2d3777a2020-08-07 18:48:51 -0700282 String eapolDeleteRetryNew = get(properties, EAPOL_DELETE_RETRY_MAX_ATTEMPS);
283 eapolDeleteRetryMaxAttempts = isNullOrEmpty(eapolDeleteRetryNew) ? EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT :
284 Integer.parseInt(eapolDeleteRetryNew.trim());
285
286 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}, EapolDeleteRetryMaxAttempts: {}",
287 defaultBpId, multicastServiceName, eapolDeleteRetryMaxAttempts);
Gamze Abaka33feef52019-02-27 08:16:47 +0000288
alshabibe0559672016-02-21 14:49:51 -0800289 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000290 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200291 defaultBpId = DEFAULT_BP_ID_DEFAULT;
292 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800293 }
294 }
295
alshabib32232c82016-02-25 17:57:24 -0500296 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000297 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200298 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000299 DeviceId deviceId = connectPoint.deviceId();
300 PortNumber subscriberPortNo = connectPoint.port();
Gamze Abaka838d8142019-02-21 07:06:55 +0000301 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000302 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000303
Saurav Das026650f2020-09-21 18:56:35 -0700304 if (isSubscriberInstalled(connectPoint)) {
305 log.warn("Subscriber at {} already provisioned or in the process .."
306 + " not taking any more action", connectPoint);
307 return true;
308 }
309
310 // Find the subscriber config at this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000311 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100312 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000313 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100314 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100315 }
Jonathan Harte533a422015-10-20 17:31:24 -0700316
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100317 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000318 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100319 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000320 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100321 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700322 }
323
Saurav Das2d3777a2020-08-07 18:48:51 -0700324 // delete Eapol authentication flow with default bandwidth
325 // wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
326 // retry deletion if it fails/times-out
327 retryExecutor.execute(new DeleteEapolInstallSub(connectPoint,
328 uplinkPort, sub, 1));
Amit Ghosh31939522018-08-16 13:28:21 +0100329 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800330 }
331
Saurav Das026650f2020-09-21 18:56:35 -0700332 // returns true if subscriber is programmed or in the process of being programmed
333 private boolean isSubscriberInstalled(ConnectPoint connectPoint) {
334 Collection<? extends UniTagInformation> uniTagInformationSet =
335 programmedSubs.get(connectPoint).value();
336 if (!uniTagInformationSet.isEmpty()) {
337 return true;
338 }
339
340 for (SubscriberFlowInfo fi : pendingSubscribers) {
341 if (fi.getDevId().equals(connectPoint.deviceId())
342 && fi.getUniPort().equals(connectPoint.port())) {
343 return true;
344 }
345 }
346
347 return false;
348 }
349
Saurav Das2d3777a2020-08-07 18:48:51 -0700350 private class DeleteEapolInstallSub implements Runnable {
351 ConnectPoint cp;
352 Port uplinkPort;
353 SubscriberAndDeviceInformation sub;
354 private int attemptNumber;
355
356 DeleteEapolInstallSub(ConnectPoint cp, Port uplinkPort,
357 SubscriberAndDeviceInformation sub,
358 int attemptNumber) {
359 this.cp = cp;
360 this.uplinkPort = uplinkPort;
361 this.sub = sub;
362 this.attemptNumber = attemptNumber;
363 }
364
365 @Override
366 public void run() {
367 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
368 oltFlowService.processEapolFilteringObjectives(cp.deviceId(), cp.port(),
369 defaultBpId, filterFuture,
370 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
371 false);
372 filterFuture.thenAcceptAsync(filterStatus -> {
373 if (filterStatus == null) {
374 log.info("Default eapol flow deleted in attempt {} of {}"
375 + "... provisioning subscriber flows {}",
376 attemptNumber, eapolDeleteRetryMaxAttempts, cp);
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700377
378 // FIXME this is needed to prevent that default EAPOL flow removal and
379 // data plane flows install are received by the device at the same time
380 provisionExecutor.schedule(
381 () -> provisionUniTagList(cp, uplinkPort.number(), sub),
382 provisionDelay, TimeUnit.MILLISECONDS);
Saurav Das2d3777a2020-08-07 18:48:51 -0700383 } else {
384 if (attemptNumber <= eapolDeleteRetryMaxAttempts) {
385 log.warn("The filtering future failed {} for subscriber {}"
386 + "... retrying {} of {} attempts",
387 filterStatus, cp, attemptNumber, eapolDeleteRetryMaxAttempts);
388 retryExecutor.execute(
389 new DeleteEapolInstallSub(cp, uplinkPort, sub,
390 attemptNumber + 1));
391 } else {
392 log.error("The filtering future failed {} for subscriber {}"
393 + "after {} attempts. Subscriber provisioning failed",
394 filterStatus, cp, eapolDeleteRetryMaxAttempts);
395 sub.uniTagList().forEach(ut -> failedSubs.put(cp, ut));
396 }
397 }
398 });
399 }
400
401 }
402
alshabibb7a9e172016-01-13 11:23:53 -0800403 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000404 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700405 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000406
Saurav Daseae48de2019-06-19 13:26:15 -0700407 // Get the subscriber connected to this port from the local cache
408 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000409 DeviceId deviceId = connectPoint.deviceId();
410 PortNumber subscriberPortNo = connectPoint.port();
411
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800412 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000413 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000414 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000415 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800416 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800417 }
418
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100419 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000420 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100421 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000422 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100423 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800424 }
425
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000426 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100427
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000428 if (multicastServiceName.equals(uniTag.getServiceName())) {
429 continue;
430 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000431
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000432 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800433
Saurav Das9da7d522020-03-23 19:14:35 -0700434 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000435 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
436 uniTag.getUpstreamBandwidthProfile(),
437 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100438
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000439 Port port = deviceService.getPort(deviceId, subscriberPortNo);
440 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700441 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000442 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
443 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
444 } else {
445 log.debug("Port {} is no longer enabled or it's unavailable. Not "
446 + "reprogramming default eapol flow", connectPoint);
447 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100448 }
Amit Ghosh31939522018-08-16 13:28:21 +0100449 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800450 }
451
Gamze Abakaf59c0912019-04-19 08:24:28 +0000452
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000453 @Override
454 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
455 Optional<VlanId> cTag, Optional<Integer> tpId) {
456
457 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
458 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000459
Amit Ghosh31939522018-08-16 13:28:21 +0100460 // Check if we can find the connect point to which this subscriber is connected
461 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
462 if (subsPort == null) {
463 log.warn("ConnectPoint for {} not found", subscriberId);
464 return false;
465 }
466
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100467 if (!sTag.isPresent() && !cTag.isPresent()) {
468 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000469 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100470 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
471 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000472 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100473 return false;
474 }
475
Gamze Abakaf59c0912019-04-19 08:24:28 +0000476 //delete Eapol authentication flow with default bandwidth
477 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000478 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000479 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
480 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
481 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000482 filterFuture.thenAcceptAsync(filterStatus -> {
483 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000484 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
485 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000486 }
487 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100488 return true;
489 } else {
490 log.warn("Provisioning failed for subscriber: {}", subscriberId);
491 return false;
492 }
Amit Ghosh31939522018-08-16 13:28:21 +0100493 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100494
alshabibe0559672016-02-21 14:49:51 -0800495 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000496 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
497 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100498 // Check if we can find the connect point to which this subscriber is connected
499 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
500 if (subsPort == null) {
501 log.warn("ConnectPoint for {} not found", subscriberId);
502 return false;
503 }
504
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100505 if (!sTag.isPresent() && !cTag.isPresent()) {
506 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000507 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100508 // Get the uplink port
509 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
510 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000511 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100512 return false;
513 }
514
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000515 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
516 if (!tagInfo.isPresent()) {
517 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
518 subsPort, cTag, sTag, tpId);
519 return false;
520 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000521
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000522 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100523 return true;
524 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000525 log.warn("Removing subscriber is not possible - please check the provided information" +
526 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100527 return false;
528 }
Amit Ghosh31939522018-08-16 13:28:21 +0100529 }
530
531 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000532 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800533 return programmedSubs.stream()
534 .collect(collectingAndThen(
535 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
536 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700537 }
538
539 @Override
Saurav Das2d3777a2020-08-07 18:48:51 -0700540 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getFailedSubs() {
541 return failedSubs.stream()
542 .collect(collectingAndThen(
543 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
544 ImmutableMap::copyOf));
545 }
546
547 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100548 public List<DeviceId> fetchOlts() {
549 // look through all the devices and find the ones that are OLTs as per Sadis
550 List<DeviceId> olts = new ArrayList<>();
551 Iterable<Device> devices = deviceService.getDevices();
552 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700553 if (getOltInfo(d) != null) {
554 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100555 olts.add(d.id());
556 }
557 }
558 return olts;
alshabibe0559672016-02-21 14:49:51 -0800559 }
560
Amit Ghosh31939522018-08-16 13:28:21 +0100561 /**
562 * Finds the connect point to which a subscriber is connected.
563 *
564 * @param id The id of the subscriber, this is the same ID as in Sadis
565 * @return Subscribers ConnectPoint if found else null
566 */
567 private ConnectPoint findSubscriberConnectPoint(String id) {
568
569 Iterable<Device> devices = deviceService.getDevices();
570 for (Device d : devices) {
571 for (Port p : deviceService.getPorts(d.id())) {
572 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
573 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
574 log.debug("Found on device {} port {}", d.id(), p.number());
575 return new ConnectPoint(d.id(), p.number());
576 }
577 }
578 }
579 return null;
580 }
581
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000582 /**
583 * Gets the context of the bandwidth profile information for the given parameter.
584 *
585 * @param bandwidthProfile the bandwidth profile id
586 * @return the context of the bandwidth profile information
587 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000588 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
589 if (bandwidthProfile == null) {
590 return null;
591 }
592 return bpService.get(bandwidthProfile);
593 }
594
Gamze Abaka838d8142019-02-21 07:06:55 +0000595 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000596 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000597 *
598 * @param deviceId the device identifier
599 * @param uplink uplink port of the OLT
600 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000601 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000602 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000603 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000604 PortNumber subscriberPort, UniTagInformation uniTag) {
605
606 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800607
608 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
609 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
610
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000611 VlanId deviceVlan = uniTag.getPonSTag();
612 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000613
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000614 MeterId upstreamMeterId = oltMeterService
615 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
616 MeterId downstreamMeterId = oltMeterService
617 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000618
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000619 ForwardingObjective.Builder upFwd =
620 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
621 ForwardingObjective.Builder downFwd =
622 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
623
Andrea Campanella7c49b792020-05-11 11:36:53 +0200624 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
625 upstreamMeterId, uniTag, false, true);
626 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
627 upstreamMeterId, uniTag, false, true);
alshabibbf23a1f2016-01-14 17:27:11 -0800628
alshabib4ceaed32016-03-03 18:00:58 -0800629 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
630 @Override
631 public void onSuccess(Objective objective) {
632 upFuture.complete(null);
633 }
alshabibbf23a1f2016-01-14 17:27:11 -0800634
alshabib4ceaed32016-03-03 18:00:58 -0800635 @Override
636 public void onError(Objective objective, ObjectiveError error) {
637 upFuture.complete(error);
638 }
639 }));
640
641 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
642 @Override
643 public void onSuccess(Objective objective) {
644 downFuture.complete(null);
645 }
646
647 @Override
648 public void onError(Objective objective, ObjectiveError error) {
649 downFuture.complete(error);
650 }
651 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800652
653 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000654 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800655 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000656 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
657 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
658 uniTag.getTechnologyProfileId(), subscriberPort);
659 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800660 } else if (downStatus != null) {
661 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000662 "on port {} failed downstream uninstallation: {}",
663 subscriberVlan, deviceId, subscriberPort, downStatus);
664 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800665 } else if (upStatus != null) {
666 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000667 "on port {} failed upstream uninstallation: {}",
668 subscriberVlan, deviceId, subscriberPort, upStatus);
669 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800670 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000671 Port port = deviceService.getPort(deviceId, subscriberPort);
672 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
673 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800674 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700675 }
676
Gamze Abaka838d8142019-02-21 07:06:55 +0000677 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000678 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000679 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000680 * @param connectPoint the connection point of the subscriber
681 * @param uplinkPort uplink port of the OLT (the nni port)
682 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000683 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000684 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
685 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000686
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700687 log.debug("Provisioning vlans for subscriber on dev/port: {}", connectPoint.toString());
688 if (log.isTraceEnabled()) {
689 log.trace("Subscriber informations {}", sub);
690 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000691
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000692 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700693 log.warn("Unitaglist doesn't exist for the subscriber {} on dev/port {}",
694 sub.id(), connectPoint.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000695 return;
696 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000697
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000698 DeviceId deviceId = connectPoint.deviceId();
699 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000700
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000701 for (UniTagInformation uniTag : sub.uniTagList()) {
702 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
703 }
704 }
alshabib3ea82642016-01-12 18:06:53 -0800705
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000706 /**
707 * Finds the uni tag information and provisions the found information.
708 * If the uni tag information is not found, returns
709 *
710 * @param deviceId the access device id
711 * @param uplinkPort the nni port
712 * @param subscriberPort the uni port
713 * @param innerVlan the pon c tag
714 * @param outerVlan the pon s tag
715 * @param tpId the technology profile id
716 */
717 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
718 PortNumber subscriberPort,
719 VlanId innerVlan,
720 VlanId outerVlan,
721 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700722
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000723 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
724 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
725 if (!gotTagInformation.isPresent()) {
726 return;
727 }
728 UniTagInformation tagInformation = gotTagInformation.get();
729 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
730 }
alshabib3ea82642016-01-12 18:06:53 -0800731
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000732 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800733 if (add) {
734 programmedSubs.put(connectPoint, tagInformation);
735 } else {
736 programmedSubs.remove(connectPoint, tagInformation);
737 }
Jonathan Harte533a422015-10-20 17:31:24 -0700738 }
739
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000740 /**
741 * Installs a uni tag information flow.
742 *
743 * @param deviceId the access device id
744 * @param uplinkPort the nni port
745 * @param subscriberPort the uni port
746 * @param tagInfo the uni tag information
747 */
748 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
749 UniTagInformation tagInfo) {
750
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700751 log.debug("Provisioning vlan-based flows for the uniTagInformation {} on dev/port {}/{}",
752 tagInfo, deviceId, subscriberPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000753
754 Port port = deviceService.getPort(deviceId, subscriberPort);
755
756 if (multicastServiceName.equals(tagInfo.getServiceName())) {
757 // IGMP flows are taken care of along with VOD service
758 // Please note that for each service, Subscriber Registered event will be sent
759 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
760 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
761 tagInfo.getTechnologyProfileId()));
762 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000763 }
764
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000765 BandwidthProfileInformation upstreamBpInfo =
766 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
767 BandwidthProfileInformation downstreamBpInfo =
768 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700769 if (upstreamBpInfo == null) {
770 log.warn("No meter installed since no Upstream BW Profile definition found for "
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700771 + "ctag {} stag {} tpId {} and dev/port: {}/{}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700772 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
773 tagInfo.getTechnologyProfileId(), deviceId,
774 subscriberPort);
775 return;
776 }
777 if (downstreamBpInfo == null) {
778 log.warn("No meter installed since no Downstream BW Profile definition found for "
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700779 + "ctag {} stag {} tpId {} and dev/port: {}/{}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700780 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
781 tagInfo.getTechnologyProfileId(), deviceId,
782 subscriberPort);
783 return;
784 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000785
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700786 // check for meterIds for the upstream and downstream bandwidth profiles
787 MeterId upMeterId = oltMeterService
788 .getMeterIdFromBpMapping(deviceId, upstreamBpInfo.id());
789 MeterId downMeterId = oltMeterService
790 .getMeterIdFromBpMapping(deviceId, downstreamBpInfo.id());
791 SubscriberFlowInfo fi = new SubscriberFlowInfo(deviceId, uplinkPort, subscriberPort,
792 tagInfo, downMeterId, upMeterId,
793 downstreamBpInfo.id(), upstreamBpInfo.id());
794
795 if (upMeterId != null && downMeterId != null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700796 log.debug("Meters are existing for upstream {} and downstream {} on dev/port {}/{}",
797 upstreamBpInfo.id(), downstreamBpInfo.id(), deviceId, subscriberPort);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700798 handleSubFlowsWithMeters(fi);
799 } else {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700800 log.debug("Adding {} on {}/{} to pending subs", fi, deviceId, subscriberPort);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700801 // one or both meters are not ready. It's possible they are in the process of being
802 // created for other subscribers that share the same bandwidth profile.
803 pendingSubscribers.add(fi);
804
805 // queue up the meters to be created
806 if (upMeterId == null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700807 log.debug("Missing meter for upstream {} on {}/{}", upstreamBpInfo.id(), deviceId, subscriberPort);
Andrea Campanella600d2e22020-06-22 11:00:31 +0200808 checkAndCreateDevMeter(deviceId, upstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700809 }
810 if (downMeterId == null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700811 log.debug("Missing meter for downstream {} on {}/{}", downstreamBpInfo.id(), deviceId, subscriberPort);
Andrea Campanella600d2e22020-06-22 11:00:31 +0200812 checkAndCreateDevMeter(deviceId, downstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700813 }
814 }
815 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200816 private void checkAndCreateDevMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
Andrea Campanellad1e26642020-10-23 12:08:32 +0200817 //If false the meter is already being installed, skipping installation
818 if (!oltMeterService.checkAndAddPendingMeter(deviceId, bwpInfo)) {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700819 return;
820 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200821 createMeter(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700822 }
823
Andrea Campanella600d2e22020-06-22 11:00:31 +0200824 private void createMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700825 log.debug("Creating Meter with {} on {}", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700826 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200827
Andrea Campanella600d2e22020-06-22 11:00:31 +0200828 MeterId meterId = oltMeterService.createMeter(deviceId, bwpInfo,
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700829 meterFuture);
830
831 meterFuture.thenAcceptAsync(result -> {
832 // iterate through the subscribers on hold
833 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
834 while (subsIterator.hasNext()) {
835 SubscriberFlowInfo fi = subsIterator.next();
836 if (result == null) {
837 // meter install sent to device
Andrea Campanella600d2e22020-06-22 11:00:31 +0200838 log.debug("Meter {} installed for bw {} on {}", meterId, bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700839
840 MeterId upMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200841 .getMeterIdFromBpMapping(deviceId, fi.getUpBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700842 MeterId downMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200843 .getMeterIdFromBpMapping(deviceId, fi.getDownBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700844 if (upMeterId != null && downMeterId != null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700845 log.debug("Provisioning subscriber after meter {} " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700846 "installation and both meters are present " +
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700847 "upstream {} and downstream {} on {}/{}",
848 meterId, upMeterId, downMeterId, deviceId, fi.getUniPort());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700849 // put in the meterIds because when fi was first
850 // created there may or may not have been a meterId
851 // depending on whether the meter was created or
852 // not at that time.
853 fi.setUpMeterId(upMeterId);
854 fi.setDownMeterId(downMeterId);
855 handleSubFlowsWithMeters(fi);
856 subsIterator.remove();
857 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200858 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700859 } else {
860 // meter install failed
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700861 log.error("Addition of subscriber {} on {}/{} failed due to meter " +
862 "{} with result {}", fi, deviceId, fi.getUniPort(), meterId, result);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700863 subsIterator.remove();
Andrea Campanella600d2e22020-06-22 11:00:31 +0200864 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700865 }
866 }
867 });
868 }
869 /**
870 * Add subscriber flows given meter information for both upstream and
871 * downstream directions.
872 *
873 * @param subscriberFlowInfo relevant information for subscriber
874 */
875 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700876 log.debug("Provisioning subscriber flows on {}/{} based on {}",
877 subscriberFlowInfo.getDevId(), subscriberFlowInfo.getUniPort(), subscriberFlowInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700878 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000879 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
880 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000881
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700882 ForwardingObjective.Builder upFwd =
883 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
884 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
885 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
886 @Override
887 public void onSuccess(Objective objective) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700888 log.debug("Upstream HSIA flow {} installed successfully on {}/{}",
889 subscriberFlowInfo, subscriberFlowInfo.getDevId(), subscriberFlowInfo.getUniPort());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700890 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000891 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000892
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700893 @Override
894 public void onError(Objective objective, ObjectiveError error) {
895 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000896 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700897 }));
898
899 ForwardingObjective.Builder downFwd =
900 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
901 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
902 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
903 @Override
904 public void onSuccess(Objective objective) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700905 log.debug("Downstream HSIA flow {} installed successfully on {}/{}",
906 subscriberFlowInfo, subscriberFlowInfo.getDevId(), subscriberFlowInfo.getUniPort());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700907 downFuture.complete(null);
908 }
909
910 @Override
911 public void onError(Objective objective, ObjectiveError error) {
912 downFuture.complete(error);
913 }
914 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000915
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100916 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000917 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100918 if (downStatus != null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700919 log.error("Flow with innervlan {} and outerVlan {} on {}/{} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700920 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
921 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000922 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100923 } else if (upStatus != null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700924 log.error("Flow with innervlan {} and outerVlan {} on {}/{} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700925 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
926 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000927 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000928 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700929 log.debug("Upstream and downstream data plane flows are installed successfully " +
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700930 "for {}/{}", subscriberFlowInfo.getDevId(), subscriberFlowInfo.getUniPort());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700931 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
932 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000933 tagInfo.getUpstreamBandwidthProfile(),
934 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700935 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
936 subscriberFlowInfo.getUniPort(),
937 subscriberFlowInfo.getUpId(),
938 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000939
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700940 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
941 subscriberFlowInfo.getUniPort(),
942 subscriberFlowInfo.getUpId(),
943 tagInfo, true, true);
944 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
945 subscriberFlowInfo.getUniPort()),
946 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100947 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700948 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
949 deviceService.getPort(subscriberFlowInfo.getDevId(),
950 subscriberFlowInfo.getUniPort()),
951 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
952 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100953 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100954 }
955
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000956 /**
957 * Checks the subscriber uni tag list and find the uni tag information.
958 * using the pon c tag, pon s tag and the technology profile id
959 * May return Optional<null>
960 *
961 * @param cp the connection point of the subscriber
962 * @param innerVlan pon c tag
963 * @param outerVlan pon s tag
964 * @param tpId the technology profile id
965 * @return the found uni tag information
966 */
967 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
968 int tpId) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700969 log.debug("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}",
970 cp.toString(), innerVlan, outerVlan, tpId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000971 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000972 if (subInfo == null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700973 log.warn("Subscriber information doesn't exist for the connect point {}", cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000974 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000975 }
976
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000977 List<UniTagInformation> uniTagList = subInfo.uniTagList();
978 if (uniTagList == null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700979 log.warn("Uni tag list is not found for the subscriber {} on {}", subInfo.id(), cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000980 return Optional.empty();
981 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100982
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000983 UniTagInformation service = null;
984 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
985 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
986 && tpId == tagInfo.getTechnologyProfileId()) {
987 service = tagInfo;
988 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700989 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000990 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000991
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000992 if (service == null) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -0700993 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {} on {}",
994 innerVlan, outerVlan, tpId, cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000995 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000996 }
997
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000998 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100999 }
1000
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001001 /**
Jonathan Hart403372d2018-08-22 11:44:13 -07001002 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
1003 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001004 *
1005 * @param dev Device to look for
1006 */
Jonathan Hart403372d2018-08-22 11:44:13 -07001007 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001008 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001009 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001010 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001011
1012 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -07001013 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001014 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001015 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001016 continue;
1017 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001018 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +01001019 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -07001020 log.info("Creating Eapol on {}/{}", dev.id(), p.number());
Andrea Campanellaa2491782020-03-13 18:09:31 +01001021 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1022 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
1023 } else {
Matteo Scandolo19b56f62020-10-29 13:29:21 -07001024 log.debug("Subscriber Eapol on {}/{} is already provisioned, not installing default",
1025 dev.id(), p.number());
Andrea Campanellaa2491782020-03-13 18:09:31 +01001026 }
Jonathan Hart403372d2018-08-22 11:44:13 -07001027 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001028 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001029 }
1030 }
1031 }
1032 }
1033
Jonathan Hart403372d2018-08-22 11:44:13 -07001034
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001035 /**
1036 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001037 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001038 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1039 * this logic needs to be changed
1040 *
1041 * @param dev Device to look for
1042 * @return The uplink Port of the OLT
1043 */
1044 private Port getUplinkPort(Device dev) {
1045 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001046 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -07001047 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001048 if (deviceInfo == null) {
1049 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001050 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -07001051 return null;
1052 }
1053 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +05301054 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001055 .filter(port -> isNniPort(port) ||
1056 (port.number().toLong() == deviceInfo.uplinkPort()))
1057 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +05301058 if (optionalPort.isPresent()) {
1059 log.trace("getUplinkPort: Found port {}", optionalPort.get());
1060 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001061 }
1062
Saurav Daseae48de2019-06-19 13:26:15 -07001063 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001064 return null;
1065 }
1066
1067 /**
1068 * Return the subscriber on a port.
1069 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001070 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001071 * @return subscriber if found else null
1072 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001073 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1074 Port port = deviceService.getPort(cp);
1075 checkNotNull(port, "Invalid connect point");
1076 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001077 return subsService.get(portName);
1078 }
1079
Gamze Abakaad329652018-12-20 10:12:21 +00001080 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001081 * Checks whether the given port of the device is a uni port or not.
1082 *
1083 * @param d the access device
1084 * @param p the port of the device
1085 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +00001086 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001087 private boolean isUniPort(Device d, Port p) {
1088 Port ulPort = getUplinkPort(d);
1089 if (ulPort != null) {
1090 return (ulPort.number().toLong() != p.number().toLong());
1091 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +05301092 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
1093 //checks whether the port name starts with nni- which is the signature of an NNI Port
1094 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
1095 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
1096 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
1097 return false;
1098 }
1099 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001100 }
1101
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001102 /**
1103 * Gets the given device details from SADIS.
1104 * If the device is not found, returns null
1105 *
1106 * @param dev the access device
1107 * @return the olt information
1108 */
Jonathan Hart4c538002018-08-23 10:11:54 -07001109 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1110 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001111 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001112 }
1113
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001114 // Custom-built function, when the device is not available we need a fallback mechanism
1115 private boolean isLocalLeader(DeviceId deviceId) {
1116 if (!mastershipService.isLocalMaster(deviceId)) {
1117 // When the device is available we just check the mastership
1118 if (deviceService.isAvailable(deviceId)) {
1119 return false;
1120 }
1121 // Fallback with Leadership service - device id is used as topic
1122 NodeId leader = leadershipService.runForLeadership(
1123 deviceId.toString()).leaderNodeId();
1124 // Verify if this node is the leader
1125 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001126 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001127 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001128 }
1129
kdarapuaa5da252020-04-10 15:58:05 +05301130 private boolean isNniPort(Port port) {
1131 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1132 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1133 }
1134 return false;
1135 }
1136
alshabibf0e7e702015-05-30 18:22:36 -07001137 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001138 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1139
alshabibf0e7e702015-05-30 18:22:36 -07001140 @Override
1141 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001142 eventExecutor.execute(() -> {
1143 DeviceId devId = event.subject().id();
1144 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001145 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001146 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001147
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001148 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1149 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1150 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001151 return;
1152 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001153
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001154 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001155 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001156 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1157 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1158 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001159 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001160 // Since no mastership of the device is present upon disconnection
1161 // the method in the FlowRuleManager only empties the local copy
1162 // of the DeviceFlowTable thus this method needs to get called
1163 // on every instance, see how it's done in the InternalDeviceListener
1164 // in FlowRuleManager: no mastership check for purgeOnDisconnection
Andrea Campanella3f34c992020-07-15 10:54:10 +02001165 handleDeviceDisconnection(dev, false, false);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001166 return;
1167 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001168 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001169 return;
1170 }
1171
Matteo Scandolo19b56f62020-10-29 13:29:21 -07001172 log.debug("OLT got {} event for {}/{}", eventType, event.subject(), event.port());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001173
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001174 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001175 // it's possible that we got an event for a previously
1176 // programmed OLT that is no longer available in SADIS
1177 // we let such events go through
1178 if (!programmedDevices.contains(devId)) {
1179 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001180 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001181 return;
1182 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001183 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001184
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001185 switch (event.type()) {
1186 //TODO: Port handling and bookkeeping should be improved once
1187 // olt firmware handles correct behaviour.
1188 case PORT_ADDED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001189 if (!deviceService.isAvailable(devId)) {
1190 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1191 return;
1192 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001193 if (isUniPort(dev, port)) {
1194 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001195
1196 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -07001197 log.info("eapol will be sent for port added {}/{}", devId, port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001198 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1199 null,
Andrea Campanella3f34c992020-07-15 10:54:10 +02001200 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1201 true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001202 }
1203 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001204 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1205 if (deviceInfo != null) {
1206 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1207 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001208 }
1209 break;
1210 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001211 if (isUniPort(dev, port)) {
Andrea Campanellacf0e3052020-08-27 11:05:39 +02001212 // if no subscriber is provisioned we need to remove the default EAPOL
1213 // if a subscriber was provisioned the default EAPOL will not be there and we can skip.
1214 // The EAPOL with subscriber tag will be removed by removeSubscriber call.
1215 Collection<? extends UniTagInformation> uniTagInformationSet =
1216 programmedSubs.get(new ConnectPoint(port.element().id(), port.number())).value();
1217 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Matteo Scandolo19b56f62020-10-29 13:29:21 -07001218 log.info("No subscriber provisioned on port {}/{} in PORT_REMOVED event, " +
1219 "removing default EAPOL flow", devId, port);
Andrea Campanellacf0e3052020-08-27 11:05:39 +02001220 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1221 null,
1222 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1223 false);
1224 } else {
1225 removeSubscriber(new ConnectPoint(devId, port.number()));
1226 }
Andy Bavier160e8682019-05-07 18:32:22 -07001227
Gamze Abaka838d8142019-02-21 07:06:55 +00001228 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001229 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001230 break;
1231 case PORT_UPDATED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001232 if (!deviceService.isAvailable(devId)) {
1233 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1234 return;
1235 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001236 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001237 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1238 if (deviceInfo != null && port.isEnabled()) {
1239 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1240 port.number());
1241 oltFlowService.processNniFilteringObjectives(dev.id(),
1242 port.number(), true);
1243 }
1244 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001245 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001246 ConnectPoint cp = new ConnectPoint(devId, port.number());
1247 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001248 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001249 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001250 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001251 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001252 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001253 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001254 null,
1255 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1256 port.isEnabled());
1257 }
1258 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001259 log.info("eapol will be {} for dev/port updated {}/{}",
1260 (port.isEnabled()) ? "added" : "removed",
1261 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001262 uniTagInformationSet.forEach(uniTag ->
1263 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1264 uniTag.getUpstreamBandwidthProfile(), null,
1265 uniTag.getPonCTag(), port.isEnabled()));
1266 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001267 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001268 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001269 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001270 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001271 }
alshabibbb83aa22016-02-10 15:08:23 -08001272 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001273 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001274 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001275 break;
1276 case DEVICE_REMOVED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001277 handleDeviceDisconnection(dev, true, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001278 break;
1279 case DEVICE_AVAILABILITY_CHANGED:
1280 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001281 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001282 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001283 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301284 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001285 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001286 + "flushing all state for dev:{}", devId);
Andrea Campanella3f34c992020-07-15 10:54:10 +02001287 handleDeviceDisconnection(dev, true, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001288 } else {
1289 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001290 + "assuming temporary disconnection, "
1291 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301292 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001293 }
1294 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001295 default:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001296 log.debug("Not handling event {}", event);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001297 return;
1298 }
1299 });
alshabibf0e7e702015-05-30 18:22:36 -07001300 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001301
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001302 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1303 deviceService.getPorts(device.id()).stream()
1304 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1305 .filter(p -> isUniPort(device, p))
1306 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1307 }
1308
Andrea Campanella3f34c992020-07-15 10:54:10 +02001309 private void handleDeviceDisconnection(Device device, boolean sendDisconnectedEvent, boolean sendUniEvent) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001310 programmedDevices.remove(device.id());
1311 removeAllSubscribers(device.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +02001312 //Handle case where OLT disconnects during subscriber provisioning
1313 pendingSubscribers.removeIf(fi -> fi.getDevId().equals(device.id()));
1314 oltFlowService.clearDeviceState(device.id());
1315
1316 //Complete meter and flow purge
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301317 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001318 oltMeterService.clearMeters(device.id());
Andrea Campanella3f34c992020-07-15 10:54:10 +02001319 if (sendDisconnectedEvent) {
1320 post(new AccessDeviceEvent(
1321 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1322 null, null, null));
1323 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001324 if (sendUniEvent) {
1325 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001326 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001327 }
1328
1329 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1330 post(new AccessDeviceEvent(
1331 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1332 null, null, null));
1333 programmedDevices.add(dev.id());
1334 checkAndCreateDeviceFlows(dev);
1335 if (sendUniEvent) {
1336 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1337 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001338 }
Gamze Abakada282b42019-03-11 13:16:48 +00001339
1340 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001341 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1342 .filter(e -> e.getKey().deviceId().equals(deviceId))
1343 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001344
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001345 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001346 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001347
Gamze Abaka641fc072018-09-04 09:16:27 +00001348 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001349
1350 private class InternalClusterListener implements ClusterEventListener {
1351
1352 @Override
1353 public void event(ClusterEvent event) {
1354 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1355 hasher.addServer(event.subject().id());
1356 }
1357 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1358 hasher.removeServer(event.subject().id());
1359 }
1360 }
1361 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001362
Hardik Windlass395ff372019-06-13 05:16:00 +00001363}