blob: 3bb561a7ecd612eb2d58f4f77cf45660d7dcc836 [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;
28import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID;
29import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID_DEFAULT;
30import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_MCAST_SERVICE_NAME;
31import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Saurav Das2d3777a2020-08-07 18:48:51 -070032import static org.opencord.olt.impl.OsgiPropertyConstants.EAPOL_DELETE_RETRY_MAX_ATTEMPS;
33import static org.opencord.olt.impl.OsgiPropertyConstants.EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT;
Saurav Das9da7d522020-03-23 19:14:35 -070034import static org.slf4j.LoggerFactory.getLogger;
35
36import java.util.ArrayList;
37import java.util.Collection;
38import java.util.Dictionary;
Andrea Campanella0c3309d2020-05-29 01:51:18 -070039import java.util.Iterator;
Saurav Das9da7d522020-03-23 19:14:35 -070040import java.util.List;
41import java.util.Map;
42import java.util.Optional;
43import java.util.Properties;
44import java.util.Set;
45import java.util.concurrent.CompletableFuture;
46import java.util.concurrent.ExecutorService;
47import java.util.concurrent.Executors;
48
alshabibf0e7e702015-05-30 18:22:36 -070049import org.onlab.packet.VlanId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080050import org.onlab.util.KryoNamespace;
Gamze Abaka1f62dd92020-05-07 08:58:13 +000051import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080052import org.onosproject.cluster.ClusterEvent;
53import org.onosproject.cluster.ClusterEventListener;
54import org.onosproject.cluster.ClusterService;
55import org.onosproject.cluster.ControllerNode;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020056import org.onosproject.cluster.LeadershipService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080057import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070058import org.onosproject.core.ApplicationId;
59import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080060import org.onosproject.event.AbstractListenerManager;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020061import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010062import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070063import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010064import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070065import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080066import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070067import org.onosproject.net.PortNumber;
68import org.onosproject.net.device.DeviceEvent;
69import org.onosproject.net.device.DeviceListener;
70import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053071import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070072import org.onosproject.net.flowobjective.FlowObjectiveService;
73import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080074import org.onosproject.net.flowobjective.Objective;
75import org.onosproject.net.flowobjective.ObjectiveContext;
76import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070077import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080078import org.onosproject.store.serializers.KryoNamespaces;
79import org.onosproject.store.service.ConsistentMultimap;
80import org.onosproject.store.service.Serializer;
81import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070082import org.opencord.olt.AccessDeviceEvent;
83import org.opencord.olt.AccessDeviceListener;
84import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010085import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000086import org.opencord.olt.internalapi.AccessDeviceFlowService;
87import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000088import org.opencord.sadis.BandwidthProfileInformation;
89import org.opencord.sadis.BaseInformationService;
90import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010091import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000092import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080093import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070094import org.osgi.service.component.annotations.Activate;
95import org.osgi.service.component.annotations.Component;
96import org.osgi.service.component.annotations.Deactivate;
97import org.osgi.service.component.annotations.Modified;
98import org.osgi.service.component.annotations.Reference;
99import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -0700100import org.slf4j.Logger;
101
Saurav Das9da7d522020-03-23 19:14:35 -0700102import com.google.common.collect.ImmutableMap;
103import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700104
105/**
Jonathan Harte533a422015-10-20 17:31:24 -0700106 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700107 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700108@Component(immediate = true,
109 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700110 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000111 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Saurav Das2d3777a2020-08-07 18:48:51 -0700112 EAPOL_DELETE_RETRY_MAX_ATTEMPS + ":Integer=" +
113 EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700114 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800115public class Olt
116 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
117 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800118 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800119
Gamze Abakada282b42019-03-11 13:16:48 +0000120 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000121 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800122
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800123 public static final int HASH_WEIGHT = 10;
124
alshabibf0e7e702015-05-30 18:22:36 -0700125 private final Logger log = getLogger(getClass());
126
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530127 private static final String NNI = "nni-";
128
Carmelo Casconeca931162019-07-15 18:22:24 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700130 protected FlowObjectiveService flowObjectiveService;
131
Carmelo Casconeca931162019-07-15 18:22:24 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700133 protected DeviceService deviceService;
134
Carmelo Casconeca931162019-07-15 18:22:24 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700136 protected CoreService coreService;
137
Carmelo Casconeca931162019-07-15 18:22:24 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000139 protected SadisService sadisService;
140
Carmelo Casconeca931162019-07-15 18:22:24 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000142 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800143
Carmelo Casconeca931162019-07-15 18:22:24 -0700144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000145 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000146
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800147 @Reference(cardinality = ReferenceCardinality.MANDATORY)
148 protected StorageService storageService;
149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY)
151 protected ClusterService clusterService;
152
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530153 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200154 protected MastershipService mastershipService;
155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY)
157 protected LeadershipService leadershipService;
158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530160 protected FlowRuleService flowRuleService;
161
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000162 @Reference(cardinality = ReferenceCardinality.MANDATORY)
163 protected ComponentConfigService componentConfigService;
164
Carmelo Casconeca931162019-07-15 18:22:24 -0700165 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800166 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700167 **/
168 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000169
Carmelo Casconeca931162019-07-15 18:22:24 -0700170 /**
Gamze Abaka51a34e82020-05-08 13:03:14 +0000171 * Default multicast service name.
Carmelo Casconeca931162019-07-15 18:22:24 -0700172 **/
Gamze Abaka51a34e82020-05-08 13:03:14 +0000173 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Gamze Abaka33feef52019-02-27 08:16:47 +0000174
Saurav Das2d3777a2020-08-07 18:48:51 -0700175 /**
176 * Default amounts of eapol retry.
177 **/
178 protected int eapolDeleteRetryMaxAttempts = EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT;
179
alshabibf0e7e702015-05-30 18:22:36 -0700180 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800181 private final ClusterEventListener clusterListener = new InternalClusterListener();
182
183 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700184
Gamze Abaka641fc072018-09-04 09:16:27 +0000185 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
186 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700187
Gamze Abaka641fc072018-09-04 09:16:27 +0000188 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000189 groupedThreads("onos/olt-service",
190 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100191
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700192 protected ExecutorService eventExecutor;
Saurav Das2d3777a2020-08-07 18:48:51 -0700193 protected ExecutorService retryExecutor;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700194
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800195 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Das2d3777a2020-08-07 18:48:51 -0700196 private ConsistentMultimap<ConnectPoint, UniTagInformation> failedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800197
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700198 private Set<SubscriberFlowInfo> pendingSubscribers;
199
alshabibf0e7e702015-05-30 18:22:36 -0700200 @Activate
alshabibe0559672016-02-21 14:49:51 -0800201 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000202 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
203 "events-%d", log));
Saurav Das2d3777a2020-08-07 18:48:51 -0700204 retryExecutor = Executors.newCachedThreadPool();
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700205
alshabibe0559672016-02-21 14:49:51 -0800206 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000207 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000208 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800209
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800210 KryoNamespace serializer = KryoNamespace.newBuilder()
211 .register(KryoNamespaces.API)
212 .register(UniTagInformation.class)
213 .build();
214
215 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
216 .withName("volt-programmed-subs")
217 .withSerializer(Serializer.using(serializer))
218 .withApplicationId(appId)
219 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700220
Saurav Das2d3777a2020-08-07 18:48:51 -0700221 failedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
222 .withName("volt-failed-subs")
223 .withSerializer(Serializer.using(serializer))
224 .withApplicationId(appId)
225 .build();
226
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700227 pendingSubscribers = Sets.newConcurrentHashSet();
alshabib8e4fd2f2016-01-12 15:55:53 -0800228 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
229
Gamze Abaka641fc072018-09-04 09:16:27 +0000230 subsService = sadisService.getSubscriberInfoService();
231 bpService = sadisService.getBandwidthProfileService();
232
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800233 List<NodeId> readyNodes = clusterService.getNodes().stream()
234 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
235 .map(ControllerNode::id)
236 .collect(toList());
237 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
238 clusterService.addListener(clusterListener);
239
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100240 // look for all provisioned devices in Sadis and create EAPOL flows for the
241 // UNI ports
242 Iterable<Device> devices = deviceService.getDevices();
243 for (Device d : devices) {
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200244 if (isLocalLeader(d.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800245 checkAndCreateDeviceFlows(d);
246 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100247 }
alshabib4ceaed32016-03-03 18:00:58 -0800248
alshabibba357492016-01-27 13:49:46 -0800249 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700250 log.info("Started with Application ID {}", appId.id());
251 }
252
253 @Deactivate
254 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000255 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800256 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800257 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700258 eventDispatcher.removeSink(AccessDeviceEvent.class);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700259 eventExecutor.shutdown();
Saurav Das2d3777a2020-08-07 18:48:51 -0700260 retryExecutor.shutdown();
alshabibf0e7e702015-05-30 18:22:36 -0700261 log.info("Stopped");
262 }
263
alshabibe0559672016-02-21 14:49:51 -0800264 @Modified
265 public void modified(ComponentContext context) {
266 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
267
268 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200269 String bpId = get(properties, DEFAULT_BP_ID);
270 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000271
Andrea Campanella971d5b92020-05-07 11:20:43 +0200272 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
273 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000274
Saurav Das2d3777a2020-08-07 18:48:51 -0700275 String eapolDeleteRetryNew = get(properties, EAPOL_DELETE_RETRY_MAX_ATTEMPS);
276 eapolDeleteRetryMaxAttempts = isNullOrEmpty(eapolDeleteRetryNew) ? EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT :
277 Integer.parseInt(eapolDeleteRetryNew.trim());
278
279 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}, EapolDeleteRetryMaxAttempts: {}",
280 defaultBpId, multicastServiceName, eapolDeleteRetryMaxAttempts);
Gamze Abaka33feef52019-02-27 08:16:47 +0000281
alshabibe0559672016-02-21 14:49:51 -0800282 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000283 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200284 defaultBpId = DEFAULT_BP_ID_DEFAULT;
285 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800286 }
287 }
288
alshabib32232c82016-02-25 17:57:24 -0500289 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000290 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200291 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000292 DeviceId deviceId = connectPoint.deviceId();
293 PortNumber subscriberPortNo = connectPoint.port();
294
295 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000296 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000297
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100298 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000299 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100300 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000301 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100302 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100303 }
Jonathan Harte533a422015-10-20 17:31:24 -0700304
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100305 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000306 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100307 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000308 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100309 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700310 }
311
Saurav Das2d3777a2020-08-07 18:48:51 -0700312 // delete Eapol authentication flow with default bandwidth
313 // wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
314 // retry deletion if it fails/times-out
315 retryExecutor.execute(new DeleteEapolInstallSub(connectPoint,
316 uplinkPort, sub, 1));
Amit Ghosh31939522018-08-16 13:28:21 +0100317 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800318 }
319
Saurav Das2d3777a2020-08-07 18:48:51 -0700320 private class DeleteEapolInstallSub implements Runnable {
321 ConnectPoint cp;
322 Port uplinkPort;
323 SubscriberAndDeviceInformation sub;
324 private int attemptNumber;
325
326 DeleteEapolInstallSub(ConnectPoint cp, Port uplinkPort,
327 SubscriberAndDeviceInformation sub,
328 int attemptNumber) {
329 this.cp = cp;
330 this.uplinkPort = uplinkPort;
331 this.sub = sub;
332 this.attemptNumber = attemptNumber;
333 }
334
335 @Override
336 public void run() {
337 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
338 oltFlowService.processEapolFilteringObjectives(cp.deviceId(), cp.port(),
339 defaultBpId, filterFuture,
340 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
341 false);
342 filterFuture.thenAcceptAsync(filterStatus -> {
343 if (filterStatus == null) {
344 log.info("Default eapol flow deleted in attempt {} of {}"
345 + "... provisioning subscriber flows {}",
346 attemptNumber, eapolDeleteRetryMaxAttempts, cp);
347 provisionUniTagList(cp, uplinkPort.number(), sub);
348 } else {
349 if (attemptNumber <= eapolDeleteRetryMaxAttempts) {
350 log.warn("The filtering future failed {} for subscriber {}"
351 + "... retrying {} of {} attempts",
352 filterStatus, cp, attemptNumber, eapolDeleteRetryMaxAttempts);
353 retryExecutor.execute(
354 new DeleteEapolInstallSub(cp, uplinkPort, sub,
355 attemptNumber + 1));
356 } else {
357 log.error("The filtering future failed {} for subscriber {}"
358 + "after {} attempts. Subscriber provisioning failed",
359 filterStatus, cp, eapolDeleteRetryMaxAttempts);
360 sub.uniTagList().forEach(ut -> failedSubs.put(cp, ut));
361 }
362 }
363 });
364 }
365
366 }
367
alshabibb7a9e172016-01-13 11:23:53 -0800368 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000369 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700370 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000371
Saurav Daseae48de2019-06-19 13:26:15 -0700372 // Get the subscriber connected to this port from the local cache
373 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000374 DeviceId deviceId = connectPoint.deviceId();
375 PortNumber subscriberPortNo = connectPoint.port();
376
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800377 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000378 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000379 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000380 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800381 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800382 }
383
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100384 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000385 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100386 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000387 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100388 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800389 }
390
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000391 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100392
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000393 if (multicastServiceName.equals(uniTag.getServiceName())) {
394 continue;
395 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000396
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000397 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800398
Saurav Das9da7d522020-03-23 19:14:35 -0700399 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000400 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
401 uniTag.getUpstreamBandwidthProfile(),
402 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100403
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000404 Port port = deviceService.getPort(deviceId, subscriberPortNo);
405 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700406 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000407 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
408 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
409 } else {
410 log.debug("Port {} is no longer enabled or it's unavailable. Not "
411 + "reprogramming default eapol flow", connectPoint);
412 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100413 }
Amit Ghosh31939522018-08-16 13:28:21 +0100414 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800415 }
416
Gamze Abakaf59c0912019-04-19 08:24:28 +0000417
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000418 @Override
419 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
420 Optional<VlanId> cTag, Optional<Integer> tpId) {
421
422 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
423 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000424
Amit Ghosh31939522018-08-16 13:28:21 +0100425 // Check if we can find the connect point to which this subscriber is connected
426 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
427 if (subsPort == null) {
428 log.warn("ConnectPoint for {} not found", subscriberId);
429 return false;
430 }
431
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100432 if (!sTag.isPresent() && !cTag.isPresent()) {
433 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000434 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100435 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
436 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000437 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100438 return false;
439 }
440
Gamze Abakaf59c0912019-04-19 08:24:28 +0000441 //delete Eapol authentication flow with default bandwidth
442 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000443 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000444 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
445 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
446 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000447 filterFuture.thenAcceptAsync(filterStatus -> {
448 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000449 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
450 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000451 }
452 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100453 return true;
454 } else {
455 log.warn("Provisioning failed for subscriber: {}", subscriberId);
456 return false;
457 }
Amit Ghosh31939522018-08-16 13:28:21 +0100458 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100459
alshabibe0559672016-02-21 14:49:51 -0800460 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000461 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
462 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100463 // Check if we can find the connect point to which this subscriber is connected
464 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
465 if (subsPort == null) {
466 log.warn("ConnectPoint for {} not found", subscriberId);
467 return false;
468 }
469
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100470 if (!sTag.isPresent() && !cTag.isPresent()) {
471 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000472 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100473 // Get the uplink port
474 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
475 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000476 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100477 return false;
478 }
479
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000480 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
481 if (!tagInfo.isPresent()) {
482 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
483 subsPort, cTag, sTag, tpId);
484 return false;
485 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000486
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000487 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100488 return true;
489 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000490 log.warn("Removing subscriber is not possible - please check the provided information" +
491 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100492 return false;
493 }
Amit Ghosh31939522018-08-16 13:28:21 +0100494 }
495
496 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000497 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800498 return programmedSubs.stream()
499 .collect(collectingAndThen(
500 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
501 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700502 }
503
504 @Override
Saurav Das2d3777a2020-08-07 18:48:51 -0700505 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getFailedSubs() {
506 return failedSubs.stream()
507 .collect(collectingAndThen(
508 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
509 ImmutableMap::copyOf));
510 }
511
512 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100513 public List<DeviceId> fetchOlts() {
514 // look through all the devices and find the ones that are OLTs as per Sadis
515 List<DeviceId> olts = new ArrayList<>();
516 Iterable<Device> devices = deviceService.getDevices();
517 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700518 if (getOltInfo(d) != null) {
519 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100520 olts.add(d.id());
521 }
522 }
523 return olts;
alshabibe0559672016-02-21 14:49:51 -0800524 }
525
Amit Ghosh31939522018-08-16 13:28:21 +0100526 /**
527 * Finds the connect point to which a subscriber is connected.
528 *
529 * @param id The id of the subscriber, this is the same ID as in Sadis
530 * @return Subscribers ConnectPoint if found else null
531 */
532 private ConnectPoint findSubscriberConnectPoint(String id) {
533
534 Iterable<Device> devices = deviceService.getDevices();
535 for (Device d : devices) {
536 for (Port p : deviceService.getPorts(d.id())) {
537 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
538 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
539 log.debug("Found on device {} port {}", d.id(), p.number());
540 return new ConnectPoint(d.id(), p.number());
541 }
542 }
543 }
544 return null;
545 }
546
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000547 /**
548 * Gets the context of the bandwidth profile information for the given parameter.
549 *
550 * @param bandwidthProfile the bandwidth profile id
551 * @return the context of the bandwidth profile information
552 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000553 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
554 if (bandwidthProfile == null) {
555 return null;
556 }
557 return bpService.get(bandwidthProfile);
558 }
559
Gamze Abaka838d8142019-02-21 07:06:55 +0000560 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000561 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000562 *
563 * @param deviceId the device identifier
564 * @param uplink uplink port of the OLT
565 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000566 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000567 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000568 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000569 PortNumber subscriberPort, UniTagInformation uniTag) {
570
571 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800572
573 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
574 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
575
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000576 VlanId deviceVlan = uniTag.getPonSTag();
577 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000578
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000579 MeterId upstreamMeterId = oltMeterService
580 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
581 MeterId downstreamMeterId = oltMeterService
582 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000583
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000584 ForwardingObjective.Builder upFwd =
585 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
586 ForwardingObjective.Builder downFwd =
587 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
588
Andrea Campanella7c49b792020-05-11 11:36:53 +0200589 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
590 upstreamMeterId, uniTag, false, true);
591 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
592 upstreamMeterId, uniTag, false, true);
alshabibbf23a1f2016-01-14 17:27:11 -0800593
alshabib4ceaed32016-03-03 18:00:58 -0800594 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
595 @Override
596 public void onSuccess(Objective objective) {
597 upFuture.complete(null);
598 }
alshabibbf23a1f2016-01-14 17:27:11 -0800599
alshabib4ceaed32016-03-03 18:00:58 -0800600 @Override
601 public void onError(Objective objective, ObjectiveError error) {
602 upFuture.complete(error);
603 }
604 }));
605
606 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
607 @Override
608 public void onSuccess(Objective objective) {
609 downFuture.complete(null);
610 }
611
612 @Override
613 public void onError(Objective objective, ObjectiveError error) {
614 downFuture.complete(error);
615 }
616 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800617
618 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000619 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800620 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000621 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
622 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
623 uniTag.getTechnologyProfileId(), subscriberPort);
624 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800625 } else if (downStatus != null) {
626 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000627 "on port {} failed downstream uninstallation: {}",
628 subscriberVlan, deviceId, subscriberPort, downStatus);
629 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800630 } else if (upStatus != null) {
631 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000632 "on port {} failed upstream uninstallation: {}",
633 subscriberVlan, deviceId, subscriberPort, upStatus);
634 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800635 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000636 Port port = deviceService.getPort(deviceId, subscriberPort);
637 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
638 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800639 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700640 }
641
Gamze Abaka838d8142019-02-21 07:06:55 +0000642 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000643 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000644 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000645 * @param connectPoint the connection point of the subscriber
646 * @param uplinkPort uplink port of the OLT (the nni port)
647 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000648 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000649 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
650 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000651
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700652 log.debug("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000653
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000654 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
655 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
656 return;
657 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000658
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000659 DeviceId deviceId = connectPoint.deviceId();
660 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000661
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000662 for (UniTagInformation uniTag : sub.uniTagList()) {
663 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
664 }
665 }
alshabib3ea82642016-01-12 18:06:53 -0800666
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000667 /**
668 * Finds the uni tag information and provisions the found information.
669 * If the uni tag information is not found, returns
670 *
671 * @param deviceId the access device id
672 * @param uplinkPort the nni port
673 * @param subscriberPort the uni port
674 * @param innerVlan the pon c tag
675 * @param outerVlan the pon s tag
676 * @param tpId the technology profile id
677 */
678 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
679 PortNumber subscriberPort,
680 VlanId innerVlan,
681 VlanId outerVlan,
682 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700683
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000684 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
685 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
686 if (!gotTagInformation.isPresent()) {
687 return;
688 }
689 UniTagInformation tagInformation = gotTagInformation.get();
690 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
691 }
alshabib3ea82642016-01-12 18:06:53 -0800692
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000693 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800694 if (add) {
695 programmedSubs.put(connectPoint, tagInformation);
696 } else {
697 programmedSubs.remove(connectPoint, tagInformation);
698 }
Jonathan Harte533a422015-10-20 17:31:24 -0700699 }
700
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000701 /**
702 * Installs a uni tag information flow.
703 *
704 * @param deviceId the access device id
705 * @param uplinkPort the nni port
706 * @param subscriberPort the uni port
707 * @param tagInfo the uni tag information
708 */
709 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
710 UniTagInformation tagInfo) {
711
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700712 log.debug("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000713
714 Port port = deviceService.getPort(deviceId, subscriberPort);
715
716 if (multicastServiceName.equals(tagInfo.getServiceName())) {
717 // IGMP flows are taken care of along with VOD service
718 // Please note that for each service, Subscriber Registered event will be sent
719 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
720 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
721 tagInfo.getTechnologyProfileId()));
722 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000723 }
724
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000725 BandwidthProfileInformation upstreamBpInfo =
726 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
727 BandwidthProfileInformation downstreamBpInfo =
728 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700729 if (upstreamBpInfo == null) {
730 log.warn("No meter installed since no Upstream BW Profile definition found for "
731 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
732 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
733 tagInfo.getTechnologyProfileId(), deviceId,
734 subscriberPort);
735 return;
736 }
737 if (downstreamBpInfo == null) {
738 log.warn("No meter installed since no Downstream BW Profile definition found for "
739 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
740 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
741 tagInfo.getTechnologyProfileId(), deviceId,
742 subscriberPort);
743 return;
744 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000745
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700746 // check for meterIds for the upstream and downstream bandwidth profiles
747 MeterId upMeterId = oltMeterService
748 .getMeterIdFromBpMapping(deviceId, upstreamBpInfo.id());
749 MeterId downMeterId = oltMeterService
750 .getMeterIdFromBpMapping(deviceId, downstreamBpInfo.id());
751 SubscriberFlowInfo fi = new SubscriberFlowInfo(deviceId, uplinkPort, subscriberPort,
752 tagInfo, downMeterId, upMeterId,
753 downstreamBpInfo.id(), upstreamBpInfo.id());
754
755 if (upMeterId != null && downMeterId != null) {
756 log.debug("Meters are existing for upstream {} and downstream {}",
757 upstreamBpInfo.id(), downstreamBpInfo.id());
758 handleSubFlowsWithMeters(fi);
759 } else {
760 log.debug("Adding {} to pending subs", fi);
761 // one or both meters are not ready. It's possible they are in the process of being
762 // created for other subscribers that share the same bandwidth profile.
763 pendingSubscribers.add(fi);
764
765 // queue up the meters to be created
766 if (upMeterId == null) {
767 log.debug("Missing meter for upstream {}", upstreamBpInfo.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200768 checkAndCreateDevMeter(deviceId, upstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700769 }
770 if (downMeterId == null) {
771 log.debug("Missing meter for downstream {}", downstreamBpInfo.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200772 checkAndCreateDevMeter(deviceId, downstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700773 }
774 }
775 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200776 private void checkAndCreateDevMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
777 if (oltMeterService.isMeterPending(deviceId, bwpInfo)) {
778 log.debug("Meter is already pending {} on device {}", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700779 return;
780 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200781 oltMeterService.addToPendingMeters(deviceId, bwpInfo);
782 createMeter(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700783 }
784
Andrea Campanella600d2e22020-06-22 11:00:31 +0200785 private void createMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
786 log.debug("Creating Meter with {} on {} for subscriber", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700787 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200788
Andrea Campanella600d2e22020-06-22 11:00:31 +0200789 MeterId meterId = oltMeterService.createMeter(deviceId, bwpInfo,
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700790 meterFuture);
791
792 meterFuture.thenAcceptAsync(result -> {
793 // iterate through the subscribers on hold
794 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
795 while (subsIterator.hasNext()) {
796 SubscriberFlowInfo fi = subsIterator.next();
797 if (result == null) {
798 // meter install sent to device
Andrea Campanella600d2e22020-06-22 11:00:31 +0200799 log.debug("Meter {} installed for bw {} on {}", meterId, bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700800
801 MeterId upMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200802 .getMeterIdFromBpMapping(deviceId, fi.getUpBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700803 MeterId downMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200804 .getMeterIdFromBpMapping(deviceId, fi.getDownBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700805 if (upMeterId != null && downMeterId != null) {
806 log.debug("Provisioning subscriber after meter {}" +
807 "installation and both meters are present " +
808 "upstream {} and downstream {}",
809 meterId, upMeterId, downMeterId);
810 // put in the meterIds because when fi was first
811 // created there may or may not have been a meterId
812 // depending on whether the meter was created or
813 // not at that time.
814 fi.setUpMeterId(upMeterId);
815 fi.setDownMeterId(downMeterId);
816 handleSubFlowsWithMeters(fi);
817 subsIterator.remove();
818 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200819 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700820 } else {
821 // meter install failed
822 log.error("Addition of subscriber {} failed due to meter " +
823 "{} with result {}", fi, meterId, result);
824 subsIterator.remove();
Andrea Campanella600d2e22020-06-22 11:00:31 +0200825 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700826 }
827 }
828 });
829 }
830 /**
831 * Add subscriber flows given meter information for both upstream and
832 * downstream directions.
833 *
834 * @param subscriberFlowInfo relevant information for subscriber
835 */
836 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
837 log.debug("Provisioning subscriber flows based on {}", subscriberFlowInfo);
838 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000839 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
840 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000841
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700842 ForwardingObjective.Builder upFwd =
843 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
844 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
845 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
846 @Override
847 public void onSuccess(Objective objective) {
848 log.debug("Upstream flow installed successfully {}", subscriberFlowInfo);
849 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000850 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000851
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700852 @Override
853 public void onError(Objective objective, ObjectiveError error) {
854 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000855 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700856 }));
857
858 ForwardingObjective.Builder downFwd =
859 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
860 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
861 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
862 @Override
863 public void onSuccess(Objective objective) {
864 log.debug("Downstream flow installed successfully {}", subscriberFlowInfo);
865 downFuture.complete(null);
866 }
867
868 @Override
869 public void onError(Objective objective, ObjectiveError error) {
870 downFuture.complete(error);
871 }
872 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000873
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100874 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000875 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100876 if (downStatus != null) {
877 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000878 "on port {} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700879 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
880 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000881 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100882 } else if (upStatus != null) {
883 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000884 "on port {} failed upstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700885 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
886 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000887 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000888 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700889 log.debug("Upstream and downstream data plane flows are installed successfully " +
890 "for {}", subscriberFlowInfo);
891 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
892 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000893 tagInfo.getUpstreamBandwidthProfile(),
894 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700895 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
896 subscriberFlowInfo.getUniPort(),
897 subscriberFlowInfo.getUpId(),
898 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000899
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700900 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
901 subscriberFlowInfo.getUniPort(),
902 subscriberFlowInfo.getUpId(),
903 tagInfo, true, true);
904 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
905 subscriberFlowInfo.getUniPort()),
906 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100907 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700908 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
909 deviceService.getPort(subscriberFlowInfo.getDevId(),
910 subscriberFlowInfo.getUniPort()),
911 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
912 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100913 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100914 }
915
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000916 /**
917 * Checks the subscriber uni tag list and find the uni tag information.
918 * using the pon c tag, pon s tag and the technology profile id
919 * May return Optional<null>
920 *
921 * @param cp the connection point of the subscriber
922 * @param innerVlan pon c tag
923 * @param outerVlan pon s tag
924 * @param tpId the technology profile id
925 * @return the found uni tag information
926 */
927 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
928 int tpId) {
929 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
930 outerVlan, tpId);
931 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000932 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000933 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
934 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000935 }
936
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000937 List<UniTagInformation> uniTagList = subInfo.uniTagList();
938 if (uniTagList == null) {
939 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
940 return Optional.empty();
941 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100942
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000943 UniTagInformation service = null;
944 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
945 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
946 && tpId == tagInfo.getTechnologyProfileId()) {
947 service = tagInfo;
948 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700949 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000950 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000951
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000952 if (service == null) {
953 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
954 innerVlan, outerVlan, tpId);
955 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000956 }
957
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000958 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100959 }
960
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100961 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700962 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
963 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100964 *
965 * @param dev Device to look for
966 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700967 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100968 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000969 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000970 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100971
972 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700973 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100974 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800975 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000976 continue;
977 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100978 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100979 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
980 log.info("Creating Eapol for the uni {}", p);
981 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
982 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
983 } else {
984 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
985 "provisioned, not installing default", p.number(), dev.id());
986 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700987 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000988 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100989 }
990 }
991 }
992 }
993
Jonathan Hart403372d2018-08-22 11:44:13 -0700994
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100995 /**
996 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000997 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100998 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
999 * this logic needs to be changed
1000 *
1001 * @param dev Device to look for
1002 * @return The uplink Port of the OLT
1003 */
1004 private Port getUplinkPort(Device dev) {
1005 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001006 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -07001007 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001008 if (deviceInfo == null) {
1009 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001010 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -07001011 return null;
1012 }
1013 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +05301014 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001015 .filter(port -> isNniPort(port) ||
1016 (port.number().toLong() == deviceInfo.uplinkPort()))
1017 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +05301018 if (optionalPort.isPresent()) {
1019 log.trace("getUplinkPort: Found port {}", optionalPort.get());
1020 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001021 }
1022
Saurav Daseae48de2019-06-19 13:26:15 -07001023 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001024 return null;
1025 }
1026
1027 /**
1028 * Return the subscriber on a port.
1029 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001030 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001031 * @return subscriber if found else null
1032 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001033 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1034 Port port = deviceService.getPort(cp);
1035 checkNotNull(port, "Invalid connect point");
1036 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001037 return subsService.get(portName);
1038 }
1039
Gamze Abakaad329652018-12-20 10:12:21 +00001040 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001041 * Checks whether the given port of the device is a uni port or not.
1042 *
1043 * @param d the access device
1044 * @param p the port of the device
1045 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +00001046 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001047 private boolean isUniPort(Device d, Port p) {
1048 Port ulPort = getUplinkPort(d);
1049 if (ulPort != null) {
1050 return (ulPort.number().toLong() != p.number().toLong());
1051 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +05301052 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
1053 //checks whether the port name starts with nni- which is the signature of an NNI Port
1054 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
1055 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
1056 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
1057 return false;
1058 }
1059 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001060 }
1061
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001062 /**
1063 * Gets the given device details from SADIS.
1064 * If the device is not found, returns null
1065 *
1066 * @param dev the access device
1067 * @return the olt information
1068 */
Jonathan Hart4c538002018-08-23 10:11:54 -07001069 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1070 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001071 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001072 }
1073
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001074 // Custom-built function, when the device is not available we need a fallback mechanism
1075 private boolean isLocalLeader(DeviceId deviceId) {
1076 if (!mastershipService.isLocalMaster(deviceId)) {
1077 // When the device is available we just check the mastership
1078 if (deviceService.isAvailable(deviceId)) {
1079 return false;
1080 }
1081 // Fallback with Leadership service - device id is used as topic
1082 NodeId leader = leadershipService.runForLeadership(
1083 deviceId.toString()).leaderNodeId();
1084 // Verify if this node is the leader
1085 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001086 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001087 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001088 }
1089
kdarapuaa5da252020-04-10 15:58:05 +05301090 private boolean isNniPort(Port port) {
1091 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1092 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1093 }
1094 return false;
1095 }
1096
alshabibf0e7e702015-05-30 18:22:36 -07001097 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001098 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1099
alshabibf0e7e702015-05-30 18:22:36 -07001100 @Override
1101 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001102 eventExecutor.execute(() -> {
1103 DeviceId devId = event.subject().id();
1104 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001105 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001106 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001107
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001108 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1109 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1110 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001111 return;
1112 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001113
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001114 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001115 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001116 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1117 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1118 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001119 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001120 // Since no mastership of the device is present upon disconnection
1121 // the method in the FlowRuleManager only empties the local copy
1122 // of the DeviceFlowTable thus this method needs to get called
1123 // on every instance, see how it's done in the InternalDeviceListener
1124 // in FlowRuleManager: no mastership check for purgeOnDisconnection
Andrea Campanella3f34c992020-07-15 10:54:10 +02001125 handleDeviceDisconnection(dev, false, false);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001126 return;
1127 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001128 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001129 return;
1130 }
1131
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001132 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
1133
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001134 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001135 // it's possible that we got an event for a previously
1136 // programmed OLT that is no longer available in SADIS
1137 // we let such events go through
1138 if (!programmedDevices.contains(devId)) {
1139 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001140 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001141 return;
1142 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001143 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001144
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001145 switch (event.type()) {
1146 //TODO: Port handling and bookkeeping should be improved once
1147 // olt firmware handles correct behaviour.
1148 case PORT_ADDED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001149 if (!deviceService.isAvailable(devId)) {
1150 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1151 return;
1152 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001153 if (isUniPort(dev, port)) {
1154 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001155
1156 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1157 log.info("eapol will be sent for port added {}", port);
1158 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1159 null,
Andrea Campanella3f34c992020-07-15 10:54:10 +02001160 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1161 true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001162 }
1163 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001164 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1165 if (deviceInfo != null) {
1166 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1167 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001168 }
1169 break;
1170 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001171 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001172 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001173 log.info("eapol will be send for port {} removed", port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001174 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1175 null,
1176 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001177
Gamze Abaka838d8142019-02-21 07:06:55 +00001178 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001179 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001180 break;
1181 case PORT_UPDATED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001182 if (!deviceService.isAvailable(devId)) {
1183 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1184 return;
1185 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001186 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001187 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1188 if (deviceInfo != null && port.isEnabled()) {
1189 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1190 port.number());
1191 oltFlowService.processNniFilteringObjectives(dev.id(),
1192 port.number(), true);
1193 }
1194 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001195 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001196 ConnectPoint cp = new ConnectPoint(devId, port.number());
1197 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001198 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001199 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001200 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001201 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001202 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001203 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001204 null,
1205 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1206 port.isEnabled());
1207 }
1208 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001209 log.info("eapol will be {} for dev/port updated {}/{}",
1210 (port.isEnabled()) ? "added" : "removed",
1211 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001212 uniTagInformationSet.forEach(uniTag ->
1213 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1214 uniTag.getUpstreamBandwidthProfile(), null,
1215 uniTag.getPonCTag(), port.isEnabled()));
1216 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001217 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001218 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001219 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001220 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001221 }
alshabibbb83aa22016-02-10 15:08:23 -08001222 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001223 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001224 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001225 break;
1226 case DEVICE_REMOVED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001227 handleDeviceDisconnection(dev, true, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001228 break;
1229 case DEVICE_AVAILABILITY_CHANGED:
1230 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001231 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001232 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001233 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301234 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001235 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001236 + "flushing all state for dev:{}", devId);
Andrea Campanella3f34c992020-07-15 10:54:10 +02001237 handleDeviceDisconnection(dev, true, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001238 } else {
1239 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001240 + "assuming temporary disconnection, "
1241 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301242 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001243 }
1244 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001245 default:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001246 log.debug("Not handling event {}", event);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001247 return;
1248 }
1249 });
alshabibf0e7e702015-05-30 18:22:36 -07001250 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001251
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001252 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1253 deviceService.getPorts(device.id()).stream()
1254 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1255 .filter(p -> isUniPort(device, p))
1256 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1257 }
1258
Andrea Campanella3f34c992020-07-15 10:54:10 +02001259 private void handleDeviceDisconnection(Device device, boolean sendDisconnectedEvent, boolean sendUniEvent) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001260 programmedDevices.remove(device.id());
1261 removeAllSubscribers(device.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +02001262 //Handle case where OLT disconnects during subscriber provisioning
1263 pendingSubscribers.removeIf(fi -> fi.getDevId().equals(device.id()));
1264 oltFlowService.clearDeviceState(device.id());
1265
1266 //Complete meter and flow purge
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301267 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001268 oltMeterService.clearMeters(device.id());
Andrea Campanella3f34c992020-07-15 10:54:10 +02001269 if (sendDisconnectedEvent) {
1270 post(new AccessDeviceEvent(
1271 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1272 null, null, null));
1273 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001274 if (sendUniEvent) {
1275 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001276 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001277 }
1278
1279 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1280 post(new AccessDeviceEvent(
1281 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1282 null, null, null));
1283 programmedDevices.add(dev.id());
1284 checkAndCreateDeviceFlows(dev);
1285 if (sendUniEvent) {
1286 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1287 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001288 }
Gamze Abakada282b42019-03-11 13:16:48 +00001289
1290 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001291 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1292 .filter(e -> e.getKey().deviceId().equals(deviceId))
1293 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001294
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001295 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001296 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001297
Gamze Abaka641fc072018-09-04 09:16:27 +00001298 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001299
1300 private class InternalClusterListener implements ClusterEventListener {
1301
1302 @Override
1303 public void event(ClusterEvent event) {
1304 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1305 hasher.addServer(event.subject().id());
1306 }
1307 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1308 hasher.removeServer(event.subject().id());
1309 }
1310 }
1311 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001312
Hardik Windlass395ff372019-06-13 05:16:00 +00001313}