blob: 37aeafbe1786f6db1f0d6fd3c6e32dd30224de16 [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;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080047import org.onosproject.cluster.ClusterEvent;
48import org.onosproject.cluster.ClusterEventListener;
49import org.onosproject.cluster.ClusterService;
50import org.onosproject.cluster.ControllerNode;
51import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070052import org.onosproject.core.ApplicationId;
53import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080054import org.onosproject.event.AbstractListenerManager;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010055import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070056import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010057import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070058import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080059import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070060import org.onosproject.net.PortNumber;
61import org.onosproject.net.device.DeviceEvent;
62import org.onosproject.net.device.DeviceListener;
63import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053064import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070065import org.onosproject.net.flowobjective.FlowObjectiveService;
66import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080067import org.onosproject.net.flowobjective.Objective;
68import org.onosproject.net.flowobjective.ObjectiveContext;
69import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070070import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080071import org.onosproject.store.serializers.KryoNamespaces;
72import org.onosproject.store.service.ConsistentMultimap;
73import org.onosproject.store.service.Serializer;
74import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070075import org.opencord.olt.AccessDeviceEvent;
76import org.opencord.olt.AccessDeviceListener;
77import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010078import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000079import org.opencord.olt.internalapi.AccessDeviceFlowService;
80import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000081import org.opencord.sadis.BandwidthProfileInformation;
82import org.opencord.sadis.BaseInformationService;
83import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010084import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000085import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080086import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070087import org.osgi.service.component.annotations.Activate;
88import org.osgi.service.component.annotations.Component;
89import org.osgi.service.component.annotations.Deactivate;
90import org.osgi.service.component.annotations.Modified;
91import org.osgi.service.component.annotations.Reference;
92import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070093import org.slf4j.Logger;
94
Saurav Das9da7d522020-03-23 19:14:35 -070095import com.google.common.collect.ImmutableMap;
96import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -070097
98/**
Jonathan Harte533a422015-10-20 17:31:24 -070099 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700100 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700101@Component(immediate = true,
102 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700103 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000104 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700105 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800106public class Olt
107 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
108 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800109 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800110
Gamze Abakada282b42019-03-11 13:16:48 +0000111 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000112 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800113
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800114 public static final int HASH_WEIGHT = 10;
115
alshabibf0e7e702015-05-30 18:22:36 -0700116 private final Logger log = getLogger(getClass());
117
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530118 private static final String NNI = "nni-";
119
Carmelo Casconeca931162019-07-15 18:22:24 -0700120 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700121 protected FlowObjectiveService flowObjectiveService;
122
Carmelo Casconeca931162019-07-15 18:22:24 -0700123 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700124 protected DeviceService deviceService;
125
Carmelo Casconeca931162019-07-15 18:22:24 -0700126 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700127 protected CoreService coreService;
128
Carmelo Casconeca931162019-07-15 18:22:24 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000130 protected SadisService sadisService;
131
Carmelo Casconeca931162019-07-15 18:22:24 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000133 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800134
Carmelo Casconeca931162019-07-15 18:22:24 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000136 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000137
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
139 protected StorageService storageService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
142 protected ClusterService clusterService;
143
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
145 protected FlowRuleService flowRuleService;
146
Carmelo Casconeca931162019-07-15 18:22:24 -0700147 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800148 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700149 **/
150 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000151
Carmelo Casconeca931162019-07-15 18:22:24 -0700152 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000153 * Deleting Meters based on flow count statistics.
Carmelo Casconeca931162019-07-15 18:22:24 -0700154 **/
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000155 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME;
Gamze Abaka33feef52019-02-27 08:16:47 +0000156
alshabibf0e7e702015-05-30 18:22:36 -0700157 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800158 private final ClusterEventListener clusterListener = new InternalClusterListener();
159
160 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700161
Gamze Abaka641fc072018-09-04 09:16:27 +0000162 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
163 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700164
Gamze Abaka641fc072018-09-04 09:16:27 +0000165 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000166 groupedThreads("onos/olt-service",
167 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100168
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700169 protected ExecutorService eventExecutor;
170
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800171 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800172
alshabibf0e7e702015-05-30 18:22:36 -0700173 @Activate
alshabibe0559672016-02-21 14:49:51 -0800174 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000175 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
176 "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800177 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000178 ApplicationId appId = coreService.registerApplication(APP_NAME);
Saurav Das62ad75e2019-03-05 12:22:22 -0800179
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800180 KryoNamespace serializer = KryoNamespace.newBuilder()
181 .register(KryoNamespaces.API)
182 .register(UniTagInformation.class)
183 .build();
184
185 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
186 .withName("volt-programmed-subs")
187 .withSerializer(Serializer.using(serializer))
188 .withApplicationId(appId)
189 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700190
alshabib8e4fd2f2016-01-12 15:55:53 -0800191 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
192
Gamze Abaka641fc072018-09-04 09:16:27 +0000193 subsService = sadisService.getSubscriberInfoService();
194 bpService = sadisService.getBandwidthProfileService();
195
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800196 List<NodeId> readyNodes = clusterService.getNodes().stream()
197 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
198 .map(ControllerNode::id)
199 .collect(toList());
200 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
201 clusterService.addListener(clusterListener);
202
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100203 // look for all provisioned devices in Sadis and create EAPOL flows for the
204 // UNI ports
205 Iterable<Device> devices = deviceService.getDevices();
206 for (Device d : devices) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800207 if (isDeviceMine(d.id())) {
208 checkAndCreateDeviceFlows(d);
209 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100210 }
alshabib4ceaed32016-03-03 18:00:58 -0800211
alshabibba357492016-01-27 13:49:46 -0800212 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700213 log.info("Started with Application ID {}", appId.id());
214 }
215
216 @Deactivate
217 public void deactivate() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800218 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800219 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700220 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700221 log.info("Stopped");
222 }
223
alshabibe0559672016-02-21 14:49:51 -0800224 @Modified
225 public void modified(ComponentContext context) {
226 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
227
228 try {
Gamze Abakaad329652018-12-20 10:12:21 +0000229 String bpId = get(properties, "defaultBpId");
230 defaultBpId = bpId;
231
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000232 String mcastSN = get(properties, "multicastServiceName");
233 multicastServiceName = mcastSN;
234
235 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000236
alshabibe0559672016-02-21 14:49:51 -0800237 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000238 log.error("Error while modifying the properties", e);
alshabibe0559672016-02-21 14:49:51 -0800239 }
240 }
241
alshabib32232c82016-02-25 17:57:24 -0500242 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000243 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700244 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000245 DeviceId deviceId = connectPoint.deviceId();
246 PortNumber subscriberPortNo = connectPoint.port();
247
248 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000249 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000250
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100251 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000252 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100253 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000254 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100255 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100256 }
Jonathan Harte533a422015-10-20 17:31:24 -0700257
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100258 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000259 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100260 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000261 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100262 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700263 }
264
Gamze Abaka838d8142019-02-21 07:06:55 +0000265 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000266 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000267 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000268 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
269 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
270 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000271 filterFuture.thenAcceptAsync(filterStatus -> {
272 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000273 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Gamze Abaka33feef52019-02-27 08:16:47 +0000274 }
275 });
Amit Ghosh31939522018-08-16 13:28:21 +0100276 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800277 }
278
279 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000280 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700281 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000282
Saurav Daseae48de2019-06-19 13:26:15 -0700283 // Get the subscriber connected to this port from the local cache
284 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000285 DeviceId deviceId = connectPoint.deviceId();
286 PortNumber subscriberPortNo = connectPoint.port();
287
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800288 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000289 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000290 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000291 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800292 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800293 }
294
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100295 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000296 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100297 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000298 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100299 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800300 }
301
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000302 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100303
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000304 if (multicastServiceName.equals(uniTag.getServiceName())) {
305 continue;
306 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000307
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000308 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800309
Saurav Das9da7d522020-03-23 19:14:35 -0700310 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000311 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
312 uniTag.getUpstreamBandwidthProfile(),
313 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100314
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000315 Port port = deviceService.getPort(deviceId, subscriberPortNo);
316 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700317 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000318 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
319 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
320 } else {
321 log.debug("Port {} is no longer enabled or it's unavailable. Not "
322 + "reprogramming default eapol flow", connectPoint);
323 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100324 }
Amit Ghosh31939522018-08-16 13:28:21 +0100325 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800326 }
327
Gamze Abakaf59c0912019-04-19 08:24:28 +0000328
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000329 @Override
330 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
331 Optional<VlanId> cTag, Optional<Integer> tpId) {
332
333 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
334 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000335
Amit Ghosh31939522018-08-16 13:28:21 +0100336 // Check if we can find the connect point to which this subscriber is connected
337 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
338 if (subsPort == null) {
339 log.warn("ConnectPoint for {} not found", subscriberId);
340 return false;
341 }
342
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100343 if (!sTag.isPresent() && !cTag.isPresent()) {
344 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000345 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100346 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
347 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000348 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100349 return false;
350 }
351
Gamze Abakaf59c0912019-04-19 08:24:28 +0000352 //delete Eapol authentication flow with default bandwidth
353 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000354 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000355 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
356 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
357 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000358 filterFuture.thenAcceptAsync(filterStatus -> {
359 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000360 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
361 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000362 }
363 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100364 return true;
365 } else {
366 log.warn("Provisioning failed for subscriber: {}", subscriberId);
367 return false;
368 }
Amit Ghosh31939522018-08-16 13:28:21 +0100369 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100370
alshabibe0559672016-02-21 14:49:51 -0800371 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000372 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
373 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100374 // Check if we can find the connect point to which this subscriber is connected
375 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
376 if (subsPort == null) {
377 log.warn("ConnectPoint for {} not found", subscriberId);
378 return false;
379 }
380
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100381 if (!sTag.isPresent() && !cTag.isPresent()) {
382 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000383 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100384 // Get the uplink port
385 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
386 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000387 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100388 return false;
389 }
390
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000391 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
392 if (!tagInfo.isPresent()) {
393 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
394 subsPort, cTag, sTag, tpId);
395 return false;
396 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000397
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000398 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100399 return true;
400 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000401 log.warn("Removing subscriber is not possible - please check the provided information" +
402 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100403 return false;
404 }
Amit Ghosh31939522018-08-16 13:28:21 +0100405 }
406
407 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000408 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800409 return programmedSubs.stream()
410 .collect(collectingAndThen(
411 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
412 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700413 }
414
415 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100416 public List<DeviceId> fetchOlts() {
417 // look through all the devices and find the ones that are OLTs as per Sadis
418 List<DeviceId> olts = new ArrayList<>();
419 Iterable<Device> devices = deviceService.getDevices();
420 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700421 if (getOltInfo(d) != null) {
422 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100423 olts.add(d.id());
424 }
425 }
426 return olts;
alshabibe0559672016-02-21 14:49:51 -0800427 }
428
Amit Ghosh31939522018-08-16 13:28:21 +0100429 /**
430 * Finds the connect point to which a subscriber is connected.
431 *
432 * @param id The id of the subscriber, this is the same ID as in Sadis
433 * @return Subscribers ConnectPoint if found else null
434 */
435 private ConnectPoint findSubscriberConnectPoint(String id) {
436
437 Iterable<Device> devices = deviceService.getDevices();
438 for (Device d : devices) {
439 for (Port p : deviceService.getPorts(d.id())) {
440 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
441 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
442 log.debug("Found on device {} port {}", d.id(), p.number());
443 return new ConnectPoint(d.id(), p.number());
444 }
445 }
446 }
447 return null;
448 }
449
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000450 /**
451 * Gets the context of the bandwidth profile information for the given parameter.
452 *
453 * @param bandwidthProfile the bandwidth profile id
454 * @return the context of the bandwidth profile information
455 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000456 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
457 if (bandwidthProfile == null) {
458 return null;
459 }
460 return bpService.get(bandwidthProfile);
461 }
462
Gamze Abaka838d8142019-02-21 07:06:55 +0000463 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000464 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000465 *
466 * @param deviceId the device identifier
467 * @param uplink uplink port of the OLT
468 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000469 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000470 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000471 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000472 PortNumber subscriberPort, UniTagInformation uniTag) {
473
474 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800475
476 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
477 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
478
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000479 VlanId deviceVlan = uniTag.getPonSTag();
480 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000481
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000482 MeterId upstreamMeterId = oltMeterService
483 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
484 MeterId downstreamMeterId = oltMeterService
485 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000486
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000487 ForwardingObjective.Builder upFwd =
488 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
489 ForwardingObjective.Builder downFwd =
490 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
491
492 if (uniTag.getIsIgmpRequired()) {
493 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
494 upstreamMeterId, uniTag, false, true);
495 }
496 if (uniTag.getIsDhcpRequired()) {
497 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
498 upstreamMeterId, uniTag, false, true);
499 }
alshabibbf23a1f2016-01-14 17:27:11 -0800500
alshabib4ceaed32016-03-03 18:00:58 -0800501 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
502 @Override
503 public void onSuccess(Objective objective) {
504 upFuture.complete(null);
505 }
alshabibbf23a1f2016-01-14 17:27:11 -0800506
alshabib4ceaed32016-03-03 18:00:58 -0800507 @Override
508 public void onError(Objective objective, ObjectiveError error) {
509 upFuture.complete(error);
510 }
511 }));
512
513 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
514 @Override
515 public void onSuccess(Objective objective) {
516 downFuture.complete(null);
517 }
518
519 @Override
520 public void onError(Objective objective, ObjectiveError error) {
521 downFuture.complete(error);
522 }
523 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800524
525 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000526 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800527 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000528 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
529 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
530 uniTag.getTechnologyProfileId(), subscriberPort);
531 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800532 } else if (downStatus != null) {
533 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000534 "on port {} failed downstream uninstallation: {}",
535 subscriberVlan, deviceId, subscriberPort, downStatus);
536 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800537 } else if (upStatus != null) {
538 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000539 "on port {} failed upstream uninstallation: {}",
540 subscriberVlan, deviceId, subscriberPort, upStatus);
541 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800542 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000543 Port port = deviceService.getPort(deviceId, subscriberPort);
544 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
545 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800546 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700547 }
548
Gamze Abaka838d8142019-02-21 07:06:55 +0000549 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000550 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000551 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000552 * @param connectPoint the connection point of the subscriber
553 * @param uplinkPort uplink port of the OLT (the nni port)
554 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000555 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000556 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
557 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000558
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000559 log.info("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000560
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000561 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
562 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
563 return;
564 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000565
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000566 DeviceId deviceId = connectPoint.deviceId();
567 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000568
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000569 for (UniTagInformation uniTag : sub.uniTagList()) {
570 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
571 }
572 }
alshabib3ea82642016-01-12 18:06:53 -0800573
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000574 /**
575 * Finds the uni tag information and provisions the found information.
576 * If the uni tag information is not found, returns
577 *
578 * @param deviceId the access device id
579 * @param uplinkPort the nni port
580 * @param subscriberPort the uni port
581 * @param innerVlan the pon c tag
582 * @param outerVlan the pon s tag
583 * @param tpId the technology profile id
584 */
585 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
586 PortNumber subscriberPort,
587 VlanId innerVlan,
588 VlanId outerVlan,
589 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700590
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000591 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
592 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
593 if (!gotTagInformation.isPresent()) {
594 return;
595 }
596 UniTagInformation tagInformation = gotTagInformation.get();
597 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
598 }
alshabib3ea82642016-01-12 18:06:53 -0800599
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000600 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800601 if (add) {
602 programmedSubs.put(connectPoint, tagInformation);
603 } else {
604 programmedSubs.remove(connectPoint, tagInformation);
605 }
Jonathan Harte533a422015-10-20 17:31:24 -0700606 }
607
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000608 /**
609 * Installs a uni tag information flow.
610 *
611 * @param deviceId the access device id
612 * @param uplinkPort the nni port
613 * @param subscriberPort the uni port
614 * @param tagInfo the uni tag information
615 */
616 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
617 UniTagInformation tagInfo) {
618
619 log.info("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
620
621 Port port = deviceService.getPort(deviceId, subscriberPort);
622
623 if (multicastServiceName.equals(tagInfo.getServiceName())) {
624 // IGMP flows are taken care of along with VOD service
625 // Please note that for each service, Subscriber Registered event will be sent
626 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
627 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
628 tagInfo.getTechnologyProfileId()));
629 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000630 }
631
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100632 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
633
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000634 BandwidthProfileInformation upstreamBpInfo =
635 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
636 BandwidthProfileInformation downstreamBpInfo =
637 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000638
639 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
640 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000641 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
642 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000643
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000644 MeterId upstreamMeterId = oltMeterService.createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
645
646 MeterId downstreamMeterId = oltMeterService.createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000647
648 upstreamMeterFuture.thenAcceptAsync(result -> {
649 if (result == null) {
650 log.info("Upstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000651 "Sending subscriber flows.", upstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000652
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000653 ForwardingObjective.Builder upFwd =
654 oltFlowService.createUpBuilder(uplinkPort, subscriberPort, upstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000655
656 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
657 @Override
658 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000659 log.info("Upstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000660 upFuture.complete(null);
661 }
662
663 @Override
664 public void onError(Objective objective, ObjectiveError error) {
665 upFuture.complete(error);
666 }
667 }));
668
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000669 } else if (upstreamBpInfo == null) {
670 log.warn("No meter installed since no Upstream BW Profile definition found for " +
671 "ctag {} stag {} tpId {} and Device/port: {}:{}",
672 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
673 tagInfo.getTechnologyProfileId(),
674 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000675 } else {
676 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000677 "Result {} and MeterId {}", result, upstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000678 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800679 }).exceptionally(ex -> {
680 log.error("Upstream flow failed: " + ex.getMessage());
681 upFuture.complete(ObjectiveError.UNKNOWN);
682 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000683 });
684
685 downsteamMeterFuture.thenAcceptAsync(result -> {
686 if (result == null) {
687 log.info("Downstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000688 "Sending subscriber flows.", downstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000689
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000690 ForwardingObjective.Builder downFwd =
691 oltFlowService.createDownBuilder(uplinkPort, subscriberPort, downstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000692
693 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
694 @Override
695 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000696 log.info("Downstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000697 downFuture.complete(null);
698 }
699
700 @Override
701 public void onError(Objective objective, ObjectiveError error) {
702 downFuture.complete(error);
703 }
704 }));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000705
706 } else if (downstreamBpInfo == null) {
707 log.warn("No meter installed since no Downstream BW Profile definition found for " +
708 "ctag {} stag {} tpId {} and Device/port: {}:{}",
709 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
710 tagInfo.getTechnologyProfileId(),
711 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000712 } else {
713 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000714 "Result {} and MeterId {}", result, downstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000715 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800716 }).exceptionally(ex -> {
717 log.error("Downstream flow failed: " + ex.getMessage());
718 downFuture.complete(ObjectiveError.UNKNOWN);
719 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000720 });
721
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100722 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000723 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100724 if (downStatus != null) {
725 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000726 "on port {} failed downstream installation: {}",
727 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, downStatus);
728 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100729 } else if (upStatus != null) {
730 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000731 "on port {} failed upstream installation: {}",
732 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, upStatus);
733 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000734 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000735 log.info("Upstream and downstream data plane flows are installed successfully.");
736 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPort,
737 tagInfo.getUpstreamBandwidthProfile(),
738 null, tagInfo.getPonCTag(), true);
739 if (tagInfo.getIsDhcpRequired()) {
740 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
741 upstreamMeterId, tagInfo, true, true);
742 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000743
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000744 if (tagInfo.getIsIgmpRequired()) {
745 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort, upstreamMeterId, tagInfo,
746 true, true);
747 }
748 updateProgrammedSubscriber(cp, tagInfo, true);
749 post(new AccessDeviceEvent(type, deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
750 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100751 }
752 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100753 }
754
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000755 /**
756 * Checks the subscriber uni tag list and find the uni tag information.
757 * using the pon c tag, pon s tag and the technology profile id
758 * May return Optional<null>
759 *
760 * @param cp the connection point of the subscriber
761 * @param innerVlan pon c tag
762 * @param outerVlan pon s tag
763 * @param tpId the technology profile id
764 * @return the found uni tag information
765 */
766 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
767 int tpId) {
768 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
769 outerVlan, tpId);
770 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000771 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000772 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
773 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000774 }
775
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000776 List<UniTagInformation> uniTagList = subInfo.uniTagList();
777 if (uniTagList == null) {
778 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
779 return Optional.empty();
780 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100781
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000782 UniTagInformation service = null;
783 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
784 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
785 && tpId == tagInfo.getTechnologyProfileId()) {
786 service = tagInfo;
787 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700788 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000789 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000790
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000791 if (service == null) {
792 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
793 innerVlan, outerVlan, tpId);
794 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000795 }
796
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000797 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100798 }
799
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100800 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700801 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
802 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100803 *
804 * @param dev Device to look for
805 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700806 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100807 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000808 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000809 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100810
811 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700812 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100813 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800814 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000815 continue;
816 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100817 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100818 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
819 log.info("Creating Eapol for the uni {}", p);
820 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
821 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
822 } else {
823 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
824 "provisioned, not installing default", p.number(), dev.id());
825 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700826 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000827 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100828 }
829 }
830 }
831 }
832
Jonathan Hart403372d2018-08-22 11:44:13 -0700833
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100834 /**
835 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000836 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100837 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
838 * this logic needs to be changed
839 *
840 * @param dev Device to look for
841 * @return The uplink Port of the OLT
842 */
843 private Port getUplinkPort(Device dev) {
844 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000845 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700846 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700847 if (deviceInfo == null) {
848 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000849 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700850 return null;
851 }
852 // Return the port that has been configured as the uplink port of this OLT in Sadis
Gamze Abakaad329652018-12-20 10:12:21 +0000853 for (Port p : deviceService.getPorts(dev.id())) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700854 if (p.number().toLong() == deviceInfo.uplinkPort()) {
Saurav Daseae48de2019-06-19 13:26:15 -0700855 log.trace("getUplinkPort: Found port {}", p);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700856 return p;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100857 }
858 }
859
Saurav Daseae48de2019-06-19 13:26:15 -0700860 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100861 return null;
862 }
863
864 /**
865 * Return the subscriber on a port.
866 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800867 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100868 * @return subscriber if found else null
869 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800870 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
871 Port port = deviceService.getPort(cp);
872 checkNotNull(port, "Invalid connect point");
873 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100874 return subsService.get(portName);
875 }
876
Gamze Abakaad329652018-12-20 10:12:21 +0000877 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000878 * Checks whether the given port of the device is a uni port or not.
879 *
880 * @param d the access device
881 * @param p the port of the device
882 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000883 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100884 private boolean isUniPort(Device d, Port p) {
885 Port ulPort = getUplinkPort(d);
886 if (ulPort != null) {
887 return (ulPort.number().toLong() != p.number().toLong());
888 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530889 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
890 //checks whether the port name starts with nni- which is the signature of an NNI Port
891 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
892 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
893 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
894 return false;
895 }
896 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700897 }
898
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000899 /**
900 * Gets the given device details from SADIS.
901 * If the device is not found, returns null
902 *
903 * @param dev the access device
904 * @return the olt information
905 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700906 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
907 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +0000908 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -0700909 }
910
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800911 /**
912 * Determines if this instance should handle this device based on
913 * consistent hashing.
914 *
915 * @param id device ID
916 * @return true if this instance should handle the device, otherwise false
917 */
918 private boolean isDeviceMine(DeviceId id) {
919 NodeId nodeId = hasher.hash(id.toString());
920 if (log.isDebugEnabled()) {
921 log.debug("Node that will handle {} is {}", id, nodeId);
922 }
923 return nodeId.equals(clusterService.getLocalNode().id());
924 }
925
alshabibf0e7e702015-05-30 18:22:36 -0700926 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800927 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
928
alshabibf0e7e702015-05-30 18:22:36 -0700929 @Override
930 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700931 eventExecutor.execute(() -> {
932 DeviceId devId = event.subject().id();
933 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +0000934 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000935 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -0700936
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000937 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
938 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
939 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700940 return;
941 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700942
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800943 // Only handle the event if the device belongs to us
944 if (!isDeviceMine(event.subject().id())) {
945 return;
946 }
947
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000948 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
949
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700950 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800951 // it's possible that we got an event for a previously
952 // programmed OLT that is no longer available in SADIS
953 // we let such events go through
954 if (!programmedDevices.contains(devId)) {
955 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000956 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -0800957 return;
958 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700959 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700960
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700961 switch (event.type()) {
962 //TODO: Port handling and bookkeeping should be improved once
963 // olt firmware handles correct behaviour.
964 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000965 if (isUniPort(dev, port)) {
966 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000967
968 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
969 log.info("eapol will be sent for port added {}", port);
970 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
971 null,
972 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700973 }
974 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000975 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
976 if (deviceInfo != null) {
977 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
978 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700979 }
980 break;
981 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000982 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +0000983 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000984 log.info("eapol will be send for port removed", port);
985 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
986 null,
987 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -0700988
Gamze Abaka838d8142019-02-21 07:06:55 +0000989 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700990 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700991 break;
992 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000993 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -0700994 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
995 if (deviceInfo != null && port.isEnabled()) {
996 log.debug("NNI dev/port {}/{} enabled", dev.id(),
997 port.number());
998 oltFlowService.processNniFilteringObjectives(dev.id(),
999 port.number(), true);
1000 }
1001 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001002 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001003 ConnectPoint cp = new ConnectPoint(devId, port.number());
1004 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001005 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001006 if (!port.number().equals(PortNumber.LOCAL)) {
1007 log.info("eapol will be {} for dev/port updated {}/{}",
1008 (port.isEnabled()) ? "added" : "removed",
1009 devId, port.number());
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001010 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001011 null,
1012 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1013 port.isEnabled());
1014 }
1015 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001016 log.info("eapol will be {} for dev/port updated {}/{}",
1017 (port.isEnabled()) ? "added" : "removed",
1018 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001019 uniTagInformationSet.forEach(uniTag ->
1020 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1021 uniTag.getUpstreamBandwidthProfile(), null,
1022 uniTag.getPonCTag(), port.isEnabled()));
1023 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001024 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001025 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001026 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001027 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001028 }
alshabibbb83aa22016-02-10 15:08:23 -08001029 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001030 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001031 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001032 break;
1033 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001034 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001035 break;
1036 case DEVICE_AVAILABILITY_CHANGED:
1037 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001038 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001039 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001040 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301041 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001042 log.info("Handling controlled device disconnection .. "
1043 + "flushing all state for dev:{}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301044 handleDeviceDisconnection(dev, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001045 } else {
1046 log.info("Disconnected device has available ports .. "
1047 + "assuming temporary disconnection, "
1048 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301049 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001050 }
1051 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001052 default:
1053 return;
1054 }
1055 });
alshabibf0e7e702015-05-30 18:22:36 -07001056 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001057
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001058 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1059 deviceService.getPorts(device.id()).stream()
1060 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1061 .filter(p -> isUniPort(device, p))
1062 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1063 }
1064
1065 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
1066 programmedDevices.remove(device.id());
1067 removeAllSubscribers(device.id());
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301068 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001069 oltMeterService.clearMeters(device.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001070 post(new AccessDeviceEvent(
1071 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1072 null, null, null));
1073 if (sendUniEvent) {
1074 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001075 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001076 }
1077
1078 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1079 post(new AccessDeviceEvent(
1080 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1081 null, null, null));
1082 programmedDevices.add(dev.id());
1083 checkAndCreateDeviceFlows(dev);
1084 if (sendUniEvent) {
1085 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1086 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001087 }
Gamze Abakada282b42019-03-11 13:16:48 +00001088
1089 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001090 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1091 .filter(e -> e.getKey().deviceId().equals(deviceId))
1092 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001093
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001094 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001095 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001096
Gamze Abaka641fc072018-09-04 09:16:27 +00001097 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001098
1099 private class InternalClusterListener implements ClusterEventListener {
1100
1101 @Override
1102 public void event(ClusterEvent event) {
1103 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1104 hasher.addServer(event.subject().id());
1105 }
1106 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1107 hasher.removeServer(event.subject().id());
1108 }
1109 }
1110 }
Hardik Windlass395ff372019-06-13 05:16:00 +00001111}