blob: d57265708100fb71fa4c39233ee702e2c8b036aa [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();
Gamze Abaka838d8142019-02-21 07:06:55 +0000294 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000295 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000296
Saurav Das4d8030c2020-09-21 18:56:35 -0700297 if (isSubscriberInstalled(connectPoint)) {
298 log.warn("Subscriber at {} already provisioned or in the process .."
299 + " not taking any more action", connectPoint);
300 return true;
301 }
302
303 // Find the subscriber config at this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000304 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100305 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000306 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100307 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100308 }
Jonathan Harte533a422015-10-20 17:31:24 -0700309
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100310 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000311 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100312 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000313 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100314 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700315 }
316
Saurav Das2d3777a2020-08-07 18:48:51 -0700317 // delete Eapol authentication flow with default bandwidth
318 // wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
319 // retry deletion if it fails/times-out
320 retryExecutor.execute(new DeleteEapolInstallSub(connectPoint,
321 uplinkPort, sub, 1));
Amit Ghosh31939522018-08-16 13:28:21 +0100322 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800323 }
324
Saurav Das4d8030c2020-09-21 18:56:35 -0700325 // returns true if subscriber is programmed or in the process of being programmed
326 private boolean isSubscriberInstalled(ConnectPoint connectPoint) {
327 Collection<? extends UniTagInformation> uniTagInformationSet =
328 programmedSubs.get(connectPoint).value();
329 if (!uniTagInformationSet.isEmpty()) {
330 return true;
331 }
332
333 for (SubscriberFlowInfo fi : pendingSubscribers) {
334 if (fi.getDevId().equals(connectPoint.deviceId())
335 && fi.getUniPort().equals(connectPoint.port())) {
336 return true;
337 }
338 }
339
340 return false;
341 }
342
Saurav Das2d3777a2020-08-07 18:48:51 -0700343 private class DeleteEapolInstallSub implements Runnable {
344 ConnectPoint cp;
345 Port uplinkPort;
346 SubscriberAndDeviceInformation sub;
347 private int attemptNumber;
348
349 DeleteEapolInstallSub(ConnectPoint cp, Port uplinkPort,
350 SubscriberAndDeviceInformation sub,
351 int attemptNumber) {
352 this.cp = cp;
353 this.uplinkPort = uplinkPort;
354 this.sub = sub;
355 this.attemptNumber = attemptNumber;
356 }
357
358 @Override
359 public void run() {
360 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
361 oltFlowService.processEapolFilteringObjectives(cp.deviceId(), cp.port(),
362 defaultBpId, filterFuture,
363 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
364 false);
365 filterFuture.thenAcceptAsync(filterStatus -> {
366 if (filterStatus == null) {
367 log.info("Default eapol flow deleted in attempt {} of {}"
368 + "... provisioning subscriber flows {}",
369 attemptNumber, eapolDeleteRetryMaxAttempts, cp);
370 provisionUniTagList(cp, uplinkPort.number(), sub);
371 } else {
372 if (attemptNumber <= eapolDeleteRetryMaxAttempts) {
373 log.warn("The filtering future failed {} for subscriber {}"
374 + "... retrying {} of {} attempts",
375 filterStatus, cp, attemptNumber, eapolDeleteRetryMaxAttempts);
376 retryExecutor.execute(
377 new DeleteEapolInstallSub(cp, uplinkPort, sub,
378 attemptNumber + 1));
379 } else {
380 log.error("The filtering future failed {} for subscriber {}"
381 + "after {} attempts. Subscriber provisioning failed",
382 filterStatus, cp, eapolDeleteRetryMaxAttempts);
383 sub.uniTagList().forEach(ut -> failedSubs.put(cp, ut));
384 }
385 }
386 });
387 }
388
389 }
390
alshabibb7a9e172016-01-13 11:23:53 -0800391 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000392 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700393 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000394
Saurav Daseae48de2019-06-19 13:26:15 -0700395 // Get the subscriber connected to this port from the local cache
396 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000397 DeviceId deviceId = connectPoint.deviceId();
398 PortNumber subscriberPortNo = connectPoint.port();
399
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800400 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000401 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000402 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000403 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800404 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800405 }
406
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100407 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000408 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100409 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000410 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100411 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800412 }
413
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000414 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100415
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000416 if (multicastServiceName.equals(uniTag.getServiceName())) {
417 continue;
418 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000419
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000420 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800421
Saurav Das9da7d522020-03-23 19:14:35 -0700422 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000423 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
424 uniTag.getUpstreamBandwidthProfile(),
425 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100426
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000427 Port port = deviceService.getPort(deviceId, subscriberPortNo);
428 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700429 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000430 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
431 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
432 } else {
433 log.debug("Port {} is no longer enabled or it's unavailable. Not "
434 + "reprogramming default eapol flow", connectPoint);
435 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100436 }
Amit Ghosh31939522018-08-16 13:28:21 +0100437 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800438 }
439
Gamze Abakaf59c0912019-04-19 08:24:28 +0000440
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000441 @Override
442 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
443 Optional<VlanId> cTag, Optional<Integer> tpId) {
444
445 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
446 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000447
Amit Ghosh31939522018-08-16 13:28:21 +0100448 // Check if we can find the connect point to which this subscriber is connected
449 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
450 if (subsPort == null) {
451 log.warn("ConnectPoint for {} not found", subscriberId);
452 return false;
453 }
454
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100455 if (!sTag.isPresent() && !cTag.isPresent()) {
456 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000457 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100458 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
459 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000460 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100461 return false;
462 }
463
Gamze Abakaf59c0912019-04-19 08:24:28 +0000464 //delete Eapol authentication flow with default bandwidth
465 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000466 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000467 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
468 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
469 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000470 filterFuture.thenAcceptAsync(filterStatus -> {
471 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000472 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
473 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000474 }
475 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100476 return true;
477 } else {
478 log.warn("Provisioning failed for subscriber: {}", subscriberId);
479 return false;
480 }
Amit Ghosh31939522018-08-16 13:28:21 +0100481 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100482
alshabibe0559672016-02-21 14:49:51 -0800483 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000484 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
485 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100486 // Check if we can find the connect point to which this subscriber is connected
487 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
488 if (subsPort == null) {
489 log.warn("ConnectPoint for {} not found", subscriberId);
490 return false;
491 }
492
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100493 if (!sTag.isPresent() && !cTag.isPresent()) {
494 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000495 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100496 // Get the uplink port
497 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
498 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000499 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100500 return false;
501 }
502
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000503 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
504 if (!tagInfo.isPresent()) {
505 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
506 subsPort, cTag, sTag, tpId);
507 return false;
508 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000509
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000510 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100511 return true;
512 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000513 log.warn("Removing subscriber is not possible - please check the provided information" +
514 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100515 return false;
516 }
Amit Ghosh31939522018-08-16 13:28:21 +0100517 }
518
519 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000520 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800521 return programmedSubs.stream()
522 .collect(collectingAndThen(
523 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
524 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700525 }
526
527 @Override
Saurav Das2d3777a2020-08-07 18:48:51 -0700528 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getFailedSubs() {
529 return failedSubs.stream()
530 .collect(collectingAndThen(
531 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
532 ImmutableMap::copyOf));
533 }
534
535 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100536 public List<DeviceId> fetchOlts() {
537 // look through all the devices and find the ones that are OLTs as per Sadis
538 List<DeviceId> olts = new ArrayList<>();
539 Iterable<Device> devices = deviceService.getDevices();
540 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700541 if (getOltInfo(d) != null) {
542 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100543 olts.add(d.id());
544 }
545 }
546 return olts;
alshabibe0559672016-02-21 14:49:51 -0800547 }
548
Amit Ghosh31939522018-08-16 13:28:21 +0100549 /**
550 * Finds the connect point to which a subscriber is connected.
551 *
552 * @param id The id of the subscriber, this is the same ID as in Sadis
553 * @return Subscribers ConnectPoint if found else null
554 */
555 private ConnectPoint findSubscriberConnectPoint(String id) {
556
557 Iterable<Device> devices = deviceService.getDevices();
558 for (Device d : devices) {
559 for (Port p : deviceService.getPorts(d.id())) {
560 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
561 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
562 log.debug("Found on device {} port {}", d.id(), p.number());
563 return new ConnectPoint(d.id(), p.number());
564 }
565 }
566 }
567 return null;
568 }
569
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000570 /**
571 * Gets the context of the bandwidth profile information for the given parameter.
572 *
573 * @param bandwidthProfile the bandwidth profile id
574 * @return the context of the bandwidth profile information
575 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000576 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
577 if (bandwidthProfile == null) {
578 return null;
579 }
580 return bpService.get(bandwidthProfile);
581 }
582
Gamze Abaka838d8142019-02-21 07:06:55 +0000583 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000584 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000585 *
586 * @param deviceId the device identifier
587 * @param uplink uplink port of the OLT
588 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000589 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000590 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000591 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000592 PortNumber subscriberPort, UniTagInformation uniTag) {
593
594 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800595
596 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
597 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
598
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000599 VlanId deviceVlan = uniTag.getPonSTag();
600 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000601
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000602 MeterId upstreamMeterId = oltMeterService
603 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
604 MeterId downstreamMeterId = oltMeterService
605 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000606
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000607 ForwardingObjective.Builder upFwd =
608 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
609 ForwardingObjective.Builder downFwd =
610 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
611
Andrea Campanella7c49b792020-05-11 11:36:53 +0200612 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
613 upstreamMeterId, uniTag, false, true);
614 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
615 upstreamMeterId, uniTag, false, true);
alshabibbf23a1f2016-01-14 17:27:11 -0800616
alshabib4ceaed32016-03-03 18:00:58 -0800617 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
618 @Override
619 public void onSuccess(Objective objective) {
620 upFuture.complete(null);
621 }
alshabibbf23a1f2016-01-14 17:27:11 -0800622
alshabib4ceaed32016-03-03 18:00:58 -0800623 @Override
624 public void onError(Objective objective, ObjectiveError error) {
625 upFuture.complete(error);
626 }
627 }));
628
629 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
630 @Override
631 public void onSuccess(Objective objective) {
632 downFuture.complete(null);
633 }
634
635 @Override
636 public void onError(Objective objective, ObjectiveError error) {
637 downFuture.complete(error);
638 }
639 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800640
641 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000642 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800643 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000644 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
645 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
646 uniTag.getTechnologyProfileId(), subscriberPort);
647 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800648 } else if (downStatus != null) {
649 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000650 "on port {} failed downstream uninstallation: {}",
651 subscriberVlan, deviceId, subscriberPort, downStatus);
652 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800653 } else if (upStatus != null) {
654 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000655 "on port {} failed upstream uninstallation: {}",
656 subscriberVlan, deviceId, subscriberPort, upStatus);
657 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800658 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000659 Port port = deviceService.getPort(deviceId, subscriberPort);
660 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
661 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800662 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700663 }
664
Gamze Abaka838d8142019-02-21 07:06:55 +0000665 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000666 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000667 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000668 * @param connectPoint the connection point of the subscriber
669 * @param uplinkPort uplink port of the OLT (the nni port)
670 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000671 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000672 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
673 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000674
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700675 log.debug("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000676
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000677 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
678 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
679 return;
680 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000681
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000682 DeviceId deviceId = connectPoint.deviceId();
683 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000684
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000685 for (UniTagInformation uniTag : sub.uniTagList()) {
686 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
687 }
688 }
alshabib3ea82642016-01-12 18:06:53 -0800689
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000690 /**
691 * Finds the uni tag information and provisions the found information.
692 * If the uni tag information is not found, returns
693 *
694 * @param deviceId the access device id
695 * @param uplinkPort the nni port
696 * @param subscriberPort the uni port
697 * @param innerVlan the pon c tag
698 * @param outerVlan the pon s tag
699 * @param tpId the technology profile id
700 */
701 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
702 PortNumber subscriberPort,
703 VlanId innerVlan,
704 VlanId outerVlan,
705 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700706
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000707 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
708 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
709 if (!gotTagInformation.isPresent()) {
710 return;
711 }
712 UniTagInformation tagInformation = gotTagInformation.get();
713 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
714 }
alshabib3ea82642016-01-12 18:06:53 -0800715
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000716 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800717 if (add) {
718 programmedSubs.put(connectPoint, tagInformation);
719 } else {
720 programmedSubs.remove(connectPoint, tagInformation);
721 }
Jonathan Harte533a422015-10-20 17:31:24 -0700722 }
723
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000724 /**
725 * Installs a uni tag information flow.
726 *
727 * @param deviceId the access device id
728 * @param uplinkPort the nni port
729 * @param subscriberPort the uni port
730 * @param tagInfo the uni tag information
731 */
732 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
733 UniTagInformation tagInfo) {
734
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700735 log.debug("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000736
737 Port port = deviceService.getPort(deviceId, subscriberPort);
738
739 if (multicastServiceName.equals(tagInfo.getServiceName())) {
740 // IGMP flows are taken care of along with VOD service
741 // Please note that for each service, Subscriber Registered event will be sent
742 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
743 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
744 tagInfo.getTechnologyProfileId()));
745 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000746 }
747
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000748 BandwidthProfileInformation upstreamBpInfo =
749 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
750 BandwidthProfileInformation downstreamBpInfo =
751 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700752 if (upstreamBpInfo == null) {
753 log.warn("No meter installed since no Upstream BW Profile definition found for "
754 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
755 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
756 tagInfo.getTechnologyProfileId(), deviceId,
757 subscriberPort);
758 return;
759 }
760 if (downstreamBpInfo == null) {
761 log.warn("No meter installed since no Downstream BW Profile definition found for "
762 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
763 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
764 tagInfo.getTechnologyProfileId(), deviceId,
765 subscriberPort);
766 return;
767 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000768
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700769 // check for meterIds for the upstream and downstream bandwidth profiles
770 MeterId upMeterId = oltMeterService
771 .getMeterIdFromBpMapping(deviceId, upstreamBpInfo.id());
772 MeterId downMeterId = oltMeterService
773 .getMeterIdFromBpMapping(deviceId, downstreamBpInfo.id());
774 SubscriberFlowInfo fi = new SubscriberFlowInfo(deviceId, uplinkPort, subscriberPort,
775 tagInfo, downMeterId, upMeterId,
776 downstreamBpInfo.id(), upstreamBpInfo.id());
777
778 if (upMeterId != null && downMeterId != null) {
779 log.debug("Meters are existing for upstream {} and downstream {}",
780 upstreamBpInfo.id(), downstreamBpInfo.id());
781 handleSubFlowsWithMeters(fi);
782 } else {
783 log.debug("Adding {} to pending subs", fi);
784 // one or both meters are not ready. It's possible they are in the process of being
785 // created for other subscribers that share the same bandwidth profile.
786 pendingSubscribers.add(fi);
787
788 // queue up the meters to be created
789 if (upMeterId == null) {
790 log.debug("Missing meter for upstream {}", upstreamBpInfo.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200791 checkAndCreateDevMeter(deviceId, upstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700792 }
793 if (downMeterId == null) {
794 log.debug("Missing meter for downstream {}", downstreamBpInfo.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200795 checkAndCreateDevMeter(deviceId, downstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700796 }
797 }
798 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200799 private void checkAndCreateDevMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
800 if (oltMeterService.isMeterPending(deviceId, bwpInfo)) {
801 log.debug("Meter is already pending {} on device {}", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700802 return;
803 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200804 oltMeterService.addToPendingMeters(deviceId, bwpInfo);
805 createMeter(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700806 }
807
Andrea Campanella600d2e22020-06-22 11:00:31 +0200808 private void createMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
809 log.debug("Creating Meter with {} on {} for subscriber", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700810 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200811
Andrea Campanella600d2e22020-06-22 11:00:31 +0200812 MeterId meterId = oltMeterService.createMeter(deviceId, bwpInfo,
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700813 meterFuture);
814
815 meterFuture.thenAcceptAsync(result -> {
816 // iterate through the subscribers on hold
817 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
818 while (subsIterator.hasNext()) {
819 SubscriberFlowInfo fi = subsIterator.next();
820 if (result == null) {
821 // meter install sent to device
Andrea Campanella600d2e22020-06-22 11:00:31 +0200822 log.debug("Meter {} installed for bw {} on {}", meterId, bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700823
824 MeterId upMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200825 .getMeterIdFromBpMapping(deviceId, fi.getUpBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700826 MeterId downMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200827 .getMeterIdFromBpMapping(deviceId, fi.getDownBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700828 if (upMeterId != null && downMeterId != null) {
829 log.debug("Provisioning subscriber after meter {}" +
830 "installation and both meters are present " +
831 "upstream {} and downstream {}",
832 meterId, upMeterId, downMeterId);
833 // put in the meterIds because when fi was first
834 // created there may or may not have been a meterId
835 // depending on whether the meter was created or
836 // not at that time.
837 fi.setUpMeterId(upMeterId);
838 fi.setDownMeterId(downMeterId);
839 handleSubFlowsWithMeters(fi);
840 subsIterator.remove();
841 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200842 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700843 } else {
844 // meter install failed
845 log.error("Addition of subscriber {} failed due to meter " +
846 "{} with result {}", fi, meterId, result);
847 subsIterator.remove();
Andrea Campanella600d2e22020-06-22 11:00:31 +0200848 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700849 }
850 }
851 });
852 }
853 /**
854 * Add subscriber flows given meter information for both upstream and
855 * downstream directions.
856 *
857 * @param subscriberFlowInfo relevant information for subscriber
858 */
859 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
860 log.debug("Provisioning subscriber flows based on {}", subscriberFlowInfo);
861 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000862 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
863 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000864
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700865 ForwardingObjective.Builder upFwd =
866 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
867 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
868 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
869 @Override
870 public void onSuccess(Objective objective) {
871 log.debug("Upstream flow installed successfully {}", subscriberFlowInfo);
872 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000873 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000874
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700875 @Override
876 public void onError(Objective objective, ObjectiveError error) {
877 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000878 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700879 }));
880
881 ForwardingObjective.Builder downFwd =
882 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
883 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
884 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
885 @Override
886 public void onSuccess(Objective objective) {
887 log.debug("Downstream flow installed successfully {}", subscriberFlowInfo);
888 downFuture.complete(null);
889 }
890
891 @Override
892 public void onError(Objective objective, ObjectiveError error) {
893 downFuture.complete(error);
894 }
895 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000896
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100897 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000898 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100899 if (downStatus != null) {
900 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000901 "on port {} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700902 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
903 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000904 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100905 } else if (upStatus != null) {
906 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000907 "on port {} failed upstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700908 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
909 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000910 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000911 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700912 log.debug("Upstream and downstream data plane flows are installed successfully " +
913 "for {}", subscriberFlowInfo);
914 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
915 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000916 tagInfo.getUpstreamBandwidthProfile(),
917 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700918 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
919 subscriberFlowInfo.getUniPort(),
920 subscriberFlowInfo.getUpId(),
921 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000922
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700923 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
924 subscriberFlowInfo.getUniPort(),
925 subscriberFlowInfo.getUpId(),
926 tagInfo, true, true);
927 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
928 subscriberFlowInfo.getUniPort()),
929 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100930 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700931 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
932 deviceService.getPort(subscriberFlowInfo.getDevId(),
933 subscriberFlowInfo.getUniPort()),
934 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
935 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100936 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100937 }
938
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000939 /**
940 * Checks the subscriber uni tag list and find the uni tag information.
941 * using the pon c tag, pon s tag and the technology profile id
942 * May return Optional<null>
943 *
944 * @param cp the connection point of the subscriber
945 * @param innerVlan pon c tag
946 * @param outerVlan pon s tag
947 * @param tpId the technology profile id
948 * @return the found uni tag information
949 */
950 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
951 int tpId) {
952 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
953 outerVlan, tpId);
954 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000955 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000956 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
957 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000958 }
959
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000960 List<UniTagInformation> uniTagList = subInfo.uniTagList();
961 if (uniTagList == null) {
962 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
963 return Optional.empty();
964 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100965
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000966 UniTagInformation service = null;
967 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
968 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
969 && tpId == tagInfo.getTechnologyProfileId()) {
970 service = tagInfo;
971 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700972 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000973 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000974
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000975 if (service == null) {
976 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
977 innerVlan, outerVlan, tpId);
978 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000979 }
980
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000981 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100982 }
983
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100984 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700985 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
986 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100987 *
988 * @param dev Device to look for
989 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700990 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100991 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000992 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000993 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100994
995 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700996 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100997 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800998 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000999 continue;
1000 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001001 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +01001002 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
1003 log.info("Creating Eapol for the uni {}", p);
1004 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1005 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
1006 } else {
1007 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
1008 "provisioned, not installing default", p.number(), dev.id());
1009 }
Jonathan Hart403372d2018-08-22 11:44:13 -07001010 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001011 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001012 }
1013 }
1014 }
1015 }
1016
Jonathan Hart403372d2018-08-22 11:44:13 -07001017
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001018 /**
1019 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001020 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001021 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1022 * this logic needs to be changed
1023 *
1024 * @param dev Device to look for
1025 * @return The uplink Port of the OLT
1026 */
1027 private Port getUplinkPort(Device dev) {
1028 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001029 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -07001030 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001031 if (deviceInfo == null) {
1032 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001033 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -07001034 return null;
1035 }
1036 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +05301037 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001038 .filter(port -> isNniPort(port) ||
1039 (port.number().toLong() == deviceInfo.uplinkPort()))
1040 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +05301041 if (optionalPort.isPresent()) {
1042 log.trace("getUplinkPort: Found port {}", optionalPort.get());
1043 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001044 }
1045
Saurav Daseae48de2019-06-19 13:26:15 -07001046 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001047 return null;
1048 }
1049
1050 /**
1051 * Return the subscriber on a port.
1052 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001053 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001054 * @return subscriber if found else null
1055 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001056 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1057 Port port = deviceService.getPort(cp);
1058 checkNotNull(port, "Invalid connect point");
1059 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001060 return subsService.get(portName);
1061 }
1062
Gamze Abakaad329652018-12-20 10:12:21 +00001063 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001064 * Checks whether the given port of the device is a uni port or not.
1065 *
1066 * @param d the access device
1067 * @param p the port of the device
1068 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +00001069 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001070 private boolean isUniPort(Device d, Port p) {
1071 Port ulPort = getUplinkPort(d);
1072 if (ulPort != null) {
1073 return (ulPort.number().toLong() != p.number().toLong());
1074 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +05301075 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
1076 //checks whether the port name starts with nni- which is the signature of an NNI Port
1077 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
1078 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
1079 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
1080 return false;
1081 }
1082 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001083 }
1084
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001085 /**
1086 * Gets the given device details from SADIS.
1087 * If the device is not found, returns null
1088 *
1089 * @param dev the access device
1090 * @return the olt information
1091 */
Jonathan Hart4c538002018-08-23 10:11:54 -07001092 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1093 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001094 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001095 }
1096
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001097 // Custom-built function, when the device is not available we need a fallback mechanism
1098 private boolean isLocalLeader(DeviceId deviceId) {
1099 if (!mastershipService.isLocalMaster(deviceId)) {
1100 // When the device is available we just check the mastership
1101 if (deviceService.isAvailable(deviceId)) {
1102 return false;
1103 }
1104 // Fallback with Leadership service - device id is used as topic
1105 NodeId leader = leadershipService.runForLeadership(
1106 deviceId.toString()).leaderNodeId();
1107 // Verify if this node is the leader
1108 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001109 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001110 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001111 }
1112
kdarapuaa5da252020-04-10 15:58:05 +05301113 private boolean isNniPort(Port port) {
1114 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1115 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1116 }
1117 return false;
1118 }
1119
alshabibf0e7e702015-05-30 18:22:36 -07001120 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001121 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1122
alshabibf0e7e702015-05-30 18:22:36 -07001123 @Override
1124 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001125 eventExecutor.execute(() -> {
1126 DeviceId devId = event.subject().id();
1127 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001128 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001129 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001130
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001131 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1132 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1133 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001134 return;
1135 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001136
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001137 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001138 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001139 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1140 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1141 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001142 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001143 // Since no mastership of the device is present upon disconnection
1144 // the method in the FlowRuleManager only empties the local copy
1145 // of the DeviceFlowTable thus this method needs to get called
1146 // on every instance, see how it's done in the InternalDeviceListener
1147 // in FlowRuleManager: no mastership check for purgeOnDisconnection
Andrea Campanella3f34c992020-07-15 10:54:10 +02001148 handleDeviceDisconnection(dev, false, false);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001149 return;
1150 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001151 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001152 return;
1153 }
1154
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001155 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
1156
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001157 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001158 // it's possible that we got an event for a previously
1159 // programmed OLT that is no longer available in SADIS
1160 // we let such events go through
1161 if (!programmedDevices.contains(devId)) {
1162 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001163 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001164 return;
1165 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001166 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001167
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001168 switch (event.type()) {
1169 //TODO: Port handling and bookkeeping should be improved once
1170 // olt firmware handles correct behaviour.
1171 case PORT_ADDED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001172 if (!deviceService.isAvailable(devId)) {
1173 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1174 return;
1175 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001176 if (isUniPort(dev, port)) {
1177 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001178
1179 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1180 log.info("eapol will be sent for port added {}", port);
1181 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1182 null,
Andrea Campanella3f34c992020-07-15 10:54:10 +02001183 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1184 true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001185 }
1186 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001187 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1188 if (deviceInfo != null) {
1189 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1190 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001191 }
1192 break;
1193 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001194 if (isUniPort(dev, port)) {
Andrea Campanellacf0e3052020-08-27 11:05:39 +02001195 // if no subscriber is provisioned we need to remove the default EAPOL
1196 // if a subscriber was provisioned the default EAPOL will not be there and we can skip.
1197 // The EAPOL with subscriber tag will be removed by removeSubscriber call.
1198 Collection<? extends UniTagInformation> uniTagInformationSet =
1199 programmedSubs.get(new ConnectPoint(port.element().id(), port.number())).value();
1200 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
1201 log.info("No subscriber provisioned on port {} in PORT_REMOVED event, " +
1202 "removing default EAPOL flow", port);
1203 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1204 null,
1205 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1206 false);
1207 } else {
1208 removeSubscriber(new ConnectPoint(devId, port.number()));
1209 }
Andy Bavier160e8682019-05-07 18:32:22 -07001210
Gamze Abaka838d8142019-02-21 07:06:55 +00001211 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001212 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001213 break;
1214 case PORT_UPDATED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001215 if (!deviceService.isAvailable(devId)) {
1216 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1217 return;
1218 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001219 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001220 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1221 if (deviceInfo != null && port.isEnabled()) {
1222 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1223 port.number());
1224 oltFlowService.processNniFilteringObjectives(dev.id(),
1225 port.number(), true);
1226 }
1227 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001228 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001229 ConnectPoint cp = new ConnectPoint(devId, port.number());
1230 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001231 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001232 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001233 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001234 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001235 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001236 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001237 null,
1238 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1239 port.isEnabled());
1240 }
1241 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001242 log.info("eapol will be {} for dev/port updated {}/{}",
1243 (port.isEnabled()) ? "added" : "removed",
1244 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001245 uniTagInformationSet.forEach(uniTag ->
1246 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1247 uniTag.getUpstreamBandwidthProfile(), null,
1248 uniTag.getPonCTag(), port.isEnabled()));
1249 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001250 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001251 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001252 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001253 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001254 }
alshabibbb83aa22016-02-10 15:08:23 -08001255 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001256 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001257 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001258 break;
1259 case DEVICE_REMOVED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001260 handleDeviceDisconnection(dev, true, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001261 break;
1262 case DEVICE_AVAILABILITY_CHANGED:
1263 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001264 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001265 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001266 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301267 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001268 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001269 + "flushing all state for dev:{}", devId);
Andrea Campanella3f34c992020-07-15 10:54:10 +02001270 handleDeviceDisconnection(dev, true, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001271 } else {
1272 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001273 + "assuming temporary disconnection, "
1274 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301275 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001276 }
1277 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001278 default:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001279 log.debug("Not handling event {}", event);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001280 return;
1281 }
1282 });
alshabibf0e7e702015-05-30 18:22:36 -07001283 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001284
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001285 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1286 deviceService.getPorts(device.id()).stream()
1287 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1288 .filter(p -> isUniPort(device, p))
1289 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1290 }
1291
Andrea Campanella3f34c992020-07-15 10:54:10 +02001292 private void handleDeviceDisconnection(Device device, boolean sendDisconnectedEvent, boolean sendUniEvent) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001293 programmedDevices.remove(device.id());
1294 removeAllSubscribers(device.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +02001295 //Handle case where OLT disconnects during subscriber provisioning
1296 pendingSubscribers.removeIf(fi -> fi.getDevId().equals(device.id()));
1297 oltFlowService.clearDeviceState(device.id());
1298
1299 //Complete meter and flow purge
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301300 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001301 oltMeterService.clearMeters(device.id());
Andrea Campanella3f34c992020-07-15 10:54:10 +02001302 if (sendDisconnectedEvent) {
1303 post(new AccessDeviceEvent(
1304 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1305 null, null, null));
1306 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001307 if (sendUniEvent) {
1308 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001309 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001310 }
1311
1312 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1313 post(new AccessDeviceEvent(
1314 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1315 null, null, null));
1316 programmedDevices.add(dev.id());
1317 checkAndCreateDeviceFlows(dev);
1318 if (sendUniEvent) {
1319 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1320 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001321 }
Gamze Abakada282b42019-03-11 13:16:48 +00001322
1323 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001324 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1325 .filter(e -> e.getKey().deviceId().equals(deviceId))
1326 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001327
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001328 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001329 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001330
Gamze Abaka641fc072018-09-04 09:16:27 +00001331 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001332
1333 private class InternalClusterListener implements ClusterEventListener {
1334
1335 @Override
1336 public void event(ClusterEvent event) {
1337 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1338 hasher.addServer(event.subject().id());
1339 }
1340 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1341 hasher.removeServer(event.subject().id());
1342 }
1343 }
1344 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001345
Hardik Windlass395ff372019-06-13 05:16:00 +00001346}