blob: 36ef722c3c4a70d05daeecb99364a11f2d284721 [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;
Gamze Abaka33feef52019-02-27 08:16:47 +000026import java.util.ArrayList;
Saurav Das4c1a6a92019-06-19 13:26:15 -070027import java.util.Arrays;
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
alshabibf0e7e702015-05-30 18:22:36 -070042import org.apache.felix.scr.annotations.Activate;
43import org.apache.felix.scr.annotations.Component;
44import org.apache.felix.scr.annotations.Deactivate;
alshabibe0559672016-02-21 14:49:51 -080045import org.apache.felix.scr.annotations.Modified;
46import org.apache.felix.scr.annotations.Property;
alshabibf0e7e702015-05-30 18:22:36 -070047import org.apache.felix.scr.annotations.Reference;
48import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070049import org.apache.felix.scr.annotations.Service;
alshabibdec2e252016-01-15 12:20:25 -080050import org.onlab.packet.EthType;
Amit Ghosh95e2f652017-08-23 12:49:46 +010051import org.onlab.packet.IPv4;
Matteo Scandolo63460d12018-11-02 16:19:04 -070052import org.onlab.packet.IPv6;
Amit Ghosh95e2f652017-08-23 12:49:46 +010053import org.onlab.packet.TpPort;
alshabibf0e7e702015-05-30 18:22:36 -070054import org.onlab.packet.VlanId;
Amit Ghosh95e2f652017-08-23 12:49:46 +010055import org.onlab.util.Tools;
alshabibe0559672016-02-21 14:49:51 -080056import org.onosproject.cfg.ComponentConfigService;
alshabibf0e7e702015-05-30 18:22:36 -070057import org.onosproject.core.ApplicationId;
58import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080059import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080060import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010061import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070062import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010063import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070064import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080065import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070066import org.onosproject.net.PortNumber;
67import org.onosproject.net.device.DeviceEvent;
68import org.onosproject.net.device.DeviceListener;
69import org.onosproject.net.device.DeviceService;
70import org.onosproject.net.flow.DefaultTrafficSelector;
71import org.onosproject.net.flow.DefaultTrafficTreatment;
72import org.onosproject.net.flow.TrafficSelector;
73import org.onosproject.net.flow.TrafficTreatment;
alshabibdec2e252016-01-15 12:20:25 -080074import org.onosproject.net.flow.criteria.Criteria;
75import org.onosproject.net.flowobjective.DefaultFilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070076import org.onosproject.net.flowobjective.DefaultForwardingObjective;
alshabibdec2e252016-01-15 12:20:25 -080077import org.onosproject.net.flowobjective.FilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070078import org.onosproject.net.flowobjective.FlowObjectiveService;
79import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080080import org.onosproject.net.flowobjective.Objective;
81import org.onosproject.net.flowobjective.ObjectiveContext;
82import org.onosproject.net.flowobjective.ObjectiveError;
Gamze Abaka33feef52019-02-27 08:16:47 +000083import org.onosproject.net.meter.Band;
84import org.onosproject.net.meter.DefaultBand;
85import org.onosproject.net.meter.DefaultMeterRequest;
86import org.onosproject.net.meter.Meter;
87import org.onosproject.net.meter.MeterContext;
Saurav Das4c1a6a92019-06-19 13:26:15 -070088import org.onosproject.net.meter.MeterEvent;
Gamze Abaka33feef52019-02-27 08:16:47 +000089import org.onosproject.net.meter.MeterFailReason;
Saurav Das4c1a6a92019-06-19 13:26:15 -070090import org.onosproject.net.meter.MeterId;
Gamze Abaka33feef52019-02-27 08:16:47 +000091import org.onosproject.net.meter.MeterKey;
Gamze Abaka33feef52019-02-27 08:16:47 +000092import org.onosproject.net.meter.MeterListener;
93import org.onosproject.net.meter.MeterRequest;
Saurav Das4c1a6a92019-06-19 13:26:15 -070094import org.onosproject.net.meter.MeterService;
95import org.onosproject.store.serializers.KryoNamespaces;
96import org.onosproject.store.service.ConsistentMultimap;
97import org.onosproject.store.service.Serializer;
98import org.onosproject.store.service.StorageService;
alshabib36a4d732016-06-01 16:03:59 -070099import org.opencord.olt.AccessDeviceEvent;
100import org.opencord.olt.AccessDeviceListener;
101import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +0100102import org.opencord.olt.AccessSubscriberId;
Gamze Abaka641fc072018-09-04 09:16:27 +0000103import org.opencord.sadis.BandwidthProfileInformation;
104import org.opencord.sadis.BaseInformationService;
105import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100106import org.opencord.sadis.SubscriberAndDeviceInformation;
alshabibe0559672016-02-21 14:49:51 -0800107import org.osgi.service.component.ComponentContext;
alshabibf0e7e702015-05-30 18:22:36 -0700108import org.slf4j.Logger;
109
Saurav Das82b8e6d2018-10-04 15:25:12 -0700110import com.google.common.collect.ImmutableMap;
Saurav Das4c1a6a92019-06-19 13:26:15 -0700111import com.google.common.collect.ImmutableSet;
Saurav Das82b8e6d2018-10-04 15:25:12 -0700112import 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) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700325 log.info("Call to provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000326 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
Saurav Das4c1a6a92019-06-19 13:26:15 -0700345 SubscriberAndDeviceInformation prgSub = programmedSubs.get(connectPoint);
346 if (prgSub != null) {
347 log.warn("Subscriber {} on connectionPoint {} was previously programmed .. "
348 + "taking no action. Note that updating a subscribers params "
349 + "(vlans, bw, tpid etc) requires removing the subscriber"
350 + "before re-provisioning the subscriber", prgSub.id(),
351 connectPoint);
352 return true;
353 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000354
Gamze Abaka838d8142019-02-21 07:06:55 +0000355 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000356 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Saurav Das4c1a6a92019-06-19 13:26:15 -0700357 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
Gamze Abakada282b42019-03-11 13:16:48 +0000358 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
359 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000360 removeMeterIdFromBpMapping(deviceId, defaultBpId);
Gamze Abaka838d8142019-02-21 07:06:55 +0000361
Gamze Abaka33feef52019-02-27 08:16:47 +0000362 //install subscriber flows
363 filterFuture.thenAcceptAsync(filterStatus -> {
364 if (filterStatus == null) {
365 provisionSubscriberBasedFlows(connectPoint, uplinkPort.number(), Optional.empty(), sub);
366 }
367 });
Gamze Abaka838d8142019-02-21 07:06:55 +0000368
Saurav Das82b8e6d2018-10-04 15:25:12 -0700369 // cache subscriber info
Gamze Abaka838d8142019-02-21 07:06:55 +0000370 programmedSubs.put(connectPoint, sub);
Amit Ghosh31939522018-08-16 13:28:21 +0100371 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800372 }
373
374 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000375 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700376 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000377
Saurav Das4c1a6a92019-06-19 13:26:15 -0700378 // Get the subscriber connected to this port from the local cache
379 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000380 DeviceId deviceId = connectPoint.deviceId();
381 PortNumber subscriberPortNo = connectPoint.port();
382
383 SubscriberAndDeviceInformation subscriber = programmedSubs.get(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100384 if (subscriber == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000385 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
386 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800387 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800388 }
389
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100390 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000391 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100392 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000393 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100394 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800395 }
396
Gamze Abaka33feef52019-02-27 08:16:47 +0000397 //delete dhcp & igmp trap flows
398 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100399
Saurav Das4c1a6a92019-06-19 13:26:15 -0700400 // remove dhcp filters
Gamze Abaka33feef52019-02-27 08:16:47 +0000401 processDhcpFilteringObjectives(deviceId, subscriberPortNo,
402 upstreamMeterId, subscriber.technologyProfileId(), false, true);
Gamze Abaka838d8142019-02-21 07:06:55 +0000403
Saurav Das4c1a6a92019-06-19 13:26:15 -0700404 // remove igmp filters
Gamze Abaka33feef52019-02-27 08:16:47 +0000405 processIgmpFilteringObjectives(deviceId, subscriberPortNo,
406 upstreamMeterId, subscriber.technologyProfileId(), false);
alshabibbf23a1f2016-01-14 17:27:11 -0800407
Gamze Abaka33feef52019-02-27 08:16:47 +0000408 //unprovision vlans
409 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, subscriber, Optional.empty());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100410
411 // Remove if there are any flows for the additional Vlans
Gamze Abaka838d8142019-02-21 07:06:55 +0000412 Collection<? extends Map.Entry<VlanId, VlanId>> vlansList = additionalVlans.get(connectPoint).value();
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100413
414 // Remove the flows for the additional vlans for this subscriber
415 for (Map.Entry<VlanId, VlanId> vlans : vlansList) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000416 unprovisionTransparentFlows(deviceId, uplinkPort.number(), subscriberPortNo,
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100417 vlans.getValue(), vlans.getKey());
418
419 // Remove it from the map also
Gamze Abaka838d8142019-02-21 07:06:55 +0000420 additionalVlans.remove(connectPoint, vlans);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100421 }
422
Saurav Das4c1a6a92019-06-19 13:26:15 -0700423 // re-install eapol with default bandwidth profile
Gamze Abaka33feef52019-02-27 08:16:47 +0000424 processEapolFilteringObjectives(deviceId, subscriberPortNo,
Gamze Abakada282b42019-03-11 13:16:48 +0000425 subscriber.upstreamBandwidthProfile(), null, subscriber.cTag(), false);
Andy Bavier160e8682019-05-07 18:32:22 -0700426
427 Port port = deviceService.getPort(deviceId, subscriberPortNo);
428 if (port != null && port.isEnabled()) {
429 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
430 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Saurav Das4c1a6a92019-06-19 13:26:15 -0700431 } else {
432 log.debug("Port {} is no longer enabled or it's unavailable. Not "
433 + "reprogramming default eapol flow", connectPoint);
Andy Bavier160e8682019-05-07 18:32:22 -0700434 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000435
Gamze Abaka838d8142019-02-21 07:06:55 +0000436 programmedSubs.remove(connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100437 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800438 }
439
Amit Ghosh31939522018-08-16 13:28:21 +0100440 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100441 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Gamze Abakaf59c0912019-04-19 08:24:28 +0000442
443 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}", subscriberId, sTag, cTag);
444
Amit Ghosh31939522018-08-16 13:28:21 +0100445 // Check if we can find the connect point to which this subscriber is connected
446 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
447 if (subsPort == null) {
448 log.warn("ConnectPoint for {} not found", subscriberId);
449 return false;
450 }
451
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100452 if (!sTag.isPresent() && !cTag.isPresent()) {
453 return provisionSubscriber(subsPort);
454 } else if (sTag.isPresent() && cTag.isPresent()) {
455 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
456 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000457 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100458 return false;
459 }
460
Gamze Abakaf59c0912019-04-19 08:24:28 +0000461 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
462
463 //delete Eapol authentication flow with default bandwidth
464 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
465 processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId, filterFuture,
466 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
467 removeMeterIdFromBpMapping(subsPort.deviceId(), defaultBpId);
468
469 //install subscriber flows
470 filterFuture.thenAcceptAsync(filterStatus -> {
471 if (filterStatus == null) {
472 provisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
473 cTag.get(), sTag.get());
474 }
475 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100476 return true;
477 } else {
478 log.warn("Provisioning failed for subscriber: {}", subscriberId);
479 return false;
480 }
Amit Ghosh31939522018-08-16 13:28:21 +0100481 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100482
alshabibe0559672016-02-21 14:49:51 -0800483 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100484 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Amit Ghosh31939522018-08-16 13:28:21 +0100485 // Check if we can find the connect point to which this subscriber is connected
486 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
487 if (subsPort == null) {
488 log.warn("ConnectPoint for {} not found", subscriberId);
489 return false;
490 }
491
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100492 if (!sTag.isPresent() && !cTag.isPresent()) {
493 return removeSubscriber(subsPort);
494 } else if (sTag.isPresent() && cTag.isPresent()) {
495 // Get the uplink port
496 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
497 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000498 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100499 return false;
500 }
501
502 unprovisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
503 cTag.get(), sTag.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000504
505 programmedSubs.remove(subsPort);
506
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100507 return true;
508 } else {
509 log.warn("Removing subscriber failed for: {}", subscriberId);
510 return false;
511 }
Amit Ghosh31939522018-08-16 13:28:21 +0100512 }
513
514 @Override
515 public Collection<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> getSubscribers() {
516 ArrayList<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> subs = new ArrayList<>();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100517
Saurav Das82b8e6d2018-10-04 15:25:12 -0700518 // Get the subscribers for all the devices configured in sadis
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100519 // If the port is UNI, is enabled and exists in Sadis then copy it
520 for (Device d : deviceService.getDevices()) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700521 if (getOltInfo(d) == null) {
522 continue; // not an olt, or not configured in sadis
523 }
Gamze Abakaad329652018-12-20 10:12:21 +0000524 for (Port p : deviceService.getPorts(d.id())) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100525 if (isUniPort(d, p) && p.isEnabled()) {
526 ConnectPoint cp = new ConnectPoint(d.id(), p.number());
527
528 SubscriberAndDeviceInformation sub = getSubscriber(cp);
529 if (sub != null) {
Amit Ghosh31939522018-08-16 13:28:21 +0100530 Map.Entry<VlanId, VlanId> vlans = new AbstractMap.SimpleEntry(sub.sTag(), sub.cTag());
531 subs.add(new AbstractMap.SimpleEntry(cp, vlans));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100532 }
533 }
534 }
535 }
536
537 return subs;
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800538 }
539
540 @Override
Saurav Das82b8e6d2018-10-04 15:25:12 -0700541 public ImmutableMap<ConnectPoint, SubscriberAndDeviceInformation> getProgSubs() {
542 return ImmutableMap.copyOf(programmedSubs);
543 }
544
545 @Override
Gamze Abaka33feef52019-02-27 08:16:47 +0000546 public ImmutableSet<MeterKey> getProgMeters() {
547 return ImmutableSet.copyOf(programmedMeters);
548 }
549
550 @Override
551 public ImmutableMap<String, List<MeterKey>> getBpMeterMappings() {
552 return ImmutableMap.copyOf(bpInfoToMeter);
553 }
554
555 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100556 public List<DeviceId> fetchOlts() {
557 // look through all the devices and find the ones that are OLTs as per Sadis
558 List<DeviceId> olts = new ArrayList<>();
559 Iterable<Device> devices = deviceService.getDevices();
560 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700561 if (getOltInfo(d) != null) {
562 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100563 olts.add(d.id());
564 }
565 }
566 return olts;
alshabibe0559672016-02-21 14:49:51 -0800567 }
568
Amit Ghosh31939522018-08-16 13:28:21 +0100569 /**
570 * Finds the connect point to which a subscriber is connected.
571 *
572 * @param id The id of the subscriber, this is the same ID as in Sadis
573 * @return Subscribers ConnectPoint if found else null
574 */
575 private ConnectPoint findSubscriberConnectPoint(String id) {
576
577 Iterable<Device> devices = deviceService.getDevices();
578 for (Device d : devices) {
579 for (Port p : deviceService.getPorts(d.id())) {
580 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
581 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
582 log.debug("Found on device {} port {}", d.id(), p.number());
583 return new ConnectPoint(d.id(), p.number());
584 }
585 }
586 }
587 return null;
588 }
589
Gamze Abaka641fc072018-09-04 09:16:27 +0000590 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
591 if (bandwidthProfile == null) {
592 return null;
593 }
594 return bpService.get(bandwidthProfile);
595 }
596
Gamze Abaka838d8142019-02-21 07:06:55 +0000597 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000598 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000599 *
600 * @param deviceId the device identifier
601 * @param uplink uplink port of the OLT
602 * @param subscriberPort uni port
603 * @param subscriber subscriber info that includes s, c tags, tech profile and bandwidth profile references
604 * @param defaultVlan default vlan of the subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +0000605 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000606 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
607 PortNumber subscriberPort, SubscriberAndDeviceInformation subscriber,
608 Optional<VlanId> defaultVlan) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700609 log.info("Unprovisioning vlans for subscriber {} on dev/port: {}/{}",
610 subscriber, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800611
612 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
613 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
614
Gamze Abaka641fc072018-09-04 09:16:27 +0000615 VlanId deviceVlan = subscriber.sTag();
616 VlanId subscriberVlan = subscriber.cTag();
617
Gamze Abaka33feef52019-02-27 08:16:47 +0000618 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
619 MeterId downstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.downstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000620
alshabib4ceaed32016-03-03 18:00:58 -0800621 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000622 subscriberVlan, deviceVlan,
623 defaultVlan, upstreamMeterId, subscriber.technologyProfileId());
alshabib4ceaed32016-03-03 18:00:58 -0800624 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000625 subscriberVlan, deviceVlan,
626 defaultVlan, downstreamMeterId, subscriber.technologyProfileId());
alshabibbf23a1f2016-01-14 17:27:11 -0800627
alshabib4ceaed32016-03-03 18:00:58 -0800628 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
629 @Override
630 public void onSuccess(Objective objective) {
631 upFuture.complete(null);
632 }
alshabibbf23a1f2016-01-14 17:27:11 -0800633
alshabib4ceaed32016-03-03 18:00:58 -0800634 @Override
635 public void onError(Objective objective, ObjectiveError error) {
636 upFuture.complete(error);
637 }
638 }));
639
640 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
641 @Override
642 public void onSuccess(Objective objective) {
643 downFuture.complete(null);
644 }
645
646 @Override
647 public void onError(Objective objective, ObjectiveError error) {
648 downFuture.complete(error);
649 }
650 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800651
652 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
653 if (upStatus == null && downStatus == null) {
654 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000655 deviceId,
656 deviceVlan,
657 subscriberVlan));
alshabibbf23a1f2016-01-14 17:27:11 -0800658 } else if (downStatus != null) {
659 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000660 "on port {} failed downstream uninstallation: {}",
661 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800662 } else if (upStatus != null) {
663 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000664 "on port {} failed upstream uninstallation: {}",
665 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800666 }
667 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800668
Gamze Abaka33feef52019-02-27 08:16:47 +0000669 programmedMeters.remove(MeterKey.key(deviceId, upstreamMeterId));
670 programmedMeters.remove(MeterKey.key(deviceId, downstreamMeterId));
Gamze Abaka838d8142019-02-21 07:06:55 +0000671 log.debug("programmed Meters size {}", programmedMeters.size());
Jonathan Harte533a422015-10-20 17:31:24 -0700672 }
673
Gamze Abaka838d8142019-02-21 07:06:55 +0000674 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000675 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000676 *
677 * @param port the connection point of the subscriber
678 * @param uplinkPort uplink port of the OLT
679 * @param defaultVlan default vlan of the subscriber
680 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000681 */
Saurav Das4c1a6a92019-06-19 13:26:15 -0700682 private void provisionSubscriberBasedFlows(ConnectPoint port, PortNumber uplinkPort,
683 Optional<VlanId> defaultVlan,
Gamze Abaka33feef52019-02-27 08:16:47 +0000684 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000685
Saurav Das4c1a6a92019-06-19 13:26:15 -0700686 log.info("Provisioning vlans for subscriber {} on dev/port: {}",
687 sub, port);
Gamze Abaka641fc072018-09-04 09:16:27 +0000688
689 DeviceId deviceId = port.deviceId();
690 PortNumber subscriberPort = port.port();
691 VlanId deviceVlan = sub.sTag();
692 VlanId subscriberVlan = sub.cTag();
693 int techProfId = sub.technologyProfileId();
694
695 BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(sub.upstreamBandwidthProfile());
696 BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(sub.downstreamBandwidthProfile());
697
alshabib3ea82642016-01-12 18:06:53 -0800698 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
699 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
Gamze Abaka33feef52019-02-27 08:16:47 +0000700 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
701 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
alshabib3ea82642016-01-12 18:06:53 -0800702
Gamze Abaka33feef52019-02-27 08:16:47 +0000703 MeterId upstreamMeterId = createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
704 MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Jonathan Harte533a422015-10-20 17:31:24 -0700705
Gamze Abaka33feef52019-02-27 08:16:47 +0000706 //install upstream flows
707 upstreamMeterFuture.thenAcceptAsync(result -> {
708 if (result == null) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700709 log.info("Upstream Meter {} is in the device {}. " +
Gamze Abaka33feef52019-02-27 08:16:47 +0000710 "Sending subscriber flows.", upstreamMeterId, deviceId);
711 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
712 subscriberVlan, deviceVlan,
713 defaultVlan, upstreamMeterId, techProfId);
alshabib3ea82642016-01-12 18:06:53 -0800714
Gamze Abaka33feef52019-02-27 08:16:47 +0000715
716 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
717 @Override
718 public void onSuccess(Objective objective) {
719 upFuture.complete(null);
720 }
721
722 @Override
723 public void onError(Objective objective, ObjectiveError error) {
724 upFuture.complete(error);
725 }
726 }));
727 } else {
728 log.warn("Meter installation error while sending upstream flows. " +
729 "Result {} and MeterId {}", result, upstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800730 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000731 });
alshabibbf23a1f2016-01-14 17:27:11 -0800732
Gamze Abaka33feef52019-02-27 08:16:47 +0000733 //install downstream flows
734 downsteamMeterFuture.thenAcceptAsync(result -> {
735 if (result == null) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700736 log.info("Downstream Meter {} is in the device {}. " +
Gamze Abaka33feef52019-02-27 08:16:47 +0000737 "Sending subscriber flows.", downstreamMeterId, deviceId);
738 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
739 subscriberVlan, deviceVlan,
740 defaultVlan, downstreamMeterId, techProfId);
741
742 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
743 @Override
744 public void onSuccess(Objective objective) {
745 downFuture.complete(null);
746 }
747
748 @Override
749 public void onError(Objective objective, ObjectiveError error) {
750 downFuture.complete(error);
751 }
752 }));
753 } else {
754 log.warn("Meter installation error while sending downstream flows. " +
755 "Result {} and MeterId {}", result, downstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800756 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000757 });
alshabibbf23a1f2016-01-14 17:27:11 -0800758
Gamze Abaka33feef52019-02-27 08:16:47 +0000759 //send eapol & dhcp & igmp flows
760 //send Subscriber Registered event
alshabib3ea82642016-01-12 18:06:53 -0800761 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
762 if (upStatus == null && downStatus == null) {
Gamze Abaka33feef52019-02-27 08:16:47 +0000763
764 if (upstreamMeterId != null) {
765 //re-install Eapol authentication flow with the subscribers' upstream bandwidth profile
766 processEapolFilteringObjectives(deviceId, subscriberPort, sub.upstreamBandwidthProfile(),
Gamze Abakada282b42019-03-11 13:16:48 +0000767 null, sub.cTag(), true);
Gamze Abaka33feef52019-02-27 08:16:47 +0000768
769 processDhcpFilteringObjectives(deviceId, subscriberPort,
770 upstreamMeterId, sub.technologyProfileId(), true, true);
771
772 processIgmpFilteringObjectives(deviceId, subscriberPort,
773 upstreamMeterId, sub.technologyProfileId(), true);
774 }
775
alshabib3ea82642016-01-12 18:06:53 -0800776 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000777 deviceId,
778 deviceVlan,
779 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800780
alshabib3ea82642016-01-12 18:06:53 -0800781 } else if (downStatus != null) {
782 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000783 "on port {} failed downstream installation: {}",
784 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabib3ea82642016-01-12 18:06:53 -0800785 } else if (upStatus != null) {
786 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000787 "on port {} failed upstream installation: {}",
788 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabib3ea82642016-01-12 18:06:53 -0800789 }
790 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700791 }
792
Gamze Abaka33feef52019-02-27 08:16:47 +0000793 private MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo,
794 CompletableFuture<Object> meterFuture) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000795 if (bpInfo == null) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700796 log.warn("Bandwidth profile information cannot be null when creating meter");
Gamze Abaka641fc072018-09-04 09:16:27 +0000797 return null;
798 }
799
Gamze Abaka33feef52019-02-27 08:16:47 +0000800 MeterId meterId = getMeterIdFromBpMapping(deviceId, bpInfo.id());
Gamze Abaka641fc072018-09-04 09:16:27 +0000801 if (meterId != null) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700802 log.debug("Meter {} was previously created for bp {}", meterId,
803 bpInfo.id());
Gamze Abaka33feef52019-02-27 08:16:47 +0000804 meterFuture.complete(null);
Gamze Abaka641fc072018-09-04 09:16:27 +0000805 return meterId;
806 }
807
808 List<Band> meterBands = createMeterBands(bpInfo);
809
810 MeterRequest meterRequest = DefaultMeterRequest.builder()
811 .withBands(meterBands)
812 .withUnit(Meter.Unit.KB_PER_SEC)
Gamze Abaka33feef52019-02-27 08:16:47 +0000813 .withContext(new MeterContext() {
814 @Override
815 public void onSuccess(MeterRequest op) {
Saurav Das4c1a6a92019-06-19 13:26:15 -0700816 log.debug("meter addition confirmed for bpInfo:{}", bpInfo);
Gamze Abaka33feef52019-02-27 08:16:47 +0000817 meterFuture.complete(null);
818 }
819
820 @Override
821 public void onError(MeterRequest op, MeterFailReason reason) {
822 meterFuture.complete(reason);
823 }
824 })
Gamze Abaka641fc072018-09-04 09:16:27 +0000825 .forDevice(deviceId)
826 .fromApp(appId)
827 .burst()
828 .add();
829
830 Meter meter = meterService.submit(meterRequest);
Gamze Abaka33feef52019-02-27 08:16:47 +0000831 addMeterIdToBpMapping(deviceId, meter.id(), bpInfo.id());
Saurav Das4c1a6a92019-06-19 13:26:15 -0700832 log.info("Meter creation message sent for Meter Id {}", meter.id());
Gamze Abaka33feef52019-02-27 08:16:47 +0000833 programmedMeters.add(MeterKey.key(deviceId, meter.id()));
Gamze Abaka641fc072018-09-04 09:16:27 +0000834 return meter.id();
835 }
836
837 private List<Band> createMeterBands(BandwidthProfileInformation bpInfo) {
838 List<Band> meterBands = new ArrayList<>();
839
840 meterBands.add(createMeterBand(bpInfo.committedInformationRate(), bpInfo.committedBurstSize()));
841 meterBands.add(createMeterBand(bpInfo.exceededInformationRate(), bpInfo.exceededBurstSize()));
Gamze Abakaad329652018-12-20 10:12:21 +0000842 meterBands.add(createMeterBand(bpInfo.assuredInformationRate(), 0L));
Gamze Abaka641fc072018-09-04 09:16:27 +0000843
Gamze Abaka641fc072018-09-04 09:16:27 +0000844 return meterBands;
845 }
846
847 private Band createMeterBand(long rate, Long burst) {
848 return DefaultBand.builder()
849 .withRate(rate) //already Kbps
850 .burstSize(burst) // already Kbits
851 .ofType(Band.Type.DROP) // no matter
852 .build();
853 }
854
alshabib4ceaed32016-03-03 18:00:58 -0800855 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
856 PortNumber subscriberPort,
857 VlanId subscriberVlan,
858 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000859 Optional<VlanId> defaultVlan,
860 MeterId meterId,
861 int techProfId) {
alshabib4ceaed32016-03-03 18:00:58 -0800862 TrafficSelector downstream = DefaultTrafficSelector.builder()
863 .matchVlanId(deviceVlan)
864 .matchInPort(uplinkPort)
865 .matchInnerVlanId(subscriberVlan)
866 .build();
867
Gamze Abaka641fc072018-09-04 09:16:27 +0000868 TrafficTreatment.Builder downstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800869 .popVlan()
870 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
Gamze Abaka641fc072018-09-04 09:16:27 +0000871 .setOutput(subscriberPort);
872
873 if (meterId != null) {
874 downstreamTreatmentBuilder.meter(meterId);
875 }
876
Gamze Abakaad329652018-12-20 10:12:21 +0000877 downstreamTreatmentBuilder.writeMetadata(createMetadata(subscriberVlan, techProfId, subscriberPort), 0);
alshabib4ceaed32016-03-03 18:00:58 -0800878
879 return DefaultForwardingObjective.builder()
880 .withFlag(ForwardingObjective.Flag.VERSATILE)
881 .withPriority(1000)
882 .makePermanent()
883 .withSelector(downstream)
884 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000885 .withTreatment(downstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800886 }
887
888 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
889 PortNumber subscriberPort,
890 VlanId subscriberVlan,
891 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000892 Optional<VlanId> defaultVlan,
893 MeterId meterId,
894 int technologyProfileId) {
895
896
897 VlanId dVlan = defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan));
898
899 if (subscriberVlan.toShort() == 4096) {
900 dVlan = subscriberVlan;
901 }
902
alshabib4ceaed32016-03-03 18:00:58 -0800903 TrafficSelector upstream = DefaultTrafficSelector.builder()
Gamze Abaka641fc072018-09-04 09:16:27 +0000904 .matchVlanId(dVlan)
alshabib4ceaed32016-03-03 18:00:58 -0800905 .matchInPort(subscriberPort)
906 .build();
907
908
Gamze Abaka641fc072018-09-04 09:16:27 +0000909 TrafficTreatment.Builder upstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800910 .pushVlan()
911 .setVlanId(subscriberVlan)
912 .pushVlan()
913 .setVlanId(deviceVlan)
Gamze Abaka641fc072018-09-04 09:16:27 +0000914 .setOutput(uplinkPort);
915
916 if (meterId != null) {
917 upstreamTreatmentBuilder.meter(meterId);
918 }
919
Gamze Abakaad329652018-12-20 10:12:21 +0000920 upstreamTreatmentBuilder.writeMetadata(createMetadata(deviceVlan, technologyProfileId, uplinkPort), 0L);
alshabib4ceaed32016-03-03 18:00:58 -0800921
922 return DefaultForwardingObjective.builder()
923 .withFlag(ForwardingObjective.Flag.VERSATILE)
924 .withPriority(1000)
925 .makePermanent()
926 .withSelector(upstream)
927 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000928 .withTreatment(upstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800929 }
Gamze Abakaad329652018-12-20 10:12:21 +0000930
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100931 private void provisionTransparentFlows(DeviceId deviceId, PortNumber uplinkPort,
932 PortNumber subscriberPort,
933 VlanId innerVlan,
934 VlanId outerVlan) {
935
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100936 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
937
Gamze Abakaf59c0912019-04-19 08:24:28 +0000938 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
939
940 BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(
941 subInfo.upstreamBandwidthProfile());
942 BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(
943 subInfo.downstreamBandwidthProfile());
944
945 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
946 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
947 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
948 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
949
950 MeterId upstreamMeterId = createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
951 MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
952
953 upstreamMeterFuture.thenAcceptAsync(result -> {
954 if (result == null) {
955 log.info("Upstream Meter {} is sent to the device {}. " +
956 "Sending subscriber flows.", upstreamMeterId, deviceId);
957
958 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplinkPort, subscriberPort,
959 innerVlan, outerVlan, upstreamMeterId, subInfo);
960
961 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
962 @Override
963 public void onSuccess(Objective objective) {
964 upFuture.complete(null);
965 }
966
967 @Override
968 public void onError(Objective objective, ObjectiveError error) {
969 upFuture.complete(error);
970 }
971 }));
972
973 } else {
974 log.warn("Meter installation error while sending upstream flows. " +
975 "Result {} and MeterId {}", result, upstreamMeterId);
976 }
977 });
978
979 downsteamMeterFuture.thenAcceptAsync(result -> {
980 if (result == null) {
981 log.info("Downstream Meter {} is sent to the device {}. " +
982 "Sending subscriber flows.", downstreamMeterId, deviceId);
983
984 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplinkPort, subscriberPort,
985 innerVlan, outerVlan, downstreamMeterId, subInfo);
986
987 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
988 @Override
989 public void onSuccess(Objective objective) {
990 downFuture.complete(null);
991 }
992
993 @Override
994 public void onError(Objective objective, ObjectiveError error) {
995 downFuture.complete(error);
996 }
997 }));
998 } else {
999 log.warn("Meter installation error while sending upstream flows. " +
1000 "Result {} and MeterId {}", result, downstreamMeterId);
1001 }
1002 });
1003
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001004 additionalVlans.put(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
1005
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001006 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
1007 if (downStatus != null) {
1008 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001009 "on port {} failed downstream installation: {}",
Gamze Abakaf59c0912019-04-19 08:24:28 +00001010 innerVlan, outerVlan, deviceId, cp, downStatus);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001011 } else if (upStatus != null) {
1012 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001013 "on port {} failed upstream installation: {}",
Gamze Abakaf59c0912019-04-19 08:24:28 +00001014 innerVlan, outerVlan, deviceId, cp, upStatus);
1015 } else {
1016 processEapolFilteringObjectives(deviceId, subscriberPort, subInfo.upstreamBandwidthProfile(),
1017 null, subInfo.cTag(), true);
1018
1019 // cache subscriber info
1020 programmedSubs.put(cp, subInfo);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001021 }
1022 }, oltInstallers);
1023
1024 }
1025
1026 private ForwardingObjective.Builder transparentDownBuilder(PortNumber uplinkPort,
1027 PortNumber subscriberPort,
1028 VlanId innerVlan,
Gamze Abakaf59c0912019-04-19 08:24:28 +00001029 VlanId outerVlan,
1030 MeterId downstreamMeterId,
1031 SubscriberAndDeviceInformation subInfo) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001032 TrafficSelector downstream = DefaultTrafficSelector.builder()
1033 .matchVlanId(outerVlan)
1034 .matchInPort(uplinkPort)
1035 .matchInnerVlanId(innerVlan)
1036 .build();
1037
Gamze Abakaf59c0912019-04-19 08:24:28 +00001038 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1039 if (downstreamMeterId != null) {
1040 tBuilder.meter(downstreamMeterId);
1041 }
1042
1043 TrafficTreatment downstreamTreatment = tBuilder
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001044 .setOutput(subscriberPort)
Gamze Abakaf59c0912019-04-19 08:24:28 +00001045 .writeMetadata(createMetadata(subInfo.cTag(), subInfo.technologyProfileId(), subscriberPort), 0)
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001046 .build();
1047
1048 return DefaultForwardingObjective.builder()
1049 .withFlag(ForwardingObjective.Flag.VERSATILE)
1050 .withPriority(1000)
1051 .makePermanent()
1052 .withSelector(downstream)
1053 .fromApp(appId)
1054 .withTreatment(downstreamTreatment);
1055 }
1056
1057 private ForwardingObjective.Builder transparentUpBuilder(PortNumber uplinkPort,
1058 PortNumber subscriberPort,
1059 VlanId innerVlan,
Gamze Abakaf59c0912019-04-19 08:24:28 +00001060 VlanId outerVlan,
1061 MeterId upstreamMeterId,
1062 SubscriberAndDeviceInformation subInfo) {
1063
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001064 TrafficSelector upstream = DefaultTrafficSelector.builder()
1065 .matchVlanId(outerVlan)
1066 .matchInPort(subscriberPort)
1067 .matchInnerVlanId(innerVlan)
1068 .build();
1069
Gamze Abakaf59c0912019-04-19 08:24:28 +00001070 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1071 if (upstreamMeterId != null) {
1072 tBuilder.meter(upstreamMeterId);
1073 }
1074
1075 TrafficTreatment upstreamTreatment = tBuilder
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001076 .setOutput(uplinkPort)
Gamze Abakaf59c0912019-04-19 08:24:28 +00001077 .writeMetadata(createMetadata(subInfo.sTag(), subInfo.technologyProfileId(), uplinkPort), 0)
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001078 .build();
1079
1080 return DefaultForwardingObjective.builder()
1081 .withFlag(ForwardingObjective.Flag.VERSATILE)
1082 .withPriority(1000)
1083 .makePermanent()
1084 .withSelector(upstream)
1085 .fromApp(appId)
1086 .withTreatment(upstreamTreatment);
1087 }
1088
1089 private void unprovisionTransparentFlows(DeviceId deviceId, PortNumber uplink,
1090 PortNumber subscriberPort, VlanId innerVlan,
1091 VlanId outerVlan) {
1092
1093 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
1094
Gamze Abakaf59c0912019-04-19 08:24:28 +00001095 SubscriberAndDeviceInformation subInfo = programmedSubs.get(cp);
1096 if (subInfo == null) {
1097 log.warn("Subscriber is not programmed before for the connectPoint {}", cp);
1098 return;
1099 }
1100
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001101 additionalVlans.remove(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
1102
1103 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
1104 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
1105
Gamze Abakaf59c0912019-04-19 08:24:28 +00001106 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subInfo.upstreamBandwidthProfile());
1107 MeterId downstreamMeterId = getMeterIdFromBpMapping(deviceId, subInfo.downstreamBandwidthProfile());
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001108
Gamze Abakaf59c0912019-04-19 08:24:28 +00001109 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplink, subscriberPort,
1110 innerVlan, outerVlan, upstreamMeterId, subInfo);
1111 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplink, subscriberPort,
1112 innerVlan, outerVlan, downstreamMeterId, subInfo);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001113
1114 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
1115 @Override
1116 public void onSuccess(Objective objective) {
1117 upFuture.complete(null);
1118 }
1119
1120 @Override
1121 public void onError(Objective objective, ObjectiveError error) {
1122 upFuture.complete(error);
1123 }
1124 }));
1125
1126 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
1127 @Override
1128 public void onSuccess(Objective objective) {
1129 downFuture.complete(null);
1130 }
1131
1132 @Override
1133 public void onError(Objective objective, ObjectiveError error) {
1134 downFuture.complete(error);
1135 }
1136 }));
1137
1138 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
1139 if (downStatus != null) {
1140 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001141 "on port {} failed downstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001142 innerVlan, outerVlan, deviceId, subscriberPort, downStatus);
1143 } else if (upStatus != null) {
1144 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001145 "on port {} failed upstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001146 innerVlan, outerVlan, deviceId, subscriberPort, upStatus);
1147 }
1148 }, oltInstallers);
1149
Gamze Abakaf59c0912019-04-19 08:24:28 +00001150 //re-install eapol
1151 processEapolFilteringObjectives(deviceId, subscriberPort,
1152 subInfo.upstreamBandwidthProfile(), null, subInfo.cTag(), false);
1153 processEapolFilteringObjectives(deviceId, subscriberPort, defaultBpId,
1154 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
1155
1156 programmedMeters.remove(MeterKey.key(deviceId, upstreamMeterId));
1157 programmedMeters.remove(MeterKey.key(deviceId, downstreamMeterId));
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001158 }
1159
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001160 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
1161 Port port = deviceService.getPort(devId, portNumber);
Andy Bavier160e8682019-05-07 18:32:22 -07001162 if (port != null) {
1163 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
1164 if (info != null && info.technologyProfileId() != -1) {
1165 return info.technologyProfileId();
1166 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001167 }
1168 return defaultTechProfileId;
1169 }
1170
Gamze Abaka838d8142019-02-21 07:06:55 +00001171 /**
Gamze Abakada282b42019-03-11 13:16:48 +00001172 * Returns the write metadata value including tech profile reference and innerVlan.
1173 * For param cVlan, null can be sent
Gamze Abaka838d8142019-02-21 07:06:55 +00001174 *
Gamze Abakada282b42019-03-11 13:16:48 +00001175 * @param cVlan c (customer) tag of one subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +00001176 * @param techProfileId tech profile id of one subscriber
Gamze Abakada282b42019-03-11 13:16:48 +00001177 * @return the write metadata value including tech profile reference and innerVlan
Gamze Abaka838d8142019-02-21 07:06:55 +00001178 */
Gamze Abakada282b42019-03-11 13:16:48 +00001179 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
1180 if (cVlan == null) {
1181 return (long) techProfileId << 32;
1182 }
1183 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
Gamze Abaka838d8142019-02-21 07:06:55 +00001184 }
1185
1186 /**
1187 * Trap eapol authentication packets to the controller.
1188 *
Gamze Abaka33feef52019-02-27 08:16:47 +00001189 * @param devId the device identifier
1190 * @param portNumber the port for which this trap flow is designated
1191 * @param bpId bandwidth profile id to add the related meter to the flow
1192 * @param filterFuture completable future for this filtering objective operation
Gamze Abakada282b42019-03-11 13:16:48 +00001193 * @param vlanId the default or customer tag for a subscriber
Gamze Abaka33feef52019-02-27 08:16:47 +00001194 * @param install true to install the flow, false to remove the flow
Gamze Abaka838d8142019-02-21 07:06:55 +00001195 */
Gamze Abaka33feef52019-02-27 08:16:47 +00001196 private void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
1197 CompletableFuture<ObjectiveError> filterFuture,
Gamze Abakada282b42019-03-11 13:16:48 +00001198 VlanId vlanId, boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001199
1200 if (!enableEapol) {
1201 log.debug("Eapol filtering is disabled.");
1202 if (filterFuture != null) {
1203 filterFuture.complete(null);
1204 }
1205 return;
1206 }
1207
alshabib09753b52016-03-04 14:55:19 -08001208 if (!mastershipService.isLocalMaster(devId)) {
1209 return;
1210 }
alshabibbb83aa22016-02-10 15:08:23 -08001211 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abakaad329652018-12-20 10:12:21 +00001212 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gamze Abaka33feef52019-02-27 08:16:47 +00001213 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Gamze Abaka838d8142019-02-21 07:06:55 +00001214 MeterId meterId;
alshabibbb83aa22016-02-10 15:08:23 -08001215
Gamze Abaka838d8142019-02-21 07:06:55 +00001216 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
1217 if (bpInfo != null) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001218 meterId = createMeter(devId, bpInfo, meterFuture);
Gamze Abakaad329652018-12-20 10:12:21 +00001219 treatmentBuilder.meter(meterId);
1220 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001221 log.warn("Bandwidth profile {} is not found. Authentication flow will not be installed", bpId);
1222 return;
Gamze Abakaad329652018-12-20 10:12:21 +00001223 }
1224
Gamze Abaka33feef52019-02-27 08:16:47 +00001225 meterFuture.thenAcceptAsync(result -> {
1226 if (result == null) {
1227 log.info("Meter {} for the device {} is installed. " +
1228 "{} EAPOL trap flow", meterId, devId, install ? "Installing " : "Removing ");
1229 int techProfileId = getDefaultTechProfileId(devId, portNumber);
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001230
Gamze Abaka33feef52019-02-27 08:16:47 +00001231 //Authentication trap flow uses only tech profile id as write metadata value
1232 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
1233 .withKey(Criteria.matchInPort(portNumber))
1234 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
Gamze Abakada282b42019-03-11 13:16:48 +00001235 .addCondition(Criteria.matchVlanId(vlanId))
Gamze Abaka33feef52019-02-27 08:16:47 +00001236 .withMeta(treatmentBuilder
Gamze Abakada282b42019-03-11 13:16:48 +00001237 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
Gamze Abaka33feef52019-02-27 08:16:47 +00001238 .setOutput(PortNumber.CONTROLLER).build())
1239 .fromApp(appId)
1240 .withPriority(10000)
1241 .add(new ObjectiveContext() {
1242 @Override
1243 public void onSuccess(Objective objective) {
1244 log.info("Eapol filter for {} on {} {} with meter {}.",
1245 devId, portNumber, (install) ? INSTALLED : REMOVED, meterId);
1246 if (filterFuture != null) {
1247 filterFuture.complete(null);
1248 }
1249 }
alshabibdec2e252016-01-15 12:20:25 -08001250
Gamze Abaka33feef52019-02-27 08:16:47 +00001251 @Override
1252 public void onError(Objective objective, ObjectiveError error) {
1253 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
1254 devId, portNumber, meterId, (install) ? INSTALLATION : REMOVAL,
1255 error);
1256 if (filterFuture != null) {
1257 filterFuture.complete(error);
1258 }
1259 }
1260 });
alshabibdec2e252016-01-15 12:20:25 -08001261
Gamze Abaka33feef52019-02-27 08:16:47 +00001262 flowObjectiveService.filter(devId, eapol);
1263 } else {
1264 log.warn("Meter installation error while sending eapol trap flow. " +
1265 "Result {} and MeterId {}", result, meterId);
1266 }
1267 });
alshabibdec2e252016-01-15 12:20:25 -08001268 }
1269
Jonathan Hart403372d2018-08-22 11:44:13 -07001270 /**
1271 * Installs trap filtering objectives for particular traffic types on an
1272 * NNI port.
1273 *
Gamze Abakaad329652018-12-20 10:12:21 +00001274 * @param devId device ID
1275 * @param port port number
Jonathan Hart403372d2018-08-22 11:44:13 -07001276 * @param install true to install, false to remove
1277 */
1278 private void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
1279 processLldpFilteringObjective(devId, port, install);
Gamze Abaka33feef52019-02-27 08:16:47 +00001280 processDhcpFilteringObjectives(devId, port, null, -1, install, false);
Jonathan Hart403372d2018-08-22 11:44:13 -07001281 }
1282
1283 private void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
1284 if (!mastershipService.isLocalMaster(devId)) {
1285 return;
1286 }
1287 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1288
1289 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
1290 .withKey(Criteria.matchInPort(port))
1291 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
1292 .withMeta(DefaultTrafficTreatment.builder()
1293 .setOutput(PortNumber.CONTROLLER).build())
1294 .fromApp(appId)
1295 .withPriority(10000)
1296 .add(new ObjectiveContext() {
1297 @Override
1298 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001299 log.info("LLDP filter for device {} on port {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001300 devId, port, (install) ? INSTALLED : REMOVED);
Jonathan Hart403372d2018-08-22 11:44:13 -07001301 }
1302
1303 @Override
1304 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001305 log.info("LLDP filter for device {} on port {} failed {} because {}",
Gamze Abaka838d8142019-02-21 07:06:55 +00001306 devId, port, (install) ? INSTALLATION : REMOVAL,
Saurav Das82b8e6d2018-10-04 15:25:12 -07001307 error);
Jonathan Hart403372d2018-08-22 11:44:13 -07001308 }
1309 });
1310
1311 flowObjectiveService.filter(devId, lldp);
1312
1313 }
1314
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001315 /**
1316 * Trap dhcp packets to the controller.
1317 *
Gamze Abaka838d8142019-02-21 07:06:55 +00001318 * @param devId the device identifier
1319 * @param port the port for which this trap flow is designated
1320 * @param upstreamMeterId the upstream meter id that includes the upstream
1321 * bandwidth profile values such as PIR,CIR. If no meter id needs to be referenced,
1322 * null can be sent
1323 * @param techProfileId the technology profile id that is used to create write
1324 * metadata instruction value. If no tech profile id needs to be referenced,
1325 * -1 can be sent
1326 * @param install true to install the flow, false to remove the flow
1327 * @param upstream true if trapped packets are flowing upstream towards
1328 * server, false if packets are flowing downstream towards client
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001329 */
1330 private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port,
Gamze Abaka838d8142019-02-21 07:06:55 +00001331 MeterId upstreamMeterId,
1332 int techProfileId,
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001333 boolean install,
1334 boolean upstream) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001335
1336 if (!enableDhcpOnProvisioning) {
1337 log.debug("Dhcp provisioning is disabled.");
1338 return;
1339 }
1340
Amit Ghosh95e2f652017-08-23 12:49:46 +01001341 if (!mastershipService.isLocalMaster(devId)) {
1342 return;
1343 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001344
Matteo Scandolo63460d12018-11-02 16:19:04 -07001345 if (enableDhcpV4) {
1346 int udpSrc = (upstream) ? 68 : 67;
1347 int udpDst = (upstream) ? 67 : 68;
1348
1349 EthType ethType = EthType.EtherType.IPV4.ethType();
1350 byte protocol = IPv4.PROTOCOL_UDP;
1351
Gamze Abaka838d8142019-02-21 07:06:55 +00001352 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1353 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001354 }
1355
1356 if (enableDhcpV6) {
1357 int udpSrc = (upstream) ? 547 : 546;
1358 int udpDst = (upstream) ? 546 : 547;
1359
1360 EthType ethType = EthType.EtherType.IPV6.ethType();
1361 byte protocol = IPv6.PROTOCOL_UDP;
1362
Gamze Abaka838d8142019-02-21 07:06:55 +00001363 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1364 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001365 }
1366
1367 }
1368
1369 private void addDhcpFilteringObjectives(DeviceId devId,
1370 PortNumber port,
1371 int udpSrc,
1372 int udpDst,
1373 EthType ethType,
Gamze Abaka838d8142019-02-21 07:06:55 +00001374 MeterId upstreamMeterId,
1375 int techProfileId,
Matteo Scandolo63460d12018-11-02 16:19:04 -07001376 byte protocol,
1377 boolean install) {
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001378
1379 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001380 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1381
1382 if (upstreamMeterId != null) {
1383 treatmentBuilder.meter(upstreamMeterId);
1384 }
1385
1386 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001387 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001388 }
1389
Amit Ghosh95e2f652017-08-23 12:49:46 +01001390 FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny())
1391 .withKey(Criteria.matchInPort(port))
Matteo Scandolo63460d12018-11-02 16:19:04 -07001392 .addCondition(Criteria.matchEthType(ethType))
1393 .addCondition(Criteria.matchIPProtocol(protocol))
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001394 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1395 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Gamze Abaka838d8142019-02-21 07:06:55 +00001396 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001397 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001398 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -04001399 .withPriority(10000)
Amit Ghosh95e2f652017-08-23 12:49:46 +01001400 .add(new ObjectiveContext() {
1401 @Override
1402 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001403 log.info("DHCP {} filter for device {} on port {} {}.",
Gamze Abakaad329652018-12-20 10:12:21 +00001404 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001405 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001406 }
1407
1408 @Override
1409 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001410 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
Gamze Abakaad329652018-12-20 10:12:21 +00001411 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001412 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abakaad329652018-12-20 10:12:21 +00001413 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001414 }
1415 });
1416
1417 flowObjectiveService.filter(devId, dhcpUpstream);
1418 }
1419
Gamze Abaka838d8142019-02-21 07:06:55 +00001420 private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
1421 MeterId upstreamMeterId,
1422 int techProfileId,
1423 boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001424
Gamze Abakaf59c0912019-04-19 08:24:28 +00001425 if (!enableIgmpOnProvisioning) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001426 log.debug("Igmp provisioning is disabled.");
1427 return;
1428 }
1429
Amit Ghosh95e2f652017-08-23 12:49:46 +01001430 if (!mastershipService.isLocalMaster(devId)) {
1431 return;
1432 }
1433
Gamze Abaka641fc072018-09-04 09:16:27 +00001434 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001435 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1436
1437 if (upstreamMeterId != null) {
1438 treatmentBuilder.meter(upstreamMeterId);
1439 }
1440
1441 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001442 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001443 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001444
1445 builder = install ? builder.permit() : builder.deny();
1446
1447 FilteringObjective igmp = builder
1448 .withKey(Criteria.matchInPort(port))
1449 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1450 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
Gamze Abaka838d8142019-02-21 07:06:55 +00001451 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001452 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001453 .fromApp(appId)
1454 .withPriority(10000)
1455 .add(new ObjectiveContext() {
1456 @Override
1457 public void onSuccess(Objective objective) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001458 log.info("Igmp filter for {} on {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001459 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001460 }
1461
1462 @Override
1463 public void onError(Objective objective, ObjectiveError error) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001464 log.info("Igmp filter for {} on {} failed {} because {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001465 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abaka641fc072018-09-04 09:16:27 +00001466 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001467 }
1468 });
1469
1470 flowObjectiveService.filter(devId, igmp);
1471 }
1472
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001473 /**
Jonathan Hart403372d2018-08-22 11:44:13 -07001474 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
1475 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001476 *
1477 * @param dev Device to look for
1478 */
Jonathan Hart403372d2018-08-22 11:44:13 -07001479 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001480 // we create only for the ones we are master of
1481 if (!mastershipService.isLocalMaster(dev.id())) {
Gamze Abaka641fc072018-09-04 09:16:27 +00001482 return;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001483 }
1484 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001485 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Jonathan Hart403372d2018-08-22 11:44:13 -07001486 log.debug("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001487
1488 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -07001489 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001490 for (Port p : deviceService.getPorts(dev.id())) {
1491 if (isUniPort(dev, p)) {
Gamze Abakada282b42019-03-11 13:16:48 +00001492 processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1493 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Jonathan Hart403372d2018-08-22 11:44:13 -07001494 } else {
1495 processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001496 }
1497 }
1498 }
1499 }
1500
Jonathan Hart403372d2018-08-22 11:44:13 -07001501
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001502 /**
1503 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001504 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001505 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1506 * this logic needs to be changed
1507 *
1508 * @param dev Device to look for
1509 * @return The uplink Port of the OLT
1510 */
1511 private Port getUplinkPort(Device dev) {
1512 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001513 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Das4c1a6a92019-06-19 13:26:15 -07001514 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001515 if (deviceInfo == null) {
1516 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
1517 + " info", dev.id());
1518 return null;
1519 }
1520 // Return the port that has been configured as the uplink port of this OLT in Sadis
Gamze Abakaad329652018-12-20 10:12:21 +00001521 for (Port p : deviceService.getPorts(dev.id())) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001522 if (p.number().toLong() == deviceInfo.uplinkPort()) {
Saurav Das4c1a6a92019-06-19 13:26:15 -07001523 log.trace("getUplinkPort: Found port {}", p);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001524 return p;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001525 }
1526 }
1527
Saurav Das4c1a6a92019-06-19 13:26:15 -07001528 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001529 return null;
1530 }
1531
1532 /**
1533 * Return the subscriber on a port.
1534 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001535 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001536 * @return subscriber if found else null
1537 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001538 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1539 Port port = deviceService.getPort(cp);
1540 checkNotNull(port, "Invalid connect point");
1541 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001542 return subsService.get(portName);
1543 }
1544
Gamze Abakaad329652018-12-20 10:12:21 +00001545 /**
1546 * Write metadata instruction value (metadata) is 8 bytes.
Gamze Abaka838d8142019-02-21 07:06:55 +00001547 * <p>
Gamze Abakaad329652018-12-20 10:12:21 +00001548 * MS 2 bytes: C Tag
1549 * Next 2 bytes: Technology Profile Id
1550 * Next 4 bytes: Port number (uni or nni)
1551 */
1552
1553 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
1554
1555 if (techProfileId == -1) {
1556 techProfileId = DEFAULT_TP_ID;
1557 }
1558
1559 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
1560 }
1561
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001562 private boolean isUniPort(Device d, Port p) {
1563 Port ulPort = getUplinkPort(d);
1564 if (ulPort != null) {
1565 return (ulPort.number().toLong() != p.number().toLong());
1566 }
1567 return false;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001568 }
1569
Jonathan Hart4c538002018-08-23 10:11:54 -07001570 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1571 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001572 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001573 }
1574
Gamze Abaka33feef52019-02-27 08:16:47 +00001575 private MeterId getMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfile) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001576 if (bpInfoToMeter.get(bandwidthProfile) == null) {
Saurav Das4c1a6a92019-06-19 13:26:15 -07001577 log.warn("Bandwidth Profile '{}' is not currently mapped to a meter",
1578 bandwidthProfile);
Gamze Abaka33feef52019-02-27 08:16:47 +00001579 return null;
1580 }
1581
1582 Optional<MeterKey> meterKeyForDevice = bpInfoToMeter.get(bandwidthProfile)
1583 .stream()
1584 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1585 .findFirst();
Saurav Das4c1a6a92019-06-19 13:26:15 -07001586 if (meterKeyForDevice.isPresent()) {
1587 log.debug("Found meter {} for bandwidth profile {}",
1588 meterKeyForDevice.get().meterId(), bandwidthProfile);
1589 return meterKeyForDevice.get().meterId();
1590 } else {
1591 log.warn("Bandwidth profile '{}' is not currently mapped to a meter",
1592 bandwidthProfile);
1593 return null;
1594 }
Gamze Abaka33feef52019-02-27 08:16:47 +00001595 }
1596
1597 private void addMeterIdToBpMapping(DeviceId deviceId, MeterId meterId, String bandwidthProfile) {
1598
1599 if (bpInfoToMeter.get(bandwidthProfile) == null) {
1600 bpInfoToMeter.put(bandwidthProfile,
1601 new ArrayList<>(Arrays.asList(MeterKey.key(deviceId, meterId))));
1602 } else {
Gamze Abaka33feef52019-02-27 08:16:47 +00001603 List<MeterKey> meterKeyListForBp = bpInfoToMeter.get(bandwidthProfile);
1604 meterKeyListForBp.add(MeterKey.key(deviceId, meterId));
1605 }
1606 }
1607
1608 private void removeMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfileId) {
1609 List<MeterKey> meterKeysForBp = bpInfoToMeter.get(bandwidthProfileId);
1610 if (meterKeysForBp != null) {
1611 meterKeysForBp.stream()
1612 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1613 .findFirst().ifPresent(mk -> {
1614 meterKeysForBp.remove(mk);
1615 programmedMeters.remove(mk);
1616 });
1617 }
1618 }
1619
alshabibf0e7e702015-05-30 18:22:36 -07001620 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001621 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1622
alshabibf0e7e702015-05-30 18:22:36 -07001623 @Override
1624 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001625 eventExecutor.execute(() -> {
1626 DeviceId devId = event.subject().id();
1627 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001628 Port port = event.port();
Jonathan Hart4c538002018-08-23 10:11:54 -07001629
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001630 if (event.type() == DeviceEvent.Type.PORT_STATS_UPDATED) {
1631 return;
1632 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001633
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001634 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001635 // it's possible that we got an event for a previously
1636 // programmed OLT that is no longer available in SADIS
1637 // we let such events go through
1638 if (!programmedDevices.contains(devId)) {
1639 log.warn("No device info found for {}, this is either "
1640 + "not an OLT or not known to sadis", dev);
1641 return;
1642 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001643 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001644
Saurav Das4c1a6a92019-06-19 13:26:15 -07001645 log.debug("OLT got {} event for {}: {}", event.type(),
1646 event.subject().id(), event);
Jonathan Hart4c538002018-08-23 10:11:54 -07001647
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001648 switch (event.type()) {
1649 //TODO: Port handling and bookkeeping should be improved once
1650 // olt firmware handles correct behaviour.
1651 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001652 if (isUniPort(dev, port)) {
1653 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Gamze Abaka838d8142019-02-21 07:06:55 +00001654 if (port.isEnabled()) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001655 processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Gamze Abakada282b42019-03-11 13:16:48 +00001656 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001657 }
1658 } else {
1659 checkAndCreateDeviceFlows(dev);
1660 }
1661 break;
1662 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001663 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001664 removeSubscriber(new ConnectPoint(devId, port.number()));
Andy Bavier160e8682019-05-07 18:32:22 -07001665 processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1666 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
1667
Gamze Abaka838d8142019-02-21 07:06:55 +00001668 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001669 }
1670
1671 break;
1672 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001673 if (!isUniPort(dev, port)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001674 break;
1675 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001676
Gamze Abakada282b42019-03-11 13:16:48 +00001677 SubscriberAndDeviceInformation sub = programmedSubs
1678 .get(new ConnectPoint(devId, port.number()));
1679 VlanId vlanId = sub == null ? VlanId.vlanId(EAPOL_DEFAULT_VLAN) : sub.cTag();
1680
1681 String bpId = getCurrentBandwidthProfile(new ConnectPoint(devId, port.number()));
1682
Gamze Abaka838d8142019-02-21 07:06:55 +00001683 if (port.isEnabled()) {
Gamze Abakada282b42019-03-11 13:16:48 +00001684 processEapolFilteringObjectives(devId, port.number(), bpId,
1685 null, vlanId, true);
Gamze Abaka33feef52019-02-27 08:16:47 +00001686
Gamze Abaka838d8142019-02-21 07:06:55 +00001687 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001688 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001689 processEapolFilteringObjectives(devId, port.number(), bpId,
1690 null, vlanId, false);
Gamze Abaka838d8142019-02-21 07:06:55 +00001691 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001692 }
alshabibbb83aa22016-02-10 15:08:23 -08001693 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001694 case DEVICE_ADDED:
alshabib7c190012016-02-09 18:22:33 -08001695 post(new AccessDeviceEvent(
1696 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1697 null, null));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001698 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001699 // Send UNI_ADDED events for all existing ports
1700 deviceService.getPorts(devId).stream()
1701 .filter(p -> isUniPort(dev, p))
1702 .filter(Port::isEnabled)
1703 .forEach(p -> post(new AccessDeviceEvent(
1704 AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
1705
Jonathan Hart403372d2018-08-22 11:44:13 -07001706 checkAndCreateDeviceFlows(dev);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001707 break;
1708 case DEVICE_REMOVED:
1709 deviceService.getPorts(devId).stream()
1710 .filter(p -> isUniPort(dev, p))
1711 .forEach(p -> post(new AccessDeviceEvent(
1712 AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001713 programmedDevices.remove(devId);
Gamze Abakada282b42019-03-11 13:16:48 +00001714 removeAllSubscribers(devId);
alshabib7c190012016-02-09 18:22:33 -08001715 post(new AccessDeviceEvent(
1716 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1717 null, null));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001718 break;
1719 case DEVICE_AVAILABILITY_CHANGED:
1720 if (deviceService.isAvailable(devId)) {
1721 post(new AccessDeviceEvent(
1722 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1723 null, null));
Gamze Abakada282b42019-03-11 13:16:48 +00001724 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001725 checkAndCreateDeviceFlows(dev);
1726 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001727 programmedDevices.remove(devId);
1728 removeAllSubscribers(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001729 post(new AccessDeviceEvent(
1730 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1731 null, null));
1732 }
1733 break;
1734 case DEVICE_UPDATED:
1735 case DEVICE_SUSPENDED:
1736 case PORT_STATS_UPDATED:
1737 default:
1738 return;
1739 }
1740 });
alshabibf0e7e702015-05-30 18:22:36 -07001741 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001742
1743 private String getCurrentBandwidthProfile(ConnectPoint connectPoint) {
1744 SubscriberAndDeviceInformation sub = programmedSubs.get(connectPoint);
1745 if (sub != null) {
1746 return sub.upstreamBandwidthProfile();
1747 }
1748 return defaultBpId;
1749 }
Gamze Abakada282b42019-03-11 13:16:48 +00001750
1751 private void removeAllSubscribers(DeviceId deviceId) {
1752 List<ConnectPoint> connectPoints = programmedSubs.keySet().stream()
1753 .filter(ks -> Objects.equals(ks.deviceId(), deviceId))
1754 .collect(Collectors.toList());
1755
1756 connectPoints.forEach(cp -> programmedSubs.remove(cp));
1757 }
alshabibf0e7e702015-05-30 18:22:36 -07001758 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001759
1760 private class InternalMeterListener implements MeterListener {
1761
1762 @Override
1763 public void event(MeterEvent meterEvent) {
1764 if (deleteMeters && MeterEvent.Type.METER_REFERENCE_COUNT_ZERO.equals(meterEvent.type())) {
Saurav Das4c1a6a92019-06-19 13:26:15 -07001765 log.debug("Zero Count Meter {} received", meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001766 Meter meter = meterEvent.subject();
Gamze Abaka33feef52019-02-27 08:16:47 +00001767 if (meter != null && appId.equals(meter.appId()) &&
1768 !programmedMeters.contains(MeterKey.key(meter.deviceId(), meter.id()))) {
Saurav Das4c1a6a92019-06-19 13:26:15 -07001769 log.info("Deleting unreferenced, no longer programmed Meter {}",
1770 meter.id());
Gamze Abaka641fc072018-09-04 09:16:27 +00001771 deleteMeter(meter.deviceId(), meter.id());
1772 }
1773 } else if (MeterEvent.Type.METER_REMOVED.equals(meterEvent.type())) {
Saurav Das4c1a6a92019-06-19 13:26:15 -07001774 log.debug("Meter removed event is received. Meter is {}",
1775 meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001776 removeMeterFromBpMap(meterEvent.subject());
1777 }
1778 }
1779
1780 private void deleteMeter(DeviceId deviceId, MeterId meterId) {
1781 Meter meter = meterService.getMeter(deviceId, meterId);
Gamze Abaka838d8142019-02-21 07:06:55 +00001782 if (meter != null) {
1783 MeterRequest meterRequest = DefaultMeterRequest.builder()
1784 .withBands(meter.bands())
1785 .withUnit(meter.unit())
1786 .forDevice(deviceId)
1787 .fromApp(appId)
1788 .burst()
1789 .remove();
Gamze Abaka641fc072018-09-04 09:16:27 +00001790
Gamze Abaka838d8142019-02-21 07:06:55 +00001791 meterService.withdraw(meterRequest, meterId);
1792 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001793 }
1794
1795 private void removeMeterFromBpMap(Meter meter) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001796 bpInfoToMeter.values().forEach(meterKeys -> meterKeys.stream()
1797 .filter(meterKey -> (meterKey.deviceId().equals(meter.deviceId()))
1798 && meterKey.meterId().equals(meter.id())).findFirst().
1799 ifPresent(mk -> {
1800 meterKeys.remove(mk);
Gamze Abakada282b42019-03-11 13:16:48 +00001801 programmedMeters.remove(mk);
Gamze Abaka33feef52019-02-27 08:16:47 +00001802 log.info("Deleted from the internal map. MeterKey {}", mk);
1803 log.info("Programmed meters {}", programmedMeters);
1804 }));
Gamze Abaka641fc072018-09-04 09:16:27 +00001805 }
1806 }
1807}