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