blob: 3e2aa834f8c1ac897b34893cf8227b2c04c8bd0f [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
Andrea Campanella7c49b792020-05-11 11:36:53 +0200509 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
510 upstreamMeterId, uniTag, false, true);
511 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
512 upstreamMeterId, uniTag, false, true);
alshabibbf23a1f2016-01-14 17:27:11 -0800513
alshabib4ceaed32016-03-03 18:00:58 -0800514 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
515 @Override
516 public void onSuccess(Objective objective) {
517 upFuture.complete(null);
518 }
alshabibbf23a1f2016-01-14 17:27:11 -0800519
alshabib4ceaed32016-03-03 18:00:58 -0800520 @Override
521 public void onError(Objective objective, ObjectiveError error) {
522 upFuture.complete(error);
523 }
524 }));
525
526 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
527 @Override
528 public void onSuccess(Objective objective) {
529 downFuture.complete(null);
530 }
531
532 @Override
533 public void onError(Objective objective, ObjectiveError error) {
534 downFuture.complete(error);
535 }
536 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800537
538 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000539 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800540 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000541 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
542 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
543 uniTag.getTechnologyProfileId(), subscriberPort);
544 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800545 } else if (downStatus != null) {
546 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000547 "on port {} failed downstream uninstallation: {}",
548 subscriberVlan, deviceId, subscriberPort, downStatus);
549 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800550 } else if (upStatus != null) {
551 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000552 "on port {} failed upstream uninstallation: {}",
553 subscriberVlan, deviceId, subscriberPort, upStatus);
554 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800555 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000556 Port port = deviceService.getPort(deviceId, subscriberPort);
557 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
558 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800559 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700560 }
561
Gamze Abaka838d8142019-02-21 07:06:55 +0000562 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000563 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000564 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000565 * @param connectPoint the connection point of the subscriber
566 * @param uplinkPort uplink port of the OLT (the nni port)
567 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000568 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000569 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
570 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000571
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000572 log.info("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000573
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000574 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
575 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
576 return;
577 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000578
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000579 DeviceId deviceId = connectPoint.deviceId();
580 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000581
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000582 for (UniTagInformation uniTag : sub.uniTagList()) {
583 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
584 }
585 }
alshabib3ea82642016-01-12 18:06:53 -0800586
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000587 /**
588 * Finds the uni tag information and provisions the found information.
589 * If the uni tag information is not found, returns
590 *
591 * @param deviceId the access device id
592 * @param uplinkPort the nni port
593 * @param subscriberPort the uni port
594 * @param innerVlan the pon c tag
595 * @param outerVlan the pon s tag
596 * @param tpId the technology profile id
597 */
598 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
599 PortNumber subscriberPort,
600 VlanId innerVlan,
601 VlanId outerVlan,
602 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700603
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000604 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
605 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
606 if (!gotTagInformation.isPresent()) {
607 return;
608 }
609 UniTagInformation tagInformation = gotTagInformation.get();
610 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
611 }
alshabib3ea82642016-01-12 18:06:53 -0800612
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000613 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800614 if (add) {
615 programmedSubs.put(connectPoint, tagInformation);
616 } else {
617 programmedSubs.remove(connectPoint, tagInformation);
618 }
Jonathan Harte533a422015-10-20 17:31:24 -0700619 }
620
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000621 /**
622 * Installs a uni tag information flow.
623 *
624 * @param deviceId the access device id
625 * @param uplinkPort the nni port
626 * @param subscriberPort the uni port
627 * @param tagInfo the uni tag information
628 */
629 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
630 UniTagInformation tagInfo) {
631
632 log.info("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
633
634 Port port = deviceService.getPort(deviceId, subscriberPort);
635
636 if (multicastServiceName.equals(tagInfo.getServiceName())) {
637 // IGMP flows are taken care of along with VOD service
638 // Please note that for each service, Subscriber Registered event will be sent
639 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
640 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
641 tagInfo.getTechnologyProfileId()));
642 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000643 }
644
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100645 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
646
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000647 BandwidthProfileInformation upstreamBpInfo =
648 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
649 BandwidthProfileInformation downstreamBpInfo =
650 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000651
652 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
653 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000654 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
655 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000656
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000657 MeterId upstreamMeterId = oltMeterService.createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
658
659 MeterId downstreamMeterId = oltMeterService.createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000660
661 upstreamMeterFuture.thenAcceptAsync(result -> {
662 if (result == null) {
663 log.info("Upstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000664 "Sending subscriber flows.", upstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000665
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000666 ForwardingObjective.Builder upFwd =
667 oltFlowService.createUpBuilder(uplinkPort, subscriberPort, upstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000668
669 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
670 @Override
671 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000672 log.info("Upstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000673 upFuture.complete(null);
674 }
675
676 @Override
677 public void onError(Objective objective, ObjectiveError error) {
678 upFuture.complete(error);
679 }
680 }));
681
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000682 } else if (upstreamBpInfo == null) {
683 log.warn("No meter installed since no Upstream BW Profile definition found for " +
684 "ctag {} stag {} tpId {} and Device/port: {}:{}",
685 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
686 tagInfo.getTechnologyProfileId(),
687 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000688 } else {
689 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000690 "Result {} and MeterId {}", result, upstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000691 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800692 }).exceptionally(ex -> {
693 log.error("Upstream flow failed: " + ex.getMessage());
694 upFuture.complete(ObjectiveError.UNKNOWN);
695 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000696 });
697
698 downsteamMeterFuture.thenAcceptAsync(result -> {
699 if (result == null) {
700 log.info("Downstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000701 "Sending subscriber flows.", downstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000702
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000703 ForwardingObjective.Builder downFwd =
704 oltFlowService.createDownBuilder(uplinkPort, subscriberPort, downstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000705
706 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
707 @Override
708 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000709 log.info("Downstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000710 downFuture.complete(null);
711 }
712
713 @Override
714 public void onError(Objective objective, ObjectiveError error) {
715 downFuture.complete(error);
716 }
717 }));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000718
719 } else if (downstreamBpInfo == null) {
720 log.warn("No meter installed since no Downstream BW Profile definition found for " +
721 "ctag {} stag {} tpId {} and Device/port: {}:{}",
722 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
723 tagInfo.getTechnologyProfileId(),
724 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000725 } else {
726 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000727 "Result {} and MeterId {}", result, downstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000728 }
Daniele Moro7cbf4312020-03-06 17:24:12 -0800729 }).exceptionally(ex -> {
730 log.error("Downstream flow failed: " + ex.getMessage());
731 downFuture.complete(ObjectiveError.UNKNOWN);
732 return null;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000733 });
734
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100735 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000736 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100737 if (downStatus != null) {
738 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000739 "on port {} failed downstream installation: {}",
740 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, downStatus);
741 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100742 } else if (upStatus != null) {
743 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000744 "on port {} failed upstream installation: {}",
745 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, upStatus);
746 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000747 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000748 log.info("Upstream and downstream data plane flows are installed successfully.");
749 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPort,
750 tagInfo.getUpstreamBandwidthProfile(),
751 null, tagInfo.getPonCTag(), true);
Andrea Campanella7c49b792020-05-11 11:36:53 +0200752 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
753 upstreamMeterId, tagInfo, true, true);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000754
Andrea Campanella7c49b792020-05-11 11:36:53 +0200755 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort, upstreamMeterId, tagInfo,
756 true, true);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000757 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
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200922 // Custom-built function, when the device is not available we need a fallback mechanism
923 private boolean isLocalLeader(DeviceId deviceId) {
924 if (!mastershipService.isLocalMaster(deviceId)) {
925 // When the device is available we just check the mastership
926 if (deviceService.isAvailable(deviceId)) {
927 return false;
928 }
929 // Fallback with Leadership service - device id is used as topic
930 NodeId leader = leadershipService.runForLeadership(
931 deviceId.toString()).leaderNodeId();
932 // Verify if this node is the leader
933 return clusterService.getLocalNode().id().equals(leader);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800934 }
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200935 return true;
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800936 }
937
kdarapuaa5da252020-04-10 15:58:05 +0530938 private boolean isNniPort(Port port) {
939 if (port.annotations().keys().contains(AnnotationKeys.PORT_NAME)) {
940 return port.annotations().value(AnnotationKeys.PORT_NAME).contains(NNI);
941 }
942 return false;
943 }
944
alshabibf0e7e702015-05-30 18:22:36 -0700945 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800946 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
947
alshabibf0e7e702015-05-30 18:22:36 -0700948 @Override
949 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700950 eventExecutor.execute(() -> {
951 DeviceId devId = event.subject().id();
952 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +0000953 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000954 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -0700955
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000956 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
957 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
958 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700959 return;
960 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700961
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200962 boolean isLocalLeader = isLocalLeader(devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800963 // Only handle the event if the device belongs to us
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +0200964 if (!isLocalLeader && event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)
965 && !deviceService.isAvailable(devId) && deviceService.getPorts(devId).isEmpty()) {
966 log.info("Cleaning local state for non master instance upon " +
967 "device disconnection {}", devId);
968 programmedDevices.remove(devId);
969 // Since no mastership of the device is present upon disconnection
970 // the method in the FlowRuleManager only empties the local copy
971 // of the DeviceFlowTable thus this method needs to get called
972 // on every instance, see how it's done in the InternalDeviceListener
973 // in FlowRuleManager: no mastership check for purgeOnDisconnection
974 flowRuleService.purgeFlowRules(devId);
975 return;
976 } else if (!isLocalLeader) {
977 log.info("not handling event because of not local leader for {}", devId);
Jonathan Hart4f178fa2020-02-03 10:46:01 -0800978 return;
979 }
980
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000981 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
982
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700983 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800984 // it's possible that we got an event for a previously
985 // programmed OLT that is no longer available in SADIS
986 // we let such events go through
987 if (!programmedDevices.contains(devId)) {
988 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000989 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -0800990 return;
991 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700992 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700993
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700994 switch (event.type()) {
995 //TODO: Port handling and bookkeeping should be improved once
996 // olt firmware handles correct behaviour.
997 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000998 if (isUniPort(dev, port)) {
999 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001000
1001 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
1002 log.info("eapol will be sent for port added {}", port);
1003 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1004 null,
1005 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001006 }
1007 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001008 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1009 if (deviceInfo != null) {
1010 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
1011 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001012 }
1013 break;
1014 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001015 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001016 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellaaf39b4c2020-05-13 14:07:44 +02001017 log.info("eapol will be send for port {} removed", port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001018 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1019 null,
1020 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -07001021
Gamze Abaka838d8142019-02-21 07:06:55 +00001022 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001023 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001024 break;
1025 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001026 if (!isUniPort(dev, port)) {
Saurav Das9da7d522020-03-23 19:14:35 -07001027 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
1028 if (deviceInfo != null && port.isEnabled()) {
1029 log.debug("NNI dev/port {}/{} enabled", dev.id(),
1030 port.number());
1031 oltFlowService.processNniFilteringObjectives(dev.id(),
1032 port.number(), true);
1033 }
1034 return;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001035 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001036 ConnectPoint cp = new ConnectPoint(devId, port.number());
1037 Collection<? extends UniTagInformation> uniTagInformationSet = programmedSubs.get(cp).value();
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001038 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Saurav Dasb776aef2020-03-09 14:29:46 -07001039 if (!port.number().equals(PortNumber.LOCAL)) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001040 log.info("eapol will be {} for dev/port updated {}/{} with default vlan {}",
Saurav Dasb776aef2020-03-09 14:29:46 -07001041 (port.isEnabled()) ? "added" : "removed",
Matteo Scandolo3a037a32020-04-01 12:17:50 -07001042 devId, port.number(), EAPOL_DEFAULT_VLAN);
Matteo Scandolo27c471c2020-02-11 16:41:53 -08001043 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001044 null,
1045 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
1046 port.isEnabled());
1047 }
1048 } else {
Saurav Dasb776aef2020-03-09 14:29:46 -07001049 log.info("eapol will be {} for dev/port updated {}/{}",
1050 (port.isEnabled()) ? "added" : "removed",
1051 devId, port.number());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001052 uniTagInformationSet.forEach(uniTag ->
1053 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
1054 uniTag.getUpstreamBandwidthProfile(), null,
1055 uniTag.getPonCTag(), port.isEnabled()));
1056 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001057 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +00001058 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001059 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001060 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001061 }
alshabibbb83aa22016-02-10 15:08:23 -08001062 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001063 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001064 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001065 break;
1066 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001067 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001068 break;
1069 case DEVICE_AVAILABILITY_CHANGED:
1070 if (deviceService.isAvailable(devId)) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001071 log.info("Handling available device: {}", dev.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001072 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001073 } else {
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301074 if (deviceService.getPorts(devId).isEmpty()) {
Saurav Dasbd3b6712020-03-31 23:28:35 -07001075 log.info("Handling controlled device disconnection .. "
1076 + "flushing all state for dev:{}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301077 handleDeviceDisconnection(dev, false);
Saurav Dasbd3b6712020-03-31 23:28:35 -07001078 } else {
1079 log.info("Disconnected device has available ports .. "
1080 + "assuming temporary disconnection, "
1081 + "retaining state for device {}", devId);
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301082 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001083 }
1084 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001085 default:
1086 return;
1087 }
1088 });
alshabibf0e7e702015-05-30 18:22:36 -07001089 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001090
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001091 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
1092 deviceService.getPorts(device.id()).stream()
1093 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
1094 .filter(p -> isUniPort(device, p))
1095 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
1096 }
1097
1098 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
1099 programmedDevices.remove(device.id());
1100 removeAllSubscribers(device.id());
Hardik Windlassa58fbee2020-03-12 18:33:55 +05301101 flowRuleService.purgeFlowRules(device.id());
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001102 oltMeterService.clearMeters(device.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001103 post(new AccessDeviceEvent(
1104 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
1105 null, null, null));
1106 if (sendUniEvent) {
1107 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +00001108 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001109 }
1110
1111 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
1112 post(new AccessDeviceEvent(
1113 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
1114 null, null, null));
1115 programmedDevices.add(dev.id());
1116 checkAndCreateDeviceFlows(dev);
1117 if (sendUniEvent) {
1118 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
1119 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001120 }
Gamze Abakada282b42019-03-11 13:16:48 +00001121
1122 private void removeAllSubscribers(DeviceId deviceId) {
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001123 List<Map.Entry<ConnectPoint, UniTagInformation>> subs = programmedSubs.stream()
1124 .filter(e -> e.getKey().deviceId().equals(deviceId))
1125 .collect(toList());
Gamze Abakada282b42019-03-11 13:16:48 +00001126
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001127 subs.forEach(e -> programmedSubs.remove(e.getKey(), e.getValue()));
Gamze Abakada282b42019-03-11 13:16:48 +00001128 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001129
Gamze Abaka641fc072018-09-04 09:16:27 +00001130 }
Jonathan Hart4f178fa2020-02-03 10:46:01 -08001131
1132 private class InternalClusterListener implements ClusterEventListener {
1133
1134 @Override
1135 public void event(ClusterEvent event) {
1136 if (event.type() == ClusterEvent.Type.INSTANCE_READY) {
1137 hasher.addServer(event.subject().id());
1138 }
1139 if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED) {
1140 hasher.removeServer(event.subject().id());
1141 }
1142 }
1143 }
Hardik Windlass395ff372019-06-13 05:16:00 +00001144}