blob: ebb27f83a7ad427438b2878ea7e1ffe727899438 [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;
19import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
20import static java.util.stream.Collectors.collectingAndThen;
21import static java.util.stream.Collectors.groupingBy;
22import static java.util.stream.Collectors.mapping;
23import static java.util.stream.Collectors.toList;
24import static java.util.stream.Collectors.toSet;
25import static org.onlab.util.Tools.get;
26import static org.onlab.util.Tools.groupedThreads;
27import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID;
28import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID_DEFAULT;
29import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_MCAST_SERVICE_NAME;
30import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
31import static org.slf4j.LoggerFactory.getLogger;
32
33import java.util.ArrayList;
34import java.util.Collection;
35import java.util.Dictionary;
36import java.util.List;
37import java.util.Map;
38import java.util.Optional;
39import java.util.Properties;
40import java.util.Set;
41import java.util.concurrent.CompletableFuture;
42import java.util.concurrent.ExecutorService;
43import java.util.concurrent.Executors;
44
alshabibf0e7e702015-05-30 18:22:36 -070045import org.onlab.packet.VlanId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080046import org.onlab.util.KryoNamespace;
Gamze Abaka1f62dd92020-05-07 08:58:13 +000047import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080048import org.onosproject.cluster.ClusterEvent;
49import org.onosproject.cluster.ClusterEventListener;
50import org.onosproject.cluster.ClusterService;
51import org.onosproject.cluster.ControllerNode;
52import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070053import org.onosproject.core.ApplicationId;
54import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080055import org.onosproject.event.AbstractListenerManager;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010056import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070057import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010058import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070059import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080060import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070061import org.onosproject.net.PortNumber;
62import org.onosproject.net.device.DeviceEvent;
63import org.onosproject.net.device.DeviceListener;
64import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053065import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070066import org.onosproject.net.flowobjective.FlowObjectiveService;
67import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080068import org.onosproject.net.flowobjective.Objective;
69import org.onosproject.net.flowobjective.ObjectiveContext;
70import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070071import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080072import org.onosproject.store.serializers.KryoNamespaces;
73import org.onosproject.store.service.ConsistentMultimap;
74import org.onosproject.store.service.Serializer;
75import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070076import org.opencord.olt.AccessDeviceEvent;
77import org.opencord.olt.AccessDeviceListener;
78import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010079import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000080import org.opencord.olt.internalapi.AccessDeviceFlowService;
81import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000082import org.opencord.sadis.BandwidthProfileInformation;
83import org.opencord.sadis.BaseInformationService;
84import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010085import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000086import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080087import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070088import org.osgi.service.component.annotations.Activate;
89import org.osgi.service.component.annotations.Component;
90import org.osgi.service.component.annotations.Deactivate;
91import org.osgi.service.component.annotations.Modified;
92import org.osgi.service.component.annotations.Reference;
93import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070094import org.slf4j.Logger;
95
Saurav Das9da7d522020-03-23 19:14:35 -070096import com.google.common.collect.ImmutableMap;
97import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -070098
99/**
Jonathan Harte533a422015-10-20 17:31:24 -0700100 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700101 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700102@Component(immediate = true,
103 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700104 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000105 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700106 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800107public class Olt
108 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
109 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800110 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800111
Gamze Abakada282b42019-03-11 13:16:48 +0000112 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000113 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800114
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800115 public static final int HASH_WEIGHT = 10;
116
alshabibf0e7e702015-05-30 18:22:36 -0700117 private final Logger log = getLogger(getClass());
118
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530119 private static final String NNI = "nni-";
120
Carmelo Casconeca931162019-07-15 18:22:24 -0700121 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700122 protected FlowObjectiveService flowObjectiveService;
123
Carmelo Casconeca931162019-07-15 18:22:24 -0700124 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700125 protected DeviceService deviceService;
126
Carmelo Casconeca931162019-07-15 18:22:24 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700128 protected CoreService coreService;
129
Carmelo Casconeca931162019-07-15 18:22:24 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000131 protected SadisService sadisService;
132
Carmelo Casconeca931162019-07-15 18:22:24 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000134 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800135
Carmelo Casconeca931162019-07-15 18:22:24 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000137 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000138
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800139 @Reference(cardinality = ReferenceCardinality.MANDATORY)
140 protected StorageService storageService;
141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
143 protected ClusterService clusterService;
144
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
146 protected FlowRuleService flowRuleService;
147
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
149 protected ComponentConfigService componentConfigService;
150
Carmelo Casconeca931162019-07-15 18:22:24 -0700151 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800152 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700153 **/
154 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000155
Carmelo Casconeca931162019-07-15 18:22:24 -0700156 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000157 * Deleting Meters based on flow count statistics.
Carmelo Casconeca931162019-07-15 18:22:24 -0700158 **/
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000159 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME;
Gamze Abaka33feef52019-02-27 08:16:47 +0000160
alshabibf0e7e702015-05-30 18:22:36 -0700161 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800162 private final ClusterEventListener clusterListener = new InternalClusterListener();
163
164 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700165
Gamze Abaka641fc072018-09-04 09:16:27 +0000166 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
167 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700168
Gamze Abaka641fc072018-09-04 09:16:27 +0000169 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000170 groupedThreads("onos/olt-service",
171 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100172
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700173 protected ExecutorService eventExecutor;
174
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800175 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800176
alshabibf0e7e702015-05-30 18:22:36 -0700177 @Activate
alshabibe0559672016-02-21 14:49:51 -0800178 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000179 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
180 "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800181 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000182 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000183 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800184
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800185 KryoNamespace serializer = KryoNamespace.newBuilder()
186 .register(KryoNamespaces.API)
187 .register(UniTagInformation.class)
188 .build();
189
190 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
191 .withName("volt-programmed-subs")
192 .withSerializer(Serializer.using(serializer))
193 .withApplicationId(appId)
194 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700195
alshabib8e4fd2f2016-01-12 15:55:53 -0800196 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
197
Gamze Abaka641fc072018-09-04 09:16:27 +0000198 subsService = sadisService.getSubscriberInfoService();
199 bpService = sadisService.getBandwidthProfileService();
200
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800201 List<NodeId> readyNodes = clusterService.getNodes().stream()
202 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
203 .map(ControllerNode::id)
204 .collect(toList());
205 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
206 clusterService.addListener(clusterListener);
207
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100208 // look for all provisioned devices in Sadis and create EAPOL flows for the
209 // UNI ports
210 Iterable<Device> devices = deviceService.getDevices();
211 for (Device d : devices) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800212 if (isDeviceMine(d.id())) {
213 checkAndCreateDeviceFlows(d);
214 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100215 }
alshabib4ceaed32016-03-03 18:00:58 -0800216
alshabibba357492016-01-27 13:49:46 -0800217 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700218 log.info("Started with Application ID {}", appId.id());
219 }
220
221 @Deactivate
222 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000223 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800224 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800225 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700226 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700227 log.info("Stopped");
228 }
229
alshabibe0559672016-02-21 14:49:51 -0800230 @Modified
231 public void modified(ComponentContext context) {
232 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
233
234 try {
Gamze Abakaad329652018-12-20 10:12:21 +0000235 String bpId = get(properties, "defaultBpId");
236 defaultBpId = bpId;
237
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000238 String mcastSN = get(properties, "multicastServiceName");
239 multicastServiceName = mcastSN;
240
241 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000242
alshabibe0559672016-02-21 14:49:51 -0800243 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000244 log.error("Error while modifying the properties", e);
alshabibe0559672016-02-21 14:49:51 -0800245 }
246 }
247
alshabib32232c82016-02-25 17:57:24 -0500248 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000249 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700250 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000251 DeviceId deviceId = connectPoint.deviceId();
252 PortNumber subscriberPortNo = connectPoint.port();
253
254 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000255 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000256
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100257 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000258 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100259 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000260 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100261 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100262 }
Jonathan Harte533a422015-10-20 17:31:24 -0700263
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100264 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000265 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100266 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000267 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100268 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700269 }
270
Gamze Abaka838d8142019-02-21 07:06:55 +0000271 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000272 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000273 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000274 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
275 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
276 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000277 filterFuture.thenAcceptAsync(filterStatus -> {
278 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000279 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Gamze Abaka33feef52019-02-27 08:16:47 +0000280 }
281 });
Amit Ghosh31939522018-08-16 13:28:21 +0100282 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800283 }
284
285 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000286 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700287 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000288
Saurav Daseae48de2019-06-19 13:26:15 -0700289 // Get the subscriber connected to this port from the local cache
290 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000291 DeviceId deviceId = connectPoint.deviceId();
292 PortNumber subscriberPortNo = connectPoint.port();
293
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800294 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000295 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000296 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000297 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800298 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800299 }
300
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100301 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000302 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100303 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000304 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100305 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800306 }
307
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000308 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100309
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000310 if (multicastServiceName.equals(uniTag.getServiceName())) {
311 continue;
312 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000313
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000314 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800315
Saurav Das9da7d522020-03-23 19:14:35 -0700316 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000317 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
318 uniTag.getUpstreamBandwidthProfile(),
319 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100320
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000321 Port port = deviceService.getPort(deviceId, subscriberPortNo);
322 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700323 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000324 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
325 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
326 } else {
327 log.debug("Port {} is no longer enabled or it's unavailable. Not "
328 + "reprogramming default eapol flow", connectPoint);
329 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100330 }
Amit Ghosh31939522018-08-16 13:28:21 +0100331 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800332 }
333
Gamze Abakaf59c0912019-04-19 08:24:28 +0000334
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000335 @Override
336 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
337 Optional<VlanId> cTag, Optional<Integer> tpId) {
338
339 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
340 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000341
Amit Ghosh31939522018-08-16 13:28:21 +0100342 // Check if we can find the connect point to which this subscriber is connected
343 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
344 if (subsPort == null) {
345 log.warn("ConnectPoint for {} not found", subscriberId);
346 return false;
347 }
348
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100349 if (!sTag.isPresent() && !cTag.isPresent()) {
350 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000351 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100352 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
353 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000354 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100355 return false;
356 }
357
Gamze Abakaf59c0912019-04-19 08:24:28 +0000358 //delete Eapol authentication flow with default bandwidth
359 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000360 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000361 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
362 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
363 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000364 filterFuture.thenAcceptAsync(filterStatus -> {
365 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000366 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
367 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000368 }
369 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100370 return true;
371 } else {
372 log.warn("Provisioning failed for subscriber: {}", subscriberId);
373 return false;
374 }
Amit Ghosh31939522018-08-16 13:28:21 +0100375 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100376
alshabibe0559672016-02-21 14:49:51 -0800377 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000378 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
379 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100380 // Check if we can find the connect point to which this subscriber is connected
381 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
382 if (subsPort == null) {
383 log.warn("ConnectPoint for {} not found", subscriberId);
384 return false;
385 }
386
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100387 if (!sTag.isPresent() && !cTag.isPresent()) {
388 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000389 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100390 // Get the uplink port
391 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
392 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000393 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100394 return false;
395 }
396
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000397 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
398 if (!tagInfo.isPresent()) {
399 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
400 subsPort, cTag, sTag, tpId);
401 return false;
402 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000403
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000404 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100405 return true;
406 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000407 log.warn("Removing subscriber is not possible - please check the provided information" +
408 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100409 return false;
410 }
Amit Ghosh31939522018-08-16 13:28:21 +0100411 }
412
413 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000414 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800415 return programmedSubs.stream()
416 .collect(collectingAndThen(
417 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
418 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700419 }
420
421 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100422 public List<DeviceId> fetchOlts() {
423 // look through all the devices and find the ones that are OLTs as per Sadis
424 List<DeviceId> olts = new ArrayList<>();
425 Iterable<Device> devices = deviceService.getDevices();
426 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700427 if (getOltInfo(d) != null) {
428 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100429 olts.add(d.id());
430 }
431 }
432 return olts;
alshabibe0559672016-02-21 14:49:51 -0800433 }
434
Amit Ghosh31939522018-08-16 13:28:21 +0100435 /**
436 * Finds the connect point to which a subscriber is connected.
437 *
438 * @param id The id of the subscriber, this is the same ID as in Sadis
439 * @return Subscribers ConnectPoint if found else null
440 */
441 private ConnectPoint findSubscriberConnectPoint(String id) {
442
443 Iterable<Device> devices = deviceService.getDevices();
444 for (Device d : devices) {
445 for (Port p : deviceService.getPorts(d.id())) {
446 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
447 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
448 log.debug("Found on device {} port {}", d.id(), p.number());
449 return new ConnectPoint(d.id(), p.number());
450 }
451 }
452 }
453 return null;
454 }
455
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000456 /**
457 * Gets the context of the bandwidth profile information for the given parameter.
458 *
459 * @param bandwidthProfile the bandwidth profile id
460 * @return the context of the bandwidth profile information
461 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000462 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
463 if (bandwidthProfile == null) {
464 return null;
465 }
466 return bpService.get(bandwidthProfile);
467 }
468
Gamze Abaka838d8142019-02-21 07:06:55 +0000469 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000470 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000471 *
472 * @param deviceId the device identifier
473 * @param uplink uplink port of the OLT
474 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000475 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000476 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000477 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000478 PortNumber subscriberPort, UniTagInformation uniTag) {
479
480 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800481
482 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
483 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
484
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000485 VlanId deviceVlan = uniTag.getPonSTag();
486 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000487
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000488 MeterId upstreamMeterId = oltMeterService
489 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
490 MeterId downstreamMeterId = oltMeterService
491 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000492
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000493 ForwardingObjective.Builder upFwd =
494 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
495 ForwardingObjective.Builder downFwd =
496 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
497
498 if (uniTag.getIsIgmpRequired()) {
499 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
500 upstreamMeterId, uniTag, false, true);
501 }
502 if (uniTag.getIsDhcpRequired()) {
503 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
504 upstreamMeterId, uniTag, false, true);
505 }
alshabibbf23a1f2016-01-14 17:27:11 -0800506
alshabib4ceaed32016-03-03 18:00:58 -0800507 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
508 @Override
509 public void onSuccess(Objective objective) {
510 upFuture.complete(null);
511 }
alshabibbf23a1f2016-01-14 17:27:11 -0800512
alshabib4ceaed32016-03-03 18:00:58 -0800513 @Override
514 public void onError(Objective objective, ObjectiveError error) {
515 upFuture.complete(error);
516 }
517 }));
518
519 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
520 @Override
521 public void onSuccess(Objective objective) {
522 downFuture.complete(null);
523 }
524
525 @Override
526 public void onError(Objective objective, ObjectiveError error) {
527 downFuture.complete(error);
528 }
529 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800530
531 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000532 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800533 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000534 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
535 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
536 uniTag.getTechnologyProfileId(), subscriberPort);
537 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800538 } else if (downStatus != null) {
539 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000540 "on port {} failed downstream uninstallation: {}",
541 subscriberVlan, deviceId, subscriberPort, downStatus);
542 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800543 } else if (upStatus != null) {
544 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000545 "on port {} failed upstream uninstallation: {}",
546 subscriberVlan, deviceId, subscriberPort, upStatus);
547 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800548 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000549 Port port = deviceService.getPort(deviceId, subscriberPort);
550 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
551 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800552 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700553 }
554
Gamze Abaka838d8142019-02-21 07:06:55 +0000555 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000556 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000557 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000558 * @param connectPoint the connection point of the subscriber
559 * @param uplinkPort uplink port of the OLT (the nni port)
560 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000561 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000562 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
563 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000564
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000565 log.info("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000566
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000567 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
568 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
569 return;
570 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000571
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000572 DeviceId deviceId = connectPoint.deviceId();
573 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000574
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000575 for (UniTagInformation uniTag : sub.uniTagList()) {
576 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
577 }
578 }
alshabib3ea82642016-01-12 18:06:53 -0800579
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000580 /**
581 * Finds the uni tag information and provisions the found information.
582 * If the uni tag information is not found, returns
583 *
584 * @param deviceId the access device id
585 * @param uplinkPort the nni port
586 * @param subscriberPort the uni port
587 * @param innerVlan the pon c tag
588 * @param outerVlan the pon s tag
589 * @param tpId the technology profile id
590 */
591 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
592 PortNumber subscriberPort,
593 VlanId innerVlan,
594 VlanId outerVlan,
595 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700596
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000597 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
598 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
599 if (!gotTagInformation.isPresent()) {
600 return;
601 }
602 UniTagInformation tagInformation = gotTagInformation.get();
603 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
604 }
alshabib3ea82642016-01-12 18:06:53 -0800605
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000606 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800607 if (add) {
608 programmedSubs.put(connectPoint, tagInformation);
609 } else {
610 programmedSubs.remove(connectPoint, tagInformation);
611 }
Jonathan Harte533a422015-10-20 17:31:24 -0700612 }
613
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000614 /**
615 * Installs a uni tag information flow.
616 *
617 * @param deviceId the access device id
618 * @param uplinkPort the nni port
619 * @param subscriberPort the uni port
620 * @param tagInfo the uni tag information
621 */
622 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
623 UniTagInformation tagInfo) {
624
625 log.info("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
626
627 Port port = deviceService.getPort(deviceId, subscriberPort);
628
629 if (multicastServiceName.equals(tagInfo.getServiceName())) {
630 // IGMP flows are taken care of along with VOD service
631 // Please note that for each service, Subscriber Registered event will be sent
632 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
633 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
634 tagInfo.getTechnologyProfileId()));
635 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000636 }
637
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100638 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
639
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000640 BandwidthProfileInformation upstreamBpInfo =
641 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
642 BandwidthProfileInformation downstreamBpInfo =
643 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000644
645 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
646 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000647 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
648 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000649
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000650 MeterId upstreamMeterId = oltMeterService.createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
651
652 MeterId downstreamMeterId = oltMeterService.createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000653
654 upstreamMeterFuture.thenAcceptAsync(result -> {
655 if (result == null) {
656 log.info("Upstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000657 "Sending subscriber flows.", upstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000658
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000659 ForwardingObjective.Builder upFwd =
660 oltFlowService.createUpBuilder(uplinkPort, subscriberPort, upstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000661
662 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
663 @Override
664 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000665 log.info("Upstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000666 upFuture.complete(null);
667 }
668
669 @Override
670 public void onError(Objective objective, ObjectiveError error) {
671 upFuture.complete(error);
672 }
673 }));
674
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000675 } else if (upstreamBpInfo == null) {
676 log.warn("No meter installed since no Upstream BW Profile definition found for " +
677 "ctag {} stag {} tpId {} and Device/port: {}:{}",
678 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
679 tagInfo.getTechnologyProfileId(),
680 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000681 } else {
682 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000683 "Result {} and MeterId {}", result, upstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000684 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800685 }).exceptionally(ex -> {
686 log.error("Upstream flow failed: " + ex.getMessage());
687 upFuture.complete(ObjectiveError.UNKNOWN);
688 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000689 });
690
691 downsteamMeterFuture.thenAcceptAsync(result -> {
692 if (result == null) {
693 log.info("Downstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000694 "Sending subscriber flows.", downstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000695
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000696 ForwardingObjective.Builder downFwd =
697 oltFlowService.createDownBuilder(uplinkPort, subscriberPort, downstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000698
699 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
700 @Override
701 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000702 log.info("Downstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000703 downFuture.complete(null);
704 }
705
706 @Override
707 public void onError(Objective objective, ObjectiveError error) {
708 downFuture.complete(error);
709 }
710 }));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000711
712 } else if (downstreamBpInfo == null) {
713 log.warn("No meter installed since no Downstream BW Profile definition found for " +
714 "ctag {} stag {} tpId {} and Device/port: {}:{}",
715 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
716 tagInfo.getTechnologyProfileId(),
717 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000718 } else {
719 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000720 "Result {} and MeterId {}", result, downstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000721 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800722 }).exceptionally(ex -> {
723 log.error("Downstream flow failed: " + ex.getMessage());
724 downFuture.complete(ObjectiveError.UNKNOWN);
725 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000726 });
727
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100728 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000729 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100730 if (downStatus != null) {
731 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000732 "on port {} failed downstream installation: {}",
733 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, downStatus);
734 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100735 } else if (upStatus != null) {
736 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000737 "on port {} failed upstream installation: {}",
738 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, upStatus);
739 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000740 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000741 log.info("Upstream and downstream data plane flows are installed successfully.");
742 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPort,
743 tagInfo.getUpstreamBandwidthProfile(),
744 null, tagInfo.getPonCTag(), true);
745 if (tagInfo.getIsDhcpRequired()) {
746 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
747 upstreamMeterId, tagInfo, true, true);
748 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000749
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000750 if (tagInfo.getIsIgmpRequired()) {
751 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort, upstreamMeterId, tagInfo,
752 true, true);
753 }
754 updateProgrammedSubscriber(cp, tagInfo, true);
755 post(new AccessDeviceEvent(type, deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
756 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100757 }
758 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100759 }
760
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000761 /**
762 * Checks the subscriber uni tag list and find the uni tag information.
763 * using the pon c tag, pon s tag and the technology profile id
764 * May return Optional<null>
765 *
766 * @param cp the connection point of the subscriber
767 * @param innerVlan pon c tag
768 * @param outerVlan pon s tag
769 * @param tpId the technology profile id
770 * @return the found uni tag information
771 */
772 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
773 int tpId) {
774 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
775 outerVlan, tpId);
776 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000777 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000778 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
779 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000780 }
781
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000782 List<UniTagInformation> uniTagList = subInfo.uniTagList();
783 if (uniTagList == null) {
784 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
785 return Optional.empty();
786 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100787
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000788 UniTagInformation service = null;
789 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
790 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
791 && tpId == tagInfo.getTechnologyProfileId()) {
792 service = tagInfo;
793 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700794 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000795 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000796
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000797 if (service == null) {
798 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
799 innerVlan, outerVlan, tpId);
800 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000801 }
802
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000803 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100804 }
805
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100806 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700807 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
808 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100809 *
810 * @param dev Device to look for
811 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700812 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100813 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000814 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000815 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100816
817 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700818 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100819 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800820 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000821 continue;
822 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100823 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100824 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
825 log.info("Creating Eapol for the uni {}", p);
826 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
827 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
828 } else {
829 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
830 "provisioned, not installing default", p.number(), dev.id());
831 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700832 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000833 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100834 }
835 }
836 }
837 }
838
Jonathan Hart403372d2018-08-22 11:44:13 -0700839
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100840 /**
841 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000842 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100843 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
844 * this logic needs to be changed
845 *
846 * @param dev Device to look for
847 * @return The uplink Port of the OLT
848 */
849 private Port getUplinkPort(Device dev) {
850 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000851 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700852 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700853 if (deviceInfo == null) {
854 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000855 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700856 return null;
857 }
858 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +0530859 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
860 .filter(port -> isNniPort(port) ||
861 (port.number().toLong() == deviceInfo.uplinkPort()))
862 .findFirst();
863 if (optionalPort.isPresent()) {
864 log.trace("getUplinkPort: Found port {}", optionalPort.get());
865 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100866 }
867
Saurav Daseae48de2019-06-19 13:26:15 -0700868 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100869 return null;
870 }
871
872 /**
873 * Return the subscriber on a port.
874 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800875 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100876 * @return subscriber if found else null
877 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800878 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
879 Port port = deviceService.getPort(cp);
880 checkNotNull(port, "Invalid connect point");
881 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100882 return subsService.get(portName);
883 }
884
Gamze Abakaad329652018-12-20 10:12:21 +0000885 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000886 * Checks whether the given port of the device is a uni port or not.
887 *
888 * @param d the access device
889 * @param p the port of the device
890 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000891 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100892 private boolean isUniPort(Device d, Port p) {
893 Port ulPort = getUplinkPort(d);
894 if (ulPort != null) {
895 return (ulPort.number().toLong() != p.number().toLong());
896 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530897 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
898 //checks whether the port name starts with nni- which is the signature of an NNI Port
899 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
900 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
901 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
902 return false;
903 }
904 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700905 }
906
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000907 /**
908 * Gets the given device details from SADIS.
909 * If the device is not found, returns null
910 *
911 * @param dev the access device
912 * @return the olt information
913 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700914 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
915 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +0000916 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -0700917 }
918
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800919 /**
920 * Determines if this instance should handle this device based on
921 * consistent hashing.
922 *
923 * @param id device ID
924 * @return true if this instance should handle the device, otherwise false
925 */
926 private boolean isDeviceMine(DeviceId id) {
927 NodeId nodeId = hasher.hash(id.toString());
928 if (log.isDebugEnabled()) {
929 log.debug("Node that will handle {} is {}", id, nodeId);
930 }
931 return nodeId.equals(clusterService.getLocalNode().id());
932 }
933
kdarapuaa5da252020-04-10 15:58:05 +0530934 private boolean isNniPort(Port port) {
935 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
936 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
937 }
938 return false;
939 }
940
alshabibf0e7e702015-05-30 18:22:36 -0700941 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800942 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
943
alshabibf0e7e702015-05-30 18:22:36 -0700944 @Override
945 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700946 eventExecutor.execute(() -> {
947 DeviceId devId = event.subject().id();
948 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +0000949 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000950 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -0700951
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000952 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
953 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
954 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700955 return;
956 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700957
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800958 // Only handle the event if the device belongs to us
959 if (!isDeviceMine(event.subject().id())) {
960 return;
961 }
962
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000963 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
964
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700965 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800966 // it's possible that we got an event for a previously
967 // programmed OLT that is no longer available in SADIS
968 // we let such events go through
969 if (!programmedDevices.contains(devId)) {
970 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000971 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -0800972 return;
973 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700974 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700975
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700976 switch (event.type()) {
977 //TODO: Port handling and bookkeeping should be improved once
978 // olt firmware handles correct behaviour.
979 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000980 if (isUniPort(dev, port)) {
981 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000982
983 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
984 log.info("eapol will be sent for port added {}", port);
985 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
986 null,
987 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700988 }
989 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000990 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
991 if (deviceInfo != null) {
992 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
993 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700994 }
995 break;
996 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000997 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +0000998 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000999 log.info("eapol will be send for port removed", port);
1000 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1001 null,
1002 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001003
Gamze Abaka838d8142019-02-21 07:06:55 +00001004 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001005 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001006 break;
1007 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001008 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001009 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1010 if (deviceInfo != null && port.isEnabled()) {
1011 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1012 port.number());
1013 oltFlowService.processNniFilteringObjectives(dev.id(),
1014 port.number(), true);
1015 }
1016 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001017 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001018 ConnectPoint cp = new ConnectPoint(devId, port.number());
1019 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001020 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001021 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001022 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001023 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001024 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001025 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001026 null,
1027 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1028 port.isEnabled());
1029 }
1030 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001031 log.info("eapol will be {} for dev/port updated {}/{}",
1032 (port.isEnabled()) ? "added" : "removed",
1033 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001034 uniTagInformationSet.forEach(uniTag ->
1035 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1036 uniTag.getUpstreamBandwidthProfile(), null,
1037 uniTag.getPonCTag(), port.isEnabled()));
1038 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001039 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001040 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001041 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001042 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001043 }
alshabibbb83aa22016-02-10 15:08:23 -08001044 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001045 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001046 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001047 break;
1048 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001049 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001050 break;
1051 case DEVICE_AVAILABILITY_CHANGED:
1052 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001053 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001054 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001055 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301056 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001057 log.info("Handling controlled device disconnection .. "
1058 + "flushing all state for dev:{}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301059 handleDeviceDisconnection(dev, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001060 } else {
1061 log.info("Disconnected device has available ports .. "
1062 + "assuming temporary disconnection, "
1063 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301064 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001065 }
1066 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001067 default:
1068 return;
1069 }
1070 });
alshabibf0e7e702015-05-30 18:22:36 -07001071 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001072
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001073 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1074 deviceService.getPorts(device.id()).stream()
1075 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1076 .filter(p -> isUniPort(device, p))
1077 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1078 }
1079
1080 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
1081 programmedDevices.remove(device.id());
1082 removeAllSubscribers(device.id());
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301083 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001084 oltMeterService.clearMeters(device.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001085 post(new AccessDeviceEvent(
1086 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1087 null, null, null));
1088 if (sendUniEvent) {
1089 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001090 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001091 }
1092
1093 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1094 post(new AccessDeviceEvent(
1095 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1096 null, null, null));
1097 programmedDevices.add(dev.id());
1098 checkAndCreateDeviceFlows(dev);
1099 if (sendUniEvent) {
1100 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1101 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001102 }
Gamze Abakada282b42019-03-11 13:16:48 +00001103
1104 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001105 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1106 .filter(e -> e.getKey().deviceId().equals(deviceId))
1107 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001108
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001109 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001110 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001111
Gamze Abaka641fc072018-09-04 09:16:27 +00001112 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001113
1114 private class InternalClusterListener implements ClusterEventListener {
1115
1116 @Override
1117 public void event(ClusterEvent event) {
1118 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1119 hasher.addServer(event.subject().id());
1120 }
1121 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1122 hasher.removeServer(event.subject().id());
1123 }
1124 }
1125 }
Hardik Windlass395ff372019-06-13 05:16:00 +00001126}