blob: 29f070f9313c3f196611bdb6cc4e1f363b20619e [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
Amit Ghosh95e2f652017-08-23 12:49:46 +0100130 @Property(name = "enableDhcpIgmpOnProvisioning", boolValue = false,
131 label = "Create the DHCP and IGMP Flow rules when a subscriber is provisioned")
132 protected boolean enableDhcpIgmpOnProvisioning = false;
133
alshabibf0e7e702015-05-30 18:22:36 -0700134 private final DeviceListener deviceListener = new InternalDeviceListener();
135
136 private ApplicationId appId;
137
alshabib3ea82642016-01-12 18:06:53 -0800138 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
alshabibbf23a1f2016-01-14 17:27:11 -0800139 groupedThreads("onos/olt-service",
140 "olt-installer-%d"));
alshabib4ae2b402015-06-05 14:55:24 -0700141
Jonathan Harte533a422015-10-20 17:31:24 -0700142 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
143
alshabib4ceaed32016-03-03 18:00:58 -0800144 private Map<ConnectPoint, VlanId> subscribers;
alshabibbf23a1f2016-01-14 17:27:11 -0800145
Jonathan Harte533a422015-10-20 17:31:24 -0700146 private InternalNetworkConfigListener configListener =
147 new InternalNetworkConfigListener();
148 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
149 AccessDeviceConfig.class;
150
151 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
152 new ConfigFactory<DeviceId, AccessDeviceConfig>(
153 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
alshabibbf23a1f2016-01-14 17:27:11 -0800154 @Override
155 public AccessDeviceConfig createConfig() {
156 return new AccessDeviceConfig();
157 }
158 };
159
alshabibf0e7e702015-05-30 18:22:36 -0700160
161 @Activate
alshabibe0559672016-02-21 14:49:51 -0800162 public void activate(ComponentContext context) {
163 modified(context);
Charles Chan54f110f2017-01-20 11:22:42 -0800164 appId = coreService.registerApplication(APP_NAME);
alshabibe0559672016-02-21 14:49:51 -0800165 componentConfigService.registerProperties(getClass());
alshabibc4dfe852015-06-05 13:35:13 -0700166
alshabib8e4fd2f2016-01-12 15:55:53 -0800167 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
168
Jonathan Harte533a422015-10-20 17:31:24 -0700169 networkConfig.registerConfigFactory(configFactory);
170 networkConfig.addListener(configListener);
171
alshabibdec2e252016-01-15 12:20:25 -0800172
Jonathan Harte533a422015-10-20 17:31:24 -0700173 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
174 subject -> {
175 AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class);
176 if (config != null) {
177 AccessDeviceData data = config.getOlt();
178 oltData.put(data.deviceId(), data);
179 }
180 }
181 );
182
alshabibdec2e252016-01-15 12:20:25 -0800183 oltData.keySet().stream()
184 .flatMap(did -> deviceService.getPorts(did).stream())
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700185 .filter(this::isUni)
186 .forEach(this::initializeUni);
alshabibdec2e252016-01-15 12:20:25 -0800187
alshabib4ceaed32016-03-03 18:00:58 -0800188 subscribers = storageService.<ConnectPoint, VlanId>consistentMapBuilder()
189 .withName(SUBSCRIBERS)
190 .withSerializer(Serializer.using(KryoNamespaces.API))
191 .build().asJavaMap();
192
alshabibba357492016-01-27 13:49:46 -0800193 deviceService.addListener(deviceListener);
194
alshabibf0e7e702015-05-30 18:22:36 -0700195 log.info("Started with Application ID {}", appId.id());
196 }
197
198 @Deactivate
199 public void deactivate() {
alshabibe0559672016-02-21 14:49:51 -0800200 componentConfigService.unregisterProperties(getClass(), false);
alshabib62e9ce72016-02-11 17:31:36 -0800201 deviceService.removeListener(deviceListener);
Jonathan Harte533a422015-10-20 17:31:24 -0700202 networkConfig.removeListener(configListener);
203 networkConfig.unregisterConfigFactory(configFactory);
alshabibf0e7e702015-05-30 18:22:36 -0700204 log.info("Stopped");
205 }
206
alshabibe0559672016-02-21 14:49:51 -0800207 @Modified
208 public void modified(ComponentContext context) {
209 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
210
211 try {
212 String s = get(properties, "defaultVlan");
213 defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100214
215 Boolean o = Tools.isPropertyEnabled(properties, "enableDhcpIgmpOnProvisioning");
216 if (o != null) {
217 enableDhcpIgmpOnProvisioning = o;
218 }
alshabibe0559672016-02-21 14:49:51 -0800219 } catch (Exception e) {
220 defaultVlan = DEFAULT_VLAN;
221 }
222 }
223
alshabib32232c82016-02-25 17:57:24 -0500224 @Override
Jonathan Harte533a422015-10-20 17:31:24 -0700225 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
Jonathan Hart94b90492018-04-24 14:02:25 -0700226 checkNotNull(deviceService.getPort(port.deviceId(), port.port()),
227 "Invalid connect point");
Jonathan Harte533a422015-10-20 17:31:24 -0700228 AccessDeviceData olt = oltData.get(port.deviceId());
229
230 if (olt == null) {
231 log.warn("No data found for OLT device {}", port.deviceId());
Jonathan Hart94b90492018-04-24 14:02:25 -0700232 throw new IllegalArgumentException("Missing OLT configuration for device");
Jonathan Harte533a422015-10-20 17:31:24 -0700233 }
234
Amit Ghosh95e2f652017-08-23 12:49:46 +0100235 if (enableDhcpIgmpOnProvisioning) {
236 processDhcpFilteringObjectives(olt.deviceId(), port.port(), true);
237 }
238
Jonathan Hart52998382015-11-10 16:09:22 -0800239 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
Amit Ghosh95e2f652017-08-23 12:49:46 +0100240 olt.defaultVlan());
241
242 if (enableDhcpIgmpOnProvisioning) {
243 processIgmpFilteringObjectives(olt.deviceId(), port.port(), true);
244 }
alshabibb7a9e172016-01-13 11:23:53 -0800245 }
246
247 @Override
248 public void removeSubscriber(ConnectPoint port) {
alshabibbf23a1f2016-01-14 17:27:11 -0800249 AccessDeviceData olt = oltData.get(port.deviceId());
250
251 if (olt == null) {
252 log.warn("No data found for OLT device {}", port.deviceId());
253 return;
254 }
255
alshabib4ceaed32016-03-03 18:00:58 -0800256 VlanId subscriberVlan = subscribers.remove(port);
257
258 if (subscriberVlan == null) {
259 log.warn("Unknown subscriber at location {}", port);
260 return;
261 }
262
Amit Ghosh95e2f652017-08-23 12:49:46 +0100263 if (enableDhcpIgmpOnProvisioning) {
264 processDhcpFilteringObjectives(olt.deviceId(), port.port(), false);
265 }
266
alshabib4ceaed32016-03-03 18:00:58 -0800267 unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), subscriberVlan,
268 olt.vlan(), olt.defaultVlan());
alshabibbf23a1f2016-01-14 17:27:11 -0800269
Amit Ghosh95e2f652017-08-23 12:49:46 +0100270 if (enableDhcpIgmpOnProvisioning) {
271 processIgmpFilteringObjectives(olt.deviceId(), port.port(), false);
272 }
alshabibbf23a1f2016-01-14 17:27:11 -0800273 }
274
Amit Ghosh95e2f652017-08-23 12:49:46 +0100275
alshabibe0559672016-02-21 14:49:51 -0800276 @Override
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800277 public Collection<Map.Entry<ConnectPoint, VlanId>> getSubscribers() {
278 return subscribers.entrySet();
279 }
280
281 @Override
alshabibe0559672016-02-21 14:49:51 -0800282 public Map<DeviceId, AccessDeviceData> fetchOlts() {
283 return Maps.newHashMap(oltData);
284 }
285
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700286 private void initializeUni(Port port) {
287 DeviceId deviceId = (DeviceId) port.element().id();
288
289 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, deviceId, port));
290
291 if (port.isEnabled()) {
292 processFilteringObjectives(deviceId, port.number(), true);
293 }
294 }
295
alshabibbf23a1f2016-01-14 17:27:11 -0800296 private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
alshabib4ceaed32016-03-03 18:00:58 -0800297 PortNumber subscriberPort, VlanId subscriberVlan,
298 VlanId deviceVlan, Optional<VlanId> defaultVlan) {
alshabibbf23a1f2016-01-14 17:27:11 -0800299
300 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
301 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
302
alshabib4ceaed32016-03-03 18:00:58 -0800303 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
304 subscriberVlan, deviceVlan,
305 defaultVlan);
306 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
307 subscriberVlan, deviceVlan,
308 defaultVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800309
310
alshabib4ceaed32016-03-03 18:00:58 -0800311 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
312 @Override
313 public void onSuccess(Objective objective) {
314 upFuture.complete(null);
315 }
alshabibbf23a1f2016-01-14 17:27:11 -0800316
alshabib4ceaed32016-03-03 18:00:58 -0800317 @Override
318 public void onError(Objective objective, ObjectiveError error) {
319 upFuture.complete(error);
320 }
321 }));
322
323 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
324 @Override
325 public void onSuccess(Objective objective) {
326 downFuture.complete(null);
327 }
328
329 @Override
330 public void onError(Objective objective, ObjectiveError error) {
331 downFuture.complete(error);
332 }
333 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800334
335 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
336 if (upStatus == null && downStatus == null) {
337 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
338 deviceId,
339 deviceVlan,
340 subscriberVlan));
341 } else if (downStatus != null) {
342 log.error("Subscriber with vlan {} on device {} " +
343 "on port {} failed downstream uninstallation: {}",
344 subscriberVlan, deviceId, subscriberPort, downStatus);
345 } else if (upStatus != null) {
346 log.error("Subscriber with vlan {} on device {} " +
347 "on port {} failed upstream uninstallation: {}",
348 subscriberVlan, deviceId, subscriberPort, upStatus);
349 }
350 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800351
Jonathan Harte533a422015-10-20 17:31:24 -0700352 }
353
354 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
355 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800356 VlanId subscriberVlan, VlanId deviceVlan,
357 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700358
alshabib3ea82642016-01-12 18:06:53 -0800359 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
360 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
361
alshabib4ceaed32016-03-03 18:00:58 -0800362 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
363 subscriberVlan, deviceVlan,
364 defaultVlan);
Jonathan Harte533a422015-10-20 17:31:24 -0700365
366
alshabib4ceaed32016-03-03 18:00:58 -0800367 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
368 subscriberVlan, deviceVlan,
369 defaultVlan);
alshabib3ea82642016-01-12 18:06:53 -0800370
alshabibbf23a1f2016-01-14 17:27:11 -0800371 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800372 subscribers.put(cp, subscriberVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800373
alshabibbf23a1f2016-01-14 17:27:11 -0800374 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
375 @Override
376 public void onSuccess(Objective objective) {
377 upFuture.complete(null);
378 }
379
380 @Override
381 public void onError(Objective objective, ObjectiveError error) {
382 upFuture.complete(error);
383 }
384 }));
385
alshabibbf23a1f2016-01-14 17:27:11 -0800386 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
387 @Override
388 public void onSuccess(Objective objective) {
389 downFuture.complete(null);
390 }
391
392 @Override
393 public void onError(Objective objective, ObjectiveError error) {
394 downFuture.complete(error);
395 }
396 }));
alshabib3ea82642016-01-12 18:06:53 -0800397
398 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
399 if (upStatus == null && downStatus == null) {
400 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
401 deviceId,
402 deviceVlan,
403 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800404
alshabib3ea82642016-01-12 18:06:53 -0800405 } else if (downStatus != null) {
406 log.error("Subscriber with vlan {} on device {} " +
407 "on port {} failed downstream installation: {}",
408 subscriberVlan, deviceId, subscriberPort, downStatus);
409 } else if (upStatus != null) {
410 log.error("Subscriber with vlan {} on device {} " +
411 "on port {} failed upstream installation: {}",
412 subscriberVlan, deviceId, subscriberPort, upStatus);
413 }
414 }, oltInstallers);
415
Jonathan Harte533a422015-10-20 17:31:24 -0700416 }
417
alshabib4ceaed32016-03-03 18:00:58 -0800418 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
419 PortNumber subscriberPort,
420 VlanId subscriberVlan,
421 VlanId deviceVlan,
422 Optional<VlanId> defaultVlan) {
423 TrafficSelector downstream = DefaultTrafficSelector.builder()
424 .matchVlanId(deviceVlan)
425 .matchInPort(uplinkPort)
426 .matchInnerVlanId(subscriberVlan)
427 .build();
428
429 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
430 .popVlan()
431 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
432 .setOutput(subscriberPort)
433 .build();
434
435 return DefaultForwardingObjective.builder()
436 .withFlag(ForwardingObjective.Flag.VERSATILE)
437 .withPriority(1000)
438 .makePermanent()
439 .withSelector(downstream)
440 .fromApp(appId)
441 .withTreatment(downstreamTreatment);
442 }
443
444 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
445 PortNumber subscriberPort,
446 VlanId subscriberVlan,
447 VlanId deviceVlan,
448 Optional<VlanId> defaultVlan) {
449 TrafficSelector upstream = DefaultTrafficSelector.builder()
450 .matchVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
451 .matchInPort(subscriberPort)
452 .build();
453
454
455 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
456 .pushVlan()
457 .setVlanId(subscriberVlan)
458 .pushVlan()
459 .setVlanId(deviceVlan)
460 .setOutput(uplinkPort)
461 .build();
462
463 return DefaultForwardingObjective.builder()
464 .withFlag(ForwardingObjective.Flag.VERSATILE)
465 .withPriority(1000)
466 .makePermanent()
467 .withSelector(upstream)
468 .fromApp(appId)
469 .withTreatment(upstreamTreatment);
470 }
471
alshabib50d9fc52016-02-12 15:47:20 -0800472 private void processFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
alshabib09753b52016-03-04 14:55:19 -0800473 if (!mastershipService.isLocalMaster(devId)) {
474 return;
475 }
alshabibbb83aa22016-02-10 15:08:23 -0800476 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
477
478 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
alshabib50d9fc52016-02-12 15:47:20 -0800479 .withKey(Criteria.matchInPort(port))
alshabibdec2e252016-01-15 12:20:25 -0800480 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
481 .withMeta(DefaultTrafficTreatment.builder()
482 .setOutput(PortNumber.CONTROLLER).build())
483 .fromApp(appId)
484 .withPriority(1000)
485 .add(new ObjectiveContext() {
486 @Override
487 public void onSuccess(Objective objective) {
488 log.info("Eapol filter for {} on {} installed.",
489 devId, port);
490 }
491
492 @Override
493 public void onError(Objective objective, ObjectiveError error) {
494 log.info("Eapol filter for {} on {} failed because {}",
495 devId, port, error);
496 }
497 });
498
alshabibdec2e252016-01-15 12:20:25 -0800499 flowObjectiveService.filter(devId, eapol);
alshabib000b6fc2016-02-01 17:25:00 -0800500
alshabibdec2e252016-01-15 12:20:25 -0800501 }
502
Amit Ghosh95e2f652017-08-23 12:49:46 +0100503 private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
504 if (!mastershipService.isLocalMaster(devId)) {
505 return;
506 }
507 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
508
509 FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny())
510 .withKey(Criteria.matchInPort(port))
511 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
512 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_UDP))
513 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(68)))
514 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(67)))
515 .withMeta(DefaultTrafficTreatment.builder()
516 .setOutput(PortNumber.CONTROLLER).build())
517 .fromApp(appId)
518 .withPriority(1000)
519 .add(new ObjectiveContext() {
520 @Override
521 public void onSuccess(Objective objective) {
522 log.info("DHCP filter for {} on {} installed.",
523 devId, port);
524 }
525
526 @Override
527 public void onError(Objective objective, ObjectiveError error) {
528 log.info("DHCP filter for {} on {} failed because {}",
529 devId, port, error);
530 }
531 });
532
533 flowObjectiveService.filter(devId, dhcpUpstream);
534 }
535
536 private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
537 if (!mastershipService.isLocalMaster(devId)) {
538 return;
539 }
540
541 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
542
543 builder = install ? builder.permit() : builder.deny();
544
545 FilteringObjective igmp = builder
546 .withKey(Criteria.matchInPort(port))
547 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
548 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
549 .withMeta(DefaultTrafficTreatment.builder()
550 .setOutput(PortNumber.CONTROLLER).build())
551 .fromApp(appId)
552 .withPriority(10000)
553 .add(new ObjectiveContext() {
554 @Override
555 public void onSuccess(Objective objective) {
556 log.info("Igmp filter for {} on {} installed.",
557 devId, port);
558 }
559
560 @Override
561 public void onError(Objective objective, ObjectiveError error) {
562 log.info("Igmp filter for {} on {} failed because {}.",
563 devId, port, error);
564 }
565 });
566
567 flowObjectiveService.filter(devId, igmp);
568 }
569
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700570 private boolean isUni(Port port) {
571 return !oltData.get(port.element().id()).uplink().equals(port.number());
572 }
573
alshabibf0e7e702015-05-30 18:22:36 -0700574 private class InternalDeviceListener implements DeviceListener {
575 @Override
576 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800577 DeviceId devId = event.subject().id();
578 if (!oltData.containsKey(devId)) {
alshabib8e4fd2f2016-01-12 15:55:53 -0800579 return;
580 }
alshabibf0e7e702015-05-30 18:22:36 -0700581 switch (event.type()) {
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800582 //TODO: Port handling and bookkeeping should be improved once
alshabibdec2e252016-01-15 12:20:25 -0800583 // olt firmware handles correct behaviour.
alshabibf0e7e702015-05-30 18:22:36 -0700584 case PORT_ADDED:
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700585 if (!oltData.get(devId).uplink().equals(event.port().number())) {
586 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port()));
587
588 if (event.port().isEnabled()) {
589 processFilteringObjectives(devId, event.port().number(), true);
590 }
alshabibdec2e252016-01-15 12:20:25 -0800591 }
592 break;
593 case PORT_REMOVED:
594 AccessDeviceData olt = oltData.get(devId);
alshabib4ceaed32016-03-03 18:00:58 -0800595 VlanId vlan = subscribers.get(new ConnectPoint(devId,
596 event.port().number()));
dvaddirec63dc822017-10-05 22:25:13 +0530597 if (vlan != null) {
598 unprovisionSubscriber(devId, olt.uplink(),
599 event.port().number(),
600 vlan, olt.vlan(), olt.defaultVlan());
601 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700602 if (!oltData.get(devId).uplink().equals(event.port().number())) {
603 if (event.port().isEnabled()) {
604 processFilteringObjectives(devId, event.port().number(), false);
605 }
606
607 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, event.port()));
alshabibbb83aa22016-02-10 15:08:23 -0800608 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700609
alshabibdec2e252016-01-15 12:20:25 -0800610 break;
alshabibf0e7e702015-05-30 18:22:36 -0700611 case PORT_UPDATED:
alshabibbb83aa22016-02-10 15:08:23 -0800612 if (oltData.get(devId).uplink().equals(event.port().number())) {
613 break;
614 }
615 if (event.port().isEnabled()) {
alshabib50d9fc52016-02-12 15:47:20 -0800616 processFilteringObjectives(devId, event.port().number(), true);
alshabibbb83aa22016-02-10 15:08:23 -0800617 } else {
alshabib50d9fc52016-02-12 15:47:20 -0800618 processFilteringObjectives(devId, event.port().number(), false);
alshabibbb83aa22016-02-10 15:08:23 -0800619 }
alshabibf0e7e702015-05-30 18:22:36 -0700620 break;
621 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800622 post(new AccessDeviceEvent(
623 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
624 null, null));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700625
626 // Send UNI_ADDED events for all existing ports
627 deviceService.getPorts(devId).stream()
628 .filter(Olt.this::isUni)
629 .forEach(p -> post(new AccessDeviceEvent(
630 AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
631
alshabibe0559672016-02-21 14:49:51 -0800632 provisionDefaultFlows(devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800633 break;
alshabibf0e7e702015-05-30 18:22:36 -0700634 case DEVICE_REMOVED:
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700635 deviceService.getPorts(devId).stream()
636 .filter(Olt.this::isUni)
637 .forEach(p -> post(new AccessDeviceEvent(
638 AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
639
alshabib8e4fd2f2016-01-12 15:55:53 -0800640 post(new AccessDeviceEvent(
641 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
642 null, null));
643 break;
alshabib7c190012016-02-09 18:22:33 -0800644 case DEVICE_AVAILABILITY_CHANGED:
645 if (deviceService.isAvailable(devId)) {
646 post(new AccessDeviceEvent(
647 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
648 null, null));
649 } else {
650 post(new AccessDeviceEvent(
651 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
652 null, null));
653 }
654 break;
alshabib8e4fd2f2016-01-12 15:55:53 -0800655 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700656 case DEVICE_SUSPENDED:
alshabibf0e7e702015-05-30 18:22:36 -0700657 case PORT_STATS_UPDATED:
658 default:
659 return;
660 }
661 }
662 }
663
Jonathan Harte533a422015-10-20 17:31:24 -0700664 private class InternalNetworkConfigListener implements NetworkConfigListener {
665 @Override
666 public void event(NetworkConfigEvent event) {
667 switch (event.type()) {
668
alshabibbf23a1f2016-01-14 17:27:11 -0800669 case CONFIG_ADDED:
670 case CONFIG_UPDATED:
alshabibe0559672016-02-21 14:49:51 -0800671
672 AccessDeviceConfig config =
673 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
674 if (config != null) {
675 oltData.put(config.getOlt().deviceId(), config.getOlt());
676 provisionDefaultFlows((DeviceId) event.subject());
Jonathan Harte533a422015-10-20 17:31:24 -0700677 }
alshabibe0559672016-02-21 14:49:51 -0800678
alshabibbf23a1f2016-01-14 17:27:11 -0800679 break;
alshabibe0559672016-02-21 14:49:51 -0800680 case CONFIG_REGISTERED:
alshabibbf23a1f2016-01-14 17:27:11 -0800681 case CONFIG_UNREGISTERED:
alshabibe0559672016-02-21 14:49:51 -0800682 break;
alshabibbf23a1f2016-01-14 17:27:11 -0800683 case CONFIG_REMOVED:
alshabibe0559672016-02-21 14:49:51 -0800684 oltData.remove(event.subject());
alshabibbf23a1f2016-01-14 17:27:11 -0800685 default:
686 break;
Jonathan Harte533a422015-10-20 17:31:24 -0700687 }
688 }
alshabibe0559672016-02-21 14:49:51 -0800689
690 @Override
691 public boolean isRelevant(NetworkConfigEvent event) {
692 return event.configClass().equals(CONFIG_CLASS);
693 }
Jonathan Harte533a422015-10-20 17:31:24 -0700694 }
alshabibf0e7e702015-05-30 18:22:36 -0700695
alshabibbb83aa22016-02-10 15:08:23 -0800696 private void provisionDefaultFlows(DeviceId deviceId) {
alshabib09753b52016-03-04 14:55:19 -0800697 if (!mastershipService.isLocalMaster(deviceId)) {
698 return;
699 }
alshabibbb83aa22016-02-10 15:08:23 -0800700 List<Port> ports = deviceService.getPorts(deviceId);
701
702 ports.stream()
703 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
704 .filter(p -> p.isEnabled())
alshabib50d9fc52016-02-12 15:47:20 -0800705 .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(),
706 p.number(), true));
alshabibbb83aa22016-02-10 15:08:23 -0800707
708 }
709
alshabibf0e7e702015-05-30 18:22:36 -0700710}