blob: 3323480d567caa979eb68ac170c4b84c3d9f4b95 [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 Das026650f2020-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 Das026650f2020-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) {
Andrea Campanellad1e26642020-10-23 12:08:32 +0200800 //If false the meter is already being installed, skipping installation
801 if (!oltMeterService.checkAndAddPendingMeter(deviceId, bwpInfo)) {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700802 return;
803 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200804 createMeter(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700805 }
806
Andrea Campanella600d2e22020-06-22 11:00:31 +0200807 private void createMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
808 log.debug("Creating Meter with {} on {} for subscriber", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700809 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200810
Andrea Campanella600d2e22020-06-22 11:00:31 +0200811 MeterId meterId = oltMeterService.createMeter(deviceId, bwpInfo,
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700812 meterFuture);
813
814 meterFuture.thenAcceptAsync(result -> {
815 // iterate through the subscribers on hold
816 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
817 while (subsIterator.hasNext()) {
818 SubscriberFlowInfo fi = subsIterator.next();
819 if (result == null) {
820 // meter install sent to device
Andrea Campanella600d2e22020-06-22 11:00:31 +0200821 log.debug("Meter {} installed for bw {} on {}", meterId, bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700822
823 MeterId upMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200824 .getMeterIdFromBpMapping(deviceId, fi.getUpBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700825 MeterId downMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200826 .getMeterIdFromBpMapping(deviceId, fi.getDownBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700827 if (upMeterId != null && downMeterId != null) {
828 log.debug("Provisioning subscriber after meter {}" +
829 "installation and both meters are present " +
830 "upstream {} and downstream {}",
831 meterId, upMeterId, downMeterId);
832 // put in the meterIds because when fi was first
833 // created there may or may not have been a meterId
834 // depending on whether the meter was created or
835 // not at that time.
836 fi.setUpMeterId(upMeterId);
837 fi.setDownMeterId(downMeterId);
838 handleSubFlowsWithMeters(fi);
839 subsIterator.remove();
840 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200841 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700842 } else {
843 // meter install failed
844 log.error("Addition of subscriber {} failed due to meter " +
845 "{} with result {}", fi, meterId, result);
846 subsIterator.remove();
Andrea Campanella600d2e22020-06-22 11:00:31 +0200847 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700848 }
849 }
850 });
851 }
852 /**
853 * Add subscriber flows given meter information for both upstream and
854 * downstream directions.
855 *
856 * @param subscriberFlowInfo relevant information for subscriber
857 */
858 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
859 log.debug("Provisioning subscriber flows based on {}", subscriberFlowInfo);
860 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000861 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
862 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000863
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700864 ForwardingObjective.Builder upFwd =
865 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
866 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
867 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
868 @Override
869 public void onSuccess(Objective objective) {
870 log.debug("Upstream flow installed successfully {}", subscriberFlowInfo);
871 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000872 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000873
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700874 @Override
875 public void onError(Objective objective, ObjectiveError error) {
876 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000877 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700878 }));
879
880 ForwardingObjective.Builder downFwd =
881 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
882 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
883 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
884 @Override
885 public void onSuccess(Objective objective) {
886 log.debug("Downstream flow installed successfully {}", subscriberFlowInfo);
887 downFuture.complete(null);
888 }
889
890 @Override
891 public void onError(Objective objective, ObjectiveError error) {
892 downFuture.complete(error);
893 }
894 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000895
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100896 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000897 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100898 if (downStatus != null) {
899 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000900 "on port {} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700901 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
902 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000903 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100904 } else if (upStatus != null) {
905 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000906 "on port {} failed upstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700907 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
908 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000909 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000910 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700911 log.debug("Upstream and downstream data plane flows are installed successfully " +
912 "for {}", subscriberFlowInfo);
913 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
914 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000915 tagInfo.getUpstreamBandwidthProfile(),
916 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700917 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
918 subscriberFlowInfo.getUniPort(),
919 subscriberFlowInfo.getUpId(),
920 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000921
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700922 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
923 subscriberFlowInfo.getUniPort(),
924 subscriberFlowInfo.getUpId(),
925 tagInfo, true, true);
926 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
927 subscriberFlowInfo.getUniPort()),
928 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100929 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700930 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
931 deviceService.getPort(subscriberFlowInfo.getDevId(),
932 subscriberFlowInfo.getUniPort()),
933 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
934 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100935 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100936 }
937
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000938 /**
939 * Checks the subscriber uni tag list and find the uni tag information.
940 * using the pon c tag, pon s tag and the technology profile id
941 * May return Optional<null>
942 *
943 * @param cp the connection point of the subscriber
944 * @param innerVlan pon c tag
945 * @param outerVlan pon s tag
946 * @param tpId the technology profile id
947 * @return the found uni tag information
948 */
949 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
950 int tpId) {
951 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
952 outerVlan, tpId);
953 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000954 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000955 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
956 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000957 }
958
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000959 List<UniTagInformation> uniTagList = subInfo.uniTagList();
960 if (uniTagList == null) {
961 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
962 return Optional.empty();
963 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100964
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000965 UniTagInformation service = null;
966 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
967 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
968 && tpId == tagInfo.getTechnologyProfileId()) {
969 service = tagInfo;
970 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700971 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000972 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000973
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000974 if (service == null) {
975 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
976 innerVlan, outerVlan, tpId);
977 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000978 }
979
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000980 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100981 }
982
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100983 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700984 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
985 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100986 *
987 * @param dev Device to look for
988 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700989 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100990 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000991 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000992 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100993
994 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700995 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100996 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800997 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000998 continue;
999 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001000 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +01001001 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
1002 log.info("Creating Eapol for the uni {}", p);
1003 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1004 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
1005 } else {
1006 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
1007 "provisioned, not installing default", p.number(), dev.id());
1008 }
Jonathan Hart403372d2018-08-22 11:44:13 -07001009 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001010 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001011 }
1012 }
1013 }
1014 }
1015
Jonathan Hart403372d2018-08-22 11:44:13 -07001016
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001017 /**
1018 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001019 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001020 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1021 * this logic needs to be changed
1022 *
1023 * @param dev Device to look for
1024 * @return The uplink Port of the OLT
1025 */
1026 private Port getUplinkPort(Device dev) {
1027 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001028 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -07001029 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001030 if (deviceInfo == null) {
1031 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001032 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -07001033 return null;
1034 }
1035 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +05301036 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001037 .filter(port -> isNniPort(port) ||
1038 (port.number().toLong() == deviceInfo.uplinkPort()))
1039 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +05301040 if (optionalPort.isPresent()) {
1041 log.trace("getUplinkPort: Found port {}", optionalPort.get());
1042 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001043 }
1044
Saurav Daseae48de2019-06-19 13:26:15 -07001045 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001046 return null;
1047 }
1048
1049 /**
1050 * Return the subscriber on a port.
1051 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001052 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001053 * @return subscriber if found else null
1054 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001055 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1056 Port port = deviceService.getPort(cp);
1057 checkNotNull(port, "Invalid connect point");
1058 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001059 return subsService.get(portName);
1060 }
1061
Gamze Abakaad329652018-12-20 10:12:21 +00001062 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001063 * Checks whether the given port of the device is a uni port or not.
1064 *
1065 * @param d the access device
1066 * @param p the port of the device
1067 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +00001068 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001069 private boolean isUniPort(Device d, Port p) {
1070 Port ulPort = getUplinkPort(d);
1071 if (ulPort != null) {
1072 return (ulPort.number().toLong() != p.number().toLong());
1073 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +05301074 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
1075 //checks whether the port name starts with nni- which is the signature of an NNI Port
1076 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
1077 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
1078 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
1079 return false;
1080 }
1081 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001082 }
1083
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001084 /**
1085 * Gets the given device details from SADIS.
1086 * If the device is not found, returns null
1087 *
1088 * @param dev the access device
1089 * @return the olt information
1090 */
Jonathan Hart4c538002018-08-23 10:11:54 -07001091 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1092 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001093 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001094 }
1095
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001096 // Custom-built function, when the device is not available we need a fallback mechanism
1097 private boolean isLocalLeader(DeviceId deviceId) {
1098 if (!mastershipService.isLocalMaster(deviceId)) {
1099 // When the device is available we just check the mastership
1100 if (deviceService.isAvailable(deviceId)) {
1101 return false;
1102 }
1103 // Fallback with Leadership service - device id is used as topic
1104 NodeId leader = leadershipService.runForLeadership(
1105 deviceId.toString()).leaderNodeId();
1106 // Verify if this node is the leader
1107 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001108 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001109 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001110 }
1111
kdarapuaa5da252020-04-10 15:58:05 +05301112 private boolean isNniPort(Port port) {
1113 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1114 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1115 }
1116 return false;
1117 }
1118
alshabibf0e7e702015-05-30 18:22:36 -07001119 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001120 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1121
alshabibf0e7e702015-05-30 18:22:36 -07001122 @Override
1123 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001124 eventExecutor.execute(() -> {
1125 DeviceId devId = event.subject().id();
1126 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001127 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001128 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001129
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001130 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1131 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1132 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001133 return;
1134 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001135
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001136 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001137 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001138 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1139 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1140 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001141 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001142 // Since no mastership of the device is present upon disconnection
1143 // the method in the FlowRuleManager only empties the local copy
1144 // of the DeviceFlowTable thus this method needs to get called
1145 // on every instance, see how it's done in the InternalDeviceListener
1146 // in FlowRuleManager: no mastership check for purgeOnDisconnection
Andrea Campanella3f34c992020-07-15 10:54:10 +02001147 handleDeviceDisconnection(dev, false, false);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001148 return;
1149 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001150 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001151 return;
1152 }
1153
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001154 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
1155
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001156 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001157 // it's possible that we got an event for a previously
1158 // programmed OLT that is no longer available in SADIS
1159 // we let such events go through
1160 if (!programmedDevices.contains(devId)) {
1161 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001162 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001163 return;
1164 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001165 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001166
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001167 switch (event.type()) {
1168 //TODO: Port handling and bookkeeping should be improved once
1169 // olt firmware handles correct behaviour.
1170 case PORT_ADDED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001171 if (!deviceService.isAvailable(devId)) {
1172 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1173 return;
1174 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001175 if (isUniPort(dev, port)) {
1176 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001177
1178 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1179 log.info("eapol will be sent for port added {}", port);
1180 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1181 null,
Andrea Campanella3f34c992020-07-15 10:54:10 +02001182 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1183 true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001184 }
1185 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001186 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1187 if (deviceInfo != null) {
1188 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1189 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001190 }
1191 break;
1192 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001193 if (isUniPort(dev, port)) {
Andrea Campanellacf0e3052020-08-27 11:05:39 +02001194 // if no subscriber is provisioned we need to remove the default EAPOL
1195 // if a subscriber was provisioned the default EAPOL will not be there and we can skip.
1196 // The EAPOL with subscriber tag will be removed by removeSubscriber call.
1197 Collection<? extends UniTagInformation> uniTagInformationSet =
1198 programmedSubs.get(new ConnectPoint(port.element().id(), port.number())).value();
1199 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
1200 log.info("No subscriber provisioned on port {} in PORT_REMOVED event, " +
1201 "removing default EAPOL flow", port);
1202 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1203 null,
1204 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1205 false);
1206 } else {
1207 removeSubscriber(new ConnectPoint(devId, port.number()));
1208 }
Andy Bavier160e8682019-05-07 18:32:22 -07001209
Gamze Abaka838d8142019-02-21 07:06:55 +00001210 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001211 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001212 break;
1213 case PORT_UPDATED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001214 if (!deviceService.isAvailable(devId)) {
1215 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1216 return;
1217 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001218 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001219 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1220 if (deviceInfo != null && port.isEnabled()) {
1221 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1222 port.number());
1223 oltFlowService.processNniFilteringObjectives(dev.id(),
1224 port.number(), true);
1225 }
1226 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001227 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001228 ConnectPoint cp = new ConnectPoint(devId, port.number());
1229 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001230 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001231 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001232 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001233 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001234 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001235 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001236 null,
1237 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1238 port.isEnabled());
1239 }
1240 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001241 log.info("eapol will be {} for dev/port updated {}/{}",
1242 (port.isEnabled()) ? "added" : "removed",
1243 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001244 uniTagInformationSet.forEach(uniTag ->
1245 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1246 uniTag.getUpstreamBandwidthProfile(), null,
1247 uniTag.getPonCTag(), port.isEnabled()));
1248 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001249 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001250 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001251 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001252 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001253 }
alshabibbb83aa22016-02-10 15:08:23 -08001254 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001255 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001256 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001257 break;
1258 case DEVICE_REMOVED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001259 handleDeviceDisconnection(dev, true, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001260 break;
1261 case DEVICE_AVAILABILITY_CHANGED:
1262 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001263 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001264 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001265 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301266 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001267 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001268 + "flushing all state for dev:{}", devId);
Andrea Campanella3f34c992020-07-15 10:54:10 +02001269 handleDeviceDisconnection(dev, true, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001270 } else {
1271 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001272 + "assuming temporary disconnection, "
1273 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301274 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001275 }
1276 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001277 default:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001278 log.debug("Not handling event {}", event);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001279 return;
1280 }
1281 });
alshabibf0e7e702015-05-30 18:22:36 -07001282 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001283
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001284 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1285 deviceService.getPorts(device.id()).stream()
1286 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1287 .filter(p -> isUniPort(device, p))
1288 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1289 }
1290
Andrea Campanella3f34c992020-07-15 10:54:10 +02001291 private void handleDeviceDisconnection(Device device, boolean sendDisconnectedEvent, boolean sendUniEvent) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001292 programmedDevices.remove(device.id());
1293 removeAllSubscribers(device.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +02001294 //Handle case where OLT disconnects during subscriber provisioning
1295 pendingSubscribers.removeIf(fi -> fi.getDevId().equals(device.id()));
1296 oltFlowService.clearDeviceState(device.id());
1297
1298 //Complete meter and flow purge
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301299 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001300 oltMeterService.clearMeters(device.id());
Andrea Campanella3f34c992020-07-15 10:54:10 +02001301 if (sendDisconnectedEvent) {
1302 post(new AccessDeviceEvent(
1303 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1304 null, null, null));
1305 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001306 if (sendUniEvent) {
1307 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001308 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001309 }
1310
1311 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1312 post(new AccessDeviceEvent(
1313 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1314 null, null, null));
1315 programmedDevices.add(dev.id());
1316 checkAndCreateDeviceFlows(dev);
1317 if (sendUniEvent) {
1318 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1319 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001320 }
Gamze Abakada282b42019-03-11 13:16:48 +00001321
1322 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001323 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1324 .filter(e -> e.getKey().deviceId().equals(deviceId))
1325 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001326
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001327 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001328 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001329
Gamze Abaka641fc072018-09-04 09:16:27 +00001330 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001331
1332 private class InternalClusterListener implements ClusterEventListener {
1333
1334 @Override
1335 public void event(ClusterEvent event) {
1336 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1337 hasher.addServer(event.subject().id());
1338 }
1339 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1340 hasher.removeServer(event.subject().id());
1341 }
1342 }
1343 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001344
Hardik Windlass395ff372019-06-13 05:16:00 +00001345}