blob: 60d9feb2217ba39813244184e8fb12b7d61da511 [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 Daseae48de2019-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 Daseae48de2019-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 Daseae48de2019-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 Daseae48de2019-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 Daseae48de2019-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 Daseae48de2019-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");
Hardik Windlass395ff372019-06-13 05:16:00 +0000331
332 // If the subscriber is modified then first remove the previous and proceed
333 SubscriberAndDeviceInformation subscriber = programmedSubs.get(connectPoint);
334 if (subscriber != null) {
335 log.info("Subscriber on connectionPoint {} was previously programmed, " +
336 "remove it before adding again", connectPoint);
337 removeSubscriber(connectPoint);
338 }
339
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100340 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000341 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100342 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000343 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100344 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100345 }
Jonathan Harte533a422015-10-20 17:31:24 -0700346
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100347 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000348 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100349 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000350 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100351 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700352 }
353
Saurav Daseae48de2019-06-19 13:26:15 -0700354 SubscriberAndDeviceInformation prgSub = programmedSubs.get(connectPoint);
355 if (prgSub != null) {
356 log.warn("Subscriber {} on connectionPoint {} was previously programmed .. "
357 + "taking no action. Note that updating a subscribers params "
358 + "(vlans, bw, tpid etc) requires removing the subscriber"
359 + "before re-provisioning the subscriber", prgSub.id(),
360 connectPoint);
361 return true;
362 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000363
Gamze Abaka838d8142019-02-21 07:06:55 +0000364 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000365 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Saurav Daseae48de2019-06-19 13:26:15 -0700366 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
Gamze Abakada282b42019-03-11 13:16:48 +0000367 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
368 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Saurav Das747a7da2019-06-27 12:28:06 -0700369 // do not remove meter from bpInfoToMeter mapping as flows for other ONUs
370 // could still be using it - this also prevents duplicate meters from being
371 // created for the same bandwidth profile
372 // we still need to remove from programmedMeters so the meter can be
373 // deleted if its reference count drops to zero
374 removeMeterIdFromPrgMeters(deviceId, defaultBpId);
Gamze Abaka838d8142019-02-21 07:06:55 +0000375
Gamze Abaka33feef52019-02-27 08:16:47 +0000376 //install subscriber flows
377 filterFuture.thenAcceptAsync(filterStatus -> {
378 if (filterStatus == null) {
379 provisionSubscriberBasedFlows(connectPoint, uplinkPort.number(), Optional.empty(), sub);
380 }
381 });
Gamze Abaka838d8142019-02-21 07:06:55 +0000382
Saurav Das82b8e6d2018-10-04 15:25:12 -0700383 // cache subscriber info
Gamze Abaka838d8142019-02-21 07:06:55 +0000384 programmedSubs.put(connectPoint, sub);
Amit Ghosh31939522018-08-16 13:28:21 +0100385 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800386 }
387
388 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000389 public boolean removeSubscriber(ConnectPoint connectPoint) {
Saurav Daseae48de2019-06-19 13:26:15 -0700390 log.info("Call to un-provision subscriber at {}", connectPoint);
Gamze Abaka838d8142019-02-21 07:06:55 +0000391
Saurav Daseae48de2019-06-19 13:26:15 -0700392 // Get the subscriber connected to this port from the local cache
393 // If we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000394 DeviceId deviceId = connectPoint.deviceId();
395 PortNumber subscriberPortNo = connectPoint.port();
396
397 SubscriberAndDeviceInformation subscriber = programmedSubs.get(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100398 if (subscriber == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000399 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
400 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800401 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800402 }
403
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100404 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000405 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100406 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000407 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100408 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800409 }
410
Gamze Abaka33feef52019-02-27 08:16:47 +0000411 //delete dhcp & igmp trap flows
412 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100413
Saurav Daseae48de2019-06-19 13:26:15 -0700414 // remove dhcp filters
Gamze Abaka33feef52019-02-27 08:16:47 +0000415 processDhcpFilteringObjectives(deviceId, subscriberPortNo,
416 upstreamMeterId, subscriber.technologyProfileId(), false, true);
Gamze Abaka838d8142019-02-21 07:06:55 +0000417
Saurav Daseae48de2019-06-19 13:26:15 -0700418 // remove igmp filters
Gamze Abaka33feef52019-02-27 08:16:47 +0000419 processIgmpFilteringObjectives(deviceId, subscriberPortNo,
420 upstreamMeterId, subscriber.technologyProfileId(), false);
alshabibbf23a1f2016-01-14 17:27:11 -0800421
Gamze Abaka33feef52019-02-27 08:16:47 +0000422 //unprovision vlans
423 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, subscriber, Optional.empty());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100424
425 // Remove if there are any flows for the additional Vlans
Gamze Abaka838d8142019-02-21 07:06:55 +0000426 Collection<? extends Map.Entry<VlanId, VlanId>> vlansList = additionalVlans.get(connectPoint).value();
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100427
428 // Remove the flows for the additional vlans for this subscriber
429 for (Map.Entry<VlanId, VlanId> vlans : vlansList) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000430 unprovisionTransparentFlows(deviceId, uplinkPort.number(), subscriberPortNo,
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100431 vlans.getValue(), vlans.getKey());
432
433 // Remove it from the map also
Gamze Abaka838d8142019-02-21 07:06:55 +0000434 additionalVlans.remove(connectPoint, vlans);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100435 }
436
Saurav Daseae48de2019-06-19 13:26:15 -0700437 // re-install eapol with default bandwidth profile
Gamze Abaka33feef52019-02-27 08:16:47 +0000438 processEapolFilteringObjectives(deviceId, subscriberPortNo,
Gamze Abakada282b42019-03-11 13:16:48 +0000439 subscriber.upstreamBandwidthProfile(), null, subscriber.cTag(), false);
Andy Bavier160e8682019-05-07 18:32:22 -0700440
441 Port port = deviceService.getPort(deviceId, subscriberPortNo);
442 if (port != null && port.isEnabled()) {
443 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
444 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Saurav Daseae48de2019-06-19 13:26:15 -0700445 } else {
446 log.debug("Port {} is no longer enabled or it's unavailable. Not "
447 + "reprogramming default eapol flow", connectPoint);
Andy Bavier160e8682019-05-07 18:32:22 -0700448 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000449
Gamze Abaka838d8142019-02-21 07:06:55 +0000450 programmedSubs.remove(connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100451 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800452 }
453
Amit Ghosh31939522018-08-16 13:28:21 +0100454 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100455 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Gamze Abakaf59c0912019-04-19 08:24:28 +0000456
457 log.info("Provisioning subscriber using subscriberId {}, sTag {}, cTag {}", subscriberId, sTag, cTag);
458
Amit Ghosh31939522018-08-16 13:28:21 +0100459 // Check if we can find the connect point to which this subscriber is connected
460 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
461 if (subsPort == null) {
462 log.warn("ConnectPoint for {} not found", subscriberId);
463 return false;
464 }
465
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100466 if (!sTag.isPresent() && !cTag.isPresent()) {
467 return provisionSubscriber(subsPort);
468 } else if (sTag.isPresent() && cTag.isPresent()) {
469 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
470 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000471 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100472 return false;
473 }
474
Gamze Abakaf59c0912019-04-19 08:24:28 +0000475 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
476
477 //delete Eapol authentication flow with default bandwidth
478 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
479 processEapolFilteringObjectives(subsPort.deviceId(), subsPort.port(), defaultBpId, filterFuture,
480 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Saurav Das747a7da2019-06-27 12:28:06 -0700481 // do not remove meter from bpInfoToMeter mapping as flows for other ONUs
482 // could still be using it - this also prevents duplicate meters from being
483 // created for the same bandwidth profile
484 // we still need to remove from programmedMeters so the meter can be
485 // deleted if its reference count drops to zero
486 removeMeterIdFromPrgMeters(subsPort.deviceId(), defaultBpId);
Gamze Abakaf59c0912019-04-19 08:24:28 +0000487
488 //install subscriber flows
489 filterFuture.thenAcceptAsync(filterStatus -> {
490 if (filterStatus == null) {
491 provisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
492 cTag.get(), sTag.get());
493 }
494 });
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100495 return true;
496 } else {
497 log.warn("Provisioning failed for subscriber: {}", subscriberId);
498 return false;
499 }
Amit Ghosh31939522018-08-16 13:28:21 +0100500 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100501
alshabibe0559672016-02-21 14:49:51 -0800502 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100503 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Amit Ghosh31939522018-08-16 13:28:21 +0100504 // Check if we can find the connect point to which this subscriber is connected
505 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
506 if (subsPort == null) {
507 log.warn("ConnectPoint for {} not found", subscriberId);
508 return false;
509 }
510
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100511 if (!sTag.isPresent() && !cTag.isPresent()) {
512 return removeSubscriber(subsPort);
513 } else if (sTag.isPresent() && cTag.isPresent()) {
514 // Get the uplink port
515 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
516 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000517 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100518 return false;
519 }
520
521 unprovisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
522 cTag.get(), sTag.get());
Gamze Abakaf59c0912019-04-19 08:24:28 +0000523
524 programmedSubs.remove(subsPort);
525
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100526 return true;
527 } else {
528 log.warn("Removing subscriber failed for: {}", subscriberId);
529 return false;
530 }
Amit Ghosh31939522018-08-16 13:28:21 +0100531 }
532
533 @Override
534 public Collection<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> getSubscribers() {
535 ArrayList<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> subs = new ArrayList<>();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100536
Saurav Das82b8e6d2018-10-04 15:25:12 -0700537 // Get the subscribers for all the devices configured in sadis
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100538 // If the port is UNI, is enabled and exists in Sadis then copy it
539 for (Device d : deviceService.getDevices()) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700540 if (getOltInfo(d) == null) {
541 continue; // not an olt, or not configured in sadis
542 }
Gamze Abakaad329652018-12-20 10:12:21 +0000543 for (Port p : deviceService.getPorts(d.id())) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100544 if (isUniPort(d, p) && p.isEnabled()) {
545 ConnectPoint cp = new ConnectPoint(d.id(), p.number());
546
547 SubscriberAndDeviceInformation sub = getSubscriber(cp);
548 if (sub != null) {
Amit Ghosh31939522018-08-16 13:28:21 +0100549 Map.Entry<VlanId, VlanId> vlans = new AbstractMap.SimpleEntry(sub.sTag(), sub.cTag());
550 subs.add(new AbstractMap.SimpleEntry(cp, vlans));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100551 }
552 }
553 }
554 }
555
556 return subs;
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800557 }
558
559 @Override
Saurav Das82b8e6d2018-10-04 15:25:12 -0700560 public ImmutableMap<ConnectPoint, SubscriberAndDeviceInformation> getProgSubs() {
561 return ImmutableMap.copyOf(programmedSubs);
562 }
563
564 @Override
Gamze Abaka33feef52019-02-27 08:16:47 +0000565 public ImmutableSet<MeterKey> getProgMeters() {
566 return ImmutableSet.copyOf(programmedMeters);
567 }
568
569 @Override
570 public ImmutableMap<String, List<MeterKey>> getBpMeterMappings() {
571 return ImmutableMap.copyOf(bpInfoToMeter);
572 }
573
574 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100575 public List<DeviceId> fetchOlts() {
576 // look through all the devices and find the ones that are OLTs as per Sadis
577 List<DeviceId> olts = new ArrayList<>();
578 Iterable<Device> devices = deviceService.getDevices();
579 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700580 if (getOltInfo(d) != null) {
581 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100582 olts.add(d.id());
583 }
584 }
585 return olts;
alshabibe0559672016-02-21 14:49:51 -0800586 }
587
Amit Ghosh31939522018-08-16 13:28:21 +0100588 /**
589 * Finds the connect point to which a subscriber is connected.
590 *
591 * @param id The id of the subscriber, this is the same ID as in Sadis
592 * @return Subscribers ConnectPoint if found else null
593 */
594 private ConnectPoint findSubscriberConnectPoint(String id) {
595
596 Iterable<Device> devices = deviceService.getDevices();
597 for (Device d : devices) {
598 for (Port p : deviceService.getPorts(d.id())) {
599 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
600 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
601 log.debug("Found on device {} port {}", d.id(), p.number());
602 return new ConnectPoint(d.id(), p.number());
603 }
604 }
605 }
606 return null;
607 }
608
Gamze Abaka641fc072018-09-04 09:16:27 +0000609 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
610 if (bandwidthProfile == null) {
611 return null;
612 }
613 return bpService.get(bandwidthProfile);
614 }
615
Gamze Abaka838d8142019-02-21 07:06:55 +0000616 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000617 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000618 *
619 * @param deviceId the device identifier
620 * @param uplink uplink port of the OLT
621 * @param subscriberPort uni port
622 * @param subscriber subscriber info that includes s, c tags, tech profile and bandwidth profile references
623 * @param defaultVlan default vlan of the subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +0000624 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000625 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
626 PortNumber subscriberPort, SubscriberAndDeviceInformation subscriber,
627 Optional<VlanId> defaultVlan) {
Saurav Daseae48de2019-06-19 13:26:15 -0700628 log.info("Unprovisioning vlans for subscriber {} on dev/port: {}/{}",
629 subscriber, deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800630
631 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
632 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
633
Gamze Abaka641fc072018-09-04 09:16:27 +0000634 VlanId deviceVlan = subscriber.sTag();
635 VlanId subscriberVlan = subscriber.cTag();
636
Gamze Abaka33feef52019-02-27 08:16:47 +0000637 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
638 MeterId downstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.downstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000639
alshabib4ceaed32016-03-03 18:00:58 -0800640 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000641 subscriberVlan, deviceVlan,
642 defaultVlan, upstreamMeterId, subscriber.technologyProfileId());
alshabib4ceaed32016-03-03 18:00:58 -0800643 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000644 subscriberVlan, deviceVlan,
645 defaultVlan, downstreamMeterId, subscriber.technologyProfileId());
alshabibbf23a1f2016-01-14 17:27:11 -0800646
alshabib4ceaed32016-03-03 18:00:58 -0800647 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
648 @Override
649 public void onSuccess(Objective objective) {
650 upFuture.complete(null);
651 }
alshabibbf23a1f2016-01-14 17:27:11 -0800652
alshabib4ceaed32016-03-03 18:00:58 -0800653 @Override
654 public void onError(Objective objective, ObjectiveError error) {
655 upFuture.complete(error);
656 }
657 }));
658
659 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
660 @Override
661 public void onSuccess(Objective objective) {
662 downFuture.complete(null);
663 }
664
665 @Override
666 public void onError(Objective objective, ObjectiveError error) {
667 downFuture.complete(error);
668 }
669 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800670
671 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
672 if (upStatus == null && downStatus == null) {
673 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000674 deviceId,
675 deviceVlan,
676 subscriberVlan));
alshabibbf23a1f2016-01-14 17:27:11 -0800677 } else if (downStatus != null) {
678 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000679 "on port {} failed downstream uninstallation: {}",
680 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800681 } else if (upStatus != null) {
682 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000683 "on port {} failed upstream uninstallation: {}",
684 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800685 }
686 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800687
Gamze Abaka33feef52019-02-27 08:16:47 +0000688 programmedMeters.remove(MeterKey.key(deviceId, upstreamMeterId));
689 programmedMeters.remove(MeterKey.key(deviceId, downstreamMeterId));
Gamze Abaka838d8142019-02-21 07:06:55 +0000690 log.debug("programmed Meters size {}", programmedMeters.size());
Jonathan Harte533a422015-10-20 17:31:24 -0700691 }
692
Gamze Abaka838d8142019-02-21 07:06:55 +0000693 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000694 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000695 *
696 * @param port the connection point of the subscriber
697 * @param uplinkPort uplink port of the OLT
698 * @param defaultVlan default vlan of the subscriber
699 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000700 */
Saurav Daseae48de2019-06-19 13:26:15 -0700701 private void provisionSubscriberBasedFlows(ConnectPoint port, PortNumber uplinkPort,
702 Optional<VlanId> defaultVlan,
Gamze Abaka33feef52019-02-27 08:16:47 +0000703 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000704
Saurav Daseae48de2019-06-19 13:26:15 -0700705 log.info("Provisioning vlans for subscriber {} on dev/port: {}",
706 sub, port);
Gamze Abaka641fc072018-09-04 09:16:27 +0000707
708 DeviceId deviceId = port.deviceId();
709 PortNumber subscriberPort = port.port();
710 VlanId deviceVlan = sub.sTag();
711 VlanId subscriberVlan = sub.cTag();
712 int techProfId = sub.technologyProfileId();
713
714 BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(sub.upstreamBandwidthProfile());
715 BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(sub.downstreamBandwidthProfile());
716
alshabib3ea82642016-01-12 18:06:53 -0800717 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
718 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
Gamze Abaka33feef52019-02-27 08:16:47 +0000719 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
720 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
alshabib3ea82642016-01-12 18:06:53 -0800721
Gamze Abaka33feef52019-02-27 08:16:47 +0000722 MeterId upstreamMeterId = createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
723 MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Jonathan Harte533a422015-10-20 17:31:24 -0700724
Gamze Abaka33feef52019-02-27 08:16:47 +0000725 //install upstream flows
726 upstreamMeterFuture.thenAcceptAsync(result -> {
727 if (result == null) {
Saurav Daseae48de2019-06-19 13:26:15 -0700728 log.info("Upstream Meter {} is in the device {}. " +
Gamze Abaka33feef52019-02-27 08:16:47 +0000729 "Sending subscriber flows.", upstreamMeterId, deviceId);
730 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
731 subscriberVlan, deviceVlan,
732 defaultVlan, upstreamMeterId, techProfId);
alshabib3ea82642016-01-12 18:06:53 -0800733
Gamze Abaka33feef52019-02-27 08:16:47 +0000734
735 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
736 @Override
737 public void onSuccess(Objective objective) {
738 upFuture.complete(null);
739 }
740
741 @Override
742 public void onError(Objective objective, ObjectiveError error) {
743 upFuture.complete(error);
744 }
745 }));
746 } else {
747 log.warn("Meter installation error while sending upstream flows. " +
748 "Result {} and MeterId {}", result, upstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800749 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000750 });
alshabibbf23a1f2016-01-14 17:27:11 -0800751
Gamze Abaka33feef52019-02-27 08:16:47 +0000752 //install downstream flows
753 downsteamMeterFuture.thenAcceptAsync(result -> {
754 if (result == null) {
Saurav Daseae48de2019-06-19 13:26:15 -0700755 log.info("Downstream Meter {} is in the device {}. " +
Gamze Abaka33feef52019-02-27 08:16:47 +0000756 "Sending subscriber flows.", downstreamMeterId, deviceId);
757 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
758 subscriberVlan, deviceVlan,
759 defaultVlan, downstreamMeterId, techProfId);
760
761 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
762 @Override
763 public void onSuccess(Objective objective) {
764 downFuture.complete(null);
765 }
766
767 @Override
768 public void onError(Objective objective, ObjectiveError error) {
769 downFuture.complete(error);
770 }
771 }));
772 } else {
773 log.warn("Meter installation error while sending downstream flows. " +
774 "Result {} and MeterId {}", result, downstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800775 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000776 });
alshabibbf23a1f2016-01-14 17:27:11 -0800777
Gamze Abaka33feef52019-02-27 08:16:47 +0000778 //send eapol & dhcp & igmp flows
779 //send Subscriber Registered event
alshabib3ea82642016-01-12 18:06:53 -0800780 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
781 if (upStatus == null && downStatus == null) {
Gamze Abaka33feef52019-02-27 08:16:47 +0000782
783 if (upstreamMeterId != null) {
784 //re-install Eapol authentication flow with the subscribers' upstream bandwidth profile
785 processEapolFilteringObjectives(deviceId, subscriberPort, sub.upstreamBandwidthProfile(),
Gamze Abakada282b42019-03-11 13:16:48 +0000786 null, sub.cTag(), true);
Gamze Abaka33feef52019-02-27 08:16:47 +0000787
788 processDhcpFilteringObjectives(deviceId, subscriberPort,
789 upstreamMeterId, sub.technologyProfileId(), true, true);
790
791 processIgmpFilteringObjectives(deviceId, subscriberPort,
792 upstreamMeterId, sub.technologyProfileId(), true);
793 }
794
alshabib3ea82642016-01-12 18:06:53 -0800795 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000796 deviceId,
797 deviceVlan,
798 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800799
alshabib3ea82642016-01-12 18:06:53 -0800800 } else if (downStatus != null) {
801 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000802 "on port {} failed downstream installation: {}",
803 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabib3ea82642016-01-12 18:06:53 -0800804 } else if (upStatus != null) {
805 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000806 "on port {} failed upstream installation: {}",
807 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabib3ea82642016-01-12 18:06:53 -0800808 }
809 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700810 }
811
Gamze Abaka33feef52019-02-27 08:16:47 +0000812 private MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo,
813 CompletableFuture<Object> meterFuture) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000814 if (bpInfo == null) {
Saurav Daseae48de2019-06-19 13:26:15 -0700815 log.warn("Bandwidth profile information cannot be null when creating meter");
Gamze Abaka641fc072018-09-04 09:16:27 +0000816 return null;
817 }
818
Gamze Abaka33feef52019-02-27 08:16:47 +0000819 MeterId meterId = getMeterIdFromBpMapping(deviceId, bpInfo.id());
Gamze Abaka641fc072018-09-04 09:16:27 +0000820 if (meterId != null) {
Saurav Daseae48de2019-06-19 13:26:15 -0700821 log.debug("Meter {} was previously created for bp {}", meterId,
822 bpInfo.id());
Gamze Abaka33feef52019-02-27 08:16:47 +0000823 meterFuture.complete(null);
Gamze Abaka641fc072018-09-04 09:16:27 +0000824 return meterId;
825 }
826
827 List<Band> meterBands = createMeterBands(bpInfo);
828
829 MeterRequest meterRequest = DefaultMeterRequest.builder()
830 .withBands(meterBands)
831 .withUnit(Meter.Unit.KB_PER_SEC)
Gamze Abaka33feef52019-02-27 08:16:47 +0000832 .withContext(new MeterContext() {
833 @Override
834 public void onSuccess(MeterRequest op) {
Saurav Daseae48de2019-06-19 13:26:15 -0700835 log.debug("meter addition confirmed for bpInfo:{}", bpInfo);
Gamze Abaka33feef52019-02-27 08:16:47 +0000836 meterFuture.complete(null);
837 }
838
839 @Override
840 public void onError(MeterRequest op, MeterFailReason reason) {
841 meterFuture.complete(reason);
842 }
843 })
Gamze Abaka641fc072018-09-04 09:16:27 +0000844 .forDevice(deviceId)
845 .fromApp(appId)
846 .burst()
847 .add();
848
849 Meter meter = meterService.submit(meterRequest);
Gamze Abaka33feef52019-02-27 08:16:47 +0000850 addMeterIdToBpMapping(deviceId, meter.id(), bpInfo.id());
Saurav Daseae48de2019-06-19 13:26:15 -0700851 log.info("Meter creation message sent for Meter Id {}", meter.id());
Gamze Abaka33feef52019-02-27 08:16:47 +0000852 programmedMeters.add(MeterKey.key(deviceId, meter.id()));
Gamze Abaka641fc072018-09-04 09:16:27 +0000853 return meter.id();
854 }
855
856 private List<Band> createMeterBands(BandwidthProfileInformation bpInfo) {
857 List<Band> meterBands = new ArrayList<>();
858
859 meterBands.add(createMeterBand(bpInfo.committedInformationRate(), bpInfo.committedBurstSize()));
860 meterBands.add(createMeterBand(bpInfo.exceededInformationRate(), bpInfo.exceededBurstSize()));
Gamze Abakaad329652018-12-20 10:12:21 +0000861 meterBands.add(createMeterBand(bpInfo.assuredInformationRate(), 0L));
Gamze Abaka641fc072018-09-04 09:16:27 +0000862
Gamze Abaka641fc072018-09-04 09:16:27 +0000863 return meterBands;
864 }
865
866 private Band createMeterBand(long rate, Long burst) {
867 return DefaultBand.builder()
868 .withRate(rate) //already Kbps
869 .burstSize(burst) // already Kbits
870 .ofType(Band.Type.DROP) // no matter
871 .build();
872 }
873
alshabib4ceaed32016-03-03 18:00:58 -0800874 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
875 PortNumber subscriberPort,
876 VlanId subscriberVlan,
877 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000878 Optional<VlanId> defaultVlan,
879 MeterId meterId,
880 int techProfId) {
alshabib4ceaed32016-03-03 18:00:58 -0800881 TrafficSelector downstream = DefaultTrafficSelector.builder()
882 .matchVlanId(deviceVlan)
883 .matchInPort(uplinkPort)
884 .matchInnerVlanId(subscriberVlan)
885 .build();
886
Gamze Abaka641fc072018-09-04 09:16:27 +0000887 TrafficTreatment.Builder downstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800888 .popVlan()
889 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
Gamze Abaka641fc072018-09-04 09:16:27 +0000890 .setOutput(subscriberPort);
891
892 if (meterId != null) {
893 downstreamTreatmentBuilder.meter(meterId);
894 }
895
Gamze Abakaad329652018-12-20 10:12:21 +0000896 downstreamTreatmentBuilder.writeMetadata(createMetadata(subscriberVlan, techProfId, subscriberPort), 0);
alshabib4ceaed32016-03-03 18:00:58 -0800897
898 return DefaultForwardingObjective.builder()
899 .withFlag(ForwardingObjective.Flag.VERSATILE)
900 .withPriority(1000)
901 .makePermanent()
902 .withSelector(downstream)
903 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000904 .withTreatment(downstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800905 }
906
907 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
908 PortNumber subscriberPort,
909 VlanId subscriberVlan,
910 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000911 Optional<VlanId> defaultVlan,
912 MeterId meterId,
913 int technologyProfileId) {
914
915
916 VlanId dVlan = defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan));
917
918 if (subscriberVlan.toShort() == 4096) {
919 dVlan = subscriberVlan;
920 }
921
alshabib4ceaed32016-03-03 18:00:58 -0800922 TrafficSelector upstream = DefaultTrafficSelector.builder()
Gamze Abaka641fc072018-09-04 09:16:27 +0000923 .matchVlanId(dVlan)
alshabib4ceaed32016-03-03 18:00:58 -0800924 .matchInPort(subscriberPort)
925 .build();
926
927
Gamze Abaka641fc072018-09-04 09:16:27 +0000928 TrafficTreatment.Builder upstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800929 .pushVlan()
930 .setVlanId(subscriberVlan)
931 .pushVlan()
932 .setVlanId(deviceVlan)
Gamze Abaka641fc072018-09-04 09:16:27 +0000933 .setOutput(uplinkPort);
934
935 if (meterId != null) {
936 upstreamTreatmentBuilder.meter(meterId);
937 }
938
Gamze Abakaad329652018-12-20 10:12:21 +0000939 upstreamTreatmentBuilder.writeMetadata(createMetadata(deviceVlan, technologyProfileId, uplinkPort), 0L);
alshabib4ceaed32016-03-03 18:00:58 -0800940
941 return DefaultForwardingObjective.builder()
942 .withFlag(ForwardingObjective.Flag.VERSATILE)
943 .withPriority(1000)
944 .makePermanent()
945 .withSelector(upstream)
946 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000947 .withTreatment(upstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800948 }
Gamze Abakaad329652018-12-20 10:12:21 +0000949
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100950 private void provisionTransparentFlows(DeviceId deviceId, PortNumber uplinkPort,
951 PortNumber subscriberPort,
952 VlanId innerVlan,
953 VlanId outerVlan) {
954
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100955 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
956
Gamze Abakaf59c0912019-04-19 08:24:28 +0000957 SubscriberAndDeviceInformation subInfo = getSubscriber(cp);
958
959 BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(
960 subInfo.upstreamBandwidthProfile());
961 BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(
962 subInfo.downstreamBandwidthProfile());
963
964 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
965 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
966 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
967 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
968
969 MeterId upstreamMeterId = createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
970 MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
971
972 upstreamMeterFuture.thenAcceptAsync(result -> {
973 if (result == null) {
974 log.info("Upstream Meter {} is sent to the device {}. " +
975 "Sending subscriber flows.", upstreamMeterId, deviceId);
976
977 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplinkPort, subscriberPort,
978 innerVlan, outerVlan, upstreamMeterId, subInfo);
979
980 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
981 @Override
982 public void onSuccess(Objective objective) {
983 upFuture.complete(null);
984 }
985
986 @Override
987 public void onError(Objective objective, ObjectiveError error) {
988 upFuture.complete(error);
989 }
990 }));
991
992 } else {
993 log.warn("Meter installation error while sending upstream flows. " +
994 "Result {} and MeterId {}", result, upstreamMeterId);
995 }
996 });
997
998 downsteamMeterFuture.thenAcceptAsync(result -> {
999 if (result == null) {
1000 log.info("Downstream Meter {} is sent to the device {}. " +
1001 "Sending subscriber flows.", downstreamMeterId, deviceId);
1002
1003 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplinkPort, subscriberPort,
1004 innerVlan, outerVlan, downstreamMeterId, subInfo);
1005
1006 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
1007 @Override
1008 public void onSuccess(Objective objective) {
1009 downFuture.complete(null);
1010 }
1011
1012 @Override
1013 public void onError(Objective objective, ObjectiveError error) {
1014 downFuture.complete(error);
1015 }
1016 }));
1017 } else {
1018 log.warn("Meter installation error while sending upstream flows. " +
1019 "Result {} and MeterId {}", result, downstreamMeterId);
1020 }
1021 });
1022
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001023 additionalVlans.put(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
1024
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001025 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
1026 if (downStatus != null) {
1027 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001028 "on port {} failed downstream installation: {}",
Gamze Abakaf59c0912019-04-19 08:24:28 +00001029 innerVlan, outerVlan, deviceId, cp, downStatus);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001030 } else if (upStatus != null) {
1031 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001032 "on port {} failed upstream installation: {}",
Gamze Abakaf59c0912019-04-19 08:24:28 +00001033 innerVlan, outerVlan, deviceId, cp, upStatus);
1034 } else {
1035 processEapolFilteringObjectives(deviceId, subscriberPort, subInfo.upstreamBandwidthProfile(),
1036 null, subInfo.cTag(), true);
1037
1038 // cache subscriber info
1039 programmedSubs.put(cp, subInfo);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001040 }
1041 }, oltInstallers);
1042
1043 }
1044
1045 private ForwardingObjective.Builder transparentDownBuilder(PortNumber uplinkPort,
1046 PortNumber subscriberPort,
1047 VlanId innerVlan,
Gamze Abakaf59c0912019-04-19 08:24:28 +00001048 VlanId outerVlan,
1049 MeterId downstreamMeterId,
1050 SubscriberAndDeviceInformation subInfo) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001051 TrafficSelector downstream = DefaultTrafficSelector.builder()
1052 .matchVlanId(outerVlan)
1053 .matchInPort(uplinkPort)
1054 .matchInnerVlanId(innerVlan)
1055 .build();
1056
Gamze Abakaf59c0912019-04-19 08:24:28 +00001057 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1058 if (downstreamMeterId != null) {
1059 tBuilder.meter(downstreamMeterId);
1060 }
1061
1062 TrafficTreatment downstreamTreatment = tBuilder
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001063 .setOutput(subscriberPort)
Gamze Abakaf59c0912019-04-19 08:24:28 +00001064 .writeMetadata(createMetadata(subInfo.cTag(), subInfo.technologyProfileId(), subscriberPort), 0)
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001065 .build();
1066
1067 return DefaultForwardingObjective.builder()
1068 .withFlag(ForwardingObjective.Flag.VERSATILE)
1069 .withPriority(1000)
1070 .makePermanent()
1071 .withSelector(downstream)
1072 .fromApp(appId)
1073 .withTreatment(downstreamTreatment);
1074 }
1075
1076 private ForwardingObjective.Builder transparentUpBuilder(PortNumber uplinkPort,
1077 PortNumber subscriberPort,
1078 VlanId innerVlan,
Gamze Abakaf59c0912019-04-19 08:24:28 +00001079 VlanId outerVlan,
1080 MeterId upstreamMeterId,
1081 SubscriberAndDeviceInformation subInfo) {
1082
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001083 TrafficSelector upstream = DefaultTrafficSelector.builder()
1084 .matchVlanId(outerVlan)
1085 .matchInPort(subscriberPort)
1086 .matchInnerVlanId(innerVlan)
1087 .build();
1088
Gamze Abakaf59c0912019-04-19 08:24:28 +00001089 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1090 if (upstreamMeterId != null) {
1091 tBuilder.meter(upstreamMeterId);
1092 }
1093
1094 TrafficTreatment upstreamTreatment = tBuilder
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001095 .setOutput(uplinkPort)
Gamze Abakaf59c0912019-04-19 08:24:28 +00001096 .writeMetadata(createMetadata(subInfo.sTag(), subInfo.technologyProfileId(), uplinkPort), 0)
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001097 .build();
1098
1099 return DefaultForwardingObjective.builder()
1100 .withFlag(ForwardingObjective.Flag.VERSATILE)
1101 .withPriority(1000)
1102 .makePermanent()
1103 .withSelector(upstream)
1104 .fromApp(appId)
1105 .withTreatment(upstreamTreatment);
1106 }
1107
1108 private void unprovisionTransparentFlows(DeviceId deviceId, PortNumber uplink,
1109 PortNumber subscriberPort, VlanId innerVlan,
1110 VlanId outerVlan) {
1111
1112 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
1113
Gamze Abakaf59c0912019-04-19 08:24:28 +00001114 SubscriberAndDeviceInformation subInfo = programmedSubs.get(cp);
1115 if (subInfo == null) {
1116 log.warn("Subscriber is not programmed before for the connectPoint {}", cp);
1117 return;
1118 }
1119
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001120 additionalVlans.remove(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
1121
1122 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
1123 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
1124
Gamze Abakaf59c0912019-04-19 08:24:28 +00001125 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subInfo.upstreamBandwidthProfile());
1126 MeterId downstreamMeterId = getMeterIdFromBpMapping(deviceId, subInfo.downstreamBandwidthProfile());
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001127
Gamze Abakaf59c0912019-04-19 08:24:28 +00001128 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplink, subscriberPort,
1129 innerVlan, outerVlan, upstreamMeterId, subInfo);
1130 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplink, subscriberPort,
1131 innerVlan, outerVlan, downstreamMeterId, subInfo);
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001132
1133 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
1134 @Override
1135 public void onSuccess(Objective objective) {
1136 upFuture.complete(null);
1137 }
1138
1139 @Override
1140 public void onError(Objective objective, ObjectiveError error) {
1141 upFuture.complete(error);
1142 }
1143 }));
1144
1145 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
1146 @Override
1147 public void onSuccess(Objective objective) {
1148 downFuture.complete(null);
1149 }
1150
1151 @Override
1152 public void onError(Objective objective, ObjectiveError error) {
1153 downFuture.complete(error);
1154 }
1155 }));
1156
1157 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
1158 if (downStatus != null) {
1159 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001160 "on port {} failed downstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001161 innerVlan, outerVlan, deviceId, subscriberPort, downStatus);
1162 } else if (upStatus != null) {
1163 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001164 "on port {} failed upstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001165 innerVlan, outerVlan, deviceId, subscriberPort, upStatus);
1166 }
1167 }, oltInstallers);
1168
Gamze Abakaf59c0912019-04-19 08:24:28 +00001169 //re-install eapol
1170 processEapolFilteringObjectives(deviceId, subscriberPort,
1171 subInfo.upstreamBandwidthProfile(), null, subInfo.cTag(), false);
1172 processEapolFilteringObjectives(deviceId, subscriberPort, defaultBpId,
1173 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
1174
1175 programmedMeters.remove(MeterKey.key(deviceId, upstreamMeterId));
1176 programmedMeters.remove(MeterKey.key(deviceId, downstreamMeterId));
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001177 }
1178
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001179 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
1180 Port port = deviceService.getPort(devId, portNumber);
Andy Bavier160e8682019-05-07 18:32:22 -07001181 if (port != null) {
1182 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
1183 if (info != null && info.technologyProfileId() != -1) {
1184 return info.technologyProfileId();
1185 }
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001186 }
1187 return defaultTechProfileId;
1188 }
1189
Gamze Abaka838d8142019-02-21 07:06:55 +00001190 /**
Gamze Abakada282b42019-03-11 13:16:48 +00001191 * Returns the write metadata value including tech profile reference and innerVlan.
1192 * For param cVlan, null can be sent
Gamze Abaka838d8142019-02-21 07:06:55 +00001193 *
Gamze Abakada282b42019-03-11 13:16:48 +00001194 * @param cVlan c (customer) tag of one subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +00001195 * @param techProfileId tech profile id of one subscriber
Gamze Abakada282b42019-03-11 13:16:48 +00001196 * @return the write metadata value including tech profile reference and innerVlan
Gamze Abaka838d8142019-02-21 07:06:55 +00001197 */
Gamze Abakada282b42019-03-11 13:16:48 +00001198 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
1199 if (cVlan == null) {
1200 return (long) techProfileId << 32;
1201 }
1202 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
Gamze Abaka838d8142019-02-21 07:06:55 +00001203 }
1204
1205 /**
1206 * Trap eapol authentication packets to the controller.
1207 *
Gamze Abaka33feef52019-02-27 08:16:47 +00001208 * @param devId the device identifier
1209 * @param portNumber the port for which this trap flow is designated
1210 * @param bpId bandwidth profile id to add the related meter to the flow
1211 * @param filterFuture completable future for this filtering objective operation
Gamze Abakada282b42019-03-11 13:16:48 +00001212 * @param vlanId the default or customer tag for a subscriber
Gamze Abaka33feef52019-02-27 08:16:47 +00001213 * @param install true to install the flow, false to remove the flow
Gamze Abaka838d8142019-02-21 07:06:55 +00001214 */
Gamze Abaka33feef52019-02-27 08:16:47 +00001215 private void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
1216 CompletableFuture<ObjectiveError> filterFuture,
Gamze Abakada282b42019-03-11 13:16:48 +00001217 VlanId vlanId, boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001218 if (!enableEapol) {
1219 log.debug("Eapol filtering is disabled.");
1220 if (filterFuture != null) {
1221 filterFuture.complete(null);
1222 }
1223 return;
1224 }
1225
alshabib09753b52016-03-04 14:55:19 -08001226 if (!mastershipService.isLocalMaster(devId)) {
1227 return;
1228 }
alshabibbb83aa22016-02-10 15:08:23 -08001229 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abakaad329652018-12-20 10:12:21 +00001230 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gamze Abaka33feef52019-02-27 08:16:47 +00001231 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
alshabibbb83aa22016-02-10 15:08:23 -08001232
Gamze Abaka838d8142019-02-21 07:06:55 +00001233 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
Saurav Das747a7da2019-06-27 12:28:06 -07001234 if (bpInfo == null) {
1235 log.warn("Bandwidth profile {} is not found. Authentication flow"
1236 + " will not be installed", bpId);
Gamze Abaka838d8142019-02-21 07:06:55 +00001237 return;
Gamze Abakaad329652018-12-20 10:12:21 +00001238 }
1239
Saurav Das747a7da2019-06-27 12:28:06 -07001240 // check if meter exists and create it only for an install
1241 MeterId meterId = getMeterIdFromBpMapping(devId, bpInfo.id());
1242 if (meterId == null) {
1243 if (install) {
1244 meterId = createMeter(devId, bpInfo, meterFuture);
1245 treatmentBuilder.meter(meterId);
1246 } else {
1247 // this case should not happen as the request to remove an eapol
1248 // flow should mean that the flow points to a meter that exists.
1249 // Nevertheless we can still delete the flow as we only need the
1250 // correct 'match' to do so.
1251 log.warn("Unknown meter id for bp {}, still proceeding with "
1252 + "delete of eapol flow for {}/{}", bpInfo.id(), devId,
1253 portNumber);
1254 meterFuture.complete(null);
1255 }
1256 } else {
1257 log.debug("Meter {} was previously created for bp {}", meterId,
1258 bpInfo.id());
1259 treatmentBuilder.meter(meterId);
1260 meterFuture.complete(null);
1261 }
1262
1263 final MeterId mId = meterId;
Gamze Abaka33feef52019-02-27 08:16:47 +00001264 meterFuture.thenAcceptAsync(result -> {
1265 if (result == null) {
Saurav Das747a7da2019-06-27 12:28:06 -07001266 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
1267 mId, bpId, devId, portNumber,
1268 (install) ? "Installing " : "Removing ");
Gamze Abaka33feef52019-02-27 08:16:47 +00001269 int techProfileId = getDefaultTechProfileId(devId, portNumber);
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001270
Gamze Abaka33feef52019-02-27 08:16:47 +00001271 //Authentication trap flow uses only tech profile id as write metadata value
1272 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
1273 .withKey(Criteria.matchInPort(portNumber))
1274 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
Gamze Abakada282b42019-03-11 13:16:48 +00001275 .addCondition(Criteria.matchVlanId(vlanId))
Gamze Abaka33feef52019-02-27 08:16:47 +00001276 .withMeta(treatmentBuilder
Gamze Abakada282b42019-03-11 13:16:48 +00001277 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
Gamze Abaka33feef52019-02-27 08:16:47 +00001278 .setOutput(PortNumber.CONTROLLER).build())
1279 .fromApp(appId)
1280 .withPriority(10000)
1281 .add(new ObjectiveContext() {
1282 @Override
1283 public void onSuccess(Objective objective) {
1284 log.info("Eapol filter for {} on {} {} with meter {}.",
Saurav Das747a7da2019-06-27 12:28:06 -07001285 devId, portNumber, (install) ? INSTALLED : REMOVED, mId);
Gamze Abaka33feef52019-02-27 08:16:47 +00001286 if (filterFuture != null) {
1287 filterFuture.complete(null);
1288 }
1289 }
alshabibdec2e252016-01-15 12:20:25 -08001290
Gamze Abaka33feef52019-02-27 08:16:47 +00001291 @Override
1292 public void onError(Objective objective, ObjectiveError error) {
1293 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
Saurav Das747a7da2019-06-27 12:28:06 -07001294 devId, portNumber, mId, (install) ? INSTALLATION : REMOVAL,
Gamze Abaka33feef52019-02-27 08:16:47 +00001295 error);
1296 if (filterFuture != null) {
1297 filterFuture.complete(error);
1298 }
1299 }
1300 });
alshabibdec2e252016-01-15 12:20:25 -08001301
Gamze Abaka33feef52019-02-27 08:16:47 +00001302 flowObjectiveService.filter(devId, eapol);
1303 } else {
1304 log.warn("Meter installation error while sending eapol trap flow. " +
Saurav Das747a7da2019-06-27 12:28:06 -07001305 "Result {} and MeterId {}", result, mId);
Gamze Abaka33feef52019-02-27 08:16:47 +00001306 }
1307 });
alshabibdec2e252016-01-15 12:20:25 -08001308 }
1309
Jonathan Hart403372d2018-08-22 11:44:13 -07001310 /**
1311 * Installs trap filtering objectives for particular traffic types on an
1312 * NNI port.
1313 *
Gamze Abakaad329652018-12-20 10:12:21 +00001314 * @param devId device ID
1315 * @param port port number
Jonathan Hart403372d2018-08-22 11:44:13 -07001316 * @param install true to install, false to remove
1317 */
1318 private void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
1319 processLldpFilteringObjective(devId, port, install);
Gamze Abaka33feef52019-02-27 08:16:47 +00001320 processDhcpFilteringObjectives(devId, port, null, -1, install, false);
Jonathan Hart403372d2018-08-22 11:44:13 -07001321 }
1322
1323 private void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
1324 if (!mastershipService.isLocalMaster(devId)) {
1325 return;
1326 }
1327 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1328
1329 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
1330 .withKey(Criteria.matchInPort(port))
1331 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
1332 .withMeta(DefaultTrafficTreatment.builder()
1333 .setOutput(PortNumber.CONTROLLER).build())
1334 .fromApp(appId)
1335 .withPriority(10000)
1336 .add(new ObjectiveContext() {
1337 @Override
1338 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001339 log.info("LLDP filter for device {} on port {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001340 devId, port, (install) ? INSTALLED : REMOVED);
Jonathan Hart403372d2018-08-22 11:44:13 -07001341 }
1342
1343 @Override
1344 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001345 log.info("LLDP filter for device {} on port {} failed {} because {}",
Gamze Abaka838d8142019-02-21 07:06:55 +00001346 devId, port, (install) ? INSTALLATION : REMOVAL,
Saurav Das82b8e6d2018-10-04 15:25:12 -07001347 error);
Jonathan Hart403372d2018-08-22 11:44:13 -07001348 }
1349 });
1350
1351 flowObjectiveService.filter(devId, lldp);
1352
1353 }
1354
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001355 /**
1356 * Trap dhcp packets to the controller.
1357 *
Gamze Abaka838d8142019-02-21 07:06:55 +00001358 * @param devId the device identifier
1359 * @param port the port for which this trap flow is designated
1360 * @param upstreamMeterId the upstream meter id that includes the upstream
1361 * bandwidth profile values such as PIR,CIR. If no meter id needs to be referenced,
1362 * null can be sent
1363 * @param techProfileId the technology profile id that is used to create write
1364 * metadata instruction value. If no tech profile id needs to be referenced,
1365 * -1 can be sent
1366 * @param install true to install the flow, false to remove the flow
1367 * @param upstream true if trapped packets are flowing upstream towards
1368 * server, false if packets are flowing downstream towards client
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001369 */
1370 private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port,
Gamze Abaka838d8142019-02-21 07:06:55 +00001371 MeterId upstreamMeterId,
1372 int techProfileId,
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001373 boolean install,
1374 boolean upstream) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001375
1376 if (!enableDhcpOnProvisioning) {
1377 log.debug("Dhcp provisioning is disabled.");
1378 return;
1379 }
1380
Amit Ghosh95e2f652017-08-23 12:49:46 +01001381 if (!mastershipService.isLocalMaster(devId)) {
1382 return;
1383 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001384
Matteo Scandolo63460d12018-11-02 16:19:04 -07001385 if (enableDhcpV4) {
1386 int udpSrc = (upstream) ? 68 : 67;
1387 int udpDst = (upstream) ? 67 : 68;
1388
1389 EthType ethType = EthType.EtherType.IPV4.ethType();
1390 byte protocol = IPv4.PROTOCOL_UDP;
1391
Gamze Abaka838d8142019-02-21 07:06:55 +00001392 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1393 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001394 }
1395
1396 if (enableDhcpV6) {
1397 int udpSrc = (upstream) ? 547 : 546;
1398 int udpDst = (upstream) ? 546 : 547;
1399
1400 EthType ethType = EthType.EtherType.IPV6.ethType();
1401 byte protocol = IPv6.PROTOCOL_UDP;
1402
Gamze Abaka838d8142019-02-21 07:06:55 +00001403 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1404 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001405 }
1406
1407 }
1408
1409 private void addDhcpFilteringObjectives(DeviceId devId,
1410 PortNumber port,
1411 int udpSrc,
1412 int udpDst,
1413 EthType ethType,
Gamze Abaka838d8142019-02-21 07:06:55 +00001414 MeterId upstreamMeterId,
1415 int techProfileId,
Matteo Scandolo63460d12018-11-02 16:19:04 -07001416 byte protocol,
1417 boolean install) {
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001418
1419 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001420 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1421
1422 if (upstreamMeterId != null) {
1423 treatmentBuilder.meter(upstreamMeterId);
1424 }
1425
1426 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001427 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001428 }
1429
Amit Ghosh95e2f652017-08-23 12:49:46 +01001430 FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny())
1431 .withKey(Criteria.matchInPort(port))
Matteo Scandolo63460d12018-11-02 16:19:04 -07001432 .addCondition(Criteria.matchEthType(ethType))
1433 .addCondition(Criteria.matchIPProtocol(protocol))
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001434 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1435 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Gamze Abaka838d8142019-02-21 07:06:55 +00001436 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001437 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001438 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -04001439 .withPriority(10000)
Amit Ghosh95e2f652017-08-23 12:49:46 +01001440 .add(new ObjectiveContext() {
1441 @Override
1442 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001443 log.info("DHCP {} filter for device {} on port {} {}.",
Gamze Abakaad329652018-12-20 10:12:21 +00001444 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001445 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001446 }
1447
1448 @Override
1449 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001450 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
Gamze Abakaad329652018-12-20 10:12:21 +00001451 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001452 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abakaad329652018-12-20 10:12:21 +00001453 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001454 }
1455 });
1456
1457 flowObjectiveService.filter(devId, dhcpUpstream);
1458 }
1459
Gamze Abaka838d8142019-02-21 07:06:55 +00001460 private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
1461 MeterId upstreamMeterId,
1462 int techProfileId,
1463 boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001464
Gamze Abakaf59c0912019-04-19 08:24:28 +00001465 if (!enableIgmpOnProvisioning) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001466 log.debug("Igmp provisioning is disabled.");
1467 return;
1468 }
1469
Amit Ghosh95e2f652017-08-23 12:49:46 +01001470 if (!mastershipService.isLocalMaster(devId)) {
1471 return;
1472 }
1473
Gamze Abaka641fc072018-09-04 09:16:27 +00001474 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001475 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1476
1477 if (upstreamMeterId != null) {
1478 treatmentBuilder.meter(upstreamMeterId);
1479 }
1480
1481 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001482 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001483 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001484
1485 builder = install ? builder.permit() : builder.deny();
1486
1487 FilteringObjective igmp = builder
1488 .withKey(Criteria.matchInPort(port))
1489 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1490 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
Gamze Abaka838d8142019-02-21 07:06:55 +00001491 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001492 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001493 .fromApp(appId)
1494 .withPriority(10000)
1495 .add(new ObjectiveContext() {
1496 @Override
1497 public void onSuccess(Objective objective) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001498 log.info("Igmp filter for {} on {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001499 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001500 }
1501
1502 @Override
1503 public void onError(Objective objective, ObjectiveError error) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001504 log.info("Igmp filter for {} on {} failed {} because {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001505 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abaka641fc072018-09-04 09:16:27 +00001506 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001507 }
1508 });
1509
1510 flowObjectiveService.filter(devId, igmp);
1511 }
1512
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001513 /**
Jonathan Hart403372d2018-08-22 11:44:13 -07001514 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
1515 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001516 *
1517 * @param dev Device to look for
1518 */
Jonathan Hart403372d2018-08-22 11:44:13 -07001519 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001520 // we create only for the ones we are master of
1521 if (!mastershipService.isLocalMaster(dev.id())) {
Gamze Abaka641fc072018-09-04 09:16:27 +00001522 return;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001523 }
1524 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001525 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Jonathan Hart403372d2018-08-22 11:44:13 -07001526 log.debug("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001527
1528 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -07001529 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001530 for (Port p : deviceService.getPorts(dev.id())) {
1531 if (isUniPort(dev, p)) {
Gamze Abakada282b42019-03-11 13:16:48 +00001532 processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1533 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Jonathan Hart403372d2018-08-22 11:44:13 -07001534 } else {
1535 processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001536 }
1537 }
1538 }
1539 }
1540
Jonathan Hart403372d2018-08-22 11:44:13 -07001541
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001542 /**
1543 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001544 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001545 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1546 * this logic needs to be changed
1547 *
1548 * @param dev Device to look for
1549 * @return The uplink Port of the OLT
1550 */
1551 private Port getUplinkPort(Device dev) {
1552 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001553 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Saurav Daseae48de2019-06-19 13:26:15 -07001554 log.trace("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001555 if (deviceInfo == null) {
1556 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
1557 + " info", dev.id());
1558 return null;
1559 }
1560 // Return the port that has been configured as the uplink port of this OLT in Sadis
Gamze Abakaad329652018-12-20 10:12:21 +00001561 for (Port p : deviceService.getPorts(dev.id())) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001562 if (p.number().toLong() == deviceInfo.uplinkPort()) {
Saurav Daseae48de2019-06-19 13:26:15 -07001563 log.trace("getUplinkPort: Found port {}", p);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001564 return p;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001565 }
1566 }
1567
Saurav Daseae48de2019-06-19 13:26:15 -07001568 log.warn("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001569 return null;
1570 }
1571
1572 /**
1573 * Return the subscriber on a port.
1574 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001575 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001576 * @return subscriber if found else null
1577 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001578 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1579 Port port = deviceService.getPort(cp);
1580 checkNotNull(port, "Invalid connect point");
1581 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001582 return subsService.get(portName);
1583 }
1584
Gamze Abakaad329652018-12-20 10:12:21 +00001585 /**
1586 * Write metadata instruction value (metadata) is 8 bytes.
Gamze Abaka838d8142019-02-21 07:06:55 +00001587 * <p>
Gamze Abakaad329652018-12-20 10:12:21 +00001588 * MS 2 bytes: C Tag
1589 * Next 2 bytes: Technology Profile Id
1590 * Next 4 bytes: Port number (uni or nni)
1591 */
1592
1593 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
1594
1595 if (techProfileId == -1) {
1596 techProfileId = DEFAULT_TP_ID;
1597 }
1598
1599 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
1600 }
1601
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001602 private boolean isUniPort(Device d, Port p) {
1603 Port ulPort = getUplinkPort(d);
1604 if (ulPort != null) {
1605 return (ulPort.number().toLong() != p.number().toLong());
1606 }
1607 return false;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001608 }
1609
Jonathan Hart4c538002018-08-23 10:11:54 -07001610 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1611 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001612 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001613 }
1614
Gamze Abaka33feef52019-02-27 08:16:47 +00001615 private MeterId getMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfile) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001616 if (bpInfoToMeter.get(bandwidthProfile) == null) {
Saurav Daseae48de2019-06-19 13:26:15 -07001617 log.warn("Bandwidth Profile '{}' is not currently mapped to a meter",
1618 bandwidthProfile);
Gamze Abaka33feef52019-02-27 08:16:47 +00001619 return null;
1620 }
1621
1622 Optional<MeterKey> meterKeyForDevice = bpInfoToMeter.get(bandwidthProfile)
1623 .stream()
1624 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1625 .findFirst();
Saurav Daseae48de2019-06-19 13:26:15 -07001626 if (meterKeyForDevice.isPresent()) {
1627 log.debug("Found meter {} for bandwidth profile {}",
1628 meterKeyForDevice.get().meterId(), bandwidthProfile);
1629 return meterKeyForDevice.get().meterId();
1630 } else {
1631 log.warn("Bandwidth profile '{}' is not currently mapped to a meter",
1632 bandwidthProfile);
1633 return null;
1634 }
Gamze Abaka33feef52019-02-27 08:16:47 +00001635 }
1636
1637 private void addMeterIdToBpMapping(DeviceId deviceId, MeterId meterId, String bandwidthProfile) {
1638
1639 if (bpInfoToMeter.get(bandwidthProfile) == null) {
1640 bpInfoToMeter.put(bandwidthProfile,
1641 new ArrayList<>(Arrays.asList(MeterKey.key(deviceId, meterId))));
1642 } else {
Gamze Abaka33feef52019-02-27 08:16:47 +00001643 List<MeterKey> meterKeyListForBp = bpInfoToMeter.get(bandwidthProfile);
1644 meterKeyListForBp.add(MeterKey.key(deviceId, meterId));
1645 }
1646 }
1647
Saurav Das747a7da2019-06-27 12:28:06 -07001648 /**
1649 * Removes meter key from programmed meters. If the meter has no reference
1650 * count in the data plane and is not present in the programmed-meters store
1651 * it will be removed.
1652 *
1653 * @param deviceId device identifier
1654 * @param bandwidthProfileId name of bandwidth profile
1655 */
1656 private void removeMeterIdFromPrgMeters(DeviceId deviceId,
1657 String bandwidthProfileId) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001658 List<MeterKey> meterKeysForBp = bpInfoToMeter.get(bandwidthProfileId);
1659 if (meterKeysForBp != null) {
1660 meterKeysForBp.stream()
1661 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1662 .findFirst().ifPresent(mk -> {
Gamze Abaka33feef52019-02-27 08:16:47 +00001663 programmedMeters.remove(mk);
1664 });
1665 }
1666 }
1667
alshabibf0e7e702015-05-30 18:22:36 -07001668 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001669 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1670
alshabibf0e7e702015-05-30 18:22:36 -07001671 @Override
1672 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001673 eventExecutor.execute(() -> {
1674 DeviceId devId = event.subject().id();
1675 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001676 Port port = event.port();
Jonathan Hart4c538002018-08-23 10:11:54 -07001677
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001678 if (event.type() == DeviceEvent.Type.PORT_STATS_UPDATED) {
1679 return;
1680 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001681
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001682 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001683 // it's possible that we got an event for a previously
1684 // programmed OLT that is no longer available in SADIS
1685 // we let such events go through
1686 if (!programmedDevices.contains(devId)) {
1687 log.warn("No device info found for {}, this is either "
1688 + "not an OLT or not known to sadis", dev);
1689 return;
1690 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001691 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001692
Saurav Daseae48de2019-06-19 13:26:15 -07001693 log.debug("OLT got {} event for {}: {}", event.type(),
1694 event.subject().id(), event);
Jonathan Hart4c538002018-08-23 10:11:54 -07001695
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001696 switch (event.type()) {
1697 //TODO: Port handling and bookkeeping should be improved once
1698 // olt firmware handles correct behaviour.
1699 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001700 if (isUniPort(dev, port)) {
1701 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Gamze Abaka838d8142019-02-21 07:06:55 +00001702 if (port.isEnabled()) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001703 processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Gamze Abakada282b42019-03-11 13:16:48 +00001704 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001705 }
1706 } else {
1707 checkAndCreateDeviceFlows(dev);
1708 }
1709 break;
1710 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001711 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001712 removeSubscriber(new ConnectPoint(devId, port.number()));
Andy Bavier160e8682019-05-07 18:32:22 -07001713 processEapolFilteringObjectives(devId, port.number(), defaultBpId,
1714 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
1715
Gamze Abaka838d8142019-02-21 07:06:55 +00001716 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001717 }
1718
1719 break;
1720 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001721 if (!isUniPort(dev, port)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001722 break;
1723 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001724
Gamze Abakada282b42019-03-11 13:16:48 +00001725 SubscriberAndDeviceInformation sub = programmedSubs
1726 .get(new ConnectPoint(devId, port.number()));
1727 VlanId vlanId = sub == null ? VlanId.vlanId(EAPOL_DEFAULT_VLAN) : sub.cTag();
1728
1729 String bpId = getCurrentBandwidthProfile(new ConnectPoint(devId, port.number()));
1730
Gamze Abaka838d8142019-02-21 07:06:55 +00001731 if (port.isEnabled()) {
Gamze Abakada282b42019-03-11 13:16:48 +00001732 processEapolFilteringObjectives(devId, port.number(), bpId,
1733 null, vlanId, true);
Gamze Abaka33feef52019-02-27 08:16:47 +00001734
Gamze Abaka838d8142019-02-21 07:06:55 +00001735 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001736 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001737 processEapolFilteringObjectives(devId, port.number(), bpId,
1738 null, vlanId, false);
Gamze Abaka838d8142019-02-21 07:06:55 +00001739 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001740 }
alshabibbb83aa22016-02-10 15:08:23 -08001741 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001742 case DEVICE_ADDED:
alshabib7c190012016-02-09 18:22:33 -08001743 post(new AccessDeviceEvent(
1744 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1745 null, null));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001746 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001747 // Send UNI_ADDED events for all existing ports
1748 deviceService.getPorts(devId).stream()
1749 .filter(p -> isUniPort(dev, p))
1750 .filter(Port::isEnabled)
1751 .forEach(p -> post(new AccessDeviceEvent(
1752 AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
1753
Jonathan Hart403372d2018-08-22 11:44:13 -07001754 checkAndCreateDeviceFlows(dev);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001755 break;
1756 case DEVICE_REMOVED:
1757 deviceService.getPorts(devId).stream()
1758 .filter(p -> isUniPort(dev, p))
1759 .forEach(p -> post(new AccessDeviceEvent(
1760 AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001761 programmedDevices.remove(devId);
Gamze Abakada282b42019-03-11 13:16:48 +00001762 removeAllSubscribers(devId);
alshabib7c190012016-02-09 18:22:33 -08001763 post(new AccessDeviceEvent(
1764 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1765 null, null));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001766 break;
1767 case DEVICE_AVAILABILITY_CHANGED:
1768 if (deviceService.isAvailable(devId)) {
1769 post(new AccessDeviceEvent(
1770 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1771 null, null));
Gamze Abakada282b42019-03-11 13:16:48 +00001772 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001773 checkAndCreateDeviceFlows(dev);
1774 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001775 programmedDevices.remove(devId);
1776 removeAllSubscribers(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001777 post(new AccessDeviceEvent(
1778 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1779 null, null));
1780 }
1781 break;
1782 case DEVICE_UPDATED:
1783 case DEVICE_SUSPENDED:
1784 case PORT_STATS_UPDATED:
1785 default:
1786 return;
1787 }
1788 });
alshabibf0e7e702015-05-30 18:22:36 -07001789 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001790
1791 private String getCurrentBandwidthProfile(ConnectPoint connectPoint) {
1792 SubscriberAndDeviceInformation sub = programmedSubs.get(connectPoint);
1793 if (sub != null) {
1794 return sub.upstreamBandwidthProfile();
1795 }
1796 return defaultBpId;
1797 }
Gamze Abakada282b42019-03-11 13:16:48 +00001798
1799 private void removeAllSubscribers(DeviceId deviceId) {
1800 List<ConnectPoint> connectPoints = programmedSubs.keySet().stream()
1801 .filter(ks -> Objects.equals(ks.deviceId(), deviceId))
1802 .collect(Collectors.toList());
1803
1804 connectPoints.forEach(cp -> programmedSubs.remove(cp));
1805 }
alshabibf0e7e702015-05-30 18:22:36 -07001806 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001807
1808 private class InternalMeterListener implements MeterListener {
1809
1810 @Override
1811 public void event(MeterEvent meterEvent) {
1812 if (deleteMeters && MeterEvent.Type.METER_REFERENCE_COUNT_ZERO.equals(meterEvent.type())) {
Saurav Daseae48de2019-06-19 13:26:15 -07001813 log.debug("Zero Count Meter {} received", meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001814 Meter meter = meterEvent.subject();
Gamze Abaka33feef52019-02-27 08:16:47 +00001815 if (meter != null && appId.equals(meter.appId()) &&
1816 !programmedMeters.contains(MeterKey.key(meter.deviceId(), meter.id()))) {
Saurav Daseae48de2019-06-19 13:26:15 -07001817 log.info("Deleting unreferenced, no longer programmed Meter {}",
1818 meter.id());
Gamze Abaka641fc072018-09-04 09:16:27 +00001819 deleteMeter(meter.deviceId(), meter.id());
1820 }
1821 } else if (MeterEvent.Type.METER_REMOVED.equals(meterEvent.type())) {
Saurav Daseae48de2019-06-19 13:26:15 -07001822 log.debug("Meter removed event is received. Meter is {}",
1823 meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001824 removeMeterFromBpMap(meterEvent.subject());
1825 }
1826 }
1827
1828 private void deleteMeter(DeviceId deviceId, MeterId meterId) {
1829 Meter meter = meterService.getMeter(deviceId, meterId);
Gamze Abaka838d8142019-02-21 07:06:55 +00001830 if (meter != null) {
1831 MeterRequest meterRequest = DefaultMeterRequest.builder()
1832 .withBands(meter.bands())
1833 .withUnit(meter.unit())
1834 .forDevice(deviceId)
1835 .fromApp(appId)
1836 .burst()
1837 .remove();
Gamze Abaka641fc072018-09-04 09:16:27 +00001838
Gamze Abaka838d8142019-02-21 07:06:55 +00001839 meterService.withdraw(meterRequest, meterId);
1840 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001841 }
1842
1843 private void removeMeterFromBpMap(Meter meter) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001844 bpInfoToMeter.values().forEach(meterKeys -> meterKeys.stream()
1845 .filter(meterKey -> (meterKey.deviceId().equals(meter.deviceId()))
1846 && meterKey.meterId().equals(meter.id())).findFirst().
1847 ifPresent(mk -> {
1848 meterKeys.remove(mk);
Gamze Abakada282b42019-03-11 13:16:48 +00001849 programmedMeters.remove(mk);
Gamze Abaka33feef52019-02-27 08:16:47 +00001850 log.info("Deleted from the internal map. MeterKey {}", mk);
1851 log.info("Programmed meters {}", programmedMeters);
1852 }));
Gamze Abaka641fc072018-09-04 09:16:27 +00001853 }
1854 }
Hardik Windlass395ff372019-06-13 05:16:00 +00001855}