blob: 33206e9659585e6f6aa9f40c26afdf4306859b5b [file] [log] [blame]
alshabibf0e7e702015-05-30 18:22:36 -07001/*
Brian O'Connord6a135a2017-08-03 22:46:05 -07002 * Copyright 2016-present Open Networking Foundation
alshabibf0e7e702015-05-30 18:22:36 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
alshabib36a4d732016-06-01 16:03:59 -070016package org.opencord.olt.impl;
alshabibf0e7e702015-05-30 18:22:36 -070017
alshabibbf23a1f2016-01-14 17:27:11 -080018import com.google.common.collect.Maps;
alshabibf0e7e702015-05-30 18:22:36 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
alshabibe0559672016-02-21 14:49:51 -080022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
alshabibf0e7e702015-05-30 18:22:36 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070026import org.apache.felix.scr.annotations.Service;
alshabibdec2e252016-01-15 12:20:25 -080027import org.onlab.packet.EthType;
Amit Ghosh95e2f652017-08-23 12:49:46 +010028import org.onlab.packet.IPv4;
29import org.onlab.packet.TpPort;
alshabibf0e7e702015-05-30 18:22:36 -070030import org.onlab.packet.VlanId;
Amit Ghosh95e2f652017-08-23 12:49:46 +010031import org.onlab.util.Tools;
alshabibe0559672016-02-21 14:49:51 -080032import org.onosproject.cfg.ComponentConfigService;
alshabibf0e7e702015-05-30 18:22:36 -070033import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080035import org.onosproject.event.AbstractListenerManager;
alshabib09753b52016-03-04 14:55:19 -080036import org.onosproject.mastership.MastershipService;
Jonathan Harte533a422015-10-20 17:31:24 -070037import org.onosproject.net.ConnectPoint;
alshabibf0e7e702015-05-30 18:22:36 -070038import org.onosproject.net.DeviceId;
alshabibdec2e252016-01-15 12:20:25 -080039import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070040import org.onosproject.net.PortNumber;
Jonathan Harte533a422015-10-20 17:31:24 -070041import org.onosproject.net.config.ConfigFactory;
42import org.onosproject.net.config.NetworkConfigEvent;
43import org.onosproject.net.config.NetworkConfigListener;
44import org.onosproject.net.config.NetworkConfigRegistry;
45import org.onosproject.net.config.basics.SubjectFactories;
alshabibf0e7e702015-05-30 18:22:36 -070046import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.flow.DefaultTrafficSelector;
50import org.onosproject.net.flow.DefaultTrafficTreatment;
51import org.onosproject.net.flow.TrafficSelector;
52import org.onosproject.net.flow.TrafficTreatment;
alshabibdec2e252016-01-15 12:20:25 -080053import org.onosproject.net.flow.criteria.Criteria;
54import org.onosproject.net.flowobjective.DefaultFilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070055import org.onosproject.net.flowobjective.DefaultForwardingObjective;
alshabibdec2e252016-01-15 12:20:25 -080056import org.onosproject.net.flowobjective.FilteringObjective;
alshabibf0e7e702015-05-30 18:22:36 -070057import org.onosproject.net.flowobjective.FlowObjectiveService;
58import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080059import org.onosproject.net.flowobjective.Objective;
60import org.onosproject.net.flowobjective.ObjectiveContext;
61import org.onosproject.net.flowobjective.ObjectiveError;
Jonathan Hart94b90492018-04-24 14:02:25 -070062import org.onosproject.store.serializers.KryoNamespaces;
63import org.onosproject.store.service.Serializer;
64import org.onosproject.store.service.StorageService;
alshabibf3a573e2016-06-01 17:39:10 -070065import org.opencord.cordconfig.access.AccessDeviceConfig;
66import org.opencord.cordconfig.access.AccessDeviceData;
alshabib36a4d732016-06-01 16:03:59 -070067import org.opencord.olt.AccessDeviceEvent;
68import org.opencord.olt.AccessDeviceListener;
69import org.opencord.olt.AccessDeviceService;
alshabibe0559672016-02-21 14:49:51 -080070import org.osgi.service.component.ComponentContext;
alshabibf0e7e702015-05-30 18:22:36 -070071import org.slf4j.Logger;
72
Jonathan Hartfd6c1b32016-03-08 14:09:09 -080073import java.util.Collection;
alshabibe0559672016-02-21 14:49:51 -080074import java.util.Dictionary;
alshabibbb83aa22016-02-10 15:08:23 -080075import java.util.List;
Jonathan Harte533a422015-10-20 17:31:24 -070076import java.util.Map;
Jonathan Hart52998382015-11-10 16:09:22 -080077import java.util.Optional;
alshabibe0559672016-02-21 14:49:51 -080078import java.util.Properties;
alshabib3ea82642016-01-12 18:06:53 -080079import java.util.concurrent.CompletableFuture;
Jonathan Harte533a422015-10-20 17:31:24 -070080import java.util.concurrent.ConcurrentHashMap;
alshabib3ea82642016-01-12 18:06:53 -080081import java.util.concurrent.ExecutorService;
82import java.util.concurrent.Executors;
alshabibf0e7e702015-05-30 18:22:36 -070083
Jonathan Hart94b90492018-04-24 14:02:25 -070084import static com.google.common.base.Preconditions.checkNotNull;
alshabibe0559672016-02-21 14:49:51 -080085import static com.google.common.base.Strings.isNullOrEmpty;
86import static org.onlab.util.Tools.get;
alshabib3ea82642016-01-12 18:06:53 -080087import static org.onlab.util.Tools.groupedThreads;
alshabibf0e7e702015-05-30 18:22:36 -070088import static org.slf4j.LoggerFactory.getLogger;
89
90/**
Jonathan Harte533a422015-10-20 17:31:24 -070091 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -070092 */
Jonathan Harte533a422015-10-20 17:31:24 -070093@Service
alshabibf0e7e702015-05-30 18:22:36 -070094@Component(immediate = true)
alshabib8e4fd2f2016-01-12 15:55:53 -080095public class Olt
96 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
97 implements AccessDeviceService {
Charles Chan54f110f2017-01-20 11:22:42 -080098 private static final String APP_NAME = "org.opencord.olt";
alshabibe0559672016-02-21 14:49:51 -080099
100 private static final short DEFAULT_VLAN = 0;
alshabib4ceaed32016-03-03 18:00:58 -0800101 private static final String SUBSCRIBERS = "existing-subscribers";
alshabibe0559672016-02-21 14:49:51 -0800102
alshabibf0e7e702015-05-30 18:22:36 -0700103 private final Logger log = getLogger(getClass());
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected FlowObjectiveService flowObjectiveService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib09753b52016-03-04 14:55:19 -0800109 protected MastershipService mastershipService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibf0e7e702015-05-30 18:22:36 -0700112 protected DeviceService deviceService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected CoreService coreService;
116
Jonathan Harte533a422015-10-20 17:31:24 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected NetworkConfigRegistry networkConfig;
119
alshabibe0559672016-02-21 14:49:51 -0800120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected ComponentConfigService componentConfigService;
122
alshabib4ceaed32016-03-03 18:00:58 -0800123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected StorageService storageService;
alshabibe0559672016-02-21 14:49:51 -0800125
126 @Property(name = "defaultVlan", intValue = DEFAULT_VLAN,
127 label = "Default VLAN RG<->ONU traffic")
128 private int defaultVlan = DEFAULT_VLAN;
129
Matt Jeanneret3f579262018-06-14 17:16:23 -0400130 @Property(name = "enableDhcpOnProvisioning", boolValue = true,
131 label = "Create the DHCP Flow rules when a subscriber is provisioned")
132 protected boolean enableDhcpOnProvisioning = false;
133
134 @Property(name = "enableIgmpOnProvisioning", boolValue = false,
135 label = "Create IGMP Flow rules when a subscriber is provisioned")
136 protected boolean enableIgmpOnProvisioning = false;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100137
alshabibf0e7e702015-05-30 18:22:36 -0700138 private final DeviceListener deviceListener = new InternalDeviceListener();
139
140 private ApplicationId appId;
141
alshabib3ea82642016-01-12 18:06:53 -0800142 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
alshabibbf23a1f2016-01-14 17:27:11 -0800143 groupedThreads("onos/olt-service",
144 "olt-installer-%d"));
alshabib4ae2b402015-06-05 14:55:24 -0700145
Jonathan Harte533a422015-10-20 17:31:24 -0700146 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
147
alshabib4ceaed32016-03-03 18:00:58 -0800148 private Map<ConnectPoint, VlanId> subscribers;
alshabibbf23a1f2016-01-14 17:27:11 -0800149
Jonathan Harte533a422015-10-20 17:31:24 -0700150 private InternalNetworkConfigListener configListener =
151 new InternalNetworkConfigListener();
152 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
153 AccessDeviceConfig.class;
154
155 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
156 new ConfigFactory<DeviceId, AccessDeviceConfig>(
157 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
alshabibbf23a1f2016-01-14 17:27:11 -0800158 @Override
159 public AccessDeviceConfig createConfig() {
160 return new AccessDeviceConfig();
161 }
162 };
163
alshabibf0e7e702015-05-30 18:22:36 -0700164
165 @Activate
alshabibe0559672016-02-21 14:49:51 -0800166 public void activate(ComponentContext context) {
167 modified(context);
Charles Chan54f110f2017-01-20 11:22:42 -0800168 appId = coreService.registerApplication(APP_NAME);
alshabibe0559672016-02-21 14:49:51 -0800169 componentConfigService.registerProperties(getClass());
alshabibc4dfe852015-06-05 13:35:13 -0700170
alshabib8e4fd2f2016-01-12 15:55:53 -0800171 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
172
Jonathan Harte533a422015-10-20 17:31:24 -0700173 networkConfig.registerConfigFactory(configFactory);
174 networkConfig.addListener(configListener);
175
alshabibdec2e252016-01-15 12:20:25 -0800176
Jonathan Harte533a422015-10-20 17:31:24 -0700177 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
178 subject -> {
179 AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class);
180 if (config != null) {
181 AccessDeviceData data = config.getOlt();
182 oltData.put(data.deviceId(), data);
183 }
184 }
185 );
186
alshabibdec2e252016-01-15 12:20:25 -0800187 oltData.keySet().stream()
188 .flatMap(did -> deviceService.getPorts(did).stream())
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700189 .filter(this::isUni)
190 .forEach(this::initializeUni);
alshabibdec2e252016-01-15 12:20:25 -0800191
alshabib4ceaed32016-03-03 18:00:58 -0800192 subscribers = storageService.<ConnectPoint, VlanId>consistentMapBuilder()
193 .withName(SUBSCRIBERS)
194 .withSerializer(Serializer.using(KryoNamespaces.API))
195 .build().asJavaMap();
196
alshabibba357492016-01-27 13:49:46 -0800197 deviceService.addListener(deviceListener);
198
alshabibf0e7e702015-05-30 18:22:36 -0700199 log.info("Started with Application ID {}", appId.id());
200 }
201
202 @Deactivate
203 public void deactivate() {
alshabibe0559672016-02-21 14:49:51 -0800204 componentConfigService.unregisterProperties(getClass(), false);
alshabib62e9ce72016-02-11 17:31:36 -0800205 deviceService.removeListener(deviceListener);
Jonathan Harte533a422015-10-20 17:31:24 -0700206 networkConfig.removeListener(configListener);
207 networkConfig.unregisterConfigFactory(configFactory);
alshabibf0e7e702015-05-30 18:22:36 -0700208 log.info("Stopped");
209 }
210
alshabibe0559672016-02-21 14:49:51 -0800211 @Modified
212 public void modified(ComponentContext context) {
213 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
214
215 try {
216 String s = get(properties, "defaultVlan");
217 defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim());
Amit Ghosh95e2f652017-08-23 12:49:46 +0100218
Matt Jeanneret3f579262018-06-14 17:16:23 -0400219 Boolean o = Tools.isPropertyEnabled(properties, "enableDhcpOnProvisioning");
Amit Ghosh95e2f652017-08-23 12:49:46 +0100220 if (o != null) {
Matt Jeanneret3f579262018-06-14 17:16:23 -0400221 enableDhcpOnProvisioning = o;
Amit Ghosh95e2f652017-08-23 12:49:46 +0100222 }
Matt Jeanneret3f579262018-06-14 17:16:23 -0400223
224 Boolean p = Tools.isPropertyEnabled(properties, "enableIgmpOnProvisioning");
225 if (p != null) {
226 enableIgmpOnProvisioning = p;
227 }
228
alshabibe0559672016-02-21 14:49:51 -0800229 } catch (Exception e) {
230 defaultVlan = DEFAULT_VLAN;
231 }
232 }
233
alshabib32232c82016-02-25 17:57:24 -0500234 @Override
Jonathan Harte533a422015-10-20 17:31:24 -0700235 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
Jonathan Hart94b90492018-04-24 14:02:25 -0700236 checkNotNull(deviceService.getPort(port.deviceId(), port.port()),
237 "Invalid connect point");
Jonathan Harte533a422015-10-20 17:31:24 -0700238 AccessDeviceData olt = oltData.get(port.deviceId());
239
240 if (olt == null) {
241 log.warn("No data found for OLT device {}", port.deviceId());
Jonathan Hart94b90492018-04-24 14:02:25 -0700242 throw new IllegalArgumentException("Missing OLT configuration for device");
Jonathan Harte533a422015-10-20 17:31:24 -0700243 }
244
Matt Jeanneret3f579262018-06-14 17:16:23 -0400245 if (enableDhcpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100246 processDhcpFilteringObjectives(olt.deviceId(), port.port(), true);
247 }
248
Jonathan Hart52998382015-11-10 16:09:22 -0800249 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
Amit Ghosh95e2f652017-08-23 12:49:46 +0100250 olt.defaultVlan());
251
Matt Jeanneret3f579262018-06-14 17:16:23 -0400252 if (enableIgmpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100253 processIgmpFilteringObjectives(olt.deviceId(), port.port(), true);
254 }
alshabibb7a9e172016-01-13 11:23:53 -0800255 }
256
257 @Override
258 public void removeSubscriber(ConnectPoint port) {
alshabibbf23a1f2016-01-14 17:27:11 -0800259 AccessDeviceData olt = oltData.get(port.deviceId());
260
261 if (olt == null) {
262 log.warn("No data found for OLT device {}", port.deviceId());
263 return;
264 }
265
alshabib4ceaed32016-03-03 18:00:58 -0800266 VlanId subscriberVlan = subscribers.remove(port);
267
268 if (subscriberVlan == null) {
269 log.warn("Unknown subscriber at location {}", port);
270 return;
271 }
272
Matt Jeanneret3f579262018-06-14 17:16:23 -0400273 if (enableDhcpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100274 processDhcpFilteringObjectives(olt.deviceId(), port.port(), false);
275 }
276
alshabib4ceaed32016-03-03 18:00:58 -0800277 unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), subscriberVlan,
278 olt.vlan(), olt.defaultVlan());
alshabibbf23a1f2016-01-14 17:27:11 -0800279
Matt Jeanneret3f579262018-06-14 17:16:23 -0400280 if (enableIgmpOnProvisioning) {
Amit Ghosh95e2f652017-08-23 12:49:46 +0100281 processIgmpFilteringObjectives(olt.deviceId(), port.port(), false);
282 }
alshabibbf23a1f2016-01-14 17:27:11 -0800283 }
284
Amit Ghosh95e2f652017-08-23 12:49:46 +0100285
alshabibe0559672016-02-21 14:49:51 -0800286 @Override
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800287 public Collection<Map.Entry<ConnectPoint, VlanId>> getSubscribers() {
288 return subscribers.entrySet();
289 }
290
291 @Override
alshabibe0559672016-02-21 14:49:51 -0800292 public Map<DeviceId, AccessDeviceData> fetchOlts() {
293 return Maps.newHashMap(oltData);
294 }
295
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700296 private void initializeUni(Port port) {
297 DeviceId deviceId = (DeviceId) port.element().id();
298
299 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, deviceId, port));
300
301 if (port.isEnabled()) {
302 processFilteringObjectives(deviceId, port.number(), true);
303 }
304 }
305
alshabibbf23a1f2016-01-14 17:27:11 -0800306 private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
alshabib4ceaed32016-03-03 18:00:58 -0800307 PortNumber subscriberPort, VlanId subscriberVlan,
308 VlanId deviceVlan, Optional<VlanId> defaultVlan) {
alshabibbf23a1f2016-01-14 17:27:11 -0800309
310 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
311 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
312
alshabib4ceaed32016-03-03 18:00:58 -0800313 ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort,
314 subscriberVlan, deviceVlan,
315 defaultVlan);
316 ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort,
317 subscriberVlan, deviceVlan,
318 defaultVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800319
320
alshabib4ceaed32016-03-03 18:00:58 -0800321 flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() {
322 @Override
323 public void onSuccess(Objective objective) {
324 upFuture.complete(null);
325 }
alshabibbf23a1f2016-01-14 17:27:11 -0800326
alshabib4ceaed32016-03-03 18:00:58 -0800327 @Override
328 public void onError(Objective objective, ObjectiveError error) {
329 upFuture.complete(error);
330 }
331 }));
332
333 flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() {
334 @Override
335 public void onSuccess(Objective objective) {
336 downFuture.complete(null);
337 }
338
339 @Override
340 public void onError(Objective objective, ObjectiveError error) {
341 downFuture.complete(error);
342 }
343 }));
alshabibbf23a1f2016-01-14 17:27:11 -0800344
345 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
346 if (upStatus == null && downStatus == null) {
347 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED,
348 deviceId,
349 deviceVlan,
350 subscriberVlan));
351 } else if (downStatus != null) {
352 log.error("Subscriber with vlan {} on device {} " +
353 "on port {} failed downstream uninstallation: {}",
354 subscriberVlan, deviceId, subscriberPort, downStatus);
355 } else if (upStatus != null) {
356 log.error("Subscriber with vlan {} on device {} " +
357 "on port {} failed upstream uninstallation: {}",
358 subscriberVlan, deviceId, subscriberPort, upStatus);
359 }
360 }, oltInstallers);
alshabibb7a9e172016-01-13 11:23:53 -0800361
Jonathan Harte533a422015-10-20 17:31:24 -0700362 }
363
364 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
365 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800366 VlanId subscriberVlan, VlanId deviceVlan,
367 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700368
alshabib3ea82642016-01-12 18:06:53 -0800369 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
370 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
371
alshabib4ceaed32016-03-03 18:00:58 -0800372 ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort,
373 subscriberVlan, deviceVlan,
374 defaultVlan);
Jonathan Harte533a422015-10-20 17:31:24 -0700375
376
alshabib4ceaed32016-03-03 18:00:58 -0800377 ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort,
378 subscriberVlan, deviceVlan,
379 defaultVlan);
alshabib3ea82642016-01-12 18:06:53 -0800380
alshabibbf23a1f2016-01-14 17:27:11 -0800381 ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort);
alshabibbf23a1f2016-01-14 17:27:11 -0800382 subscribers.put(cp, subscriberVlan);
alshabibbf23a1f2016-01-14 17:27:11 -0800383
alshabibbf23a1f2016-01-14 17:27:11 -0800384 flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() {
385 @Override
386 public void onSuccess(Objective objective) {
387 upFuture.complete(null);
388 }
389
390 @Override
391 public void onError(Objective objective, ObjectiveError error) {
392 upFuture.complete(error);
393 }
394 }));
395
alshabibbf23a1f2016-01-14 17:27:11 -0800396 flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() {
397 @Override
398 public void onSuccess(Objective objective) {
399 downFuture.complete(null);
400 }
401
402 @Override
403 public void onError(Objective objective, ObjectiveError error) {
404 downFuture.complete(error);
405 }
406 }));
alshabib3ea82642016-01-12 18:06:53 -0800407
408 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
409 if (upStatus == null && downStatus == null) {
410 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
411 deviceId,
412 deviceVlan,
413 subscriberVlan));
alshabib50d9fc52016-02-12 15:47:20 -0800414
alshabib3ea82642016-01-12 18:06:53 -0800415 } else if (downStatus != null) {
416 log.error("Subscriber with vlan {} on device {} " +
417 "on port {} failed downstream installation: {}",
418 subscriberVlan, deviceId, subscriberPort, downStatus);
419 } else if (upStatus != null) {
420 log.error("Subscriber with vlan {} on device {} " +
421 "on port {} failed upstream installation: {}",
422 subscriberVlan, deviceId, subscriberPort, upStatus);
423 }
424 }, oltInstallers);
425
Jonathan Harte533a422015-10-20 17:31:24 -0700426 }
427
alshabib4ceaed32016-03-03 18:00:58 -0800428 private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort,
429 PortNumber subscriberPort,
430 VlanId subscriberVlan,
431 VlanId deviceVlan,
432 Optional<VlanId> defaultVlan) {
433 TrafficSelector downstream = DefaultTrafficSelector.builder()
434 .matchVlanId(deviceVlan)
435 .matchInPort(uplinkPort)
436 .matchInnerVlanId(subscriberVlan)
437 .build();
438
439 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
440 .popVlan()
441 .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
442 .setOutput(subscriberPort)
443 .build();
444
445 return DefaultForwardingObjective.builder()
446 .withFlag(ForwardingObjective.Flag.VERSATILE)
447 .withPriority(1000)
448 .makePermanent()
449 .withSelector(downstream)
450 .fromApp(appId)
451 .withTreatment(downstreamTreatment);
452 }
453
454 private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort,
455 PortNumber subscriberPort,
456 VlanId subscriberVlan,
457 VlanId deviceVlan,
458 Optional<VlanId> defaultVlan) {
459 TrafficSelector upstream = DefaultTrafficSelector.builder()
460 .matchVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
461 .matchInPort(subscriberPort)
462 .build();
463
464
465 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
466 .pushVlan()
467 .setVlanId(subscriberVlan)
468 .pushVlan()
469 .setVlanId(deviceVlan)
470 .setOutput(uplinkPort)
471 .build();
472
473 return DefaultForwardingObjective.builder()
474 .withFlag(ForwardingObjective.Flag.VERSATILE)
475 .withPriority(1000)
476 .makePermanent()
477 .withSelector(upstream)
478 .fromApp(appId)
479 .withTreatment(upstreamTreatment);
480 }
481
alshabib50d9fc52016-02-12 15:47:20 -0800482 private void processFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
alshabib09753b52016-03-04 14:55:19 -0800483 if (!mastershipService.isLocalMaster(devId)) {
484 return;
485 }
alshabibbb83aa22016-02-10 15:08:23 -0800486 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
487
488 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
alshabib50d9fc52016-02-12 15:47:20 -0800489 .withKey(Criteria.matchInPort(port))
alshabibdec2e252016-01-15 12:20:25 -0800490 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
491 .withMeta(DefaultTrafficTreatment.builder()
492 .setOutput(PortNumber.CONTROLLER).build())
493 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -0400494 .withPriority(10000)
alshabibdec2e252016-01-15 12:20:25 -0800495 .add(new ObjectiveContext() {
496 @Override
497 public void onSuccess(Objective objective) {
498 log.info("Eapol filter for {} on {} installed.",
499 devId, port);
500 }
501
502 @Override
503 public void onError(Objective objective, ObjectiveError error) {
504 log.info("Eapol filter for {} on {} failed because {}",
505 devId, port, error);
506 }
507 });
508
alshabibdec2e252016-01-15 12:20:25 -0800509 flowObjectiveService.filter(devId, eapol);
alshabib000b6fc2016-02-01 17:25:00 -0800510
alshabibdec2e252016-01-15 12:20:25 -0800511 }
512
Amit Ghosh95e2f652017-08-23 12:49:46 +0100513 private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
514 if (!mastershipService.isLocalMaster(devId)) {
515 return;
516 }
517 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
518
519 FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny())
520 .withKey(Criteria.matchInPort(port))
521 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
522 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_UDP))
523 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(68)))
524 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(67)))
525 .withMeta(DefaultTrafficTreatment.builder()
526 .setOutput(PortNumber.CONTROLLER).build())
527 .fromApp(appId)
Matt Jeanneret3f579262018-06-14 17:16:23 -0400528 .withPriority(10000)
Amit Ghosh95e2f652017-08-23 12:49:46 +0100529 .add(new ObjectiveContext() {
530 @Override
531 public void onSuccess(Objective objective) {
532 log.info("DHCP filter for {} on {} installed.",
533 devId, port);
534 }
535
536 @Override
537 public void onError(Objective objective, ObjectiveError error) {
538 log.info("DHCP filter for {} on {} failed because {}",
539 devId, port, error);
540 }
541 });
542
543 flowObjectiveService.filter(devId, dhcpUpstream);
544 }
545
546 private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
547 if (!mastershipService.isLocalMaster(devId)) {
548 return;
549 }
550
551 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
552
553 builder = install ? builder.permit() : builder.deny();
554
555 FilteringObjective igmp = builder
556 .withKey(Criteria.matchInPort(port))
557 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
558 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
559 .withMeta(DefaultTrafficTreatment.builder()
560 .setOutput(PortNumber.CONTROLLER).build())
561 .fromApp(appId)
562 .withPriority(10000)
563 .add(new ObjectiveContext() {
564 @Override
565 public void onSuccess(Objective objective) {
566 log.info("Igmp filter for {} on {} installed.",
567 devId, port);
568 }
569
570 @Override
571 public void onError(Objective objective, ObjectiveError error) {
572 log.info("Igmp filter for {} on {} failed because {}.",
573 devId, port, error);
574 }
575 });
576
577 flowObjectiveService.filter(devId, igmp);
578 }
579
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700580 private boolean isUni(Port port) {
581 return !oltData.get(port.element().id()).uplink().equals(port.number());
582 }
583
alshabibf0e7e702015-05-30 18:22:36 -0700584 private class InternalDeviceListener implements DeviceListener {
585 @Override
586 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800587 DeviceId devId = event.subject().id();
588 if (!oltData.containsKey(devId)) {
alshabib8e4fd2f2016-01-12 15:55:53 -0800589 return;
590 }
Jonathan Hart6960f822018-07-02 15:19:14 -0700591 if (event.type() != DeviceEvent.Type.PORT_STATS_UPDATED) {
592 log.debug("Olt got {} event for {}", event.type(), event.subject());
593 }
alshabibf0e7e702015-05-30 18:22:36 -0700594 switch (event.type()) {
Jonathan Hartfd6c1b32016-03-08 14:09:09 -0800595 //TODO: Port handling and bookkeeping should be improved once
alshabibdec2e252016-01-15 12:20:25 -0800596 // olt firmware handles correct behaviour.
alshabibf0e7e702015-05-30 18:22:36 -0700597 case PORT_ADDED:
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700598 if (!oltData.get(devId).uplink().equals(event.port().number())) {
599 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port()));
600
601 if (event.port().isEnabled()) {
602 processFilteringObjectives(devId, event.port().number(), true);
603 }
alshabibdec2e252016-01-15 12:20:25 -0800604 }
605 break;
606 case PORT_REMOVED:
607 AccessDeviceData olt = oltData.get(devId);
alshabib4ceaed32016-03-03 18:00:58 -0800608 VlanId vlan = subscribers.get(new ConnectPoint(devId,
609 event.port().number()));
dvaddirec63dc822017-10-05 22:25:13 +0530610 if (vlan != null) {
611 unprovisionSubscriber(devId, olt.uplink(),
612 event.port().number(),
613 vlan, olt.vlan(), olt.defaultVlan());
614 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700615 if (!oltData.get(devId).uplink().equals(event.port().number())) {
616 if (event.port().isEnabled()) {
617 processFilteringObjectives(devId, event.port().number(), false);
618 }
619
620 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, event.port()));
alshabibbb83aa22016-02-10 15:08:23 -0800621 }
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700622
alshabibdec2e252016-01-15 12:20:25 -0800623 break;
alshabibf0e7e702015-05-30 18:22:36 -0700624 case PORT_UPDATED:
alshabibbb83aa22016-02-10 15:08:23 -0800625 if (oltData.get(devId).uplink().equals(event.port().number())) {
626 break;
627 }
628 if (event.port().isEnabled()) {
alshabib50d9fc52016-02-12 15:47:20 -0800629 processFilteringObjectives(devId, event.port().number(), true);
Jonathan Hart6960f822018-07-02 15:19:14 -0700630 post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port()));
alshabibbb83aa22016-02-10 15:08:23 -0800631 } else {
alshabib50d9fc52016-02-12 15:47:20 -0800632 processFilteringObjectives(devId, event.port().number(), false);
alshabibbb83aa22016-02-10 15:08:23 -0800633 }
alshabibf0e7e702015-05-30 18:22:36 -0700634 break;
635 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800636 post(new AccessDeviceEvent(
637 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
638 null, null));
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700639
640 // Send UNI_ADDED events for all existing ports
641 deviceService.getPorts(devId).stream()
642 .filter(Olt.this::isUni)
Jonathan Hart98de8a72018-05-30 17:47:01 -0700643 .filter(Port::isEnabled)
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700644 .forEach(p -> post(new AccessDeviceEvent(
645 AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
646
alshabibe0559672016-02-21 14:49:51 -0800647 provisionDefaultFlows(devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800648 break;
alshabibf0e7e702015-05-30 18:22:36 -0700649 case DEVICE_REMOVED:
Jonathan Hart1d34c8b2018-05-05 15:37:28 -0700650 deviceService.getPorts(devId).stream()
651 .filter(Olt.this::isUni)
652 .forEach(p -> post(new AccessDeviceEvent(
653 AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
654
alshabib8e4fd2f2016-01-12 15:55:53 -0800655 post(new AccessDeviceEvent(
656 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
657 null, null));
658 break;
alshabib7c190012016-02-09 18:22:33 -0800659 case DEVICE_AVAILABILITY_CHANGED:
660 if (deviceService.isAvailable(devId)) {
661 post(new AccessDeviceEvent(
662 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
663 null, null));
664 } else {
665 post(new AccessDeviceEvent(
666 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
667 null, null));
668 }
669 break;
alshabib8e4fd2f2016-01-12 15:55:53 -0800670 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700671 case DEVICE_SUSPENDED:
alshabibf0e7e702015-05-30 18:22:36 -0700672 case PORT_STATS_UPDATED:
673 default:
674 return;
675 }
676 }
677 }
678
Jonathan Harte533a422015-10-20 17:31:24 -0700679 private class InternalNetworkConfigListener implements NetworkConfigListener {
680 @Override
681 public void event(NetworkConfigEvent event) {
682 switch (event.type()) {
683
alshabibbf23a1f2016-01-14 17:27:11 -0800684 case CONFIG_ADDED:
685 case CONFIG_UPDATED:
alshabibe0559672016-02-21 14:49:51 -0800686
687 AccessDeviceConfig config =
688 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
689 if (config != null) {
690 oltData.put(config.getOlt().deviceId(), config.getOlt());
691 provisionDefaultFlows((DeviceId) event.subject());
Jonathan Harte533a422015-10-20 17:31:24 -0700692 }
alshabibe0559672016-02-21 14:49:51 -0800693
alshabibbf23a1f2016-01-14 17:27:11 -0800694 break;
alshabibe0559672016-02-21 14:49:51 -0800695 case CONFIG_REGISTERED:
alshabibbf23a1f2016-01-14 17:27:11 -0800696 case CONFIG_UNREGISTERED:
alshabibe0559672016-02-21 14:49:51 -0800697 break;
alshabibbf23a1f2016-01-14 17:27:11 -0800698 case CONFIG_REMOVED:
alshabibe0559672016-02-21 14:49:51 -0800699 oltData.remove(event.subject());
alshabibbf23a1f2016-01-14 17:27:11 -0800700 default:
701 break;
Jonathan Harte533a422015-10-20 17:31:24 -0700702 }
703 }
alshabibe0559672016-02-21 14:49:51 -0800704
705 @Override
706 public boolean isRelevant(NetworkConfigEvent event) {
707 return event.configClass().equals(CONFIG_CLASS);
708 }
Jonathan Harte533a422015-10-20 17:31:24 -0700709 }
alshabibf0e7e702015-05-30 18:22:36 -0700710
alshabibbb83aa22016-02-10 15:08:23 -0800711 private void provisionDefaultFlows(DeviceId deviceId) {
alshabib09753b52016-03-04 14:55:19 -0800712 if (!mastershipService.isLocalMaster(deviceId)) {
713 return;
714 }
alshabibbb83aa22016-02-10 15:08:23 -0800715 List<Port> ports = deviceService.getPorts(deviceId);
716
717 ports.stream()
718 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
719 .filter(p -> p.isEnabled())
alshabib50d9fc52016-02-12 15:47:20 -0800720 .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(),
721 p.number(), true));
alshabibbb83aa22016-02-10 15:08:23 -0800722
723 }
724
alshabibf0e7e702015-05-30 18:22:36 -0700725}