blob: 78b8ecfb3dcab2aef157d397dbdd1e86e4e5a4e9 [file] [log] [blame]
alshabibf0e7e702015-05-30 18:22:36 -07001/*
Brian O'Connord6a135a2017-08-03 22:46:05 -07002 * Copyright 2016-present Open Networking Foundation
alshabibf0e7e702015-05-30 18:22:36 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
alshabib36a4d732016-06-01 16:03:59 -070016package org.opencord.olt.impl;
alshabibf0e7e702015-05-30 18:22:36 -070017
Saurav Das9da7d522020-03-23 19:14:35 -070018import static com.google.common.base.Preconditions.checkNotNull;
Andrea Campanella971d5b92020-05-07 11:20:43 +020019import static com.google.common.base.Strings.isNullOrEmpty;
Saurav Das9da7d522020-03-23 19:14:35 -070020import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
21import static java.util.stream.Collectors.collectingAndThen;
22import static java.util.stream.Collectors.groupingBy;
23import static java.util.stream.Collectors.mapping;
24import static java.util.stream.Collectors.toList;
25import static java.util.stream.Collectors.toSet;
26import static org.onlab.util.Tools.get;
27import static org.onlab.util.Tools.groupedThreads;
28import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID;
29import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID_DEFAULT;
30import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_MCAST_SERVICE_NAME;
31import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
32import static org.slf4j.LoggerFactory.getLogger;
33
34import java.util.ArrayList;
35import java.util.Collection;
36import java.util.Dictionary;
37import java.util.List;
38import java.util.Map;
39import java.util.Optional;
40import java.util.Properties;
41import java.util.Set;
42import java.util.concurrent.CompletableFuture;
43import java.util.concurrent.ExecutorService;
44import java.util.concurrent.Executors;
45
alshabibf0e7e702015-05-30 18:22:36 -070046import org.onlab.packet.VlanId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080047import org.onlab.util.KryoNamespace;
Gamze Abaka1f62dd92020-05-07 08:58:13 +000048import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080049import org.onosproject.cluster.ClusterEvent;
50import org.onosproject.cluster.ClusterEventListener;
51import org.onosproject.cluster.ClusterService;
52import org.onosproject.cluster.ControllerNode;
53import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070054import org.onosproject.core.ApplicationId;
55import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080056import org.onosproject.event.AbstractListenerManager;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010057import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070058import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010059import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070060import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080061import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070062import org.onosproject.net.PortNumber;
63import org.onosproject.net.device.DeviceEvent;
64import org.onosproject.net.device.DeviceListener;
65import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053066import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070067import org.onosproject.net.flowobjective.FlowObjectiveService;
68import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080069import org.onosproject.net.flowobjective.Objective;
70import org.onosproject.net.flowobjective.ObjectiveContext;
71import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070072import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080073import org.onosproject.store.serializers.KryoNamespaces;
74import org.onosproject.store.service.ConsistentMultimap;
75import org.onosproject.store.service.Serializer;
76import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070077import org.opencord.olt.AccessDeviceEvent;
78import org.opencord.olt.AccessDeviceListener;
79import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010080import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000081import org.opencord.olt.internalapi.AccessDeviceFlowService;
82import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000083import org.opencord.sadis.BandwidthProfileInformation;
84import org.opencord.sadis.BaseInformationService;
85import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010086import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000087import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080088import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070089import org.osgi.service.component.annotations.Activate;
90import org.osgi.service.component.annotations.Component;
91import org.osgi.service.component.annotations.Deactivate;
92import org.osgi.service.component.annotations.Modified;
93import org.osgi.service.component.annotations.Reference;
94import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070095import org.slf4j.Logger;
96
Saurav Das9da7d522020-03-23 19:14:35 -070097import com.google.common.collect.ImmutableMap;
98import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -070099
100/**
Jonathan Harte533a422015-10-20 17:31:24 -0700101 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700102 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700103@Component(immediate = true,
104 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700105 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000106 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700107 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800108public class Olt
109 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
110 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800111 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800112
Gamze Abakada282b42019-03-11 13:16:48 +0000113 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000114 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800115
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800116 public static final int HASH_WEIGHT = 10;
117
alshabibf0e7e702015-05-30 18:22:36 -0700118 private final Logger log = getLogger(getClass());
119
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530120 private static final String NNI = "nni-";
121
Carmelo Casconeca931162019-07-15 18:22:24 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700123 protected FlowObjectiveService flowObjectiveService;
124
Carmelo Casconeca931162019-07-15 18:22:24 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700126 protected DeviceService deviceService;
127
Carmelo Casconeca931162019-07-15 18:22:24 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700129 protected CoreService coreService;
130
Carmelo Casconeca931162019-07-15 18:22:24 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000132 protected SadisService sadisService;
133
Carmelo Casconeca931162019-07-15 18:22:24 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000135 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800136
Carmelo Casconeca931162019-07-15 18:22:24 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000138 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000139
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800140 @Reference(cardinality = ReferenceCardinality.MANDATORY)
141 protected StorageService storageService;
142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY)
144 protected ClusterService clusterService;
145
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530146 @Reference(cardinality = ReferenceCardinality.MANDATORY)
147 protected FlowRuleService flowRuleService;
148
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000149 @Reference(cardinality = ReferenceCardinality.MANDATORY)
150 protected ComponentConfigService componentConfigService;
151
Carmelo Casconeca931162019-07-15 18:22:24 -0700152 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800153 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700154 **/
155 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000156
Carmelo Casconeca931162019-07-15 18:22:24 -0700157 /**
Gamze Abaka51a34e82020-05-08 13:03:14 +0000158 * Default multicast service name.
Carmelo Casconeca931162019-07-15 18:22:24 -0700159 **/
Gamze Abaka51a34e82020-05-08 13:03:14 +0000160 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Gamze Abaka33feef52019-02-27 08:16:47 +0000161
alshabibf0e7e702015-05-30 18:22:36 -0700162 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800163 private final ClusterEventListener clusterListener = new InternalClusterListener();
164
165 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700166
Gamze Abaka641fc072018-09-04 09:16:27 +0000167 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
168 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700169
Gamze Abaka641fc072018-09-04 09:16:27 +0000170 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000171 groupedThreads("onos/olt-service",
172 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100173
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700174 protected ExecutorService eventExecutor;
175
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800176 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800177
alshabibf0e7e702015-05-30 18:22:36 -0700178 @Activate
alshabibe0559672016-02-21 14:49:51 -0800179 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000180 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
181 "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800182 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000183 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000184 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800185
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800186 KryoNamespace serializer = KryoNamespace.newBuilder()
187 .register(KryoNamespaces.API)
188 .register(UniTagInformation.class)
189 .build();
190
191 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
192 .withName("volt-programmed-subs")
193 .withSerializer(Serializer.using(serializer))
194 .withApplicationId(appId)
195 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700196
alshabib8e4fd2f2016-01-12 15:55:53 -0800197 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
198
Gamze Abaka641fc072018-09-04 09:16:27 +0000199 subsService = sadisService.getSubscriberInfoService();
200 bpService = sadisService.getBandwidthProfileService();
201
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800202 List<NodeId> readyNodes = clusterService.getNodes().stream()
203 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
204 .map(ControllerNode::id)
205 .collect(toList());
206 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
207 clusterService.addListener(clusterListener);
208
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100209 // look for all provisioned devices in Sadis and create EAPOL flows for the
210 // UNI ports
211 Iterable<Device> devices = deviceService.getDevices();
212 for (Device d : devices) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800213 if (isDeviceMine(d.id())) {
214 checkAndCreateDeviceFlows(d);
215 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100216 }
alshabib4ceaed32016-03-03 18:00:58 -0800217
alshabibba357492016-01-27 13:49:46 -0800218 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700219 log.info("Started with Application ID {}", appId.id());
220 }
221
222 @Deactivate
223 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000224 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800225 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800226 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700227 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700228 log.info("Stopped");
229 }
230
alshabibe0559672016-02-21 14:49:51 -0800231 @Modified
232 public void modified(ComponentContext context) {
233 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
234
235 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200236 String bpId = get(properties, DEFAULT_BP_ID);
237 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000238
Andrea Campanella971d5b92020-05-07 11:20:43 +0200239 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
240 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000241
242 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000243
alshabibe0559672016-02-21 14:49:51 -0800244 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000245 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200246 defaultBpId = DEFAULT_BP_ID_DEFAULT;
247 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800248 }
249 }
250
alshabib32232c82016-02-25 17:57:24 -0500251 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000252 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700253 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000254 DeviceId deviceId = connectPoint.deviceId();
255 PortNumber subscriberPortNo = connectPoint.port();
256
257 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000258 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000259
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100260 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000261 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100262 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000263 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100264 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100265 }
Jonathan Harte533a422015-10-20 17:31:24 -0700266
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100267 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000268 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100269 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000270 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100271 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700272 }
273
Gamze Abaka838d8142019-02-21 07:06:55 +0000274 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000275 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000276 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000277 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
278 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
279 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000280 filterFuture.thenAcceptAsync(filterStatus -> {
281 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000282 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Gamze Abaka33feef52019-02-27 08:16:47 +0000283 }
284 });
Amit Ghosh31939522018-08-16 13:28:21 +0100285 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800286 }
287
288 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000289 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700290 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000291
Saurav Daseae48de2019-06-19 13:26:15 -0700292 // Get the subscriber connected to this port from the local cache
293 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000294 DeviceId deviceId = connectPoint.deviceId();
295 PortNumber subscriberPortNo = connectPoint.port();
296
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800297 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000298 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000299 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000300 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800301 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800302 }
303
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100304 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000305 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100306 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000307 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100308 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800309 }
310
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000311 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100312
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000313 if (multicastServiceName.equals(uniTag.getServiceName())) {
314 continue;
315 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000316
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000317 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800318
Saurav Das9da7d522020-03-23 19:14:35 -0700319 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000320 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
321 uniTag.getUpstreamBandwidthProfile(),
322 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100323
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000324 Port port = deviceService.getPort(deviceId, subscriberPortNo);
325 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700326 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000327 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
328 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
329 } else {
330 log.debug("Port {} is no longer enabled or it's unavailable. Not "
331 + "reprogramming default eapol flow", connectPoint);
332 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100333 }
Amit Ghosh31939522018-08-16 13:28:21 +0100334 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800335 }
336
Gamze Abakaf59c0912019-04-19 08:24:28 +0000337
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000338 @Override
339 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
340 Optional<VlanId> cTag, Optional<Integer> tpId) {
341
342 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
343 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000344
Amit Ghosh31939522018-08-16 13:28:21 +0100345 // Check if we can find the connect point to which this subscriber is connected
346 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
347 if (subsPort == null) {
348 log.warn("ConnectPoint for {} not found", subscriberId);
349 return false;
350 }
351
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100352 if (!sTag.isPresent() && !cTag.isPresent()) {
353 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000354 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100355 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
356 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000357 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100358 return false;
359 }
360
Gamze Abakaf59c0912019-04-19 08:24:28 +0000361 //delete Eapol authentication flow with default bandwidth
362 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000363 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000364 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
365 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
366 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000367 filterFuture.thenAcceptAsync(filterStatus -> {
368 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000369 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
370 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000371 }
372 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100373 return true;
374 } else {
375 log.warn("Provisioning failed for subscriber: {}", subscriberId);
376 return false;
377 }
Amit Ghosh31939522018-08-16 13:28:21 +0100378 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100379
alshabibe0559672016-02-21 14:49:51 -0800380 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000381 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
382 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100383 // Check if we can find the connect point to which this subscriber is connected
384 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
385 if (subsPort == null) {
386 log.warn("ConnectPoint for {} not found", subscriberId);
387 return false;
388 }
389
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100390 if (!sTag.isPresent() && !cTag.isPresent()) {
391 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000392 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100393 // Get the uplink port
394 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
395 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000396 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100397 return false;
398 }
399
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000400 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
401 if (!tagInfo.isPresent()) {
402 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
403 subsPort, cTag, sTag, tpId);
404 return false;
405 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000406
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000407 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100408 return true;
409 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000410 log.warn("Removing subscriber is not possible - please check the provided information" +
411 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100412 return false;
413 }
Amit Ghosh31939522018-08-16 13:28:21 +0100414 }
415
416 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000417 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800418 return programmedSubs.stream()
419 .collect(collectingAndThen(
420 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
421 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700422 }
423
424 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100425 public List<DeviceId> fetchOlts() {
426 // look through all the devices and find the ones that are OLTs as per Sadis
427 List<DeviceId> olts = new ArrayList<>();
428 Iterable<Device> devices = deviceService.getDevices();
429 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700430 if (getOltInfo(d) != null) {
431 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100432 olts.add(d.id());
433 }
434 }
435 return olts;
alshabibe0559672016-02-21 14:49:51 -0800436 }
437
Amit Ghosh31939522018-08-16 13:28:21 +0100438 /**
439 * Finds the connect point to which a subscriber is connected.
440 *
441 * @param id The id of the subscriber, this is the same ID as in Sadis
442 * @return Subscribers ConnectPoint if found else null
443 */
444 private ConnectPoint findSubscriberConnectPoint(String id) {
445
446 Iterable<Device> devices = deviceService.getDevices();
447 for (Device d : devices) {
448 for (Port p : deviceService.getPorts(d.id())) {
449 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
450 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
451 log.debug("Found on device {} port {}", d.id(), p.number());
452 return new ConnectPoint(d.id(), p.number());
453 }
454 }
455 }
456 return null;
457 }
458
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000459 /**
460 * Gets the context of the bandwidth profile information for the given parameter.
461 *
462 * @param bandwidthProfile the bandwidth profile id
463 * @return the context of the bandwidth profile information
464 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000465 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
466 if (bandwidthProfile == null) {
467 return null;
468 }
469 return bpService.get(bandwidthProfile);
470 }
471
Gamze Abaka838d8142019-02-21 07:06:55 +0000472 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000473 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000474 *
475 * @param deviceId the device identifier
476 * @param uplink uplink port of the OLT
477 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000478 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000479 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000480 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000481 PortNumber subscriberPort, UniTagInformation uniTag) {
482
483 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800484
485 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
486 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
487
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000488 VlanId deviceVlan = uniTag.getPonSTag();
489 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000490
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000491 MeterId upstreamMeterId = oltMeterService
492 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
493 MeterId downstreamMeterId = oltMeterService
494 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000495
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000496 ForwardingObjective.Builder upFwd =
497 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
498 ForwardingObjective.Builder downFwd =
499 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
500
501 if (uniTag.getIsIgmpRequired()) {
502 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
503 upstreamMeterId, uniTag, false, true);
504 }
505 if (uniTag.getIsDhcpRequired()) {
506 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
507 upstreamMeterId, uniTag, false, true);
508 }
alshabibbf23a1f2016-01-14 17:27:11 -0800509
alshabib4ceaed32016-03-03 18:00:58 -0800510 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
511 @Override
512 public void onSuccess(Objective objective) {
513 upFuture.complete(null);
514 }
alshabibbf23a1f2016-01-14 17:27:11 -0800515
alshabib4ceaed32016-03-03 18:00:58 -0800516 @Override
517 public void onError(Objective objective, ObjectiveError error) {
518 upFuture.complete(error);
519 }
520 }));
521
522 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
523 @Override
524 public void onSuccess(Objective objective) {
525 downFuture.complete(null);
526 }
527
528 @Override
529 public void onError(Objective objective, ObjectiveError error) {
530 downFuture.complete(error);
531 }
532 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800533
534 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000535 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800536 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000537 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
538 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
539 uniTag.getTechnologyProfileId(), subscriberPort);
540 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800541 } else if (downStatus != null) {
542 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000543 "on port {} failed downstream uninstallation: {}",
544 subscriberVlan, deviceId, subscriberPort, downStatus);
545 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800546 } else if (upStatus != null) {
547 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000548 "on port {} failed upstream uninstallation: {}",
549 subscriberVlan, deviceId, subscriberPort, upStatus);
550 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800551 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000552 Port port = deviceService.getPort(deviceId, subscriberPort);
553 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
554 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800555 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700556 }
557
Gamze Abaka838d8142019-02-21 07:06:55 +0000558 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000559 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000560 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000561 * @param connectPoint the connection point of the subscriber
562 * @param uplinkPort uplink port of the OLT (the nni port)
563 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000564 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000565 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
566 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000567
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000568 log.info("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000569
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000570 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
571 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
572 return;
573 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000574
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000575 DeviceId deviceId = connectPoint.deviceId();
576 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000577
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000578 for (UniTagInformation uniTag : sub.uniTagList()) {
579 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
580 }
581 }
alshabib3ea82642016-01-12 18:06:53 -0800582
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000583 /**
584 * Finds the uni tag information and provisions the found information.
585 * If the uni tag information is not found, returns
586 *
587 * @param deviceId the access device id
588 * @param uplinkPort the nni port
589 * @param subscriberPort the uni port
590 * @param innerVlan the pon c tag
591 * @param outerVlan the pon s tag
592 * @param tpId the technology profile id
593 */
594 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
595 PortNumber subscriberPort,
596 VlanId innerVlan,
597 VlanId outerVlan,
598 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700599
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000600 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
601 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
602 if (!gotTagInformation.isPresent()) {
603 return;
604 }
605 UniTagInformation tagInformation = gotTagInformation.get();
606 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
607 }
alshabib3ea82642016-01-12 18:06:53 -0800608
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000609 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800610 if (add) {
611 programmedSubs.put(connectPoint, tagInformation);
612 } else {
613 programmedSubs.remove(connectPoint, tagInformation);
614 }
Jonathan Harte533a422015-10-20 17:31:24 -0700615 }
616
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000617 /**
618 * Installs a uni tag information flow.
619 *
620 * @param deviceId the access device id
621 * @param uplinkPort the nni port
622 * @param subscriberPort the uni port
623 * @param tagInfo the uni tag information
624 */
625 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
626 UniTagInformation tagInfo) {
627
628 log.info("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
629
630 Port port = deviceService.getPort(deviceId, subscriberPort);
631
632 if (multicastServiceName.equals(tagInfo.getServiceName())) {
633 // IGMP flows are taken care of along with VOD service
634 // Please note that for each service, Subscriber Registered event will be sent
635 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
636 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
637 tagInfo.getTechnologyProfileId()));
638 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000639 }
640
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100641 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
642
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000643 BandwidthProfileInformation upstreamBpInfo =
644 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
645 BandwidthProfileInformation downstreamBpInfo =
646 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000647
648 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
649 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000650 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
651 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000652
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000653 MeterId upstreamMeterId = oltMeterService.createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
654
655 MeterId downstreamMeterId = oltMeterService.createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000656
657 upstreamMeterFuture.thenAcceptAsync(result -> {
658 if (result == null) {
659 log.info("Upstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000660 "Sending subscriber flows.", upstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000661
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000662 ForwardingObjective.Builder upFwd =
663 oltFlowService.createUpBuilder(uplinkPort, subscriberPort, upstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000664
665 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
666 @Override
667 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000668 log.info("Upstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000669 upFuture.complete(null);
670 }
671
672 @Override
673 public void onError(Objective objective, ObjectiveError error) {
674 upFuture.complete(error);
675 }
676 }));
677
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000678 } else if (upstreamBpInfo == null) {
679 log.warn("No meter installed since no Upstream BW Profile definition found for " +
680 "ctag {} stag {} tpId {} and Device/port: {}:{}",
681 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
682 tagInfo.getTechnologyProfileId(),
683 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000684 } else {
685 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000686 "Result {} and MeterId {}", result, upstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000687 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800688 }).exceptionally(ex -> {
689 log.error("Upstream flow failed: " + ex.getMessage());
690 upFuture.complete(ObjectiveError.UNKNOWN);
691 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000692 });
693
694 downsteamMeterFuture.thenAcceptAsync(result -> {
695 if (result == null) {
696 log.info("Downstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000697 "Sending subscriber flows.", downstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000698
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000699 ForwardingObjective.Builder downFwd =
700 oltFlowService.createDownBuilder(uplinkPort, subscriberPort, downstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000701
702 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
703 @Override
704 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000705 log.info("Downstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000706 downFuture.complete(null);
707 }
708
709 @Override
710 public void onError(Objective objective, ObjectiveError error) {
711 downFuture.complete(error);
712 }
713 }));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000714
715 } else if (downstreamBpInfo == null) {
716 log.warn("No meter installed since no Downstream BW Profile definition found for " +
717 "ctag {} stag {} tpId {} and Device/port: {}:{}",
718 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
719 tagInfo.getTechnologyProfileId(),
720 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000721 } else {
722 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000723 "Result {} and MeterId {}", result, downstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000724 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800725 }).exceptionally(ex -> {
726 log.error("Downstream flow failed: " + ex.getMessage());
727 downFuture.complete(ObjectiveError.UNKNOWN);
728 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000729 });
730
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100731 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000732 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100733 if (downStatus != null) {
734 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000735 "on port {} failed downstream installation: {}",
736 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, downStatus);
737 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100738 } else if (upStatus != null) {
739 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000740 "on port {} failed upstream installation: {}",
741 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, upStatus);
742 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000743 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000744 log.info("Upstream and downstream data plane flows are installed successfully.");
745 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPort,
746 tagInfo.getUpstreamBandwidthProfile(),
747 null, tagInfo.getPonCTag(), true);
748 if (tagInfo.getIsDhcpRequired()) {
749 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
750 upstreamMeterId, tagInfo, true, true);
751 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000752
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000753 if (tagInfo.getIsIgmpRequired()) {
754 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort, upstreamMeterId, tagInfo,
755 true, true);
756 }
757 updateProgrammedSubscriber(cp, tagInfo, true);
758 post(new AccessDeviceEvent(type, deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
759 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100760 }
761 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100762 }
763
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000764 /**
765 * Checks the subscriber uni tag list and find the uni tag information.
766 * using the pon c tag, pon s tag and the technology profile id
767 * May return Optional<null>
768 *
769 * @param cp the connection point of the subscriber
770 * @param innerVlan pon c tag
771 * @param outerVlan pon s tag
772 * @param tpId the technology profile id
773 * @return the found uni tag information
774 */
775 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
776 int tpId) {
777 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
778 outerVlan, tpId);
779 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000780 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000781 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
782 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000783 }
784
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000785 List<UniTagInformation> uniTagList = subInfo.uniTagList();
786 if (uniTagList == null) {
787 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
788 return Optional.empty();
789 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100790
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000791 UniTagInformation service = null;
792 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
793 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
794 && tpId == tagInfo.getTechnologyProfileId()) {
795 service = tagInfo;
796 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700797 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000798 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000799
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000800 if (service == null) {
801 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
802 innerVlan, outerVlan, tpId);
803 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000804 }
805
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000806 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100807 }
808
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100809 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700810 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
811 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100812 *
813 * @param dev Device to look for
814 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700815 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100816 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000817 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000818 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100819
820 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700821 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100822 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800823 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000824 continue;
825 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100826 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100827 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
828 log.info("Creating Eapol for the uni {}", p);
829 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
830 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
831 } else {
832 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
833 "provisioned, not installing default", p.number(), dev.id());
834 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700835 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000836 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100837 }
838 }
839 }
840 }
841
Jonathan Hart403372d2018-08-22 11:44:13 -0700842
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100843 /**
844 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000845 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100846 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
847 * this logic needs to be changed
848 *
849 * @param dev Device to look for
850 * @return The uplink Port of the OLT
851 */
852 private Port getUplinkPort(Device dev) {
853 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000854 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700855 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700856 if (deviceInfo == null) {
857 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000858 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700859 return null;
860 }
861 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +0530862 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
863 .filter(port -> isNniPort(port) ||
864 (port.number().toLong() == deviceInfo.uplinkPort()))
865 .findFirst();
866 if (optionalPort.isPresent()) {
867 log.trace("getUplinkPort: Found port {}", optionalPort.get());
868 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100869 }
870
Saurav Daseae48de2019-06-19 13:26:15 -0700871 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100872 return null;
873 }
874
875 /**
876 * Return the subscriber on a port.
877 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800878 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100879 * @return subscriber if found else null
880 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800881 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
882 Port port = deviceService.getPort(cp);
883 checkNotNull(port, "Invalid connect point");
884 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100885 return subsService.get(portName);
886 }
887
Gamze Abakaad329652018-12-20 10:12:21 +0000888 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000889 * Checks whether the given port of the device is a uni port or not.
890 *
891 * @param d the access device
892 * @param p the port of the device
893 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000894 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100895 private boolean isUniPort(Device d, Port p) {
896 Port ulPort = getUplinkPort(d);
897 if (ulPort != null) {
898 return (ulPort.number().toLong() != p.number().toLong());
899 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530900 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
901 //checks whether the port name starts with nni- which is the signature of an NNI Port
902 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
903 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
904 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
905 return false;
906 }
907 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700908 }
909
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000910 /**
911 * Gets the given device details from SADIS.
912 * If the device is not found, returns null
913 *
914 * @param dev the access device
915 * @return the olt information
916 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700917 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
918 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +0000919 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -0700920 }
921
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800922 /**
923 * Determines if this instance should handle this device based on
924 * consistent hashing.
925 *
926 * @param id device ID
927 * @return true if this instance should handle the device, otherwise false
928 */
929 private boolean isDeviceMine(DeviceId id) {
930 NodeId nodeId = hasher.hash(id.toString());
931 if (log.isDebugEnabled()) {
932 log.debug("Node that will handle {} is {}", id, nodeId);
933 }
934 return nodeId.equals(clusterService.getLocalNode().id());
935 }
936
kdarapuaa5da252020-04-10 15:58:05 +0530937 private boolean isNniPort(Port port) {
938 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
939 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
940 }
941 return false;
942 }
943
alshabibf0e7e702015-05-30 18:22:36 -0700944 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800945 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
946
alshabibf0e7e702015-05-30 18:22:36 -0700947 @Override
948 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700949 eventExecutor.execute(() -> {
950 DeviceId devId = event.subject().id();
951 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +0000952 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000953 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -0700954
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000955 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
956 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
957 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700958 return;
959 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700960
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800961 // Only handle the event if the device belongs to us
962 if (!isDeviceMine(event.subject().id())) {
963 return;
964 }
965
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000966 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
967
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700968 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800969 // it's possible that we got an event for a previously
970 // programmed OLT that is no longer available in SADIS
971 // we let such events go through
972 if (!programmedDevices.contains(devId)) {
973 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000974 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -0800975 return;
976 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700977 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700978
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700979 switch (event.type()) {
980 //TODO: Port handling and bookkeeping should be improved once
981 // olt firmware handles correct behaviour.
982 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000983 if (isUniPort(dev, port)) {
984 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000985
986 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
987 log.info("eapol will be sent for port added {}", port);
988 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
989 null,
990 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700991 }
992 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000993 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
994 if (deviceInfo != null) {
995 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
996 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700997 }
998 break;
999 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001000 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001001 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001002 log.info("eapol will be send for port removed", port);
1003 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1004 null,
1005 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001006
Gamze Abaka838d8142019-02-21 07:06:55 +00001007 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001008 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001009 break;
1010 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001011 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001012 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1013 if (deviceInfo != null && port.isEnabled()) {
1014 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1015 port.number());
1016 oltFlowService.processNniFilteringObjectives(dev.id(),
1017 port.number(), true);
1018 }
1019 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001020 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001021 ConnectPoint cp = new ConnectPoint(devId, port.number());
1022 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001023 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001024 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001025 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001026 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001027 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001028 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001029 null,
1030 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1031 port.isEnabled());
1032 }
1033 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001034 log.info("eapol will be {} for dev/port updated {}/{}",
1035 (port.isEnabled()) ? "added" : "removed",
1036 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001037 uniTagInformationSet.forEach(uniTag ->
1038 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1039 uniTag.getUpstreamBandwidthProfile(), null,
1040 uniTag.getPonCTag(), port.isEnabled()));
1041 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001042 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001043 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001044 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001045 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001046 }
alshabibbb83aa22016-02-10 15:08:23 -08001047 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001048 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001049 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001050 break;
1051 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001052 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001053 break;
1054 case DEVICE_AVAILABILITY_CHANGED:
1055 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001056 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001057 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001058 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301059 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001060 log.info("Handling controlled device disconnection .. "
1061 + "flushing all state for dev:{}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301062 handleDeviceDisconnection(dev, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001063 } else {
1064 log.info("Disconnected device has available ports .. "
1065 + "assuming temporary disconnection, "
1066 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301067 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001068 }
1069 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001070 default:
1071 return;
1072 }
1073 });
alshabibf0e7e702015-05-30 18:22:36 -07001074 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001075
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001076 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1077 deviceService.getPorts(device.id()).stream()
1078 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1079 .filter(p -> isUniPort(device, p))
1080 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1081 }
1082
1083 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
1084 programmedDevices.remove(device.id());
1085 removeAllSubscribers(device.id());
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301086 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001087 oltMeterService.clearMeters(device.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001088 post(new AccessDeviceEvent(
1089 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1090 null, null, null));
1091 if (sendUniEvent) {
1092 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001093 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001094 }
1095
1096 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1097 post(new AccessDeviceEvent(
1098 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1099 null, null, null));
1100 programmedDevices.add(dev.id());
1101 checkAndCreateDeviceFlows(dev);
1102 if (sendUniEvent) {
1103 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1104 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001105 }
Gamze Abakada282b42019-03-11 13:16:48 +00001106
1107 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001108 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1109 .filter(e -> e.getKey().deviceId().equals(deviceId))
1110 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001111
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001112 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001113 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001114
Gamze Abaka641fc072018-09-04 09:16:27 +00001115 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001116
1117 private class InternalClusterListener implements ClusterEventListener {
1118
1119 @Override
1120 public void event(ClusterEvent event) {
1121 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1122 hasher.addServer(event.subject().id());
1123 }
1124 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1125 hasher.removeServer(event.subject().id());
1126 }
1127 }
1128 }
Hardik Windlass395ff372019-06-13 05:16:00 +00001129}