blob: c98b69cdfc8986beab8e43f9345c28571f70f645 [file] [log] [blame]
alshabibf0e7e702015-05-30 18:22:36 -07001/*
Brian O'Connord6a135a2017-08-03 22:46:05 -07002 * Copyright 2016-present Open Networking Foundation
alshabibf0e7e702015-05-30 18:22:36 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
alshabib36a4d732016-06-01 16:03:59 -070016package org.opencord.olt.impl;
alshabibf0e7e702015-05-30 18:22:36 -070017
Saurav Das9da7d522020-03-23 19:14:35 -070018import static com.google.common.base.Preconditions.checkNotNull;
Andrea Campanella971d5b92020-05-07 11:20:43 +020019import static com.google.common.base.Strings.isNullOrEmpty;
Saurav Das9da7d522020-03-23 19:14:35 -070020import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
21import static java.util.stream.Collectors.collectingAndThen;
22import static java.util.stream.Collectors.groupingBy;
23import static java.util.stream.Collectors.mapping;
24import static java.util.stream.Collectors.toList;
25import static java.util.stream.Collectors.toSet;
26import static org.onlab.util.Tools.get;
27import static org.onlab.util.Tools.groupedThreads;
Matteo Scandoloa4aaa972020-10-23 15:24:38 -070028import static org.opencord.olt.impl.OsgiPropertyConstants.*;
Saurav Das9da7d522020-03-23 19:14:35 -070029import static org.slf4j.LoggerFactory.getLogger;
30
31import java.util.ArrayList;
32import java.util.Collection;
33import java.util.Dictionary;
Andrea Campanella0c3309d2020-05-29 01:51:18 -070034import java.util.Iterator;
Saurav Das9da7d522020-03-23 19:14:35 -070035import java.util.List;
36import java.util.Map;
37import java.util.Optional;
38import java.util.Properties;
39import java.util.Set;
40import java.util.concurrent.CompletableFuture;
41import java.util.concurrent.ExecutorService;
42import java.util.concurrent.Executors;
Matteo Scandoloa4aaa972020-10-23 15:24:38 -070043import java.util.concurrent.ScheduledExecutorService;
44import java.util.concurrent.TimeUnit;
Saurav Das9da7d522020-03-23 19:14:35 -070045
alshabibf0e7e702015-05-30 18:22:36 -070046import org.onlab.packet.VlanId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080047import org.onlab.util.KryoNamespace;
Gamze Abaka1f62dd92020-05-07 08:58:13 +000048import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080049import org.onosproject.cluster.ClusterEvent;
50import org.onosproject.cluster.ClusterEventListener;
51import org.onosproject.cluster.ClusterService;
52import org.onosproject.cluster.ControllerNode;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020053import org.onosproject.cluster.LeadershipService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080054import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onosproject.core.ApplicationId;
56import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080057import org.onosproject.event.AbstractListenerManager;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020058import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010059import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070060import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010061import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070062import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080063import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070064import org.onosproject.net.PortNumber;
65import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053068import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
70import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080071import org.onosproject.net.flowobjective.Objective;
72import org.onosproject.net.flowobjective.ObjectiveContext;
73import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070074import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080075import org.onosproject.store.serializers.KryoNamespaces;
76import org.onosproject.store.service.ConsistentMultimap;
77import org.onosproject.store.service.Serializer;
78import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070079import org.opencord.olt.AccessDeviceEvent;
80import org.opencord.olt.AccessDeviceListener;
81import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010082import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000083import org.opencord.olt.internalapi.AccessDeviceFlowService;
84import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000085import org.opencord.sadis.BandwidthProfileInformation;
86import org.opencord.sadis.BaseInformationService;
87import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010088import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000089import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080090import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070091import org.osgi.service.component.annotations.Activate;
92import org.osgi.service.component.annotations.Component;
93import org.osgi.service.component.annotations.Deactivate;
94import org.osgi.service.component.annotations.Modified;
95import org.osgi.service.component.annotations.Reference;
96import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070097import org.slf4j.Logger;
98
Saurav Das9da7d522020-03-23 19:14:35 -070099import com.google.common.collect.ImmutableMap;
100import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700101
102/**
Jonathan Harte533a422015-10-20 17:31:24 -0700103 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700104 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700105@Component(immediate = true,
106 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700107 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000108 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Saurav Das2d3777a2020-08-07 18:48:51 -0700109 EAPOL_DELETE_RETRY_MAX_ATTEMPS + ":Integer=" +
110 EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT,
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700111 PROVISION_DELAY + ":Integer=" + PROVISION_DELAY_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700112 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800113public class Olt
114 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
115 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800116 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800117
Gamze Abakada282b42019-03-11 13:16:48 +0000118 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000119 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800120
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800121 public static final int HASH_WEIGHT = 10;
122
alshabibf0e7e702015-05-30 18:22:36 -0700123 private final Logger log = getLogger(getClass());
124
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530125 private static final String NNI = "nni-";
126
Carmelo Casconeca931162019-07-15 18:22:24 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700128 protected FlowObjectiveService flowObjectiveService;
129
Carmelo Casconeca931162019-07-15 18:22:24 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700131 protected DeviceService deviceService;
132
Carmelo Casconeca931162019-07-15 18:22:24 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700134 protected CoreService coreService;
135
Carmelo Casconeca931162019-07-15 18:22:24 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000137 protected SadisService sadisService;
138
Carmelo Casconeca931162019-07-15 18:22:24 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000140 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800141
Carmelo Casconeca931162019-07-15 18:22:24 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000143 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000144
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
146 protected StorageService storageService;
147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
149 protected ClusterService clusterService;
150
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200152 protected MastershipService mastershipService;
153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY)
155 protected LeadershipService leadershipService;
156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530158 protected FlowRuleService flowRuleService;
159
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000160 @Reference(cardinality = ReferenceCardinality.MANDATORY)
161 protected ComponentConfigService componentConfigService;
162
Carmelo Casconeca931162019-07-15 18:22:24 -0700163 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800164 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700165 **/
166 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000167
Carmelo Casconeca931162019-07-15 18:22:24 -0700168 /**
Gamze Abaka51a34e82020-05-08 13:03:14 +0000169 * Default multicast service name.
Carmelo Casconeca931162019-07-15 18:22:24 -0700170 **/
Gamze Abaka51a34e82020-05-08 13:03:14 +0000171 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Gamze Abaka33feef52019-02-27 08:16:47 +0000172
Saurav Das2d3777a2020-08-07 18:48:51 -0700173 /**
174 * Default amounts of eapol retry.
175 **/
176 protected int eapolDeleteRetryMaxAttempts = EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT;
177
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700178 /**
179 * Delay between EAPOL removal and data plane flows provisioning.
180 */
181 protected int provisionDelay = PROVISION_DELAY_DEFAULT;
182
alshabibf0e7e702015-05-30 18:22:36 -0700183 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800184 private final ClusterEventListener clusterListener = new InternalClusterListener();
185
186 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700187
Gamze Abaka641fc072018-09-04 09:16:27 +0000188 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
189 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700190
Gamze Abaka641fc072018-09-04 09:16:27 +0000191 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000192 groupedThreads("onos/olt-service",
193 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100194
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700195 protected ExecutorService eventExecutor;
Saurav Das2d3777a2020-08-07 18:48:51 -0700196 protected ExecutorService retryExecutor;
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700197 protected ScheduledExecutorService provisionExecutor;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700198
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800199 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Das2d3777a2020-08-07 18:48:51 -0700200 private ConsistentMultimap<ConnectPoint, UniTagInformation> failedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800201
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700202 private Set<SubscriberFlowInfo> pendingSubscribers;
203
alshabibf0e7e702015-05-30 18:22:36 -0700204 @Activate
alshabibe0559672016-02-21 14:49:51 -0800205 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000206 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
207 "events-%d", log));
Saurav Das2d3777a2020-08-07 18:48:51 -0700208 retryExecutor = Executors.newCachedThreadPool();
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700209 provisionExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
210 "provision-%d", log));
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700211
alshabibe0559672016-02-21 14:49:51 -0800212 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000213 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000214 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800215
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800216 KryoNamespace serializer = KryoNamespace.newBuilder()
217 .register(KryoNamespaces.API)
218 .register(UniTagInformation.class)
219 .build();
220
221 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
222 .withName("volt-programmed-subs")
223 .withSerializer(Serializer.using(serializer))
224 .withApplicationId(appId)
225 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700226
Saurav Das2d3777a2020-08-07 18:48:51 -0700227 failedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
228 .withName("volt-failed-subs")
229 .withSerializer(Serializer.using(serializer))
230 .withApplicationId(appId)
231 .build();
232
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700233 pendingSubscribers = Sets.newConcurrentHashSet();
alshabib8e4fd2f2016-01-12 15:55:53 -0800234 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
235
Gamze Abaka641fc072018-09-04 09:16:27 +0000236 subsService = sadisService.getSubscriberInfoService();
237 bpService = sadisService.getBandwidthProfileService();
238
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800239 List<NodeId> readyNodes = clusterService.getNodes().stream()
240 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
241 .map(ControllerNode::id)
242 .collect(toList());
243 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
244 clusterService.addListener(clusterListener);
245
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100246 // look for all provisioned devices in Sadis and create EAPOL flows for the
247 // UNI ports
248 Iterable<Device> devices = deviceService.getDevices();
249 for (Device d : devices) {
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200250 if (isLocalLeader(d.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800251 checkAndCreateDeviceFlows(d);
252 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100253 }
alshabib4ceaed32016-03-03 18:00:58 -0800254
alshabibba357492016-01-27 13:49:46 -0800255 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700256 log.info("Started with Application ID {}", appId.id());
257 }
258
259 @Deactivate
260 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000261 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800262 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800263 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700264 eventDispatcher.removeSink(AccessDeviceEvent.class);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700265 eventExecutor.shutdown();
Saurav Das2d3777a2020-08-07 18:48:51 -0700266 retryExecutor.shutdown();
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700267 provisionExecutor.shutdown();
alshabibf0e7e702015-05-30 18:22:36 -0700268 log.info("Stopped");
269 }
270
alshabibe0559672016-02-21 14:49:51 -0800271 @Modified
272 public void modified(ComponentContext context) {
273 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
274
275 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200276 String bpId = get(properties, DEFAULT_BP_ID);
277 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000278
Andrea Campanella971d5b92020-05-07 11:20:43 +0200279 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
280 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000281
Saurav Das2d3777a2020-08-07 18:48:51 -0700282 String eapolDeleteRetryNew = get(properties, EAPOL_DELETE_RETRY_MAX_ATTEMPS);
283 eapolDeleteRetryMaxAttempts = isNullOrEmpty(eapolDeleteRetryNew) ? EAPOL_DELETE_RETRY_MAX_ATTEMPS_DEFAULT :
284 Integer.parseInt(eapolDeleteRetryNew.trim());
285
286 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}, EapolDeleteRetryMaxAttempts: {}",
287 defaultBpId, multicastServiceName, eapolDeleteRetryMaxAttempts);
Gamze Abaka33feef52019-02-27 08:16:47 +0000288
alshabibe0559672016-02-21 14:49:51 -0800289 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000290 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200291 defaultBpId = DEFAULT_BP_ID_DEFAULT;
292 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800293 }
294 }
295
alshabib32232c82016-02-25 17:57:24 -0500296 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000297 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200298 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000299 DeviceId deviceId = connectPoint.deviceId();
300 PortNumber subscriberPortNo = connectPoint.port();
Gamze Abaka838d8142019-02-21 07:06:55 +0000301 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000302 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000303
Saurav Das026650f2020-09-21 18:56:35 -0700304 if (isSubscriberInstalled(connectPoint)) {
305 log.warn("Subscriber at {} already provisioned or in the process .."
306 + " not taking any more action", connectPoint);
307 return true;
308 }
309
310 // Find the subscriber config at this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000311 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100312 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000313 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100314 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100315 }
Jonathan Harte533a422015-10-20 17:31:24 -0700316
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100317 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000318 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100319 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000320 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100321 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700322 }
323
Saurav Das2d3777a2020-08-07 18:48:51 -0700324 // delete Eapol authentication flow with default bandwidth
325 // wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
326 // retry deletion if it fails/times-out
327 retryExecutor.execute(new DeleteEapolInstallSub(connectPoint,
328 uplinkPort, sub, 1));
Amit Ghosh31939522018-08-16 13:28:21 +0100329 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800330 }
331
Saurav Das026650f2020-09-21 18:56:35 -0700332 // returns true if subscriber is programmed or in the process of being programmed
333 private boolean isSubscriberInstalled(ConnectPoint connectPoint) {
334 Collection<? extends UniTagInformation> uniTagInformationSet =
335 programmedSubs.get(connectPoint).value();
336 if (!uniTagInformationSet.isEmpty()) {
337 return true;
338 }
339
340 for (SubscriberFlowInfo fi : pendingSubscribers) {
341 if (fi.getDevId().equals(connectPoint.deviceId())
342 && fi.getUniPort().equals(connectPoint.port())) {
343 return true;
344 }
345 }
346
347 return false;
348 }
349
Saurav Das2d3777a2020-08-07 18:48:51 -0700350 private class DeleteEapolInstallSub implements Runnable {
351 ConnectPoint cp;
352 Port uplinkPort;
353 SubscriberAndDeviceInformation sub;
354 private int attemptNumber;
355
356 DeleteEapolInstallSub(ConnectPoint cp, Port uplinkPort,
357 SubscriberAndDeviceInformation sub,
358 int attemptNumber) {
359 this.cp = cp;
360 this.uplinkPort = uplinkPort;
361 this.sub = sub;
362 this.attemptNumber = attemptNumber;
363 }
364
365 @Override
366 public void run() {
367 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
368 oltFlowService.processEapolFilteringObjectives(cp.deviceId(), cp.port(),
369 defaultBpId, filterFuture,
370 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
371 false);
372 filterFuture.thenAcceptAsync(filterStatus -> {
373 if (filterStatus == null) {
374 log.info("Default eapol flow deleted in attempt {} of {}"
375 + "... provisioning subscriber flows {}",
376 attemptNumber, eapolDeleteRetryMaxAttempts, cp);
Matteo Scandoloa4aaa972020-10-23 15:24:38 -0700377
378 // FIXME this is needed to prevent that default EAPOL flow removal and
379 // data plane flows install are received by the device at the same time
380 provisionExecutor.schedule(
381 () -> provisionUniTagList(cp, uplinkPort.number(), sub),
382 provisionDelay, TimeUnit.MILLISECONDS);
Saurav Das2d3777a2020-08-07 18:48:51 -0700383 } else {
384 if (attemptNumber <= eapolDeleteRetryMaxAttempts) {
385 log.warn("The filtering future failed {} for subscriber {}"
386 + "... retrying {} of {} attempts",
387 filterStatus, cp, attemptNumber, eapolDeleteRetryMaxAttempts);
388 retryExecutor.execute(
389 new DeleteEapolInstallSub(cp, uplinkPort, sub,
390 attemptNumber + 1));
391 } else {
392 log.error("The filtering future failed {} for subscriber {}"
393 + "after {} attempts. Subscriber provisioning failed",
394 filterStatus, cp, eapolDeleteRetryMaxAttempts);
395 sub.uniTagList().forEach(ut -> failedSubs.put(cp, ut));
396 }
397 }
398 });
399 }
400
401 }
402
alshabibb7a9e172016-01-13 11:23:53 -0800403 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000404 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700405 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000406
Saurav Daseae48de2019-06-19 13:26:15 -0700407 // Get the subscriber connected to this port from the local cache
408 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000409 DeviceId deviceId = connectPoint.deviceId();
410 PortNumber subscriberPortNo = connectPoint.port();
411
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800412 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000413 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000414 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000415 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800416 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800417 }
418
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100419 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000420 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100421 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000422 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100423 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800424 }
425
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000426 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100427
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000428 if (multicastServiceName.equals(uniTag.getServiceName())) {
429 continue;
430 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000431
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000432 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800433
Saurav Das9da7d522020-03-23 19:14:35 -0700434 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000435 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
436 uniTag.getUpstreamBandwidthProfile(),
437 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100438
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000439 Port port = deviceService.getPort(deviceId, subscriberPortNo);
440 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700441 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000442 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
443 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
444 } else {
445 log.debug("Port {} is no longer enabled or it's unavailable. Not "
446 + "reprogramming default eapol flow", connectPoint);
447 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100448 }
Amit Ghosh31939522018-08-16 13:28:21 +0100449 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800450 }
451
Gamze Abakaf59c0912019-04-19 08:24:28 +0000452
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000453 @Override
454 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
455 Optional<VlanId> cTag, Optional<Integer> tpId) {
456
457 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
458 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000459
Amit Ghosh31939522018-08-16 13:28:21 +0100460 // Check if we can find the connect point to which this subscriber is connected
461 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
462 if (subsPort == null) {
463 log.warn("ConnectPoint for {} not found", subscriberId);
464 return false;
465 }
466
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100467 if (!sTag.isPresent() && !cTag.isPresent()) {
468 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000469 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100470 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
471 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000472 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100473 return false;
474 }
475
Gamze Abakaf59c0912019-04-19 08:24:28 +0000476 //delete Eapol authentication flow with default bandwidth
477 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000478 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000479 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
480 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
481 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000482 filterFuture.thenAcceptAsync(filterStatus -> {
483 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000484 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
485 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000486 }
487 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100488 return true;
489 } else {
490 log.warn("Provisioning failed for subscriber: {}", subscriberId);
491 return false;
492 }
Amit Ghosh31939522018-08-16 13:28:21 +0100493 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100494
alshabibe0559672016-02-21 14:49:51 -0800495 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000496 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
497 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100498 // Check if we can find the connect point to which this subscriber is connected
499 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
500 if (subsPort == null) {
501 log.warn("ConnectPoint for {} not found", subscriberId);
502 return false;
503 }
504
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100505 if (!sTag.isPresent() && !cTag.isPresent()) {
506 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000507 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100508 // Get the uplink port
509 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
510 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000511 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100512 return false;
513 }
514
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000515 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
516 if (!tagInfo.isPresent()) {
517 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
518 subsPort, cTag, sTag, tpId);
519 return false;
520 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000521
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000522 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100523 return true;
524 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000525 log.warn("Removing subscriber is not possible - please check the provided information" +
526 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100527 return false;
528 }
Amit Ghosh31939522018-08-16 13:28:21 +0100529 }
530
531 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000532 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800533 return programmedSubs.stream()
534 .collect(collectingAndThen(
535 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
536 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700537 }
538
539 @Override
Saurav Das2d3777a2020-08-07 18:48:51 -0700540 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getFailedSubs() {
541 return failedSubs.stream()
542 .collect(collectingAndThen(
543 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
544 ImmutableMap::copyOf));
545 }
546
547 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100548 public List<DeviceId> fetchOlts() {
549 // look through all the devices and find the ones that are OLTs as per Sadis
550 List<DeviceId> olts = new ArrayList<>();
551 Iterable<Device> devices = deviceService.getDevices();
552 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700553 if (getOltInfo(d) != null) {
554 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100555 olts.add(d.id());
556 }
557 }
558 return olts;
alshabibe0559672016-02-21 14:49:51 -0800559 }
560
Amit Ghosh31939522018-08-16 13:28:21 +0100561 /**
562 * Finds the connect point to which a subscriber is connected.
563 *
564 * @param id The id of the subscriber, this is the same ID as in Sadis
565 * @return Subscribers ConnectPoint if found else null
566 */
567 private ConnectPoint findSubscriberConnectPoint(String id) {
568
569 Iterable<Device> devices = deviceService.getDevices();
570 for (Device d : devices) {
571 for (Port p : deviceService.getPorts(d.id())) {
572 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
573 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
574 log.debug("Found on device {} port {}", d.id(), p.number());
575 return new ConnectPoint(d.id(), p.number());
576 }
577 }
578 }
579 return null;
580 }
581
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000582 /**
583 * Gets the context of the bandwidth profile information for the given parameter.
584 *
585 * @param bandwidthProfile the bandwidth profile id
586 * @return the context of the bandwidth profile information
587 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000588 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
589 if (bandwidthProfile == null) {
590 return null;
591 }
592 return bpService.get(bandwidthProfile);
593 }
594
Gamze Abaka838d8142019-02-21 07:06:55 +0000595 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000596 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000597 *
598 * @param deviceId the device identifier
599 * @param uplink uplink port of the OLT
600 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000601 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000602 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000603 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000604 PortNumber subscriberPort, UniTagInformation uniTag) {
605
606 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800607
608 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
609 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
610
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000611 VlanId deviceVlan = uniTag.getPonSTag();
612 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000613
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000614 MeterId upstreamMeterId = oltMeterService
615 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
616 MeterId downstreamMeterId = oltMeterService
617 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000618
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000619 ForwardingObjective.Builder upFwd =
620 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
621 ForwardingObjective.Builder downFwd =
622 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
623
Andrea Campanella7c49b792020-05-11 11:36:53 +0200624 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
625 upstreamMeterId, uniTag, false, true);
626 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
627 upstreamMeterId, uniTag, false, true);
alshabibbf23a1f2016-01-14 17:27:11 -0800628
alshabib4ceaed32016-03-03 18:00:58 -0800629 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
630 @Override
631 public void onSuccess(Objective objective) {
632 upFuture.complete(null);
633 }
alshabibbf23a1f2016-01-14 17:27:11 -0800634
alshabib4ceaed32016-03-03 18:00:58 -0800635 @Override
636 public void onError(Objective objective, ObjectiveError error) {
637 upFuture.complete(error);
638 }
639 }));
640
641 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
642 @Override
643 public void onSuccess(Objective objective) {
644 downFuture.complete(null);
645 }
646
647 @Override
648 public void onError(Objective objective, ObjectiveError error) {
649 downFuture.complete(error);
650 }
651 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800652
653 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000654 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800655 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000656 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
657 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
658 uniTag.getTechnologyProfileId(), subscriberPort);
659 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800660 } else if (downStatus != null) {
661 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000662 "on port {} failed downstream uninstallation: {}",
663 subscriberVlan, deviceId, subscriberPort, downStatus);
664 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800665 } else if (upStatus != null) {
666 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000667 "on port {} failed upstream uninstallation: {}",
668 subscriberVlan, deviceId, subscriberPort, upStatus);
669 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800670 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000671 Port port = deviceService.getPort(deviceId, subscriberPort);
672 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
673 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800674 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700675 }
676
Gamze Abaka838d8142019-02-21 07:06:55 +0000677 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000678 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000679 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000680 * @param connectPoint the connection point of the subscriber
681 * @param uplinkPort uplink port of the OLT (the nni port)
682 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000683 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000684 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
685 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000686
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700687 log.debug("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000688
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000689 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
690 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
691 return;
692 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000693
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000694 DeviceId deviceId = connectPoint.deviceId();
695 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000696
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000697 for (UniTagInformation uniTag : sub.uniTagList()) {
698 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
699 }
700 }
alshabib3ea82642016-01-12 18:06:53 -0800701
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000702 /**
703 * Finds the uni tag information and provisions the found information.
704 * If the uni tag information is not found, returns
705 *
706 * @param deviceId the access device id
707 * @param uplinkPort the nni port
708 * @param subscriberPort the uni port
709 * @param innerVlan the pon c tag
710 * @param outerVlan the pon s tag
711 * @param tpId the technology profile id
712 */
713 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
714 PortNumber subscriberPort,
715 VlanId innerVlan,
716 VlanId outerVlan,
717 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700718
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000719 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
720 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
721 if (!gotTagInformation.isPresent()) {
722 return;
723 }
724 UniTagInformation tagInformation = gotTagInformation.get();
725 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
726 }
alshabib3ea82642016-01-12 18:06:53 -0800727
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000728 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800729 if (add) {
730 programmedSubs.put(connectPoint, tagInformation);
731 } else {
732 programmedSubs.remove(connectPoint, tagInformation);
733 }
Jonathan Harte533a422015-10-20 17:31:24 -0700734 }
735
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000736 /**
737 * Installs a uni tag information flow.
738 *
739 * @param deviceId the access device id
740 * @param uplinkPort the nni port
741 * @param subscriberPort the uni port
742 * @param tagInfo the uni tag information
743 */
744 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
745 UniTagInformation tagInfo) {
746
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700747 log.debug("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000748
749 Port port = deviceService.getPort(deviceId, subscriberPort);
750
751 if (multicastServiceName.equals(tagInfo.getServiceName())) {
752 // IGMP flows are taken care of along with VOD service
753 // Please note that for each service, Subscriber Registered event will be sent
754 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
755 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
756 tagInfo.getTechnologyProfileId()));
757 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000758 }
759
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000760 BandwidthProfileInformation upstreamBpInfo =
761 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
762 BandwidthProfileInformation downstreamBpInfo =
763 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700764 if (upstreamBpInfo == null) {
765 log.warn("No meter installed since no Upstream BW Profile definition found for "
766 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
767 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
768 tagInfo.getTechnologyProfileId(), deviceId,
769 subscriberPort);
770 return;
771 }
772 if (downstreamBpInfo == null) {
773 log.warn("No meter installed since no Downstream BW Profile definition found for "
774 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
775 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
776 tagInfo.getTechnologyProfileId(), deviceId,
777 subscriberPort);
778 return;
779 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000780
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700781 // check for meterIds for the upstream and downstream bandwidth profiles
782 MeterId upMeterId = oltMeterService
783 .getMeterIdFromBpMapping(deviceId, upstreamBpInfo.id());
784 MeterId downMeterId = oltMeterService
785 .getMeterIdFromBpMapping(deviceId, downstreamBpInfo.id());
786 SubscriberFlowInfo fi = new SubscriberFlowInfo(deviceId, uplinkPort, subscriberPort,
787 tagInfo, downMeterId, upMeterId,
788 downstreamBpInfo.id(), upstreamBpInfo.id());
789
790 if (upMeterId != null && downMeterId != null) {
791 log.debug("Meters are existing for upstream {} and downstream {}",
792 upstreamBpInfo.id(), downstreamBpInfo.id());
793 handleSubFlowsWithMeters(fi);
794 } else {
795 log.debug("Adding {} to pending subs", fi);
796 // one or both meters are not ready. It's possible they are in the process of being
797 // created for other subscribers that share the same bandwidth profile.
798 pendingSubscribers.add(fi);
799
800 // queue up the meters to be created
801 if (upMeterId == null) {
802 log.debug("Missing meter for upstream {}", upstreamBpInfo.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200803 checkAndCreateDevMeter(deviceId, upstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700804 }
805 if (downMeterId == null) {
806 log.debug("Missing meter for downstream {}", downstreamBpInfo.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200807 checkAndCreateDevMeter(deviceId, downstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700808 }
809 }
810 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200811 private void checkAndCreateDevMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
Andrea Campanellad1e26642020-10-23 12:08:32 +0200812 //If false the meter is already being installed, skipping installation
813 if (!oltMeterService.checkAndAddPendingMeter(deviceId, bwpInfo)) {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700814 return;
815 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200816 createMeter(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700817 }
818
Andrea Campanella600d2e22020-06-22 11:00:31 +0200819 private void createMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
820 log.debug("Creating Meter with {} on {} for subscriber", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700821 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200822
Andrea Campanella600d2e22020-06-22 11:00:31 +0200823 MeterId meterId = oltMeterService.createMeter(deviceId, bwpInfo,
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700824 meterFuture);
825
826 meterFuture.thenAcceptAsync(result -> {
827 // iterate through the subscribers on hold
828 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
829 while (subsIterator.hasNext()) {
830 SubscriberFlowInfo fi = subsIterator.next();
831 if (result == null) {
832 // meter install sent to device
Andrea Campanella600d2e22020-06-22 11:00:31 +0200833 log.debug("Meter {} installed for bw {} on {}", meterId, bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700834
835 MeterId upMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200836 .getMeterIdFromBpMapping(deviceId, fi.getUpBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700837 MeterId downMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200838 .getMeterIdFromBpMapping(deviceId, fi.getDownBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700839 if (upMeterId != null && downMeterId != null) {
840 log.debug("Provisioning subscriber after meter {}" +
841 "installation and both meters are present " +
842 "upstream {} and downstream {}",
843 meterId, upMeterId, downMeterId);
844 // put in the meterIds because when fi was first
845 // created there may or may not have been a meterId
846 // depending on whether the meter was created or
847 // not at that time.
848 fi.setUpMeterId(upMeterId);
849 fi.setDownMeterId(downMeterId);
850 handleSubFlowsWithMeters(fi);
851 subsIterator.remove();
852 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200853 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700854 } else {
855 // meter install failed
856 log.error("Addition of subscriber {} failed due to meter " +
857 "{} with result {}", fi, meterId, result);
858 subsIterator.remove();
Andrea Campanella600d2e22020-06-22 11:00:31 +0200859 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700860 }
861 }
862 });
863 }
864 /**
865 * Add subscriber flows given meter information for both upstream and
866 * downstream directions.
867 *
868 * @param subscriberFlowInfo relevant information for subscriber
869 */
870 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
871 log.debug("Provisioning subscriber flows based on {}", subscriberFlowInfo);
872 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000873 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
874 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000875
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700876 ForwardingObjective.Builder upFwd =
877 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
878 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
879 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
880 @Override
881 public void onSuccess(Objective objective) {
882 log.debug("Upstream flow installed successfully {}", subscriberFlowInfo);
883 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000884 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000885
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700886 @Override
887 public void onError(Objective objective, ObjectiveError error) {
888 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000889 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700890 }));
891
892 ForwardingObjective.Builder downFwd =
893 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
894 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
895 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
896 @Override
897 public void onSuccess(Objective objective) {
898 log.debug("Downstream flow installed successfully {}", subscriberFlowInfo);
899 downFuture.complete(null);
900 }
901
902 @Override
903 public void onError(Objective objective, ObjectiveError error) {
904 downFuture.complete(error);
905 }
906 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000907
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100908 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000909 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100910 if (downStatus != null) {
911 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000912 "on port {} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700913 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
914 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000915 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100916 } else if (upStatus != null) {
917 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000918 "on port {} failed upstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700919 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
920 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000921 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000922 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700923 log.debug("Upstream and downstream data plane flows are installed successfully " +
924 "for {}", subscriberFlowInfo);
925 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
926 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000927 tagInfo.getUpstreamBandwidthProfile(),
928 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700929 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
930 subscriberFlowInfo.getUniPort(),
931 subscriberFlowInfo.getUpId(),
932 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000933
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700934 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
935 subscriberFlowInfo.getUniPort(),
936 subscriberFlowInfo.getUpId(),
937 tagInfo, true, true);
938 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
939 subscriberFlowInfo.getUniPort()),
940 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100941 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700942 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
943 deviceService.getPort(subscriberFlowInfo.getDevId(),
944 subscriberFlowInfo.getUniPort()),
945 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
946 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100947 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100948 }
949
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000950 /**
951 * Checks the subscriber uni tag list and find the uni tag information.
952 * using the pon c tag, pon s tag and the technology profile id
953 * May return Optional<null>
954 *
955 * @param cp the connection point of the subscriber
956 * @param innerVlan pon c tag
957 * @param outerVlan pon s tag
958 * @param tpId the technology profile id
959 * @return the found uni tag information
960 */
961 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
962 int tpId) {
963 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
964 outerVlan, tpId);
965 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000966 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000967 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
968 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000969 }
970
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000971 List<UniTagInformation> uniTagList = subInfo.uniTagList();
972 if (uniTagList == null) {
973 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
974 return Optional.empty();
975 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100976
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000977 UniTagInformation service = null;
978 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
979 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
980 && tpId == tagInfo.getTechnologyProfileId()) {
981 service = tagInfo;
982 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700983 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000984 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000985
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000986 if (service == null) {
987 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
988 innerVlan, outerVlan, tpId);
989 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000990 }
991
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000992 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100993 }
994
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100995 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700996 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
997 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100998 *
999 * @param dev Device to look for
1000 */
Jonathan Hart403372d2018-08-22 11:44:13 -07001001 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001002 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001003 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001004 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001005
1006 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -07001007 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001008 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001009 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001010 continue;
1011 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001012 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +01001013 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
1014 log.info("Creating Eapol for the uni {}", p);
1015 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1016 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
1017 } else {
1018 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
1019 "provisioned, not installing default", p.number(), dev.id());
1020 }
Jonathan Hart403372d2018-08-22 11:44:13 -07001021 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001022 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001023 }
1024 }
1025 }
1026 }
1027
Jonathan Hart403372d2018-08-22 11:44:13 -07001028
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001029 /**
1030 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001031 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001032 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1033 * this logic needs to be changed
1034 *
1035 * @param dev Device to look for
1036 * @return The uplink Port of the OLT
1037 */
1038 private Port getUplinkPort(Device dev) {
1039 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001040 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -07001041 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001042 if (deviceInfo == null) {
1043 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001044 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -07001045 return null;
1046 }
1047 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +05301048 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001049 .filter(port -> isNniPort(port) ||
1050 (port.number().toLong() == deviceInfo.uplinkPort()))
1051 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +05301052 if (optionalPort.isPresent()) {
1053 log.trace("getUplinkPort: Found port {}", optionalPort.get());
1054 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001055 }
1056
Saurav Daseae48de2019-06-19 13:26:15 -07001057 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001058 return null;
1059 }
1060
1061 /**
1062 * Return the subscriber on a port.
1063 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001064 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001065 * @return subscriber if found else null
1066 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001067 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1068 Port port = deviceService.getPort(cp);
1069 checkNotNull(port, "Invalid connect point");
1070 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001071 return subsService.get(portName);
1072 }
1073
Gamze Abakaad329652018-12-20 10:12:21 +00001074 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001075 * Checks whether the given port of the device is a uni port or not.
1076 *
1077 * @param d the access device
1078 * @param p the port of the device
1079 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +00001080 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001081 private boolean isUniPort(Device d, Port p) {
1082 Port ulPort = getUplinkPort(d);
1083 if (ulPort != null) {
1084 return (ulPort.number().toLong() != p.number().toLong());
1085 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +05301086 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
1087 //checks whether the port name starts with nni- which is the signature of an NNI Port
1088 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
1089 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
1090 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
1091 return false;
1092 }
1093 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001094 }
1095
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001096 /**
1097 * Gets the given device details from SADIS.
1098 * If the device is not found, returns null
1099 *
1100 * @param dev the access device
1101 * @return the olt information
1102 */
Jonathan Hart4c538002018-08-23 10:11:54 -07001103 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1104 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001105 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001106 }
1107
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001108 // Custom-built function, when the device is not available we need a fallback mechanism
1109 private boolean isLocalLeader(DeviceId deviceId) {
1110 if (!mastershipService.isLocalMaster(deviceId)) {
1111 // When the device is available we just check the mastership
1112 if (deviceService.isAvailable(deviceId)) {
1113 return false;
1114 }
1115 // Fallback with Leadership service - device id is used as topic
1116 NodeId leader = leadershipService.runForLeadership(
1117 deviceId.toString()).leaderNodeId();
1118 // Verify if this node is the leader
1119 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001120 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001121 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001122 }
1123
kdarapuaa5da252020-04-10 15:58:05 +05301124 private boolean isNniPort(Port port) {
1125 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1126 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1127 }
1128 return false;
1129 }
1130
alshabibf0e7e702015-05-30 18:22:36 -07001131 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001132 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1133
alshabibf0e7e702015-05-30 18:22:36 -07001134 @Override
1135 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001136 eventExecutor.execute(() -> {
1137 DeviceId devId = event.subject().id();
1138 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001139 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001140 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001141
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001142 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1143 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1144 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001145 return;
1146 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001147
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001148 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001149 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001150 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1151 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1152 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001153 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001154 // Since no mastership of the device is present upon disconnection
1155 // the method in the FlowRuleManager only empties the local copy
1156 // of the DeviceFlowTable thus this method needs to get called
1157 // on every instance, see how it's done in the InternalDeviceListener
1158 // in FlowRuleManager: no mastership check for purgeOnDisconnection
Andrea Campanella3f34c992020-07-15 10:54:10 +02001159 handleDeviceDisconnection(dev, false, false);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001160 return;
1161 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001162 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001163 return;
1164 }
1165
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001166 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
1167
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001168 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001169 // it's possible that we got an event for a previously
1170 // programmed OLT that is no longer available in SADIS
1171 // we let such events go through
1172 if (!programmedDevices.contains(devId)) {
1173 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001174 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001175 return;
1176 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001177 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001178
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001179 switch (event.type()) {
1180 //TODO: Port handling and bookkeeping should be improved once
1181 // olt firmware handles correct behaviour.
1182 case PORT_ADDED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001183 if (!deviceService.isAvailable(devId)) {
1184 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1185 return;
1186 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001187 if (isUniPort(dev, port)) {
1188 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001189
1190 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1191 log.info("eapol will be sent for port added {}", port);
1192 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1193 null,
Andrea Campanella3f34c992020-07-15 10:54:10 +02001194 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1195 true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001196 }
1197 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001198 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1199 if (deviceInfo != null) {
1200 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1201 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001202 }
1203 break;
1204 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001205 if (isUniPort(dev, port)) {
Andrea Campanellacf0e3052020-08-27 11:05:39 +02001206 // if no subscriber is provisioned we need to remove the default EAPOL
1207 // if a subscriber was provisioned the default EAPOL will not be there and we can skip.
1208 // The EAPOL with subscriber tag will be removed by removeSubscriber call.
1209 Collection<? extends UniTagInformation> uniTagInformationSet =
1210 programmedSubs.get(new ConnectPoint(port.element().id(), port.number())).value();
1211 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
1212 log.info("No subscriber provisioned on port {} in PORT_REMOVED event, " +
1213 "removing default EAPOL flow", port);
1214 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1215 null,
1216 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1217 false);
1218 } else {
1219 removeSubscriber(new ConnectPoint(devId, port.number()));
1220 }
Andy Bavier160e8682019-05-07 18:32:22 -07001221
Gamze Abaka838d8142019-02-21 07:06:55 +00001222 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001223 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001224 break;
1225 case PORT_UPDATED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001226 if (!deviceService.isAvailable(devId)) {
1227 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1228 return;
1229 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001230 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001231 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1232 if (deviceInfo != null && port.isEnabled()) {
1233 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1234 port.number());
1235 oltFlowService.processNniFilteringObjectives(dev.id(),
1236 port.number(), true);
1237 }
1238 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001239 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001240 ConnectPoint cp = new ConnectPoint(devId, port.number());
1241 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001242 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001243 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001244 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001245 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001246 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001247 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001248 null,
1249 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1250 port.isEnabled());
1251 }
1252 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001253 log.info("eapol will be {} for dev/port updated {}/{}",
1254 (port.isEnabled()) ? "added" : "removed",
1255 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001256 uniTagInformationSet.forEach(uniTag ->
1257 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1258 uniTag.getUpstreamBandwidthProfile(), null,
1259 uniTag.getPonCTag(), port.isEnabled()));
1260 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001261 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001262 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001263 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001264 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001265 }
alshabibbb83aa22016-02-10 15:08:23 -08001266 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001267 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001268 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001269 break;
1270 case DEVICE_REMOVED:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001271 handleDeviceDisconnection(dev, true, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001272 break;
1273 case DEVICE_AVAILABILITY_CHANGED:
1274 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001275 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001276 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001277 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301278 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001279 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001280 + "flushing all state for dev:{}", devId);
Andrea Campanella3f34c992020-07-15 10:54:10 +02001281 handleDeviceDisconnection(dev, true, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001282 } else {
1283 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001284 + "assuming temporary disconnection, "
1285 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301286 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001287 }
1288 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001289 default:
Andrea Campanella3f34c992020-07-15 10:54:10 +02001290 log.debug("Not handling event {}", event);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001291 return;
1292 }
1293 });
alshabibf0e7e702015-05-30 18:22:36 -07001294 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001295
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001296 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1297 deviceService.getPorts(device.id()).stream()
1298 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1299 .filter(p -> isUniPort(device, p))
1300 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1301 }
1302
Andrea Campanella3f34c992020-07-15 10:54:10 +02001303 private void handleDeviceDisconnection(Device device, boolean sendDisconnectedEvent, boolean sendUniEvent) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001304 programmedDevices.remove(device.id());
1305 removeAllSubscribers(device.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +02001306 //Handle case where OLT disconnects during subscriber provisioning
1307 pendingSubscribers.removeIf(fi -> fi.getDevId().equals(device.id()));
1308 oltFlowService.clearDeviceState(device.id());
1309
1310 //Complete meter and flow purge
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301311 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001312 oltMeterService.clearMeters(device.id());
Andrea Campanella3f34c992020-07-15 10:54:10 +02001313 if (sendDisconnectedEvent) {
1314 post(new AccessDeviceEvent(
1315 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1316 null, null, null));
1317 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001318 if (sendUniEvent) {
1319 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001320 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001321 }
1322
1323 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1324 post(new AccessDeviceEvent(
1325 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1326 null, null, null));
1327 programmedDevices.add(dev.id());
1328 checkAndCreateDeviceFlows(dev);
1329 if (sendUniEvent) {
1330 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1331 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001332 }
Gamze Abakada282b42019-03-11 13:16:48 +00001333
1334 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001335 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1336 .filter(e -> e.getKey().deviceId().equals(deviceId))
1337 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001338
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001339 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001340 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001341
Gamze Abaka641fc072018-09-04 09:16:27 +00001342 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001343
1344 private class InternalClusterListener implements ClusterEventListener {
1345
1346 @Override
1347 public void event(ClusterEvent event) {
1348 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1349 hasher.addServer(event.subject().id());
1350 }
1351 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1352 hasher.removeServer(event.subject().id());
1353 }
1354 }
1355 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001356
Hardik Windlass395ff372019-06-13 05:16:00 +00001357}