blob: d5e8e32d150e4fb975c1a6905665c81e2aee2290 [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;
Andrea Campanella0c3309d2020-05-29 01:51:18 -070044import java.util.concurrent.ConcurrentHashMap;
Saurav Das9da7d522020-03-23 19:14:35 -070045import java.util.concurrent.ExecutorService;
46import java.util.concurrent.Executors;
47
alshabibf0e7e702015-05-30 18:22:36 -070048import org.onlab.packet.VlanId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080049import org.onlab.util.KryoNamespace;
Gamze Abaka1f62dd92020-05-07 08:58:13 +000050import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080051import org.onosproject.cluster.ClusterEvent;
52import org.onosproject.cluster.ClusterEventListener;
53import org.onosproject.cluster.ClusterService;
54import org.onosproject.cluster.ControllerNode;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020055import org.onosproject.cluster.LeadershipService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080056import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070057import org.onosproject.core.ApplicationId;
58import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080059import org.onosproject.event.AbstractListenerManager;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020060import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010061import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070062import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010063import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070064import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080065import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070066import org.onosproject.net.PortNumber;
67import org.onosproject.net.device.DeviceEvent;
68import org.onosproject.net.device.DeviceListener;
69import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053070import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070071import org.onosproject.net.flowobjective.FlowObjectiveService;
72import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080073import org.onosproject.net.flowobjective.Objective;
74import org.onosproject.net.flowobjective.ObjectiveContext;
75import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070076import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080077import org.onosproject.store.serializers.KryoNamespaces;
78import org.onosproject.store.service.ConsistentMultimap;
79import org.onosproject.store.service.Serializer;
80import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070081import org.opencord.olt.AccessDeviceEvent;
82import org.opencord.olt.AccessDeviceListener;
83import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010084import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000085import org.opencord.olt.internalapi.AccessDeviceFlowService;
86import org.opencord.olt.internalapi.AccessDeviceMeterService;
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<DeviceBandwidthProfile> pendingMeters;
189 private Set<SubscriberFlowInfo> pendingSubscribers;
190
alshabibf0e7e702015-05-30 18:22:36 -0700191 @Activate
alshabibe0559672016-02-21 14:49:51 -0800192 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000193 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
194 "events-%d", log));
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700195
alshabibe0559672016-02-21 14:49:51 -0800196 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000197 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000198 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800199
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800200 KryoNamespace serializer = KryoNamespace.newBuilder()
201 .register(KryoNamespaces.API)
202 .register(UniTagInformation.class)
203 .build();
204
205 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
206 .withName("volt-programmed-subs")
207 .withSerializer(Serializer.using(serializer))
208 .withApplicationId(appId)
209 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700210
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700211 pendingMeters = ConcurrentHashMap.newKeySet();
212 pendingSubscribers = Sets.newConcurrentHashSet();
alshabib8e4fd2f2016-01-12 15:55:53 -0800213 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
214
Gamze Abaka641fc072018-09-04 09:16:27 +0000215 subsService = sadisService.getSubscriberInfoService();
216 bpService = sadisService.getBandwidthProfileService();
217
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800218 List<NodeId> readyNodes = clusterService.getNodes().stream()
219 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
220 .map(ControllerNode::id)
221 .collect(toList());
222 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
223 clusterService.addListener(clusterListener);
224
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100225 // look for all provisioned devices in Sadis and create EAPOL flows for the
226 // UNI ports
227 Iterable<Device> devices = deviceService.getDevices();
228 for (Device d : devices) {
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200229 if (isLocalLeader(d.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800230 checkAndCreateDeviceFlows(d);
231 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100232 }
alshabib4ceaed32016-03-03 18:00:58 -0800233
alshabibba357492016-01-27 13:49:46 -0800234 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700235 log.info("Started with Application ID {}", appId.id());
236 }
237
238 @Deactivate
239 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000240 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800241 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800242 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700243 eventDispatcher.removeSink(AccessDeviceEvent.class);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700244 eventExecutor.shutdown();
alshabibf0e7e702015-05-30 18:22:36 -0700245 log.info("Stopped");
246 }
247
alshabibe0559672016-02-21 14:49:51 -0800248 @Modified
249 public void modified(ComponentContext context) {
250 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
251
252 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200253 String bpId = get(properties, DEFAULT_BP_ID);
254 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000255
Andrea Campanella971d5b92020-05-07 11:20:43 +0200256 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
257 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000258
259 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000260
alshabibe0559672016-02-21 14:49:51 -0800261 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000262 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200263 defaultBpId = DEFAULT_BP_ID_DEFAULT;
264 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800265 }
266 }
267
alshabib32232c82016-02-25 17:57:24 -0500268 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000269 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700270 log.info("Call to provisioning subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000271 DeviceId deviceId = connectPoint.deviceId();
272 PortNumber subscriberPortNo = connectPoint.port();
273
274 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000275 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000276
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100277 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000278 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100279 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000280 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100281 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100282 }
Jonathan Harte533a422015-10-20 17:31:24 -0700283
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100284 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000285 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100286 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000287 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100288 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700289 }
290
Gamze Abaka838d8142019-02-21 07:06:55 +0000291 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000292 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000293 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000294 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
295 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
296 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000297 filterFuture.thenAcceptAsync(filterStatus -> {
298 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000299 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Gamze Abaka33feef52019-02-27 08:16:47 +0000300 }
301 });
Amit Ghosh31939522018-08-16 13:28:21 +0100302 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800303 }
304
305 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000306 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700307 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000308
Saurav Daseae48de2019-06-19 13:26:15 -0700309 // Get the subscriber connected to this port from the local cache
310 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000311 DeviceId deviceId = connectPoint.deviceId();
312 PortNumber subscriberPortNo = connectPoint.port();
313
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800314 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000315 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000316 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000317 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800318 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800319 }
320
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100321 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000322 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100323 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000324 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100325 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800326 }
327
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000328 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100329
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000330 if (multicastServiceName.equals(uniTag.getServiceName())) {
331 continue;
332 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000333
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000334 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800335
Saurav Das9da7d522020-03-23 19:14:35 -0700336 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000337 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
338 uniTag.getUpstreamBandwidthProfile(),
339 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100340
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000341 Port port = deviceService.getPort(deviceId, subscriberPortNo);
342 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700343 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000344 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
345 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
346 } else {
347 log.debug("Port {} is no longer enabled or it's unavailable. Not "
348 + "reprogramming default eapol flow", connectPoint);
349 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100350 }
Amit Ghosh31939522018-08-16 13:28:21 +0100351 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800352 }
353
Gamze Abakaf59c0912019-04-19 08:24:28 +0000354
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000355 @Override
356 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
357 Optional<VlanId> cTag, Optional<Integer> tpId) {
358
359 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
360 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000361
Amit Ghosh31939522018-08-16 13:28:21 +0100362 // Check if we can find the connect point to which this subscriber is connected
363 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
364 if (subsPort == null) {
365 log.warn("ConnectPoint for {} not found", subscriberId);
366 return false;
367 }
368
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100369 if (!sTag.isPresent() && !cTag.isPresent()) {
370 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000371 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100372 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
373 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000374 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100375 return false;
376 }
377
Gamze Abakaf59c0912019-04-19 08:24:28 +0000378 //delete Eapol authentication flow with default bandwidth
379 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000380 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000381 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
382 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
383 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000384 filterFuture.thenAcceptAsync(filterStatus -> {
385 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000386 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
387 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000388 }
389 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100390 return true;
391 } else {
392 log.warn("Provisioning failed for subscriber: {}", subscriberId);
393 return false;
394 }
Amit Ghosh31939522018-08-16 13:28:21 +0100395 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100396
alshabibe0559672016-02-21 14:49:51 -0800397 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000398 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
399 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100400 // Check if we can find the connect point to which this subscriber is connected
401 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
402 if (subsPort == null) {
403 log.warn("ConnectPoint for {} not found", subscriberId);
404 return false;
405 }
406
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100407 if (!sTag.isPresent() && !cTag.isPresent()) {
408 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000409 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100410 // Get the uplink port
411 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
412 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000413 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100414 return false;
415 }
416
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000417 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
418 if (!tagInfo.isPresent()) {
419 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
420 subsPort, cTag, sTag, tpId);
421 return false;
422 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000423
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000424 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100425 return true;
426 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000427 log.warn("Removing subscriber is not possible - please check the provided information" +
428 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100429 return false;
430 }
Amit Ghosh31939522018-08-16 13:28:21 +0100431 }
432
433 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000434 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800435 return programmedSubs.stream()
436 .collect(collectingAndThen(
437 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
438 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700439 }
440
441 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100442 public List<DeviceId> fetchOlts() {
443 // look through all the devices and find the ones that are OLTs as per Sadis
444 List<DeviceId> olts = new ArrayList<>();
445 Iterable<Device> devices = deviceService.getDevices();
446 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700447 if (getOltInfo(d) != null) {
448 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100449 olts.add(d.id());
450 }
451 }
452 return olts;
alshabibe0559672016-02-21 14:49:51 -0800453 }
454
Amit Ghosh31939522018-08-16 13:28:21 +0100455 /**
456 * Finds the connect point to which a subscriber is connected.
457 *
458 * @param id The id of the subscriber, this is the same ID as in Sadis
459 * @return Subscribers ConnectPoint if found else null
460 */
461 private ConnectPoint findSubscriberConnectPoint(String id) {
462
463 Iterable<Device> devices = deviceService.getDevices();
464 for (Device d : devices) {
465 for (Port p : deviceService.getPorts(d.id())) {
466 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
467 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
468 log.debug("Found on device {} port {}", d.id(), p.number());
469 return new ConnectPoint(d.id(), p.number());
470 }
471 }
472 }
473 return null;
474 }
475
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000476 /**
477 * Gets the context of the bandwidth profile information for the given parameter.
478 *
479 * @param bandwidthProfile the bandwidth profile id
480 * @return the context of the bandwidth profile information
481 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000482 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
483 if (bandwidthProfile == null) {
484 return null;
485 }
486 return bpService.get(bandwidthProfile);
487 }
488
Gamze Abaka838d8142019-02-21 07:06:55 +0000489 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000490 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000491 *
492 * @param deviceId the device identifier
493 * @param uplink uplink port of the OLT
494 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000495 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000496 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000497 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000498 PortNumber subscriberPort, UniTagInformation uniTag) {
499
500 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800501
502 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
503 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
504
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000505 VlanId deviceVlan = uniTag.getPonSTag();
506 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000507
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000508 MeterId upstreamMeterId = oltMeterService
509 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
510 MeterId downstreamMeterId = oltMeterService
511 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000512
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000513 ForwardingObjective.Builder upFwd =
514 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
515 ForwardingObjective.Builder downFwd =
516 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
517
Andrea Campanella7c49b792020-05-11 11:36:53 +0200518 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
519 upstreamMeterId, uniTag, false, true);
520 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
521 upstreamMeterId, uniTag, false, true);
alshabibbf23a1f2016-01-14 17:27:11 -0800522
alshabib4ceaed32016-03-03 18:00:58 -0800523 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
524 @Override
525 public void onSuccess(Objective objective) {
526 upFuture.complete(null);
527 }
alshabibbf23a1f2016-01-14 17:27:11 -0800528
alshabib4ceaed32016-03-03 18:00:58 -0800529 @Override
530 public void onError(Objective objective, ObjectiveError error) {
531 upFuture.complete(error);
532 }
533 }));
534
535 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
536 @Override
537 public void onSuccess(Objective objective) {
538 downFuture.complete(null);
539 }
540
541 @Override
542 public void onError(Objective objective, ObjectiveError error) {
543 downFuture.complete(error);
544 }
545 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800546
547 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000548 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800549 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000550 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
551 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
552 uniTag.getTechnologyProfileId(), subscriberPort);
553 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800554 } else if (downStatus != null) {
555 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000556 "on port {} failed downstream uninstallation: {}",
557 subscriberVlan, deviceId, subscriberPort, downStatus);
558 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800559 } else if (upStatus != null) {
560 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000561 "on port {} failed upstream uninstallation: {}",
562 subscriberVlan, deviceId, subscriberPort, upStatus);
563 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800564 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000565 Port port = deviceService.getPort(deviceId, subscriberPort);
566 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
567 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800568 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700569 }
570
Gamze Abaka838d8142019-02-21 07:06:55 +0000571 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000572 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000573 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000574 * @param connectPoint the connection point of the subscriber
575 * @param uplinkPort uplink port of the OLT (the nni port)
576 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000577 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000578 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
579 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000580
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700581 log.debug("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000582
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000583 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
584 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
585 return;
586 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000587
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000588 DeviceId deviceId = connectPoint.deviceId();
589 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000590
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000591 for (UniTagInformation uniTag : sub.uniTagList()) {
592 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
593 }
594 }
alshabib3ea82642016-01-12 18:06:53 -0800595
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000596 /**
597 * Finds the uni tag information and provisions the found information.
598 * If the uni tag information is not found, returns
599 *
600 * @param deviceId the access device id
601 * @param uplinkPort the nni port
602 * @param subscriberPort the uni port
603 * @param innerVlan the pon c tag
604 * @param outerVlan the pon s tag
605 * @param tpId the technology profile id
606 */
607 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
608 PortNumber subscriberPort,
609 VlanId innerVlan,
610 VlanId outerVlan,
611 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700612
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000613 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
614 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
615 if (!gotTagInformation.isPresent()) {
616 return;
617 }
618 UniTagInformation tagInformation = gotTagInformation.get();
619 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
620 }
alshabib3ea82642016-01-12 18:06:53 -0800621
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000622 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800623 if (add) {
624 programmedSubs.put(connectPoint, tagInformation);
625 } else {
626 programmedSubs.remove(connectPoint, tagInformation);
627 }
Jonathan Harte533a422015-10-20 17:31:24 -0700628 }
629
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000630 /**
631 * Installs a uni tag information flow.
632 *
633 * @param deviceId the access device id
634 * @param uplinkPort the nni port
635 * @param subscriberPort the uni port
636 * @param tagInfo the uni tag information
637 */
638 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
639 UniTagInformation tagInfo) {
640
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700641 log.debug("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000642
643 Port port = deviceService.getPort(deviceId, subscriberPort);
644
645 if (multicastServiceName.equals(tagInfo.getServiceName())) {
646 // IGMP flows are taken care of along with VOD service
647 // Please note that for each service, Subscriber Registered event will be sent
648 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
649 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
650 tagInfo.getTechnologyProfileId()));
651 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000652 }
653
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000654 BandwidthProfileInformation upstreamBpInfo =
655 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
656 BandwidthProfileInformation downstreamBpInfo =
657 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700658 if (upstreamBpInfo == null) {
659 log.warn("No meter installed since no Upstream BW Profile definition found for "
660 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
661 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
662 tagInfo.getTechnologyProfileId(), deviceId,
663 subscriberPort);
664 return;
665 }
666 if (downstreamBpInfo == null) {
667 log.warn("No meter installed since no Downstream BW Profile definition found for "
668 + "ctag {} stag {} tpId {} and Device/port: {}:{}",
669 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
670 tagInfo.getTechnologyProfileId(), deviceId,
671 subscriberPort);
672 return;
673 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000674
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700675 // check for meterIds for the upstream and downstream bandwidth profiles
676 MeterId upMeterId = oltMeterService
677 .getMeterIdFromBpMapping(deviceId, upstreamBpInfo.id());
678 MeterId downMeterId = oltMeterService
679 .getMeterIdFromBpMapping(deviceId, downstreamBpInfo.id());
680 SubscriberFlowInfo fi = new SubscriberFlowInfo(deviceId, uplinkPort, subscriberPort,
681 tagInfo, downMeterId, upMeterId,
682 downstreamBpInfo.id(), upstreamBpInfo.id());
683
684 if (upMeterId != null && downMeterId != null) {
685 log.debug("Meters are existing for upstream {} and downstream {}",
686 upstreamBpInfo.id(), downstreamBpInfo.id());
687 handleSubFlowsWithMeters(fi);
688 } else {
689 log.debug("Adding {} to pending subs", fi);
690 // one or both meters are not ready. It's possible they are in the process of being
691 // created for other subscribers that share the same bandwidth profile.
692 pendingSubscribers.add(fi);
693
694 // queue up the meters to be created
695 if (upMeterId == null) {
696 log.debug("Missing meter for upstream {}", upstreamBpInfo.id());
697 checkAndCreateDevMeter(new DeviceBandwidthProfile(deviceId, upstreamBpInfo));
698 }
699 if (downMeterId == null) {
700 log.debug("Missing meter for downstream {}", downstreamBpInfo.id());
701 checkAndCreateDevMeter(new DeviceBandwidthProfile(deviceId, downstreamBpInfo));
702 }
703 }
704 }
705 private void checkAndCreateDevMeter(DeviceBandwidthProfile dm) {
706 if (pendingMeters.contains(dm)) {
707 return;
708 }
709 pendingMeters.add(dm);
710 createMeter(dm);
711 }
712
713 private void createMeter(DeviceBandwidthProfile dm) {
714 log.debug("Creating Meter {} from queue", dm);
715 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
716 MeterId meterId = oltMeterService.createMeter(dm.getDevId(), dm.getBwInfo(),
717 meterFuture);
718
719 meterFuture.thenAcceptAsync(result -> {
720 // iterate through the subscribers on hold
721 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
722 while (subsIterator.hasNext()) {
723 SubscriberFlowInfo fi = subsIterator.next();
724 if (result == null) {
725 // meter install sent to device
726 log.debug("Meter {} installed for bw {}", meterId, dm.getBwInfo());
727
728 MeterId upMeterId = oltMeterService
729 .getMeterIdFromBpMapping(dm.getDevId(), fi.getUpBpInfo());
730 MeterId downMeterId = oltMeterService
731 .getMeterIdFromBpMapping(dm.getDevId(), fi.getDownBpInfo());
732 if (upMeterId != null && downMeterId != null) {
733 log.debug("Provisioning subscriber after meter {}" +
734 "installation and both meters are present " +
735 "upstream {} and downstream {}",
736 meterId, upMeterId, downMeterId);
737 // put in the meterIds because when fi was first
738 // created there may or may not have been a meterId
739 // depending on whether the meter was created or
740 // not at that time.
741 fi.setUpMeterId(upMeterId);
742 fi.setDownMeterId(downMeterId);
743 handleSubFlowsWithMeters(fi);
744 subsIterator.remove();
745 }
746 pendingMeters.remove(new DeviceBandwidthProfile(dm.getDevId(), dm.getBwInfo()));
747 } else {
748 // meter install failed
749 log.error("Addition of subscriber {} failed due to meter " +
750 "{} with result {}", fi, meterId, result);
751 subsIterator.remove();
752 pendingMeters.remove(new DeviceBandwidthProfile(dm.getDevId(), dm.getBwInfo()));
753 }
754 }
755 });
756 }
757 /**
758 * Add subscriber flows given meter information for both upstream and
759 * downstream directions.
760 *
761 * @param subscriberFlowInfo relevant information for subscriber
762 */
763 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
764 log.debug("Provisioning subscriber flows based on {}", subscriberFlowInfo);
765 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000766 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
767 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000768
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700769 ForwardingObjective.Builder upFwd =
770 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
771 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
772 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
773 @Override
774 public void onSuccess(Objective objective) {
775 log.debug("Upstream flow installed successfully {}", subscriberFlowInfo);
776 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000777 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000778
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700779 @Override
780 public void onError(Objective objective, ObjectiveError error) {
781 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000782 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700783 }));
784
785 ForwardingObjective.Builder downFwd =
786 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
787 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
788 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
789 @Override
790 public void onSuccess(Objective objective) {
791 log.debug("Downstream flow installed successfully {}", subscriberFlowInfo);
792 downFuture.complete(null);
793 }
794
795 @Override
796 public void onError(Objective objective, ObjectiveError error) {
797 downFuture.complete(error);
798 }
799 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000800
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100801 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000802 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100803 if (downStatus != null) {
804 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000805 "on port {} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700806 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
807 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000808 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100809 } else if (upStatus != null) {
810 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000811 "on port {} failed upstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700812 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
813 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000814 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000815 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700816 log.debug("Upstream and downstream data plane flows are installed successfully " +
817 "for {}", subscriberFlowInfo);
818 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
819 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000820 tagInfo.getUpstreamBandwidthProfile(),
821 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700822 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
823 subscriberFlowInfo.getUniPort(),
824 subscriberFlowInfo.getUpId(),
825 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000826
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700827 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
828 subscriberFlowInfo.getUniPort(),
829 subscriberFlowInfo.getUpId(),
830 tagInfo, true, true);
831 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
832 subscriberFlowInfo.getUniPort()),
833 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100834 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700835 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
836 deviceService.getPort(subscriberFlowInfo.getDevId(),
837 subscriberFlowInfo.getUniPort()),
838 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
839 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100840 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100841 }
842
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000843 /**
844 * Checks the subscriber uni tag list and find the uni tag information.
845 * using the pon c tag, pon s tag and the technology profile id
846 * May return Optional<null>
847 *
848 * @param cp the connection point of the subscriber
849 * @param innerVlan pon c tag
850 * @param outerVlan pon s tag
851 * @param tpId the technology profile id
852 * @return the found uni tag information
853 */
854 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
855 int tpId) {
856 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
857 outerVlan, tpId);
858 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000859 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000860 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
861 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000862 }
863
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000864 List<UniTagInformation> uniTagList = subInfo.uniTagList();
865 if (uniTagList == null) {
866 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
867 return Optional.empty();
868 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100869
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000870 UniTagInformation service = null;
871 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
872 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
873 && tpId == tagInfo.getTechnologyProfileId()) {
874 service = tagInfo;
875 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700876 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000877 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000878
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000879 if (service == null) {
880 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
881 innerVlan, outerVlan, tpId);
882 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000883 }
884
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000885 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100886 }
887
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100888 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700889 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
890 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100891 *
892 * @param dev Device to look for
893 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700894 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100895 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000896 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000897 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100898
899 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700900 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100901 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800902 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000903 continue;
904 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100905 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100906 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
907 log.info("Creating Eapol for the uni {}", p);
908 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
909 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
910 } else {
911 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
912 "provisioned, not installing default", p.number(), dev.id());
913 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700914 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000915 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100916 }
917 }
918 }
919 }
920
Jonathan Hart403372d2018-08-22 11:44:13 -0700921
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100922 /**
923 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000924 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100925 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
926 * this logic needs to be changed
927 *
928 * @param dev Device to look for
929 * @return The uplink Port of the OLT
930 */
931 private Port getUplinkPort(Device dev) {
932 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000933 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700934 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700935 if (deviceInfo == null) {
936 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000937 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700938 return null;
939 }
940 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +0530941 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700942 .filter(port -> isNniPort(port) ||
943 (port.number().toLong() == deviceInfo.uplinkPort()))
944 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +0530945 if (optionalPort.isPresent()) {
946 log.trace("getUplinkPort: Found port {}", optionalPort.get());
947 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100948 }
949
Saurav Daseae48de2019-06-19 13:26:15 -0700950 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100951 return null;
952 }
953
954 /**
955 * Return the subscriber on a port.
956 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800957 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100958 * @return subscriber if found else null
959 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800960 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
961 Port port = deviceService.getPort(cp);
962 checkNotNull(port, "Invalid connect point");
963 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100964 return subsService.get(portName);
965 }
966
Gamze Abakaad329652018-12-20 10:12:21 +0000967 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000968 * Checks whether the given port of the device is a uni port or not.
969 *
970 * @param d the access device
971 * @param p the port of the device
972 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000973 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100974 private boolean isUniPort(Device d, Port p) {
975 Port ulPort = getUplinkPort(d);
976 if (ulPort != null) {
977 return (ulPort.number().toLong() != p.number().toLong());
978 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530979 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
980 //checks whether the port name starts with nni- which is the signature of an NNI Port
981 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
982 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
983 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
984 return false;
985 }
986 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700987 }
988
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000989 /**
990 * Gets the given device details from SADIS.
991 * If the device is not found, returns null
992 *
993 * @param dev the access device
994 * @return the olt information
995 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700996 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
997 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +0000998 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -0700999 }
1000
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001001 // Custom-built function, when the device is not available we need a fallback mechanism
1002 private boolean isLocalLeader(DeviceId deviceId) {
1003 if (!mastershipService.isLocalMaster(deviceId)) {
1004 // When the device is available we just check the mastership
1005 if (deviceService.isAvailable(deviceId)) {
1006 return false;
1007 }
1008 // Fallback with Leadership service - device id is used as topic
1009 NodeId leader = leadershipService.runForLeadership(
1010 deviceId.toString()).leaderNodeId();
1011 // Verify if this node is the leader
1012 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001013 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001014 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001015 }
1016
kdarapuaa5da252020-04-10 15:58:05 +05301017 private boolean isNniPort(Port port) {
1018 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1019 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1020 }
1021 return false;
1022 }
1023
alshabibf0e7e702015-05-30 18:22:36 -07001024 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001025 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1026
alshabibf0e7e702015-05-30 18:22:36 -07001027 @Override
1028 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001029 eventExecutor.execute(() -> {
1030 DeviceId devId = event.subject().id();
1031 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001032 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001033 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001034
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001035 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1036 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1037 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001038 return;
1039 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001040
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001041 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001042 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001043 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1044 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1045 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001046 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001047 programmedDevices.remove(devId);
1048 // Since no mastership of the device is present upon disconnection
1049 // the method in the FlowRuleManager only empties the local copy
1050 // of the DeviceFlowTable thus this method needs to get called
1051 // on every instance, see how it's done in the InternalDeviceListener
1052 // in FlowRuleManager: no mastership check for purgeOnDisconnection
1053 flowRuleService.purgeFlowRules(devId);
1054 return;
1055 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001056 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001057 return;
1058 }
1059
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001060 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
1061
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001062 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001063 // it's possible that we got an event for a previously
1064 // programmed OLT that is no longer available in SADIS
1065 // we let such events go through
1066 if (!programmedDevices.contains(devId)) {
1067 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001068 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001069 return;
1070 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001071 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001072
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001073 switch (event.type()) {
1074 //TODO: Port handling and bookkeeping should be improved once
1075 // olt firmware handles correct behaviour.
1076 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001077 if (isUniPort(dev, port)) {
1078 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001079
1080 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1081 log.info("eapol will be sent for port added {}", port);
1082 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1083 null,
1084 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001085 }
1086 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001087 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1088 if (deviceInfo != null) {
1089 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1090 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001091 }
1092 break;
1093 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001094 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001095 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001096 log.info("eapol will be send for port {} removed", port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001097 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1098 null,
1099 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001100
Gamze Abaka838d8142019-02-21 07:06:55 +00001101 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001102 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001103 break;
1104 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001105 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001106 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1107 if (deviceInfo != null && port.isEnabled()) {
1108 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1109 port.number());
1110 oltFlowService.processNniFilteringObjectives(dev.id(),
1111 port.number(), true);
1112 }
1113 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001114 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001115 ConnectPoint cp = new ConnectPoint(devId, port.number());
1116 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001117 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001118 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001119 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001120 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001121 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001122 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001123 null,
1124 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1125 port.isEnabled());
1126 }
1127 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001128 log.info("eapol will be {} for dev/port updated {}/{}",
1129 (port.isEnabled()) ? "added" : "removed",
1130 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001131 uniTagInformationSet.forEach(uniTag ->
1132 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1133 uniTag.getUpstreamBandwidthProfile(), null,
1134 uniTag.getPonCTag(), port.isEnabled()));
1135 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001136 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001137 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001138 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001139 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001140 }
alshabibbb83aa22016-02-10 15:08:23 -08001141 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001142 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001143 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001144 break;
1145 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001146 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001147 break;
1148 case DEVICE_AVAILABILITY_CHANGED:
1149 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001150 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001151 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001152 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301153 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001154 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001155 + "flushing all state for dev:{}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301156 handleDeviceDisconnection(dev, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001157 } else {
1158 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001159 + "assuming temporary disconnection, "
1160 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301161 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001162 }
1163 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001164 default:
1165 return;
1166 }
1167 });
alshabibf0e7e702015-05-30 18:22:36 -07001168 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001169
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001170 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1171 deviceService.getPorts(device.id()).stream()
1172 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1173 .filter(p -> isUniPort(device, p))
1174 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1175 }
1176
1177 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
1178 programmedDevices.remove(device.id());
1179 removeAllSubscribers(device.id());
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301180 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001181 oltMeterService.clearMeters(device.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001182 post(new AccessDeviceEvent(
1183 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1184 null, null, null));
1185 if (sendUniEvent) {
1186 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001187 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001188 }
1189
1190 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1191 post(new AccessDeviceEvent(
1192 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1193 null, null, null));
1194 programmedDevices.add(dev.id());
1195 checkAndCreateDeviceFlows(dev);
1196 if (sendUniEvent) {
1197 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1198 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001199 }
Gamze Abakada282b42019-03-11 13:16:48 +00001200
1201 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001202 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1203 .filter(e -> e.getKey().deviceId().equals(deviceId))
1204 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001205
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001206 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001207 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001208
Gamze Abaka641fc072018-09-04 09:16:27 +00001209 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001210
1211 private class InternalClusterListener implements ClusterEventListener {
1212
1213 @Override
1214 public void event(ClusterEvent event) {
1215 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1216 hasher.addServer(event.subject().id());
1217 }
1218 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1219 hasher.removeServer(event.subject().id());
1220 }
1221 }
1222 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001223
Hardik Windlass395ff372019-06-13 05:16:00 +00001224}