alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Vavilapalli | 0d21dd1 | 2016-01-16 19:21:59 -0800 | [diff] [blame] | 16 | package org.onosproject.olt.impl; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 17 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
| 19 | import com.google.common.collect.Sets; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 28 | import org.onlab.packet.EthType; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigService; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 33 | import org.onosproject.event.AbstractListenerManager; |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Port; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 42 | import org.onosproject.net.config.basics.SubjectFactories; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceEvent; |
| 44 | import org.onosproject.net.device.DeviceListener; |
| 45 | import org.onosproject.net.device.DeviceService; |
| 46 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.criteria.Criteria; |
| 51 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.FilteringObjective; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 55 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flowobjective.Objective; |
| 57 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 58 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 59 | import org.onosproject.olt.AccessDeviceConfig; |
| 60 | import org.onosproject.olt.AccessDeviceData; |
Srikanth Vavilapalli | 0d21dd1 | 2016-01-16 19:21:59 -0800 | [diff] [blame] | 61 | import org.onosproject.olt.AccessDeviceEvent; |
| 62 | import org.onosproject.olt.AccessDeviceListener; |
| 63 | import org.onosproject.olt.AccessDeviceService; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 64 | import org.osgi.service.component.ComponentContext; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 65 | import org.slf4j.Logger; |
| 66 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 67 | import java.util.Dictionary; |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 68 | import java.util.List; |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 69 | import java.util.Map; |
Jonathan Hart | 4832784 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 70 | import java.util.Optional; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 71 | import java.util.Properties; |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 72 | import java.util.Set; |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 73 | import java.util.concurrent.CompletableFuture; |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 74 | import java.util.concurrent.ConcurrentHashMap; |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 75 | import java.util.concurrent.ExecutorService; |
| 76 | import java.util.concurrent.Executors; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 77 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 78 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 79 | import static org.onlab.util.Tools.get; |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 80 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 81 | import static org.slf4j.LoggerFactory.getLogger; |
| 82 | |
| 83 | /** |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 84 | * Provisions rules on access devices. |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 85 | */ |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 86 | @Service |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 87 | @Component(immediate = true) |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 88 | public class Olt |
| 89 | extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener> |
| 90 | implements AccessDeviceService { |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 91 | |
| 92 | private static final short DEFAULT_VLAN = 0; |
| 93 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 94 | private final Logger log = getLogger(getClass()); |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected FlowObjectiveService flowObjectiveService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected DeviceService deviceService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected CoreService coreService; |
| 104 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected NetworkConfigRegistry networkConfig; |
| 107 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected ComponentConfigService componentConfigService; |
| 110 | |
| 111 | |
| 112 | @Property(name = "defaultVlan", intValue = DEFAULT_VLAN, |
| 113 | label = "Default VLAN RG<->ONU traffic") |
| 114 | private int defaultVlan = DEFAULT_VLAN; |
| 115 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 116 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 117 | |
| 118 | private ApplicationId appId; |
| 119 | |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 120 | private ExecutorService oltInstallers = Executors.newFixedThreadPool(4, |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 121 | groupedThreads("onos/olt-service", |
| 122 | "olt-installer-%d")); |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 123 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 124 | private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>(); |
| 125 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 126 | private Map<ConnectPoint, Set<ForwardingObjective.Builder>> objectives = |
| 127 | Maps.newConcurrentMap(); |
| 128 | |
| 129 | private Map<ConnectPoint, VlanId> subscribers = Maps.newConcurrentMap(); |
| 130 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 131 | private InternalNetworkConfigListener configListener = |
| 132 | new InternalNetworkConfigListener(); |
| 133 | private static final Class<AccessDeviceConfig> CONFIG_CLASS = |
| 134 | AccessDeviceConfig.class; |
| 135 | |
| 136 | private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory = |
| 137 | new ConfigFactory<DeviceId, AccessDeviceConfig>( |
| 138 | SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") { |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 139 | @Override |
| 140 | public AccessDeviceConfig createConfig() { |
| 141 | return new AccessDeviceConfig(); |
| 142 | } |
| 143 | }; |
| 144 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 145 | |
| 146 | @Activate |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 147 | public void activate(ComponentContext context) { |
| 148 | modified(context); |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 149 | appId = coreService.registerApplication("org.onosproject.olt"); |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 150 | componentConfigService.registerProperties(getClass()); |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 151 | |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 152 | eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry); |
| 153 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 154 | networkConfig.registerConfigFactory(configFactory); |
| 155 | networkConfig.addListener(configListener); |
| 156 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 157 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 158 | networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach( |
| 159 | subject -> { |
| 160 | AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class); |
| 161 | if (config != null) { |
| 162 | AccessDeviceData data = config.getOlt(); |
| 163 | oltData.put(data.deviceId(), data); |
| 164 | } |
| 165 | } |
| 166 | ); |
| 167 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 168 | oltData.keySet().stream() |
| 169 | .flatMap(did -> deviceService.getPorts(did).stream()) |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 170 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 171 | .filter(p -> p.isEnabled()) |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 172 | .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(), |
| 173 | p.number(), true)); |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 174 | |
alshabib | 87f90e4 | 2016-01-27 13:49:46 -0800 | [diff] [blame] | 175 | deviceService.addListener(deviceListener); |
| 176 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 177 | log.info("Started with Application ID {}", appId.id()); |
| 178 | } |
| 179 | |
| 180 | @Deactivate |
| 181 | public void deactivate() { |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 182 | componentConfigService.unregisterProperties(getClass(), false); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 183 | deviceService.removeListener(deviceListener); |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 184 | networkConfig.removeListener(configListener); |
| 185 | networkConfig.unregisterConfigFactory(configFactory); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 186 | log.info("Stopped"); |
| 187 | } |
| 188 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 189 | @Modified |
| 190 | public void modified(ComponentContext context) { |
| 191 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 192 | |
| 193 | try { |
| 194 | String s = get(properties, "defaultVlan"); |
| 195 | defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim()); |
| 196 | } catch (Exception e) { |
| 197 | defaultVlan = DEFAULT_VLAN; |
| 198 | } |
| 199 | } |
| 200 | |
alshabib | 56efe43 | 2016-02-25 17:57:24 -0500 | [diff] [blame^] | 201 | @Override |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 202 | public void provisionSubscriber(ConnectPoint port, VlanId vlan) { |
| 203 | AccessDeviceData olt = oltData.get(port.deviceId()); |
| 204 | |
| 205 | if (olt == null) { |
| 206 | log.warn("No data found for OLT device {}", port.deviceId()); |
| 207 | return; |
| 208 | } |
| 209 | |
Jonathan Hart | 4832784 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 210 | provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(), |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 211 | olt.defaultVlan()); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void removeSubscriber(ConnectPoint port) { |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 216 | AccessDeviceData olt = oltData.get(port.deviceId()); |
| 217 | |
| 218 | if (olt == null) { |
| 219 | log.warn("No data found for OLT device {}", port.deviceId()); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), olt.vlan()); |
| 224 | |
| 225 | } |
| 226 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 227 | @Override |
| 228 | public Map<DeviceId, AccessDeviceData> fetchOlts() { |
| 229 | return Maps.newHashMap(oltData); |
| 230 | } |
| 231 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 232 | private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink, |
| 233 | PortNumber subscriberPort, VlanId deviceVlan) { |
| 234 | |
| 235 | //FIXME: This method is slightly ugly but it'll do until we have a better |
| 236 | // way to remove flows from the flow store. |
| 237 | |
| 238 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 239 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 240 | |
| 241 | ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort); |
| 242 | |
| 243 | VlanId subscriberVlan = subscribers.remove(cp); |
| 244 | |
| 245 | Set<ForwardingObjective.Builder> fwds = objectives.remove(cp); |
| 246 | |
| 247 | if (fwds == null || fwds.size() != 2) { |
| 248 | log.warn("Unknown or incomplete subscriber at {}", cp); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | fwds.stream().forEach( |
| 254 | fwd -> flowObjectiveService.forward(deviceId, |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 255 | fwd.remove(new ObjectiveContext() { |
| 256 | @Override |
| 257 | public void onSuccess(Objective objective) { |
| 258 | upFuture.complete(null); |
| 259 | } |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 260 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 261 | @Override |
| 262 | public void onError(Objective objective, ObjectiveError error) { |
| 263 | upFuture.complete(error); |
| 264 | } |
| 265 | }))); |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 266 | |
| 267 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 268 | if (upStatus == null && downStatus == null) { |
| 269 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED, |
| 270 | deviceId, |
| 271 | deviceVlan, |
| 272 | subscriberVlan)); |
| 273 | } else if (downStatus != null) { |
| 274 | log.error("Subscriber with vlan {} on device {} " + |
| 275 | "on port {} failed downstream uninstallation: {}", |
| 276 | subscriberVlan, deviceId, subscriberPort, downStatus); |
| 277 | } else if (upStatus != null) { |
| 278 | log.error("Subscriber with vlan {} on device {} " + |
| 279 | "on port {} failed upstream uninstallation: {}", |
| 280 | subscriberVlan, deviceId, subscriberPort, upStatus); |
| 281 | } |
| 282 | }, oltInstallers); |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 283 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort, |
| 287 | PortNumber subscriberPort, |
Jonathan Hart | 4832784 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 288 | VlanId subscriberVlan, VlanId deviceVlan, |
| 289 | Optional<VlanId> defaultVlan) { |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 290 | |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 291 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 292 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 293 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 294 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 295 | .matchVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan))) |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 296 | .matchInPort(subscriberPort) |
| 297 | .build(); |
| 298 | |
| 299 | TrafficSelector downstream = DefaultTrafficSelector.builder() |
| 300 | .matchVlanId(deviceVlan) |
| 301 | .matchInPort(uplinkPort) |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 302 | .matchInnerVlanId(subscriberVlan) |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 303 | .build(); |
| 304 | |
| 305 | TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder() |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 306 | .pushVlan() |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 307 | .setVlanId(subscriberVlan) |
| 308 | .pushVlan() |
| 309 | .setVlanId(deviceVlan) |
| 310 | .setOutput(uplinkPort) |
| 311 | .build(); |
| 312 | |
| 313 | TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder() |
| 314 | .popVlan() |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 315 | .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan))) |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 316 | .setOutput(subscriberPort) |
| 317 | .build(); |
| 318 | |
| 319 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 320 | ForwardingObjective.Builder upFwd = DefaultForwardingObjective.builder() |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 321 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 322 | .withPriority(1000) |
| 323 | .makePermanent() |
| 324 | .withSelector(upstream) |
| 325 | .fromApp(appId) |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 326 | .withTreatment(upstreamTreatment); |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 327 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 328 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 329 | ForwardingObjective.Builder downFwd = DefaultForwardingObjective.builder() |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 330 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 331 | .withPriority(1000) |
| 332 | .makePermanent() |
| 333 | .withSelector(downstream) |
| 334 | .fromApp(appId) |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 335 | .withTreatment(downstreamTreatment); |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 336 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 337 | ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort); |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 338 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 339 | subscribers.put(cp, subscriberVlan); |
| 340 | objectives.put(cp, Sets.newHashSet(upFwd, downFwd)); |
| 341 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 342 | flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() { |
| 343 | @Override |
| 344 | public void onSuccess(Objective objective) { |
| 345 | upFuture.complete(null); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void onError(Objective objective, ObjectiveError error) { |
| 350 | upFuture.complete(error); |
| 351 | } |
| 352 | })); |
| 353 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 354 | flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() { |
| 355 | @Override |
| 356 | public void onSuccess(Objective objective) { |
| 357 | downFuture.complete(null); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void onError(Objective objective, ObjectiveError error) { |
| 362 | downFuture.complete(error); |
| 363 | } |
| 364 | })); |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 365 | |
| 366 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 367 | if (upStatus == null && downStatus == null) { |
| 368 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED, |
| 369 | deviceId, |
| 370 | deviceVlan, |
| 371 | subscriberVlan)); |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 372 | |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 373 | } else if (downStatus != null) { |
| 374 | log.error("Subscriber with vlan {} on device {} " + |
| 375 | "on port {} failed downstream installation: {}", |
| 376 | subscriberVlan, deviceId, subscriberPort, downStatus); |
| 377 | } else if (upStatus != null) { |
| 378 | log.error("Subscriber with vlan {} on device {} " + |
| 379 | "on port {} failed upstream installation: {}", |
| 380 | subscriberVlan, deviceId, subscriberPort, upStatus); |
| 381 | } |
| 382 | }, oltInstallers); |
| 383 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 384 | } |
| 385 | |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 386 | private void processFilteringObjectives(DeviceId devId, PortNumber port, boolean install) { |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 387 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 388 | |
| 389 | FilteringObjective eapol = (install ? builder.permit() : builder.deny()) |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 390 | .withKey(Criteria.matchInPort(port)) |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 391 | .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType())) |
| 392 | .withMeta(DefaultTrafficTreatment.builder() |
| 393 | .setOutput(PortNumber.CONTROLLER).build()) |
| 394 | .fromApp(appId) |
| 395 | .withPriority(1000) |
| 396 | .add(new ObjectiveContext() { |
| 397 | @Override |
| 398 | public void onSuccess(Objective objective) { |
| 399 | log.info("Eapol filter for {} on {} installed.", |
| 400 | devId, port); |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void onError(Objective objective, ObjectiveError error) { |
| 405 | log.info("Eapol filter for {} on {} failed because {}", |
| 406 | devId, port, error); |
| 407 | } |
| 408 | }); |
| 409 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 410 | flowObjectiveService.filter(devId, eapol); |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 411 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 412 | } |
| 413 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 414 | private class InternalDeviceListener implements DeviceListener { |
| 415 | @Override |
| 416 | public void event(DeviceEvent event) { |
alshabib | 49cadbd | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 417 | DeviceId devId = event.subject().id(); |
| 418 | if (!oltData.containsKey(devId)) { |
| 419 | log.debug("Device {} is not an OLT", devId); |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 420 | return; |
| 421 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 422 | switch (event.type()) { |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 423 | //TODO: Port handling and bookkeeping should be inproved once |
| 424 | // olt firmware handles correct behaviour. |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 425 | case PORT_ADDED: |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 426 | if (!oltData.get(devId).uplink().equals(event.port().number()) && |
| 427 | event.port().isEnabled()) { |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 428 | processFilteringObjectives(devId, event.port().number(), true); |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 429 | } |
| 430 | break; |
| 431 | case PORT_REMOVED: |
| 432 | AccessDeviceData olt = oltData.get(devId); |
| 433 | unprovisionSubscriber(devId, olt.uplink(), |
| 434 | event.port().number(), |
| 435 | olt.vlan()); |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 436 | if (!oltData.get(devId).uplink().equals(event.port().number()) && |
| 437 | event.port().isEnabled()) { |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 438 | processFilteringObjectives(devId, event.port().number(), false); |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 439 | } |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 440 | break; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 441 | case PORT_UPDATED: |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 442 | if (oltData.get(devId).uplink().equals(event.port().number())) { |
| 443 | break; |
| 444 | } |
| 445 | if (event.port().isEnabled()) { |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 446 | processFilteringObjectives(devId, event.port().number(), true); |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 447 | } else { |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 448 | processFilteringObjectives(devId, event.port().number(), false); |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 449 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 450 | break; |
| 451 | case DEVICE_ADDED: |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 452 | post(new AccessDeviceEvent( |
| 453 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 454 | null, null)); |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 455 | provisionDefaultFlows(devId); |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 456 | break; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 457 | case DEVICE_REMOVED: |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 458 | post(new AccessDeviceEvent( |
| 459 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 460 | null, null)); |
| 461 | break; |
alshabib | e5ec240 | 2016-02-09 18:22:33 -0800 | [diff] [blame] | 462 | case DEVICE_AVAILABILITY_CHANGED: |
| 463 | if (deviceService.isAvailable(devId)) { |
| 464 | post(new AccessDeviceEvent( |
| 465 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 466 | null, null)); |
| 467 | } else { |
| 468 | post(new AccessDeviceEvent( |
| 469 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 470 | null, null)); |
| 471 | } |
| 472 | break; |
alshabib | 6b139b4 | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 473 | case DEVICE_UPDATED: |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 474 | case DEVICE_SUSPENDED: |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 475 | case PORT_STATS_UPDATED: |
| 476 | default: |
| 477 | return; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 482 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 483 | @Override |
| 484 | public void event(NetworkConfigEvent event) { |
| 485 | switch (event.type()) { |
| 486 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 487 | case CONFIG_ADDED: |
| 488 | case CONFIG_UPDATED: |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 489 | |
| 490 | AccessDeviceConfig config = |
| 491 | networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS); |
| 492 | if (config != null) { |
| 493 | oltData.put(config.getOlt().deviceId(), config.getOlt()); |
| 494 | provisionDefaultFlows((DeviceId) event.subject()); |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 495 | } |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 496 | |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 497 | break; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 498 | case CONFIG_REGISTERED: |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 499 | case CONFIG_UNREGISTERED: |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 500 | break; |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 501 | case CONFIG_REMOVED: |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 502 | oltData.remove(event.subject()); |
alshabib | 02cbe6a | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 503 | default: |
| 504 | break; |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 507 | |
| 508 | @Override |
| 509 | public boolean isRelevant(NetworkConfigEvent event) { |
| 510 | return event.configClass().equals(CONFIG_CLASS); |
| 511 | } |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 512 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 513 | |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 514 | private void provisionDefaultFlows(DeviceId deviceId) { |
| 515 | List<Port> ports = deviceService.getPorts(deviceId); |
| 516 | |
| 517 | ports.stream() |
| 518 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 519 | .filter(p -> p.isEnabled()) |
alshabib | ea9eed9 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 520 | .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(), |
| 521 | p.number(), true)); |
alshabib | 8f8060d | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 522 | |
| 523 | } |
| 524 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 525 | } |