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 | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 153 | .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(), p, true)); |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 154 | |
alshabib | ba35749 | 2016-01-27 13:49:46 -0800 | [diff] [blame] | 155 | deviceService.addListener(deviceListener); |
| 156 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 157 | log.info("Started with Application ID {}", appId.id()); |
| 158 | } |
| 159 | |
| 160 | @Deactivate |
| 161 | public void deactivate() { |
alshabib | 62e9ce7 | 2016-02-11 17:31:36 -0800 | [diff] [blame^] | 162 | deviceService.removeListener(deviceListener); |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 163 | networkConfig.removeListener(configListener); |
| 164 | networkConfig.unregisterConfigFactory(configFactory); |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 165 | log.info("Stopped"); |
| 166 | } |
| 167 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public void provisionSubscriber(ConnectPoint port, VlanId vlan) { |
| 170 | AccessDeviceData olt = oltData.get(port.deviceId()); |
| 171 | |
| 172 | if (olt == null) { |
| 173 | log.warn("No data found for OLT device {}", port.deviceId()); |
| 174 | return; |
| 175 | } |
| 176 | |
Jonathan Hart | 5299838 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 177 | provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(), |
alshabib | b7a9e17 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 178 | olt.defaultVlan()); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void removeSubscriber(ConnectPoint port) { |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 183 | AccessDeviceData olt = oltData.get(port.deviceId()); |
| 184 | |
| 185 | if (olt == null) { |
| 186 | log.warn("No data found for OLT device {}", port.deviceId()); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | unprovisionSubscriber(olt.deviceId(), olt.uplink(), port.port(), olt.vlan()); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink, |
| 195 | PortNumber subscriberPort, VlanId deviceVlan) { |
| 196 | |
| 197 | //FIXME: This method is slightly ugly but it'll do until we have a better |
| 198 | // way to remove flows from the flow store. |
| 199 | |
| 200 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 201 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 202 | |
| 203 | ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort); |
| 204 | |
| 205 | VlanId subscriberVlan = subscribers.remove(cp); |
| 206 | |
| 207 | Set<ForwardingObjective.Builder> fwds = objectives.remove(cp); |
| 208 | |
| 209 | if (fwds == null || fwds.size() != 2) { |
| 210 | log.warn("Unknown or incomplete subscriber at {}", cp); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | fwds.stream().forEach( |
| 216 | fwd -> flowObjectiveService.forward(deviceId, |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 217 | fwd.remove(new ObjectiveContext() { |
| 218 | @Override |
| 219 | public void onSuccess(Objective objective) { |
| 220 | upFuture.complete(null); |
| 221 | } |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 222 | |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 223 | @Override |
| 224 | public void onError(Objective objective, ObjectiveError error) { |
| 225 | upFuture.complete(error); |
| 226 | } |
| 227 | }))); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 228 | |
| 229 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 230 | if (upStatus == null && downStatus == null) { |
| 231 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED, |
| 232 | deviceId, |
| 233 | deviceVlan, |
| 234 | subscriberVlan)); |
| 235 | } else if (downStatus != null) { |
| 236 | log.error("Subscriber with vlan {} on device {} " + |
| 237 | "on port {} failed downstream uninstallation: {}", |
| 238 | subscriberVlan, deviceId, subscriberPort, downStatus); |
| 239 | } else if (upStatus != null) { |
| 240 | log.error("Subscriber with vlan {} on device {} " + |
| 241 | "on port {} failed upstream uninstallation: {}", |
| 242 | subscriberVlan, deviceId, subscriberPort, upStatus); |
| 243 | } |
| 244 | }, oltInstallers); |
alshabib | b7a9e17 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 245 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort, |
| 249 | PortNumber subscriberPort, |
Jonathan Hart | 5299838 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 250 | VlanId subscriberVlan, VlanId deviceVlan, |
| 251 | Optional<VlanId> defaultVlan) { |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 252 | |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 253 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 254 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 255 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 256 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
Jonathan Hart | 5299838 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 257 | .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN) |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 258 | .matchInPort(subscriberPort) |
| 259 | .build(); |
| 260 | |
| 261 | TrafficSelector downstream = DefaultTrafficSelector.builder() |
| 262 | .matchVlanId(deviceVlan) |
| 263 | .matchInPort(uplinkPort) |
alshabib | b7a9e17 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 264 | .matchInnerVlanId(subscriberVlan) |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 265 | .build(); |
| 266 | |
| 267 | TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder() |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 268 | .pushVlan() |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 269 | .setVlanId(subscriberVlan) |
| 270 | .pushVlan() |
| 271 | .setVlanId(deviceVlan) |
| 272 | .setOutput(uplinkPort) |
| 273 | .build(); |
| 274 | |
| 275 | TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder() |
| 276 | .popVlan() |
Jonathan Hart | 5299838 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 277 | .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN) |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 278 | .setOutput(subscriberPort) |
| 279 | .build(); |
| 280 | |
| 281 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 282 | ForwardingObjective.Builder upFwd = DefaultForwardingObjective.builder() |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 283 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 284 | .withPriority(1000) |
| 285 | .makePermanent() |
| 286 | .withSelector(upstream) |
| 287 | .fromApp(appId) |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 288 | .withTreatment(upstreamTreatment); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 289 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 290 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 291 | ForwardingObjective.Builder downFwd = DefaultForwardingObjective.builder() |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 292 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 293 | .withPriority(1000) |
| 294 | .makePermanent() |
| 295 | .withSelector(downstream) |
| 296 | .fromApp(appId) |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 297 | .withTreatment(downstreamTreatment); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 298 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 299 | ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort); |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 300 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 301 | subscribers.put(cp, subscriberVlan); |
| 302 | objectives.put(cp, Sets.newHashSet(upFwd, downFwd)); |
| 303 | |
| 304 | |
| 305 | flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() { |
| 306 | @Override |
| 307 | public void onSuccess(Objective objective) { |
| 308 | upFuture.complete(null); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void onError(Objective objective, ObjectiveError error) { |
| 313 | upFuture.complete(error); |
| 314 | } |
| 315 | })); |
| 316 | |
| 317 | |
| 318 | flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() { |
| 319 | @Override |
| 320 | public void onSuccess(Objective objective) { |
| 321 | downFuture.complete(null); |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public void onError(Objective objective, ObjectiveError error) { |
| 326 | downFuture.complete(error); |
| 327 | } |
| 328 | })); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 329 | |
| 330 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 331 | if (upStatus == null && downStatus == null) { |
| 332 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED, |
| 333 | deviceId, |
| 334 | deviceVlan, |
| 335 | subscriberVlan)); |
| 336 | } else if (downStatus != null) { |
| 337 | log.error("Subscriber with vlan {} on device {} " + |
| 338 | "on port {} failed downstream installation: {}", |
| 339 | subscriberVlan, deviceId, subscriberPort, downStatus); |
| 340 | } else if (upStatus != null) { |
| 341 | log.error("Subscriber with vlan {} on device {} " + |
| 342 | "on port {} failed upstream installation: {}", |
| 343 | subscriberVlan, deviceId, subscriberPort, upStatus); |
| 344 | } |
| 345 | }, oltInstallers); |
| 346 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 347 | } |
| 348 | |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 349 | private void processFilteringObjectives(DeviceId devId, Port port, boolean install) { |
| 350 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 351 | |
| 352 | FilteringObjective eapol = (install ? builder.permit() : builder.deny()) |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 353 | .withKey(Criteria.matchInPort(port.number())) |
| 354 | .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType())) |
| 355 | .withMeta(DefaultTrafficTreatment.builder() |
| 356 | .setOutput(PortNumber.CONTROLLER).build()) |
| 357 | .fromApp(appId) |
| 358 | .withPriority(1000) |
| 359 | .add(new ObjectiveContext() { |
| 360 | @Override |
| 361 | public void onSuccess(Objective objective) { |
| 362 | log.info("Eapol filter for {} on {} installed.", |
| 363 | devId, port); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void onError(Objective objective, ObjectiveError error) { |
| 368 | log.info("Eapol filter for {} on {} failed because {}", |
| 369 | devId, port, error); |
| 370 | } |
| 371 | }); |
| 372 | |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 373 | flowObjectiveService.filter(devId, eapol); |
alshabib | 000b6fc | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 374 | |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 375 | } |
| 376 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 377 | private class InternalDeviceListener implements DeviceListener { |
| 378 | @Override |
| 379 | public void event(DeviceEvent event) { |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 380 | DeviceId devId = event.subject().id(); |
| 381 | if (!oltData.containsKey(devId)) { |
| 382 | log.debug("Device {} is not an OLT", devId); |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 383 | return; |
| 384 | } |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 385 | switch (event.type()) { |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 386 | //TODO: Port handling and bookkeeping should be inproved once |
| 387 | // olt firmware handles correct behaviour. |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 388 | case PORT_ADDED: |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 389 | if (!oltData.get(devId).uplink().equals(event.port().number()) && |
| 390 | event.port().isEnabled()) { |
| 391 | processFilteringObjectives(devId, event.port(), true); |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 392 | } |
| 393 | break; |
| 394 | case PORT_REMOVED: |
| 395 | AccessDeviceData olt = oltData.get(devId); |
| 396 | unprovisionSubscriber(devId, olt.uplink(), |
| 397 | event.port().number(), |
| 398 | olt.vlan()); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 399 | if (!oltData.get(devId).uplink().equals(event.port().number()) && |
| 400 | event.port().isEnabled()) { |
| 401 | processFilteringObjectives(devId, event.port(), false); |
| 402 | } |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 403 | break; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 404 | case PORT_UPDATED: |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 405 | if (oltData.get(devId).uplink().equals(event.port().number())) { |
| 406 | break; |
| 407 | } |
| 408 | if (event.port().isEnabled()) { |
| 409 | processFilteringObjectives(devId, event.port(), true); |
| 410 | } else { |
| 411 | processFilteringObjectives(devId, event.port(), false); |
| 412 | } |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 413 | break; |
| 414 | case DEVICE_ADDED: |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 415 | post(new AccessDeviceEvent( |
| 416 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 417 | null, null)); |
| 418 | break; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 419 | case DEVICE_REMOVED: |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 420 | post(new AccessDeviceEvent( |
| 421 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 422 | null, null)); |
| 423 | break; |
alshabib | 7c19001 | 2016-02-09 18:22:33 -0800 | [diff] [blame] | 424 | case DEVICE_AVAILABILITY_CHANGED: |
| 425 | if (deviceService.isAvailable(devId)) { |
| 426 | post(new AccessDeviceEvent( |
| 427 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 428 | null, null)); |
| 429 | } else { |
| 430 | post(new AccessDeviceEvent( |
| 431 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 432 | null, null)); |
| 433 | } |
| 434 | break; |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 435 | case DEVICE_UPDATED: |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 436 | case DEVICE_SUSPENDED: |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 437 | case PORT_STATS_UPDATED: |
| 438 | default: |
| 439 | return; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 444 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 445 | @Override |
| 446 | public void event(NetworkConfigEvent event) { |
| 447 | switch (event.type()) { |
| 448 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 449 | case CONFIG_ADDED: |
| 450 | case CONFIG_UPDATED: |
| 451 | if (event.configClass().equals(CONFIG_CLASS)) { |
| 452 | AccessDeviceConfig config = |
| 453 | networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS); |
| 454 | if (config != null) { |
| 455 | oltData.put(config.getOlt().deviceId(), config.getOlt()); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 456 | provisionDefaultFlows((DeviceId) event.subject()); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 457 | } |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 458 | } |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 459 | break; |
| 460 | case CONFIG_UNREGISTERED: |
| 461 | case CONFIG_REMOVED: |
| 462 | default: |
| 463 | break; |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | } |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 467 | |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 468 | private void provisionDefaultFlows(DeviceId deviceId) { |
| 469 | List<Port> ports = deviceService.getPorts(deviceId); |
| 470 | |
| 471 | ports.stream() |
| 472 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 473 | .filter(p -> p.isEnabled()) |
| 474 | .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(), p, true)); |
| 475 | |
| 476 | } |
| 477 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 478 | } |