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