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