blob: ce94c2b95c9e8b26cbc269cd52acf887dcc052dc [file] [log] [blame]
alshabibf0e7e702015-05-30 18:22:36 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
Srikanth Vavilapalli2e684912016-01-16 19:21:59 -080016package org.onosproject.olt.impl;
alshabibf0e7e702015-05-30 18:22:36 -070017
alshabibbf23a1f2016-01-14 17:27:11 -080018import com.google.common.collect.Maps;
19import com.google.common.collect.Sets;
alshabibf0e7e702015-05-30 18:22:36 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
alshabibf0e7e702015-05-30 18:22:36 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070025import org.apache.felix.scr.annotations.Service;
alshabibdec2e252016-01-15 12:20:25 -080026import org.onlab.packet.EthType;
27import org.onlab.packet.IPv4;
alshabibf0e7e702015-05-30 18:22:36 -070028import org.onlab.packet.VlanId;
alshabibf0e7e702015-05-30 18:22:36 -070029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080031import org.onosproject.event.AbstractListenerManager;
Jonathan Harte533a422015-10-20 17:31:24 -070032import org.onosproject.net.ConnectPoint;
alshabibf0e7e702015-05-30 18:22:36 -070033import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080034import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070035import org.onosproject.net.PortNumber;
Jonathan Harte533a422015-10-20 17:31:24 -070036import org.onosproject.net.config.ConfigFactory;
37import org.onosproject.net.config.NetworkConfigEvent;
38import org.onosproject.net.config.NetworkConfigListener;
39import org.onosproject.net.config.NetworkConfigRegistry;
40import org.onosproject.net.config.basics.SubjectFactories;
alshabibf0e7e702015-05-30 18:22:36 -070041import org.onosproject.net.device.DeviceEvent;
42import org.onosproject.net.device.DeviceListener;
43import org.onosproject.net.device.DeviceService;
44import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
alshabibdec2e252016-01-15 12:20:25 -080048import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flowobjective.DefaultFilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070050import org.onosproject.net.flowobjective.DefaultForwardingObjective;
alshabibdec2e252016-01-15 12:20:25 -080051import org.onosproject.net.flowobjective.FilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070052import org.onosproject.net.flowobjective.FlowObjectiveService;
53import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080054import org.onosproject.net.flowobjective.Objective;
55import org.onosproject.net.flowobjective.ObjectiveContext;
56import org.onosproject.net.flowobjective.ObjectiveError;
Srikanth Vavilapalli2e684912016-01-16 19:21:59 -080057import org.onosproject.olt.AccessDeviceEvent;
58import org.onosproject.olt.AccessDeviceListener;
59import org.onosproject.olt.AccessDeviceService;
alshabibf0e7e702015-05-30 18:22:36 -070060import org.slf4j.Logger;
61
Jonathan Harte533a422015-10-20 17:31:24 -070062import java.util.Map;
Jonathan Hart52998382015-11-10 16:09:22 -080063import java.util.Optional;
alshabibbf23a1f2016-01-14 17:27:11 -080064import java.util.Set;
alshabib3ea82642016-01-12 18:06:53 -080065import java.util.concurrent.CompletableFuture;
Jonathan Harte533a422015-10-20 17:31:24 -070066import java.util.concurrent.ConcurrentHashMap;
alshabib3ea82642016-01-12 18:06:53 -080067import java.util.concurrent.ExecutorService;
68import java.util.concurrent.Executors;
alshabibf0e7e702015-05-30 18:22:36 -070069
alshabib3ea82642016-01-12 18:06:53 -080070import static org.onlab.util.Tools.groupedThreads;
alshabibf0e7e702015-05-30 18:22:36 -070071import static org.slf4j.LoggerFactory.getLogger;
72
73/**
Jonathan Harte533a422015-10-20 17:31:24 -070074 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -070075 */
Jonathan Harte533a422015-10-20 17:31:24 -070076@Service
alshabibf0e7e702015-05-30 18:22:36 -070077@Component(immediate = true)
alshabib8e4fd2f2016-01-12 15:55:53 -080078public class Olt
79 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
80 implements AccessDeviceService {
alshabibf0e7e702015-05-30 18:22:36 -070081 private final Logger log = getLogger(getClass());
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected FlowObjectiveService flowObjectiveService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected DeviceService deviceService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected CoreService coreService;
91
Jonathan Harte533a422015-10-20 17:31:24 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected NetworkConfigRegistry networkConfig;
94
alshabibf0e7e702015-05-30 18:22:36 -070095 private final DeviceListener deviceListener = new InternalDeviceListener();
96
97 private ApplicationId appId;
98
Jonathan Harte533a422015-10-20 17:31:24 -070099 private static final VlanId DEFAULT_VLAN = VlanId.vlanId((short) 0);
alshabibcf3eb952015-06-01 10:57:31 -0700100
alshabib3ea82642016-01-12 18:06:53 -0800101 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
alshabibbf23a1f2016-01-14 17:27:11 -0800102 groupedThreads("onos/olt-service",
103 "olt-installer-%d"));
alshabib4ae2b402015-06-05 14:55:24 -0700104
Jonathan Harte533a422015-10-20 17:31:24 -0700105 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
106
alshabibbf23a1f2016-01-14 17:27:11 -0800107 private Map<ConnectPoint, Set<ForwardingObjective.Builder>> objectives =
108 Maps.newConcurrentMap();
109
110 private Map<ConnectPoint, VlanId> subscribers = Maps.newConcurrentMap();
111
Jonathan Harte533a422015-10-20 17:31:24 -0700112 private InternalNetworkConfigListener configListener =
113 new InternalNetworkConfigListener();
114 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
115 AccessDeviceConfig.class;
116
117 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
118 new ConfigFactory<DeviceId, AccessDeviceConfig>(
119 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
alshabibbf23a1f2016-01-14 17:27:11 -0800120 @Override
121 public AccessDeviceConfig createConfig() {
122 return new AccessDeviceConfig();
123 }
124 };
125
alshabibf0e7e702015-05-30 18:22:36 -0700126
127 @Activate
128 public void activate() {
alshabib4ae2b402015-06-05 14:55:24 -0700129 appId = coreService.registerApplication("org.onosproject.olt");
alshabibc4dfe852015-06-05 13:35:13 -0700130
alshabib8e4fd2f2016-01-12 15:55:53 -0800131 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
132
Jonathan Harte533a422015-10-20 17:31:24 -0700133 networkConfig.registerConfigFactory(configFactory);
134 networkConfig.addListener(configListener);
135
alshabibdec2e252016-01-15 12:20:25 -0800136
Jonathan Harte533a422015-10-20 17:31:24 -0700137 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
138 subject -> {
139 AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class);
140 if (config != null) {
141 AccessDeviceData data = config.getOlt();
142 oltData.put(data.deviceId(), data);
143 }
144 }
145 );
146
alshabibdec2e252016-01-15 12:20:25 -0800147 oltData.keySet().stream()
148 .flatMap(did -> deviceService.getPorts(did).stream())
149 .filter(p -> oltData.get(p.element().id()).uplink() != p.number())
150 .filter(p -> p.isEnabled())
151 .forEach(p -> installFilteringObjectives((DeviceId) p.element().id(), p));
152
alshabibba357492016-01-27 13:49:46 -0800153 deviceService.addListener(deviceListener);
154
alshabibf0e7e702015-05-30 18:22:36 -0700155 log.info("Started with Application ID {}", appId.id());
156 }
157
158 @Deactivate
159 public void deactivate() {
Jonathan Harte533a422015-10-20 17:31:24 -0700160 networkConfig.removeListener(configListener);
161 networkConfig.unregisterConfigFactory(configFactory);
alshabibf0e7e702015-05-30 18:22:36 -0700162 log.info("Stopped");
163 }
164
Jonathan Harte533a422015-10-20 17:31:24 -0700165 @Override
166 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
167 AccessDeviceData olt = oltData.get(port.deviceId());
168
169 if (olt == null) {
170 log.warn("No data found for OLT device {}", port.deviceId());
171 return;
172 }
173
Jonathan Hart52998382015-11-10 16:09:22 -0800174 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
alshabibb7a9e172016-01-13 11:23:53 -0800175 olt.defaultVlan());
176 }
177
178 @Override
179 public void removeSubscriber(ConnectPoint port) {
alshabibbf23a1f2016-01-14 17:27:11 -0800180 AccessDeviceData olt = oltData.get(port.deviceId());
181
182 if (olt == null) {
183 log.warn("No data found for OLT device {}", port.deviceId());
184 return;
185 }
186
187 unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), olt.vlan());
188
189 }
190
191 private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
192 PortNumber subscriberPort, VlanId deviceVlan) {
193
194 //FIXME: This method is slightly ugly but it'll do until we have a better
195 // way to remove flows from the flow store.
196
197 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
198 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
199
200 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
201
202 VlanId subscriberVlan = subscribers.remove(cp);
203
204 Set<ForwardingObjective.Builder> fwds = objectives.remove(cp);
205
206 if (fwds == null || fwds.size() != 2) {
207 log.warn("Unknown or incomplete subscriber at {}", cp);
208 return;
209 }
210
211
212 fwds.stream().forEach(
213 fwd -> flowObjectiveService.forward(deviceId,
alshabibdec2e252016-01-15 12:20:25 -0800214 fwd.remove(new ObjectiveContext() {
215 @Override
216 public void onSuccess(Objective objective) {
217 upFuture.complete(null);
218 }
alshabibbf23a1f2016-01-14 17:27:11 -0800219
alshabibdec2e252016-01-15 12:20:25 -0800220 @Override
221 public void onError(Objective objective, ObjectiveError error) {
222 upFuture.complete(error);
223 }
224 })));
alshabibbf23a1f2016-01-14 17:27:11 -0800225
226 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
227 if (upStatus == null && downStatus == null) {
228 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
229 deviceId,
230 deviceVlan,
231 subscriberVlan));
232 } else if (downStatus != null) {
233 log.error("Subscriber with vlan {} on device {} " +
234 "on port {} failed downstream uninstallation: {}",
235 subscriberVlan, deviceId, subscriberPort, downStatus);
236 } else if (upStatus != null) {
237 log.error("Subscriber with vlan {} on device {} " +
238 "on port {} failed upstream uninstallation: {}",
239 subscriberVlan, deviceId, subscriberPort, upStatus);
240 }
241 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800242
Jonathan Harte533a422015-10-20 17:31:24 -0700243 }
244
245 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
246 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800247 VlanId subscriberVlan, VlanId deviceVlan,
248 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700249
alshabib3ea82642016-01-12 18:06:53 -0800250 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
251 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
252
Jonathan Harte533a422015-10-20 17:31:24 -0700253 TrafficSelector upstream = DefaultTrafficSelector.builder()
Jonathan Hart52998382015-11-10 16:09:22 -0800254 .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700255 .matchInPort(subscriberPort)
256 .build();
257
258 TrafficSelector downstream = DefaultTrafficSelector.builder()
259 .matchVlanId(deviceVlan)
260 .matchInPort(uplinkPort)
alshabibb7a9e172016-01-13 11:23:53 -0800261 .matchInnerVlanId(subscriberVlan)
Jonathan Harte533a422015-10-20 17:31:24 -0700262 .build();
263
264 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
alshabib3ea82642016-01-12 18:06:53 -0800265 .pushVlan()
Jonathan Harte533a422015-10-20 17:31:24 -0700266 .setVlanId(subscriberVlan)
267 .pushVlan()
268 .setVlanId(deviceVlan)
269 .setOutput(uplinkPort)
270 .build();
271
272 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
273 .popVlan()
Jonathan Hart52998382015-11-10 16:09:22 -0800274 .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700275 .setOutput(subscriberPort)
276 .build();
277
278
alshabibbf23a1f2016-01-14 17:27:11 -0800279 ForwardingObjective.Builder upFwd = DefaultForwardingObjective.builder()
Jonathan Harte533a422015-10-20 17:31:24 -0700280 .withFlag(ForwardingObjective.Flag.VERSATILE)
281 .withPriority(1000)
282 .makePermanent()
283 .withSelector(upstream)
284 .fromApp(appId)
alshabibbf23a1f2016-01-14 17:27:11 -0800285 .withTreatment(upstreamTreatment);
alshabib3ea82642016-01-12 18:06:53 -0800286
Jonathan Harte533a422015-10-20 17:31:24 -0700287
alshabibbf23a1f2016-01-14 17:27:11 -0800288 ForwardingObjective.Builder downFwd = DefaultForwardingObjective.builder()
Jonathan Harte533a422015-10-20 17:31:24 -0700289 .withFlag(ForwardingObjective.Flag.VERSATILE)
290 .withPriority(1000)
291 .makePermanent()
292 .withSelector(downstream)
293 .fromApp(appId)
alshabibbf23a1f2016-01-14 17:27:11 -0800294 .withTreatment(downstreamTreatment);
alshabib3ea82642016-01-12 18:06:53 -0800295
alshabibbf23a1f2016-01-14 17:27:11 -0800296 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
Jonathan Harte533a422015-10-20 17:31:24 -0700297
alshabibbf23a1f2016-01-14 17:27:11 -0800298 subscribers.put(cp, subscriberVlan);
299 objectives.put(cp, Sets.newHashSet(upFwd, downFwd));
300
301
302 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
303 @Override
304 public void onSuccess(Objective objective) {
305 upFuture.complete(null);
306 }
307
308 @Override
309 public void onError(Objective objective, ObjectiveError error) {
310 upFuture.complete(error);
311 }
312 }));
313
314
315 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
316 @Override
317 public void onSuccess(Objective objective) {
318 downFuture.complete(null);
319 }
320
321 @Override
322 public void onError(Objective objective, ObjectiveError error) {
323 downFuture.complete(error);
324 }
325 }));
alshabib3ea82642016-01-12 18:06:53 -0800326
327 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
328 if (upStatus == null && downStatus == null) {
329 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
330 deviceId,
331 deviceVlan,
332 subscriberVlan));
333 } else if (downStatus != null) {
334 log.error("Subscriber with vlan {} on device {} " +
335 "on port {} failed downstream installation: {}",
336 subscriberVlan, deviceId, subscriberPort, downStatus);
337 } else if (upStatus != null) {
338 log.error("Subscriber with vlan {} on device {} " +
339 "on port {} failed upstream installation: {}",
340 subscriberVlan, deviceId, subscriberPort, upStatus);
341 }
342 }, oltInstallers);
343
Jonathan Harte533a422015-10-20 17:31:24 -0700344 }
345
alshabibdec2e252016-01-15 12:20:25 -0800346 private void installFilteringObjectives(DeviceId devId, Port port) {
347 FilteringObjective eapol = DefaultFilteringObjective.builder()
348 .permit()
349 .withKey(Criteria.matchInPort(port.number()))
350 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
351 .withMeta(DefaultTrafficTreatment.builder()
352 .setOutput(PortNumber.CONTROLLER).build())
353 .fromApp(appId)
354 .withPriority(1000)
355 .add(new ObjectiveContext() {
356 @Override
357 public void onSuccess(Objective objective) {
358 log.info("Eapol filter for {} on {} installed.",
359 devId, port);
360 }
361
362 @Override
363 public void onError(Objective objective, ObjectiveError error) {
364 log.info("Eapol filter for {} on {} failed because {}",
365 devId, port, error);
366 }
367 });
368
369
370 FilteringObjective igmp = DefaultFilteringObjective.builder()
371 .permit()
372 .withKey(Criteria.matchInPort(port.number()))
373 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
374 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
375 .withMeta(DefaultTrafficTreatment.builder()
376 .setOutput(PortNumber.CONTROLLER).build())
377 .fromApp(appId)
378 .withPriority(1000)
379 .add(new ObjectiveContext() {
380 @Override
381 public void onSuccess(Objective objective) {
382 log.info("Igmp filter for {} on {} installed.",
383 devId, port);
384 }
385
386 @Override
387 public void onError(Objective objective, ObjectiveError error) {
388 log.info("Igmp filter for {} on {} failed because {}.",
389 devId, port, error);
390 }
391 });
392
393 flowObjectiveService.filter(devId, eapol);
394 flowObjectiveService.filter(devId, igmp);
395 }
396
alshabibf0e7e702015-05-30 18:22:36 -0700397 private class InternalDeviceListener implements DeviceListener {
398 @Override
399 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800400 DeviceId devId = event.subject().id();
401 if (!oltData.containsKey(devId)) {
402 log.debug("Device {} is not an OLT", devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800403 return;
404 }
alshabibf0e7e702015-05-30 18:22:36 -0700405 switch (event.type()) {
alshabibdec2e252016-01-15 12:20:25 -0800406 //TODO: Port handling and bookkeeping should be inproved once
407 // olt firmware handles correct behaviour.
alshabibf0e7e702015-05-30 18:22:36 -0700408 case PORT_ADDED:
alshabibdec2e252016-01-15 12:20:25 -0800409 if (event.port().isEnabled()) {
410 installFilteringObjectives(devId, event.port());
411 }
412 break;
413 case PORT_REMOVED:
414 AccessDeviceData olt = oltData.get(devId);
415 unprovisionSubscriber(devId, olt.uplink(),
416 event.port().number(),
417 olt.vlan());
418 installFilteringObjectives(devId, event.port());
419 break;
alshabibf0e7e702015-05-30 18:22:36 -0700420 case PORT_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700421 break;
422 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800423 post(new AccessDeviceEvent(
424 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
425 null, null));
426 break;
alshabibf0e7e702015-05-30 18:22:36 -0700427 case DEVICE_REMOVED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800428 post(new AccessDeviceEvent(
429 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
430 null, null));
431 break;
432 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700433 case DEVICE_SUSPENDED:
434 case DEVICE_AVAILABILITY_CHANGED:
alshabibf0e7e702015-05-30 18:22:36 -0700435 case PORT_STATS_UPDATED:
436 default:
437 return;
438 }
439 }
440 }
441
Jonathan Harte533a422015-10-20 17:31:24 -0700442 private class InternalNetworkConfigListener implements NetworkConfigListener {
443 @Override
444 public void event(NetworkConfigEvent event) {
445 switch (event.type()) {
446
alshabibbf23a1f2016-01-14 17:27:11 -0800447 case CONFIG_ADDED:
448 case CONFIG_UPDATED:
449 if (event.configClass().equals(CONFIG_CLASS)) {
450 AccessDeviceConfig config =
451 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
452 if (config != null) {
453 oltData.put(config.getOlt().deviceId(), config.getOlt());
454 }
Jonathan Harte533a422015-10-20 17:31:24 -0700455 }
alshabibbf23a1f2016-01-14 17:27:11 -0800456 break;
457 case CONFIG_UNREGISTERED:
458 case CONFIG_REMOVED:
459 default:
460 break;
Jonathan Harte533a422015-10-20 17:31:24 -0700461 }
462 }
463 }
alshabibf0e7e702015-05-30 18:22:36 -0700464
465}