blob: 28a48d9ae1413e28968a9bda1d9a3f3335c346bb [file] [log] [blame]
alshabibf0e7e702015-05-30 18:22:36 -07001/*
Brian O'Connord6a135a2017-08-03 22:46:05 -07002 * Copyright 2016-present Open Networking Foundation
alshabibf0e7e702015-05-30 18:22:36 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
alshabib36a4d732016-06-01 16:03:59 -070016package org.opencord.olt.impl;
alshabibf0e7e702015-05-30 18:22:36 -070017
Saurav Das82b8e6d2018-10-04 15:25:12 -070018import static com.google.common.base.Preconditions.checkNotNull;
19import static com.google.common.base.Strings.isNullOrEmpty;
20import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
21import static org.onlab.util.Tools.get;
22import static org.onlab.util.Tools.groupedThreads;
23import static org.slf4j.LoggerFactory.getLogger;
24
25import java.util.AbstractMap;
Saurav Das62ad75e2019-03-05 12:22:22 -080026import java.util.Arrays;
Gamze Abaka33feef52019-02-27 08:16:47 +000027import java.util.ArrayList;
Saurav Das82b8e6d2018-10-04 15:25:12 -070028import java.util.Collection;
29import java.util.Dictionary;
30import java.util.List;
31import java.util.Map;
Gamze Abakada282b42019-03-11 13:16:48 +000032import java.util.Objects;
Saurav Das82b8e6d2018-10-04 15:25:12 -070033import java.util.Optional;
34import java.util.Properties;
Gamze Abaka33feef52019-02-27 08:16:47 +000035import java.util.Set;
Saurav Das82b8e6d2018-10-04 15:25:12 -070036import java.util.concurrent.CompletableFuture;
Gamze Abaka33feef52019-02-27 08:16:47 +000037import java.util.concurrent.ConcurrentHashMap;
Saurav Das82b8e6d2018-10-04 15:25:12 -070038import java.util.concurrent.ExecutorService;
39import java.util.concurrent.Executors;
Gamze Abakada282b42019-03-11 13:16:48 +000040import java.util.stream.Collectors;
Saurav Das82b8e6d2018-10-04 15:25:12 -070041
Gamze Abaka33feef52019-02-27 08:16:47 +000042import com.google.common.collect.ImmutableSet;
alshabibf0e7e702015-05-30 18:22:36 -070043import org.apache.felix.scr.annotations.Activate;
44import org.apache.felix.scr.annotations.Component;
45import org.apache.felix.scr.annotations.Deactivate;
alshabibe0559672016-02-21 14:49:51 -080046import org.apache.felix.scr.annotations.Modified;
47import org.apache.felix.scr.annotations.Property;
alshabibf0e7e702015-05-30 18:22:36 -070048import org.apache.felix.scr.annotations.Reference;
49import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070050import org.apache.felix.scr.annotations.Service;
alshabibdec2e252016-01-15 12:20:25 -080051import org.onlab.packet.EthType;
Amit Ghosh95e2f652017-08-23 12:49:46 +010052import org.onlab.packet.IPv4;
Matteo Scandolo63460d12018-11-02 16:19:04 -070053import org.onlab.packet.IPv6;
Amit Ghosh95e2f652017-08-23 12:49:46 +010054import org.onlab.packet.TpPort;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onlab.packet.VlanId;
Amit Ghosh95e2f652017-08-23 12:49:46 +010056import org.onlab.util.Tools;
alshabibe0559672016-02-21 14:49:51 -080057import org.onosproject.cfg.ComponentConfigService;
alshabibf0e7e702015-05-30 18:22:36 -070058import org.onosproject.core.ApplicationId;
59import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080060import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080061import org.onosproject.mastership.MastershipService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010062import org.onosproject.net.AnnotationKeys;
Jonathan Harte533a422015-10-20 17:31:24 -070063import org.onosproject.net.ConnectPoint;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010064import org.onosproject.net.Device;
alshabibf0e7e702015-05-30 18:22:36 -070065import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080066import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070067import org.onosproject.net.PortNumber;
68import org.onosproject.net.device.DeviceEvent;
69import org.onosproject.net.device.DeviceListener;
70import org.onosproject.net.device.DeviceService;
71import org.onosproject.net.flow.DefaultTrafficSelector;
72import org.onosproject.net.flow.DefaultTrafficTreatment;
73import org.onosproject.net.flow.TrafficSelector;
74import org.onosproject.net.flow.TrafficTreatment;
alshabibdec2e252016-01-15 12:20:25 -080075import org.onosproject.net.flow.criteria.Criteria;
76import org.onosproject.net.flowobjective.DefaultFilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070077import org.onosproject.net.flowobjective.DefaultForwardingObjective;
alshabibdec2e252016-01-15 12:20:25 -080078import org.onosproject.net.flowobjective.FilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070079import org.onosproject.net.flowobjective.FlowObjectiveService;
80import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080081import org.onosproject.net.flowobjective.Objective;
82import org.onosproject.net.flowobjective.ObjectiveContext;
83import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das62ad75e2019-03-05 12:22:22 -080084import org.onosproject.store.serializers.KryoNamespaces;
85import org.onosproject.store.service.ConsistentMultimap;
86import org.onosproject.store.service.Serializer;
87import org.onosproject.store.service.StorageService;
Gamze Abaka33feef52019-02-27 08:16:47 +000088import org.onosproject.net.meter.Band;
89import org.onosproject.net.meter.DefaultBand;
90import org.onosproject.net.meter.DefaultMeterRequest;
91import org.onosproject.net.meter.Meter;
92import org.onosproject.net.meter.MeterContext;
93import org.onosproject.net.meter.MeterFailReason;
94import org.onosproject.net.meter.MeterKey;
95import org.onosproject.net.meter.MeterService;
96import org.onosproject.net.meter.MeterListener;
97import org.onosproject.net.meter.MeterRequest;
98import org.onosproject.net.meter.MeterId;
99import org.onosproject.net.meter.MeterEvent;
alshabib36a4d732016-06-01 16:03:59 -0700100import org.opencord.olt.AccessDeviceEvent;
101import org.opencord.olt.AccessDeviceListener;
102import org.opencord.olt.AccessDeviceService;
Amit Ghosh31939522018-08-16 13:28:21 +0100103import org.opencord.olt.AccessSubscriberId;
Gamze Abaka641fc072018-09-04 09:16:27 +0000104import org.opencord.sadis.BandwidthProfileInformation;
105import org.opencord.sadis.BaseInformationService;
106import org.opencord.sadis.SadisService;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100107import org.opencord.sadis.SubscriberAndDeviceInformation;
alshabibe0559672016-02-21 14:49:51 -0800108import org.osgi.service.component.ComponentContext;
alshabibf0e7e702015-05-30 18:22:36 -0700109import org.slf4j.Logger;
110
Saurav Das82b8e6d2018-10-04 15:25:12 -0700111import com.google.common.collect.ImmutableMap;
112import com.google.common.collect.Maps;
Saurav Dasa9d5f442019-03-06 19:32:48 -0800113import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -0700114
115/**
Jonathan Harte533a422015-10-20 17:31:24 -0700116 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -0700117 */
Jonathan Harte533a422015-10-20 17:31:24 -0700118@Service
alshabibf0e7e702015-05-30 18:22:36 -0700119@Component(immediate = true)
alshabib8e4fd2f2016-01-12 15:55:53 -0800120public class Olt
121 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
122 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -0800123 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -0800124
125 private static final short DEFAULT_VLAN = 0;
Gamze Abakada282b42019-03-11 13:16:48 +0000126 private static final short EAPOL_DEFAULT_VLAN = 4091;
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000127 private static final int DEFAULT_TP_ID = 64;
Gamze Abakaad329652018-12-20 10:12:21 +0000128 private static final String DEFAULT_BP_ID = "Default";
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100129 private static final String ADDITIONAL_VLANS = "additional-vlans";
Gamze Abaka838d8142019-02-21 07:06:55 +0000130 private static final String NO_UPLINK_PORT = "No uplink port found for OLT device {}";
131 private static final String INSTALLED = "installed";
132 private static final String REMOVED = "removed";
133 private static final String INSTALLATION = "installation";
134 private static final String REMOVAL = "removal";
alshabibe0559672016-02-21 14:49:51 -0800135
alshabibf0e7e702015-05-30 18:22:36 -0700136 private final Logger log = getLogger(getClass());
137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected FlowObjectiveService flowObjectiveService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib09753b52016-03-04 14:55:19 -0800142 protected MastershipService mastershipService;
143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibf0e7e702015-05-30 18:22:36 -0700145 protected DeviceService deviceService;
146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
148 protected CoreService coreService;
149
Jonathan Harte533a422015-10-20 17:31:24 -0700150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe0559672016-02-21 14:49:51 -0800151 protected ComponentConfigService componentConfigService;
152
alshabib4ceaed32016-03-03 18:00:58 -0800153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka641fc072018-09-04 09:16:27 +0000154 protected SadisService sadisService;
155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
157 protected MeterService meterService;
alshabibe0559672016-02-21 14:49:51 -0800158
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
160 protected StorageService storageService;
161
alshabibe0559672016-02-21 14:49:51 -0800162 @Property(name = "defaultVlan", intValue = DEFAULT_VLAN,
163 label = "Default VLAN RG<->ONU traffic")
164 private int defaultVlan = DEFAULT_VLAN;
165
Matt Jeanneret3f579262018-06-14 17:16:23 -0400166 @Property(name = "enableDhcpOnProvisioning", boolValue = true,
167 label = "Create the DHCP Flow rules when a subscriber is provisioned")
168 protected boolean enableDhcpOnProvisioning = false;
169
Matteo Scandolo63460d12018-11-02 16:19:04 -0700170 @Property(name = "enableDhcpV4", boolValue = true,
171 label = "Enable flows for DHCP v4")
172 protected boolean enableDhcpV4 = true;
173
174 @Property(name = "enableDhcpV6", boolValue = true,
175 label = "Enable flows for DHCP v6")
176 protected boolean enableDhcpV6 = false;
177
Matt Jeanneret3f579262018-06-14 17:16:23 -0400178 @Property(name = "enableIgmpOnProvisioning", boolValue = false,
179 label = "Create IGMP Flow rules when a subscriber is provisioned")
180 protected boolean enableIgmpOnProvisioning = false;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100181
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000182 @Property(name = "deleteMeters", boolValue = true,
Gamze Abaka641fc072018-09-04 09:16:27 +0000183 label = "Deleting Meters based on flow count statistics")
Gamze Abaka1efc80c2019-02-15 12:10:54 +0000184 protected boolean deleteMeters = true;
Gamze Abaka641fc072018-09-04 09:16:27 +0000185
Gamze Abakaad329652018-12-20 10:12:21 +0000186 @Property(name = "defaultTechProfileId", intValue = DEFAULT_TP_ID,
187 label = "Default technology profile id that is used for authentication trap flows")
188 protected int defaultTechProfileId = DEFAULT_TP_ID;
189
190 @Property(name = "defaultBpId", value = DEFAULT_BP_ID,
191 label = "Default bandwidth profile id that is used for authentication trap flows")
192 protected String defaultBpId = DEFAULT_BP_ID;
193
Gamze Abaka33feef52019-02-27 08:16:47 +0000194 @Property(name = "enableEapol", boolValue = true,
195 label = "Send EAPOL authentication trap flows before subscriber provisioning")
196 protected boolean enableEapol = true;
197
alshabibf0e7e702015-05-30 18:22:36 -0700198 private final DeviceListener deviceListener = new InternalDeviceListener();
Gamze Abaka641fc072018-09-04 09:16:27 +0000199 private final MeterListener meterListener = new InternalMeterListener();
alshabibf0e7e702015-05-30 18:22:36 -0700200
201 private ApplicationId appId;
Gamze Abaka641fc072018-09-04 09:16:27 +0000202 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
203 private BaseInformationService<BandwidthProfileInformation> bpService;
alshabibf0e7e702015-05-30 18:22:36 -0700204
Gamze Abaka33feef52019-02-27 08:16:47 +0000205 private Map<String, List<MeterKey>> bpInfoToMeter = new ConcurrentHashMap<>();
Gamze Abaka641fc072018-09-04 09:16:27 +0000206
207 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
208 groupedThreads("onos/olt-service",
209 "olt-installer-%d"));
alshabibf0e7e702015-05-30 18:22:36 -0700210
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100211 private ConsistentMultimap<ConnectPoint, Map.Entry<VlanId, VlanId>> additionalVlans;
212
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700213 protected ExecutorService eventExecutor;
214
Saurav Das82b8e6d2018-10-04 15:25:12 -0700215 private Map<ConnectPoint, SubscriberAndDeviceInformation> programmedSubs;
Gamze Abaka33feef52019-02-27 08:16:47 +0000216 private Set<MeterKey> programmedMeters;
Saurav Das82b8e6d2018-10-04 15:25:12 -0700217
Saurav Dasa9d5f442019-03-06 19:32:48 -0800218
alshabibf0e7e702015-05-30 18:22:36 -0700219 @Activate
alshabibe0559672016-02-21 14:49:51 -0800220 public void activate(ComponentContext context) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -0700221 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt", "events-%d", log));
alshabibe0559672016-02-21 14:49:51 -0800222 modified(context);
Charles Chan54f110f2017-01-20 11:22:42 -0800223 appId = coreService.registerApplication(APP_NAME);
Saurav Das62ad75e2019-03-05 12:22:22 -0800224
225 // ensure that flow rules are purged from flow-store upon olt-disconnection
226 // when olt reconnects, the port-numbers may change for the ONUs
227 // making flows pushed earlier invalid
228 componentConfigService
229 .preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Gamze Abaka33feef52019-02-27 08:16:47 +0000230 "purgeOnDisconnection", "true");
Gamze Abakada282b42019-03-11 13:16:48 +0000231 componentConfigService
232 .preSetProperty("org.onosproject.net.meter.impl.MeterManager",
233 "purgeOnDisconnection", "true");
alshabibe0559672016-02-21 14:49:51 -0800234 componentConfigService.registerProperties(getClass());
Saurav Das82b8e6d2018-10-04 15:25:12 -0700235 programmedSubs = Maps.newConcurrentMap();
Gamze Abaka33feef52019-02-27 08:16:47 +0000236 programmedMeters = ConcurrentHashMap.newKeySet();
alshabibc4dfe852015-06-05 13:35:13 -0700237
alshabib8e4fd2f2016-01-12 15:55:53 -0800238 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
239
Gamze Abaka641fc072018-09-04 09:16:27 +0000240 subsService = sadisService.getSubscriberInfoService();
241 bpService = sadisService.getBandwidthProfileService();
242
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100243 // look for all provisioned devices in Sadis and create EAPOL flows for the
244 // UNI ports
245 Iterable<Device> devices = deviceService.getDevices();
246 for (Device d : devices) {
Jonathan Hart403372d2018-08-22 11:44:13 -0700247 checkAndCreateDeviceFlows(d);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100248 }
alshabib4ceaed32016-03-03 18:00:58 -0800249
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100250 additionalVlans = storageService.<ConnectPoint, Map.Entry<VlanId, VlanId>>consistentMultimapBuilder()
251 .withName(ADDITIONAL_VLANS)
252 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
253 AbstractMap.SimpleEntry.class))
254 .build();
255
alshabibba357492016-01-27 13:49:46 -0800256 deviceService.addListener(deviceListener);
Gamze Abaka641fc072018-09-04 09:16:27 +0000257 meterService.addListener(meterListener);
alshabibba357492016-01-27 13:49:46 -0800258
alshabibf0e7e702015-05-30 18:22:36 -0700259 log.info("Started with Application ID {}", appId.id());
260 }
261
262 @Deactivate
263 public void deactivate() {
alshabibe0559672016-02-21 14:49:51 -0800264 componentConfigService.unregisterProperties(getClass(), false);
alshabib62e9ce72016-02-11 17:31:36 -0800265 deviceService.removeListener(deviceListener);
Gamze Abaka641fc072018-09-04 09:16:27 +0000266 meterService.removeListener(meterListener);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700267 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700268 log.info("Stopped");
269 }
270
alshabibe0559672016-02-21 14:49:51 -0800271 @Modified
272 public void modified(ComponentContext context) {
273 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
274
275 try {
276 String s = get(properties, "defaultVlan");
277 defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100278
Matt Jeanneret3f579262018-06-14 17:16:23 -0400279 Boolean o = Tools.isPropertyEnabled(properties, "enableDhcpOnProvisioning");
Amit Ghosh95e2f652017-08-23 12:49:46 +0100280 if (o != null) {
Matt Jeanneret3f579262018-06-14 17:16:23 -0400281 enableDhcpOnProvisioning = o;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100282 }
Matt Jeanneret3f579262018-06-14 17:16:23 -0400283
Matteo Scandolo63460d12018-11-02 16:19:04 -0700284 Boolean v4 = Tools.isPropertyEnabled(properties, "enableDhcpV4");
285 if (v4 != null) {
286 enableDhcpV4 = v4;
287 }
288
289 Boolean v6 = Tools.isPropertyEnabled(properties, "enableDhcpV6");
290 if (v6 != null) {
291 enableDhcpV6 = v6;
292 }
293
Matt Jeanneret3f579262018-06-14 17:16:23 -0400294 Boolean p = Tools.isPropertyEnabled(properties, "enableIgmpOnProvisioning");
295 if (p != null) {
296 enableIgmpOnProvisioning = p;
297 }
298
Matteo Scandolo63460d12018-11-02 16:19:04 -0700299 log.info("DHCP Settings [enableDhcpOnProvisioning: {}, enableDhcpV4: {}, enableDhcpV6: {}]",
Gamze Abakaad329652018-12-20 10:12:21 +0000300 enableDhcpOnProvisioning, enableDhcpV4, enableDhcpV6);
Matteo Scandolo63460d12018-11-02 16:19:04 -0700301
Gamze Abaka641fc072018-09-04 09:16:27 +0000302 Boolean d = Tools.isPropertyEnabled(properties, "deleteMeters");
303 if (d != null) {
304 deleteMeters = d;
305 }
306
Gamze Abakaad329652018-12-20 10:12:21 +0000307 String tpId = get(properties, "defaultTechProfileId");
308 defaultTechProfileId = isNullOrEmpty(s) ? DEFAULT_TP_ID : Integer.parseInt(tpId.trim());
309
310 String bpId = get(properties, "defaultBpId");
311 defaultBpId = bpId;
312
Gamze Abaka33feef52019-02-27 08:16:47 +0000313 Boolean eap = Tools.isPropertyEnabled(properties, "enableEapol");
314 if (eap != null) {
315 enableEapol = eap;
316 }
317
alshabibe0559672016-02-21 14:49:51 -0800318 } catch (Exception e) {
319 defaultVlan = DEFAULT_VLAN;
320 }
321 }
322
alshabib32232c82016-02-25 17:57:24 -0500323 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000324 public boolean provisionSubscriber(ConnectPoint connectPoint) {
325
326 DeviceId deviceId = connectPoint.deviceId();
327 PortNumber subscriberPortNo = connectPoint.port();
328
329 checkNotNull(deviceService.getPort(deviceId, subscriberPortNo),
Jonathan Hart94b90492018-04-24 14:02:25 -0700330 "Invalid connect point");
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100331 // Find the subscriber on this connect point
Gamze Abaka838d8142019-02-21 07:06:55 +0000332 SubscriberAndDeviceInformation sub = getSubscriber(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100333 if (sub == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000334 log.warn("No subscriber found for {}", connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100335 return false;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100336 }
Jonathan Harte533a422015-10-20 17:31:24 -0700337
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100338 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000339 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100340 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000341 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100342 return false;
Jonathan Harte533a422015-10-20 17:31:24 -0700343 }
344
Gamze Abaka33feef52019-02-27 08:16:47 +0000345 CompletableFuture<ObjectiveError> filterFuture = new CompletableFuture();
346
Gamze Abaka838d8142019-02-21 07:06:55 +0000347 //delete Eapol authentication flow with default bandwidth
Gamze Abaka33feef52019-02-27 08:16:47 +0000348 //wait until Eapol rule with defaultBpId is removed to install subscriber-based rules
Gamze Abakada282b42019-03-11 13:16:48 +0000349 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId, filterFuture,
350 VlanId.vlanId(EAPOL_DEFAULT_VLAN), false);
Gamze Abaka33feef52019-02-27 08:16:47 +0000351 removeMeterIdFromBpMapping(deviceId, defaultBpId);
Gamze Abaka838d8142019-02-21 07:06:55 +0000352
Gamze Abaka33feef52019-02-27 08:16:47 +0000353 //install subscriber flows
354 filterFuture.thenAcceptAsync(filterStatus -> {
355 if (filterStatus == null) {
356 provisionSubscriberBasedFlows(connectPoint, uplinkPort.number(), Optional.empty(), sub);
357 }
358 });
Gamze Abaka838d8142019-02-21 07:06:55 +0000359
Saurav Das82b8e6d2018-10-04 15:25:12 -0700360 // cache subscriber info
Gamze Abaka838d8142019-02-21 07:06:55 +0000361 programmedSubs.put(connectPoint, sub);
Amit Ghosh31939522018-08-16 13:28:21 +0100362 return true;
alshabibb7a9e172016-01-13 11:23:53 -0800363 }
364
365 @Override
Gamze Abaka838d8142019-02-21 07:06:55 +0000366 public boolean removeSubscriber(ConnectPoint connectPoint) {
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800367 // Get the subscriber connected to this port from the local cache
368 // as if we don't know about the subscriber there's no need to remove it
Gamze Abaka838d8142019-02-21 07:06:55 +0000369
370 DeviceId deviceId = connectPoint.deviceId();
371 PortNumber subscriberPortNo = connectPoint.port();
372
373 SubscriberAndDeviceInformation subscriber = programmedSubs.get(connectPoint);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100374 if (subscriber == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000375 log.warn("Subscriber on connectionPoint {} was not previously programmed, " +
376 "no need to remove it", connectPoint);
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800377 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800378 }
379
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100380 // Get the uplink port
Gamze Abaka838d8142019-02-21 07:06:55 +0000381 Port uplinkPort = getUplinkPort(deviceService.getDevice(deviceId));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100382 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000383 log.warn(NO_UPLINK_PORT, deviceId);
Amit Ghosh31939522018-08-16 13:28:21 +0100384 return false;
alshabib4ceaed32016-03-03 18:00:58 -0800385 }
386
Gamze Abaka33feef52019-02-27 08:16:47 +0000387 //delete dhcp & igmp trap flows
388 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100389
Gamze Abaka33feef52019-02-27 08:16:47 +0000390 //process dhcp filtering
391 processDhcpFilteringObjectives(deviceId, subscriberPortNo,
392 upstreamMeterId, subscriber.technologyProfileId(), false, true);
Gamze Abaka838d8142019-02-21 07:06:55 +0000393
Gamze Abaka33feef52019-02-27 08:16:47 +0000394 //process igmp filtering
395 processIgmpFilteringObjectives(deviceId, subscriberPortNo,
396 upstreamMeterId, subscriber.technologyProfileId(), false);
alshabibbf23a1f2016-01-14 17:27:11 -0800397
Gamze Abaka33feef52019-02-27 08:16:47 +0000398 //unprovision vlans
399 unprovisionVlans(deviceId, uplinkPort.number(), subscriberPortNo, subscriber, Optional.empty());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100400
401 // Remove if there are any flows for the additional Vlans
Gamze Abaka838d8142019-02-21 07:06:55 +0000402 Collection<? extends Map.Entry<VlanId, VlanId>> vlansList = additionalVlans.get(connectPoint).value();
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100403
404 // Remove the flows for the additional vlans for this subscriber
405 for (Map.Entry<VlanId, VlanId> vlans : vlansList) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000406 unprovisionTransparentFlows(deviceId, uplinkPort.number(), subscriberPortNo,
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100407 vlans.getValue(), vlans.getKey());
408
409 // Remove it from the map also
Gamze Abaka838d8142019-02-21 07:06:55 +0000410 additionalVlans.remove(connectPoint, vlans);
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100411 }
412
Gamze Abaka33feef52019-02-27 08:16:47 +0000413 //re-install eapol
414 processEapolFilteringObjectives(deviceId, subscriberPortNo,
Gamze Abakada282b42019-03-11 13:16:48 +0000415 subscriber.upstreamBandwidthProfile(), null, subscriber.cTag(), false);
416 processEapolFilteringObjectives(deviceId, subscriberPortNo, defaultBpId,
417 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Gamze Abaka33feef52019-02-27 08:16:47 +0000418
Gamze Abaka838d8142019-02-21 07:06:55 +0000419 programmedSubs.remove(connectPoint);
Amit Ghosh31939522018-08-16 13:28:21 +0100420 return true;
alshabibbf23a1f2016-01-14 17:27:11 -0800421 }
422
Amit Ghosh31939522018-08-16 13:28:21 +0100423 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100424 public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Amit Ghosh31939522018-08-16 13:28:21 +0100425 // Check if we can find the connect point to which this subscriber is connected
426 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
427 if (subsPort == null) {
428 log.warn("ConnectPoint for {} not found", subscriberId);
429 return false;
430 }
431
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100432 if (!sTag.isPresent() && !cTag.isPresent()) {
433 return provisionSubscriber(subsPort);
434 } else if (sTag.isPresent() && cTag.isPresent()) {
435 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
436 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000437 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100438 return false;
439 }
440
441 provisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
442 cTag.get(), sTag.get());
443 return true;
444 } else {
445 log.warn("Provisioning failed for subscriber: {}", subscriberId);
446 return false;
447 }
Amit Ghosh31939522018-08-16 13:28:21 +0100448 }
Amit Ghosh95e2f652017-08-23 12:49:46 +0100449
alshabibe0559672016-02-21 14:49:51 -0800450 @Override
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100451 public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) {
Amit Ghosh31939522018-08-16 13:28:21 +0100452 // Check if we can find the connect point to which this subscriber is connected
453 ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
454 if (subsPort == null) {
455 log.warn("ConnectPoint for {} not found", subscriberId);
456 return false;
457 }
458
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100459 if (!sTag.isPresent() && !cTag.isPresent()) {
460 return removeSubscriber(subsPort);
461 } else if (sTag.isPresent() && cTag.isPresent()) {
462 // Get the uplink port
463 Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId()));
464 if (uplinkPort == null) {
Gamze Abaka838d8142019-02-21 07:06:55 +0000465 log.warn(NO_UPLINK_PORT, subsPort.deviceId());
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100466 return false;
467 }
468
469 unprovisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(),
470 cTag.get(), sTag.get());
471 return true;
472 } else {
473 log.warn("Removing subscriber failed for: {}", subscriberId);
474 return false;
475 }
Amit Ghosh31939522018-08-16 13:28:21 +0100476 }
477
478 @Override
479 public Collection<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> getSubscribers() {
480 ArrayList<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> subs = new ArrayList<>();
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100481
Saurav Das82b8e6d2018-10-04 15:25:12 -0700482 // Get the subscribers for all the devices configured in sadis
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100483 // If the port is UNI, is enabled and exists in Sadis then copy it
484 for (Device d : deviceService.getDevices()) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700485 if (getOltInfo(d) == null) {
486 continue; // not an olt, or not configured in sadis
487 }
Gamze Abakaad329652018-12-20 10:12:21 +0000488 for (Port p : deviceService.getPorts(d.id())) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100489 if (isUniPort(d, p) && p.isEnabled()) {
490 ConnectPoint cp = new ConnectPoint(d.id(), p.number());
491
492 SubscriberAndDeviceInformation sub = getSubscriber(cp);
493 if (sub != null) {
Amit Ghosh31939522018-08-16 13:28:21 +0100494 Map.Entry<VlanId, VlanId> vlans = new AbstractMap.SimpleEntry(sub.sTag(), sub.cTag());
495 subs.add(new AbstractMap.SimpleEntry(cp, vlans));
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100496 }
497 }
498 }
499 }
500
501 return subs;
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800502 }
503
504 @Override
Saurav Das82b8e6d2018-10-04 15:25:12 -0700505 public ImmutableMap<ConnectPoint, SubscriberAndDeviceInformation> getProgSubs() {
506 return ImmutableMap.copyOf(programmedSubs);
507 }
508
509 @Override
Gamze Abaka33feef52019-02-27 08:16:47 +0000510 public ImmutableSet<MeterKey> getProgMeters() {
511 return ImmutableSet.copyOf(programmedMeters);
512 }
513
514 @Override
515 public ImmutableMap<String, List<MeterKey>> getBpMeterMappings() {
516 return ImmutableMap.copyOf(bpInfoToMeter);
517 }
518
519 @Override
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100520 public List<DeviceId> fetchOlts() {
521 // look through all the devices and find the ones that are OLTs as per Sadis
522 List<DeviceId> olts = new ArrayList<>();
523 Iterable<Device> devices = deviceService.getDevices();
524 for (Device d : devices) {
Saurav Das82b8e6d2018-10-04 15:25:12 -0700525 if (getOltInfo(d) != null) {
526 // So this is indeed an OLT device
Amit Ghosh1ed9aef2018-07-17 17:08:16 +0100527 olts.add(d.id());
528 }
529 }
530 return olts;
alshabibe0559672016-02-21 14:49:51 -0800531 }
532
Amit Ghosh31939522018-08-16 13:28:21 +0100533 /**
534 * Finds the connect point to which a subscriber is connected.
535 *
536 * @param id The id of the subscriber, this is the same ID as in Sadis
537 * @return Subscribers ConnectPoint if found else null
538 */
539 private ConnectPoint findSubscriberConnectPoint(String id) {
540
541 Iterable<Device> devices = deviceService.getDevices();
542 for (Device d : devices) {
543 for (Port p : deviceService.getPorts(d.id())) {
544 log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
545 if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
546 log.debug("Found on device {} port {}", d.id(), p.number());
547 return new ConnectPoint(d.id(), p.number());
548 }
549 }
550 }
551 return null;
552 }
553
Gamze Abaka641fc072018-09-04 09:16:27 +0000554 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
555 if (bandwidthProfile == null) {
556 return null;
557 }
558 return bpService.get(bandwidthProfile);
559 }
560
Gamze Abaka838d8142019-02-21 07:06:55 +0000561 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000562 * Removes subscriber vlan flows.
Gamze Abaka838d8142019-02-21 07:06:55 +0000563 *
564 * @param deviceId the device identifier
565 * @param uplink uplink port of the OLT
566 * @param subscriberPort uni port
567 * @param subscriber subscriber info that includes s, c tags, tech profile and bandwidth profile references
568 * @param defaultVlan default vlan of the subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +0000569 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000570 private void unprovisionVlans(DeviceId deviceId, PortNumber uplink,
571 PortNumber subscriberPort, SubscriberAndDeviceInformation subscriber,
572 Optional<VlanId> defaultVlan) {
573
574 log.info("Unprovisioning vlans...");
alshabibbf23a1f2016-01-14 17:27:11 -0800575
576 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
577 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
578
Gamze Abaka641fc072018-09-04 09:16:27 +0000579 VlanId deviceVlan = subscriber.sTag();
580 VlanId subscriberVlan = subscriber.cTag();
581
Gamze Abaka33feef52019-02-27 08:16:47 +0000582 MeterId upstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.upstreamBandwidthProfile());
583 MeterId downstreamMeterId = getMeterIdFromBpMapping(deviceId, subscriber.downstreamBandwidthProfile());
Gamze Abaka641fc072018-09-04 09:16:27 +0000584
alshabib4ceaed32016-03-03 18:00:58 -0800585 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000586 subscriberVlan, deviceVlan,
587 defaultVlan, upstreamMeterId, subscriber.technologyProfileId());
alshabib4ceaed32016-03-03 18:00:58 -0800588 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
Gamze Abaka641fc072018-09-04 09:16:27 +0000589 subscriberVlan, deviceVlan,
590 defaultVlan, downstreamMeterId, subscriber.technologyProfileId());
alshabibbf23a1f2016-01-14 17:27:11 -0800591
alshabib4ceaed32016-03-03 18:00:58 -0800592 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
593 @Override
594 public void onSuccess(Objective objective) {
595 upFuture.complete(null);
596 }
alshabibbf23a1f2016-01-14 17:27:11 -0800597
alshabib4ceaed32016-03-03 18:00:58 -0800598 @Override
599 public void onError(Objective objective, ObjectiveError error) {
600 upFuture.complete(error);
601 }
602 }));
603
604 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
605 @Override
606 public void onSuccess(Objective objective) {
607 downFuture.complete(null);
608 }
609
610 @Override
611 public void onError(Objective objective, ObjectiveError error) {
612 downFuture.complete(error);
613 }
614 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800615
616 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
617 if (upStatus == null && downStatus == null) {
618 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000619 deviceId,
620 deviceVlan,
621 subscriberVlan));
alshabibbf23a1f2016-01-14 17:27:11 -0800622 } else if (downStatus != null) {
623 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000624 "on port {} failed downstream uninstallation: {}",
625 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800626 } else if (upStatus != null) {
627 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000628 "on port {} failed upstream uninstallation: {}",
629 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabibbf23a1f2016-01-14 17:27:11 -0800630 }
631 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800632
Gamze Abaka33feef52019-02-27 08:16:47 +0000633 programmedMeters.remove(MeterKey.key(deviceId, upstreamMeterId));
634 programmedMeters.remove(MeterKey.key(deviceId, downstreamMeterId));
Gamze Abaka838d8142019-02-21 07:06:55 +0000635 log.debug("programmed Meters size {}", programmedMeters.size());
Jonathan Harte533a422015-10-20 17:31:24 -0700636 }
637
Gamze Abaka838d8142019-02-21 07:06:55 +0000638 /**
Gamze Abaka33feef52019-02-27 08:16:47 +0000639 * Adds subscriber vlan flows, dhcp, eapol and igmp trap flows for the related uni port.
Gamze Abaka838d8142019-02-21 07:06:55 +0000640 *
641 * @param port the connection point of the subscriber
642 * @param uplinkPort uplink port of the OLT
643 * @param defaultVlan default vlan of the subscriber
644 * @param sub subscriber information that includes s, c tags, tech profile and bandwidth profile references
Gamze Abaka838d8142019-02-21 07:06:55 +0000645 */
Gamze Abaka33feef52019-02-27 08:16:47 +0000646 private void provisionSubscriberBasedFlows(ConnectPoint port, PortNumber uplinkPort, Optional<VlanId> defaultVlan,
647 SubscriberAndDeviceInformation sub) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000648
649 log.info("Provisioning vlans...");
650
651 DeviceId deviceId = port.deviceId();
652 PortNumber subscriberPort = port.port();
653 VlanId deviceVlan = sub.sTag();
654 VlanId subscriberVlan = sub.cTag();
655 int techProfId = sub.technologyProfileId();
656
657 BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(sub.upstreamBandwidthProfile());
658 BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(sub.downstreamBandwidthProfile());
659
alshabib3ea82642016-01-12 18:06:53 -0800660 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
661 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
Gamze Abaka33feef52019-02-27 08:16:47 +0000662 CompletableFuture<Object> upstreamMeterFuture = new CompletableFuture<>();
663 CompletableFuture<Object> downsteamMeterFuture = new CompletableFuture<>();
alshabib3ea82642016-01-12 18:06:53 -0800664
Gamze Abaka33feef52019-02-27 08:16:47 +0000665 MeterId upstreamMeterId = createMeter(deviceId, upstreamBpInfo, upstreamMeterFuture);
666 MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo, downsteamMeterFuture);
Jonathan Harte533a422015-10-20 17:31:24 -0700667
Gamze Abaka33feef52019-02-27 08:16:47 +0000668 //install upstream flows
669 upstreamMeterFuture.thenAcceptAsync(result -> {
670 if (result == null) {
671 log.info("Upstream Meter {} is sent to the device {}. " +
672 "Sending subscriber flows.", upstreamMeterId, deviceId);
673 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
674 subscriberVlan, deviceVlan,
675 defaultVlan, upstreamMeterId, techProfId);
alshabib3ea82642016-01-12 18:06:53 -0800676
Gamze Abaka33feef52019-02-27 08:16:47 +0000677
678 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
679 @Override
680 public void onSuccess(Objective objective) {
681 upFuture.complete(null);
682 }
683
684 @Override
685 public void onError(Objective objective, ObjectiveError error) {
686 upFuture.complete(error);
687 }
688 }));
689 } else {
690 log.warn("Meter installation error while sending upstream flows. " +
691 "Result {} and MeterId {}", result, upstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800692 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000693 });
alshabibbf23a1f2016-01-14 17:27:11 -0800694
Gamze Abaka33feef52019-02-27 08:16:47 +0000695 //install downstream flows
696 downsteamMeterFuture.thenAcceptAsync(result -> {
697 if (result == null) {
698 log.info("Downstream Meter {} is sent to the device {}. " +
699 "Sending subscriber flows.", downstreamMeterId, deviceId);
700 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
701 subscriberVlan, deviceVlan,
702 defaultVlan, downstreamMeterId, techProfId);
703
704 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
705 @Override
706 public void onSuccess(Objective objective) {
707 downFuture.complete(null);
708 }
709
710 @Override
711 public void onError(Objective objective, ObjectiveError error) {
712 downFuture.complete(error);
713 }
714 }));
715 } else {
716 log.warn("Meter installation error while sending downstream flows. " +
717 "Result {} and MeterId {}", result, downstreamMeterId);
alshabibbf23a1f2016-01-14 17:27:11 -0800718 }
Gamze Abaka33feef52019-02-27 08:16:47 +0000719 });
alshabibbf23a1f2016-01-14 17:27:11 -0800720
Gamze Abaka33feef52019-02-27 08:16:47 +0000721 //send eapol & dhcp & igmp flows
722 //send Subscriber Registered event
alshabib3ea82642016-01-12 18:06:53 -0800723 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
724 if (upStatus == null && downStatus == null) {
Gamze Abaka33feef52019-02-27 08:16:47 +0000725
726 if (upstreamMeterId != null) {
727 //re-install Eapol authentication flow with the subscribers' upstream bandwidth profile
728 processEapolFilteringObjectives(deviceId, subscriberPort, sub.upstreamBandwidthProfile(),
Gamze Abakada282b42019-03-11 13:16:48 +0000729 null, sub.cTag(), true);
Gamze Abaka33feef52019-02-27 08:16:47 +0000730
731 processDhcpFilteringObjectives(deviceId, subscriberPort,
732 upstreamMeterId, sub.technologyProfileId(), true, true);
733
734 processIgmpFilteringObjectives(deviceId, subscriberPort,
735 upstreamMeterId, sub.technologyProfileId(), true);
736 }
737
alshabib3ea82642016-01-12 18:06:53 -0800738 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
Gamze Abaka641fc072018-09-04 09:16:27 +0000739 deviceId,
740 deviceVlan,
741 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800742
alshabib3ea82642016-01-12 18:06:53 -0800743 } else if (downStatus != null) {
744 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000745 "on port {} failed downstream installation: {}",
746 subscriberVlan, deviceId, subscriberPort, downStatus);
alshabib3ea82642016-01-12 18:06:53 -0800747 } else if (upStatus != null) {
748 log.error("Subscriber with vlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000749 "on port {} failed upstream installation: {}",
750 subscriberVlan, deviceId, subscriberPort, upStatus);
alshabib3ea82642016-01-12 18:06:53 -0800751 }
752 }, oltInstallers);
Jonathan Harte533a422015-10-20 17:31:24 -0700753 }
754
Gamze Abaka33feef52019-02-27 08:16:47 +0000755 private MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo,
756 CompletableFuture<Object> meterFuture) {
Gamze Abaka641fc072018-09-04 09:16:27 +0000757 if (bpInfo == null) {
758 log.warn("Bandwidth profile information is not found");
759 return null;
760 }
761
Gamze Abaka33feef52019-02-27 08:16:47 +0000762 MeterId meterId = getMeterIdFromBpMapping(deviceId, bpInfo.id());
Gamze Abaka641fc072018-09-04 09:16:27 +0000763 if (meterId != null) {
764 log.info("Meter is already added. MeterId {}", meterId);
Gamze Abaka33feef52019-02-27 08:16:47 +0000765 meterFuture.complete(null);
Gamze Abaka641fc072018-09-04 09:16:27 +0000766 return meterId;
767 }
768
769 List<Band> meterBands = createMeterBands(bpInfo);
770
771 MeterRequest meterRequest = DefaultMeterRequest.builder()
772 .withBands(meterBands)
773 .withUnit(Meter.Unit.KB_PER_SEC)
Gamze Abaka33feef52019-02-27 08:16:47 +0000774 .withContext(new MeterContext() {
775 @Override
776 public void onSuccess(MeterRequest op) {
777 log.debug("meter addition completed");
778 meterFuture.complete(null);
779 }
780
781 @Override
782 public void onError(MeterRequest op, MeterFailReason reason) {
783 meterFuture.complete(reason);
784 }
785 })
Gamze Abaka641fc072018-09-04 09:16:27 +0000786 .forDevice(deviceId)
787 .fromApp(appId)
788 .burst()
789 .add();
790
791 Meter meter = meterService.submit(meterRequest);
Gamze Abaka33feef52019-02-27 08:16:47 +0000792 addMeterIdToBpMapping(deviceId, meter.id(), bpInfo.id());
Gamze Abaka641fc072018-09-04 09:16:27 +0000793 log.info("Meter is created. Meter Id {}", meter.id());
Gamze Abaka33feef52019-02-27 08:16:47 +0000794 programmedMeters.add(MeterKey.key(deviceId, meter.id()));
Gamze Abaka838d8142019-02-21 07:06:55 +0000795 log.debug("programmed Meters size {}", programmedMeters.size());
Gamze Abaka641fc072018-09-04 09:16:27 +0000796 return meter.id();
797 }
798
799 private List<Band> createMeterBands(BandwidthProfileInformation bpInfo) {
800 List<Band> meterBands = new ArrayList<>();
801
802 meterBands.add(createMeterBand(bpInfo.committedInformationRate(), bpInfo.committedBurstSize()));
803 meterBands.add(createMeterBand(bpInfo.exceededInformationRate(), bpInfo.exceededBurstSize()));
Gamze Abakaad329652018-12-20 10:12:21 +0000804 meterBands.add(createMeterBand(bpInfo.assuredInformationRate(), 0L));
Gamze Abaka641fc072018-09-04 09:16:27 +0000805
Gamze Abaka641fc072018-09-04 09:16:27 +0000806 return meterBands;
807 }
808
809 private Band createMeterBand(long rate, Long burst) {
810 return DefaultBand.builder()
811 .withRate(rate) //already Kbps
812 .burstSize(burst) // already Kbits
813 .ofType(Band.Type.DROP) // no matter
814 .build();
815 }
816
alshabib4ceaed32016-03-03 18:00:58 -0800817 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
818 PortNumber subscriberPort,
819 VlanId subscriberVlan,
820 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000821 Optional<VlanId> defaultVlan,
822 MeterId meterId,
823 int techProfId) {
alshabib4ceaed32016-03-03 18:00:58 -0800824 TrafficSelector downstream = DefaultTrafficSelector.builder()
825 .matchVlanId(deviceVlan)
826 .matchInPort(uplinkPort)
827 .matchInnerVlanId(subscriberVlan)
828 .build();
829
Gamze Abaka641fc072018-09-04 09:16:27 +0000830 TrafficTreatment.Builder downstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800831 .popVlan()
832 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
Gamze Abaka641fc072018-09-04 09:16:27 +0000833 .setOutput(subscriberPort);
834
835 if (meterId != null) {
836 downstreamTreatmentBuilder.meter(meterId);
837 }
838
Gamze Abakaad329652018-12-20 10:12:21 +0000839 downstreamTreatmentBuilder.writeMetadata(createMetadata(subscriberVlan, techProfId, subscriberPort), 0);
alshabib4ceaed32016-03-03 18:00:58 -0800840
841 return DefaultForwardingObjective.builder()
842 .withFlag(ForwardingObjective.Flag.VERSATILE)
843 .withPriority(1000)
844 .makePermanent()
845 .withSelector(downstream)
846 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000847 .withTreatment(downstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800848 }
849
850 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
851 PortNumber subscriberPort,
852 VlanId subscriberVlan,
853 VlanId deviceVlan,
Gamze Abaka641fc072018-09-04 09:16:27 +0000854 Optional<VlanId> defaultVlan,
855 MeterId meterId,
856 int technologyProfileId) {
857
858
859 VlanId dVlan = defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan));
860
861 if (subscriberVlan.toShort() == 4096) {
862 dVlan = subscriberVlan;
863 }
864
alshabib4ceaed32016-03-03 18:00:58 -0800865 TrafficSelector upstream = DefaultTrafficSelector.builder()
Gamze Abaka641fc072018-09-04 09:16:27 +0000866 .matchVlanId(dVlan)
alshabib4ceaed32016-03-03 18:00:58 -0800867 .matchInPort(subscriberPort)
868 .build();
869
870
Gamze Abaka641fc072018-09-04 09:16:27 +0000871 TrafficTreatment.Builder upstreamTreatmentBuilder = DefaultTrafficTreatment.builder()
alshabib4ceaed32016-03-03 18:00:58 -0800872 .pushVlan()
873 .setVlanId(subscriberVlan)
874 .pushVlan()
875 .setVlanId(deviceVlan)
Gamze Abaka641fc072018-09-04 09:16:27 +0000876 .setOutput(uplinkPort);
877
878 if (meterId != null) {
879 upstreamTreatmentBuilder.meter(meterId);
880 }
881
Gamze Abakaad329652018-12-20 10:12:21 +0000882 upstreamTreatmentBuilder.writeMetadata(createMetadata(deviceVlan, technologyProfileId, uplinkPort), 0L);
alshabib4ceaed32016-03-03 18:00:58 -0800883
884 return DefaultForwardingObjective.builder()
885 .withFlag(ForwardingObjective.Flag.VERSATILE)
886 .withPriority(1000)
887 .makePermanent()
888 .withSelector(upstream)
889 .fromApp(appId)
Gamze Abaka641fc072018-09-04 09:16:27 +0000890 .withTreatment(upstreamTreatmentBuilder.build());
alshabib4ceaed32016-03-03 18:00:58 -0800891 }
Gamze Abakaad329652018-12-20 10:12:21 +0000892
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100893 private void provisionTransparentFlows(DeviceId deviceId, PortNumber uplinkPort,
894 PortNumber subscriberPort,
895 VlanId innerVlan,
896 VlanId outerVlan) {
897
898 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
899 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
900
901 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplinkPort, subscriberPort,
902 innerVlan, outerVlan);
903
904
905 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplinkPort, subscriberPort,
906 innerVlan, outerVlan);
907
908 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
909
910 additionalVlans.put(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
911
912 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
913 @Override
914 public void onSuccess(Objective objective) {
915 upFuture.complete(null);
916 }
917
918 @Override
919 public void onError(Objective objective, ObjectiveError error) {
920 upFuture.complete(error);
921 }
922 }));
923
924 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
925 @Override
926 public void onSuccess(Objective objective) {
927 downFuture.complete(null);
928 }
929
930 @Override
931 public void onError(Objective objective, ObjectiveError error) {
932 downFuture.complete(error);
933 }
934 }));
935
936 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
937 if (downStatus != null) {
938 log.error("Flow with innervlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000939 "on port {} failed downstream installation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100940 innerVlan, outerVlan, deviceId, subscriberPort, downStatus);
941 } else if (upStatus != null) {
942 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +0000943 "on port {} failed upstream installation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100944 innerVlan, outerVlan, deviceId, subscriberPort, upStatus);
945 }
946 }, oltInstallers);
947
948 }
949
950 private ForwardingObjective.Builder transparentDownBuilder(PortNumber uplinkPort,
951 PortNumber subscriberPort,
952 VlanId innerVlan,
953 VlanId outerVlan) {
954 TrafficSelector downstream = DefaultTrafficSelector.builder()
955 .matchVlanId(outerVlan)
956 .matchInPort(uplinkPort)
957 .matchInnerVlanId(innerVlan)
958 .build();
959
960 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
961 .setOutput(subscriberPort)
962 .build();
963
964 return DefaultForwardingObjective.builder()
965 .withFlag(ForwardingObjective.Flag.VERSATILE)
966 .withPriority(1000)
967 .makePermanent()
968 .withSelector(downstream)
969 .fromApp(appId)
970 .withTreatment(downstreamTreatment);
971 }
972
973 private ForwardingObjective.Builder transparentUpBuilder(PortNumber uplinkPort,
974 PortNumber subscriberPort,
975 VlanId innerVlan,
976 VlanId outerVlan) {
977 TrafficSelector upstream = DefaultTrafficSelector.builder()
978 .matchVlanId(outerVlan)
979 .matchInPort(subscriberPort)
980 .matchInnerVlanId(innerVlan)
981 .build();
982
983 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
984 .setOutput(uplinkPort)
985 .build();
986
987 return DefaultForwardingObjective.builder()
988 .withFlag(ForwardingObjective.Flag.VERSATILE)
989 .withPriority(1000)
990 .makePermanent()
991 .withSelector(upstream)
992 .fromApp(appId)
993 .withTreatment(upstreamTreatment);
994 }
995
996 private void unprovisionTransparentFlows(DeviceId deviceId, PortNumber uplink,
997 PortNumber subscriberPort, VlanId innerVlan,
998 VlanId outerVlan) {
999
1000 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
1001
1002 additionalVlans.remove(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan));
1003
1004 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
1005 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
1006
1007 ForwardingObjective.Builder upFwd = transparentUpBuilder(uplink, subscriberPort,
1008 innerVlan, outerVlan);
1009 ForwardingObjective.Builder downFwd = transparentDownBuilder(uplink, subscriberPort,
1010 innerVlan, outerVlan);
1011
1012
1013 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
1014 @Override
1015 public void onSuccess(Objective objective) {
1016 upFuture.complete(null);
1017 }
1018
1019 @Override
1020 public void onError(Objective objective, ObjectiveError error) {
1021 upFuture.complete(error);
1022 }
1023 }));
1024
1025 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
1026 @Override
1027 public void onSuccess(Objective objective) {
1028 downFuture.complete(null);
1029 }
1030
1031 @Override
1032 public void onError(Objective objective, ObjectiveError error) {
1033 downFuture.complete(error);
1034 }
1035 }));
1036
1037 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
1038 if (downStatus != null) {
1039 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001040 "on port {} failed downstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001041 innerVlan, outerVlan, deviceId, subscriberPort, downStatus);
1042 } else if (upStatus != null) {
1043 log.error("Flow with innerVlan {} and outerVlan {} on device {} " +
Gamze Abaka641fc072018-09-04 09:16:27 +00001044 "on port {} failed upstream uninstallation: {}",
Amit Ghoshe1d3f092018-10-09 19:44:33 +01001045 innerVlan, outerVlan, deviceId, subscriberPort, upStatus);
1046 }
1047 }, oltInstallers);
1048
1049 }
1050
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001051 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
1052 Port port = deviceService.getPort(devId, portNumber);
1053 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
1054 if (info != null && info.technologyProfileId() != -1) {
1055 return info.technologyProfileId();
1056 }
1057 return defaultTechProfileId;
1058 }
1059
Gamze Abaka838d8142019-02-21 07:06:55 +00001060 /**
Gamze Abakada282b42019-03-11 13:16:48 +00001061 * Returns the write metadata value including tech profile reference and innerVlan.
1062 * For param cVlan, null can be sent
Gamze Abaka838d8142019-02-21 07:06:55 +00001063 *
Gamze Abakada282b42019-03-11 13:16:48 +00001064 * @param cVlan c (customer) tag of one subscriber
Gamze Abaka838d8142019-02-21 07:06:55 +00001065 * @param techProfileId tech profile id of one subscriber
Gamze Abakada282b42019-03-11 13:16:48 +00001066 * @return the write metadata value including tech profile reference and innerVlan
Gamze Abaka838d8142019-02-21 07:06:55 +00001067 */
Gamze Abakada282b42019-03-11 13:16:48 +00001068 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
1069 if (cVlan == null) {
1070 return (long) techProfileId << 32;
1071 }
1072 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
Gamze Abaka838d8142019-02-21 07:06:55 +00001073 }
1074
1075 /**
1076 * Trap eapol authentication packets to the controller.
1077 *
Gamze Abaka33feef52019-02-27 08:16:47 +00001078 * @param devId the device identifier
1079 * @param portNumber the port for which this trap flow is designated
1080 * @param bpId bandwidth profile id to add the related meter to the flow
1081 * @param filterFuture completable future for this filtering objective operation
Gamze Abakada282b42019-03-11 13:16:48 +00001082 * @param vlanId the default or customer tag for a subscriber
Gamze Abaka33feef52019-02-27 08:16:47 +00001083 * @param install true to install the flow, false to remove the flow
Gamze Abaka838d8142019-02-21 07:06:55 +00001084 */
Gamze Abaka33feef52019-02-27 08:16:47 +00001085 private void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
1086 CompletableFuture<ObjectiveError> filterFuture,
Gamze Abakada282b42019-03-11 13:16:48 +00001087 VlanId vlanId, boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001088
1089 if (!enableEapol) {
1090 log.debug("Eapol filtering is disabled.");
1091 if (filterFuture != null) {
1092 filterFuture.complete(null);
1093 }
1094 return;
1095 }
1096
alshabib09753b52016-03-04 14:55:19 -08001097 if (!mastershipService.isLocalMaster(devId)) {
1098 return;
1099 }
alshabibbb83aa22016-02-10 15:08:23 -08001100 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abakaad329652018-12-20 10:12:21 +00001101 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Gamze Abaka33feef52019-02-27 08:16:47 +00001102 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
Gamze Abaka838d8142019-02-21 07:06:55 +00001103 MeterId meterId;
alshabibbb83aa22016-02-10 15:08:23 -08001104
Gamze Abaka838d8142019-02-21 07:06:55 +00001105 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
1106 if (bpInfo != null) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001107 meterId = createMeter(devId, bpInfo, meterFuture);
Gamze Abakaad329652018-12-20 10:12:21 +00001108 treatmentBuilder.meter(meterId);
1109 } else {
Gamze Abaka838d8142019-02-21 07:06:55 +00001110 log.warn("Bandwidth profile {} is not found. Authentication flow will not be installed", bpId);
1111 return;
Gamze Abakaad329652018-12-20 10:12:21 +00001112 }
1113
Gamze Abaka33feef52019-02-27 08:16:47 +00001114 meterFuture.thenAcceptAsync(result -> {
1115 if (result == null) {
1116 log.info("Meter {} for the device {} is installed. " +
1117 "{} EAPOL trap flow", meterId, devId, install ? "Installing " : "Removing ");
1118 int techProfileId = getDefaultTechProfileId(devId, portNumber);
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001119
Gamze Abaka33feef52019-02-27 08:16:47 +00001120 //Authentication trap flow uses only tech profile id as write metadata value
1121 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
1122 .withKey(Criteria.matchInPort(portNumber))
1123 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
Gamze Abakada282b42019-03-11 13:16:48 +00001124 .addCondition(Criteria.matchVlanId(vlanId))
Gamze Abaka33feef52019-02-27 08:16:47 +00001125 .withMeta(treatmentBuilder
Gamze Abakada282b42019-03-11 13:16:48 +00001126 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
Gamze Abaka33feef52019-02-27 08:16:47 +00001127 .setOutput(PortNumber.CONTROLLER).build())
1128 .fromApp(appId)
1129 .withPriority(10000)
1130 .add(new ObjectiveContext() {
1131 @Override
1132 public void onSuccess(Objective objective) {
1133 log.info("Eapol filter for {} on {} {} with meter {}.",
1134 devId, portNumber, (install) ? INSTALLED : REMOVED, meterId);
1135 if (filterFuture != null) {
1136 filterFuture.complete(null);
1137 }
1138 }
alshabibdec2e252016-01-15 12:20:25 -08001139
Gamze Abaka33feef52019-02-27 08:16:47 +00001140 @Override
1141 public void onError(Objective objective, ObjectiveError error) {
1142 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
1143 devId, portNumber, meterId, (install) ? INSTALLATION : REMOVAL,
1144 error);
1145 if (filterFuture != null) {
1146 filterFuture.complete(error);
1147 }
1148 }
1149 });
alshabibdec2e252016-01-15 12:20:25 -08001150
Gamze Abaka33feef52019-02-27 08:16:47 +00001151 flowObjectiveService.filter(devId, eapol);
1152 } else {
1153 log.warn("Meter installation error while sending eapol trap flow. " +
1154 "Result {} and MeterId {}", result, meterId);
1155 }
1156 });
alshabibdec2e252016-01-15 12:20:25 -08001157 }
1158
Jonathan Hart403372d2018-08-22 11:44:13 -07001159 /**
1160 * Installs trap filtering objectives for particular traffic types on an
1161 * NNI port.
1162 *
Gamze Abakaad329652018-12-20 10:12:21 +00001163 * @param devId device ID
1164 * @param port port number
Jonathan Hart403372d2018-08-22 11:44:13 -07001165 * @param install true to install, false to remove
1166 */
1167 private void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
1168 processLldpFilteringObjective(devId, port, install);
Gamze Abaka33feef52019-02-27 08:16:47 +00001169 processDhcpFilteringObjectives(devId, port, null, -1, install, false);
Jonathan Hart403372d2018-08-22 11:44:13 -07001170 }
1171
1172 private void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
1173 if (!mastershipService.isLocalMaster(devId)) {
1174 return;
1175 }
1176 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
1177
1178 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
1179 .withKey(Criteria.matchInPort(port))
1180 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
1181 .withMeta(DefaultTrafficTreatment.builder()
1182 .setOutput(PortNumber.CONTROLLER).build())
1183 .fromApp(appId)
1184 .withPriority(10000)
1185 .add(new ObjectiveContext() {
1186 @Override
1187 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001188 log.info("LLDP filter for device {} on port {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001189 devId, port, (install) ? INSTALLED : REMOVED);
Jonathan Hart403372d2018-08-22 11:44:13 -07001190 }
1191
1192 @Override
1193 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001194 log.info("LLDP filter for device {} on port {} failed {} because {}",
Gamze Abaka838d8142019-02-21 07:06:55 +00001195 devId, port, (install) ? INSTALLATION : REMOVAL,
Saurav Das82b8e6d2018-10-04 15:25:12 -07001196 error);
Jonathan Hart403372d2018-08-22 11:44:13 -07001197 }
1198 });
1199
1200 flowObjectiveService.filter(devId, lldp);
1201
1202 }
1203
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001204 /**
1205 * Trap dhcp packets to the controller.
1206 *
Gamze Abaka838d8142019-02-21 07:06:55 +00001207 * @param devId the device identifier
1208 * @param port the port for which this trap flow is designated
1209 * @param upstreamMeterId the upstream meter id that includes the upstream
1210 * bandwidth profile values such as PIR,CIR. If no meter id needs to be referenced,
1211 * null can be sent
1212 * @param techProfileId the technology profile id that is used to create write
1213 * metadata instruction value. If no tech profile id needs to be referenced,
1214 * -1 can be sent
1215 * @param install true to install the flow, false to remove the flow
1216 * @param upstream true if trapped packets are flowing upstream towards
1217 * server, false if packets are flowing downstream towards client
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001218 */
1219 private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port,
Gamze Abaka838d8142019-02-21 07:06:55 +00001220 MeterId upstreamMeterId,
1221 int techProfileId,
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001222 boolean install,
1223 boolean upstream) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001224
1225 if (!enableDhcpOnProvisioning) {
1226 log.debug("Dhcp provisioning is disabled.");
1227 return;
1228 }
1229
Amit Ghosh95e2f652017-08-23 12:49:46 +01001230 if (!mastershipService.isLocalMaster(devId)) {
1231 return;
1232 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001233
Matteo Scandolo63460d12018-11-02 16:19:04 -07001234 if (enableDhcpV4) {
1235 int udpSrc = (upstream) ? 68 : 67;
1236 int udpDst = (upstream) ? 67 : 68;
1237
1238 EthType ethType = EthType.EtherType.IPV4.ethType();
1239 byte protocol = IPv4.PROTOCOL_UDP;
1240
Gamze Abaka838d8142019-02-21 07:06:55 +00001241 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1242 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001243 }
1244
1245 if (enableDhcpV6) {
1246 int udpSrc = (upstream) ? 547 : 546;
1247 int udpDst = (upstream) ? 546 : 547;
1248
1249 EthType ethType = EthType.EtherType.IPV6.ethType();
1250 byte protocol = IPv6.PROTOCOL_UDP;
1251
Gamze Abaka838d8142019-02-21 07:06:55 +00001252 this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
1253 upstreamMeterId, techProfileId, protocol, install);
Matteo Scandolo63460d12018-11-02 16:19:04 -07001254 }
1255
1256 }
1257
1258 private void addDhcpFilteringObjectives(DeviceId devId,
1259 PortNumber port,
1260 int udpSrc,
1261 int udpDst,
1262 EthType ethType,
Gamze Abaka838d8142019-02-21 07:06:55 +00001263 MeterId upstreamMeterId,
1264 int techProfileId,
Matteo Scandolo63460d12018-11-02 16:19:04 -07001265 byte protocol,
1266 boolean install) {
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001267
1268 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001269 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1270
1271 if (upstreamMeterId != null) {
1272 treatmentBuilder.meter(upstreamMeterId);
1273 }
1274
1275 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001276 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001277 }
1278
Amit Ghosh95e2f652017-08-23 12:49:46 +01001279 FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny())
1280 .withKey(Criteria.matchInPort(port))
Matteo Scandolo63460d12018-11-02 16:19:04 -07001281 .addCondition(Criteria.matchEthType(ethType))
1282 .addCondition(Criteria.matchIPProtocol(protocol))
Saurav Dasacc5eeb2018-10-11 10:58:01 -07001283 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
1284 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Gamze Abaka838d8142019-02-21 07:06:55 +00001285 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001286 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001287 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -04001288 .withPriority(10000)
Amit Ghosh95e2f652017-08-23 12:49:46 +01001289 .add(new ObjectiveContext() {
1290 @Override
1291 public void onSuccess(Objective objective) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001292 log.info("DHCP {} filter for device {} on port {} {}.",
Gamze Abakaad329652018-12-20 10:12:21 +00001293 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001294 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001295 }
1296
1297 @Override
1298 public void onError(Objective objective, ObjectiveError error) {
Matteo Scandolo63460d12018-11-02 16:19:04 -07001299 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
Gamze Abakaad329652018-12-20 10:12:21 +00001300 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6",
Gamze Abaka838d8142019-02-21 07:06:55 +00001301 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abakaad329652018-12-20 10:12:21 +00001302 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001303 }
1304 });
1305
1306 flowObjectiveService.filter(devId, dhcpUpstream);
1307 }
1308
Gamze Abaka838d8142019-02-21 07:06:55 +00001309 private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
1310 MeterId upstreamMeterId,
1311 int techProfileId,
1312 boolean install) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001313
1314 if (enableIgmpOnProvisioning) {
1315 log.debug("Igmp provisioning is disabled.");
1316 return;
1317 }
1318
Amit Ghosh95e2f652017-08-23 12:49:46 +01001319 if (!mastershipService.isLocalMaster(devId)) {
1320 return;
1321 }
1322
Gamze Abaka641fc072018-09-04 09:16:27 +00001323 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
Gamze Abaka838d8142019-02-21 07:06:55 +00001324 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1325
1326 if (upstreamMeterId != null) {
1327 treatmentBuilder.meter(upstreamMeterId);
1328 }
1329
1330 if (techProfileId != -1) {
Gamze Abakada282b42019-03-11 13:16:48 +00001331 treatmentBuilder.writeMetadata(createTechProfValueForWm(null, techProfileId), 0);
Gamze Abaka838d8142019-02-21 07:06:55 +00001332 }
Amit Ghosh95e2f652017-08-23 12:49:46 +01001333
1334 builder = install ? builder.permit() : builder.deny();
1335
1336 FilteringObjective igmp = builder
1337 .withKey(Criteria.matchInPort(port))
1338 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
1339 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
Gamze Abaka838d8142019-02-21 07:06:55 +00001340 .withMeta(treatmentBuilder
Gamze Abaka641fc072018-09-04 09:16:27 +00001341 .setOutput(PortNumber.CONTROLLER).build())
Amit Ghosh95e2f652017-08-23 12:49:46 +01001342 .fromApp(appId)
1343 .withPriority(10000)
1344 .add(new ObjectiveContext() {
1345 @Override
1346 public void onSuccess(Objective objective) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001347 log.info("Igmp filter for {} on {} {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001348 devId, port, (install) ? INSTALLED : REMOVED);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001349 }
1350
1351 @Override
1352 public void onError(Objective objective, ObjectiveError error) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001353 log.info("Igmp filter for {} on {} failed {} because {}.",
Gamze Abaka838d8142019-02-21 07:06:55 +00001354 devId, port, (install) ? INSTALLATION : REMOVAL,
Gamze Abaka641fc072018-09-04 09:16:27 +00001355 error);
Amit Ghosh95e2f652017-08-23 12:49:46 +01001356 }
1357 });
1358
1359 flowObjectiveService.filter(devId, igmp);
1360 }
1361
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001362 /**
Jonathan Hart403372d2018-08-22 11:44:13 -07001363 * Creates trap flows for device, including DHCP and LLDP trap on NNI and
1364 * EAPOL trap on the UNIs, if device is present in Sadis config.
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001365 *
1366 * @param dev Device to look for
1367 */
Jonathan Hart403372d2018-08-22 11:44:13 -07001368 private void checkAndCreateDeviceFlows(Device dev) {
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001369 // we create only for the ones we are master of
1370 if (!mastershipService.isLocalMaster(dev.id())) {
Gamze Abaka641fc072018-09-04 09:16:27 +00001371 return;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001372 }
1373 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001374 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Jonathan Hart403372d2018-08-22 11:44:13 -07001375 log.debug("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001376
1377 if (deviceInfo != null) {
Jonathan Hart403372d2018-08-22 11:44:13 -07001378 // This is an OLT device as per Sadis, we create flows for UNI and NNI ports
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001379 for (Port p : deviceService.getPorts(dev.id())) {
1380 if (isUniPort(dev, p)) {
Gamze Abakada282b42019-03-11 13:16:48 +00001381 processEapolFilteringObjectives(dev.id(), p.number(), defaultBpId, null,
1382 VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Jonathan Hart403372d2018-08-22 11:44:13 -07001383 } else {
1384 processNniFilteringObjectives(dev.id(), p.number(), true);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001385 }
1386 }
1387 }
1388 }
1389
Jonathan Hart403372d2018-08-22 11:44:13 -07001390
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001391 /**
1392 * Get the uplink for of the OLT device.
Gamze Abakaad329652018-12-20 10:12:21 +00001393 * <p>
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001394 * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported
1395 * this logic needs to be changed
1396 *
1397 * @param dev Device to look for
1398 * @return The uplink Port of the OLT
1399 */
1400 private Port getUplinkPort(Device dev) {
1401 // check if this device is provisioned in Sadis
Gamze Abaka641fc072018-09-04 09:16:27 +00001402 SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001403 log.debug("getUplinkPort: deviceInfo {}", deviceInfo);
Saurav Das82b8e6d2018-10-04 15:25:12 -07001404 if (deviceInfo == null) {
1405 log.warn("Device {} is not configured in SADIS .. cannot fetch device"
1406 + " info", dev.id());
1407 return null;
1408 }
1409 // Return the port that has been configured as the uplink port of this OLT in Sadis
Gamze Abakaad329652018-12-20 10:12:21 +00001410 for (Port p : deviceService.getPorts(dev.id())) {
Saurav Das82b8e6d2018-10-04 15:25:12 -07001411 if (p.number().toLong() == deviceInfo.uplinkPort()) {
1412 log.debug("getUplinkPort: Found port {}", p);
1413 return p;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001414 }
1415 }
1416
Gamze Abaka838d8142019-02-21 07:06:55 +00001417 log.debug("getUplinkPort: " + NO_UPLINK_PORT, dev.id());
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001418 return null;
1419 }
1420
1421 /**
1422 * Return the subscriber on a port.
1423 *
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001424 * @param cp ConnectPoint on which to find the subscriber
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001425 * @return subscriber if found else null
1426 */
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001427 SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) {
1428 Port port = deviceService.getPort(cp);
1429 checkNotNull(port, "Invalid connect point");
1430 String portName = port.annotations().value(AnnotationKeys.PORT_NAME);
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001431 return subsService.get(portName);
1432 }
1433
Gamze Abakaad329652018-12-20 10:12:21 +00001434 /**
1435 * Write metadata instruction value (metadata) is 8 bytes.
Gamze Abaka838d8142019-02-21 07:06:55 +00001436 * <p>
Gamze Abakaad329652018-12-20 10:12:21 +00001437 * MS 2 bytes: C Tag
1438 * Next 2 bytes: Technology Profile Id
1439 * Next 4 bytes: Port number (uni or nni)
1440 */
1441
1442 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
1443
1444 if (techProfileId == -1) {
1445 techProfileId = DEFAULT_TP_ID;
1446 }
1447
1448 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
1449 }
1450
Amit Ghosh1ed9aef2018-07-17 17:08:16 +01001451 private boolean isUniPort(Device d, Port p) {
1452 Port ulPort = getUplinkPort(d);
1453 if (ulPort != null) {
1454 return (ulPort.number().toLong() != p.number().toLong());
1455 }
1456 return false;
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001457 }
1458
Jonathan Hart4c538002018-08-23 10:11:54 -07001459 private SubscriberAndDeviceInformation getOltInfo(Device dev) {
1460 String devSerialNo = dev.serialNumber();
Gamze Abaka641fc072018-09-04 09:16:27 +00001461 return subsService.get(devSerialNo);
Jonathan Hart4c538002018-08-23 10:11:54 -07001462 }
1463
Gamze Abaka33feef52019-02-27 08:16:47 +00001464 private MeterId getMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfile) {
1465
1466 if (bpInfoToMeter.get(bandwidthProfile) == null) {
1467 log.warn("Bandwidth Profile '{}' is not found in bandwidth profile map.", bandwidthProfile);
1468 return null;
1469 }
1470
1471 Optional<MeterKey> meterKeyForDevice = bpInfoToMeter.get(bandwidthProfile)
1472 .stream()
1473 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1474 .findFirst();
1475 return meterKeyForDevice.isPresent() ? meterKeyForDevice.get().meterId() : null;
1476 }
1477
1478 private void addMeterIdToBpMapping(DeviceId deviceId, MeterId meterId, String bandwidthProfile) {
1479
1480 if (bpInfoToMeter.get(bandwidthProfile) == null) {
1481 bpInfoToMeter.put(bandwidthProfile,
1482 new ArrayList<>(Arrays.asList(MeterKey.key(deviceId, meterId))));
1483 } else {
1484
1485 List<MeterKey> meterKeyListForBp = bpInfoToMeter.get(bandwidthProfile);
1486 meterKeyListForBp.add(MeterKey.key(deviceId, meterId));
1487 }
1488 }
1489
1490 private void removeMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfileId) {
1491 List<MeterKey> meterKeysForBp = bpInfoToMeter.get(bandwidthProfileId);
1492 if (meterKeysForBp != null) {
1493 meterKeysForBp.stream()
1494 .filter(meterKey -> meterKey.deviceId().equals(deviceId))
1495 .findFirst().ifPresent(mk -> {
1496 meterKeysForBp.remove(mk);
1497 programmedMeters.remove(mk);
1498 });
1499 }
1500 }
1501
alshabibf0e7e702015-05-30 18:22:36 -07001502 private class InternalDeviceListener implements DeviceListener {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001503 private Set<DeviceId> programmedDevices = Sets.newConcurrentHashSet();
1504
alshabibf0e7e702015-05-30 18:22:36 -07001505 @Override
1506 public void event(DeviceEvent event) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001507 eventExecutor.execute(() -> {
1508 DeviceId devId = event.subject().id();
1509 Device dev = event.subject();
Gamze Abaka838d8142019-02-21 07:06:55 +00001510 Port port = event.port();
Jonathan Hart4c538002018-08-23 10:11:54 -07001511
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001512 if (event.type() == DeviceEvent.Type.PORT_STATS_UPDATED) {
1513 return;
1514 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001515
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001516 if (getOltInfo(dev) == null) {
Saurav Dasa9d5f442019-03-06 19:32:48 -08001517 // it's possible that we got an event for a previously
1518 // programmed OLT that is no longer available in SADIS
1519 // we let such events go through
1520 if (!programmedDevices.contains(devId)) {
1521 log.warn("No device info found for {}, this is either "
1522 + "not an OLT or not known to sadis", dev);
1523 return;
1524 }
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001525 }
Jonathan Hart4c538002018-08-23 10:11:54 -07001526
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001527 log.debug("OLT got {} event for {}", event.type(), event.subject());
Jonathan Hart4c538002018-08-23 10:11:54 -07001528
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001529 switch (event.type()) {
1530 //TODO: Port handling and bookkeeping should be improved once
1531 // olt firmware handles correct behaviour.
1532 case PORT_ADDED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001533 if (isUniPort(dev, port)) {
1534 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Jonathan Hart4c538002018-08-23 10:11:54 -07001535
Gamze Abaka838d8142019-02-21 07:06:55 +00001536 if (port.isEnabled()) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001537 processEapolFilteringObjectives(devId, port.number(), defaultBpId,
Gamze Abakada282b42019-03-11 13:16:48 +00001538 null, VlanId.vlanId(EAPOL_DEFAULT_VLAN), true);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001539 }
1540 } else {
1541 checkAndCreateDeviceFlows(dev);
1542 }
1543 break;
1544 case PORT_REMOVED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001545 if (isUniPort(dev, port)) {
Gamze Abaka853bf252019-03-25 10:27:06 +00001546 removeSubscriber(new ConnectPoint(devId, port.number()));
Gamze Abaka838d8142019-02-21 07:06:55 +00001547 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001548 }
1549
1550 break;
1551 case PORT_UPDATED:
Gamze Abaka838d8142019-02-21 07:06:55 +00001552 if (!isUniPort(dev, port)) {
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001553 break;
1554 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001555
Gamze Abakada282b42019-03-11 13:16:48 +00001556 SubscriberAndDeviceInformation sub = programmedSubs
1557 .get(new ConnectPoint(devId, port.number()));
1558 VlanId vlanId = sub == null ? VlanId.vlanId(EAPOL_DEFAULT_VLAN) : sub.cTag();
1559
1560 String bpId = getCurrentBandwidthProfile(new ConnectPoint(devId, port.number()));
1561
Gamze Abaka838d8142019-02-21 07:06:55 +00001562 if (port.isEnabled()) {
Gamze Abakada282b42019-03-11 13:16:48 +00001563 processEapolFilteringObjectives(devId, port.number(), bpId,
1564 null, vlanId, true);
Gamze Abaka33feef52019-02-27 08:16:47 +00001565
Gamze Abaka838d8142019-02-21 07:06:55 +00001566 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, port));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001567 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001568 processEapolFilteringObjectives(devId, port.number(), bpId,
1569 null, vlanId, false);
Gamze Abaka838d8142019-02-21 07:06:55 +00001570 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, port));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -07001571 }
alshabibbb83aa22016-02-10 15:08:23 -08001572 break;
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001573 case DEVICE_ADDED:
alshabib7c190012016-02-09 18:22:33 -08001574 post(new AccessDeviceEvent(
1575 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1576 null, null));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001577 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001578 // Send UNI_ADDED events for all existing ports
1579 deviceService.getPorts(devId).stream()
1580 .filter(p -> isUniPort(dev, p))
1581 .filter(Port::isEnabled)
1582 .forEach(p -> post(new AccessDeviceEvent(
1583 AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
1584
Jonathan Hart403372d2018-08-22 11:44:13 -07001585 checkAndCreateDeviceFlows(dev);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001586 break;
1587 case DEVICE_REMOVED:
1588 deviceService.getPorts(devId).stream()
1589 .filter(p -> isUniPort(dev, p))
1590 .forEach(p -> post(new AccessDeviceEvent(
1591 AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
Saurav Dasa9d5f442019-03-06 19:32:48 -08001592 programmedDevices.remove(devId);
Gamze Abakada282b42019-03-11 13:16:48 +00001593 removeAllSubscribers(devId);
alshabib7c190012016-02-09 18:22:33 -08001594 post(new AccessDeviceEvent(
1595 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1596 null, null));
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001597 break;
1598 case DEVICE_AVAILABILITY_CHANGED:
1599 if (deviceService.isAvailable(devId)) {
1600 post(new AccessDeviceEvent(
1601 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
1602 null, null));
Gamze Abakada282b42019-03-11 13:16:48 +00001603 programmedDevices.add(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001604 checkAndCreateDeviceFlows(dev);
1605 } else {
Gamze Abakada282b42019-03-11 13:16:48 +00001606 programmedDevices.remove(devId);
1607 removeAllSubscribers(devId);
Matteo Scandolo632f0fc2018-09-07 12:21:45 -07001608 post(new AccessDeviceEvent(
1609 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
1610 null, null));
1611 }
1612 break;
1613 case DEVICE_UPDATED:
1614 case DEVICE_SUSPENDED:
1615 case PORT_STATS_UPDATED:
1616 default:
1617 return;
1618 }
1619 });
alshabibf0e7e702015-05-30 18:22:36 -07001620 }
Gamze Abaka838d8142019-02-21 07:06:55 +00001621
1622 private String getCurrentBandwidthProfile(ConnectPoint connectPoint) {
1623 SubscriberAndDeviceInformation sub = programmedSubs.get(connectPoint);
1624 if (sub != null) {
1625 return sub.upstreamBandwidthProfile();
1626 }
1627 return defaultBpId;
1628 }
Gamze Abakada282b42019-03-11 13:16:48 +00001629
1630 private void removeAllSubscribers(DeviceId deviceId) {
1631 List<ConnectPoint> connectPoints = programmedSubs.keySet().stream()
1632 .filter(ks -> Objects.equals(ks.deviceId(), deviceId))
1633 .collect(Collectors.toList());
1634
1635 connectPoints.forEach(cp -> programmedSubs.remove(cp));
1636 }
alshabibf0e7e702015-05-30 18:22:36 -07001637 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001638
1639 private class InternalMeterListener implements MeterListener {
1640
1641 @Override
1642 public void event(MeterEvent meterEvent) {
1643 if (deleteMeters && MeterEvent.Type.METER_REFERENCE_COUNT_ZERO.equals(meterEvent.type())) {
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001644 log.info("Zero Count Meter Event is received. Meter is {}", meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001645 Meter meter = meterEvent.subject();
Gamze Abaka33feef52019-02-27 08:16:47 +00001646 if (meter != null && appId.equals(meter.appId()) &&
1647 !programmedMeters.contains(MeterKey.key(meter.deviceId(), meter.id()))) {
Gamze Abaka641fc072018-09-04 09:16:27 +00001648 deleteMeter(meter.deviceId(), meter.id());
1649 }
1650 } else if (MeterEvent.Type.METER_REMOVED.equals(meterEvent.type())) {
Gamze Abaka1efc80c2019-02-15 12:10:54 +00001651 log.info("Meter removed event is received. Meter is {}", meterEvent.subject());
Gamze Abaka641fc072018-09-04 09:16:27 +00001652 removeMeterFromBpMap(meterEvent.subject());
1653 }
1654 }
1655
1656 private void deleteMeter(DeviceId deviceId, MeterId meterId) {
1657 Meter meter = meterService.getMeter(deviceId, meterId);
Gamze Abaka838d8142019-02-21 07:06:55 +00001658 if (meter != null) {
1659 MeterRequest meterRequest = DefaultMeterRequest.builder()
1660 .withBands(meter.bands())
1661 .withUnit(meter.unit())
1662 .forDevice(deviceId)
1663 .fromApp(appId)
1664 .burst()
1665 .remove();
Gamze Abaka641fc072018-09-04 09:16:27 +00001666
Gamze Abaka838d8142019-02-21 07:06:55 +00001667 meterService.withdraw(meterRequest, meterId);
1668 }
Gamze Abaka641fc072018-09-04 09:16:27 +00001669 }
1670
1671 private void removeMeterFromBpMap(Meter meter) {
Gamze Abaka33feef52019-02-27 08:16:47 +00001672 bpInfoToMeter.values().forEach(meterKeys -> meterKeys.stream()
1673 .filter(meterKey -> (meterKey.deviceId().equals(meter.deviceId()))
1674 && meterKey.meterId().equals(meter.id())).findFirst().
1675 ifPresent(mk -> {
1676 meterKeys.remove(mk);
Gamze Abakada282b42019-03-11 13:16:48 +00001677 programmedMeters.remove(mk);
Gamze Abaka33feef52019-02-27 08:16:47 +00001678 log.info("Deleted from the internal map. MeterKey {}", mk);
1679 log.info("Programmed meters {}", programmedMeters);
1680 }));
Gamze Abaka641fc072018-09-04 09:16:27 +00001681 }
1682 }
1683}