blob: 57e940a36acc549c7e9baa713de2beef118a26a7 [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
25import java.util.AbstractMap;
Saurav Das62ad75e2019-03-05 12:22:22 -080026import java.util.Arrays;
Gamze Abaka33feef52019-02-27 08:16:47 +000027import java.util.ArrayList;
Saurav Das82b8e6d2018-10-04 15:25:12 -070028import java.util.Collection;
29import java.util.Dictionary;
30import java.util.List;
31import java.util.Map;
Gamze Abakada282b42019-03-11 13:16:48 +000032import java.util.Objects;
Saurav Das82b8e6d2018-10-04 15:25:12 -070033import java.util.Optional;
34import java.util.Properties;
Gamze Abaka33feef52019-02-27 08:16:47 +000035import java.util.Set;
Saurav Das82b8e6d2018-10-04 15:25:12 -070036import java.util.concurrent.CompletableFuture;
Gamze Abaka33feef52019-02-27 08:16:47 +000037import java.util.concurrent.ConcurrentHashMap;
Saurav Das82b8e6d2018-10-04 15:25:12 -070038import java.util.concurrent.ExecutorService;
39import java.util.concurrent.Executors;
Gamze Abakada282b42019-03-11 13:16:48 +000040import java.util.stream.Collectors;
Saurav Das82b8e6d2018-10-04 15:25:12 -070041
Gamze Abaka33feef52019-02-27 08:16:47 +000042import com.google.common.collect.ImmutableSet;
alshabibf0e7e702015-05-30 18:22:36 -070043import org.apache.felix.scr.annotations.Activate;
44import org.apache.felix.scr.annotations.Component;
45import org.apache.felix.scr.annotations.Deactivate;
alshabibe0559672016-02-21 14:49:51 -080046import org.apache.felix.scr.annotations.Modified;
47import org.apache.felix.scr.annotations.Property;
alshabibf0e7e702015-05-30 18:22:36 -070048import org.apache.felix.scr.annotations.Reference;
49import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070050import org.apache.felix.scr.annotations.Service;
alshabibdec2e252016-01-15 12:20:25 -080051import org.onlab.packet.EthType;
Amit Ghosh95e2f652017-08-23 12:49:46 +010052import org.onlab.packet.IPv4;
Matteo Scandolo63460d12018-11-02 16:19:04 -070053import org.onlab.packet.IPv6;
Amit Ghosh95e2f652017-08-23 12:49:46 +010054import org.onlab.packet.TpPort;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onlab.packet.VlanId;
Amit Ghosh95e2f652017-08-23 12:49:46 +010056import org.onlab.util.Tools;
alshabibe0559672016-02-21 14:49:51 -080057import org.onosproject.cfg.ComponentConfigService;
alshabibf0e7e702015-05-30 18:22:36 -070058import org.onosproject.core.ApplicationId;
59import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080060import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080061import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010062import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070063import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010064import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070065import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080066import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070067import org.onosproject.net.PortNumber;
68import org.onosproject.net.device.DeviceEvent;
69import org.onosproject.net.device.DeviceListener;
70import org.onosproject.net.device.DeviceService;
71import org.onosproject.net.flow.DefaultTrafficSelector;
72import org.onosproject.net.flow.DefaultTrafficTreatment;
73import org.onosproject.net.flow.TrafficSelector;
74import org.onosproject.net.flow.TrafficTreatment;
alshabibdec2e252016-01-15 12:20:25 -080075import org.onosproject.net.flow.criteria.Criteria;
76import org.onosproject.net.flowobjective.DefaultFilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070077import org.onosproject.net.flowobjective.DefaultForwardingObjective;
alshabibdec2e252016-01-15 12:20:25 -080078import org.onosproject.net.flowobjective.FilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070079import org.onosproject.net.flowobjective.FlowObjectiveService;
80import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080081import org.onosproject.net.flowobjective.Objective;
82import org.onosproject.net.flowobjective.ObjectiveContext;
83import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das62ad75e2019-03-05 12:22:22 -080084import org.onosproject.store.serializers.KryoNamespaces;
85import org.onosproject.store.service.ConsistentMultimap;
86import org.onosproject.store.service.Serializer;
87import org.onosproject.store.service.StorageService;
Gamze Abaka33feef52019-02-27 08:16:47 +000088import org.onosproject.net.meter.Band;
89import org.onosproject.net.meter.DefaultBand;
90import org.onosproject.net.meter.DefaultMeterRequest;
91import org.onosproject.net.meter.Meter;
92import org.onosproject.net.meter.MeterContext;
93import org.onosproject.net.meter.MeterFailReason;
94import org.onosproject.net.meter.MeterKey;
95import org.onosproject.net.meter.MeterService;
96import org.onosproject.net.meter.MeterListener;
97import org.onosproject.net.meter.MeterRequest;
98import org.onosproject.net.meter.MeterId;
99import org.onosproject.net.meter.MeterEvent;
alshabib36a4d732016-06-01 16:03:59 -0700100import org.opencord.olt.AccessDeviceEvent;
101import org.opencord.olt.AccessDeviceListener;
102import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +0100103import org.opencord.olt.AccessSubscriberId;
Gamze Abaka641fc072018-09-04 09:16:27 +0000104import org.opencord.sadis.BandwidthProfileInformation;
105import org.opencord.sadis.BaseInformationService;
106import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100107import org.opencord.sadis.SubscriberAndDeviceInformation;
alshabibe0559672016-02-21 14:49:51 -0800108import org.osgi.service.component.ComponentContext;
alshabibf0e7e702015-05-30 18:22:36 -0700109import org.slf4j.Logger;
110
Saurav Das82b8e6d2018-10-04 15:25:12 -0700111import com.google.common.collect.ImmutableMap;
112import com.google.common.collect.Maps;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800113import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700114
115/**
Jonathan Harte533a422015-10-20 17:31:24 -0700116 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700117 */
Jonathan Harte533a422015-10-20 17:31:24 -0700118@Service
alshabibf0e7e702015-05-30 18:22:36 -0700119@Component(immediate = true)
alshabib8e4fd2f2016-01-12 15:55:53 -0800120public class Olt
121 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
122 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800123 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800124
125 private static final short DEFAULT_VLAN = 0;
Gamze Abakada282b42019-03-11 13:16:48 +0000126 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000127 private static final int DEFAULT_TP_ID = 64;
Gamze Abakaad329652018-12-20 10:12:21 +0000128 private static final String DEFAULT_BP_ID = "Default";
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100129 private static final String ADDITIONAL_VLANS = "additional-vlans";
Gamze Abaka838d8142019-02-21 07:06:55 +0000130 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
131 private static final String INSTALLED = "installed";
132 private static final String REMOVED = "removed";
133 private static final String INSTALLATION = "installation";
134 private static final String REMOVAL = "removal";
alshabibe0559672016-02-21 14:49:51 -0800135
alshabibf0e7e702015-05-30 18:22:36 -0700136 private final Logger log = getLogger(getClass());
137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected FlowObjectiveService flowObjectiveService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib09753b52016-03-04 14:55:19 -0800142 protected MastershipService mastershipService;
143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibf0e7e702015-05-30 18:22:36 -0700145 protected DeviceService deviceService;
146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
148 protected CoreService coreService;
149
Jonathan Harte533a422015-10-20 17:31:24 -0700150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe0559672016-02-21 14:49:51 -0800151 protected ComponentConfigService componentConfigService;
152
alshabib4ceaed32016-03-03 18:00:58 -0800153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000154 protected SadisService sadisService;
155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
157 protected MeterService meterService;
alshabibe0559672016-02-21 14:49:51 -0800158
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
160 protected StorageService storageService;
161
alshabibe0559672016-02-21 14:49:51 -0800162 @Property(name = "defaultVlan", intValue = DEFAULT_VLAN,
163 label = "Default VLAN RG<->ONU traffic")
164 private int defaultVlan = DEFAULT_VLAN;
165
Matt Jeanneret3f579262018-06-14 17:16:23 -0400166 @Property(name = "enableDhcpOnProvisioning", boolValue = true,
167 label = "Create the DHCP Flow rules when a subscriber is provisioned")
168 protected boolean enableDhcpOnProvisioning = false;
169
Matteo Scandolo63460d12018-11-02 16:19:04 -0700170 @Property(name = "enableDhcpV4", boolValue = true,
171 label = "Enable flows for DHCP v4")
172 protected boolean enableDhcpV4 = true;
173
174 @Property(name = "enableDhcpV6", boolValue = true,
175 label = "Enable flows for DHCP v6")
176 protected boolean enableDhcpV6 = false;
177
Matt Jeanneret3f579262018-06-14 17:16:23 -0400178 @Property(name = "enableIgmpOnProvisioning", boolValue = false,
179 label = "Create IGMP Flow rules when a subscriber is provisioned")
180 protected boolean enableIgmpOnProvisioning = false;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100181
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000182 @Property(name = "deleteMeters", boolValue = true,
Gamze Abaka641fc072018-09-04 09:16:27 +0000183 label = "Deleting Meters based on flow count statistics")
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000184 protected boolean deleteMeters = true;
Gamze Abaka641fc072018-09-04 09:16:27 +0000185
Gamze Abakaad329652018-12-20 10:12:21 +0000186 @Property(name = "defaultTechProfileId", intValue = DEFAULT_TP_ID,
187 label = "Default technology profile id that is used for authentication trap flows")
188 protected int defaultTechProfileId = DEFAULT_TP_ID;
189
190 @Property(name = "defaultBpId", value = DEFAULT_BP_ID,
191 label = "Default bandwidth profile id that is used for authentication trap flows")
192 protected String defaultBpId = DEFAULT_BP_ID;
193
Gamze Abaka33feef52019-02-27 08:16:47 +0000194 @Property(name = "enableEapol", boolValue = true,
195 label = "Send EAPOL authentication trap flows before subscriber provisioning")
196 protected boolean enableEapol = true;
197
alshabibf0e7e702015-05-30 18:22:36 -0700198 private final DeviceListener deviceListener = new InternalDeviceListener();
Gamze Abaka641fc072018-09-04 09:16:27 +0000199 private final MeterListener meterListener = new InternalMeterListener();
alshabibf0e7e702015-05-30 18:22:36 -0700200
201 private ApplicationId appId;
Gamze Abaka641fc072018-09-04 09:16:27 +0000202 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
203 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700204
Gamze Abaka33feef52019-02-27 08:16:47 +0000205 private Map<String, List<MeterKey>> bpInfoToMeter = new ConcurrentHashMap<>();
Gamze Abaka641fc072018-09-04 09:16:27 +0000206
207 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
208 groupedThreads("onos/olt-service",
209 "olt-installer-%d"));
alshabibf0e7e702015-05-30 18:22:36 -0700210
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100211 private ConsistentMultimap<ConnectPoint, Map.Entry<VlanId, VlanId>> additionalVlans;
212
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700213 protected ExecutorService eventExecutor;
214
Saurav Das82b8e6d2018-10-04 15:25:12 -0700215 private Map<ConnectPoint, SubscriberAndDeviceInformation> programmedSubs;
Gamze Abaka33feef52019-02-27 08:16:47 +0000216 private Set<MeterKey> programmedMeters;
Saurav Das82b8e6d2018-10-04 15:25:12 -0700217
Saurav Dasa9d5f442019-03-06 19:32:48 -0800218
alshabibf0e7e702015-05-30 18:22:36 -0700219 @Activate
alshabibe0559672016-02-21 14:49:51 -0800220 public void activate(ComponentContext context) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700221 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt", "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800222 modified(context);
Charles Chan54f110f2017-01-20 11:22:42 -0800223 appId = coreService.registerApplication(APP_NAME);
Saurav Das62ad75e2019-03-05 12:22:22 -0800224
225 // ensure that flow rules are purged from flow-store upon olt-disconnection
226 // when olt reconnects, the port-numbers may change for the ONUs
227 // making flows pushed earlier invalid
228 componentConfigService
229 .preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Gamze Abaka33feef52019-02-27 08:16:47 +0000230 "purgeOnDisconnection", "true");
Gamze Abakada282b42019-03-11 13:16:48 +0000231 componentConfigService
232 .preSetProperty("org.onosproject.net.meter.impl.MeterManager",
233 "purgeOnDisconnection", "true");
alshabibe0559672016-02-21 14:49:51 -0800234 componentConfigService.registerProperties(getClass());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700235 programmedSubs = Maps.newConcurrentMap();
Gamze Abaka33feef52019-02-27 08:16:47 +0000236 programmedMeters = ConcurrentHashMap.newKeySet();
alshabibc4dfe852015-06-05 13:35:13 -0700237
alshabib8e4fd2f2016-01-12 15:55:53 -0800238 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
239
Gamze Abaka641fc072018-09-04 09:16:27 +0000240 subsService = sadisService.getSubscriberInfoService();
241 bpService = sadisService.getBandwidthProfileService();
242
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100243 // look for all provisioned devices in Sadis and create EAPOL flows for the
244 // UNI ports
245 Iterable<Device> devices = deviceService.getDevices();
246 for (Device d : devices) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700247 checkAndCreateDeviceFlows(d);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100248 }
alshabib4ceaed32016-03-03 18:00:58 -0800249
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100250 additionalVlans = storageService.<ConnectPoint, Map.Entry<VlanId, VlanId>>consistentMultimapBuilder()
251 .withName(ADDITIONAL_VLANS)
252 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
253 AbstractMap.SimpleEntry.class))
254 .build();
255
alshabibba357492016-01-27 13:49:46 -0800256 deviceService.addListener(deviceListener);
Gamze Abaka641fc072018-09-04 09:16:27 +0000257 meterService.addListener(meterListener);
alshabibba357492016-01-27 13:49:46 -0800258
alshabibf0e7e702015-05-30 18:22:36 -0700259 log.info("Started with Application ID {}", appId.id());
260 }
261
262 @Deactivate
263 public void deactivate() {
alshabibe0559672016-02-21 14:49:51 -0800264 componentConfigService.unregisterProperties(getClass(), false);
alshabib62e9ce72016-02-11 17:31:36 -0800265 deviceService.removeListener(deviceListener);
Gamze Abaka641fc072018-09-04 09:16:27 +0000266 meterService.removeListener(meterListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700267 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700268 log.info("Stopped");
269 }
270
alshabibe0559672016-02-21 14:49:51 -0800271 @Modified
272 public void modified(ComponentContext context) {
273 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
274
275 try {
276 String s = get(properties, "defaultVlan");
277 defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100278
Matt Jeanneret3f579262018-06-14 17:16:23 -0400279 Boolean o = Tools.isPropertyEnabled(properties, "enableDhcpOnProvisioning");
Amit Ghosh95e2f652017-08-23 12:49:46 +0100280 if (o != null) {
Matt Jeanneret3f579262018-06-14 17:16:23 -0400281 enableDhcpOnProvisioning = o;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100282 }
Matt Jeanneret3f579262018-06-14 17:16:23 -0400283
Matteo Scandolo63460d12018-11-02 16:19:04 -0700284 Boolean v4 = Tools.isPropertyEnabled(properties, "enableDhcpV4");
285 if (v4 != null) {
286 enableDhcpV4 = v4;
287 }
288
289 Boolean v6 = Tools.isPropertyEnabled(properties, "enableDhcpV6");
290 if (v6 != null) {
291 enableDhcpV6 = v6;
292 }
293
Matt Jeanneret3f579262018-06-14 17:16:23 -0400294 Boolean p = Tools.isPropertyEnabled(properties, "enableIgmpOnProvisioning");
295 if (p != null) {
296 enableIgmpOnProvisioning = p;
297 }
298
Matteo Scandolo63460d12018-11-02 16:19:04 -0700299 log.info("DHCP Settings [enableDhcpOnProvisioning: {}, enableDhcpV4: {}, enableDhcpV6: {}]",
Gamze Abakaad329652018-12-20 10:12:21 +0000300 enableDhcpOnProvisioning, enableDhcpV4, enableDhcpV6);
Matteo Scandolo63460d12018-11-02 16:19:04 -0700301
Gamze Abaka641fc072018-09-04 09:16:27 +0000302 Boolean d = Tools.isPropertyEnabled(properties, "deleteMeters");
303 if (d != null) {
304 deleteMeters = d;
305 }
306
Gamze Abakaad329652018-12-20 10:12:21 +0000307 String tpId = get(properties, "defaultTechProfileId");
308 defaultTechProfileId = isNullOrEmpty(s) ? DEFAULT_TP_ID : Integer.parseInt(tpId.trim());
309
310 String bpId = get(properties, "defaultBpId");
311 defaultBpId = bpId;
312
Gamze Abaka33feef52019-02-27 08:16:47 +0000313 Boolean eap = Tools.isPropertyEnabled(properties, "enableEapol");
314 if (eap != null) {
315 enableEapol = eap;
316 }
317
alshabibe0559672016-02-21 14:49:51 -0800318 } catch (Exception e) {
319 defaultVlan = DEFAULT_VLAN;
320 }
321 }
322
alshabib32232c82016-02-25 17:57:24 -0500323 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000324 public boolean provisionSubscriber(ConnectPoint connectPoint) {
325
326 DeviceId deviceId = connectPoint.deviceId();
327 PortNumber subscriberPortNo = connectPoint.port();
328
329 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Jonathan Hart94b90492018-04-24 14:02:25 -0700330 "Invalid connect point");
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100331 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000332 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100333 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000334 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100335 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100336 }
Jonathan Harte533a422015-10-20 17:31:24 -0700337
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100338 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000339 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100340 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000341 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100342 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700343 }
344
Gamze Abaka33feef52019-02-27 08:16:47 +0000345 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
346
Gamze Abaka838d8142019-02-21 07:06:55 +0000347 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000348 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakada282b42019-03-11 13:16:48 +0000349 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
350 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000351 removeMeterIdFromBpMapping(deviceId, defaultBpId);
Gamze Abaka838d8142019-02-21 07:06:55 +0000352
Gamze Abaka33feef52019-02-27 08:16:47 +0000353 //install subscriber flows
354 filterFuture.thenAcceptAsync(filterStatus -> {
355 if (filterStatus == null) {
356 provisionSubscriberBasedFlows(connectPoint, uplinkPort.number(), Optional.empty(), sub);
357 }
358 });
Gamze Abaka838d8142019-02-21 07:06:55 +0000359
Saurav Das82b8e6d2018-10-04 15:25:12 -0700360 // cache subscriber info
Gamze Abaka838d8142019-02-21 07:06:55 +0000361 programmedSubs.put(connectPoint, sub);
Amit Ghosh31939522018-08-16 13:28:21 +0100362 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800363 }
364
365 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000366 public boolean removeSubscriber(ConnectPoint connectPoint) {
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800367 // Get the subscriber connected to this port from the local cache
368 // as if we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000369
370 DeviceId deviceId = connectPoint.deviceId();
371 PortNumber subscriberPortNo = connectPoint.port();
372
373 SubscriberAndDeviceInformation subscriber = programmedSubs.get(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100374 if (subscriber == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000375 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
376 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800377 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800378 }
379
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100380 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000381 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100382 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000383 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100384 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800385 }
386
Gamze Abaka33feef52019-02-27 08:16:47 +0000387 //delete dhcp & igmp trap flows
388 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100389
Gamze Abaka33feef52019-02-27 08:16:47 +0000390 //process dhcp filtering
391 processDhcpFilteringObjectives(deviceId, subscriberPortNo,
392 upstreamMeterId, subscriber.technologyProfileId(), false, true);
Gamze Abaka838d8142019-02-21 07:06:55 +0000393
Gamze Abaka33feef52019-02-27 08:16:47 +0000394 //process igmp filtering
395 processIgmpFilteringObjectives(deviceId, subscriberPortNo,
396 upstreamMeterId, subscriber.technologyProfileId(), false);
alshabibbf23a1f2016-01-14 17:27:11 -0800397
Gamze Abaka33feef52019-02-27 08:16:47 +0000398 //unprovision vlans
399 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, subscriber, Optional.empty());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100400
401 // Remove if there are any flows for the additional Vlans
Gamze Abaka838d8142019-02-21 07:06:55 +0000402 Collection<? extends Map.Entry<VlanId, VlanId>> vlansList = additionalVlans.get(connectPoint).value();
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100403
404 // Remove the flows for the additional vlans for this subscriber
405 for (Map.Entry<VlanId, VlanId> vlans : vlansList) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000406 unprovisionTransparentFlows(deviceId, uplinkPort.number(), subscriberPortNo,
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100407 vlans.getValue(), vlans.getKey());
408
409 // Remove it from the map also
Gamze Abaka838d8142019-02-21 07:06:55 +0000410 additionalVlans.remove(connectPoint, vlans);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100411 }
412
Gamze Abaka33feef52019-02-27 08:16:47 +0000413 //re-install eapol
414 processEapolFilteringObjectives(deviceId, subscriberPortNo,
Gamze Abakada282b42019-03-11 13:16:48 +0000415 subscriber.upstreamBandwidthProfile(), null, subscriber.cTag(), false);
416 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
417 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Gamze Abaka33feef52019-02-27 08:16:47 +0000418
Gamze Abaka838d8142019-02-21 07:06:55 +0000419 programmedSubs.remove(connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100420 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800421 }
422
Amit Ghosh31939522018-08-16 13:28:21 +0100423 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100424 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Gamze Abakaf59c0912019-04-19 08:24:28 +0000425
426 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}", subscriberId, sTag, cTag);
427
Amit Ghosh31939522018-08-16 13:28:21 +0100428 // Check if we can find the connect point to which this subscriber is connected
429 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
430 if (subsPort == null) {
431 log.warn("ConnectPoint for {} not found", subscriberId);
432 return false;
433 }
434
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100435 if (!sTag.isPresent() && !cTag.isPresent()) {
436 return provisionSubscriber(subsPort);
437 } else if (sTag.isPresent() && cTag.isPresent()) {
438 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
439 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000440 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100441 return false;
442 }
443
Gamze Abakaf59c0912019-04-19 08:24:28 +0000444 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
445
446 //delete Eapol authentication flow with default bandwidth
447 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
448 processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId, filterFuture,
449 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
450 removeMeterIdFromBpMapping(subsPort.deviceId(), defaultBpId);
451
452 //install subscriber flows
453 filterFuture.thenAcceptAsync(filterStatus -> {
454 if (filterStatus == null) {
455 provisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
456 cTag.get(), sTag.get());
457 }
458 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100459 return true;
460 } else {
461 log.warn("Provisioning failed for subscriber: {}", subscriberId);
462 return false;
463 }
Amit Ghosh31939522018-08-16 13:28:21 +0100464 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100465
alshabibe0559672016-02-21 14:49:51 -0800466 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100467 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Amit Ghosh31939522018-08-16 13:28:21 +0100468 // Check if we can find the connect point to which this subscriber is connected
469 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
470 if (subsPort == null) {
471 log.warn("ConnectPoint for {} not found", subscriberId);
472 return false;
473 }
474
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100475 if (!sTag.isPresent() && !cTag.isPresent()) {
476 return removeSubscriber(subsPort);
477 } else if (sTag.isPresent() && cTag.isPresent()) {
478 // Get the uplink port
479 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
480 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000481 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100482 return false;
483 }
484
485 unprovisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
486 cTag.get(), sTag.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000487
488 programmedSubs.remove(subsPort);
489
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100490 return true;
491 } else {
492 log.warn("Removing subscriber failed for: {}", subscriberId);
493 return false;
494 }
Amit Ghosh31939522018-08-16 13:28:21 +0100495 }
496
497 @Override
498 public Collection<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> getSubscribers() {
499 ArrayList<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> subs = new ArrayList<>();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100500
Saurav Das82b8e6d2018-10-04 15:25:12 -0700501 // Get the subscribers for all the devices configured in sadis
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100502 // If the port is UNI, is enabled and exists in Sadis then copy it
503 for (Device d : deviceService.getDevices()) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700504 if (getOltInfo(d) == null) {
505 continue; // not an olt, or not configured in sadis
506 }
Gamze Abakaad329652018-12-20 10:12:21 +0000507 for (Port p : deviceService.getPorts(d.id())) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100508 if (isUniPort(d, p) && p.isEnabled()) {
509 ConnectPoint cp = new ConnectPoint(d.id(), p.number());
510
511 SubscriberAndDeviceInformation sub = getSubscriber(cp);
512 if (sub != null) {
Amit Ghosh31939522018-08-16 13:28:21 +0100513 Map.Entry<VlanId, VlanId> vlans = new AbstractMap.SimpleEntry(sub.sTag(), sub.cTag());
514 subs.add(new AbstractMap.SimpleEntry(cp, vlans));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100515 }
516 }
517 }
518 }
519
520 return subs;
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800521 }
522
523 @Override
Saurav Das82b8e6d2018-10-04 15:25:12 -0700524 public ImmutableMap<ConnectPoint, SubscriberAndDeviceInformation> getProgSubs() {
525 return ImmutableMap.copyOf(programmedSubs);
526 }
527
528 @Override
Gamze Abaka33feef52019-02-27 08:16:47 +0000529 public ImmutableSet<MeterKey> getProgMeters() {
530 return ImmutableSet.copyOf(programmedMeters);
531 }
532
533 @Override
534 public ImmutableMap<String, List<MeterKey>> getBpMeterMappings() {
535 return ImmutableMap.copyOf(bpInfoToMeter);
536 }
537
538 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100539 public List<DeviceId> fetchOlts() {
540 // look through all the devices and find the ones that are OLTs as per Sadis
541 List<DeviceId> olts = new ArrayList<>();
542 Iterable<Device> devices = deviceService.getDevices();
543 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700544 if (getOltInfo(d) != null) {
545 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100546 olts.add(d.id());
547 }
548 }
549 return olts;
alshabibe0559672016-02-21 14:49:51 -0800550 }
551
Amit Ghosh31939522018-08-16 13:28:21 +0100552 /**
553 * Finds the connect point to which a subscriber is connected.
554 *
555 * @param id The id of the subscriber, this is the same ID as in Sadis
556 * @return Subscribers ConnectPoint if found else null
557 */
558 private ConnectPoint findSubscriberConnectPoint(String id) {
559
560 Iterable<Device> devices = deviceService.getDevices();
561 for (Device d : devices) {
562 for (Port p : deviceService.getPorts(d.id())) {
563 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
564 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
565 log.debug("Found on device {} port {}", d.id(), p.number());
566 return new ConnectPoint(d.id(), p.number());
567 }
568 }
569 }
570 return null;
571 }
572
Gamze Abaka641fc072018-09-04 09:16:27 +0000573 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
574 if (bandwidthProfile == null) {
575 return null;
576 }
577 return bpService.get(bandwidthProfile);
578 }
579
Gamze Abaka838d8142019-02-21 07:06:55 +0000580 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000581 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000582 *
583 * @param deviceId the device identifier
584 * @param uplink uplink port of the OLT
585 * @param subscriberPort uni port
586 * @param subscriber subscriber info that includes s, c tags, tech profile and bandwidth profile references
587 * @param defaultVlan default vlan of the subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +0000588 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000589 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
590 PortNumber subscriberPort, SubscriberAndDeviceInformation subscriber,
591 Optional<VlanId> defaultVlan) {
592
593 log.info("Unprovisioning vlans...");
alshabibbf23a1f2016-01-14 17:27:11 -0800594
595 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
596 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
597
Gamze Abaka641fc072018-09-04 09:16:27 +0000598 VlanId deviceVlan = subscriber.sTag();
599 VlanId subscriberVlan = subscriber.cTag();
600
Gamze Abaka33feef52019-02-27 08:16:47 +0000601 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
602 MeterId downstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.downstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000603
alshabib4ceaed32016-03-03 18:00:58 -0800604 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000605 subscriberVlan, deviceVlan,
606 defaultVlan, upstreamMeterId, subscriber.technologyProfileId());
alshabib4ceaed32016-03-03 18:00:58 -0800607 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000608 subscriberVlan, deviceVlan,
609 defaultVlan, downstreamMeterId, subscriber.technologyProfileId());
alshabibbf23a1f2016-01-14 17:27:11 -0800610
alshabib4ceaed32016-03-03 18:00:58 -0800611 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
612 @Override
613 public void onSuccess(Objective objective) {
614 upFuture.complete(null);
615 }
alshabibbf23a1f2016-01-14 17:27:11 -0800616
alshabib4ceaed32016-03-03 18:00:58 -0800617 @Override
618 public void onError(Objective objective, ObjectiveError error) {
619 upFuture.complete(error);
620 }
621 }));
622
623 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
624 @Override
625 public void onSuccess(Objective objective) {
626 downFuture.complete(null);
627 }
628
629 @Override
630 public void onError(Objective objective, ObjectiveError error) {
631 downFuture.complete(error);
632 }
633 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800634
635 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
636 if (upStatus == null && downStatus == null) {
637 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000638 deviceId,
639 deviceVlan,
640 subscriberVlan));
alshabibbf23a1f2016-01-14 17:27:11 -0800641 } else if (downStatus != null) {
642 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000643 "on port {} failed downstream uninstallation: {}",
644 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800645 } else if (upStatus != null) {
646 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000647 "on port {} failed upstream uninstallation: {}",
648 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800649 }
650 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800651
Gamze Abaka33feef52019-02-27 08:16:47 +0000652 programmedMeters.remove(MeterKey.key(deviceId, upstreamMeterId));
653 programmedMeters.remove(MeterKey.key(deviceId, downstreamMeterId));
Gamze Abaka838d8142019-02-21 07:06:55 +0000654 log.debug("programmed Meters size {}", programmedMeters.size());
Jonathan Harte533a422015-10-20 17:31:24 -0700655 }
656
Gamze Abaka838d8142019-02-21 07:06:55 +0000657 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000658 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000659 *
660 * @param port the connection point of the subscriber
661 * @param uplinkPort uplink port of the OLT
662 * @param defaultVlan default vlan of the subscriber
663 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000664 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000665 private void provisionSubscriberBasedFlows(ConnectPoint port, PortNumber uplinkPort, Optional<VlanId> defaultVlan,
666 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000667
668 log.info("Provisioning vlans...");
669
670 DeviceId deviceId = port.deviceId();
671 PortNumber subscriberPort = port.port();
672 VlanId deviceVlan = sub.sTag();
673 VlanId subscriberVlan = sub.cTag();
674 int techProfId = sub.technologyProfileId();
675
676 BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(sub.upstreamBandwidthProfile());
677 BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(sub.downstreamBandwidthProfile());
678
alshabib3ea82642016-01-12 18:06:53 -0800679 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
680 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
Gamze Abaka33feef52019-02-27 08:16:47 +0000681 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
682 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
alshabib3ea82642016-01-12 18:06:53 -0800683
Gamze Abaka33feef52019-02-27 08:16:47 +0000684 MeterId upstreamMeterId = createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
685 MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Jonathan Harte533a422015-10-20 17:31:24 -0700686
Gamze Abaka33feef52019-02-27 08:16:47 +0000687 //install upstream flows
688 upstreamMeterFuture.thenAcceptAsync(result -> {
689 if (result == null) {
690 log.info("Upstream Meter {} is sent to the device {}. " +
691 "Sending subscriber flows.", upstreamMeterId, deviceId);
692 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
693 subscriberVlan, deviceVlan,
694 defaultVlan, upstreamMeterId, techProfId);
alshabib3ea82642016-01-12 18:06:53 -0800695
Gamze Abaka33feef52019-02-27 08:16:47 +0000696
697 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
698 @Override
699 public void onSuccess(Objective objective) {
700 upFuture.complete(null);
701 }
702
703 @Override
704 public void onError(Objective objective, ObjectiveError error) {
705 upFuture.complete(error);
706 }
707 }));
708 } else {
709 log.warn("Meter installation error while sending upstream flows. " +
710 "Result {} and MeterId {}", result, upstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800711 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000712 });
alshabibbf23a1f2016-01-14 17:27:11 -0800713
Gamze Abaka33feef52019-02-27 08:16:47 +0000714 //install downstream flows
715 downsteamMeterFuture.thenAcceptAsync(result -> {
716 if (result == null) {
717 log.info("Downstream Meter {} is sent to the device {}. " +
718 "Sending subscriber flows.", downstreamMeterId, deviceId);
719 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
720 subscriberVlan, deviceVlan,
721 defaultVlan, downstreamMeterId, techProfId);
722
723 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
724 @Override
725 public void onSuccess(Objective objective) {
726 downFuture.complete(null);
727 }
728
729 @Override
730 public void onError(Objective objective, ObjectiveError error) {
731 downFuture.complete(error);
732 }
733 }));
734 } else {
735 log.warn("Meter installation error while sending downstream flows. " +
736 "Result {} and MeterId {}", result, downstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800737 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000738 });
alshabibbf23a1f2016-01-14 17:27:11 -0800739
Gamze Abaka33feef52019-02-27 08:16:47 +0000740 //send eapol & dhcp & igmp flows
741 //send Subscriber Registered event
alshabib3ea82642016-01-12 18:06:53 -0800742 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
743 if (upStatus == null && downStatus == null) {
Gamze Abaka33feef52019-02-27 08:16:47 +0000744
745 if (upstreamMeterId != null) {
746 //re-install Eapol authentication flow with the subscribers' upstream bandwidth profile
747 processEapolFilteringObjectives(deviceId, subscriberPort, sub.upstreamBandwidthProfile(),
Gamze Abakada282b42019-03-11 13:16:48 +0000748 null, sub.cTag(), true);
Gamze Abaka33feef52019-02-27 08:16:47 +0000749
750 processDhcpFilteringObjectives(deviceId, subscriberPort,
751 upstreamMeterId, sub.technologyProfileId(), true, true);
752
753 processIgmpFilteringObjectives(deviceId, subscriberPort,
754 upstreamMeterId, sub.technologyProfileId(), true);
755 }
756
alshabib3ea82642016-01-12 18:06:53 -0800757 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000758 deviceId,
759 deviceVlan,
760 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800761
alshabib3ea82642016-01-12 18:06:53 -0800762 } else if (downStatus != null) {
763 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000764 "on port {} failed downstream installation: {}",
765 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabib3ea82642016-01-12 18:06:53 -0800766 } else if (upStatus != null) {
767 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000768 "on port {} failed upstream installation: {}",
769 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabib3ea82642016-01-12 18:06:53 -0800770 }
771 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700772 }
773
Gamze Abaka33feef52019-02-27 08:16:47 +0000774 private MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo,
775 CompletableFuture<Object> meterFuture) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000776 if (bpInfo == null) {
777 log.warn("Bandwidth profile information is not found");
778 return null;
779 }
780
Gamze Abaka33feef52019-02-27 08:16:47 +0000781 MeterId meterId = getMeterIdFromBpMapping(deviceId, bpInfo.id());
Gamze Abaka641fc072018-09-04 09:16:27 +0000782 if (meterId != null) {
783 log.info("Meter is already added. MeterId {}", meterId);
Gamze Abaka33feef52019-02-27 08:16:47 +0000784 meterFuture.complete(null);
Gamze Abaka641fc072018-09-04 09:16:27 +0000785 return meterId;
786 }
787
788 List<Band> meterBands = createMeterBands(bpInfo);
789
790 MeterRequest meterRequest = DefaultMeterRequest.builder()
791 .withBands(meterBands)
792 .withUnit(Meter.Unit.KB_PER_SEC)
Gamze Abaka33feef52019-02-27 08:16:47 +0000793 .withContext(new MeterContext() {
794 @Override
795 public void onSuccess(MeterRequest op) {
796 log.debug("meter addition completed");
797 meterFuture.complete(null);
798 }
799
800 @Override
801 public void onError(MeterRequest op, MeterFailReason reason) {
802 meterFuture.complete(reason);
803 }
804 })
Gamze Abaka641fc072018-09-04 09:16:27 +0000805 .forDevice(deviceId)
806 .fromApp(appId)
807 .burst()
808 .add();
809
810 Meter meter = meterService.submit(meterRequest);
Gamze Abaka33feef52019-02-27 08:16:47 +0000811 addMeterIdToBpMapping(deviceId, meter.id(), bpInfo.id());
Gamze Abaka641fc072018-09-04 09:16:27 +0000812 log.info("Meter is created. Meter Id {}", meter.id());
Gamze Abaka33feef52019-02-27 08:16:47 +0000813 programmedMeters.add(MeterKey.key(deviceId, meter.id()));
Gamze Abaka838d8142019-02-21 07:06:55 +0000814 log.debug("programmed Meters size {}", programmedMeters.size());
Gamze Abaka641fc072018-09-04 09:16:27 +0000815 return meter.id();
816 }
817
818 private List<Band> createMeterBands(BandwidthProfileInformation bpInfo) {
819 List<Band> meterBands = new ArrayList<>();
820
821 meterBands.add(createMeterBand(bpInfo.committedInformationRate(), bpInfo.committedBurstSize()));
822 meterBands.add(createMeterBand(bpInfo.exceededInformationRate(), bpInfo.exceededBurstSize()));
Gamze Abakaad329652018-12-20 10:12:21 +0000823 meterBands.add(createMeterBand(bpInfo.assuredInformationRate(), 0L));
Gamze Abaka641fc072018-09-04 09:16:27 +0000824
Gamze Abaka641fc072018-09-04 09:16:27 +0000825 return meterBands;
826 }
827
828 private Band createMeterBand(long rate, Long burst) {
829 return DefaultBand.builder()
830 .withRate(rate) //already Kbps
831 .burstSize(burst) // already Kbits
832 .ofType(Band.Type.DROP) // no matter
833 .build();
834 }
835
alshabib4ceaed32016-03-03 18:00:58 -0800836 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
837 PortNumber subscriberPort,
838 VlanId subscriberVlan,
839 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000840 Optional<VlanId> defaultVlan,
841 MeterId meterId,
842 int techProfId) {
alshabib4ceaed32016-03-03 18:00:58 -0800843 TrafficSelector downstream = DefaultTrafficSelector.builder()
844 .matchVlanId(deviceVlan)
845 .matchInPort(uplinkPort)
846 .matchInnerVlanId(subscriberVlan)
847 .build();
848
Gamze Abaka641fc072018-09-04 09:16:27 +0000849 TrafficTreatment.Builder downstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800850 .popVlan()
851 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
Gamze Abaka641fc072018-09-04 09:16:27 +0000852 .setOutput(subscriberPort);
853
854 if (meterId != null) {
855 downstreamTreatmentBuilder.meter(meterId);
856 }
857
Gamze Abakaad329652018-12-20 10:12:21 +0000858 downstreamTreatmentBuilder.writeMetadata(createMetadata(subscriberVlan, techProfId, subscriberPort), 0);
alshabib4ceaed32016-03-03 18:00:58 -0800859
860 return DefaultForwardingObjective.builder()
861 .withFlag(ForwardingObjective.Flag.VERSATILE)
862 .withPriority(1000)
863 .makePermanent()
864 .withSelector(downstream)
865 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000866 .withTreatment(downstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800867 }
868
869 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
870 PortNumber subscriberPort,
871 VlanId subscriberVlan,
872 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000873 Optional<VlanId> defaultVlan,
874 MeterId meterId,
875 int technologyProfileId) {
876
877
878 VlanId dVlan = defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan));
879
880 if (subscriberVlan.toShort() == 4096) {
881 dVlan = subscriberVlan;
882 }
883
alshabib4ceaed32016-03-03 18:00:58 -0800884 TrafficSelector upstream = DefaultTrafficSelector.builder()
Gamze Abaka641fc072018-09-04 09:16:27 +0000885 .matchVlanId(dVlan)
alshabib4ceaed32016-03-03 18:00:58 -0800886 .matchInPort(subscriberPort)
887 .build();
888
889
Gamze Abaka641fc072018-09-04 09:16:27 +0000890 TrafficTreatment.Builder upstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800891 .pushVlan()
892 .setVlanId(subscriberVlan)
893 .pushVlan()
894 .setVlanId(deviceVlan)
Gamze Abaka641fc072018-09-04 09:16:27 +0000895 .setOutput(uplinkPort);
896
897 if (meterId != null) {
898 upstreamTreatmentBuilder.meter(meterId);
899 }
900
Gamze Abakaad329652018-12-20 10:12:21 +0000901 upstreamTreatmentBuilder.writeMetadata(createMetadata(deviceVlan, technologyProfileId, uplinkPort), 0L);
alshabib4ceaed32016-03-03 18:00:58 -0800902
903 return DefaultForwardingObjective.builder()
904 .withFlag(ForwardingObjective.Flag.VERSATILE)
905 .withPriority(1000)
906 .makePermanent()
907 .withSelector(upstream)
908 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000909 .withTreatment(upstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800910 }
Gamze Abakaad329652018-12-20 10:12:21 +0000911
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100912 private void provisionTransparentFlows(DeviceId deviceId, PortNumber uplinkPort,
913 PortNumber subscriberPort,
914 VlanId innerVlan,
915 VlanId outerVlan) {
916
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100917 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
918
Gamze Abakaf59c0912019-04-19 08:24:28 +0000919 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
920
921 BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(
922 subInfo.upstreamBandwidthProfile());
923 BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(
924 subInfo.downstreamBandwidthProfile());
925
926 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
927 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
928 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
929 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
930
931 MeterId upstreamMeterId = createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
932 MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
933
934 upstreamMeterFuture.thenAcceptAsync(result -> {
935 if (result == null) {
936 log.info("Upstream Meter {} is sent to the device {}. " +
937 "Sending subscriber flows.", upstreamMeterId, deviceId);
938
939 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplinkPort, subscriberPort,
940 innerVlan, outerVlan, upstreamMeterId, subInfo);
941
942 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
943 @Override
944 public void onSuccess(Objective objective) {
945 upFuture.complete(null);
946 }
947
948 @Override
949 public void onError(Objective objective, ObjectiveError error) {
950 upFuture.complete(error);
951 }
952 }));
953
954 } else {
955 log.warn("Meter installation error while sending upstream flows. " +
956 "Result {} and MeterId {}", result, upstreamMeterId);
957 }
958 });
959
960 downsteamMeterFuture.thenAcceptAsync(result -> {
961 if (result == null) {
962 log.info("Downstream Meter {} is sent to the device {}. " +
963 "Sending subscriber flows.", downstreamMeterId, deviceId);
964
965 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplinkPort, subscriberPort,
966 innerVlan, outerVlan, downstreamMeterId, subInfo);
967
968 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
969 @Override
970 public void onSuccess(Objective objective) {
971 downFuture.complete(null);
972 }
973
974 @Override
975 public void onError(Objective objective, ObjectiveError error) {
976 downFuture.complete(error);
977 }
978 }));
979 } else {
980 log.warn("Meter installation error while sending upstream flows. " +
981 "Result {} and MeterId {}", result, downstreamMeterId);
982 }
983 });
984
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100985 additionalVlans.put(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
986
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100987 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
988 if (downStatus != null) {
989 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000990 "on port {} failed downstream installation: {}",
Gamze Abakaf59c0912019-04-19 08:24:28 +0000991 innerVlan, outerVlan, deviceId, cp, downStatus);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100992 } else if (upStatus != null) {
993 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000994 "on port {} failed upstream installation: {}",
Gamze Abakaf59c0912019-04-19 08:24:28 +0000995 innerVlan, outerVlan, deviceId, cp, upStatus);
996 } else {
997 processEapolFilteringObjectives(deviceId, subscriberPort, subInfo.upstreamBandwidthProfile(),
998 null, subInfo.cTag(), true);
999
1000 // cache subscriber info
1001 programmedSubs.put(cp, subInfo);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001002 }
1003 }, oltInstallers);
1004
1005 }
1006
1007 private ForwardingObjective.Builder transparentDownBuilder(PortNumber uplinkPort,
1008 PortNumber subscriberPort,
1009 VlanId innerVlan,
Gamze Abakaf59c0912019-04-19 08:24:28 +00001010 VlanId outerVlan,
1011 MeterId downstreamMeterId,
1012 SubscriberAndDeviceInformation subInfo) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001013 TrafficSelector downstream = DefaultTrafficSelector.builder()
1014 .matchVlanId(outerVlan)
1015 .matchInPort(uplinkPort)
1016 .matchInnerVlanId(innerVlan)
1017 .build();
1018
Gamze Abakaf59c0912019-04-19 08:24:28 +00001019 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1020 if (downstreamMeterId != null) {
1021 tBuilder.meter(downstreamMeterId);
1022 }
1023
1024 TrafficTreatment downstreamTreatment = tBuilder
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001025 .setOutput(subscriberPort)
Gamze Abakaf59c0912019-04-19 08:24:28 +00001026 .writeMetadata(createMetadata(subInfo.cTag(), subInfo.technologyProfileId(), subscriberPort), 0)
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001027 .build();
1028
1029 return DefaultForwardingObjective.builder()
1030 .withFlag(ForwardingObjective.Flag.VERSATILE)
1031 .withPriority(1000)
1032 .makePermanent()
1033 .withSelector(downstream)
1034 .fromApp(appId)
1035 .withTreatment(downstreamTreatment);
1036 }
1037
1038 private ForwardingObjective.Builder transparentUpBuilder(PortNumber uplinkPort,
1039 PortNumber subscriberPort,
1040 VlanId innerVlan,
Gamze Abakaf59c0912019-04-19 08:24:28 +00001041 VlanId outerVlan,
1042 MeterId upstreamMeterId,
1043 SubscriberAndDeviceInformation subInfo) {
1044
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001045 TrafficSelector upstream = DefaultTrafficSelector.builder()
1046 .matchVlanId(outerVlan)
1047 .matchInPort(subscriberPort)
1048 .matchInnerVlanId(innerVlan)
1049 .build();
1050
Gamze Abakaf59c0912019-04-19 08:24:28 +00001051 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1052 if (upstreamMeterId != null) {
1053 tBuilder.meter(upstreamMeterId);
1054 }
1055
1056 TrafficTreatment upstreamTreatment = tBuilder
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001057 .setOutput(uplinkPort)
Gamze Abakaf59c0912019-04-19 08:24:28 +00001058 .writeMetadata(createMetadata(subInfo.sTag(), subInfo.technologyProfileId(), uplinkPort), 0)
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001059 .build();
1060
1061 return DefaultForwardingObjective.builder()
1062 .withFlag(ForwardingObjective.Flag.VERSATILE)
1063 .withPriority(1000)
1064 .makePermanent()
1065 .withSelector(upstream)
1066 .fromApp(appId)
1067 .withTreatment(upstreamTreatment);
1068 }
1069
1070 private void unprovisionTransparentFlows(DeviceId deviceId, PortNumber uplink,
1071 PortNumber subscriberPort, VlanId innerVlan,
1072 VlanId outerVlan) {
1073
1074 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
1075
Gamze Abakaf59c0912019-04-19 08:24:28 +00001076 SubscriberAndDeviceInformation subInfo = programmedSubs.get(cp);
1077 if (subInfo == null) {
1078 log.warn("Subscriber is not programmed before for the connectPoint {}", cp);
1079 return;
1080 }
1081
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001082 additionalVlans.remove(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
1083
1084 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
1085 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
1086
Gamze Abakaf59c0912019-04-19 08:24:28 +00001087 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subInfo.upstreamBandwidthProfile());
1088 MeterId downstreamMeterId = getMeterIdFromBpMapping(deviceId, subInfo.downstreamBandwidthProfile());
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001089
Gamze Abakaf59c0912019-04-19 08:24:28 +00001090 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplink, subscriberPort,
1091 innerVlan, outerVlan, upstreamMeterId, subInfo);
1092 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplink, subscriberPort,
1093 innerVlan, outerVlan, downstreamMeterId, subInfo);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001094
1095 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
1096 @Override
1097 public void onSuccess(Objective objective) {
1098 upFuture.complete(null);
1099 }
1100
1101 @Override
1102 public void onError(Objective objective, ObjectiveError error) {
1103 upFuture.complete(error);
1104 }
1105 }));
1106
1107 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
1108 @Override
1109 public void onSuccess(Objective objective) {
1110 downFuture.complete(null);
1111 }
1112
1113 @Override
1114 public void onError(Objective objective, ObjectiveError error) {
1115 downFuture.complete(error);
1116 }
1117 }));
1118
1119 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
1120 if (downStatus != null) {
1121 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001122 "on port {} failed downstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001123 innerVlan, outerVlan, deviceId, subscriberPort, downStatus);
1124 } else if (upStatus != null) {
1125 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001126 "on port {} failed upstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001127 innerVlan, outerVlan, deviceId, subscriberPort, upStatus);
1128 }
1129 }, oltInstallers);
1130
Gamze Abakaf59c0912019-04-19 08:24:28 +00001131 //re-install eapol
1132 processEapolFilteringObjectives(deviceId, subscriberPort,
1133 subInfo.upstreamBandwidthProfile(), null, subInfo.cTag(), false);
1134 processEapolFilteringObjectives(deviceId, subscriberPort, defaultBpId,
1135 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
1136
1137 programmedMeters.remove(MeterKey.key(deviceId, upstreamMeterId));
1138 programmedMeters.remove(MeterKey.key(deviceId, downstreamMeterId));
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001139 }
1140
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001141 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
1142 Port port = deviceService.getPort(devId, portNumber);
1143 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
1144 if (info != null && info.technologyProfileId() != -1) {
1145 return info.technologyProfileId();
1146 }
1147 return defaultTechProfileId;
1148 }
1149
Gamze Abaka838d8142019-02-21 07:06:55 +00001150 /**
Gamze Abakada282b42019-03-11 13:16:48 +00001151 * Returns the write metadata value including tech profile reference and innerVlan.
1152 * For param cVlan, null can be sent
Gamze Abaka838d8142019-02-21 07:06:55 +00001153 *
Gamze Abakada282b42019-03-11 13:16:48 +00001154 * @param cVlan c (customer) tag of one subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +00001155 * @param techProfileId tech profile id of one subscriber
Gamze Abakada282b42019-03-11 13:16:48 +00001156 * @return the write metadata value including tech profile reference and innerVlan
Gamze Abaka838d8142019-02-21 07:06:55 +00001157 */
Gamze Abakada282b42019-03-11 13:16:48 +00001158 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
1159 if (cVlan == null) {
1160 return (long) techProfileId << 32;
1161 }
1162 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
Gamze Abaka838d8142019-02-21 07:06:55 +00001163 }
1164
1165 /**
1166 * Trap eapol authentication packets to the controller.
1167 *
Gamze Abaka33feef52019-02-27 08:16:47 +00001168 * @param devId the device identifier
1169 * @param portNumber the port for which this trap flow is designated
1170 * @param bpId bandwidth profile id to add the related meter to the flow
1171 * @param filterFuture completable future for this filtering objective operation
Gamze Abakada282b42019-03-11 13:16:48 +00001172 * @param vlanId the default or customer tag for a subscriber
Gamze Abaka33feef52019-02-27 08:16:47 +00001173 * @param install true to install the flow, false to remove the flow
Gamze Abaka838d8142019-02-21 07:06:55 +00001174 */
Gamze Abaka33feef52019-02-27 08:16:47 +00001175 private void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
1176 CompletableFuture<ObjectiveError> filterFuture,
Gamze Abakada282b42019-03-11 13:16:48 +00001177 VlanId vlanId, boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001178
1179 if (!enableEapol) {
1180 log.debug("Eapol filtering is disabled.");
1181 if (filterFuture != null) {
1182 filterFuture.complete(null);
1183 }
1184 return;
1185 }
1186
alshabib09753b52016-03-04 14:55:19 -08001187 if (!mastershipService.isLocalMaster(devId)) {
1188 return;
1189 }
alshabibbb83aa22016-02-10 15:08:23 -08001190 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abakaad329652018-12-20 10:12:21 +00001191 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gamze Abaka33feef52019-02-27 08:16:47 +00001192 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Gamze Abaka838d8142019-02-21 07:06:55 +00001193 MeterId meterId;
alshabibbb83aa22016-02-10 15:08:23 -08001194
Gamze Abaka838d8142019-02-21 07:06:55 +00001195 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
1196 if (bpInfo != null) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001197 meterId = createMeter(devId, bpInfo, meterFuture);
Gamze Abakaad329652018-12-20 10:12:21 +00001198 treatmentBuilder.meter(meterId);
1199 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001200 log.warn("Bandwidth profile {} is not found. Authentication flow will not be installed", bpId);
1201 return;
Gamze Abakaad329652018-12-20 10:12:21 +00001202 }
1203
Gamze Abaka33feef52019-02-27 08:16:47 +00001204 meterFuture.thenAcceptAsync(result -> {
1205 if (result == null) {
1206 log.info("Meter {} for the device {} is installed. " +
1207 "{} EAPOL trap flow", meterId, devId, install ? "Installing " : "Removing ");
1208 int techProfileId = getDefaultTechProfileId(devId, portNumber);
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001209
Gamze Abaka33feef52019-02-27 08:16:47 +00001210 //Authentication trap flow uses only tech profile id as write metadata value
1211 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
1212 .withKey(Criteria.matchInPort(portNumber))
1213 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
Gamze Abakada282b42019-03-11 13:16:48 +00001214 .addCondition(Criteria.matchVlanId(vlanId))
Gamze Abaka33feef52019-02-27 08:16:47 +00001215 .withMeta(treatmentBuilder
Gamze Abakada282b42019-03-11 13:16:48 +00001216 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
Gamze Abaka33feef52019-02-27 08:16:47 +00001217 .setOutput(PortNumber.CONTROLLER).build())
1218 .fromApp(appId)
1219 .withPriority(10000)
1220 .add(new ObjectiveContext() {
1221 @Override
1222 public void onSuccess(Objective objective) {
1223 log.info("Eapol filter for {} on {} {} with meter {}.",
1224 devId, portNumber, (install) ? INSTALLED : REMOVED, meterId);
1225 if (filterFuture != null) {
1226 filterFuture.complete(null);
1227 }
1228 }
alshabibdec2e252016-01-15 12:20:25 -08001229
Gamze Abaka33feef52019-02-27 08:16:47 +00001230 @Override
1231 public void onError(Objective objective, ObjectiveError error) {
1232 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
1233 devId, portNumber, meterId, (install) ? INSTALLATION : REMOVAL,
1234 error);
1235 if (filterFuture != null) {
1236 filterFuture.complete(error);
1237 }
1238 }
1239 });
alshabibdec2e252016-01-15 12:20:25 -08001240
Gamze Abaka33feef52019-02-27 08:16:47 +00001241 flowObjectiveService.filter(devId, eapol);
1242 } else {
1243 log.warn("Meter installation error while sending eapol trap flow. " +
1244 "Result {} and MeterId {}", result, meterId);
1245 }
1246 });
alshabibdec2e252016-01-15 12:20:25 -08001247 }
1248
Jonathan Hart403372d2018-08-22 11:44:13 -07001249 /**
1250 * Installs trap filtering objectives for particular traffic types on an
1251 * NNI port.
1252 *
Gamze Abakaad329652018-12-20 10:12:21 +00001253 * @param devId device ID
1254 * @param port port number
Jonathan Hart403372d2018-08-22 11:44:13 -07001255 * @param install true to install, false to remove
1256 */
1257 private void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
1258 processLldpFilteringObjective(devId, port, install);
Gamze Abaka33feef52019-02-27 08:16:47 +00001259 processDhcpFilteringObjectives(devId, port, null, -1, install, false);
Jonathan Hart403372d2018-08-22 11:44:13 -07001260 }
1261
1262 private void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
1263 if (!mastershipService.isLocalMaster(devId)) {
1264 return;
1265 }
1266 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1267
1268 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
1269 .withKey(Criteria.matchInPort(port))
1270 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
1271 .withMeta(DefaultTrafficTreatment.builder()
1272 .setOutput(PortNumber.CONTROLLER).build())
1273 .fromApp(appId)
1274 .withPriority(10000)
1275 .add(new ObjectiveContext() {
1276 @Override
1277 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001278 log.info("LLDP filter for device {} on port {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001279 devId, port, (install) ? INSTALLED : REMOVED);
Jonathan Hart403372d2018-08-22 11:44:13 -07001280 }
1281
1282 @Override
1283 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001284 log.info("LLDP filter for device {} on port {} failed {} because {}",
Gamze Abaka838d8142019-02-21 07:06:55 +00001285 devId, port, (install) ? INSTALLATION : REMOVAL,
Saurav Das82b8e6d2018-10-04 15:25:12 -07001286 error);
Jonathan Hart403372d2018-08-22 11:44:13 -07001287 }
1288 });
1289
1290 flowObjectiveService.filter(devId, lldp);
1291
1292 }
1293
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001294 /**
1295 * Trap dhcp packets to the controller.
1296 *
Gamze Abaka838d8142019-02-21 07:06:55 +00001297 * @param devId the device identifier
1298 * @param port the port for which this trap flow is designated
1299 * @param upstreamMeterId the upstream meter id that includes the upstream
1300 * bandwidth profile values such as PIR,CIR. If no meter id needs to be referenced,
1301 * null can be sent
1302 * @param techProfileId the technology profile id that is used to create write
1303 * metadata instruction value. If no tech profile id needs to be referenced,
1304 * -1 can be sent
1305 * @param install true to install the flow, false to remove the flow
1306 * @param upstream true if trapped packets are flowing upstream towards
1307 * server, false if packets are flowing downstream towards client
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001308 */
1309 private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port,
Gamze Abaka838d8142019-02-21 07:06:55 +00001310 MeterId upstreamMeterId,
1311 int techProfileId,
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001312 boolean install,
1313 boolean upstream) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001314
1315 if (!enableDhcpOnProvisioning) {
1316 log.debug("Dhcp provisioning is disabled.");
1317 return;
1318 }
1319
Amit Ghosh95e2f652017-08-23 12:49:46 +01001320 if (!mastershipService.isLocalMaster(devId)) {
1321 return;
1322 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001323
Matteo Scandolo63460d12018-11-02 16:19:04 -07001324 if (enableDhcpV4) {
1325 int udpSrc = (upstream) ? 68 : 67;
1326 int udpDst = (upstream) ? 67 : 68;
1327
1328 EthType ethType = EthType.EtherType.IPV4.ethType();
1329 byte protocol = IPv4.PROTOCOL_UDP;
1330
Gamze Abaka838d8142019-02-21 07:06:55 +00001331 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1332 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001333 }
1334
1335 if (enableDhcpV6) {
1336 int udpSrc = (upstream) ? 547 : 546;
1337 int udpDst = (upstream) ? 546 : 547;
1338
1339 EthType ethType = EthType.EtherType.IPV6.ethType();
1340 byte protocol = IPv6.PROTOCOL_UDP;
1341
Gamze Abaka838d8142019-02-21 07:06:55 +00001342 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1343 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001344 }
1345
1346 }
1347
1348 private void addDhcpFilteringObjectives(DeviceId devId,
1349 PortNumber port,
1350 int udpSrc,
1351 int udpDst,
1352 EthType ethType,
Gamze Abaka838d8142019-02-21 07:06:55 +00001353 MeterId upstreamMeterId,
1354 int techProfileId,
Matteo Scandolo63460d12018-11-02 16:19:04 -07001355 byte protocol,
1356 boolean install) {
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001357
1358 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001359 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1360
1361 if (upstreamMeterId != null) {
1362 treatmentBuilder.meter(upstreamMeterId);
1363 }
1364
1365 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001366 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001367 }
1368
Amit Ghosh95e2f652017-08-23 12:49:46 +01001369 FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny())
1370 .withKey(Criteria.matchInPort(port))
Matteo Scandolo63460d12018-11-02 16:19:04 -07001371 .addCondition(Criteria.matchEthType(ethType))
1372 .addCondition(Criteria.matchIPProtocol(protocol))
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001373 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1374 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Gamze Abaka838d8142019-02-21 07:06:55 +00001375 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001376 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001377 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -04001378 .withPriority(10000)
Amit Ghosh95e2f652017-08-23 12:49:46 +01001379 .add(new ObjectiveContext() {
1380 @Override
1381 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001382 log.info("DHCP {} filter for device {} on port {} {}.",
Gamze Abakaad329652018-12-20 10:12:21 +00001383 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001384 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001385 }
1386
1387 @Override
1388 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001389 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
Gamze Abakaad329652018-12-20 10:12:21 +00001390 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001391 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abakaad329652018-12-20 10:12:21 +00001392 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001393 }
1394 });
1395
1396 flowObjectiveService.filter(devId, dhcpUpstream);
1397 }
1398
Gamze Abaka838d8142019-02-21 07:06:55 +00001399 private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
1400 MeterId upstreamMeterId,
1401 int techProfileId,
1402 boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001403
Gamze Abakaf59c0912019-04-19 08:24:28 +00001404 if (!enableIgmpOnProvisioning) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001405 log.debug("Igmp provisioning is disabled.");
1406 return;
1407 }
1408
Amit Ghosh95e2f652017-08-23 12:49:46 +01001409 if (!mastershipService.isLocalMaster(devId)) {
1410 return;
1411 }
1412
Gamze Abaka641fc072018-09-04 09:16:27 +00001413 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001414 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1415
1416 if (upstreamMeterId != null) {
1417 treatmentBuilder.meter(upstreamMeterId);
1418 }
1419
1420 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001421 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001422 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001423
1424 builder = install ? builder.permit() : builder.deny();
1425
1426 FilteringObjective igmp = builder
1427 .withKey(Criteria.matchInPort(port))
1428 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1429 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
Gamze Abaka838d8142019-02-21 07:06:55 +00001430 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001431 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001432 .fromApp(appId)
1433 .withPriority(10000)
1434 .add(new ObjectiveContext() {
1435 @Override
1436 public void onSuccess(Objective objective) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001437 log.info("Igmp filter for {} on {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001438 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001439 }
1440
1441 @Override
1442 public void onError(Objective objective, ObjectiveError error) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001443 log.info("Igmp filter for {} on {} failed {} because {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001444 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abaka641fc072018-09-04 09:16:27 +00001445 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001446 }
1447 });
1448
1449 flowObjectiveService.filter(devId, igmp);
1450 }
1451
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001452 /**
Jonathan Hart403372d2018-08-22 11:44:13 -07001453 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
1454 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001455 *
1456 * @param dev Device to look for
1457 */
Jonathan Hart403372d2018-08-22 11:44:13 -07001458 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001459 // we create only for the ones we are master of
1460 if (!mastershipService.isLocalMaster(dev.id())) {
Gamze Abaka641fc072018-09-04 09:16:27 +00001461 return;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001462 }
1463 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001464 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Jonathan Hart403372d2018-08-22 11:44:13 -07001465 log.debug("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001466
1467 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -07001468 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001469 for (Port p : deviceService.getPorts(dev.id())) {
1470 if (isUniPort(dev, p)) {
Gamze Abakada282b42019-03-11 13:16:48 +00001471 processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1472 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Jonathan Hart403372d2018-08-22 11:44:13 -07001473 } else {
1474 processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001475 }
1476 }
1477 }
1478 }
1479
Jonathan Hart403372d2018-08-22 11:44:13 -07001480
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001481 /**
1482 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001483 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001484 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1485 * this logic needs to be changed
1486 *
1487 * @param dev Device to look for
1488 * @return The uplink Port of the OLT
1489 */
1490 private Port getUplinkPort(Device dev) {
1491 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001492 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001493 log.debug("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001494 if (deviceInfo == null) {
1495 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
1496 + " info", dev.id());
1497 return null;
1498 }
1499 // Return the port that has been configured as the uplink port of this OLT in Sadis
Gamze Abakaad329652018-12-20 10:12:21 +00001500 for (Port p : deviceService.getPorts(dev.id())) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001501 if (p.number().toLong() == deviceInfo.uplinkPort()) {
1502 log.debug("getUplinkPort: Found port {}", p);
1503 return p;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001504 }
1505 }
1506
Gamze Abaka838d8142019-02-21 07:06:55 +00001507 log.debug("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001508 return null;
1509 }
1510
1511 /**
1512 * Return the subscriber on a port.
1513 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001514 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001515 * @return subscriber if found else null
1516 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001517 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1518 Port port = deviceService.getPort(cp);
1519 checkNotNull(port, "Invalid connect point");
1520 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001521 return subsService.get(portName);
1522 }
1523
Gamze Abakaad329652018-12-20 10:12:21 +00001524 /**
1525 * Write metadata instruction value (metadata) is 8 bytes.
Gamze Abaka838d8142019-02-21 07:06:55 +00001526 * <p>
Gamze Abakaad329652018-12-20 10:12:21 +00001527 * MS 2 bytes: C Tag
1528 * Next 2 bytes: Technology Profile Id
1529 * Next 4 bytes: Port number (uni or nni)
1530 */
1531
1532 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
1533
1534 if (techProfileId == -1) {
1535 techProfileId = DEFAULT_TP_ID;
1536 }
1537
1538 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
1539 }
1540
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001541 private boolean isUniPort(Device d, Port p) {
1542 Port ulPort = getUplinkPort(d);
1543 if (ulPort != null) {
1544 return (ulPort.number().toLong() != p.number().toLong());
1545 }
1546 return false;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001547 }
1548
Jonathan Hart4c538002018-08-23 10:11:54 -07001549 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1550 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001551 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001552 }
1553
Gamze Abaka33feef52019-02-27 08:16:47 +00001554 private MeterId getMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfile) {
1555
1556 if (bpInfoToMeter.get(bandwidthProfile) == null) {
1557 log.warn("Bandwidth Profile '{}' is not found in bandwidth profile map.", bandwidthProfile);
1558 return null;
1559 }
1560
1561 Optional<MeterKey> meterKeyForDevice = bpInfoToMeter.get(bandwidthProfile)
1562 .stream()
1563 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1564 .findFirst();
1565 return meterKeyForDevice.isPresent() ? meterKeyForDevice.get().meterId() : null;
1566 }
1567
1568 private void addMeterIdToBpMapping(DeviceId deviceId, MeterId meterId, String bandwidthProfile) {
1569
1570 if (bpInfoToMeter.get(bandwidthProfile) == null) {
1571 bpInfoToMeter.put(bandwidthProfile,
1572 new ArrayList<>(Arrays.asList(MeterKey.key(deviceId, meterId))));
1573 } else {
Gamze Abaka33feef52019-02-27 08:16:47 +00001574 List<MeterKey> meterKeyListForBp = bpInfoToMeter.get(bandwidthProfile);
1575 meterKeyListForBp.add(MeterKey.key(deviceId, meterId));
1576 }
1577 }
1578
1579 private void removeMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfileId) {
1580 List<MeterKey> meterKeysForBp = bpInfoToMeter.get(bandwidthProfileId);
1581 if (meterKeysForBp != null) {
1582 meterKeysForBp.stream()
1583 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1584 .findFirst().ifPresent(mk -> {
1585 meterKeysForBp.remove(mk);
1586 programmedMeters.remove(mk);
1587 });
1588 }
1589 }
1590
alshabibf0e7e702015-05-30 18:22:36 -07001591 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001592 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1593
alshabibf0e7e702015-05-30 18:22:36 -07001594 @Override
1595 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001596 eventExecutor.execute(() -> {
1597 DeviceId devId = event.subject().id();
1598 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001599 Port port = event.port();
Jonathan Hart4c538002018-08-23 10:11:54 -07001600
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001601 if (event.type() == DeviceEvent.Type.PORT_STATS_UPDATED) {
1602 return;
1603 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001604
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001605 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001606 // it's possible that we got an event for a previously
1607 // programmed OLT that is no longer available in SADIS
1608 // we let such events go through
1609 if (!programmedDevices.contains(devId)) {
1610 log.warn("No device info found for {}, this is either "
1611 + "not an OLT or not known to sadis", dev);
1612 return;
1613 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001614 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001615
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001616 log.debug("OLT got {} event for {}", event.type(), event.subject());
Jonathan Hart4c538002018-08-23 10:11:54 -07001617
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001618 switch (event.type()) {
1619 //TODO: Port handling and bookkeeping should be improved once
1620 // olt firmware handles correct behaviour.
1621 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001622 if (isUniPort(dev, port)) {
1623 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Jonathan Hart4c538002018-08-23 10:11:54 -07001624
Gamze Abaka838d8142019-02-21 07:06:55 +00001625 if (port.isEnabled()) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001626 processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Gamze Abakada282b42019-03-11 13:16:48 +00001627 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001628 }
1629 } else {
1630 checkAndCreateDeviceFlows(dev);
1631 }
1632 break;
1633 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001634 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001635 removeSubscriber(new ConnectPoint(devId, port.number()));
Gamze Abaka838d8142019-02-21 07:06:55 +00001636 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001637 }
1638
1639 break;
1640 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001641 if (!isUniPort(dev, port)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001642 break;
1643 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001644
Gamze Abakada282b42019-03-11 13:16:48 +00001645 SubscriberAndDeviceInformation sub = programmedSubs
1646 .get(new ConnectPoint(devId, port.number()));
1647 VlanId vlanId = sub == null ? VlanId.vlanId(EAPOL_DEFAULT_VLAN) : sub.cTag();
1648
1649 String bpId = getCurrentBandwidthProfile(new ConnectPoint(devId, port.number()));
1650
Gamze Abaka838d8142019-02-21 07:06:55 +00001651 if (port.isEnabled()) {
Gamze Abakada282b42019-03-11 13:16:48 +00001652 processEapolFilteringObjectives(devId, port.number(), bpId,
1653 null, vlanId, true);
Gamze Abaka33feef52019-02-27 08:16:47 +00001654
Gamze Abaka838d8142019-02-21 07:06:55 +00001655 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001656 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001657 processEapolFilteringObjectives(devId, port.number(), bpId,
1658 null, vlanId, false);
Gamze Abaka838d8142019-02-21 07:06:55 +00001659 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001660 }
alshabibbb83aa22016-02-10 15:08:23 -08001661 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001662 case DEVICE_ADDED:
alshabib7c190012016-02-09 18:22:33 -08001663 post(new AccessDeviceEvent(
1664 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1665 null, null));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001666 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001667 // Send UNI_ADDED events for all existing ports
1668 deviceService.getPorts(devId).stream()
1669 .filter(p -> isUniPort(dev, p))
1670 .filter(Port::isEnabled)
1671 .forEach(p -> post(new AccessDeviceEvent(
1672 AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
1673
Jonathan Hart403372d2018-08-22 11:44:13 -07001674 checkAndCreateDeviceFlows(dev);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001675 break;
1676 case DEVICE_REMOVED:
1677 deviceService.getPorts(devId).stream()
1678 .filter(p -> isUniPort(dev, p))
1679 .forEach(p -> post(new AccessDeviceEvent(
1680 AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001681 programmedDevices.remove(devId);
Gamze Abakada282b42019-03-11 13:16:48 +00001682 removeAllSubscribers(devId);
alshabib7c190012016-02-09 18:22:33 -08001683 post(new AccessDeviceEvent(
1684 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1685 null, null));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001686 break;
1687 case DEVICE_AVAILABILITY_CHANGED:
1688 if (deviceService.isAvailable(devId)) {
1689 post(new AccessDeviceEvent(
1690 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1691 null, null));
Gamze Abakada282b42019-03-11 13:16:48 +00001692 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001693 checkAndCreateDeviceFlows(dev);
1694 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001695 programmedDevices.remove(devId);
1696 removeAllSubscribers(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001697 post(new AccessDeviceEvent(
1698 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1699 null, null));
1700 }
1701 break;
1702 case DEVICE_UPDATED:
1703 case DEVICE_SUSPENDED:
1704 case PORT_STATS_UPDATED:
1705 default:
1706 return;
1707 }
1708 });
alshabibf0e7e702015-05-30 18:22:36 -07001709 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001710
1711 private String getCurrentBandwidthProfile(ConnectPoint connectPoint) {
1712 SubscriberAndDeviceInformation sub = programmedSubs.get(connectPoint);
1713 if (sub != null) {
1714 return sub.upstreamBandwidthProfile();
1715 }
1716 return defaultBpId;
1717 }
Gamze Abakada282b42019-03-11 13:16:48 +00001718
1719 private void removeAllSubscribers(DeviceId deviceId) {
1720 List<ConnectPoint> connectPoints = programmedSubs.keySet().stream()
1721 .filter(ks -> Objects.equals(ks.deviceId(), deviceId))
1722 .collect(Collectors.toList());
1723
1724 connectPoints.forEach(cp -> programmedSubs.remove(cp));
1725 }
alshabibf0e7e702015-05-30 18:22:36 -07001726 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001727
1728 private class InternalMeterListener implements MeterListener {
1729
1730 @Override
1731 public void event(MeterEvent meterEvent) {
1732 if (deleteMeters && MeterEvent.Type.METER_REFERENCE_COUNT_ZERO.equals(meterEvent.type())) {
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001733 log.info("Zero Count Meter Event is received. Meter is {}", meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001734 Meter meter = meterEvent.subject();
Gamze Abaka33feef52019-02-27 08:16:47 +00001735 if (meter != null && appId.equals(meter.appId()) &&
1736 !programmedMeters.contains(MeterKey.key(meter.deviceId(), meter.id()))) {
Gamze Abaka641fc072018-09-04 09:16:27 +00001737 deleteMeter(meter.deviceId(), meter.id());
1738 }
1739 } else if (MeterEvent.Type.METER_REMOVED.equals(meterEvent.type())) {
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001740 log.info("Meter removed event is received. Meter is {}", meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001741 removeMeterFromBpMap(meterEvent.subject());
1742 }
1743 }
1744
1745 private void deleteMeter(DeviceId deviceId, MeterId meterId) {
1746 Meter meter = meterService.getMeter(deviceId, meterId);
Gamze Abaka838d8142019-02-21 07:06:55 +00001747 if (meter != null) {
1748 MeterRequest meterRequest = DefaultMeterRequest.builder()
1749 .withBands(meter.bands())
1750 .withUnit(meter.unit())
1751 .forDevice(deviceId)
1752 .fromApp(appId)
1753 .burst()
1754 .remove();
Gamze Abaka641fc072018-09-04 09:16:27 +00001755
Gamze Abaka838d8142019-02-21 07:06:55 +00001756 meterService.withdraw(meterRequest, meterId);
1757 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001758 }
1759
1760 private void removeMeterFromBpMap(Meter meter) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001761 bpInfoToMeter.values().forEach(meterKeys -> meterKeys.stream()
1762 .filter(meterKey -> (meterKey.deviceId().equals(meter.deviceId()))
1763 && meterKey.meterId().equals(meter.id())).findFirst().
1764 ifPresent(mk -> {
1765 meterKeys.remove(mk);
Gamze Abakada282b42019-03-11 13:16:48 +00001766 programmedMeters.remove(mk);
Gamze Abaka33feef52019-02-27 08:16:47 +00001767 log.info("Deleted from the internal map. MeterKey {}", mk);
1768 log.info("Programmed meters {}", programmedMeters);
1769 }));
Gamze Abaka641fc072018-09-04 09:16:27 +00001770 }
1771 }
1772}