blob: 9717bad3a650adee2c26a68f13e917df0d015706 [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
alshabibbf23a1f2016-01-14 17:27:11 -080018import com.google.common.collect.Maps;
alshabibf0e7e702015-05-30 18:22:36 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
alshabibe0559672016-02-21 14:49:51 -080022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
alshabibf0e7e702015-05-30 18:22:36 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070026import org.apache.felix.scr.annotations.Service;
alshabibdec2e252016-01-15 12:20:25 -080027import org.onlab.packet.EthType;
Amit Ghosh95e2f652017-08-23 12:49:46 +010028import org.onlab.packet.IPv4;
29import org.onlab.packet.TpPort;
alshabibf0e7e702015-05-30 18:22:36 -070030import org.onlab.packet.VlanId;
Amit Ghosh95e2f652017-08-23 12:49:46 +010031import org.onlab.util.Tools;
alshabibe0559672016-02-21 14:49:51 -080032import org.onosproject.cfg.ComponentConfigService;
alshabibf0e7e702015-05-30 18:22:36 -070033import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080035import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080036import org.onosproject.mastership.MastershipService;
Jonathan Harte533a422015-10-20 17:31:24 -070037import org.onosproject.net.ConnectPoint;
alshabibf0e7e702015-05-30 18:22:36 -070038import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080039import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070040import org.onosproject.net.PortNumber;
Jonathan Harte533a422015-10-20 17:31:24 -070041import org.onosproject.net.config.ConfigFactory;
42import org.onosproject.net.config.NetworkConfigEvent;
43import org.onosproject.net.config.NetworkConfigListener;
44import org.onosproject.net.config.NetworkConfigRegistry;
45import org.onosproject.net.config.basics.SubjectFactories;
alshabibf0e7e702015-05-30 18:22:36 -070046import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.flow.DefaultTrafficSelector;
50import org.onosproject.net.flow.DefaultTrafficTreatment;
51import org.onosproject.net.flow.TrafficSelector;
52import org.onosproject.net.flow.TrafficTreatment;
alshabibdec2e252016-01-15 12:20:25 -080053import org.onosproject.net.flow.criteria.Criteria;
54import org.onosproject.net.flowobjective.DefaultFilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onosproject.net.flowobjective.DefaultForwardingObjective;
alshabibdec2e252016-01-15 12:20:25 -080056import org.onosproject.net.flowobjective.FilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070057import org.onosproject.net.flowobjective.FlowObjectiveService;
58import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080059import org.onosproject.net.flowobjective.Objective;
60import org.onosproject.net.flowobjective.ObjectiveContext;
61import org.onosproject.net.flowobjective.ObjectiveError;
Jonathan Hart94b90492018-04-24 14:02:25 -070062import org.onosproject.store.serializers.KryoNamespaces;
63import org.onosproject.store.service.Serializer;
64import org.onosproject.store.service.StorageService;
alshabibf3a573e2016-06-01 17:39:10 -070065import org.opencord.cordconfig.access.AccessDeviceConfig;
66import org.opencord.cordconfig.access.AccessDeviceData;
alshabib36a4d732016-06-01 16:03:59 -070067import org.opencord.olt.AccessDeviceEvent;
68import org.opencord.olt.AccessDeviceListener;
69import org.opencord.olt.AccessDeviceService;
alshabibe0559672016-02-21 14:49:51 -080070import org.osgi.service.component.ComponentContext;
alshabibf0e7e702015-05-30 18:22:36 -070071import org.slf4j.Logger;
72
Jonathan Hartfd6c1b32016-03-08 14:09:09 -080073import java.util.Collection;
alshabibe0559672016-02-21 14:49:51 -080074import java.util.Dictionary;
alshabibbb83aa22016-02-10 15:08:23 -080075import java.util.List;
Jonathan Harte533a422015-10-20 17:31:24 -070076import java.util.Map;
Jonathan Hart52998382015-11-10 16:09:22 -080077import java.util.Optional;
alshabibe0559672016-02-21 14:49:51 -080078import java.util.Properties;
alshabib3ea82642016-01-12 18:06:53 -080079import java.util.concurrent.CompletableFuture;
Jonathan Harte533a422015-10-20 17:31:24 -070080import java.util.concurrent.ConcurrentHashMap;
alshabib3ea82642016-01-12 18:06:53 -080081import java.util.concurrent.ExecutorService;
82import java.util.concurrent.Executors;
alshabibf0e7e702015-05-30 18:22:36 -070083
Jonathan Hart94b90492018-04-24 14:02:25 -070084import static com.google.common.base.Preconditions.checkNotNull;
alshabibe0559672016-02-21 14:49:51 -080085import static com.google.common.base.Strings.isNullOrEmpty;
86import static org.onlab.util.Tools.get;
alshabib3ea82642016-01-12 18:06:53 -080087import static org.onlab.util.Tools.groupedThreads;
alshabibf0e7e702015-05-30 18:22:36 -070088import static org.slf4j.LoggerFactory.getLogger;
89
90/**
Jonathan Harte533a422015-10-20 17:31:24 -070091 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -070092 */
Jonathan Harte533a422015-10-20 17:31:24 -070093@Service
alshabibf0e7e702015-05-30 18:22:36 -070094@Component(immediate = true)
alshabib8e4fd2f2016-01-12 15:55:53 -080095public class Olt
96 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
97 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -080098 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -080099
100 private static final short DEFAULT_VLAN = 0;
alshabib4ceaed32016-03-03 18:00:58 -0800101 private static final String SUBSCRIBERS = "existing-subscribers";
alshabibe0559672016-02-21 14:49:51 -0800102
alshabibf0e7e702015-05-30 18:22:36 -0700103 private final Logger log = getLogger(getClass());
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected FlowObjectiveService flowObjectiveService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib09753b52016-03-04 14:55:19 -0800109 protected MastershipService mastershipService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibf0e7e702015-05-30 18:22:36 -0700112 protected DeviceService deviceService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected CoreService coreService;
116
Jonathan Harte533a422015-10-20 17:31:24 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected NetworkConfigRegistry networkConfig;
119
alshabibe0559672016-02-21 14:49:51 -0800120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected ComponentConfigService componentConfigService;
122
alshabib4ceaed32016-03-03 18:00:58 -0800123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected StorageService storageService;
alshabibe0559672016-02-21 14:49:51 -0800125
126 @Property(name = "defaultVlan", intValue = DEFAULT_VLAN,
127 label = "Default VLAN RG<->ONU traffic")
128 private int defaultVlan = DEFAULT_VLAN;
129
Matt Jeanneret3f579262018-06-14 17:16:23 -0400130 @Property(name = "enableDhcpOnProvisioning", boolValue = true,
131 label = "Create the DHCP Flow rules when a subscriber is provisioned")
132 protected boolean enableDhcpOnProvisioning = false;
133
134 @Property(name = "enableIgmpOnProvisioning", boolValue = false,
135 label = "Create IGMP Flow rules when a subscriber is provisioned")
136 protected boolean enableIgmpOnProvisioning = false;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100137
alshabibf0e7e702015-05-30 18:22:36 -0700138 private final DeviceListener deviceListener = new InternalDeviceListener();
139
140 private ApplicationId appId;
141
alshabib3ea82642016-01-12 18:06:53 -0800142 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
alshabibbf23a1f2016-01-14 17:27:11 -0800143 groupedThreads("onos/olt-service",
144 "olt-installer-%d"));
alshabib4ae2b402015-06-05 14:55:24 -0700145
Jonathan Harte533a422015-10-20 17:31:24 -0700146 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
147
alshabib4ceaed32016-03-03 18:00:58 -0800148 private Map<ConnectPoint, VlanId> subscribers;
alshabibbf23a1f2016-01-14 17:27:11 -0800149
Jonathan Harte533a422015-10-20 17:31:24 -0700150 private InternalNetworkConfigListener configListener =
151 new InternalNetworkConfigListener();
152 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
153 AccessDeviceConfig.class;
154
155 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
156 new ConfigFactory<DeviceId, AccessDeviceConfig>(
157 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
alshabibbf23a1f2016-01-14 17:27:11 -0800158 @Override
159 public AccessDeviceConfig createConfig() {
160 return new AccessDeviceConfig();
161 }
162 };
163
alshabibf0e7e702015-05-30 18:22:36 -0700164
165 @Activate
alshabibe0559672016-02-21 14:49:51 -0800166 public void activate(ComponentContext context) {
167 modified(context);
Charles Chan54f110f2017-01-20 11:22:42 -0800168 appId = coreService.registerApplication(APP_NAME);
alshabibe0559672016-02-21 14:49:51 -0800169 componentConfigService.registerProperties(getClass());
alshabibc4dfe852015-06-05 13:35:13 -0700170
alshabib8e4fd2f2016-01-12 15:55:53 -0800171 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
172
Jonathan Harte533a422015-10-20 17:31:24 -0700173 networkConfig.registerConfigFactory(configFactory);
174 networkConfig.addListener(configListener);
175
alshabibdec2e252016-01-15 12:20:25 -0800176
Jonathan Harte533a422015-10-20 17:31:24 -0700177 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
178 subject -> {
179 AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class);
180 if (config != null) {
181 AccessDeviceData data = config.getOlt();
182 oltData.put(data.deviceId(), data);
183 }
184 }
185 );
186
alshabibdec2e252016-01-15 12:20:25 -0800187 oltData.keySet().stream()
188 .flatMap(did -> deviceService.getPorts(did).stream())
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700189 .filter(this::isUni)
190 .forEach(this::initializeUni);
alshabibdec2e252016-01-15 12:20:25 -0800191
alshabib4ceaed32016-03-03 18:00:58 -0800192 subscribers = storageService.<ConnectPoint, VlanId>consistentMapBuilder()
193 .withName(SUBSCRIBERS)
194 .withSerializer(Serializer.using(KryoNamespaces.API))
195 .build().asJavaMap();
196
alshabibba357492016-01-27 13:49:46 -0800197 deviceService.addListener(deviceListener);
198
alshabibf0e7e702015-05-30 18:22:36 -0700199 log.info("Started with Application ID {}", appId.id());
200 }
201
202 @Deactivate
203 public void deactivate() {
alshabibe0559672016-02-21 14:49:51 -0800204 componentConfigService.unregisterProperties(getClass(), false);
alshabib62e9ce72016-02-11 17:31:36 -0800205 deviceService.removeListener(deviceListener);
Jonathan Harte533a422015-10-20 17:31:24 -0700206 networkConfig.removeListener(configListener);
207 networkConfig.unregisterConfigFactory(configFactory);
Jonathan Hart5f1c8142018-07-24 17:31:59 -0700208 eventDispatcher.removeSink(AccessDeviceEvent.class);
alshabibf0e7e702015-05-30 18:22:36 -0700209 log.info("Stopped");
210 }
211
alshabibe0559672016-02-21 14:49:51 -0800212 @Modified
213 public void modified(ComponentContext context) {
214 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
215
216 try {
217 String s = get(properties, "defaultVlan");
218 defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100219
Matt Jeanneret3f579262018-06-14 17:16:23 -0400220 Boolean o = Tools.isPropertyEnabled(properties, "enableDhcpOnProvisioning");
Amit Ghosh95e2f652017-08-23 12:49:46 +0100221 if (o != null) {
Matt Jeanneret3f579262018-06-14 17:16:23 -0400222 enableDhcpOnProvisioning = o;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100223 }
Matt Jeanneret3f579262018-06-14 17:16:23 -0400224
225 Boolean p = Tools.isPropertyEnabled(properties, "enableIgmpOnProvisioning");
226 if (p != null) {
227 enableIgmpOnProvisioning = p;
228 }
229
alshabibe0559672016-02-21 14:49:51 -0800230 } catch (Exception e) {
231 defaultVlan = DEFAULT_VLAN;
232 }
233 }
234
alshabib32232c82016-02-25 17:57:24 -0500235 @Override
Jonathan Harte533a422015-10-20 17:31:24 -0700236 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
Jonathan Hart94b90492018-04-24 14:02:25 -0700237 checkNotNull(deviceService.getPort(port.deviceId(), port.port()),
238 "Invalid connect point");
Jonathan Harte533a422015-10-20 17:31:24 -0700239 AccessDeviceData olt = oltData.get(port.deviceId());
240
241 if (olt == null) {
242 log.warn("No data found for OLT device {}", port.deviceId());
Jonathan Hart94b90492018-04-24 14:02:25 -0700243 throw new IllegalArgumentException("Missing OLT configuration for device");
Jonathan Harte533a422015-10-20 17:31:24 -0700244 }
245
Matt Jeanneret3f579262018-06-14 17:16:23 -0400246 if (enableDhcpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100247 processDhcpFilteringObjectives(olt.deviceId(), port.port(), true);
248 }
249
Jonathan Hart52998382015-11-10 16:09:22 -0800250 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
Amit Ghosh95e2f652017-08-23 12:49:46 +0100251 olt.defaultVlan());
252
Matt Jeanneret3f579262018-06-14 17:16:23 -0400253 if (enableIgmpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100254 processIgmpFilteringObjectives(olt.deviceId(), port.port(), true);
255 }
alshabibb7a9e172016-01-13 11:23:53 -0800256 }
257
258 @Override
259 public void removeSubscriber(ConnectPoint port) {
alshabibbf23a1f2016-01-14 17:27:11 -0800260 AccessDeviceData olt = oltData.get(port.deviceId());
261
262 if (olt == null) {
263 log.warn("No data found for OLT device {}", port.deviceId());
264 return;
265 }
266
alshabib4ceaed32016-03-03 18:00:58 -0800267 VlanId subscriberVlan = subscribers.remove(port);
268
269 if (subscriberVlan == null) {
270 log.warn("Unknown subscriber at location {}", port);
271 return;
272 }
273
Matt Jeanneret3f579262018-06-14 17:16:23 -0400274 if (enableDhcpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100275 processDhcpFilteringObjectives(olt.deviceId(), port.port(), false);
276 }
277
alshabib4ceaed32016-03-03 18:00:58 -0800278 unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), subscriberVlan,
279 olt.vlan(), olt.defaultVlan());
alshabibbf23a1f2016-01-14 17:27:11 -0800280
Matt Jeanneret3f579262018-06-14 17:16:23 -0400281 if (enableIgmpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100282 processIgmpFilteringObjectives(olt.deviceId(), port.port(), false);
283 }
alshabibbf23a1f2016-01-14 17:27:11 -0800284 }
285
Amit Ghosh95e2f652017-08-23 12:49:46 +0100286
alshabibe0559672016-02-21 14:49:51 -0800287 @Override
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800288 public Collection<Map.Entry<ConnectPoint, VlanId>> getSubscribers() {
289 return subscribers.entrySet();
290 }
291
292 @Override
alshabibe0559672016-02-21 14:49:51 -0800293 public Map<DeviceId, AccessDeviceData> fetchOlts() {
294 return Maps.newHashMap(oltData);
295 }
296
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700297 private void initializeUni(Port port) {
298 DeviceId deviceId = (DeviceId) port.element().id();
299
300 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, deviceId, port));
301
302 if (port.isEnabled()) {
303 processFilteringObjectives(deviceId, port.number(), true);
304 }
305 }
306
alshabibbf23a1f2016-01-14 17:27:11 -0800307 private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
alshabib4ceaed32016-03-03 18:00:58 -0800308 PortNumber subscriberPort, VlanId subscriberVlan,
309 VlanId deviceVlan, Optional<VlanId> defaultVlan) {
alshabibbf23a1f2016-01-14 17:27:11 -0800310
311 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
312 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
313
alshabib4ceaed32016-03-03 18:00:58 -0800314 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
315 subscriberVlan, deviceVlan,
316 defaultVlan);
317 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
318 subscriberVlan, deviceVlan,
319 defaultVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800320
321
alshabib4ceaed32016-03-03 18:00:58 -0800322 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
323 @Override
324 public void onSuccess(Objective objective) {
325 upFuture.complete(null);
326 }
alshabibbf23a1f2016-01-14 17:27:11 -0800327
alshabib4ceaed32016-03-03 18:00:58 -0800328 @Override
329 public void onError(Objective objective, ObjectiveError error) {
330 upFuture.complete(error);
331 }
332 }));
333
334 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
335 @Override
336 public void onSuccess(Objective objective) {
337 downFuture.complete(null);
338 }
339
340 @Override
341 public void onError(Objective objective, ObjectiveError error) {
342 downFuture.complete(error);
343 }
344 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800345
346 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
347 if (upStatus == null && downStatus == null) {
348 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
349 deviceId,
350 deviceVlan,
351 subscriberVlan));
352 } else if (downStatus != null) {
353 log.error("Subscriber with vlan {} on device {} " +
354 "on port {} failed downstream uninstallation: {}",
355 subscriberVlan, deviceId, subscriberPort, downStatus);
356 } else if (upStatus != null) {
357 log.error("Subscriber with vlan {} on device {} " +
358 "on port {} failed upstream uninstallation: {}",
359 subscriberVlan, deviceId, subscriberPort, upStatus);
360 }
361 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800362
Jonathan Harte533a422015-10-20 17:31:24 -0700363 }
364
365 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
366 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800367 VlanId subscriberVlan, VlanId deviceVlan,
368 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700369
alshabib3ea82642016-01-12 18:06:53 -0800370 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
371 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
372
alshabib4ceaed32016-03-03 18:00:58 -0800373 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
374 subscriberVlan, deviceVlan,
375 defaultVlan);
Jonathan Harte533a422015-10-20 17:31:24 -0700376
377
alshabib4ceaed32016-03-03 18:00:58 -0800378 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
379 subscriberVlan, deviceVlan,
380 defaultVlan);
alshabib3ea82642016-01-12 18:06:53 -0800381
alshabibbf23a1f2016-01-14 17:27:11 -0800382 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800383 subscribers.put(cp, subscriberVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800384
alshabibbf23a1f2016-01-14 17:27:11 -0800385 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
386 @Override
387 public void onSuccess(Objective objective) {
388 upFuture.complete(null);
389 }
390
391 @Override
392 public void onError(Objective objective, ObjectiveError error) {
393 upFuture.complete(error);
394 }
395 }));
396
alshabibbf23a1f2016-01-14 17:27:11 -0800397 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
398 @Override
399 public void onSuccess(Objective objective) {
400 downFuture.complete(null);
401 }
402
403 @Override
404 public void onError(Objective objective, ObjectiveError error) {
405 downFuture.complete(error);
406 }
407 }));
alshabib3ea82642016-01-12 18:06:53 -0800408
409 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
410 if (upStatus == null && downStatus == null) {
411 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
412 deviceId,
413 deviceVlan,
414 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800415
alshabib3ea82642016-01-12 18:06:53 -0800416 } else if (downStatus != null) {
417 log.error("Subscriber with vlan {} on device {} " +
418 "on port {} failed downstream installation: {}",
419 subscriberVlan, deviceId, subscriberPort, downStatus);
420 } else if (upStatus != null) {
421 log.error("Subscriber with vlan {} on device {} " +
422 "on port {} failed upstream installation: {}",
423 subscriberVlan, deviceId, subscriberPort, upStatus);
424 }
425 }, oltInstallers);
426
Jonathan Harte533a422015-10-20 17:31:24 -0700427 }
428
alshabib4ceaed32016-03-03 18:00:58 -0800429 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
430 PortNumber subscriberPort,
431 VlanId subscriberVlan,
432 VlanId deviceVlan,
433 Optional<VlanId> defaultVlan) {
434 TrafficSelector downstream = DefaultTrafficSelector.builder()
435 .matchVlanId(deviceVlan)
436 .matchInPort(uplinkPort)
437 .matchInnerVlanId(subscriberVlan)
438 .build();
439
440 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
441 .popVlan()
442 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
443 .setOutput(subscriberPort)
444 .build();
445
446 return DefaultForwardingObjective.builder()
447 .withFlag(ForwardingObjective.Flag.VERSATILE)
448 .withPriority(1000)
449 .makePermanent()
450 .withSelector(downstream)
451 .fromApp(appId)
452 .withTreatment(downstreamTreatment);
453 }
454
455 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
456 PortNumber subscriberPort,
457 VlanId subscriberVlan,
458 VlanId deviceVlan,
459 Optional<VlanId> defaultVlan) {
460 TrafficSelector upstream = DefaultTrafficSelector.builder()
461 .matchVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
462 .matchInPort(subscriberPort)
463 .build();
464
465
466 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
467 .pushVlan()
468 .setVlanId(subscriberVlan)
469 .pushVlan()
470 .setVlanId(deviceVlan)
471 .setOutput(uplinkPort)
472 .build();
473
474 return DefaultForwardingObjective.builder()
475 .withFlag(ForwardingObjective.Flag.VERSATILE)
476 .withPriority(1000)
477 .makePermanent()
478 .withSelector(upstream)
479 .fromApp(appId)
480 .withTreatment(upstreamTreatment);
481 }
482
alshabib50d9fc52016-02-12 15:47:20 -0800483 private void processFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
alshabib09753b52016-03-04 14:55:19 -0800484 if (!mastershipService.isLocalMaster(devId)) {
485 return;
486 }
alshabibbb83aa22016-02-10 15:08:23 -0800487 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
488
489 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
alshabib50d9fc52016-02-12 15:47:20 -0800490 .withKey(Criteria.matchInPort(port))
alshabibdec2e252016-01-15 12:20:25 -0800491 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
492 .withMeta(DefaultTrafficTreatment.builder()
493 .setOutput(PortNumber.CONTROLLER).build())
494 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -0400495 .withPriority(10000)
alshabibdec2e252016-01-15 12:20:25 -0800496 .add(new ObjectiveContext() {
497 @Override
498 public void onSuccess(Objective objective) {
499 log.info("Eapol filter for {} on {} installed.",
500 devId, port);
501 }
502
503 @Override
504 public void onError(Objective objective, ObjectiveError error) {
505 log.info("Eapol filter for {} on {} failed because {}",
506 devId, port, error);
507 }
508 });
509
alshabibdec2e252016-01-15 12:20:25 -0800510 flowObjectiveService.filter(devId, eapol);
alshabib000b6fc2016-02-01 17:25:00 -0800511
alshabibdec2e252016-01-15 12:20:25 -0800512 }
513
Amit Ghosh95e2f652017-08-23 12:49:46 +0100514 private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
515 if (!mastershipService.isLocalMaster(devId)) {
516 return;
517 }
518 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
519
520 FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny())
521 .withKey(Criteria.matchInPort(port))
522 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
523 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_UDP))
524 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(68)))
525 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(67)))
526 .withMeta(DefaultTrafficTreatment.builder()
527 .setOutput(PortNumber.CONTROLLER).build())
528 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -0400529 .withPriority(10000)
Amit Ghosh95e2f652017-08-23 12:49:46 +0100530 .add(new ObjectiveContext() {
531 @Override
532 public void onSuccess(Objective objective) {
533 log.info("DHCP filter for {} on {} installed.",
534 devId, port);
535 }
536
537 @Override
538 public void onError(Objective objective, ObjectiveError error) {
539 log.info("DHCP filter for {} on {} failed because {}",
540 devId, port, error);
541 }
542 });
543
544 flowObjectiveService.filter(devId, dhcpUpstream);
545 }
546
547 private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
548 if (!mastershipService.isLocalMaster(devId)) {
549 return;
550 }
551
552 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
553
554 builder = install ? builder.permit() : builder.deny();
555
556 FilteringObjective igmp = builder
557 .withKey(Criteria.matchInPort(port))
558 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
559 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
560 .withMeta(DefaultTrafficTreatment.builder()
561 .setOutput(PortNumber.CONTROLLER).build())
562 .fromApp(appId)
563 .withPriority(10000)
564 .add(new ObjectiveContext() {
565 @Override
566 public void onSuccess(Objective objective) {
567 log.info("Igmp filter for {} on {} installed.",
568 devId, port);
569 }
570
571 @Override
572 public void onError(Objective objective, ObjectiveError error) {
573 log.info("Igmp filter for {} on {} failed because {}.",
574 devId, port, error);
575 }
576 });
577
578 flowObjectiveService.filter(devId, igmp);
579 }
580
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700581 private boolean isUni(Port port) {
582 return !oltData.get(port.element().id()).uplink().equals(port.number());
583 }
584
alshabibf0e7e702015-05-30 18:22:36 -0700585 private class InternalDeviceListener implements DeviceListener {
586 @Override
587 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800588 DeviceId devId = event.subject().id();
589 if (!oltData.containsKey(devId)) {
alshabib8e4fd2f2016-01-12 15:55:53 -0800590 return;
591 }
Jonathan Hart6960f822018-07-02 15:19:14 -0700592 if (event.type() != DeviceEvent.Type.PORT_STATS_UPDATED) {
593 log.debug("Olt got {} event for {}", event.type(), event.subject());
594 }
alshabibf0e7e702015-05-30 18:22:36 -0700595 switch (event.type()) {
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800596 //TODO: Port handling and bookkeeping should be improved once
alshabibdec2e252016-01-15 12:20:25 -0800597 // olt firmware handles correct behaviour.
alshabibf0e7e702015-05-30 18:22:36 -0700598 case PORT_ADDED:
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700599 if (!oltData.get(devId).uplink().equals(event.port().number())) {
600 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port()));
601
602 if (event.port().isEnabled()) {
603 processFilteringObjectives(devId, event.port().number(), true);
604 }
alshabibdec2e252016-01-15 12:20:25 -0800605 }
606 break;
607 case PORT_REMOVED:
608 AccessDeviceData olt = oltData.get(devId);
alshabib4ceaed32016-03-03 18:00:58 -0800609 VlanId vlan = subscribers.get(new ConnectPoint(devId,
610 event.port().number()));
dvaddirec63dc822017-10-05 22:25:13 +0530611 if (vlan != null) {
612 unprovisionSubscriber(devId, olt.uplink(),
613 event.port().number(),
614 vlan, olt.vlan(), olt.defaultVlan());
615 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700616 if (!oltData.get(devId).uplink().equals(event.port().number())) {
617 if (event.port().isEnabled()) {
618 processFilteringObjectives(devId, event.port().number(), false);
619 }
620
621 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, event.port()));
alshabibbb83aa22016-02-10 15:08:23 -0800622 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700623
alshabibdec2e252016-01-15 12:20:25 -0800624 break;
alshabibf0e7e702015-05-30 18:22:36 -0700625 case PORT_UPDATED:
alshabibbb83aa22016-02-10 15:08:23 -0800626 if (oltData.get(devId).uplink().equals(event.port().number())) {
627 break;
628 }
629 if (event.port().isEnabled()) {
alshabib50d9fc52016-02-12 15:47:20 -0800630 processFilteringObjectives(devId, event.port().number(), true);
Jonathan Hart6960f822018-07-02 15:19:14 -0700631 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port()));
alshabibbb83aa22016-02-10 15:08:23 -0800632 } else {
alshabib50d9fc52016-02-12 15:47:20 -0800633 processFilteringObjectives(devId, event.port().number(), false);
alshabibbb83aa22016-02-10 15:08:23 -0800634 }
alshabibf0e7e702015-05-30 18:22:36 -0700635 break;
636 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800637 post(new AccessDeviceEvent(
638 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
639 null, null));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700640
641 // Send UNI_ADDED events for all existing ports
642 deviceService.getPorts(devId).stream()
643 .filter(Olt.this::isUni)
Jonathan Hart98de8a72018-05-30 17:47:01 -0700644 .filter(Port::isEnabled)
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700645 .forEach(p -> post(new AccessDeviceEvent(
646 AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
647
alshabibe0559672016-02-21 14:49:51 -0800648 provisionDefaultFlows(devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800649 break;
alshabibf0e7e702015-05-30 18:22:36 -0700650 case DEVICE_REMOVED:
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700651 deviceService.getPorts(devId).stream()
652 .filter(Olt.this::isUni)
653 .forEach(p -> post(new AccessDeviceEvent(
654 AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
655
alshabib8e4fd2f2016-01-12 15:55:53 -0800656 post(new AccessDeviceEvent(
657 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
658 null, null));
659 break;
alshabib7c190012016-02-09 18:22:33 -0800660 case DEVICE_AVAILABILITY_CHANGED:
661 if (deviceService.isAvailable(devId)) {
662 post(new AccessDeviceEvent(
663 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
664 null, null));
665 } else {
666 post(new AccessDeviceEvent(
667 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
668 null, null));
669 }
670 break;
alshabib8e4fd2f2016-01-12 15:55:53 -0800671 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700672 case DEVICE_SUSPENDED:
alshabibf0e7e702015-05-30 18:22:36 -0700673 case PORT_STATS_UPDATED:
674 default:
675 return;
676 }
677 }
678 }
679
Jonathan Harte533a422015-10-20 17:31:24 -0700680 private class InternalNetworkConfigListener implements NetworkConfigListener {
681 @Override
682 public void event(NetworkConfigEvent event) {
683 switch (event.type()) {
684
alshabibbf23a1f2016-01-14 17:27:11 -0800685 case CONFIG_ADDED:
686 case CONFIG_UPDATED:
alshabibe0559672016-02-21 14:49:51 -0800687
688 AccessDeviceConfig config =
689 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
690 if (config != null) {
691 oltData.put(config.getOlt().deviceId(), config.getOlt());
692 provisionDefaultFlows((DeviceId) event.subject());
Jonathan Harte533a422015-10-20 17:31:24 -0700693 }
alshabibe0559672016-02-21 14:49:51 -0800694
alshabibbf23a1f2016-01-14 17:27:11 -0800695 break;
alshabibe0559672016-02-21 14:49:51 -0800696 case CONFIG_REGISTERED:
alshabibbf23a1f2016-01-14 17:27:11 -0800697 case CONFIG_UNREGISTERED:
alshabibe0559672016-02-21 14:49:51 -0800698 break;
alshabibbf23a1f2016-01-14 17:27:11 -0800699 case CONFIG_REMOVED:
alshabibe0559672016-02-21 14:49:51 -0800700 oltData.remove(event.subject());
alshabibbf23a1f2016-01-14 17:27:11 -0800701 default:
702 break;
Jonathan Harte533a422015-10-20 17:31:24 -0700703 }
704 }
alshabibe0559672016-02-21 14:49:51 -0800705
706 @Override
707 public boolean isRelevant(NetworkConfigEvent event) {
708 return event.configClass().equals(CONFIG_CLASS);
709 }
Jonathan Harte533a422015-10-20 17:31:24 -0700710 }
alshabibf0e7e702015-05-30 18:22:36 -0700711
alshabibbb83aa22016-02-10 15:08:23 -0800712 private void provisionDefaultFlows(DeviceId deviceId) {
alshabib09753b52016-03-04 14:55:19 -0800713 if (!mastershipService.isLocalMaster(deviceId)) {
714 return;
715 }
alshabibbb83aa22016-02-10 15:08:23 -0800716 List<Port> ports = deviceService.getPorts(deviceId);
717
718 ports.stream()
719 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
720 .filter(p -> p.isEnabled())
alshabib50d9fc52016-02-12 15:47:20 -0800721 .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(),
722 p.number(), true));
alshabibbb83aa22016-02-10 15:08:23 -0800723
724 }
725
alshabibf0e7e702015-05-30 18:22:36 -0700726}