blob: d4ed2ff0ae6119ba47775709e281d932c8297653 [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 Das82b8e6d2018-10-04 15:25:12 -070018import static com.google.common.base.Preconditions.checkNotNull;
19import static com.google.common.base.Strings.isNullOrEmpty;
20import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
21import static org.onlab.util.Tools.get;
22import static org.onlab.util.Tools.groupedThreads;
23import static org.slf4j.LoggerFactory.getLogger;
24
Gamze Abaka33feef52019-02-27 08:16:47 +000025import java.util.ArrayList;
Saurav Das82b8e6d2018-10-04 15:25:12 -070026import java.util.Dictionary;
27import java.util.List;
28import java.util.Map;
Gamze Abakada282b42019-03-11 13:16:48 +000029import java.util.Objects;
Saurav Das82b8e6d2018-10-04 15:25:12 -070030import java.util.Optional;
31import java.util.Properties;
Gamze Abaka33feef52019-02-27 08:16:47 +000032import java.util.Set;
Saurav Das82b8e6d2018-10-04 15:25:12 -070033import java.util.concurrent.CompletableFuture;
34import java.util.concurrent.ExecutorService;
35import java.util.concurrent.Executors;
Gamze Abakada282b42019-03-11 13:16:48 +000036import java.util.stream.Collectors;
Saurav Das82b8e6d2018-10-04 15:25:12 -070037
alshabibf0e7e702015-05-30 18:22:36 -070038import org.apache.felix.scr.annotations.Activate;
39import org.apache.felix.scr.annotations.Component;
40import org.apache.felix.scr.annotations.Deactivate;
alshabibe0559672016-02-21 14:49:51 -080041import org.apache.felix.scr.annotations.Modified;
42import org.apache.felix.scr.annotations.Property;
alshabibf0e7e702015-05-30 18:22:36 -070043import org.apache.felix.scr.annotations.Reference;
44import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070045import org.apache.felix.scr.annotations.Service;
alshabibf0e7e702015-05-30 18:22:36 -070046import org.onlab.packet.VlanId;
alshabibe0559672016-02-21 14:49:51 -080047import org.onosproject.cfg.ComponentConfigService;
alshabibf0e7e702015-05-30 18:22:36 -070048import org.onosproject.core.ApplicationId;
49import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080050import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080051import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010052import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070053import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010054import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080056import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070057import org.onosproject.net.PortNumber;
58import org.onosproject.net.device.DeviceEvent;
59import org.onosproject.net.device.DeviceListener;
60import org.onosproject.net.device.DeviceService;
alshabibf0e7e702015-05-30 18:22:36 -070061import org.onosproject.net.flowobjective.FlowObjectiveService;
62import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080063import org.onosproject.net.flowobjective.Objective;
64import org.onosproject.net.flowobjective.ObjectiveContext;
65import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das4c1a6a92019-06-19 13:26:15 -070066import org.onosproject.net.meter.MeterId;
alshabib36a4d732016-06-01 16:03:59 -070067import org.opencord.olt.AccessDeviceEvent;
68import org.opencord.olt.AccessDeviceListener;
69import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +010070import org.opencord.olt.AccessSubscriberId;
Gamze Abaka1b7816e2019-11-25 06:38:41 +000071import org.opencord.olt.internalapi.AccessDeviceFlowService;
72import org.opencord.olt.internalapi.AccessDeviceMeterService;
Gamze Abaka641fc072018-09-04 09:16:27 +000073import org.opencord.sadis.BandwidthProfileInformation;
74import org.opencord.sadis.BaseInformationService;
75import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010076import org.opencord.sadis.SubscriberAndDeviceInformation;
Gamze Abaka1b7816e2019-11-25 06:38:41 +000077import org.opencord.sadis.UniTagInformation;
alshabibe0559672016-02-21 14:49:51 -080078import org.osgi.service.component.ComponentContext;
alshabibf0e7e702015-05-30 18:22:36 -070079import org.slf4j.Logger;
80
Saurav Das82b8e6d2018-10-04 15:25:12 -070081import com.google.common.collect.ImmutableMap;
82import com.google.common.collect.Maps;
Saurav Dasa9d5f442019-03-06 19:32:48 -080083import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -070084
85/**
Jonathan Harte533a422015-10-20 17:31:24 -070086 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -070087 */
Jonathan Harte533a422015-10-20 17:31:24 -070088@Service
alshabibf0e7e702015-05-30 18:22:36 -070089@Component(immediate = true)
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 Abakaad329652018-12-20 10:12:21 +000096 private static final String DEFAULT_BP_ID = "Default";
Gamze Abaka1b7816e2019-11-25 06:38:41 +000097 private static final String DEFAULT_MCAST_SERVICE_NAME = "MC";
Gamze Abaka838d8142019-02-21 07:06:55 +000098 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
alshabibe0559672016-02-21 14:49:51 -080099
alshabibf0e7e702015-05-30 18:22:36 -0700100 private final Logger log = getLogger(getClass());
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected FlowObjectiveService flowObjectiveService;
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib09753b52016-03-04 14:55:19 -0800106 protected MastershipService mastershipService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibf0e7e702015-05-30 18:22:36 -0700109 protected DeviceService deviceService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected CoreService coreService;
113
Jonathan Harte533a422015-10-20 17:31:24 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe0559672016-02-21 14:49:51 -0800115 protected ComponentConfigService componentConfigService;
116
alshabib4ceaed32016-03-03 18:00:58 -0800117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000118 protected SadisService sadisService;
119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000121 protected AccessDeviceFlowService oltFlowService;
alshabibe0559672016-02-21 14:49:51 -0800122
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000124 protected AccessDeviceMeterService oltMeterService;
Gamze Abakaad329652018-12-20 10:12:21 +0000125
126 @Property(name = "defaultBpId", value = DEFAULT_BP_ID,
127 label = "Default bandwidth profile id that is used for authentication trap flows")
128 protected String defaultBpId = DEFAULT_BP_ID;
129
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000130 @Property(name = "multicastServiceName", value = DEFAULT_MCAST_SERVICE_NAME,
131 label = "The multicast service name")
132 protected String mcastServiceName = 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,
140 groupedThreads("onos/olt-service",
141 "olt-installer-%d"));
alshabibf0e7e702015-05-30 18:22:36 -0700142
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700143 protected ExecutorService eventExecutor;
144
Gamze Abaka1b7816e2019-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) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000149 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt",
150 "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800151 modified(context);
Gamze Abaka1b7816e2019-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",
Gamze Abaka33feef52019-02-27 08:16:47 +0000159 "purgeOnDisconnection", "true");
Gamze Abakada282b42019-03-11 13:16:48 +0000160 componentConfigService
161 .preSetProperty("org.onosproject.net.meter.impl.MeterManager",
162 "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 Abaka1b7816e2019-11-25 06:38:41 +0000195 defaultBpId = get(properties, "defaultBpId");
Amit Ghosh95e2f652017-08-23 12:49:46 +0100196
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000197 String serviceName = get(properties, "multicastServiceName");
198 mcastServiceName = isNullOrEmpty(serviceName) ? DEFAULT_MCAST_SERVICE_NAME : serviceName;
Matt Jeanneret3f579262018-06-14 17:16:23 -0400199
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000200 log.debug("OLT properties: DefaultBpId: {}, MulticastServiceName: {}", defaultBpId, mcastServiceName);
Gamze Abaka33feef52019-02-27 08:16:47 +0000201
alshabibe0559672016-02-21 14:49:51 -0800202 } catch (Exception e) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000203 log.error("Error while modifying the properties", e);
alshabibe0559672016-02-21 14:49:51 -0800204 }
205 }
206
alshabib32232c82016-02-25 17:57:24 -0500207 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000208 public boolean provisionSubscriber(ConnectPoint connectPoint) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700209 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000210 DeviceId deviceId = connectPoint.deviceId();
211 PortNumber subscriberPortNo = connectPoint.port();
212
213 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000214 "Invalid connect point:" + connectPoint);
215
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100216 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000217 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100218 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000219 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100220 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100221 }
Jonathan Harte533a422015-10-20 17:31:24 -0700222
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100223 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000224 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100225 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000226 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100227 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700228 }
229
Gamze Abaka838d8142019-02-21 07:06:55 +0000230 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000231 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abaka33feef52019-02-27 08:16:47 +0000232 //install subscriber flows
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000233 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
234 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
235 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000236 filterFuture.thenAcceptAsync(filterStatus -> {
237 if (filterStatus == null) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000238 provisionUniTagList(connectPoint, uplinkPort.number(), sub);
Gamze Abaka33feef52019-02-27 08:16:47 +0000239 }
240 });
Amit Ghosh31939522018-08-16 13:28:21 +0100241 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800242 }
243
244 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000245 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700246 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000247
Saurav Das4c1a6a92019-06-19 13:26:15 -0700248 // Get the subscriber connected to this port from the local cache
249 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000250 DeviceId deviceId = connectPoint.deviceId();
251 PortNumber subscriberPortNo = connectPoint.port();
252
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000253 Set<UniTagInformation> uniTagInformationSet = programmedSubs.get(connectPoint);
254 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000255 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
256 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800257 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800258 }
259
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100260 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000261 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100262 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000263 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100264 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800265 }
266
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000267 for (UniTagInformation uniTag : uniTagInformationSet) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100268
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000269 if (mcastServiceName.equals(uniTag.getServiceName())) {
270 continue;
271 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000272
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000273 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, uniTag);
alshabibbf23a1f2016-01-14 17:27:11 -0800274
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000275 // re-install eapol with default bandwidth profile
276 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo,
277 uniTag.getUpstreamBandwidthProfile(), null, uniTag.getPonCTag(), false);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100278
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000279 Port port = deviceService.getPort(deviceId, subscriberPortNo);
280 if (port != null && port.isEnabled()) {
281 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
282 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
283 } else {
284 log.debug("Port {} is no longer enabled or it's unavailable. Not "
285 + "reprogramming default eapol flow", connectPoint);
286 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100287 }
Amit Ghosh31939522018-08-16 13:28:21 +0100288 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800289 }
290
Gamze Abakaf59c0912019-04-19 08:24:28 +0000291
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000292
293 @Override
294 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
295 Optional<VlanId> cTag, Optional<Integer> tpId) {
296
297 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}, tpId {}" +
298 "", subscriberId, sTag, cTag, tpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000299
Amit Ghosh31939522018-08-16 13:28:21 +0100300 // Check if we can find the connect point to which this subscriber is connected
301 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
302 if (subsPort == null) {
303 log.warn("ConnectPoint for {} not found", subscriberId);
304 return false;
305 }
306
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100307 if (!sTag.isPresent() && !cTag.isPresent()) {
308 return provisionSubscriber(subsPort);
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000309 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100310 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
311 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000312 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100313 return false;
314 }
315
Gamze Abakaf59c0912019-04-19 08:24:28 +0000316 //delete Eapol authentication flow with default bandwidth
317 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakaf59c0912019-04-19 08:24:28 +0000318 //install subscriber flows
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000319 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
320 oltFlowService.processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId,
321 filterFuture, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000322 filterFuture.thenAcceptAsync(filterStatus -> {
323 if (filterStatus == null) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000324 provisionUniTagInformation(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
325 cTag.get(), sTag.get(), tpId.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000326 }
327 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100328 return true;
329 } else {
330 log.warn("Provisioning failed for subscriber: {}", subscriberId);
331 return false;
332 }
Amit Ghosh31939522018-08-16 13:28:21 +0100333 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100334
alshabibe0559672016-02-21 14:49:51 -0800335 @Override
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000336 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag,
337 Optional<VlanId> cTag, Optional<Integer> tpId) {
Amit Ghosh31939522018-08-16 13:28:21 +0100338 // Check if we can find the connect point to which this subscriber is connected
339 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
340 if (subsPort == null) {
341 log.warn("ConnectPoint for {} not found", subscriberId);
342 return false;
343 }
344
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100345 if (!sTag.isPresent() && !cTag.isPresent()) {
346 return removeSubscriber(subsPort);
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000347 } else if (sTag.isPresent() && cTag.isPresent() && tpId.isPresent()) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100348 // Get the uplink port
349 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
350 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000351 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100352 return false;
353 }
354
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000355 Optional<UniTagInformation> tagInfo = getUniTagInformation(subsPort, cTag.get(), sTag.get(), tpId.get());
356 if (!tagInfo.isPresent()) {
357 log.warn("UniTagInformation does not exist for Device/Port {}, cTag {}, sTag {}, tpId {}",
358 subsPort, cTag, sTag, tpId);
359 return false;
360 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000361
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000362 unprovisionVlans(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), tagInfo.get());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100363 return true;
364 } else {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000365 log.warn("Removing subscriber is not possible - please check the provided information" +
366 "for the subscriber: {}", subscriberId);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100367 return false;
368 }
Amit Ghosh31939522018-08-16 13:28:21 +0100369 }
370
371 @Override
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000372 public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700373 return ImmutableMap.copyOf(programmedSubs);
374 }
375
376 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100377 public List<DeviceId> fetchOlts() {
378 // look through all the devices and find the ones that are OLTs as per Sadis
379 List<DeviceId> olts = new ArrayList<>();
380 Iterable<Device> devices = deviceService.getDevices();
381 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700382 if (getOltInfo(d) != null) {
383 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100384 olts.add(d.id());
385 }
386 }
387 return olts;
alshabibe0559672016-02-21 14:49:51 -0800388 }
389
Amit Ghosh31939522018-08-16 13:28:21 +0100390 /**
391 * Finds the connect point to which a subscriber is connected.
392 *
393 * @param id The id of the subscriber, this is the same ID as in Sadis
394 * @return Subscribers ConnectPoint if found else null
395 */
396 private ConnectPoint findSubscriberConnectPoint(String id) {
397
398 Iterable<Device> devices = deviceService.getDevices();
399 for (Device d : devices) {
400 for (Port p : deviceService.getPorts(d.id())) {
401 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
402 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
403 log.debug("Found on device {} port {}", d.id(), p.number());
404 return new ConnectPoint(d.id(), p.number());
405 }
406 }
407 }
408 return null;
409 }
410
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000411 /**
412 * Gets the context of the bandwidth profile information for the given parameter.
413 *
414 * @param bandwidthProfile the bandwidth profile id
415 * @return the context of the bandwidth profile information
416 */
Gamze Abaka641fc072018-09-04 09:16:27 +0000417 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
418 if (bandwidthProfile == null) {
419 return null;
420 }
421 return bpService.get(bandwidthProfile);
422 }
423
Gamze Abaka838d8142019-02-21 07:06:55 +0000424 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000425 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000426 *
427 * @param deviceId the device identifier
428 * @param uplink uplink port of the OLT
429 * @param subscriberPort uni port
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000430 * @param uniTag uni tag information
Gamze Abaka838d8142019-02-21 07:06:55 +0000431 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000432 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000433 PortNumber subscriberPort, UniTagInformation uniTag) {
434
435 log.info("Unprovisioning vlans for {} at {}/{}", uniTag, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800436
437 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
438 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
439
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000440 VlanId deviceVlan = uniTag.getPonSTag();
441 VlanId subscriberVlan = uniTag.getPonCTag();
Gamze Abaka641fc072018-09-04 09:16:27 +0000442
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000443 MeterId upstreamMeterId = oltMeterService
444 .getMeterIdFromBpMapping(deviceId, uniTag.getUpstreamBandwidthProfile());
445 MeterId downstreamMeterId = oltMeterService
446 .getMeterIdFromBpMapping(deviceId, uniTag.getDownstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000447
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000448 ForwardingObjective.Builder upFwd =
449 oltFlowService.createUpBuilder(uplink, subscriberPort, upstreamMeterId, uniTag);
450 ForwardingObjective.Builder downFwd =
451 oltFlowService.createDownBuilder(uplink, subscriberPort, downstreamMeterId, uniTag);
452
453 if (uniTag.getIsIgmpRequired()) {
454 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort,
455 upstreamMeterId, uniTag, false, true);
456 }
457 if (uniTag.getIsDhcpRequired()) {
458 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
459 upstreamMeterId, uniTag, false, true);
460 }
alshabibbf23a1f2016-01-14 17:27:11 -0800461
alshabib4ceaed32016-03-03 18:00:58 -0800462 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
463 @Override
464 public void onSuccess(Objective objective) {
465 upFuture.complete(null);
466 }
alshabibbf23a1f2016-01-14 17:27:11 -0800467
alshabib4ceaed32016-03-03 18:00:58 -0800468 @Override
469 public void onError(Objective objective, ObjectiveError error) {
470 upFuture.complete(error);
471 }
472 }));
473
474 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
475 @Override
476 public void onSuccess(Objective objective) {
477 downFuture.complete(null);
478 }
479
480 @Override
481 public void onError(Objective objective, ObjectiveError error) {
482 downFuture.complete(error);
483 }
484 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800485
486 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000487 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTERED;
alshabibbf23a1f2016-01-14 17:27:11 -0800488 if (upStatus == null && downStatus == null) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000489 log.debug("Uni tag information is unregistered successfully for cTag {}, sTag {}, tpId {}, and" +
490 "Device/Port{}", uniTag.getPonCTag(), uniTag.getPonSTag(),
491 uniTag.getTechnologyProfileId(), subscriberPort);
492 updateProgrammedSubscriber(new ConnectPoint(deviceId, subscriberPort), uniTag, false);
alshabibbf23a1f2016-01-14 17:27:11 -0800493 } else if (downStatus != null) {
494 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000495 "on port {} failed downstream uninstallation: {}",
496 subscriberVlan, deviceId, subscriberPort, downStatus);
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000497 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800498 } else if (upStatus != null) {
499 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000500 "on port {} failed upstream uninstallation: {}",
501 subscriberVlan, deviceId, subscriberPort, upStatus);
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000502 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_UNREGISTRATION_FAILED;
alshabibbf23a1f2016-01-14 17:27:11 -0800503 }
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000504 Port port = deviceService.getPort(deviceId, subscriberPort);
505 post(new AccessDeviceEvent(type, deviceId, port, deviceVlan, subscriberVlan,
506 uniTag.getTechnologyProfileId()));
alshabibbf23a1f2016-01-14 17:27:11 -0800507 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700508 }
509
Gamze Abaka838d8142019-02-21 07:06:55 +0000510 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000511 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000512 *
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000513 * @param connectPoint the connection point of the subscriber
514 * @param uplinkPort uplink port of the OLT (the nni port)
515 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000516 */
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000517 private void provisionUniTagList(ConnectPoint connectPoint, PortNumber uplinkPort,
518 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000519
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000520 log.info("Provisioning vlans for subscriber {} on dev/port: {}", sub, connectPoint);
Gamze Abaka641fc072018-09-04 09:16:27 +0000521
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000522 if (sub.uniTagList() == null || sub.uniTagList().isEmpty()) {
523 log.warn("Unitaglist doesn't exist for the subscriber {}", sub.id());
524 return;
525 }
Gamze Abaka641fc072018-09-04 09:16:27 +0000526
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000527 DeviceId deviceId = connectPoint.deviceId();
528 PortNumber subscriberPort = connectPoint.port();
Gamze Abaka641fc072018-09-04 09:16:27 +0000529
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000530 for (UniTagInformation uniTag : sub.uniTagList()) {
531 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, uniTag);
532 }
533 }
alshabib3ea82642016-01-12 18:06:53 -0800534
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000535 /**
536 * Finds the uni tag information and provisions the found information.
537 * If the uni tag information is not found, returns
538 *
539 * @param deviceId the access device id
540 * @param uplinkPort the nni port
541 * @param subscriberPort the uni port
542 * @param innerVlan the pon c tag
543 * @param outerVlan the pon s tag
544 * @param tpId the technology profile id
545 */
546 private void provisionUniTagInformation(DeviceId deviceId, PortNumber uplinkPort,
547 PortNumber subscriberPort,
548 VlanId innerVlan,
549 VlanId outerVlan,
550 Integer tpId) {
Jonathan Harte533a422015-10-20 17:31:24 -0700551
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000552 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
553 Optional<UniTagInformation> gotTagInformation = getUniTagInformation(cp, innerVlan, outerVlan, tpId);
554 if (!gotTagInformation.isPresent()) {
555 return;
556 }
557 UniTagInformation tagInformation = gotTagInformation.get();
558 handleSubscriberFlows(deviceId, uplinkPort, subscriberPort, tagInformation);
559 }
alshabib3ea82642016-01-12 18:06:53 -0800560
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000561 private void updateProgrammedSubscriber(ConnectPoint connectPoint, UniTagInformation tagInformation, Boolean add) {
562 programmedSubs.compute(connectPoint, (k, v) -> {
563 if (add) {
564 if (v == null) {
565 v = Sets.newHashSet();
Gamze Abaka33feef52019-02-27 08:16:47 +0000566 }
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000567 v.add(tagInformation);
568 } else {
569 if (v != null) {
570 v.remove(tagInformation);
571 }
alshabib3ea82642016-01-12 18:06:53 -0800572 }
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000573 return v;
574 });
Jonathan Harte533a422015-10-20 17:31:24 -0700575 }
576
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000577 /**
578 * Installs a uni tag information flow.
579 *
580 * @param deviceId the access device id
581 * @param uplinkPort the nni port
582 * @param subscriberPort the uni port
583 * @param tagInfo the uni tag information
584 */
585 private void handleSubscriberFlows(DeviceId deviceId, PortNumber uplinkPort, PortNumber subscriberPort,
586 UniTagInformation tagInfo) {
587
588 log.info("Provisioning vlan-based flows for the uniTagInformation {}", tagInfo);
589
590 Port port = deviceService.getPort(deviceId, subscriberPort);
591
592 if (mcastServiceName.equals(tagInfo.getServiceName())) {
593 // IGMP flows are taken care of along with VOD service
594 // Please note that for each service, Subscriber Registered event will be sent
595 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED,
596 deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
597 tagInfo.getTechnologyProfileId()));
598 return;
Gamze Abaka641fc072018-09-04 09:16:27 +0000599 }
600
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100601 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
602
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000603 BandwidthProfileInformation upstreamBpInfo =
604 getBandwidthProfileInformation(tagInfo.getUpstreamBandwidthProfile());
605 BandwidthProfileInformation downstreamBpInfo =
606 getBandwidthProfileInformation(tagInfo.getDownstreamBandwidthProfile());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000607
608 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
609 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000610 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture<>();
611 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture<>();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000612
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000613 MeterId upstreamMeterId = oltMeterService.createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
614
615 MeterId downstreamMeterId = oltMeterService.createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000616
617 upstreamMeterFuture.thenAcceptAsync(result -> {
618 if (result == null) {
619 log.info("Upstream Meter {} is sent to the device {}. " +
620 "Sending subscriber flows.", upstreamMeterId, deviceId);
621
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000622 ForwardingObjective.Builder upFwd =
623 oltFlowService.createUpBuilder(uplinkPort, subscriberPort, upstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000624
625 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
626 @Override
627 public void onSuccess(Objective objective) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000628 log.info("Upstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000629 upFuture.complete(null);
630 }
631
632 @Override
633 public void onError(Objective objective, ObjectiveError error) {
634 upFuture.complete(error);
635 }
636 }));
637
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000638 } else if (upstreamBpInfo == null) {
639 log.warn("No meter installed since no Upstream BW Profile definition found for " +
640 "ctag {} stag {} tpId {} and Device/port: {}:{}",
641 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
642 tagInfo.getTechnologyProfileId(),
643 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000644 } else {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000645 log.warn("Meter installation error while sending upstream flows. " +
646 "Result {} and MeterId {}", result, upstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000647 }
648 });
649
650 downsteamMeterFuture.thenAcceptAsync(result -> {
651 if (result == null) {
652 log.info("Downstream Meter {} is sent to the device {}. " +
653 "Sending subscriber flows.", downstreamMeterId, deviceId);
654
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000655 ForwardingObjective.Builder downFwd =
656 oltFlowService.createDownBuilder(uplinkPort, subscriberPort, downstreamMeterId, tagInfo);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000657
658 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
659 @Override
660 public void onSuccess(Objective objective) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000661 log.info("Downstream flow installed successfully");
Gamze Abakaf59c0912019-04-19 08:24:28 +0000662 downFuture.complete(null);
663 }
664
665 @Override
666 public void onError(Objective objective, ObjectiveError error) {
667 downFuture.complete(error);
668 }
669 }));
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000670
671 } else if (downstreamBpInfo == null) {
672 log.warn("No meter installed since no Downstream BW Profile definition found for " +
673 "ctag {} stag {} tpId {} and Device/port: {}:{}",
674 tagInfo.getPonCTag(), tagInfo.getPonSTag(),
675 tagInfo.getTechnologyProfileId(),
676 deviceId, subscriberPort);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000677 } else {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000678 log.warn("Meter installation error while sending upstream flows. " +
679 "Result {} and MeterId {}", result, downstreamMeterId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000680 }
681 });
682
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100683 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000684 AccessDeviceEvent.Type type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTERED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100685 if (downStatus != null) {
686 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000687 "on port {} failed downstream installation: {}",
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000688 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, downStatus);
689 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100690 } else if (upStatus != null) {
691 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000692 "on port {} failed upstream installation: {}",
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000693 tagInfo.getPonCTag(), tagInfo.getPonSTag(), deviceId, cp, upStatus);
694 type = AccessDeviceEvent.Type.SUBSCRIBER_UNI_TAG_REGISTRATION_FAILED;
Gamze Abakaf59c0912019-04-19 08:24:28 +0000695 } else {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000696 log.info("Upstream and downstream data plane flows are installed successfully.");
697 oltFlowService.processEapolFilteringObjectives(deviceId, subscriberPort,
698 tagInfo.getUpstreamBandwidthProfile(), null, tagInfo.getPonCTag(), true);
699 if (tagInfo.getIsDhcpRequired()) {
700 oltFlowService.processDhcpFilteringObjectives(deviceId, subscriberPort,
701 upstreamMeterId, tagInfo, true, true);
702 }
Gamze Abakaf59c0912019-04-19 08:24:28 +0000703
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000704 if (tagInfo.getIsIgmpRequired()) {
705 oltFlowService.processIgmpFilteringObjectives(deviceId, subscriberPort, upstreamMeterId, tagInfo,
706 true, true);
707 }
708 updateProgrammedSubscriber(cp, tagInfo, true);
709 post(new AccessDeviceEvent(type, deviceId, port, tagInfo.getPonSTag(), tagInfo.getPonCTag(),
710 tagInfo.getTechnologyProfileId()));
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100711 }
712 }, oltInstallers);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100713 }
714
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000715 /**
716 * Checks the subscriber uni tag list and find the uni tag information.
717 * using the pon c tag, pon s tag and the technology profile id
718 * May return Optional<null>
719 *
720 * @param cp the connection point of the subscriber
721 * @param innerVlan pon c tag
722 * @param outerVlan pon s tag
723 * @param tpId the technology profile id
724 * @return the found uni tag information
725 */
726 private Optional<UniTagInformation> getUniTagInformation(ConnectPoint cp, VlanId innerVlan, VlanId outerVlan,
727 int tpId) {
728 log.info("Getting uni tag information for cp: {}, innerVlan: {}, outerVlan: {}, tpId: {}", cp, innerVlan,
729 outerVlan, tpId);
730 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000731 if (subInfo == null) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000732 log.warn("Subscriber information doesn't exist for the connect point {}", cp);
733 return Optional.empty();
Gamze Abakaf59c0912019-04-19 08:24:28 +0000734 }
735
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000736 List<UniTagInformation> uniTagList = subInfo.uniTagList();
737 if (uniTagList == null) {
738 log.warn("Uni tag list is not found for the subscriber {}", subInfo.id());
739 return Optional.empty();
740 }
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100741
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000742 UniTagInformation service = null;
743 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
744 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
745 && tpId == tagInfo.getTechnologyProfileId()) {
746 service = tagInfo;
747 break;
Andy Bavier160e8682019-05-07 18:32:22 -0700748 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000749 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000750
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000751 if (service == null) {
752 log.warn("SADIS doesn't include the service with ponCtag {} ponStag {} and tpId {}",
753 innerVlan, outerVlan, tpId);
754 return Optional.empty();
Gamze Abaka33feef52019-02-27 08:16:47 +0000755 }
756
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000757 return Optional.of(service);
Amit Ghosh95e2f652017-08-23 12:49:46 +0100758 }
759
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100760 /**
Jonathan Hart403372d2018-08-22 11:44:13 -0700761 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
762 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100763 *
764 * @param dev Device to look for
765 */
Jonathan Hart403372d2018-08-22 11:44:13 -0700766 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100767 // we create only for the ones we are master of
768 if (!mastershipService.isLocalMaster(dev.id())) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000769 return;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100770 }
771 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000772 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000773 log.info("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100774
775 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700776 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100777 for (Port p : deviceService.getPorts(dev.id())) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000778 if (PortNumber.LOCAL.equals(p.number())) {
779 continue;
780 }
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100781 if (isUniPort(dev, p)) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000782 log.info("Creating Eapol for the uni {}", p);
783 oltFlowService.processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
Gamze Abakada282b42019-03-11 13:16:48 +0000784 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Jonathan Hart403372d2018-08-22 11:44:13 -0700785 } else {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000786 oltFlowService.processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100787 }
788 }
789 }
790 }
791
Jonathan Hart403372d2018-08-22 11:44:13 -0700792
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100793 /**
794 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +0000795 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100796 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
797 * this logic needs to be changed
798 *
799 * @param dev Device to look for
800 * @return The uplink Port of the OLT
801 */
802 private Port getUplinkPort(Device dev) {
803 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +0000804 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Das4c1a6a92019-06-19 13:26:15 -0700805 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700806 if (deviceInfo == null) {
807 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
808 + " info", dev.id());
809 return null;
810 }
811 // Return the port that has been configured as the uplink port of this OLT in Sadis
Gamze Abakaad329652018-12-20 10:12:21 +0000812 for (Port p : deviceService.getPorts(dev.id())) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700813 if (p.number().toLong() == deviceInfo.uplinkPort()) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700814 log.trace("getUplinkPort: Found port {}", p);
Saurav Das82b8e6d2018-10-04 15:25:12 -0700815 return p;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100816 }
817 }
818
Saurav Das4c1a6a92019-06-19 13:26:15 -0700819 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100820 return null;
821 }
822
823 /**
824 * Return the subscriber on a port.
825 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800826 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100827 * @return subscriber if found else null
828 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800829 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
830 Port port = deviceService.getPort(cp);
831 checkNotNull(port, "Invalid connect point");
832 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100833 return subsService.get(portName);
834 }
835
Gamze Abakaad329652018-12-20 10:12:21 +0000836 /**
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000837 * Checks whether the given port of the device is a uni port or not.
838 *
839 * @param d the access device
840 * @param p the port of the device
841 * @return true if the given port is a uni port
Gamze Abakaad329652018-12-20 10:12:21 +0000842 */
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100843 private boolean isUniPort(Device d, Port p) {
844 Port ulPort = getUplinkPort(d);
845 if (ulPort != null) {
846 return (ulPort.number().toLong() != p.number().toLong());
847 }
848 return false;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700849 }
850
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000851 /**
852 * Gets the given device details from SADIS.
853 * If the device is not found, returns null
854 *
855 * @param dev the access device
856 * @return the olt information
857 */
Jonathan Hart4c538002018-08-23 10:11:54 -0700858 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
859 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +0000860 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -0700861 }
862
alshabibf0e7e702015-05-30 18:22:36 -0700863 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800864 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
865
alshabibf0e7e702015-05-30 18:22:36 -0700866 @Override
867 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700868 eventExecutor.execute(() -> {
869 DeviceId devId = event.subject().id();
870 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +0000871 Port port = event.port();
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000872 DeviceEvent.Type eventType = event.type();
Jonathan Hart4c538002018-08-23 10:11:54 -0700873
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000874 if (DeviceEvent.Type.PORT_STATS_UPDATED.equals(eventType) ||
875 DeviceEvent.Type.DEVICE_SUSPENDED.equals(eventType) ||
876 DeviceEvent.Type.DEVICE_UPDATED.equals(eventType)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700877 return;
878 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700879
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000880 log.debug("OLT got {} event for {} {}", eventType, event.subject(), event.port());
881
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700882 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -0800883 // it's possible that we got an event for a previously
884 // programmed OLT that is no longer available in SADIS
885 // we let such events go through
886 if (!programmedDevices.contains(devId)) {
887 log.warn("No device info found for {}, this is either "
888 + "not an OLT or not known to sadis", dev);
889 return;
890 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700891 }
Jonathan Hart4c538002018-08-23 10:11:54 -0700892
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700893 switch (event.type()) {
894 //TODO: Port handling and bookkeeping should be improved once
895 // olt firmware handles correct behaviour.
896 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000897 if (isUniPort(dev, port)) {
898 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000899
900 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
901 log.info("eapol will be sent for port added {}", port);
902 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Gamze Abakada282b42019-03-11 13:16:48 +0000903 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700904 }
905 } else {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000906 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
907 if (deviceInfo != null) {
908 oltFlowService.processNniFilteringObjectives(dev.id(), port.number(), true);
909 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700910 }
911 break;
912 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000913 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +0000914 removeSubscriber(new ConnectPoint(devId, port.number()));
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000915 log.info("eapol will be send for port removed", port);
916 oltFlowService.processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Andy Bavier160e8682019-05-07 18:32:22 -0700917 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
918
Gamze Abaka838d8142019-02-21 07:06:55 +0000919 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700920 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700921 break;
922 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +0000923 if (!isUniPort(dev, port)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700924 break;
925 }
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000926 Set<UniTagInformation> uniTagInformationSet = programmedSubs
Gamze Abakada282b42019-03-11 13:16:48 +0000927 .get(new ConnectPoint(devId, port.number()));
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000928 if (uniTagInformationSet == null || uniTagInformationSet.isEmpty()) {
929 if (port.isEnabled() && !port.number().equals(PortNumber.LOCAL)) {
930 log.info("eapol will be processed for port updated {}", port);
931 oltFlowService.processEapolFilteringObjectives(devId, port.number(), DEFAULT_BP_ID,
932 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), port.isEnabled());
933 }
934 } else {
935 log.info("eapol will be processed for port updated {}", port);
936 uniTagInformationSet.forEach(uniTag ->
937 oltFlowService.processEapolFilteringObjectives(devId, port.number(),
938 uniTag.getUpstreamBandwidthProfile(), null,
939 uniTag.getPonCTag(), port.isEnabled()));
940 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000941 if (port.isEnabled()) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000942 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700943 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +0000944 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700945 }
alshabibbb83aa22016-02-10 15:08:23 -0800946 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700947 case DEVICE_ADDED:
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000948 handleDeviceConnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700949 break;
950 case DEVICE_REMOVED:
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000951 handleDeviceDisconnection(dev, true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700952 break;
953 case DEVICE_AVAILABILITY_CHANGED:
954 if (deviceService.isAvailable(devId)) {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000955 handleDeviceConnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700956 } else {
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000957 handleDeviceDisconnection(dev, false);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700958 }
959 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700960 default:
961 return;
962 }
963 });
alshabibf0e7e702015-05-30 18:22:36 -0700964 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000965
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000966 private void sendUniEvent(Device device, AccessDeviceEvent.Type eventType) {
967 deviceService.getPorts(device.id()).stream()
968 .filter(p -> !PortNumber.LOCAL.equals(p.number()))
969 .filter(p -> isUniPort(device, p))
970 .forEach(p -> post(new AccessDeviceEvent(eventType, device.id(), p)));
971 }
972
973 private void handleDeviceDisconnection(Device device, boolean sendUniEvent) {
974 programmedDevices.remove(device.id());
975 removeAllSubscribers(device.id());
976 post(new AccessDeviceEvent(
977 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, device.id(),
978 null, null, null));
979 if (sendUniEvent) {
980 sendUniEvent(device, AccessDeviceEvent.Type.UNI_REMOVED);
Gamze Abaka838d8142019-02-21 07:06:55 +0000981 }
Gamze Abaka1b7816e2019-11-25 06:38:41 +0000982 }
983
984 private void handleDeviceConnection(Device dev, boolean sendUniEvent) {
985 post(new AccessDeviceEvent(
986 AccessDeviceEvent.Type.DEVICE_CONNECTED, dev.id(),
987 null, null, null));
988 programmedDevices.add(dev.id());
989 checkAndCreateDeviceFlows(dev);
990 if (sendUniEvent) {
991 sendUniEvent(dev, AccessDeviceEvent.Type.UNI_ADDED);
992 }
Gamze Abaka838d8142019-02-21 07:06:55 +0000993 }
Gamze Abakada282b42019-03-11 13:16:48 +0000994
995 private void removeAllSubscribers(DeviceId deviceId) {
996 List<ConnectPoint> connectPoints = programmedSubs.keySet().stream()
997 .filter(ks -> Objects.equals(ks.deviceId(), deviceId))
998 .collect(Collectors.toList());
999
1000 connectPoints.forEach(cp -> programmedSubs.remove(cp));
1001 }
A R Karthicke135d692019-10-29 00:15:51 +00001002
Gamze Abaka641fc072018-09-04 09:16:27 +00001003 }
A R Karthicke135d692019-10-29 00:15:51 +00001004}