blob: fe2675e23681ab39c905c725790045a7ea3a3f59 [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;
32import static org.slf4j.LoggerFactory.getLogger;
33
34import java.util.ArrayList;
35import java.util.Collection;
36import java.util.Dictionary;
Andrea Campanella0c3309d2020-05-29 01:51:18 -070037import java.util.Iterator;
Saurav Das9da7d522020-03-23 19:14:35 -070038import java.util.List;
39import java.util.Map;
40import java.util.Optional;
41import java.util.Properties;
42import java.util.Set;
43import java.util.concurrent.CompletableFuture;
44import java.util.concurrent.ExecutorService;
45import java.util.concurrent.Executors;
46
alshabibf0e7e702015-05-30 18:22:36 -070047import org.onlab.packet.VlanId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080048import org.onlab.util.KryoNamespace;
Gamze Abaka1f62dd92020-05-07 08:58:13 +000049import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080050import org.onosproject.cluster.ClusterEvent;
51import org.onosproject.cluster.ClusterEventListener;
52import org.onosproject.cluster.ClusterService;
53import org.onosproject.cluster.ControllerNode;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020054import org.onosproject.cluster.LeadershipService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080055import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070056import org.onosproject.core.ApplicationId;
57import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080058import org.onosproject.event.AbstractListenerManager;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020059import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010060import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070061import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010062import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070063import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080064import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070065import org.onosproject.net.PortNumber;
66import org.onosproject.net.device.DeviceEvent;
67import org.onosproject.net.device.DeviceListener;
68import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053069import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070070import org.onosproject.net.flowobjective.FlowObjectiveService;
71import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080072import org.onosproject.net.flowobjective.Objective;
73import org.onosproject.net.flowobjective.ObjectiveContext;
74import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070075import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080076import org.onosproject.store.serializers.KryoNamespaces;
77import org.onosproject.store.service.ConsistentMultimap;
78import org.onosproject.store.service.Serializer;
79import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070080import org.opencord.olt.AccessDeviceEvent;
81import org.opencord.olt.AccessDeviceListener;
82import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010083import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000084import org.opencord.olt.internalapi.AccessDeviceFlowService;
85import org.opencord.olt.internalapi.AccessDeviceMeterService;
Andrea Campanella3ce4d282020-06-09 13:46:58 +020086import org.opencord.olt.internalapi.DeviceBandwidthProfile;
Gamze Abaka641fc072018-09-04 09:16:27 +000087import org.opencord.sadis.BandwidthProfileInformation;
88import org.opencord.sadis.BaseInformationService;
89import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010090import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000091import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080092import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070093import org.osgi.service.component.annotations.Activate;
94import org.osgi.service.component.annotations.Component;
95import org.osgi.service.component.annotations.Deactivate;
96import org.osgi.service.component.annotations.Modified;
97import org.osgi.service.component.annotations.Reference;
98import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070099import org.slf4j.Logger;
100
Saurav Das9da7d522020-03-23 19:14:35 -0700101import com.google.common.collect.ImmutableMap;
102import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700103
104/**
Jonathan Harte533a422015-10-20 17:31:24 -0700105 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700106 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700107@Component(immediate = true,
108 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700109 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000110 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700111 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800112public class Olt
113 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
114 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800115 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800116
Gamze Abakada282b42019-03-11 13:16:48 +0000117 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000118 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800119
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800120 public static final int HASH_WEIGHT = 10;
121
alshabibf0e7e702015-05-30 18:22:36 -0700122 private final Logger log = getLogger(getClass());
123
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530124 private static final String NNI = "nni-";
125
Carmelo Casconeca931162019-07-15 18:22:24 -0700126 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700127 protected FlowObjectiveService flowObjectiveService;
128
Carmelo Casconeca931162019-07-15 18:22:24 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700130 protected DeviceService deviceService;
131
Carmelo Casconeca931162019-07-15 18:22:24 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700133 protected CoreService coreService;
134
Carmelo Casconeca931162019-07-15 18:22:24 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000136 protected SadisService sadisService;
137
Carmelo Casconeca931162019-07-15 18:22:24 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000139 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800140
Carmelo Casconeca931162019-07-15 18:22:24 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000142 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000143
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
145 protected StorageService storageService;
146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY)
148 protected ClusterService clusterService;
149
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530150 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200151 protected MastershipService mastershipService;
152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY)
154 protected LeadershipService leadershipService;
155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530157 protected FlowRuleService flowRuleService;
158
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000159 @Reference(cardinality = ReferenceCardinality.MANDATORY)
160 protected ComponentConfigService componentConfigService;
161
Carmelo Casconeca931162019-07-15 18:22:24 -0700162 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800163 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700164 **/
165 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000166
Carmelo Casconeca931162019-07-15 18:22:24 -0700167 /**
Gamze Abaka51a34e82020-05-08 13:03:14 +0000168 * Default multicast service name.
Carmelo Casconeca931162019-07-15 18:22:24 -0700169 **/
Gamze Abaka51a34e82020-05-08 13:03:14 +0000170 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Gamze Abaka33feef52019-02-27 08:16:47 +0000171
alshabibf0e7e702015-05-30 18:22:36 -0700172 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800173 private final ClusterEventListener clusterListener = new InternalClusterListener();
174
175 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700176
Gamze Abaka641fc072018-09-04 09:16:27 +0000177 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
178 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700179
Gamze Abaka641fc072018-09-04 09:16:27 +0000180 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000181 groupedThreads("onos/olt-service",
182 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100183
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700184 protected ExecutorService eventExecutor;
185
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800186 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800187
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700188 private Set<SubscriberFlowInfo> pendingSubscribers;
189
alshabibf0e7e702015-05-30 18:22:36 -0700190 @Activate
alshabibe0559672016-02-21 14:49:51 -0800191 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000192 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
193 "events-%d", log));
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700194
alshabibe0559672016-02-21 14:49:51 -0800195 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000196 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000197 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800198
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800199 KryoNamespace serializer = KryoNamespace.newBuilder()
200 .register(KryoNamespaces.API)
201 .register(UniTagInformation.class)
202 .build();
203
204 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
205 .withName("volt-programmed-subs")
206 .withSerializer(Serializer.using(serializer))
207 .withApplicationId(appId)
208 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700209
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700210 pendingSubscribers = Sets.newConcurrentHashSet();
alshabib8e4fd2f2016-01-12 15:55:53 -0800211 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
212
Gamze Abaka641fc072018-09-04 09:16:27 +0000213 subsService = sadisService.getSubscriberInfoService();
214 bpService = sadisService.getBandwidthProfileService();
215
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800216 List<NodeId> readyNodes = clusterService.getNodes().stream()
217 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
218 .map(ControllerNode::id)
219 .collect(toList());
220 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
221 clusterService.addListener(clusterListener);
222
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100223 // look for all provisioned devices in Sadis and create EAPOL flows for the
224 // UNI ports
225 Iterable<Device> devices = deviceService.getDevices();
226 for (Device d : devices) {
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200227 if (isLocalLeader(d.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800228 checkAndCreateDeviceFlows(d);
229 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100230 }
alshabib4ceaed32016-03-03 18:00:58 -0800231
alshabibba357492016-01-27 13:49:46 -0800232 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700233 log.info("Started with Application ID {}", appId.id());
234 }
235
236 @Deactivate
237 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000238 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800239 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800240 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700241 eventDispatcher.removeSink(AccessDeviceEvent.class);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700242 eventExecutor.shutdown();
alshabibf0e7e702015-05-30 18:22:36 -0700243 log.info("Stopped");
244 }
245
alshabibe0559672016-02-21 14:49:51 -0800246 @Modified
247 public void modified(ComponentContext context) {
248 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
249
250 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200251 String bpId = get(properties, DEFAULT_BP_ID);
252 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000253
Andrea Campanella971d5b92020-05-07 11:20:43 +0200254 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
255 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000256
257 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000258
alshabibe0559672016-02-21 14:49:51 -0800259 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000260 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200261 defaultBpId = DEFAULT_BP_ID_DEFAULT;
262 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800263 }
264 }
265
alshabib32232c82016-02-25 17:57:24 -0500266 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000267 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200268 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000269 DeviceId deviceId = connectPoint.deviceId();
270 PortNumber subscriberPortNo = connectPoint.port();
271
272 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000273 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000274
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100275 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000276 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100277 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000278 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100279 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100280 }
Jonathan Harte533a422015-10-20 17:31:24 -0700281
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100282 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000283 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100284 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000285 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100286 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700287 }
288
Gamze Abaka838d8142019-02-21 07:06:55 +0000289 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000290 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000291 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000292 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
293 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
294 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000295 filterFuture.thenAcceptAsync(filterStatus -> {
296 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000297 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200298 } else {
299 log.error("The filtering future did not complete properly {} " +
300 "subscriber on {} is not provisioned", filterStatus, connectPoint);
Gamze Abaka33feef52019-02-27 08:16:47 +0000301 }
302 });
Amit Ghosh31939522018-08-16 13:28:21 +0100303 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800304 }
305
306 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000307 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700308 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000309
Saurav Daseae48de2019-06-19 13:26:15 -0700310 // Get the subscriber connected to this port from the local cache
311 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000312 DeviceId deviceId = connectPoint.deviceId();
313 PortNumber subscriberPortNo = connectPoint.port();
314
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800315 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000316 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000317 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000318 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800319 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800320 }
321
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100322 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000323 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100324 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000325 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100326 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800327 }
328
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000329 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100330
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000331 if (multicastServiceName.equals(uniTag.getServiceName())) {
332 continue;
333 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000334
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000335 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800336
Saurav Das9da7d522020-03-23 19:14:35 -0700337 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000338 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
339 uniTag.getUpstreamBandwidthProfile(),
340 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100341
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000342 Port port = deviceService.getPort(deviceId, subscriberPortNo);
343 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700344 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000345 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
346 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
347 } else {
348 log.debug("Port {} is no longer enabled or it's unavailable. Not "
349 + "reprogramming default eapol flow", connectPoint);
350 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100351 }
Amit Ghosh31939522018-08-16 13:28:21 +0100352 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800353 }
354
Gamze Abakaf59c0912019-04-19 08:24:28 +0000355
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000356 @Override
357 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
358 Optional<VlanId> cTag, Optional<Integer> tpId) {
359
360 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
361 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000362
Amit Ghosh31939522018-08-16 13:28:21 +0100363 // Check if we can find the connect point to which this subscriber is connected
364 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
365 if (subsPort == null) {
366 log.warn("ConnectPoint for {} not found", subscriberId);
367 return false;
368 }
369
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100370 if (!sTag.isPresent() && !cTag.isPresent()) {
371 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000372 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100373 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
374 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000375 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100376 return false;
377 }
378
Gamze Abakaf59c0912019-04-19 08:24:28 +0000379 //delete Eapol authentication flow with default bandwidth
380 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000381 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000382 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
383 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
384 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000385 filterFuture.thenAcceptAsync(filterStatus -> {
386 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000387 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
388 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000389 }
390 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100391 return true;
392 } else {
393 log.warn("Provisioning failed for subscriber: {}", subscriberId);
394 return false;
395 }
Amit Ghosh31939522018-08-16 13:28:21 +0100396 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100397
alshabibe0559672016-02-21 14:49:51 -0800398 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000399 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
400 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100401 // Check if we can find the connect point to which this subscriber is connected
402 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
403 if (subsPort == null) {
404 log.warn("ConnectPoint for {} not found", subscriberId);
405 return false;
406 }
407
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100408 if (!sTag.isPresent() && !cTag.isPresent()) {
409 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000410 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100411 // Get the uplink port
412 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
413 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000414 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100415 return false;
416 }
417
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000418 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
419 if (!tagInfo.isPresent()) {
420 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
421 subsPort, cTag, sTag, tpId);
422 return false;
423 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000424
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000425 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100426 return true;
427 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000428 log.warn("Removing subscriber is not possible - please check the provided information" +
429 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100430 return false;
431 }
Amit Ghosh31939522018-08-16 13:28:21 +0100432 }
433
434 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000435 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800436 return programmedSubs.stream()
437 .collect(collectingAndThen(
438 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
439 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700440 }
441
442 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100443 public List<DeviceId> fetchOlts() {
444 // look through all the devices and find the ones that are OLTs as per Sadis
445 List<DeviceId> olts = new ArrayList<>();
446 Iterable<Device> devices = deviceService.getDevices();
447 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700448 if (getOltInfo(d) != null) {
449 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100450 olts.add(d.id());
451 }
452 }
453 return olts;
alshabibe0559672016-02-21 14:49:51 -0800454 }
455
Amit Ghosh31939522018-08-16 13:28:21 +0100456 /**
457 * Finds the connect point to which a subscriber is connected.
458 *
459 * @param id The id of the subscriber, this is the same ID as in Sadis
460 * @return Subscribers ConnectPoint if found else null
461 */
462 private ConnectPoint findSubscriberConnectPoint(String id) {
463
464 Iterable<Device> devices = deviceService.getDevices();
465 for (Device d : devices) {
466 for (Port p : deviceService.getPorts(d.id())) {
467 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
468 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
469 log.debug("Found on device {} port {}", d.id(), p.number());
470 return new ConnectPoint(d.id(), p.number());
471 }
472 }
473 }
474 return null;
475 }
476
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000477 /**
478 * Gets the context of the bandwidth profile information for the given parameter.
479 *
480 * @param bandwidthProfile the bandwidth profile id
481 * @return the context of the bandwidth profile information
482 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000483 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
484 if (bandwidthProfile == null) {
485 return null;
486 }
487 return bpService.get(bandwidthProfile);
488 }
489
Gamze Abaka838d8142019-02-21 07:06:55 +0000490 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000491 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000492 *
493 * @param deviceId the device identifier
494 * @param uplink uplink port of the OLT
495 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000496 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000497 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000498 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000499 PortNumber subscriberPort, UniTagInformation uniTag) {
500
501 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800502
503 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
504 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
505
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000506 VlanId deviceVlan = uniTag.getPonSTag();
507 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000508
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000509 MeterId upstreamMeterId = oltMeterService
510 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
511 MeterId downstreamMeterId = oltMeterService
512 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000513
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000514 ForwardingObjective.Builder upFwd =
515 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
516 ForwardingObjective.Builder downFwd =
517 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
518
Andrea Campanella7c49b792020-05-11 11:36:53 +0200519 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
520 upstreamMeterId, uniTag, false, true);
521 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
522 upstreamMeterId, uniTag, false, true);
alshabibbf23a1f2016-01-14 17:27:11 -0800523
alshabib4ceaed32016-03-03 18:00:58 -0800524 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
525 @Override
526 public void onSuccess(Objective objective) {
527 upFuture.complete(null);
528 }
alshabibbf23a1f2016-01-14 17:27:11 -0800529
alshabib4ceaed32016-03-03 18:00:58 -0800530 @Override
531 public void onError(Objective objective, ObjectiveError error) {
532 upFuture.complete(error);
533 }
534 }));
535
536 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
537 @Override
538 public void onSuccess(Objective objective) {
539 downFuture.complete(null);
540 }
541
542 @Override
543 public void onError(Objective objective, ObjectiveError error) {
544 downFuture.complete(error);
545 }
546 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800547
548 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000549 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800550 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000551 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
552 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
553 uniTag.getTechnologyProfileId(), subscriberPort);
554 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800555 } else if (downStatus != null) {
556 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000557 "on port {} failed downstream uninstallation: {}",
558 subscriberVlan, deviceId, subscriberPort, downStatus);
559 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800560 } else if (upStatus != null) {
561 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000562 "on port {} failed upstream uninstallation: {}",
563 subscriberVlan, deviceId, subscriberPort, upStatus);
564 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800565 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000566 Port port = deviceService.getPort(deviceId, subscriberPort);
567 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
568 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800569 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700570 }
571
Gamze Abaka838d8142019-02-21 07:06:55 +0000572 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000573 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000574 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000575 * @param connectPoint the connection point of the subscriber
576 * @param uplinkPort uplink port of the OLT (the nni port)
577 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000578 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000579 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
580 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000581
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700582 log.debug("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000583
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000584 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
585 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
586 return;
587 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000588
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000589 DeviceId deviceId = connectPoint.deviceId();
590 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000591
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000592 for (UniTagInformation uniTag : sub.uniTagList()) {
593 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
594 }
595 }
alshabib3ea82642016-01-12 18:06:53 -0800596
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000597 /**
598 * Finds the uni tag information and provisions the found information.
599 * If the uni tag information is not found, returns
600 *
601 * @param deviceId the access device id
602 * @param uplinkPort the nni port
603 * @param subscriberPort the uni port
604 * @param innerVlan the pon c tag
605 * @param outerVlan the pon s tag
606 * @param tpId the technology profile id
607 */
608 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
609 PortNumber subscriberPort,
610 VlanId innerVlan,
611 VlanId outerVlan,
612 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700613
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000614 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
615 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
616 if (!gotTagInformation.isPresent()) {
617 return;
618 }
619 UniTagInformation tagInformation = gotTagInformation.get();
620 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
621 }
alshabib3ea82642016-01-12 18:06:53 -0800622
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000623 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800624 if (add) {
625 programmedSubs.put(connectPoint, tagInformation);
626 } else {
627 programmedSubs.remove(connectPoint, tagInformation);
628 }
Jonathan Harte533a422015-10-20 17:31:24 -0700629 }
630
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000631 /**
632 * Installs a uni tag information flow.
633 *
634 * @param deviceId the access device id
635 * @param uplinkPort the nni port
636 * @param subscriberPort the uni port
637 * @param tagInfo the uni tag information
638 */
639 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
640 UniTagInformation tagInfo) {
641
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700642 log.debug("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000643
644 Port port = deviceService.getPort(deviceId, subscriberPort);
645
646 if (multicastServiceName.equals(tagInfo.getServiceName())) {
647 // IGMP flows are taken care of along with VOD service
648 // Please note that for each service, Subscriber Registered event will be sent
649 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
650 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
651 tagInfo.getTechnologyProfileId()));
652 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000653 }
654
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000655 BandwidthProfileInformation upstreamBpInfo =
656 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
657 BandwidthProfileInformation downstreamBpInfo =
658 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700659 if (upstreamBpInfo == null) {
660 log.warn("No meter installed since no Upstream BW Profile definition found for "
661 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
662 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
663 tagInfo.getTechnologyProfileId(), deviceId,
664 subscriberPort);
665 return;
666 }
667 if (downstreamBpInfo == null) {
668 log.warn("No meter installed since no Downstream BW Profile definition found for "
669 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
670 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
671 tagInfo.getTechnologyProfileId(), deviceId,
672 subscriberPort);
673 return;
674 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000675
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700676 // check for meterIds for the upstream and downstream bandwidth profiles
677 MeterId upMeterId = oltMeterService
678 .getMeterIdFromBpMapping(deviceId, upstreamBpInfo.id());
679 MeterId downMeterId = oltMeterService
680 .getMeterIdFromBpMapping(deviceId, downstreamBpInfo.id());
681 SubscriberFlowInfo fi = new SubscriberFlowInfo(deviceId, uplinkPort, subscriberPort,
682 tagInfo, downMeterId, upMeterId,
683 downstreamBpInfo.id(), upstreamBpInfo.id());
684
685 if (upMeterId != null && downMeterId != null) {
686 log.debug("Meters are existing for upstream {} and downstream {}",
687 upstreamBpInfo.id(), downstreamBpInfo.id());
688 handleSubFlowsWithMeters(fi);
689 } else {
690 log.debug("Adding {} to pending subs", fi);
691 // one or both meters are not ready. It's possible they are in the process of being
692 // created for other subscribers that share the same bandwidth profile.
693 pendingSubscribers.add(fi);
694
695 // queue up the meters to be created
696 if (upMeterId == null) {
697 log.debug("Missing meter for upstream {}", upstreamBpInfo.id());
698 checkAndCreateDevMeter(new DeviceBandwidthProfile(deviceId, upstreamBpInfo));
699 }
700 if (downMeterId == null) {
701 log.debug("Missing meter for downstream {}", downstreamBpInfo.id());
702 checkAndCreateDevMeter(new DeviceBandwidthProfile(deviceId, downstreamBpInfo));
703 }
704 }
705 }
706 private void checkAndCreateDevMeter(DeviceBandwidthProfile dm) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200707 if (oltMeterService.isMeterPending(dm)) {
708 log.debug("Meter is already pending {}", dm);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700709 return;
710 }
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200711 oltMeterService.addToPendingMeters(dm);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700712 createMeter(dm);
713 }
714
715 private void createMeter(DeviceBandwidthProfile dm) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200716 log.debug("Creating Meter {} from queue for subscriber", dm);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700717 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200718
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700719 MeterId meterId = oltMeterService.createMeter(dm.getDevId(), dm.getBwInfo(),
720 meterFuture);
721
722 meterFuture.thenAcceptAsync(result -> {
723 // iterate through the subscribers on hold
724 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
725 while (subsIterator.hasNext()) {
726 SubscriberFlowInfo fi = subsIterator.next();
727 if (result == null) {
728 // meter install sent to device
729 log.debug("Meter {} installed for bw {}", meterId, dm.getBwInfo());
730
731 MeterId upMeterId = oltMeterService
732 .getMeterIdFromBpMapping(dm.getDevId(), fi.getUpBpInfo());
733 MeterId downMeterId = oltMeterService
734 .getMeterIdFromBpMapping(dm.getDevId(), fi.getDownBpInfo());
735 if (upMeterId != null && downMeterId != null) {
736 log.debug("Provisioning subscriber after meter {}" +
737 "installation and both meters are present " +
738 "upstream {} and downstream {}",
739 meterId, upMeterId, downMeterId);
740 // put in the meterIds because when fi was first
741 // created there may or may not have been a meterId
742 // depending on whether the meter was created or
743 // not at that time.
744 fi.setUpMeterId(upMeterId);
745 fi.setDownMeterId(downMeterId);
746 handleSubFlowsWithMeters(fi);
747 subsIterator.remove();
748 }
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200749 oltMeterService.removeFromPendingMeters(new DeviceBandwidthProfile(dm.getDevId(), dm.getBwInfo()));
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700750 } else {
751 // meter install failed
752 log.error("Addition of subscriber {} failed due to meter " +
753 "{} with result {}", fi, meterId, result);
754 subsIterator.remove();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200755 oltMeterService.removeFromPendingMeters(new DeviceBandwidthProfile(dm.getDevId(), dm.getBwInfo()));
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700756 }
757 }
758 });
759 }
760 /**
761 * Add subscriber flows given meter information for both upstream and
762 * downstream directions.
763 *
764 * @param subscriberFlowInfo relevant information for subscriber
765 */
766 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
767 log.debug("Provisioning subscriber flows based on {}", subscriberFlowInfo);
768 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000769 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
770 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000771
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700772 ForwardingObjective.Builder upFwd =
773 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
774 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
775 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
776 @Override
777 public void onSuccess(Objective objective) {
778 log.debug("Upstream flow installed successfully {}", subscriberFlowInfo);
779 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000780 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000781
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700782 @Override
783 public void onError(Objective objective, ObjectiveError error) {
784 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000785 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700786 }));
787
788 ForwardingObjective.Builder downFwd =
789 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
790 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
791 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
792 @Override
793 public void onSuccess(Objective objective) {
794 log.debug("Downstream flow installed successfully {}", subscriberFlowInfo);
795 downFuture.complete(null);
796 }
797
798 @Override
799 public void onError(Objective objective, ObjectiveError error) {
800 downFuture.complete(error);
801 }
802 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000803
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100804 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000805 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100806 if (downStatus != null) {
807 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000808 "on port {} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700809 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
810 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000811 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100812 } else if (upStatus != null) {
813 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000814 "on port {} failed upstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700815 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
816 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000817 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000818 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700819 log.debug("Upstream and downstream data plane flows are installed successfully " +
820 "for {}", subscriberFlowInfo);
821 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
822 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000823 tagInfo.getUpstreamBandwidthProfile(),
824 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700825 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
826 subscriberFlowInfo.getUniPort(),
827 subscriberFlowInfo.getUpId(),
828 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000829
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700830 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
831 subscriberFlowInfo.getUniPort(),
832 subscriberFlowInfo.getUpId(),
833 tagInfo, true, true);
834 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
835 subscriberFlowInfo.getUniPort()),
836 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100837 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700838 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
839 deviceService.getPort(subscriberFlowInfo.getDevId(),
840 subscriberFlowInfo.getUniPort()),
841 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
842 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100843 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100844 }
845
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000846 /**
847 * Checks the subscriber uni tag list and find the uni tag information.
848 * using the pon c tag, pon s tag and the technology profile id
849 * May return Optional<null>
850 *
851 * @param cp the connection point of the subscriber
852 * @param innerVlan pon c tag
853 * @param outerVlan pon s tag
854 * @param tpId the technology profile id
855 * @return the found uni tag information
856 */
857 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
858 int tpId) {
859 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
860 outerVlan, tpId);
861 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000862 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000863 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
864 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000865 }
866
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000867 List<UniTagInformation> uniTagList = subInfo.uniTagList();
868 if (uniTagList == null) {
869 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
870 return Optional.empty();
871 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100872
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000873 UniTagInformation service = null;
874 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
875 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
876 && tpId == tagInfo.getTechnologyProfileId()) {
877 service = tagInfo;
878 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700879 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000880 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000881
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000882 if (service == null) {
883 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
884 innerVlan, outerVlan, tpId);
885 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000886 }
887
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000888 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100889 }
890
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100891 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700892 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
893 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100894 *
895 * @param dev Device to look for
896 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700897 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100898 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000899 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000900 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100901
902 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700903 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100904 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800905 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000906 continue;
907 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100908 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100909 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
910 log.info("Creating Eapol for the uni {}", p);
911 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
912 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
913 } else {
914 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
915 "provisioned, not installing default", p.number(), dev.id());
916 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700917 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000918 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100919 }
920 }
921 }
922 }
923
Jonathan Hart403372d2018-08-22 11:44:13 -0700924
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100925 /**
926 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000927 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100928 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
929 * this logic needs to be changed
930 *
931 * @param dev Device to look for
932 * @return The uplink Port of the OLT
933 */
934 private Port getUplinkPort(Device dev) {
935 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000936 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700937 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700938 if (deviceInfo == null) {
939 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000940 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700941 return null;
942 }
943 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +0530944 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700945 .filter(port -> isNniPort(port) ||
946 (port.number().toLong() == deviceInfo.uplinkPort()))
947 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +0530948 if (optionalPort.isPresent()) {
949 log.trace("getUplinkPort: Found port {}", optionalPort.get());
950 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100951 }
952
Saurav Daseae48de2019-06-19 13:26:15 -0700953 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100954 return null;
955 }
956
957 /**
958 * Return the subscriber on a port.
959 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800960 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100961 * @return subscriber if found else null
962 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800963 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
964 Port port = deviceService.getPort(cp);
965 checkNotNull(port, "Invalid connect point");
966 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100967 return subsService.get(portName);
968 }
969
Gamze Abakaad329652018-12-20 10:12:21 +0000970 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000971 * Checks whether the given port of the device is a uni port or not.
972 *
973 * @param d the access device
974 * @param p the port of the device
975 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000976 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100977 private boolean isUniPort(Device d, Port p) {
978 Port ulPort = getUplinkPort(d);
979 if (ulPort != null) {
980 return (ulPort.number().toLong() != p.number().toLong());
981 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530982 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
983 //checks whether the port name starts with nni- which is the signature of an NNI Port
984 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
985 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
986 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
987 return false;
988 }
989 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700990 }
991
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000992 /**
993 * Gets the given device details from SADIS.
994 * If the device is not found, returns null
995 *
996 * @param dev the access device
997 * @return the olt information
998 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700999 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1000 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001001 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001002 }
1003
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001004 // Custom-built function, when the device is not available we need a fallback mechanism
1005 private boolean isLocalLeader(DeviceId deviceId) {
1006 if (!mastershipService.isLocalMaster(deviceId)) {
1007 // When the device is available we just check the mastership
1008 if (deviceService.isAvailable(deviceId)) {
1009 return false;
1010 }
1011 // Fallback with Leadership service - device id is used as topic
1012 NodeId leader = leadershipService.runForLeadership(
1013 deviceId.toString()).leaderNodeId();
1014 // Verify if this node is the leader
1015 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001016 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001017 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001018 }
1019
kdarapuaa5da252020-04-10 15:58:05 +05301020 private boolean isNniPort(Port port) {
1021 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1022 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1023 }
1024 return false;
1025 }
1026
alshabibf0e7e702015-05-30 18:22:36 -07001027 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001028 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1029
alshabibf0e7e702015-05-30 18:22:36 -07001030 @Override
1031 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001032 eventExecutor.execute(() -> {
1033 DeviceId devId = event.subject().id();
1034 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001035 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001036 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001037
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001038 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1039 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1040 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001041 return;
1042 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001043
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001044 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001045 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001046 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1047 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1048 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001049 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001050 programmedDevices.remove(devId);
1051 // Since no mastership of the device is present upon disconnection
1052 // the method in the FlowRuleManager only empties the local copy
1053 // of the DeviceFlowTable thus this method needs to get called
1054 // on every instance, see how it's done in the InternalDeviceListener
1055 // in FlowRuleManager: no mastership check for purgeOnDisconnection
1056 flowRuleService.purgeFlowRules(devId);
1057 return;
1058 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001059 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001060 return;
1061 }
1062
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001063 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
1064
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001065 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001066 // it's possible that we got an event for a previously
1067 // programmed OLT that is no longer available in SADIS
1068 // we let such events go through
1069 if (!programmedDevices.contains(devId)) {
1070 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001071 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001072 return;
1073 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001074 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001075
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001076 switch (event.type()) {
1077 //TODO: Port handling and bookkeeping should be improved once
1078 // olt firmware handles correct behaviour.
1079 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001080 if (isUniPort(dev, port)) {
1081 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001082
1083 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1084 log.info("eapol will be sent for port added {}", port);
1085 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1086 null,
1087 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001088 }
1089 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001090 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1091 if (deviceInfo != null) {
1092 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1093 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001094 }
1095 break;
1096 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001097 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001098 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001099 log.info("eapol will be send for port {} removed", port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001100 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1101 null,
1102 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001103
Gamze Abaka838d8142019-02-21 07:06:55 +00001104 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001105 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001106 break;
1107 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001108 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001109 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1110 if (deviceInfo != null && port.isEnabled()) {
1111 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1112 port.number());
1113 oltFlowService.processNniFilteringObjectives(dev.id(),
1114 port.number(), true);
1115 }
1116 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001117 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001118 ConnectPoint cp = new ConnectPoint(devId, port.number());
1119 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001120 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001121 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001122 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001123 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001124 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001125 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001126 null,
1127 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1128 port.isEnabled());
1129 }
1130 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001131 log.info("eapol will be {} for dev/port updated {}/{}",
1132 (port.isEnabled()) ? "added" : "removed",
1133 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001134 uniTagInformationSet.forEach(uniTag ->
1135 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1136 uniTag.getUpstreamBandwidthProfile(), null,
1137 uniTag.getPonCTag(), port.isEnabled()));
1138 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001139 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001140 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001141 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001142 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001143 }
alshabibbb83aa22016-02-10 15:08:23 -08001144 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001145 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001146 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001147 break;
1148 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001149 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001150 break;
1151 case DEVICE_AVAILABILITY_CHANGED:
1152 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001153 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001154 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001155 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301156 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001157 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001158 + "flushing all state for dev:{}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301159 handleDeviceDisconnection(dev, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001160 } else {
1161 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001162 + "assuming temporary disconnection, "
1163 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301164 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001165 }
1166 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001167 default:
1168 return;
1169 }
1170 });
alshabibf0e7e702015-05-30 18:22:36 -07001171 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001172
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001173 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1174 deviceService.getPorts(device.id()).stream()
1175 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1176 .filter(p -> isUniPort(device, p))
1177 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1178 }
1179
1180 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
1181 programmedDevices.remove(device.id());
1182 removeAllSubscribers(device.id());
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301183 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001184 oltMeterService.clearMeters(device.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001185 post(new AccessDeviceEvent(
1186 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1187 null, null, null));
1188 if (sendUniEvent) {
1189 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001190 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001191 }
1192
1193 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1194 post(new AccessDeviceEvent(
1195 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1196 null, null, null));
1197 programmedDevices.add(dev.id());
1198 checkAndCreateDeviceFlows(dev);
1199 if (sendUniEvent) {
1200 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1201 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001202 }
Gamze Abakada282b42019-03-11 13:16:48 +00001203
1204 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001205 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1206 .filter(e -> e.getKey().deviceId().equals(deviceId))
1207 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001208
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001209 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001210 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001211
Gamze Abaka641fc072018-09-04 09:16:27 +00001212 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001213
1214 private class InternalClusterListener implements ClusterEventListener {
1215
1216 @Override
1217 public void event(ClusterEvent event) {
1218 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1219 hasher.addServer(event.subject().id());
1220 }
1221 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1222 hasher.removeServer(event.subject().id());
1223 }
1224 }
1225 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001226
Hardik Windlass395ff372019-06-13 05:16:00 +00001227}