blob: 42c53b7bbca5c9f3d1515f552cba34cdc2a6b661 [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;
Gamze Abaka641fc072018-09-04 09:16:27 +000086import org.opencord.sadis.BandwidthProfileInformation;
87import org.opencord.sadis.BaseInformationService;
88import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010089import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000090import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080091import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070092import org.osgi.service.component.annotations.Activate;
93import org.osgi.service.component.annotations.Component;
94import org.osgi.service.component.annotations.Deactivate;
95import org.osgi.service.component.annotations.Modified;
96import org.osgi.service.component.annotations.Reference;
97import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070098import org.slf4j.Logger;
99
Saurav Das9da7d522020-03-23 19:14:35 -0700100import com.google.common.collect.ImmutableMap;
101import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700102
103/**
Jonathan Harte533a422015-10-20 17:31:24 -0700104 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700105 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700106@Component(immediate = true,
107 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700108 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000109 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700110 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800111public class Olt
112 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
113 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800114 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800115
Gamze Abakada282b42019-03-11 13:16:48 +0000116 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000117 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800118
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800119 public static final int HASH_WEIGHT = 10;
120
alshabibf0e7e702015-05-30 18:22:36 -0700121 private final Logger log = getLogger(getClass());
122
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530123 private static final String NNI = "nni-";
124
Carmelo Casconeca931162019-07-15 18:22:24 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700126 protected FlowObjectiveService flowObjectiveService;
127
Carmelo Casconeca931162019-07-15 18:22:24 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700129 protected DeviceService deviceService;
130
Carmelo Casconeca931162019-07-15 18:22:24 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700132 protected CoreService coreService;
133
Carmelo Casconeca931162019-07-15 18:22:24 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000135 protected SadisService sadisService;
136
Carmelo Casconeca931162019-07-15 18:22:24 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000138 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800139
Carmelo Casconeca931162019-07-15 18:22:24 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000141 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000142
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800143 @Reference(cardinality = ReferenceCardinality.MANDATORY)
144 protected StorageService storageService;
145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY)
147 protected ClusterService clusterService;
148
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530149 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200150 protected MastershipService mastershipService;
151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY)
153 protected LeadershipService leadershipService;
154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530156 protected FlowRuleService flowRuleService;
157
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000158 @Reference(cardinality = ReferenceCardinality.MANDATORY)
159 protected ComponentConfigService componentConfigService;
160
Carmelo Casconeca931162019-07-15 18:22:24 -0700161 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800162 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700163 **/
164 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000165
Carmelo Casconeca931162019-07-15 18:22:24 -0700166 /**
Gamze Abaka51a34e82020-05-08 13:03:14 +0000167 * Default multicast service name.
Carmelo Casconeca931162019-07-15 18:22:24 -0700168 **/
Gamze Abaka51a34e82020-05-08 13:03:14 +0000169 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Gamze Abaka33feef52019-02-27 08:16:47 +0000170
alshabibf0e7e702015-05-30 18:22:36 -0700171 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800172 private final ClusterEventListener clusterListener = new InternalClusterListener();
173
174 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700175
Gamze Abaka641fc072018-09-04 09:16:27 +0000176 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
177 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700178
Gamze Abaka641fc072018-09-04 09:16:27 +0000179 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000180 groupedThreads("onos/olt-service",
181 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100182
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700183 protected ExecutorService eventExecutor;
184
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800185 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800186
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700187 private Set<SubscriberFlowInfo> pendingSubscribers;
188
alshabibf0e7e702015-05-30 18:22:36 -0700189 @Activate
alshabibe0559672016-02-21 14:49:51 -0800190 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000191 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
192 "events-%d", log));
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700193
alshabibe0559672016-02-21 14:49:51 -0800194 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000195 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000196 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800197
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800198 KryoNamespace serializer = KryoNamespace.newBuilder()
199 .register(KryoNamespaces.API)
200 .register(UniTagInformation.class)
201 .build();
202
203 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
204 .withName("volt-programmed-subs")
205 .withSerializer(Serializer.using(serializer))
206 .withApplicationId(appId)
207 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700208
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700209 pendingSubscribers = Sets.newConcurrentHashSet();
alshabib8e4fd2f2016-01-12 15:55:53 -0800210 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
211
Gamze Abaka641fc072018-09-04 09:16:27 +0000212 subsService = sadisService.getSubscriberInfoService();
213 bpService = sadisService.getBandwidthProfileService();
214
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800215 List<NodeId> readyNodes = clusterService.getNodes().stream()
216 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
217 .map(ControllerNode::id)
218 .collect(toList());
219 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
220 clusterService.addListener(clusterListener);
221
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100222 // look for all provisioned devices in Sadis and create EAPOL flows for the
223 // UNI ports
224 Iterable<Device> devices = deviceService.getDevices();
225 for (Device d : devices) {
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200226 if (isLocalLeader(d.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800227 checkAndCreateDeviceFlows(d);
228 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100229 }
alshabib4ceaed32016-03-03 18:00:58 -0800230
alshabibba357492016-01-27 13:49:46 -0800231 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700232 log.info("Started with Application ID {}", appId.id());
233 }
234
235 @Deactivate
236 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000237 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800238 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800239 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700240 eventDispatcher.removeSink(AccessDeviceEvent.class);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700241 eventExecutor.shutdown();
alshabibf0e7e702015-05-30 18:22:36 -0700242 log.info("Stopped");
243 }
244
alshabibe0559672016-02-21 14:49:51 -0800245 @Modified
246 public void modified(ComponentContext context) {
247 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
248
249 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200250 String bpId = get(properties, DEFAULT_BP_ID);
251 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000252
Andrea Campanella971d5b92020-05-07 11:20:43 +0200253 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
254 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000255
256 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000257
alshabibe0559672016-02-21 14:49:51 -0800258 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000259 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200260 defaultBpId = DEFAULT_BP_ID_DEFAULT;
261 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800262 }
263 }
264
alshabib32232c82016-02-25 17:57:24 -0500265 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000266 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200267 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000268 DeviceId deviceId = connectPoint.deviceId();
269 PortNumber subscriberPortNo = connectPoint.port();
270
271 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000272 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000273
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100274 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000275 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100276 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000277 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100278 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100279 }
Jonathan Harte533a422015-10-20 17:31:24 -0700280
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100281 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000282 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100283 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000284 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100285 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700286 }
287
Gamze Abaka838d8142019-02-21 07:06:55 +0000288 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000289 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000290 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000291 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
292 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
293 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000294 filterFuture.thenAcceptAsync(filterStatus -> {
295 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000296 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200297 } else {
298 log.error("The filtering future did not complete properly {} " +
299 "subscriber on {} is not provisioned", filterStatus, connectPoint);
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());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200697 checkAndCreateDevMeter(deviceId, upstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700698 }
699 if (downMeterId == null) {
700 log.debug("Missing meter for downstream {}", downstreamBpInfo.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +0200701 checkAndCreateDevMeter(deviceId, downstreamBpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700702 }
703 }
704 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200705 private void checkAndCreateDevMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
706 if (oltMeterService.isMeterPending(deviceId, bwpInfo)) {
707 log.debug("Meter is already pending {} on device {}", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700708 return;
709 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200710 oltMeterService.addToPendingMeters(deviceId, bwpInfo);
711 createMeter(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700712 }
713
Andrea Campanella600d2e22020-06-22 11:00:31 +0200714 private void createMeter(DeviceId deviceId, BandwidthProfileInformation bwpInfo) {
715 log.debug("Creating Meter with {} on {} for subscriber", bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700716 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200717
Andrea Campanella600d2e22020-06-22 11:00:31 +0200718 MeterId meterId = oltMeterService.createMeter(deviceId, bwpInfo,
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700719 meterFuture);
720
721 meterFuture.thenAcceptAsync(result -> {
722 // iterate through the subscribers on hold
723 Iterator<SubscriberFlowInfo> subsIterator = pendingSubscribers.iterator();
724 while (subsIterator.hasNext()) {
725 SubscriberFlowInfo fi = subsIterator.next();
726 if (result == null) {
727 // meter install sent to device
Andrea Campanella600d2e22020-06-22 11:00:31 +0200728 log.debug("Meter {} installed for bw {} on {}", meterId, bwpInfo, deviceId);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700729
730 MeterId upMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200731 .getMeterIdFromBpMapping(deviceId, fi.getUpBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700732 MeterId downMeterId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200733 .getMeterIdFromBpMapping(deviceId, fi.getDownBpInfo());
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700734 if (upMeterId != null && downMeterId != null) {
735 log.debug("Provisioning subscriber after meter {}" +
736 "installation and both meters are present " +
737 "upstream {} and downstream {}",
738 meterId, upMeterId, downMeterId);
739 // put in the meterIds because when fi was first
740 // created there may or may not have been a meterId
741 // depending on whether the meter was created or
742 // not at that time.
743 fi.setUpMeterId(upMeterId);
744 fi.setDownMeterId(downMeterId);
745 handleSubFlowsWithMeters(fi);
746 subsIterator.remove();
747 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200748 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700749 } else {
750 // meter install failed
751 log.error("Addition of subscriber {} failed due to meter " +
752 "{} with result {}", fi, meterId, result);
753 subsIterator.remove();
Andrea Campanella600d2e22020-06-22 11:00:31 +0200754 oltMeterService.removeFromPendingMeters(deviceId, bwpInfo);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700755 }
756 }
757 });
758 }
759 /**
760 * Add subscriber flows given meter information for both upstream and
761 * downstream directions.
762 *
763 * @param subscriberFlowInfo relevant information for subscriber
764 */
765 private void handleSubFlowsWithMeters(SubscriberFlowInfo subscriberFlowInfo) {
766 log.debug("Provisioning subscriber flows based on {}", subscriberFlowInfo);
767 UniTagInformation tagInfo = subscriberFlowInfo.getTagInfo();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000768 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
769 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000770
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700771 ForwardingObjective.Builder upFwd =
772 oltFlowService.createUpBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
773 subscriberFlowInfo.getUpId(), subscriberFlowInfo.getTagInfo());
774 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), upFwd.add(new ObjectiveContext() {
775 @Override
776 public void onSuccess(Objective objective) {
777 log.debug("Upstream flow installed successfully {}", subscriberFlowInfo);
778 upFuture.complete(null);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000779 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000780
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700781 @Override
782 public void onError(Objective objective, ObjectiveError error) {
783 upFuture.complete(error);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000784 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700785 }));
786
787 ForwardingObjective.Builder downFwd =
788 oltFlowService.createDownBuilder(subscriberFlowInfo.getNniPort(), subscriberFlowInfo.getUniPort(),
789 subscriberFlowInfo.getDownId(), subscriberFlowInfo.getTagInfo());
790 flowObjectiveService.forward(subscriberFlowInfo.getDevId(), downFwd.add(new ObjectiveContext() {
791 @Override
792 public void onSuccess(Objective objective) {
793 log.debug("Downstream flow installed successfully {}", subscriberFlowInfo);
794 downFuture.complete(null);
795 }
796
797 @Override
798 public void onError(Objective objective, ObjectiveError error) {
799 downFuture.complete(error);
800 }
801 }));
Gamze Abakaf59c0912019-04-19 08:24:28 +0000802
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100803 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000804 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100805 if (downStatus != null) {
806 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000807 "on port {} failed downstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700808 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
809 subscriberFlowInfo.getUniPort(), downStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000810 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100811 } else if (upStatus != null) {
812 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000813 "on port {} failed upstream installation: {}",
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700814 tagInfo.getPonCTag(), tagInfo.getPonSTag(), subscriberFlowInfo.getDevId(),
815 subscriberFlowInfo.getUniPort(), upStatus);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000816 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000817 } else {
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700818 log.debug("Upstream and downstream data plane flows are installed successfully " +
819 "for {}", subscriberFlowInfo);
820 oltFlowService.processEapolFilteringObjectives(subscriberFlowInfo.getDevId(),
821 subscriberFlowInfo.getUniPort(),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000822 tagInfo.getUpstreamBandwidthProfile(),
823 null, tagInfo.getPonCTag(), true);
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700824 oltFlowService.processDhcpFilteringObjectives(subscriberFlowInfo.getDevId(),
825 subscriberFlowInfo.getUniPort(),
826 subscriberFlowInfo.getUpId(),
827 tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000828
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700829 oltFlowService.processIgmpFilteringObjectives(subscriberFlowInfo.getDevId(),
830 subscriberFlowInfo.getUniPort(),
831 subscriberFlowInfo.getUpId(),
832 tagInfo, true, true);
833 updateProgrammedSubscriber(new ConnectPoint(subscriberFlowInfo.getDevId(),
834 subscriberFlowInfo.getUniPort()),
835 tagInfo, true);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100836 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700837 post(new AccessDeviceEvent(type, subscriberFlowInfo.getDevId(),
838 deviceService.getPort(subscriberFlowInfo.getDevId(),
839 subscriberFlowInfo.getUniPort()),
840 tagInfo.getPonSTag(), tagInfo.getPonCTag(),
841 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100842 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100843 }
844
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000845 /**
846 * Checks the subscriber uni tag list and find the uni tag information.
847 * using the pon c tag, pon s tag and the technology profile id
848 * May return Optional<null>
849 *
850 * @param cp the connection point of the subscriber
851 * @param innerVlan pon c tag
852 * @param outerVlan pon s tag
853 * @param tpId the technology profile id
854 * @return the found uni tag information
855 */
856 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
857 int tpId) {
858 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
859 outerVlan, tpId);
860 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000861 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000862 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
863 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000864 }
865
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000866 List<UniTagInformation> uniTagList = subInfo.uniTagList();
867 if (uniTagList == null) {
868 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
869 return Optional.empty();
870 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100871
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000872 UniTagInformation service = null;
873 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
874 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
875 && tpId == tagInfo.getTechnologyProfileId()) {
876 service = tagInfo;
877 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700878 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000879 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000880
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000881 if (service == null) {
882 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
883 innerVlan, outerVlan, tpId);
884 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000885 }
886
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000887 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100888 }
889
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100890 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700891 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
892 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100893 *
894 * @param dev Device to look for
895 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700896 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100897 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000898 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000899 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100900
901 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700902 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100903 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800904 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000905 continue;
906 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100907 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100908 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
909 log.info("Creating Eapol for the uni {}", p);
910 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
911 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
912 } else {
913 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
914 "provisioned, not installing default", p.number(), dev.id());
915 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700916 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000917 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100918 }
919 }
920 }
921 }
922
Jonathan Hart403372d2018-08-22 11:44:13 -0700923
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100924 /**
925 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000926 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100927 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
928 * this logic needs to be changed
929 *
930 * @param dev Device to look for
931 * @return The uplink Port of the OLT
932 */
933 private Port getUplinkPort(Device dev) {
934 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000935 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700936 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700937 if (deviceInfo == null) {
938 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000939 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700940 return null;
941 }
942 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +0530943 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
Andrea Campanella0c3309d2020-05-29 01:51:18 -0700944 .filter(port -> isNniPort(port) ||
945 (port.number().toLong() == deviceInfo.uplinkPort()))
946 .findFirst();
kdarapuaa5da252020-04-10 15:58:05 +0530947 if (optionalPort.isPresent()) {
948 log.trace("getUplinkPort: Found port {}", optionalPort.get());
949 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100950 }
951
Saurav Daseae48de2019-06-19 13:26:15 -0700952 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100953 return null;
954 }
955
956 /**
957 * Return the subscriber on a port.
958 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800959 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100960 * @return subscriber if found else null
961 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800962 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
963 Port port = deviceService.getPort(cp);
964 checkNotNull(port, "Invalid connect point");
965 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100966 return subsService.get(portName);
967 }
968
Gamze Abakaad329652018-12-20 10:12:21 +0000969 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000970 * Checks whether the given port of the device is a uni port or not.
971 *
972 * @param d the access device
973 * @param p the port of the device
974 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000975 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100976 private boolean isUniPort(Device d, Port p) {
977 Port ulPort = getUplinkPort(d);
978 if (ulPort != null) {
979 return (ulPort.number().toLong() != p.number().toLong());
980 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530981 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
982 //checks whether the port name starts with nni- which is the signature of an NNI Port
983 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
984 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
985 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
986 return false;
987 }
988 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700989 }
990
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000991 /**
992 * Gets the given device details from SADIS.
993 * If the device is not found, returns null
994 *
995 * @param dev the access device
996 * @return the olt information
997 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700998 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
999 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001000 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001001 }
1002
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001003 // Custom-built function, when the device is not available we need a fallback mechanism
1004 private boolean isLocalLeader(DeviceId deviceId) {
1005 if (!mastershipService.isLocalMaster(deviceId)) {
1006 // When the device is available we just check the mastership
1007 if (deviceService.isAvailable(deviceId)) {
1008 return false;
1009 }
1010 // Fallback with Leadership service - device id is used as topic
1011 NodeId leader = leadershipService.runForLeadership(
1012 deviceId.toString()).leaderNodeId();
1013 // Verify if this node is the leader
1014 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001015 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001016 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001017 }
1018
kdarapuaa5da252020-04-10 15:58:05 +05301019 private boolean isNniPort(Port port) {
1020 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
1021 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
1022 }
1023 return false;
1024 }
1025
alshabibf0e7e702015-05-30 18:22:36 -07001026 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001027 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1028
alshabibf0e7e702015-05-30 18:22:36 -07001029 @Override
1030 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001031 eventExecutor.execute(() -> {
1032 DeviceId devId = event.subject().id();
1033 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001034 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001035 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -07001036
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001037 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
1038 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
1039 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001040 return;
1041 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001042
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001043 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001044 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001045 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
1046 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
1047 log.info("Cleaning local state for non master instance upon " +
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001048 "device disconnection {}", devId);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001049 // Since no mastership of the device is present upon disconnection
1050 // the method in the FlowRuleManager only empties the local copy
1051 // of the DeviceFlowTable thus this method needs to get called
1052 // on every instance, see how it's done in the InternalDeviceListener
1053 // in FlowRuleManager: no mastership check for purgeOnDisconnection
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001054 handleDeviceDisconnection(dev, false, false);
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001055 return;
1056 } else if (!isLocalLeader) {
Andrea Campanella506df202020-05-21 10:26:12 +02001057 log.debug("Not handling event because instance is not leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001058 return;
1059 }
1060
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001061 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
1062
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001063 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001064 // it's possible that we got an event for a previously
1065 // programmed OLT that is no longer available in SADIS
1066 // we let such events go through
1067 if (!programmedDevices.contains(devId)) {
1068 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001069 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -08001070 return;
1071 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001072 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001073
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001074 switch (event.type()) {
1075 //TODO: Port handling and bookkeeping should be improved once
1076 // olt firmware handles correct behaviour.
1077 case PORT_ADDED:
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001078 if (!deviceService.isAvailable(devId)) {
1079 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1080 return;
1081 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001082 if (isUniPort(dev, port)) {
1083 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001084
1085 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1086 log.info("eapol will be sent for port added {}", port);
1087 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1088 null,
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001089 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1090 true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001091 }
1092 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001093 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1094 if (deviceInfo != null) {
1095 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1096 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001097 }
1098 break;
1099 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001100 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001101 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001102 log.info("eapol will be send for port {} removed", port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001103 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1104 null,
1105 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001106
Gamze Abaka838d8142019-02-21 07:06:55 +00001107 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001108 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001109 break;
1110 case PORT_UPDATED:
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001111 if (!deviceService.isAvailable(devId)) {
1112 log.warn("Received {} for disconnected device {}, ignoring", event, devId);
1113 return;
1114 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001115 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001116 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1117 if (deviceInfo != null && port.isEnabled()) {
1118 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1119 port.number());
1120 oltFlowService.processNniFilteringObjectives(dev.id(),
1121 port.number(), true);
1122 }
1123 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001124 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001125 ConnectPoint cp = new ConnectPoint(devId, port.number());
1126 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001127 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001128 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001129 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001130 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001131 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001132 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001133 null,
1134 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1135 port.isEnabled());
1136 }
1137 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001138 log.info("eapol will be {} for dev/port updated {}/{}",
1139 (port.isEnabled()) ? "added" : "removed",
1140 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001141 uniTagInformationSet.forEach(uniTag ->
1142 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1143 uniTag.getUpstreamBandwidthProfile(), null,
1144 uniTag.getPonCTag(), port.isEnabled()));
1145 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001146 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001147 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001148 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001149 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001150 }
alshabibbb83aa22016-02-10 15:08:23 -08001151 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001152 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001153 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001154 break;
1155 case DEVICE_REMOVED:
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001156 handleDeviceDisconnection(dev, true, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001157 break;
1158 case DEVICE_AVAILABILITY_CHANGED:
1159 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001160 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001161 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001162 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301163 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001164 log.info("Handling controlled device disconnection .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001165 + "flushing all state for dev:{}", devId);
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001166 handleDeviceDisconnection(dev, true, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001167 } else {
1168 log.info("Disconnected device has available ports .. "
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001169 + "assuming temporary disconnection, "
1170 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301171 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001172 }
1173 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001174 default:
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001175 log.debug("Not handling event {}", event);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001176 return;
1177 }
1178 });
alshabibf0e7e702015-05-30 18:22:36 -07001179 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001180
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001181 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1182 deviceService.getPorts(device.id()).stream()
1183 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1184 .filter(p -> isUniPort(device, p))
1185 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1186 }
1187
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001188 private void handleDeviceDisconnection(Device device, boolean sendDisconnectedEvent, boolean sendUniEvent) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001189 programmedDevices.remove(device.id());
1190 removeAllSubscribers(device.id());
Andrea Campanella600d2e22020-06-22 11:00:31 +02001191 //Handle case where OLT disconnects during subscriber provisioning
1192 pendingSubscribers.removeIf(fi -> fi.getDevId().equals(device.id()));
1193 oltFlowService.clearDeviceState(device.id());
1194
1195 //Complete meter and flow purge
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301196 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001197 oltMeterService.clearMeters(device.id());
Andrea Campanella73ec5bc2020-07-15 10:54:10 +02001198 if (sendDisconnectedEvent) {
1199 post(new AccessDeviceEvent(
1200 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1201 null, null, null));
1202 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001203 if (sendUniEvent) {
1204 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001205 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001206 }
1207
1208 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1209 post(new AccessDeviceEvent(
1210 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1211 null, null, null));
1212 programmedDevices.add(dev.id());
1213 checkAndCreateDeviceFlows(dev);
1214 if (sendUniEvent) {
1215 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1216 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001217 }
Gamze Abakada282b42019-03-11 13:16:48 +00001218
1219 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001220 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1221 .filter(e -> e.getKey().deviceId().equals(deviceId))
1222 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001223
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001224 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001225 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001226
Gamze Abaka641fc072018-09-04 09:16:27 +00001227 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001228
1229 private class InternalClusterListener implements ClusterEventListener {
1230
1231 @Override
1232 public void event(ClusterEvent event) {
1233 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1234 hasher.addServer(event.subject().id());
1235 }
1236 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1237 hasher.removeServer(event.subject().id());
1238 }
1239 }
1240 }
Andrea Campanella0c3309d2020-05-29 01:51:18 -07001241
Hardik Windlass395ff372019-06-13 05:16:00 +00001242}