blob: 9fc7ba2b495cf99b1fc77b7953105fc9cd3cbb5c [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;
alshabibf0e7e702015-05-30 18:22:36 -070028import org.onlab.packet.VlanId;
alshabibe0559672016-02-21 14:49:51 -080029import org.onosproject.cfg.ComponentConfigService;
alshabibf3a573e2016-06-01 17:39:10 -070030
alshabibf0e7e702015-05-30 18:22:36 -070031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080033import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080034import org.onosproject.mastership.MastershipService;
Jonathan Harte533a422015-10-20 17:31:24 -070035import org.onosproject.net.ConnectPoint;
alshabibf0e7e702015-05-30 18:22:36 -070036import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080037import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070038import org.onosproject.net.PortNumber;
alshabibf3a573e2016-06-01 17:39:10 -070039
Jonathan Harte533a422015-10-20 17:31:24 -070040import org.onosproject.net.config.ConfigFactory;
41import org.onosproject.net.config.NetworkConfigEvent;
42import org.onosproject.net.config.NetworkConfigListener;
43import org.onosproject.net.config.NetworkConfigRegistry;
44import org.onosproject.net.config.basics.SubjectFactories;
alshabibf0e7e702015-05-30 18:22:36 -070045import org.onosproject.net.device.DeviceEvent;
46import org.onosproject.net.device.DeviceListener;
47import org.onosproject.net.device.DeviceService;
48import org.onosproject.net.flow.DefaultTrafficSelector;
49import org.onosproject.net.flow.DefaultTrafficTreatment;
50import org.onosproject.net.flow.TrafficSelector;
51import org.onosproject.net.flow.TrafficTreatment;
alshabibdec2e252016-01-15 12:20:25 -080052import org.onosproject.net.flow.criteria.Criteria;
53import org.onosproject.net.flowobjective.DefaultFilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070054import org.onosproject.net.flowobjective.DefaultForwardingObjective;
alshabibdec2e252016-01-15 12:20:25 -080055import org.onosproject.net.flowobjective.FilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070056import org.onosproject.net.flowobjective.FlowObjectiveService;
57import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080058import org.onosproject.net.flowobjective.Objective;
59import org.onosproject.net.flowobjective.ObjectiveContext;
60import org.onosproject.net.flowobjective.ObjectiveError;
alshabibf3a573e2016-06-01 17:39:10 -070061import org.opencord.cordconfig.access.AccessDeviceConfig;
62import org.opencord.cordconfig.access.AccessDeviceData;
alshabib36a4d732016-06-01 16:03:59 -070063import org.opencord.olt.AccessDeviceEvent;
64import org.opencord.olt.AccessDeviceListener;
65import org.opencord.olt.AccessDeviceService;
alshabib4ceaed32016-03-03 18:00:58 -080066import org.onosproject.store.serializers.KryoNamespaces;
67import org.onosproject.store.service.Serializer;
68import org.onosproject.store.service.StorageService;
alshabibe0559672016-02-21 14:49:51 -080069import org.osgi.service.component.ComponentContext;
alshabibf0e7e702015-05-30 18:22:36 -070070import org.slf4j.Logger;
71
Jonathan Hartfd6c1b32016-03-08 14:09:09 -080072import java.util.Collection;
alshabibe0559672016-02-21 14:49:51 -080073import java.util.Dictionary;
alshabibbb83aa22016-02-10 15:08:23 -080074import java.util.List;
Jonathan Harte533a422015-10-20 17:31:24 -070075import java.util.Map;
Jonathan Hart52998382015-11-10 16:09:22 -080076import java.util.Optional;
alshabibe0559672016-02-21 14:49:51 -080077import java.util.Properties;
alshabib3ea82642016-01-12 18:06:53 -080078import java.util.concurrent.CompletableFuture;
Jonathan Harte533a422015-10-20 17:31:24 -070079import java.util.concurrent.ConcurrentHashMap;
alshabib3ea82642016-01-12 18:06:53 -080080import java.util.concurrent.ExecutorService;
81import java.util.concurrent.Executors;
alshabibf0e7e702015-05-30 18:22:36 -070082
alshabibe0559672016-02-21 14:49:51 -080083import static com.google.common.base.Strings.isNullOrEmpty;
84import static org.onlab.util.Tools.get;
alshabib3ea82642016-01-12 18:06:53 -080085import static org.onlab.util.Tools.groupedThreads;
alshabibf0e7e702015-05-30 18:22:36 -070086import static org.slf4j.LoggerFactory.getLogger;
87
88/**
Jonathan Harte533a422015-10-20 17:31:24 -070089 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -070090 */
Jonathan Harte533a422015-10-20 17:31:24 -070091@Service
alshabibf0e7e702015-05-30 18:22:36 -070092@Component(immediate = true)
alshabib8e4fd2f2016-01-12 15:55:53 -080093public class Olt
94 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
95 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -080096 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -080097
98 private static final short DEFAULT_VLAN = 0;
alshabib4ceaed32016-03-03 18:00:58 -080099 private static final String SUBSCRIBERS = "existing-subscribers";
alshabibe0559672016-02-21 14:49:51 -0800100
alshabibf0e7e702015-05-30 18:22:36 -0700101 private final Logger log = getLogger(getClass());
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected FlowObjectiveService flowObjectiveService;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib09753b52016-03-04 14:55:19 -0800107 protected MastershipService mastershipService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibf0e7e702015-05-30 18:22:36 -0700110 protected DeviceService deviceService;
111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected CoreService coreService;
114
Jonathan Harte533a422015-10-20 17:31:24 -0700115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
116 protected NetworkConfigRegistry networkConfig;
117
alshabibe0559672016-02-21 14:49:51 -0800118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
119 protected ComponentConfigService componentConfigService;
120
alshabib4ceaed32016-03-03 18:00:58 -0800121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected StorageService storageService;
alshabibe0559672016-02-21 14:49:51 -0800123
124 @Property(name = "defaultVlan", intValue = DEFAULT_VLAN,
125 label = "Default VLAN RG<->ONU traffic")
126 private int defaultVlan = DEFAULT_VLAN;
127
alshabibf0e7e702015-05-30 18:22:36 -0700128 private final DeviceListener deviceListener = new InternalDeviceListener();
129
130 private ApplicationId appId;
131
alshabib3ea82642016-01-12 18:06:53 -0800132 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
alshabibbf23a1f2016-01-14 17:27:11 -0800133 groupedThreads("onos/olt-service",
134 "olt-installer-%d"));
alshabib4ae2b402015-06-05 14:55:24 -0700135
Jonathan Harte533a422015-10-20 17:31:24 -0700136 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
137
alshabib4ceaed32016-03-03 18:00:58 -0800138 private Map<ConnectPoint, VlanId> subscribers;
alshabibbf23a1f2016-01-14 17:27:11 -0800139
Jonathan Harte533a422015-10-20 17:31:24 -0700140 private InternalNetworkConfigListener configListener =
141 new InternalNetworkConfigListener();
142 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
143 AccessDeviceConfig.class;
144
145 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
146 new ConfigFactory<DeviceId, AccessDeviceConfig>(
147 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
alshabibbf23a1f2016-01-14 17:27:11 -0800148 @Override
149 public AccessDeviceConfig createConfig() {
150 return new AccessDeviceConfig();
151 }
152 };
153
alshabibf0e7e702015-05-30 18:22:36 -0700154
155 @Activate
alshabibe0559672016-02-21 14:49:51 -0800156 public void activate(ComponentContext context) {
157 modified(context);
Charles Chan54f110f2017-01-20 11:22:42 -0800158 appId = coreService.registerApplication(APP_NAME);
alshabibe0559672016-02-21 14:49:51 -0800159 componentConfigService.registerProperties(getClass());
alshabibc4dfe852015-06-05 13:35:13 -0700160
alshabib8e4fd2f2016-01-12 15:55:53 -0800161 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
162
Jonathan Harte533a422015-10-20 17:31:24 -0700163 networkConfig.registerConfigFactory(configFactory);
164 networkConfig.addListener(configListener);
165
alshabibdec2e252016-01-15 12:20:25 -0800166
Jonathan Harte533a422015-10-20 17:31:24 -0700167 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
168 subject -> {
169 AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class);
170 if (config != null) {
171 AccessDeviceData data = config.getOlt();
172 oltData.put(data.deviceId(), data);
173 }
174 }
175 );
176
alshabibdec2e252016-01-15 12:20:25 -0800177 oltData.keySet().stream()
178 .flatMap(did -> deviceService.getPorts(did).stream())
alshabib62e9ce72016-02-11 17:31:36 -0800179 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
alshabibdec2e252016-01-15 12:20:25 -0800180 .filter(p -> p.isEnabled())
alshabib50d9fc52016-02-12 15:47:20 -0800181 .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(),
182 p.number(), true));
alshabibdec2e252016-01-15 12:20:25 -0800183
alshabib4ceaed32016-03-03 18:00:58 -0800184 subscribers = storageService.<ConnectPoint, VlanId>consistentMapBuilder()
185 .withName(SUBSCRIBERS)
186 .withSerializer(Serializer.using(KryoNamespaces.API))
187 .build().asJavaMap();
188
alshabibba357492016-01-27 13:49:46 -0800189 deviceService.addListener(deviceListener);
190
alshabibf0e7e702015-05-30 18:22:36 -0700191 log.info("Started with Application ID {}", appId.id());
192 }
193
194 @Deactivate
195 public void deactivate() {
alshabibe0559672016-02-21 14:49:51 -0800196 componentConfigService.unregisterProperties(getClass(), false);
alshabib62e9ce72016-02-11 17:31:36 -0800197 deviceService.removeListener(deviceListener);
Jonathan Harte533a422015-10-20 17:31:24 -0700198 networkConfig.removeListener(configListener);
199 networkConfig.unregisterConfigFactory(configFactory);
alshabibf0e7e702015-05-30 18:22:36 -0700200 log.info("Stopped");
201 }
202
alshabibe0559672016-02-21 14:49:51 -0800203 @Modified
204 public void modified(ComponentContext context) {
205 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
206
207 try {
208 String s = get(properties, "defaultVlan");
209 defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim());
210 } catch (Exception e) {
211 defaultVlan = DEFAULT_VLAN;
212 }
213 }
214
alshabib32232c82016-02-25 17:57:24 -0500215 @Override
Jonathan Harte533a422015-10-20 17:31:24 -0700216 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
217 AccessDeviceData olt = oltData.get(port.deviceId());
218
219 if (olt == null) {
220 log.warn("No data found for OLT device {}", port.deviceId());
221 return;
222 }
223
Jonathan Hart52998382015-11-10 16:09:22 -0800224 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
alshabibb7a9e172016-01-13 11:23:53 -0800225 olt.defaultVlan());
226 }
227
228 @Override
229 public void removeSubscriber(ConnectPoint port) {
alshabibbf23a1f2016-01-14 17:27:11 -0800230 AccessDeviceData olt = oltData.get(port.deviceId());
231
232 if (olt == null) {
233 log.warn("No data found for OLT device {}", port.deviceId());
234 return;
235 }
236
alshabib4ceaed32016-03-03 18:00:58 -0800237 VlanId subscriberVlan = subscribers.remove(port);
238
239 if (subscriberVlan == null) {
240 log.warn("Unknown subscriber at location {}", port);
241 return;
242 }
243
244 unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), subscriberVlan,
245 olt.vlan(), olt.defaultVlan());
alshabibbf23a1f2016-01-14 17:27:11 -0800246
247 }
248
alshabibe0559672016-02-21 14:49:51 -0800249 @Override
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800250 public Collection<Map.Entry<ConnectPoint, VlanId>> getSubscribers() {
251 return subscribers.entrySet();
252 }
253
254 @Override
alshabibe0559672016-02-21 14:49:51 -0800255 public Map<DeviceId, AccessDeviceData> fetchOlts() {
256 return Maps.newHashMap(oltData);
257 }
258
alshabibbf23a1f2016-01-14 17:27:11 -0800259 private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
alshabib4ceaed32016-03-03 18:00:58 -0800260 PortNumber subscriberPort, VlanId subscriberVlan,
261 VlanId deviceVlan, Optional<VlanId> defaultVlan) {
alshabibbf23a1f2016-01-14 17:27:11 -0800262
263 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
264 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
265
alshabib4ceaed32016-03-03 18:00:58 -0800266 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
267 subscriberVlan, deviceVlan,
268 defaultVlan);
269 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
270 subscriberVlan, deviceVlan,
271 defaultVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800272
273
alshabib4ceaed32016-03-03 18:00:58 -0800274 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
275 @Override
276 public void onSuccess(Objective objective) {
277 upFuture.complete(null);
278 }
alshabibbf23a1f2016-01-14 17:27:11 -0800279
alshabib4ceaed32016-03-03 18:00:58 -0800280 @Override
281 public void onError(Objective objective, ObjectiveError error) {
282 upFuture.complete(error);
283 }
284 }));
285
286 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
287 @Override
288 public void onSuccess(Objective objective) {
289 downFuture.complete(null);
290 }
291
292 @Override
293 public void onError(Objective objective, ObjectiveError error) {
294 downFuture.complete(error);
295 }
296 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800297
298 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
299 if (upStatus == null && downStatus == null) {
300 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
301 deviceId,
302 deviceVlan,
303 subscriberVlan));
304 } else if (downStatus != null) {
305 log.error("Subscriber with vlan {} on device {} " +
306 "on port {} failed downstream uninstallation: {}",
307 subscriberVlan, deviceId, subscriberPort, downStatus);
308 } else if (upStatus != null) {
309 log.error("Subscriber with vlan {} on device {} " +
310 "on port {} failed upstream uninstallation: {}",
311 subscriberVlan, deviceId, subscriberPort, upStatus);
312 }
313 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800314
Jonathan Harte533a422015-10-20 17:31:24 -0700315 }
316
317 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
318 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800319 VlanId subscriberVlan, VlanId deviceVlan,
320 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700321
alshabib3ea82642016-01-12 18:06:53 -0800322 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
323 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
324
alshabib4ceaed32016-03-03 18:00:58 -0800325 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
326 subscriberVlan, deviceVlan,
327 defaultVlan);
Jonathan Harte533a422015-10-20 17:31:24 -0700328
329
alshabib4ceaed32016-03-03 18:00:58 -0800330 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
331 subscriberVlan, deviceVlan,
332 defaultVlan);
alshabib3ea82642016-01-12 18:06:53 -0800333
alshabibbf23a1f2016-01-14 17:27:11 -0800334 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800335 subscribers.put(cp, subscriberVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800336
alshabibbf23a1f2016-01-14 17:27:11 -0800337 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
338 @Override
339 public void onSuccess(Objective objective) {
340 upFuture.complete(null);
341 }
342
343 @Override
344 public void onError(Objective objective, ObjectiveError error) {
345 upFuture.complete(error);
346 }
347 }));
348
alshabibbf23a1f2016-01-14 17:27:11 -0800349 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
350 @Override
351 public void onSuccess(Objective objective) {
352 downFuture.complete(null);
353 }
354
355 @Override
356 public void onError(Objective objective, ObjectiveError error) {
357 downFuture.complete(error);
358 }
359 }));
alshabib3ea82642016-01-12 18:06:53 -0800360
361 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
362 if (upStatus == null && downStatus == null) {
363 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
364 deviceId,
365 deviceVlan,
366 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800367
alshabib3ea82642016-01-12 18:06:53 -0800368 } else if (downStatus != null) {
369 log.error("Subscriber with vlan {} on device {} " +
370 "on port {} failed downstream installation: {}",
371 subscriberVlan, deviceId, subscriberPort, downStatus);
372 } else if (upStatus != null) {
373 log.error("Subscriber with vlan {} on device {} " +
374 "on port {} failed upstream installation: {}",
375 subscriberVlan, deviceId, subscriberPort, upStatus);
376 }
377 }, oltInstallers);
378
Jonathan Harte533a422015-10-20 17:31:24 -0700379 }
380
alshabib4ceaed32016-03-03 18:00:58 -0800381 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
382 PortNumber subscriberPort,
383 VlanId subscriberVlan,
384 VlanId deviceVlan,
385 Optional<VlanId> defaultVlan) {
386 TrafficSelector downstream = DefaultTrafficSelector.builder()
387 .matchVlanId(deviceVlan)
388 .matchInPort(uplinkPort)
389 .matchInnerVlanId(subscriberVlan)
390 .build();
391
392 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
393 .popVlan()
394 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
395 .setOutput(subscriberPort)
396 .build();
397
398 return DefaultForwardingObjective.builder()
399 .withFlag(ForwardingObjective.Flag.VERSATILE)
400 .withPriority(1000)
401 .makePermanent()
402 .withSelector(downstream)
403 .fromApp(appId)
404 .withTreatment(downstreamTreatment);
405 }
406
407 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
408 PortNumber subscriberPort,
409 VlanId subscriberVlan,
410 VlanId deviceVlan,
411 Optional<VlanId> defaultVlan) {
412 TrafficSelector upstream = DefaultTrafficSelector.builder()
413 .matchVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
414 .matchInPort(subscriberPort)
415 .build();
416
417
418 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
419 .pushVlan()
420 .setVlanId(subscriberVlan)
421 .pushVlan()
422 .setVlanId(deviceVlan)
423 .setOutput(uplinkPort)
424 .build();
425
426 return DefaultForwardingObjective.builder()
427 .withFlag(ForwardingObjective.Flag.VERSATILE)
428 .withPriority(1000)
429 .makePermanent()
430 .withSelector(upstream)
431 .fromApp(appId)
432 .withTreatment(upstreamTreatment);
433 }
434
alshabib50d9fc52016-02-12 15:47:20 -0800435 private void processFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
alshabib09753b52016-03-04 14:55:19 -0800436 if (!mastershipService.isLocalMaster(devId)) {
437 return;
438 }
alshabibbb83aa22016-02-10 15:08:23 -0800439 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
440
441 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
alshabib50d9fc52016-02-12 15:47:20 -0800442 .withKey(Criteria.matchInPort(port))
alshabibdec2e252016-01-15 12:20:25 -0800443 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
444 .withMeta(DefaultTrafficTreatment.builder()
445 .setOutput(PortNumber.CONTROLLER).build())
446 .fromApp(appId)
447 .withPriority(1000)
448 .add(new ObjectiveContext() {
449 @Override
450 public void onSuccess(Objective objective) {
451 log.info("Eapol filter for {} on {} installed.",
452 devId, port);
453 }
454
455 @Override
456 public void onError(Objective objective, ObjectiveError error) {
457 log.info("Eapol filter for {} on {} failed because {}",
458 devId, port, error);
459 }
460 });
461
alshabibdec2e252016-01-15 12:20:25 -0800462 flowObjectiveService.filter(devId, eapol);
alshabib000b6fc2016-02-01 17:25:00 -0800463
alshabibdec2e252016-01-15 12:20:25 -0800464 }
465
alshabibf0e7e702015-05-30 18:22:36 -0700466 private class InternalDeviceListener implements DeviceListener {
467 @Override
468 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800469 DeviceId devId = event.subject().id();
470 if (!oltData.containsKey(devId)) {
alshabib8e4fd2f2016-01-12 15:55:53 -0800471 return;
472 }
alshabibf0e7e702015-05-30 18:22:36 -0700473 switch (event.type()) {
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800474 //TODO: Port handling and bookkeeping should be improved once
alshabibdec2e252016-01-15 12:20:25 -0800475 // olt firmware handles correct behaviour.
alshabibf0e7e702015-05-30 18:22:36 -0700476 case PORT_ADDED:
alshabibbb83aa22016-02-10 15:08:23 -0800477 if (!oltData.get(devId).uplink().equals(event.port().number()) &&
478 event.port().isEnabled()) {
alshabib50d9fc52016-02-12 15:47:20 -0800479 processFilteringObjectives(devId, event.port().number(), true);
alshabibdec2e252016-01-15 12:20:25 -0800480 }
481 break;
482 case PORT_REMOVED:
483 AccessDeviceData olt = oltData.get(devId);
alshabib4ceaed32016-03-03 18:00:58 -0800484 VlanId vlan = subscribers.get(new ConnectPoint(devId,
485 event.port().number()));
alshabibdec2e252016-01-15 12:20:25 -0800486 unprovisionSubscriber(devId, olt.uplink(),
487 event.port().number(),
alshabib4ceaed32016-03-03 18:00:58 -0800488 vlan, olt.vlan(), olt.defaultVlan());
alshabibbb83aa22016-02-10 15:08:23 -0800489 if (!oltData.get(devId).uplink().equals(event.port().number()) &&
490 event.port().isEnabled()) {
alshabib50d9fc52016-02-12 15:47:20 -0800491 processFilteringObjectives(devId, event.port().number(), false);
alshabibbb83aa22016-02-10 15:08:23 -0800492 }
alshabibdec2e252016-01-15 12:20:25 -0800493 break;
alshabibf0e7e702015-05-30 18:22:36 -0700494 case PORT_UPDATED:
alshabibbb83aa22016-02-10 15:08:23 -0800495 if (oltData.get(devId).uplink().equals(event.port().number())) {
496 break;
497 }
498 if (event.port().isEnabled()) {
alshabib50d9fc52016-02-12 15:47:20 -0800499 processFilteringObjectives(devId, event.port().number(), true);
alshabibbb83aa22016-02-10 15:08:23 -0800500 } else {
alshabib50d9fc52016-02-12 15:47:20 -0800501 processFilteringObjectives(devId, event.port().number(), false);
alshabibbb83aa22016-02-10 15:08:23 -0800502 }
alshabibf0e7e702015-05-30 18:22:36 -0700503 break;
504 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800505 post(new AccessDeviceEvent(
506 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
507 null, null));
alshabibe0559672016-02-21 14:49:51 -0800508 provisionDefaultFlows(devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800509 break;
alshabibf0e7e702015-05-30 18:22:36 -0700510 case DEVICE_REMOVED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800511 post(new AccessDeviceEvent(
512 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
513 null, null));
514 break;
alshabib7c190012016-02-09 18:22:33 -0800515 case DEVICE_AVAILABILITY_CHANGED:
516 if (deviceService.isAvailable(devId)) {
517 post(new AccessDeviceEvent(
518 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
519 null, null));
520 } else {
521 post(new AccessDeviceEvent(
522 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
523 null, null));
524 }
525 break;
alshabib8e4fd2f2016-01-12 15:55:53 -0800526 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700527 case DEVICE_SUSPENDED:
alshabibf0e7e702015-05-30 18:22:36 -0700528 case PORT_STATS_UPDATED:
529 default:
530 return;
531 }
532 }
533 }
534
Jonathan Harte533a422015-10-20 17:31:24 -0700535 private class InternalNetworkConfigListener implements NetworkConfigListener {
536 @Override
537 public void event(NetworkConfigEvent event) {
538 switch (event.type()) {
539
alshabibbf23a1f2016-01-14 17:27:11 -0800540 case CONFIG_ADDED:
541 case CONFIG_UPDATED:
alshabibe0559672016-02-21 14:49:51 -0800542
543 AccessDeviceConfig config =
544 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
545 if (config != null) {
546 oltData.put(config.getOlt().deviceId(), config.getOlt());
547 provisionDefaultFlows((DeviceId) event.subject());
Jonathan Harte533a422015-10-20 17:31:24 -0700548 }
alshabibe0559672016-02-21 14:49:51 -0800549
alshabibbf23a1f2016-01-14 17:27:11 -0800550 break;
alshabibe0559672016-02-21 14:49:51 -0800551 case CONFIG_REGISTERED:
alshabibbf23a1f2016-01-14 17:27:11 -0800552 case CONFIG_UNREGISTERED:
alshabibe0559672016-02-21 14:49:51 -0800553 break;
alshabibbf23a1f2016-01-14 17:27:11 -0800554 case CONFIG_REMOVED:
alshabibe0559672016-02-21 14:49:51 -0800555 oltData.remove(event.subject());
alshabibbf23a1f2016-01-14 17:27:11 -0800556 default:
557 break;
Jonathan Harte533a422015-10-20 17:31:24 -0700558 }
559 }
alshabibe0559672016-02-21 14:49:51 -0800560
561 @Override
562 public boolean isRelevant(NetworkConfigEvent event) {
563 return event.configClass().equals(CONFIG_CLASS);
564 }
Jonathan Harte533a422015-10-20 17:31:24 -0700565 }
alshabibf0e7e702015-05-30 18:22:36 -0700566
alshabibbb83aa22016-02-10 15:08:23 -0800567 private void provisionDefaultFlows(DeviceId deviceId) {
alshabib09753b52016-03-04 14:55:19 -0800568 if (!mastershipService.isLocalMaster(deviceId)) {
569 return;
570 }
alshabibbb83aa22016-02-10 15:08:23 -0800571 List<Port> ports = deviceService.getPorts(deviceId);
572
573 ports.stream()
574 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
575 .filter(p -> p.isEnabled())
alshabib50d9fc52016-02-12 15:47:20 -0800576 .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(),
577 p.number(), true));
alshabibbb83aa22016-02-10 15:08:23 -0800578
579 }
580
alshabibf0e7e702015-05-30 18:22:36 -0700581}