blob: 11c1e5b0c96f5d625c055595666b2393e2a935f9 [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
alshabibf0e7e702015-05-30 18:22:36 -0700153 log.info("Started with Application ID {}", appId.id());
154 }
155
156 @Deactivate
157 public void deactivate() {
Jonathan Harte533a422015-10-20 17:31:24 -0700158 networkConfig.removeListener(configListener);
159 networkConfig.unregisterConfigFactory(configFactory);
alshabibf0e7e702015-05-30 18:22:36 -0700160 log.info("Stopped");
161 }
162
Jonathan Harte533a422015-10-20 17:31:24 -0700163 @Override
164 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
165 AccessDeviceData olt = oltData.get(port.deviceId());
166
167 if (olt == null) {
168 log.warn("No data found for OLT device {}", port.deviceId());
169 return;
170 }
171
Jonathan Hart52998382015-11-10 16:09:22 -0800172 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
alshabibb7a9e172016-01-13 11:23:53 -0800173 olt.defaultVlan());
174 }
175
176 @Override
177 public void removeSubscriber(ConnectPoint port) {
alshabibbf23a1f2016-01-14 17:27:11 -0800178 AccessDeviceData olt = oltData.get(port.deviceId());
179
180 if (olt == null) {
181 log.warn("No data found for OLT device {}", port.deviceId());
182 return;
183 }
184
185 unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), olt.vlan());
186
187 }
188
189 private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
190 PortNumber subscriberPort, VlanId deviceVlan) {
191
192 //FIXME: This method is slightly ugly but it'll do until we have a better
193 // way to remove flows from the flow store.
194
195 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
196 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
197
198 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
199
200 VlanId subscriberVlan = subscribers.remove(cp);
201
202 Set<ForwardingObjective.Builder> fwds = objectives.remove(cp);
203
204 if (fwds == null || fwds.size() != 2) {
205 log.warn("Unknown or incomplete subscriber at {}", cp);
206 return;
207 }
208
209
210 fwds.stream().forEach(
211 fwd -> flowObjectiveService.forward(deviceId,
alshabibdec2e252016-01-15 12:20:25 -0800212 fwd.remove(new ObjectiveContext() {
213 @Override
214 public void onSuccess(Objective objective) {
215 upFuture.complete(null);
216 }
alshabibbf23a1f2016-01-14 17:27:11 -0800217
alshabibdec2e252016-01-15 12:20:25 -0800218 @Override
219 public void onError(Objective objective, ObjectiveError error) {
220 upFuture.complete(error);
221 }
222 })));
alshabibbf23a1f2016-01-14 17:27:11 -0800223
224 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
225 if (upStatus == null && downStatus == null) {
226 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
227 deviceId,
228 deviceVlan,
229 subscriberVlan));
230 } else if (downStatus != null) {
231 log.error("Subscriber with vlan {} on device {} " +
232 "on port {} failed downstream uninstallation: {}",
233 subscriberVlan, deviceId, subscriberPort, downStatus);
234 } else if (upStatus != null) {
235 log.error("Subscriber with vlan {} on device {} " +
236 "on port {} failed upstream uninstallation: {}",
237 subscriberVlan, deviceId, subscriberPort, upStatus);
238 }
239 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800240
Jonathan Harte533a422015-10-20 17:31:24 -0700241 }
242
243 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
244 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800245 VlanId subscriberVlan, VlanId deviceVlan,
246 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700247
alshabib3ea82642016-01-12 18:06:53 -0800248 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
249 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
250
Jonathan Harte533a422015-10-20 17:31:24 -0700251 TrafficSelector upstream = DefaultTrafficSelector.builder()
Jonathan Hart52998382015-11-10 16:09:22 -0800252 .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700253 .matchInPort(subscriberPort)
254 .build();
255
256 TrafficSelector downstream = DefaultTrafficSelector.builder()
257 .matchVlanId(deviceVlan)
258 .matchInPort(uplinkPort)
alshabibb7a9e172016-01-13 11:23:53 -0800259 .matchInnerVlanId(subscriberVlan)
Jonathan Harte533a422015-10-20 17:31:24 -0700260 .build();
261
262 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
alshabib3ea82642016-01-12 18:06:53 -0800263 .pushVlan()
Jonathan Harte533a422015-10-20 17:31:24 -0700264 .setVlanId(subscriberVlan)
265 .pushVlan()
266 .setVlanId(deviceVlan)
267 .setOutput(uplinkPort)
268 .build();
269
270 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
271 .popVlan()
Jonathan Hart52998382015-11-10 16:09:22 -0800272 .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700273 .setOutput(subscriberPort)
274 .build();
275
276
alshabibbf23a1f2016-01-14 17:27:11 -0800277 ForwardingObjective.Builder upFwd = DefaultForwardingObjective.builder()
Jonathan Harte533a422015-10-20 17:31:24 -0700278 .withFlag(ForwardingObjective.Flag.VERSATILE)
279 .withPriority(1000)
280 .makePermanent()
281 .withSelector(upstream)
282 .fromApp(appId)
alshabibbf23a1f2016-01-14 17:27:11 -0800283 .withTreatment(upstreamTreatment);
alshabib3ea82642016-01-12 18:06:53 -0800284
Jonathan Harte533a422015-10-20 17:31:24 -0700285
alshabibbf23a1f2016-01-14 17:27:11 -0800286 ForwardingObjective.Builder downFwd = DefaultForwardingObjective.builder()
Jonathan Harte533a422015-10-20 17:31:24 -0700287 .withFlag(ForwardingObjective.Flag.VERSATILE)
288 .withPriority(1000)
289 .makePermanent()
290 .withSelector(downstream)
291 .fromApp(appId)
alshabibbf23a1f2016-01-14 17:27:11 -0800292 .withTreatment(downstreamTreatment);
alshabib3ea82642016-01-12 18:06:53 -0800293
alshabibbf23a1f2016-01-14 17:27:11 -0800294 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
Jonathan Harte533a422015-10-20 17:31:24 -0700295
alshabibbf23a1f2016-01-14 17:27:11 -0800296 subscribers.put(cp, subscriberVlan);
297 objectives.put(cp, Sets.newHashSet(upFwd, downFwd));
298
299
300 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
301 @Override
302 public void onSuccess(Objective objective) {
303 upFuture.complete(null);
304 }
305
306 @Override
307 public void onError(Objective objective, ObjectiveError error) {
308 upFuture.complete(error);
309 }
310 }));
311
312
313 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
314 @Override
315 public void onSuccess(Objective objective) {
316 downFuture.complete(null);
317 }
318
319 @Override
320 public void onError(Objective objective, ObjectiveError error) {
321 downFuture.complete(error);
322 }
323 }));
alshabib3ea82642016-01-12 18:06:53 -0800324
325 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
326 if (upStatus == null && downStatus == null) {
327 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
328 deviceId,
329 deviceVlan,
330 subscriberVlan));
331 } else if (downStatus != null) {
332 log.error("Subscriber with vlan {} on device {} " +
333 "on port {} failed downstream installation: {}",
334 subscriberVlan, deviceId, subscriberPort, downStatus);
335 } else if (upStatus != null) {
336 log.error("Subscriber with vlan {} on device {} " +
337 "on port {} failed upstream installation: {}",
338 subscriberVlan, deviceId, subscriberPort, upStatus);
339 }
340 }, oltInstallers);
341
Jonathan Harte533a422015-10-20 17:31:24 -0700342 }
343
alshabibdec2e252016-01-15 12:20:25 -0800344 private void installFilteringObjectives(DeviceId devId, Port port) {
345 FilteringObjective eapol = DefaultFilteringObjective.builder()
346 .permit()
347 .withKey(Criteria.matchInPort(port.number()))
348 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
349 .withMeta(DefaultTrafficTreatment.builder()
350 .setOutput(PortNumber.CONTROLLER).build())
351 .fromApp(appId)
352 .withPriority(1000)
353 .add(new ObjectiveContext() {
354 @Override
355 public void onSuccess(Objective objective) {
356 log.info("Eapol filter for {} on {} installed.",
357 devId, port);
358 }
359
360 @Override
361 public void onError(Objective objective, ObjectiveError error) {
362 log.info("Eapol filter for {} on {} failed because {}",
363 devId, port, error);
364 }
365 });
366
367
368 FilteringObjective igmp = DefaultFilteringObjective.builder()
369 .permit()
370 .withKey(Criteria.matchInPort(port.number()))
371 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
372 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
373 .withMeta(DefaultTrafficTreatment.builder()
374 .setOutput(PortNumber.CONTROLLER).build())
375 .fromApp(appId)
376 .withPriority(1000)
377 .add(new ObjectiveContext() {
378 @Override
379 public void onSuccess(Objective objective) {
380 log.info("Igmp filter for {} on {} installed.",
381 devId, port);
382 }
383
384 @Override
385 public void onError(Objective objective, ObjectiveError error) {
386 log.info("Igmp filter for {} on {} failed because {}.",
387 devId, port, error);
388 }
389 });
390
391 flowObjectiveService.filter(devId, eapol);
392 flowObjectiveService.filter(devId, igmp);
393 }
394
alshabibf0e7e702015-05-30 18:22:36 -0700395 private class InternalDeviceListener implements DeviceListener {
396 @Override
397 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800398 DeviceId devId = event.subject().id();
399 if (!oltData.containsKey(devId)) {
400 log.debug("Device {} is not an OLT", devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800401 return;
402 }
alshabibf0e7e702015-05-30 18:22:36 -0700403 switch (event.type()) {
alshabibdec2e252016-01-15 12:20:25 -0800404 //TODO: Port handling and bookkeeping should be inproved once
405 // olt firmware handles correct behaviour.
alshabibf0e7e702015-05-30 18:22:36 -0700406 case PORT_ADDED:
alshabibdec2e252016-01-15 12:20:25 -0800407 if (event.port().isEnabled()) {
408 installFilteringObjectives(devId, event.port());
409 }
410 break;
411 case PORT_REMOVED:
412 AccessDeviceData olt = oltData.get(devId);
413 unprovisionSubscriber(devId, olt.uplink(),
414 event.port().number(),
415 olt.vlan());
416 installFilteringObjectives(devId, event.port());
417 break;
alshabibf0e7e702015-05-30 18:22:36 -0700418 case PORT_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700419 break;
420 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800421 post(new AccessDeviceEvent(
422 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
423 null, null));
424 break;
alshabibf0e7e702015-05-30 18:22:36 -0700425 case DEVICE_REMOVED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800426 post(new AccessDeviceEvent(
427 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
428 null, null));
429 break;
430 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700431 case DEVICE_SUSPENDED:
432 case DEVICE_AVAILABILITY_CHANGED:
alshabibf0e7e702015-05-30 18:22:36 -0700433 case PORT_STATS_UPDATED:
434 default:
435 return;
436 }
437 }
438 }
439
Jonathan Harte533a422015-10-20 17:31:24 -0700440 private class InternalNetworkConfigListener implements NetworkConfigListener {
441 @Override
442 public void event(NetworkConfigEvent event) {
443 switch (event.type()) {
444
alshabibbf23a1f2016-01-14 17:27:11 -0800445 case CONFIG_ADDED:
446 case CONFIG_UPDATED:
447 if (event.configClass().equals(CONFIG_CLASS)) {
448 AccessDeviceConfig config =
449 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
450 if (config != null) {
451 oltData.put(config.getOlt().deviceId(), config.getOlt());
452 }
Jonathan Harte533a422015-10-20 17:31:24 -0700453 }
alshabibbf23a1f2016-01-14 17:27:11 -0800454 break;
455 case CONFIG_UNREGISTERED:
456 case CONFIG_REMOVED:
457 default:
458 break;
Jonathan Harte533a422015-10-20 17:31:24 -0700459 }
460 }
461 }
alshabibf0e7e702015-05-30 18:22:36 -0700462
463}