blob: ad520306097351c57f25ed404a4f28d91960b0d7 [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;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020053import org.onosproject.cluster.LeadershipService;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080054import org.onosproject.cluster.NodeId;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onosproject.core.ApplicationId;
56import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080057import org.onosproject.event.AbstractListenerManager;
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +020058import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010059import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070060import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010061import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070062import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080063import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070064import org.onosproject.net.PortNumber;
65import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Hardik Windlassa58fbee2020-03-12 18:33:55 +053068import org.onosproject.net.flow.FlowRuleService;
alshabibf0e7e702015-05-30 18:22:36 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
70import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080071import org.onosproject.net.flowobjective.Objective;
72import org.onosproject.net.flowobjective.ObjectiveContext;
73import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070074import org.onosproject.net.meter.MeterId;
Jonathan Hart4f178fa2020-02-03 10:46:01 -080075import org.onosproject.store.serializers.KryoNamespaces;
76import org.onosproject.store.service.ConsistentMultimap;
77import org.onosproject.store.service.Serializer;
78import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070079import org.opencord.olt.AccessDeviceEvent;
80import org.opencord.olt.AccessDeviceListener;
81import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010082import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000083import org.opencord.olt.internalapi.AccessDeviceFlowService;
84import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000085import org.opencord.sadis.BandwidthProfileInformation;
86import org.opencord.sadis.BaseInformationService;
87import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010088import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000089import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080090import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070091import org.osgi.service.component.annotations.Activate;
92import org.osgi.service.component.annotations.Component;
93import org.osgi.service.component.annotations.Deactivate;
94import org.osgi.service.component.annotations.Modified;
95import org.osgi.service.component.annotations.Reference;
96import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070097import org.slf4j.Logger;
98
Saurav Das9da7d522020-03-23 19:14:35 -070099import com.google.common.collect.ImmutableMap;
100import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700101
102/**
Jonathan Harte533a422015-10-20 17:31:24 -0700103 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700104 */
Carmelo Casconeca931162019-07-15 18:22:24 -0700105@Component(immediate = true,
106 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -0700107 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000108 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -0700109 })
alshabib8e4fd2f2016-01-12 15:55:53 -0800110public class Olt
111 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
112 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800113 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800114
Gamze Abakada282b42019-03-11 13:16:48 +0000115 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +0000116 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -0800117
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800118 public static final int HASH_WEIGHT = 10;
119
alshabibf0e7e702015-05-30 18:22:36 -0700120 private final Logger log = getLogger(getClass());
121
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530122 private static final String NNI = "nni-";
123
Carmelo Casconeca931162019-07-15 18:22:24 -0700124 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700125 protected FlowObjectiveService flowObjectiveService;
126
Carmelo Casconeca931162019-07-15 18:22:24 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700128 protected DeviceService deviceService;
129
Carmelo Casconeca931162019-07-15 18:22:24 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700131 protected CoreService coreService;
132
Carmelo Casconeca931162019-07-15 18:22:24 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000134 protected SadisService sadisService;
135
Carmelo Casconeca931162019-07-15 18:22:24 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000137 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800138
Carmelo Casconeca931162019-07-15 18:22:24 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000140 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000141
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
143 protected StorageService storageService;
144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
146 protected ClusterService clusterService;
147
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200149 protected MastershipService mastershipService;
150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
152 protected LeadershipService leadershipService;
153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Hardik Windlassa58fbee2020-03-12 18:33:55 +0530155 protected FlowRuleService flowRuleService;
156
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000157 @Reference(cardinality = ReferenceCardinality.MANDATORY)
158 protected ComponentConfigService componentConfigService;
159
Carmelo Casconeca931162019-07-15 18:22:24 -0700160 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800161 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700162 **/
163 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000164
Carmelo Casconeca931162019-07-15 18:22:24 -0700165 /**
Gamze Abaka51a34e82020-05-08 13:03:14 +0000166 * Default multicast service name.
Carmelo Casconeca931162019-07-15 18:22:24 -0700167 **/
Gamze Abaka51a34e82020-05-08 13:03:14 +0000168 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
Gamze Abaka33feef52019-02-27 08:16:47 +0000169
alshabibf0e7e702015-05-30 18:22:36 -0700170 private final DeviceListener deviceListener = new InternalDeviceListener();
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800171 private final ClusterEventListener clusterListener = new InternalClusterListener();
172
173 private ConsistentHasher hasher;
alshabibf0e7e702015-05-30 18:22:36 -0700174
Gamze Abaka641fc072018-09-04 09:16:27 +0000175 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
176 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700177
Gamze Abaka641fc072018-09-04 09:16:27 +0000178 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000179 groupedThreads("onos/olt-service",
180 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100181
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700182 protected ExecutorService eventExecutor;
183
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800184 private ConsistentMultimap<ConnectPoint, UniTagInformation> programmedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800185
alshabibf0e7e702015-05-30 18:22:36 -0700186 @Activate
alshabibe0559672016-02-21 14:49:51 -0800187 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000188 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
189 "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800190 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000191 ApplicationId appId = coreService.registerApplication(APP_NAME);
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000192 componentConfigService.registerProperties(getClass());
Saurav Das62ad75e2019-03-05 12:22:22 -0800193
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800194 KryoNamespace serializer = KryoNamespace.newBuilder()
195 .register(KryoNamespaces.API)
196 .register(UniTagInformation.class)
197 .build();
198
199 programmedSubs = storageService.<ConnectPoint, UniTagInformation>consistentMultimapBuilder()
200 .withName("volt-programmed-subs")
201 .withSerializer(Serializer.using(serializer))
202 .withApplicationId(appId)
203 .build();
alshabibc4dfe852015-06-05 13:35:13 -0700204
alshabib8e4fd2f2016-01-12 15:55:53 -0800205 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
206
Gamze Abaka641fc072018-09-04 09:16:27 +0000207 subsService = sadisService.getSubscriberInfoService();
208 bpService = sadisService.getBandwidthProfileService();
209
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800210 List<NodeId> readyNodes = clusterService.getNodes().stream()
211 .filter(c -> clusterService.getState(c.id()) == ControllerNode.State.READY)
212 .map(ControllerNode::id)
213 .collect(toList());
214 hasher = new ConsistentHasher(readyNodes, HASH_WEIGHT);
215 clusterService.addListener(clusterListener);
216
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100217 // look for all provisioned devices in Sadis and create EAPOL flows for the
218 // UNI ports
219 Iterable<Device> devices = deviceService.getDevices();
220 for (Device d : devices) {
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200221 if (isLocalLeader(d.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800222 checkAndCreateDeviceFlows(d);
223 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100224 }
alshabib4ceaed32016-03-03 18:00:58 -0800225
alshabibba357492016-01-27 13:49:46 -0800226 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700227 log.info("Started with Application ID {}", appId.id());
228 }
229
230 @Deactivate
231 public void deactivate() {
Gamze Abaka1f62dd92020-05-07 08:58:13 +0000232 componentConfigService.unregisterProperties(getClass(), false);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800233 clusterService.removeListener(clusterListener);
alshabib62e9ce72016-02-11 17:31:36 -0800234 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700235 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700236 log.info("Stopped");
237 }
238
alshabibe0559672016-02-21 14:49:51 -0800239 @Modified
240 public void modified(ComponentContext context) {
241 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
242
243 try {
Andrea Campanella971d5b92020-05-07 11:20:43 +0200244 String bpId = get(properties, DEFAULT_BP_ID);
245 defaultBpId = isNullOrEmpty(bpId) ? defaultBpId : bpId;
Gamze Abakaad329652018-12-20 10:12:21 +0000246
Andrea Campanella971d5b92020-05-07 11:20:43 +0200247 String mcastSN = get(properties, DEFAULT_MCAST_SERVICE_NAME);
248 multicastServiceName = isNullOrEmpty(mcastSN) ? multicastServiceName : mcastSN;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000249
250 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000251
alshabibe0559672016-02-21 14:49:51 -0800252 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000253 log.error("Error while modifying the properties", e);
Andrea Campanella971d5b92020-05-07 11:20:43 +0200254 defaultBpId = DEFAULT_BP_ID_DEFAULT;
255 multicastServiceName = DEFAULT_MCAST_SERVICE_NAME_DEFAULT;
alshabibe0559672016-02-21 14:49:51 -0800256 }
257 }
258
alshabib32232c82016-02-25 17:57:24 -0500259 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000260 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700261 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000262 DeviceId deviceId = connectPoint.deviceId();
263 PortNumber subscriberPortNo = connectPoint.port();
264
265 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000266 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000267
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100268 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000269 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100270 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000271 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100272 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100273 }
Jonathan Harte533a422015-10-20 17:31:24 -0700274
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100275 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000276 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100277 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000278 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100279 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700280 }
281
Gamze Abaka838d8142019-02-21 07:06:55 +0000282 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000283 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000284 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000285 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
286 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
287 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000288 filterFuture.thenAcceptAsync(filterStatus -> {
289 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000290 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Gamze Abaka33feef52019-02-27 08:16:47 +0000291 }
292 });
Amit Ghosh31939522018-08-16 13:28:21 +0100293 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800294 }
295
296 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000297 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700298 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000299
Saurav Daseae48de2019-06-19 13:26:15 -0700300 // Get the subscriber connected to this port from the local cache
301 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000302 DeviceId deviceId = connectPoint.deviceId();
303 PortNumber subscriberPortNo = connectPoint.port();
304
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800305 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000306 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000307 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000308 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800309 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800310 }
311
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100312 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000313 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100314 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000315 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100316 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800317 }
318
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000319 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100320
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000321 if (multicastServiceName.equals(uniTag.getServiceName())) {
322 continue;
323 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000324
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000325 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800326
Saurav Das9da7d522020-03-23 19:14:35 -0700327 // remove eapol with subscriber bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000328 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
329 uniTag.getUpstreamBandwidthProfile(),
330 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100331
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000332 Port port = deviceService.getPort(deviceId, subscriberPortNo);
333 if (port != null && port.isEnabled()) {
Saurav Das9da7d522020-03-23 19:14:35 -0700334 // reinstall eapol with default bandwidth profile
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000335 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
336 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
337 } else {
338 log.debug("Port {} is no longer enabled or it's unavailable. Not "
339 + "reprogramming default eapol flow", connectPoint);
340 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100341 }
Amit Ghosh31939522018-08-16 13:28:21 +0100342 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800343 }
344
Gamze Abakaf59c0912019-04-19 08:24:28 +0000345
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000346 @Override
347 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
348 Optional<VlanId> cTag, Optional<Integer> tpId) {
349
350 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
351 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000352
Amit Ghosh31939522018-08-16 13:28:21 +0100353 // Check if we can find the connect point to which this subscriber is connected
354 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
355 if (subsPort == null) {
356 log.warn("ConnectPoint for {} not found", subscriberId);
357 return false;
358 }
359
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100360 if (!sTag.isPresent() && !cTag.isPresent()) {
361 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000362 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100363 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
364 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000365 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100366 return false;
367 }
368
Gamze Abakaf59c0912019-04-19 08:24:28 +0000369 //delete Eapol authentication flow with default bandwidth
370 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000371 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000372 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
373 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
374 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000375 filterFuture.thenAcceptAsync(filterStatus -> {
376 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000377 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
378 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000379 }
380 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100381 return true;
382 } else {
383 log.warn("Provisioning failed for subscriber: {}", subscriberId);
384 return false;
385 }
Amit Ghosh31939522018-08-16 13:28:21 +0100386 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100387
alshabibe0559672016-02-21 14:49:51 -0800388 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000389 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
390 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100391 // Check if we can find the connect point to which this subscriber is connected
392 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
393 if (subsPort == null) {
394 log.warn("ConnectPoint for {} not found", subscriberId);
395 return false;
396 }
397
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100398 if (!sTag.isPresent() && !cTag.isPresent()) {
399 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000400 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100401 // Get the uplink port
402 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
403 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000404 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100405 return false;
406 }
407
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000408 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
409 if (!tagInfo.isPresent()) {
410 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
411 subsPort, cTag, sTag, tpId);
412 return false;
413 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000414
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000415 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100416 return true;
417 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000418 log.warn("Removing subscriber is not possible - please check the provided information" +
419 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100420 return false;
421 }
Amit Ghosh31939522018-08-16 13:28:21 +0100422 }
423
424 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000425 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800426 return programmedSubs.stream()
427 .collect(collectingAndThen(
428 groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())),
429 ImmutableMap::copyOf));
Saurav Das82b8e6d2018-10-04 15:25:12 -0700430 }
431
432 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100433 public List<DeviceId> fetchOlts() {
434 // look through all the devices and find the ones that are OLTs as per Sadis
435 List<DeviceId> olts = new ArrayList<>();
436 Iterable<Device> devices = deviceService.getDevices();
437 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700438 if (getOltInfo(d) != null) {
439 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100440 olts.add(d.id());
441 }
442 }
443 return olts;
alshabibe0559672016-02-21 14:49:51 -0800444 }
445
Amit Ghosh31939522018-08-16 13:28:21 +0100446 /**
447 * Finds the connect point to which a subscriber is connected.
448 *
449 * @param id The id of the subscriber, this is the same ID as in Sadis
450 * @return Subscribers ConnectPoint if found else null
451 */
452 private ConnectPoint findSubscriberConnectPoint(String id) {
453
454 Iterable<Device> devices = deviceService.getDevices();
455 for (Device d : devices) {
456 for (Port p : deviceService.getPorts(d.id())) {
457 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
458 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
459 log.debug("Found on device {} port {}", d.id(), p.number());
460 return new ConnectPoint(d.id(), p.number());
461 }
462 }
463 }
464 return null;
465 }
466
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000467 /**
468 * Gets the context of the bandwidth profile information for the given parameter.
469 *
470 * @param bandwidthProfile the bandwidth profile id
471 * @return the context of the bandwidth profile information
472 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000473 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
474 if (bandwidthProfile == null) {
475 return null;
476 }
477 return bpService.get(bandwidthProfile);
478 }
479
Gamze Abaka838d8142019-02-21 07:06:55 +0000480 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000481 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000482 *
483 * @param deviceId the device identifier
484 * @param uplink uplink port of the OLT
485 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000486 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000487 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000488 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000489 PortNumber subscriberPort, UniTagInformation uniTag) {
490
491 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800492
493 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
494 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
495
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000496 VlanId deviceVlan = uniTag.getPonSTag();
497 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000498
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000499 MeterId upstreamMeterId = oltMeterService
500 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
501 MeterId downstreamMeterId = oltMeterService
502 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000503
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000504 ForwardingObjective.Builder upFwd =
505 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
506 ForwardingObjective.Builder downFwd =
507 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
508
509 if (uniTag.getIsIgmpRequired()) {
510 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
511 upstreamMeterId, uniTag, false, true);
512 }
513 if (uniTag.getIsDhcpRequired()) {
514 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
515 upstreamMeterId, uniTag, false, true);
516 }
alshabibbf23a1f2016-01-14 17:27:11 -0800517
alshabib4ceaed32016-03-03 18:00:58 -0800518 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
519 @Override
520 public void onSuccess(Objective objective) {
521 upFuture.complete(null);
522 }
alshabibbf23a1f2016-01-14 17:27:11 -0800523
alshabib4ceaed32016-03-03 18:00:58 -0800524 @Override
525 public void onError(Objective objective, ObjectiveError error) {
526 upFuture.complete(error);
527 }
528 }));
529
530 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
531 @Override
532 public void onSuccess(Objective objective) {
533 downFuture.complete(null);
534 }
535
536 @Override
537 public void onError(Objective objective, ObjectiveError error) {
538 downFuture.complete(error);
539 }
540 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800541
542 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000543 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800544 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000545 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
546 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
547 uniTag.getTechnologyProfileId(), subscriberPort);
548 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800549 } else if (downStatus != null) {
550 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000551 "on port {} failed downstream uninstallation: {}",
552 subscriberVlan, deviceId, subscriberPort, downStatus);
553 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800554 } else if (upStatus != null) {
555 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000556 "on port {} failed upstream uninstallation: {}",
557 subscriberVlan, deviceId, subscriberPort, upStatus);
558 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800559 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000560 Port port = deviceService.getPort(deviceId, subscriberPort);
561 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
562 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800563 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700564 }
565
Gamze Abaka838d8142019-02-21 07:06:55 +0000566 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000567 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000568 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000569 * @param connectPoint the connection point of the subscriber
570 * @param uplinkPort uplink port of the OLT (the nni port)
571 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000572 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000573 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
574 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000575
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000576 log.info("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000577
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000578 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
579 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
580 return;
581 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000582
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000583 DeviceId deviceId = connectPoint.deviceId();
584 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000585
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000586 for (UniTagInformation uniTag : sub.uniTagList()) {
587 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
588 }
589 }
alshabib3ea82642016-01-12 18:06:53 -0800590
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000591 /**
592 * Finds the uni tag information and provisions the found information.
593 * If the uni tag information is not found, returns
594 *
595 * @param deviceId the access device id
596 * @param uplinkPort the nni port
597 * @param subscriberPort the uni port
598 * @param innerVlan the pon c tag
599 * @param outerVlan the pon s tag
600 * @param tpId the technology profile id
601 */
602 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
603 PortNumber subscriberPort,
604 VlanId innerVlan,
605 VlanId outerVlan,
606 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700607
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000608 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
609 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
610 if (!gotTagInformation.isPresent()) {
611 return;
612 }
613 UniTagInformation tagInformation = gotTagInformation.get();
614 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
615 }
alshabib3ea82642016-01-12 18:06:53 -0800616
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000617 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800618 if (add) {
619 programmedSubs.put(connectPoint, tagInformation);
620 } else {
621 programmedSubs.remove(connectPoint, tagInformation);
622 }
Jonathan Harte533a422015-10-20 17:31:24 -0700623 }
624
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000625 /**
626 * Installs a uni tag information flow.
627 *
628 * @param deviceId the access device id
629 * @param uplinkPort the nni port
630 * @param subscriberPort the uni port
631 * @param tagInfo the uni tag information
632 */
633 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
634 UniTagInformation tagInfo) {
635
636 log.info("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
637
638 Port port = deviceService.getPort(deviceId, subscriberPort);
639
640 if (multicastServiceName.equals(tagInfo.getServiceName())) {
641 // IGMP flows are taken care of along with VOD service
642 // Please note that for each service, Subscriber Registered event will be sent
643 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
644 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
645 tagInfo.getTechnologyProfileId()));
646 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000647 }
648
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100649 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
650
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000651 BandwidthProfileInformation upstreamBpInfo =
652 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
653 BandwidthProfileInformation downstreamBpInfo =
654 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000655
656 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
657 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000658 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
659 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000660
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000661 MeterId upstreamMeterId = oltMeterService.createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
662
663 MeterId downstreamMeterId = oltMeterService.createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000664
665 upstreamMeterFuture.thenAcceptAsync(result -> {
666 if (result == null) {
667 log.info("Upstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000668 "Sending subscriber flows.", upstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000669
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000670 ForwardingObjective.Builder upFwd =
671 oltFlowService.createUpBuilder(uplinkPort, subscriberPort, upstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000672
673 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
674 @Override
675 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000676 log.info("Upstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000677 upFuture.complete(null);
678 }
679
680 @Override
681 public void onError(Objective objective, ObjectiveError error) {
682 upFuture.complete(error);
683 }
684 }));
685
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000686 } else if (upstreamBpInfo == null) {
687 log.warn("No meter installed since no Upstream BW Profile definition found for " +
688 "ctag {} stag {} tpId {} and Device/port: {}:{}",
689 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
690 tagInfo.getTechnologyProfileId(),
691 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000692 } else {
693 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000694 "Result {} and MeterId {}", result, upstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000695 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800696 }).exceptionally(ex -> {
697 log.error("Upstream flow failed: " + ex.getMessage());
698 upFuture.complete(ObjectiveError.UNKNOWN);
699 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000700 });
701
702 downsteamMeterFuture.thenAcceptAsync(result -> {
703 if (result == null) {
704 log.info("Downstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000705 "Sending subscriber flows.", downstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000706
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000707 ForwardingObjective.Builder downFwd =
708 oltFlowService.createDownBuilder(uplinkPort, subscriberPort, downstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000709
710 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
711 @Override
712 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000713 log.info("Downstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000714 downFuture.complete(null);
715 }
716
717 @Override
718 public void onError(Objective objective, ObjectiveError error) {
719 downFuture.complete(error);
720 }
721 }));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000722
723 } else if (downstreamBpInfo == null) {
724 log.warn("No meter installed since no Downstream BW Profile definition found for " +
725 "ctag {} stag {} tpId {} and Device/port: {}:{}",
726 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
727 tagInfo.getTechnologyProfileId(),
728 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000729 } else {
730 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000731 "Result {} and MeterId {}", result, downstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000732 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800733 }).exceptionally(ex -> {
734 log.error("Downstream flow failed: " + ex.getMessage());
735 downFuture.complete(ObjectiveError.UNKNOWN);
736 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000737 });
738
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100739 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000740 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100741 if (downStatus != null) {
742 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000743 "on port {} failed downstream installation: {}",
744 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, downStatus);
745 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100746 } else if (upStatus != null) {
747 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000748 "on port {} failed upstream installation: {}",
749 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, upStatus);
750 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000751 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000752 log.info("Upstream and downstream data plane flows are installed successfully.");
753 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPort,
754 tagInfo.getUpstreamBandwidthProfile(),
755 null, tagInfo.getPonCTag(), true);
756 if (tagInfo.getIsDhcpRequired()) {
757 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
758 upstreamMeterId, tagInfo, true, true);
759 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000760
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000761 if (tagInfo.getIsIgmpRequired()) {
762 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort, upstreamMeterId, tagInfo,
763 true, true);
764 }
765 updateProgrammedSubscriber(cp, tagInfo, true);
766 post(new AccessDeviceEvent(type, deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
767 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100768 }
769 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100770 }
771
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000772 /**
773 * Checks the subscriber uni tag list and find the uni tag information.
774 * using the pon c tag, pon s tag and the technology profile id
775 * May return Optional<null>
776 *
777 * @param cp the connection point of the subscriber
778 * @param innerVlan pon c tag
779 * @param outerVlan pon s tag
780 * @param tpId the technology profile id
781 * @return the found uni tag information
782 */
783 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
784 int tpId) {
785 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
786 outerVlan, tpId);
787 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000788 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000789 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
790 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000791 }
792
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000793 List<UniTagInformation> uniTagList = subInfo.uniTagList();
794 if (uniTagList == null) {
795 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
796 return Optional.empty();
797 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100798
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000799 UniTagInformation service = null;
800 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
801 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
802 && tpId == tagInfo.getTechnologyProfileId()) {
803 service = tagInfo;
804 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700805 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000806 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000807
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000808 if (service == null) {
809 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
810 innerVlan, outerVlan, tpId);
811 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000812 }
813
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000814 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100815 }
816
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100817 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700818 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
819 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100820 *
821 * @param dev Device to look for
822 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700823 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100824 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000825 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000826 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100827
828 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700829 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100830 for (Port p : deviceService.getPorts(dev.id())) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800831 if (PortNumber.LOCAL.equals(p.number()) || !p.isEnabled()) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000832 continue;
833 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100834 if (isUniPort(dev, p)) {
Andrea Campanellaa2491782020-03-13 18:09:31 +0100835 if (!programmedSubs.containsKey(new ConnectPoint(dev.id(), p.number()))) {
836 log.info("Creating Eapol for the uni {}", p);
837 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
838 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
839 } else {
840 log.debug("Subscriber Eapol for UNI port {} on device {} is already " +
841 "provisioned, not installing default", p.number(), dev.id());
842 }
Jonathan Hart403372d2018-08-22 11:44:13 -0700843 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000844 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100845 }
846 }
847 }
848 }
849
Jonathan Hart403372d2018-08-22 11:44:13 -0700850
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100851 /**
852 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000853 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100854 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
855 * this logic needs to be changed
856 *
857 * @param dev Device to look for
858 * @return The uplink Port of the OLT
859 */
860 private Port getUplinkPort(Device dev) {
861 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000862 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700863 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700864 if (deviceInfo == null) {
865 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000866 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700867 return null;
868 }
869 // Return the port that has been configured as the uplink port of this OLT in Sadis
kdarapuaa5da252020-04-10 15:58:05 +0530870 Optional<Port> optionalPort = deviceService.getPorts(dev.id()).stream()
871 .filter(port -> isNniPort(port) ||
872 (port.number().toLong() == deviceInfo.uplinkPort()))
873 .findFirst();
874 if (optionalPort.isPresent()) {
875 log.trace("getUplinkPort: Found port {}", optionalPort.get());
876 return optionalPort.get();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100877 }
878
Saurav Daseae48de2019-06-19 13:26:15 -0700879 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100880 return null;
881 }
882
883 /**
884 * Return the subscriber on a port.
885 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800886 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100887 * @return subscriber if found else null
888 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800889 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
890 Port port = deviceService.getPort(cp);
891 checkNotNull(port, "Invalid connect point");
892 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100893 return subsService.get(portName);
894 }
895
Gamze Abakaad329652018-12-20 10:12:21 +0000896 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000897 * Checks whether the given port of the device is a uni port or not.
898 *
899 * @param d the access device
900 * @param p the port of the device
901 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000902 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100903 private boolean isUniPort(Device d, Port p) {
904 Port ulPort = getUplinkPort(d);
905 if (ulPort != null) {
906 return (ulPort.number().toLong() != p.number().toLong());
907 }
Thomas Lee Sd7735f92020-02-20 19:21:47 +0530908 //handles a special case where NNI port is misconfigured in SADIS and getUplinkPort(d) returns null
909 //checks whether the port name starts with nni- which is the signature of an NNI Port
910 if (p.annotations().value(AnnotationKeys.PORT_NAME) != null &&
911 p.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI)) {
912 log.error("NNI port number {} is not matching with configured value", p.number().toLong());
913 return false;
914 }
915 return true;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700916 }
917
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000918 /**
919 * Gets the given device details from SADIS.
920 * If the device is not found, returns null
921 *
922 * @param dev the access device
923 * @return the olt information
924 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700925 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
926 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +0000927 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -0700928 }
929
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200930 // Custom-built function, when the device is not available we need a fallback mechanism
931 private boolean isLocalLeader(DeviceId deviceId) {
932 if (!mastershipService.isLocalMaster(deviceId)) {
933 // When the device is available we just check the mastership
934 if (deviceService.isAvailable(deviceId)) {
935 return false;
936 }
937 // Fallback with Leadership service - device id is used as topic
938 NodeId leader = leadershipService.runForLeadership(
939 deviceId.toString()).leaderNodeId();
940 // Verify if this node is the leader
941 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800942 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200943 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800944 }
945
kdarapuaa5da252020-04-10 15:58:05 +0530946 private boolean isNniPort(Port port) {
947 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
948 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
949 }
950 return false;
951 }
952
alshabibf0e7e702015-05-30 18:22:36 -0700953 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800954 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
955
alshabibf0e7e702015-05-30 18:22:36 -0700956 @Override
957 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700958 eventExecutor.execute(() -> {
959 DeviceId devId = event.subject().id();
960 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +0000961 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000962 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -0700963
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000964 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
965 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
966 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700967 return;
968 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700969
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200970 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800971 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200972 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
973 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
974 log.info("Cleaning local state for non master instance upon " +
975 "device disconnection {}", devId);
976 programmedDevices.remove(devId);
977 // Since no mastership of the device is present upon disconnection
978 // the method in the FlowRuleManager only empties the local copy
979 // of the DeviceFlowTable thus this method needs to get called
980 // on every instance, see how it's done in the InternalDeviceListener
981 // in FlowRuleManager: no mastership check for purgeOnDisconnection
982 flowRuleService.purgeFlowRules(devId);
983 return;
984 } else if (!isLocalLeader) {
985 log.info("not handling event because of not local leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800986 return;
987 }
988
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000989 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
990
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700991 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800992 // it's possible that we got an event for a previously
993 // programmed OLT that is no longer available in SADIS
994 // we let such events go through
995 if (!programmedDevices.contains(devId)) {
996 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000997 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -0800998 return;
999 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001000 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001001
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001002 switch (event.type()) {
1003 //TODO: Port handling and bookkeeping should be improved once
1004 // olt firmware handles correct behaviour.
1005 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001006 if (isUniPort(dev, port)) {
1007 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001008
1009 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1010 log.info("eapol will be sent for port added {}", port);
1011 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1012 null,
1013 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001014 }
1015 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001016 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1017 if (deviceInfo != null) {
1018 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1019 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001020 }
1021 break;
1022 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001023 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001024 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001025 log.info("eapol will be send for port {} removed", port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001026 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1027 null,
1028 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001029
Gamze Abaka838d8142019-02-21 07:06:55 +00001030 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001031 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001032 break;
1033 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001034 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001035 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1036 if (deviceInfo != null && port.isEnabled()) {
1037 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1038 port.number());
1039 oltFlowService.processNniFilteringObjectives(dev.id(),
1040 port.number(), true);
1041 }
1042 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001043 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001044 ConnectPoint cp = new ConnectPoint(devId, port.number());
1045 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001046 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001047 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001048 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001049 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001050 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001051 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001052 null,
1053 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1054 port.isEnabled());
1055 }
1056 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001057 log.info("eapol will be {} for dev/port updated {}/{}",
1058 (port.isEnabled()) ? "added" : "removed",
1059 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001060 uniTagInformationSet.forEach(uniTag ->
1061 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1062 uniTag.getUpstreamBandwidthProfile(), null,
1063 uniTag.getPonCTag(), port.isEnabled()));
1064 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001065 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001066 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001067 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001068 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001069 }
alshabibbb83aa22016-02-10 15:08:23 -08001070 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001071 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001072 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001073 break;
1074 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001075 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001076 break;
1077 case DEVICE_AVAILABILITY_CHANGED:
1078 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001079 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001080 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001081 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301082 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001083 log.info("Handling controlled device disconnection .. "
1084 + "flushing all state for dev:{}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301085 handleDeviceDisconnection(dev, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001086 } else {
1087 log.info("Disconnected device has available ports .. "
1088 + "assuming temporary disconnection, "
1089 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301090 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001091 }
1092 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001093 default:
1094 return;
1095 }
1096 });
alshabibf0e7e702015-05-30 18:22:36 -07001097 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001098
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001099 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1100 deviceService.getPorts(device.id()).stream()
1101 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1102 .filter(p -> isUniPort(device, p))
1103 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1104 }
1105
1106 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
1107 programmedDevices.remove(device.id());
1108 removeAllSubscribers(device.id());
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301109 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001110 oltMeterService.clearMeters(device.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001111 post(new AccessDeviceEvent(
1112 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1113 null, null, null));
1114 if (sendUniEvent) {
1115 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001116 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001117 }
1118
1119 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1120 post(new AccessDeviceEvent(
1121 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1122 null, null, null));
1123 programmedDevices.add(dev.id());
1124 checkAndCreateDeviceFlows(dev);
1125 if (sendUniEvent) {
1126 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1127 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001128 }
Gamze Abakada282b42019-03-11 13:16:48 +00001129
1130 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001131 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1132 .filter(e -> e.getKey().deviceId().equals(deviceId))
1133 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001134
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001135 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001136 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001137
Gamze Abaka641fc072018-09-04 09:16:27 +00001138 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001139
1140 private class InternalClusterListener implements ClusterEventListener {
1141
1142 @Override
1143 public void event(ClusterEvent event) {
1144 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1145 hasher.addServer(event.subject().id());
1146 }
1147 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1148 hasher.removeServer(event.subject().id());
1149 }
1150 }
1151 }
Hardik Windlass395ff372019-06-13 05:16:00 +00001152}