blob: e392b41494b8399d71e580ed2282ab89aabd96e4 [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
Carmelo Casconeca931162019-07-15 18:22:24 -070018import com.google.common.collect.ImmutableMap;
Carmelo Casconeca931162019-07-15 18:22:24 -070019import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -070021import org.onlab.packet.VlanId;
alshabibe0559672016-02-21 14:49:51 -080022import org.onosproject.cfg.ComponentConfigService;
alshabibf0e7e702015-05-30 18:22:36 -070023import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080025import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080026import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010027import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070028import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010029import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070030import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080031import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070032import org.onosproject.net.PortNumber;
33import org.onosproject.net.device.DeviceEvent;
34import org.onosproject.net.device.DeviceListener;
35import org.onosproject.net.device.DeviceService;
alshabibf0e7e702015-05-30 18:22:36 -070036import org.onosproject.net.flowobjective.FlowObjectiveService;
37import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080038import org.onosproject.net.flowobjective.Objective;
39import org.onosproject.net.flowobjective.ObjectiveContext;
40import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Daseae48de2019-06-19 13:26:15 -070041import org.onosproject.net.meter.MeterId;
alshabib36a4d732016-06-01 16:03:59 -070042import org.opencord.olt.AccessDeviceEvent;
43import org.opencord.olt.AccessDeviceListener;
44import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010045import org.opencord.olt.AccessSubscriberId;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000046import org.opencord.olt.internalapi.AccessDeviceFlowService;
47import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000048import org.opencord.sadis.BandwidthProfileInformation;
49import org.opencord.sadis.BaseInformationService;
50import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010051import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000052import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080053import org.osgi.service.component.ComponentContext;
Carmelo Casconeca931162019-07-15 18:22:24 -070054import org.osgi.service.component.annotations.Activate;
55import org.osgi.service.component.annotations.Component;
56import org.osgi.service.component.annotations.Deactivate;
57import org.osgi.service.component.annotations.Modified;
58import org.osgi.service.component.annotations.Reference;
59import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibf0e7e702015-05-30 18:22:36 -070060import org.slf4j.Logger;
61
Carmelo Casconeca931162019-07-15 18:22:24 -070062import java.util.ArrayList;
Carmelo Casconeca931162019-07-15 18:22:24 -070063import java.util.Dictionary;
64import java.util.List;
65import java.util.Map;
66import java.util.Objects;
67import java.util.Optional;
68import java.util.Properties;
69import java.util.Set;
70import java.util.concurrent.CompletableFuture;
Carmelo Casconeca931162019-07-15 18:22:24 -070071import java.util.concurrent.ExecutorService;
72import java.util.concurrent.Executors;
73import java.util.stream.Collectors;
74
75import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Casconeca931162019-07-15 18:22:24 -070076import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
77import static org.onlab.util.Tools.get;
78import static org.onlab.util.Tools.groupedThreads;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000079import static org.opencord.olt.impl.OsgiPropertyConstants.*;
Carmelo Casconeca931162019-07-15 18:22:24 -070080import static org.slf4j.LoggerFactory.getLogger;
alshabibf0e7e702015-05-30 18:22:36 -070081
82/**
Jonathan Harte533a422015-10-20 17:31:24 -070083 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -070084 */
Carmelo Casconeca931162019-07-15 18:22:24 -070085@Component(immediate = true,
86 property = {
Carmelo Casconeca931162019-07-15 18:22:24 -070087 DEFAULT_BP_ID + ":String=" + DEFAULT_BP_ID_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +000088 DEFAULT_MCAST_SERVICE_NAME + ":String=" + DEFAULT_MCAST_SERVICE_NAME_DEFAULT,
Carmelo Casconeca931162019-07-15 18:22:24 -070089 })
alshabib8e4fd2f2016-01-12 15:55:53 -080090public class Olt
91 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
92 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -080093 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -080094
Gamze Abakada282b42019-03-11 13:16:48 +000095 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka838d8142019-02-21 07:06:55 +000096 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -080097
alshabibf0e7e702015-05-30 18:22:36 -070098 private final Logger log = getLogger(getClass());
99
Carmelo Casconeca931162019-07-15 18:22:24 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700101 protected FlowObjectiveService flowObjectiveService;
102
Carmelo Casconeca931162019-07-15 18:22:24 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib09753b52016-03-04 14:55:19 -0800104 protected MastershipService mastershipService;
105
Carmelo Casconeca931162019-07-15 18:22:24 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700107 protected DeviceService deviceService;
108
Carmelo Casconeca931162019-07-15 18:22:24 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibf0e7e702015-05-30 18:22:36 -0700110 protected CoreService coreService;
111
Carmelo Casconeca931162019-07-15 18:22:24 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe0559672016-02-21 14:49:51 -0800113 protected ComponentConfigService componentConfigService;
114
Carmelo Casconeca931162019-07-15 18:22:24 -0700115 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000116 protected SadisService sadisService;
117
Carmelo Casconeca931162019-07-15 18:22:24 -0700118 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000119 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800120
Carmelo Casconeca931162019-07-15 18:22:24 -0700121 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000122 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000123
Carmelo Casconeca931162019-07-15 18:22:24 -0700124 /**
Carmelo Cascone95ff5122019-11-14 14:19:13 -0800125 * Default bandwidth profile id that is used for authentication trap flows.
Carmelo Casconeca931162019-07-15 18:22:24 -0700126 **/
127 protected String defaultBpId = DEFAULT_BP_ID_DEFAULT;
Gamze Abakaad329652018-12-20 10:12:21 +0000128
Carmelo Casconeca931162019-07-15 18:22:24 -0700129 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000130 * Deleting Meters based on flow count statistics.
Carmelo Casconeca931162019-07-15 18:22:24 -0700131 **/
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000132 protected String multicastServiceName = DEFAULT_MCAST_SERVICE_NAME;
Gamze Abaka33feef52019-02-27 08:16:47 +0000133
alshabibf0e7e702015-05-30 18:22:36 -0700134 private final DeviceListener deviceListener = new InternalDeviceListener();
135
Gamze Abaka641fc072018-09-04 09:16:27 +0000136 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
137 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700138
Gamze Abaka641fc072018-09-04 09:16:27 +0000139 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000140 groupedThreads("onos/olt-service",
141 "olt-installer-%d"));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100142
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700143 protected ExecutorService eventExecutor;
144
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000145 private Map<ConnectPoint, Set<UniTagInformation>> programmedSubs;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800146
alshabibf0e7e702015-05-30 18:22:36 -0700147 @Activate
alshabibe0559672016-02-21 14:49:51 -0800148 public void activate(ComponentContext context) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000149 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
150 "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800151 modified(context);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000152 ApplicationId appId = coreService.registerApplication(APP_NAME);
Saurav Das62ad75e2019-03-05 12:22:22 -0800153
154 // ensure that flow rules are purged from flow-store upon olt-disconnection
155 // when olt reconnects, the port-numbers may change for the ONUs
156 // making flows pushed earlier invalid
157 componentConfigService
158 .preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000159 "purgeOnDisconnection", "true");
Gamze Abakada282b42019-03-11 13:16:48 +0000160 componentConfigService
161 .preSetProperty("org.onosproject.net.meter.impl.MeterManager",
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000162 "purgeOnDisconnection", "true");
alshabibe0559672016-02-21 14:49:51 -0800163 componentConfigService.registerProperties(getClass());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700164 programmedSubs = Maps.newConcurrentMap();
alshabibc4dfe852015-06-05 13:35:13 -0700165
alshabib8e4fd2f2016-01-12 15:55:53 -0800166 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
167
Gamze Abaka641fc072018-09-04 09:16:27 +0000168 subsService = sadisService.getSubscriberInfoService();
169 bpService = sadisService.getBandwidthProfileService();
170
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100171 // look for all provisioned devices in Sadis and create EAPOL flows for the
172 // UNI ports
173 Iterable<Device> devices = deviceService.getDevices();
174 for (Device d : devices) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700175 checkAndCreateDeviceFlows(d);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100176 }
alshabib4ceaed32016-03-03 18:00:58 -0800177
alshabibba357492016-01-27 13:49:46 -0800178 deviceService.addListener(deviceListener);
alshabibf0e7e702015-05-30 18:22:36 -0700179 log.info("Started with Application ID {}", appId.id());
180 }
181
182 @Deactivate
183 public void deactivate() {
alshabibe0559672016-02-21 14:49:51 -0800184 componentConfigService.unregisterProperties(getClass(), false);
alshabib62e9ce72016-02-11 17:31:36 -0800185 deviceService.removeListener(deviceListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700186 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700187 log.info("Stopped");
188 }
189
alshabibe0559672016-02-21 14:49:51 -0800190 @Modified
191 public void modified(ComponentContext context) {
192 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
193
194 try {
Gamze Abakaad329652018-12-20 10:12:21 +0000195 String bpId = get(properties, "defaultBpId");
196 defaultBpId = bpId;
197
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000198 String mcastSN = get(properties, "multicastServiceName");
199 multicastServiceName = mcastSN;
200
201 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, multicastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000202
alshabibe0559672016-02-21 14:49:51 -0800203 } catch (Exception e) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000204 log.error("Error while modifying the properties", e);
alshabibe0559672016-02-21 14:49:51 -0800205 }
206 }
207
alshabib32232c82016-02-25 17:57:24 -0500208 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000209 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700210 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000211 DeviceId deviceId = connectPoint.deviceId();
212 PortNumber subscriberPortNo = connectPoint.port();
213
214 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000215 "Invalid connect point:" + connectPoint);
Hardik Windlass395ff372019-06-13 05:16:00 +0000216
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100217 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000218 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100219 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000220 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100221 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100222 }
Jonathan Harte533a422015-10-20 17:31:24 -0700223
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100224 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000225 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100226 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000227 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100228 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700229 }
230
Gamze Abaka838d8142019-02-21 07:06:55 +0000231 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000232 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000233 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000234 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
235 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
236 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000237 filterFuture.thenAcceptAsync(filterStatus -> {
238 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000239 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Gamze Abaka33feef52019-02-27 08:16:47 +0000240 }
241 });
Amit Ghosh31939522018-08-16 13:28:21 +0100242 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800243 }
244
245 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000246 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700247 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000248
Saurav Daseae48de2019-06-19 13:26:15 -0700249 // Get the subscriber connected to this port from the local cache
250 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000251 DeviceId deviceId = connectPoint.deviceId();
252 PortNumber subscriberPortNo = connectPoint.port();
253
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000254 Set<UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint);
255 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000256 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000257 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800258 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800259 }
260
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100261 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000262 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100263 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000264 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100265 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800266 }
267
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000268 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100269
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000270 if (multicastServiceName.equals(uniTag.getServiceName())) {
271 continue;
272 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000273
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000274 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800275
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000276 // re-install eapol with default bandwidth profile
277 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
278 uniTag.getUpstreamBandwidthProfile(),
279 null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100280
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000281 Port port = deviceService.getPort(deviceId, subscriberPortNo);
282 if (port != null && port.isEnabled()) {
283 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
284 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
285 } else {
286 log.debug("Port {} is no longer enabled or it's unavailable. Not "
287 + "reprogramming default eapol flow", connectPoint);
288 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100289 }
Amit Ghosh31939522018-08-16 13:28:21 +0100290 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800291 }
292
Gamze Abakaf59c0912019-04-19 08:24:28 +0000293
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000294 @Override
295 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
296 Optional<VlanId> cTag, Optional<Integer> tpId) {
297
298 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
299 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000300
Amit Ghosh31939522018-08-16 13:28:21 +0100301 // Check if we can find the connect point to which this subscriber is connected
302 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
303 if (subsPort == null) {
304 log.warn("ConnectPoint for {} not found", subscriberId);
305 return false;
306 }
307
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100308 if (!sTag.isPresent() && !cTag.isPresent()) {
309 return provisionSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000310 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100311 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
312 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000313 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100314 return false;
315 }
316
Gamze Abakaf59c0912019-04-19 08:24:28 +0000317 //delete Eapol authentication flow with default bandwidth
318 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000319 //install subscriber flows
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000320 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
321 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
322 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000323 filterFuture.thenAcceptAsync(filterStatus -> {
324 if (filterStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000325 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
326 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000327 }
328 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100329 return true;
330 } else {
331 log.warn("Provisioning failed for subscriber: {}", subscriberId);
332 return false;
333 }
Amit Ghosh31939522018-08-16 13:28:21 +0100334 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100335
alshabibe0559672016-02-21 14:49:51 -0800336 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000337 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
338 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100339 // Check if we can find the connect point to which this subscriber is connected
340 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
341 if (subsPort == null) {
342 log.warn("ConnectPoint for {} not found", subscriberId);
343 return false;
344 }
345
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100346 if (!sTag.isPresent() && !cTag.isPresent()) {
347 return removeSubscriber(subsPort);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000348 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100349 // Get the uplink port
350 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
351 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000352 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100353 return false;
354 }
355
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000356 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
357 if (!tagInfo.isPresent()) {
358 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
359 subsPort, cTag, sTag, tpId);
360 return false;
361 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000362
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000363 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100364 return true;
365 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000366 log.warn("Removing subscriber is not possible - please check the provided information" +
367 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100368 return false;
369 }
Amit Ghosh31939522018-08-16 13:28:21 +0100370 }
371
372 @Override
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000373 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700374 return ImmutableMap.copyOf(programmedSubs);
375 }
376
377 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100378 public List<DeviceId> fetchOlts() {
379 // look through all the devices and find the ones that are OLTs as per Sadis
380 List<DeviceId> olts = new ArrayList<>();
381 Iterable<Device> devices = deviceService.getDevices();
382 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700383 if (getOltInfo(d) != null) {
384 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100385 olts.add(d.id());
386 }
387 }
388 return olts;
alshabibe0559672016-02-21 14:49:51 -0800389 }
390
Amit Ghosh31939522018-08-16 13:28:21 +0100391 /**
392 * Finds the connect point to which a subscriber is connected.
393 *
394 * @param id The id of the subscriber, this is the same ID as in Sadis
395 * @return Subscribers ConnectPoint if found else null
396 */
397 private ConnectPoint findSubscriberConnectPoint(String id) {
398
399 Iterable<Device> devices = deviceService.getDevices();
400 for (Device d : devices) {
401 for (Port p : deviceService.getPorts(d.id())) {
402 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
403 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
404 log.debug("Found on device {} port {}", d.id(), p.number());
405 return new ConnectPoint(d.id(), p.number());
406 }
407 }
408 }
409 return null;
410 }
411
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000412 /**
413 * Gets the context of the bandwidth profile information for the given parameter.
414 *
415 * @param bandwidthProfile the bandwidth profile id
416 * @return the context of the bandwidth profile information
417 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000418 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
419 if (bandwidthProfile == null) {
420 return null;
421 }
422 return bpService.get(bandwidthProfile);
423 }
424
Gamze Abaka838d8142019-02-21 07:06:55 +0000425 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000426 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000427 *
428 * @param deviceId the device identifier
429 * @param uplink uplink port of the OLT
430 * @param subscriberPort uni port
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000431 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000432 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000433 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000434 PortNumber subscriberPort, UniTagInformation uniTag) {
435
436 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800437
438 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
439 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
440
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000441 VlanId deviceVlan = uniTag.getPonSTag();
442 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000443
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000444 MeterId upstreamMeterId = oltMeterService
445 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
446 MeterId downstreamMeterId = oltMeterService
447 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000448
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000449 ForwardingObjective.Builder upFwd =
450 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
451 ForwardingObjective.Builder downFwd =
452 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
453
454 if (uniTag.getIsIgmpRequired()) {
455 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
456 upstreamMeterId, uniTag, false, true);
457 }
458 if (uniTag.getIsDhcpRequired()) {
459 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
460 upstreamMeterId, uniTag, false, true);
461 }
alshabibbf23a1f2016-01-14 17:27:11 -0800462
alshabib4ceaed32016-03-03 18:00:58 -0800463 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
464 @Override
465 public void onSuccess(Objective objective) {
466 upFuture.complete(null);
467 }
alshabibbf23a1f2016-01-14 17:27:11 -0800468
alshabib4ceaed32016-03-03 18:00:58 -0800469 @Override
470 public void onError(Objective objective, ObjectiveError error) {
471 upFuture.complete(error);
472 }
473 }));
474
475 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
476 @Override
477 public void onSuccess(Objective objective) {
478 downFuture.complete(null);
479 }
480
481 @Override
482 public void onError(Objective objective, ObjectiveError error) {
483 downFuture.complete(error);
484 }
485 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800486
487 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000488 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800489 if (upStatus == null && downStatus == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000490 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
491 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
492 uniTag.getTechnologyProfileId(), subscriberPort);
493 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800494 } else if (downStatus != null) {
495 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000496 "on port {} failed downstream uninstallation: {}",
497 subscriberVlan, deviceId, subscriberPort, downStatus);
498 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800499 } else if (upStatus != null) {
500 log.error("Subscriber with vlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000501 "on port {} failed upstream uninstallation: {}",
502 subscriberVlan, deviceId, subscriberPort, upStatus);
503 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800504 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000505 Port port = deviceService.getPort(deviceId, subscriberPort);
506 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
507 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800508 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700509 }
510
Gamze Abaka838d8142019-02-21 07:06:55 +0000511 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000512 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000513 *
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000514 * @param connectPoint the connection point of the subscriber
515 * @param uplinkPort uplink port of the OLT (the nni port)
516 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000517 */
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000518 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
519 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000520
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000521 log.info("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000522
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000523 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
524 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
525 return;
526 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000527
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000528 DeviceId deviceId = connectPoint.deviceId();
529 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000530
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000531 for (UniTagInformation uniTag : sub.uniTagList()) {
532 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
533 }
534 }
alshabib3ea82642016-01-12 18:06:53 -0800535
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000536 /**
537 * Finds the uni tag information and provisions the found information.
538 * If the uni tag information is not found, returns
539 *
540 * @param deviceId the access device id
541 * @param uplinkPort the nni port
542 * @param subscriberPort the uni port
543 * @param innerVlan the pon c tag
544 * @param outerVlan the pon s tag
545 * @param tpId the technology profile id
546 */
547 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
548 PortNumber subscriberPort,
549 VlanId innerVlan,
550 VlanId outerVlan,
551 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700552
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000553 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
554 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
555 if (!gotTagInformation.isPresent()) {
556 return;
557 }
558 UniTagInformation tagInformation = gotTagInformation.get();
559 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
560 }
alshabib3ea82642016-01-12 18:06:53 -0800561
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000562 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
563 programmedSubs.compute(connectPoint, (k, v) -> {
564 if (add) {
565 if (v == null) {
566 v = Sets.newHashSet();
Gamze Abaka33feef52019-02-27 08:16:47 +0000567 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000568 v.add(tagInformation);
569 } else {
570 if (v != null) {
571 v.remove(tagInformation);
572 }
alshabib3ea82642016-01-12 18:06:53 -0800573 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000574 return v;
575 });
Jonathan Harte533a422015-10-20 17:31:24 -0700576 }
577
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000578 /**
579 * Installs a uni tag information flow.
580 *
581 * @param deviceId the access device id
582 * @param uplinkPort the nni port
583 * @param subscriberPort the uni port
584 * @param tagInfo the uni tag information
585 */
586 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
587 UniTagInformation tagInfo) {
588
589 log.info("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
590
591 Port port = deviceService.getPort(deviceId, subscriberPort);
592
593 if (multicastServiceName.equals(tagInfo.getServiceName())) {
594 // IGMP flows are taken care of along with VOD service
595 // Please note that for each service, Subscriber Registered event will be sent
596 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
597 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
598 tagInfo.getTechnologyProfileId()));
599 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000600 }
601
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100602 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
603
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000604 BandwidthProfileInformation upstreamBpInfo =
605 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
606 BandwidthProfileInformation downstreamBpInfo =
607 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000608
609 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
610 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000611 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
612 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000613
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000614 MeterId upstreamMeterId = oltMeterService.createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
615
616 MeterId downstreamMeterId = oltMeterService.createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000617
618 upstreamMeterFuture.thenAcceptAsync(result -> {
619 if (result == null) {
620 log.info("Upstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000621 "Sending subscriber flows.", upstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000622
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000623 ForwardingObjective.Builder upFwd =
624 oltFlowService.createUpBuilder(uplinkPort, subscriberPort, upstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000625
626 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
627 @Override
628 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000629 log.info("Upstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000630 upFuture.complete(null);
631 }
632
633 @Override
634 public void onError(Objective objective, ObjectiveError error) {
635 upFuture.complete(error);
636 }
637 }));
638
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000639 } else if (upstreamBpInfo == null) {
640 log.warn("No meter installed since no Upstream BW Profile definition found for " +
641 "ctag {} stag {} tpId {} and Device/port: {}:{}",
642 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
643 tagInfo.getTechnologyProfileId(),
644 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000645 } else {
646 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000647 "Result {} and MeterId {}", result, upstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000648 }
649 });
650
651 downsteamMeterFuture.thenAcceptAsync(result -> {
652 if (result == null) {
653 log.info("Downstream Meter {} is sent to the device {}. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000654 "Sending subscriber flows.", downstreamMeterId, deviceId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000655
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000656 ForwardingObjective.Builder downFwd =
657 oltFlowService.createDownBuilder(uplinkPort, subscriberPort, downstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000658
659 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
660 @Override
661 public void onSuccess(Objective objective) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000662 log.info("Downstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000663 downFuture.complete(null);
664 }
665
666 @Override
667 public void onError(Objective objective, ObjectiveError error) {
668 downFuture.complete(error);
669 }
670 }));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000671
672 } else if (downstreamBpInfo == null) {
673 log.warn("No meter installed since no Downstream BW Profile definition found for " +
674 "ctag {} stag {} tpId {} and Device/port: {}:{}",
675 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
676 tagInfo.getTechnologyProfileId(),
677 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000678 } else {
679 log.warn("Meter installation error while sending upstream flows. " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000680 "Result {} and MeterId {}", result, downstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000681 }
682 });
683
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100684 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000685 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100686 if (downStatus != null) {
687 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000688 "on port {} failed downstream installation: {}",
689 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, downStatus);
690 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100691 } else if (upStatus != null) {
692 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000693 "on port {} failed upstream installation: {}",
694 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, upStatus);
695 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000696 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000697 log.info("Upstream and downstream data plane flows are installed successfully.");
698 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPort,
699 tagInfo.getUpstreamBandwidthProfile(),
700 null, tagInfo.getPonCTag(), true);
701 if (tagInfo.getIsDhcpRequired()) {
702 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
703 upstreamMeterId, tagInfo, true, true);
704 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000705
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000706 if (tagInfo.getIsIgmpRequired()) {
707 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort, upstreamMeterId, tagInfo,
708 true, true);
709 }
710 updateProgrammedSubscriber(cp, tagInfo, true);
711 post(new AccessDeviceEvent(type, deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
712 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100713 }
714 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100715 }
716
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000717 /**
718 * Checks the subscriber uni tag list and find the uni tag information.
719 * using the pon c tag, pon s tag and the technology profile id
720 * May return Optional<null>
721 *
722 * @param cp the connection point of the subscriber
723 * @param innerVlan pon c tag
724 * @param outerVlan pon s tag
725 * @param tpId the technology profile id
726 * @return the found uni tag information
727 */
728 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
729 int tpId) {
730 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
731 outerVlan, tpId);
732 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000733 if (subInfo == null) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000734 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
735 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000736 }
737
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000738 List<UniTagInformation> uniTagList = subInfo.uniTagList();
739 if (uniTagList == null) {
740 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
741 return Optional.empty();
742 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100743
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000744 UniTagInformation service = null;
745 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
746 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
747 && tpId == tagInfo.getTechnologyProfileId()) {
748 service = tagInfo;
749 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700750 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000751 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000752
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000753 if (service == null) {
754 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
755 innerVlan, outerVlan, tpId);
756 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000757 }
758
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000759 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100760 }
761
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100762 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700763 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
764 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100765 *
766 * @param dev Device to look for
767 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700768 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100769 // we create only for the ones we are master of
770 if (!mastershipService.isLocalMaster(dev.id())) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000771 return;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100772 }
773 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000774 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000775 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100776
777 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700778 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100779 for (Port p : deviceService.getPorts(dev.id())) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000780 if (PortNumber.LOCAL.equals(p.number())) {
781 continue;
782 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100783 if (isUniPort(dev, p)) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000784 log.info("Creating Eapol for the uni {}", p);
785 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
786 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Jonathan Hart403372d2018-08-22 11:44:13 -0700787 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000788 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100789 }
790 }
791 }
792 }
793
Jonathan Hart403372d2018-08-22 11:44:13 -0700794
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100795 /**
796 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000797 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100798 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
799 * this logic needs to be changed
800 *
801 * @param dev Device to look for
802 * @return The uplink Port of the OLT
803 */
804 private Port getUplinkPort(Device dev) {
805 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000806 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -0700807 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700808 if (deviceInfo == null) {
809 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000810 + " info", dev.id());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700811 return null;
812 }
813 // Return the port that has been configured as the uplink port of this OLT in Sadis
Gamze Abakaad329652018-12-20 10:12:21 +0000814 for (Port p : deviceService.getPorts(dev.id())) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700815 if (p.number().toLong() == deviceInfo.uplinkPort()) {
Saurav Daseae48de2019-06-19 13:26:15 -0700816 log.trace("getUplinkPort: Found port {}", p);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700817 return p;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100818 }
819 }
820
Saurav Daseae48de2019-06-19 13:26:15 -0700821 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100822 return null;
823 }
824
825 /**
826 * Return the subscriber on a port.
827 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800828 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100829 * @return subscriber if found else null
830 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800831 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
832 Port port = deviceService.getPort(cp);
833 checkNotNull(port, "Invalid connect point");
834 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100835 return subsService.get(portName);
836 }
837
Gamze Abakaad329652018-12-20 10:12:21 +0000838 /**
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000839 * Checks whether the given port of the device is a uni port or not.
840 *
841 * @param d the access device
842 * @param p the port of the device
843 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000844 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100845 private boolean isUniPort(Device d, Port p) {
846 Port ulPort = getUplinkPort(d);
847 if (ulPort != null) {
848 return (ulPort.number().toLong() != p.number().toLong());
849 }
850 return false;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700851 }
852
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000853 /**
854 * Gets the given device details from SADIS.
855 * If the device is not found, returns null
856 *
857 * @param dev the access device
858 * @return the olt information
859 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700860 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
861 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +0000862 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -0700863 }
864
alshabibf0e7e702015-05-30 18:22:36 -0700865 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800866 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
867
alshabibf0e7e702015-05-30 18:22:36 -0700868 @Override
869 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700870 eventExecutor.execute(() -> {
871 DeviceId devId = event.subject().id();
872 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +0000873 Port port = event.port();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000874 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -0700875
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000876 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
877 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
878 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700879 return;
880 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700881
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000882 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
883
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700884 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800885 // it's possible that we got an event for a previously
886 // programmed OLT that is no longer available in SADIS
887 // we let such events go through
888 if (!programmedDevices.contains(devId)) {
889 log.warn("No device info found for {}, this is either "
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000890 + "not an OLT or not known to sadis", dev);
Saurav Dasa9d5f442019-03-06 19:32:48 -0800891 return;
892 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700893 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700894
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700895 switch (event.type()) {
896 //TODO: Port handling and bookkeeping should be improved once
897 // olt firmware handles correct behaviour.
898 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000899 if (isUniPort(dev, port)) {
900 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000901
902 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
903 log.info("eapol will be sent for port added {}", port);
904 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
905 null,
906 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700907 }
908 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000909 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
910 if (deviceInfo != null) {
911 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
912 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700913 }
914 break;
915 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000916 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +0000917 removeSubscriber(new ConnectPoint(devId, port.number()));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000918 log.info("eapol will be send for port removed", port);
919 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
920 null,
921 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Andy Bavier160e8682019-05-07 18:32:22 -0700922
Gamze Abaka838d8142019-02-21 07:06:55 +0000923 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700924 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700925 break;
926 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000927 if (!isUniPort(dev, port)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700928 break;
929 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000930 Set<UniTagInformation> uniTagInformationSet = programmedSubs
Gamze Abakada282b42019-03-11 13:16:48 +0000931 .get(new ConnectPoint(devId, port.number()));
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000932 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
933 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
934 log.info("eapol will be processed for port updated {}", port);
935 oltFlowService.processEapolFilteringObjectives(devId, port.number(), DEFAULT_BP_ID,
936 null,
937 VlanId.vlanId(EAPOL_DEFAULT_VLAN),
938 port.isEnabled());
939 }
940 } else {
941 log.info("eapol will be processed for port updated {}", port);
942 uniTagInformationSet.forEach(uniTag ->
943 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
944 uniTag.getUpstreamBandwidthProfile(), null,
945 uniTag.getPonCTag(), port.isEnabled()));
946 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000947 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000948 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700949 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +0000950 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700951 }
alshabibbb83aa22016-02-10 15:08:23 -0800952 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700953 case DEVICE_ADDED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000954 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700955 break;
956 case DEVICE_REMOVED:
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000957 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700958 break;
959 case DEVICE_AVAILABILITY_CHANGED:
960 if (deviceService.isAvailable(devId)) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000961 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700962 } else {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000963 handleDeviceDisconnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700964 }
965 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700966 default:
967 return;
968 }
969 });
alshabibf0e7e702015-05-30 18:22:36 -0700970 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000971
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000972 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
973 deviceService.getPorts(device.id()).stream()
974 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
975 .filter(p -> isUniPort(device, p))
976 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
977 }
978
979 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
980 programmedDevices.remove(device.id());
981 removeAllSubscribers(device.id());
982 post(new AccessDeviceEvent(
983 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
984 null, null, null));
985 if (sendUniEvent) {
986 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +0000987 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000988 }
989
990 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
991 post(new AccessDeviceEvent(
992 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
993 null, null, null));
994 programmedDevices.add(dev.id());
995 checkAndCreateDeviceFlows(dev);
996 if (sendUniEvent) {
997 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
998 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000999 }
Gamze Abakada282b42019-03-11 13:16:48 +00001000
1001 private void removeAllSubscribers(DeviceId deviceId) {
1002 List<ConnectPoint> connectPoints = programmedSubs.keySet().stream()
1003 .filter(ks -> Objects.equals(ks.deviceId(), deviceId))
1004 .collect(Collectors.toList());
1005
1006 connectPoints.forEach(cp -> programmedSubs.remove(cp));
1007 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001008
Gamze Abaka641fc072018-09-04 09:16:27 +00001009 }
Hardik Windlass395ff372019-06-13 05:16:00 +00001010}