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)); |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 236 | processFilteringObjectives(deviceId, subscriberPort, true); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 237 | } else if (downStatus != null) { |
| 238 | log.error("Subscriber with vlan {} on device {} " + |
| 239 | "on port {} failed downstream uninstallation: {}", |
| 240 | subscriberVlan, deviceId, subscriberPort, downStatus); |
| 241 | } else if (upStatus != null) { |
| 242 | log.error("Subscriber with vlan {} on device {} " + |
| 243 | "on port {} failed upstream uninstallation: {}", |
| 244 | subscriberVlan, deviceId, subscriberPort, upStatus); |
| 245 | } |
| 246 | }, oltInstallers); |
alshabib | b7a9e17 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 247 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort, |
| 251 | PortNumber subscriberPort, |
Jonathan Hart | 5299838 | 2015-11-10 16:09:22 -0800 | [diff] [blame] | 252 | VlanId subscriberVlan, VlanId deviceVlan, |
| 253 | Optional<VlanId> defaultVlan) { |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 254 | |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 255 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 256 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 257 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 258 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
Sho SHIMIZU | 004776c | 2016-02-12 18:38:29 -0800 | [diff] [blame^] | 259 | .matchVlanId(defaultVlan.orElse(DEFAULT_VLAN)) |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 260 | .matchInPort(subscriberPort) |
| 261 | .build(); |
| 262 | |
| 263 | TrafficSelector downstream = DefaultTrafficSelector.builder() |
| 264 | .matchVlanId(deviceVlan) |
| 265 | .matchInPort(uplinkPort) |
alshabib | b7a9e17 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 266 | .matchInnerVlanId(subscriberVlan) |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 267 | .build(); |
| 268 | |
| 269 | TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder() |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 270 | .pushVlan() |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 271 | .setVlanId(subscriberVlan) |
| 272 | .pushVlan() |
| 273 | .setVlanId(deviceVlan) |
| 274 | .setOutput(uplinkPort) |
| 275 | .build(); |
| 276 | |
| 277 | TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder() |
| 278 | .popVlan() |
Sho SHIMIZU | 004776c | 2016-02-12 18:38:29 -0800 | [diff] [blame^] | 279 | .setVlanId(defaultVlan.orElse(DEFAULT_VLAN)) |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 280 | .setOutput(subscriberPort) |
| 281 | .build(); |
| 282 | |
| 283 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 284 | ForwardingObjective.Builder upFwd = DefaultForwardingObjective.builder() |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 285 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 286 | .withPriority(1000) |
| 287 | .makePermanent() |
| 288 | .withSelector(upstream) |
| 289 | .fromApp(appId) |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 290 | .withTreatment(upstreamTreatment); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 291 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 292 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 293 | ForwardingObjective.Builder downFwd = DefaultForwardingObjective.builder() |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 294 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 295 | .withPriority(1000) |
| 296 | .makePermanent() |
| 297 | .withSelector(downstream) |
| 298 | .fromApp(appId) |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 299 | .withTreatment(downstreamTreatment); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 300 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 301 | ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort); |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 302 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 303 | subscribers.put(cp, subscriberVlan); |
| 304 | objectives.put(cp, Sets.newHashSet(upFwd, downFwd)); |
| 305 | |
| 306 | |
| 307 | flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() { |
| 308 | @Override |
| 309 | public void onSuccess(Objective objective) { |
| 310 | upFuture.complete(null); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void onError(Objective objective, ObjectiveError error) { |
| 315 | upFuture.complete(error); |
| 316 | } |
| 317 | })); |
| 318 | |
| 319 | |
| 320 | flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() { |
| 321 | @Override |
| 322 | public void onSuccess(Objective objective) { |
| 323 | downFuture.complete(null); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void onError(Objective objective, ObjectiveError error) { |
| 328 | downFuture.complete(error); |
| 329 | } |
| 330 | })); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 331 | |
| 332 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 333 | if (upStatus == null && downStatus == null) { |
| 334 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED, |
| 335 | deviceId, |
| 336 | deviceVlan, |
| 337 | subscriberVlan)); |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 338 | |
| 339 | processFilteringObjectives(deviceId, subscriberPort, false); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 340 | } else if (downStatus != null) { |
| 341 | log.error("Subscriber with vlan {} on device {} " + |
| 342 | "on port {} failed downstream installation: {}", |
| 343 | subscriberVlan, deviceId, subscriberPort, downStatus); |
| 344 | } else if (upStatus != null) { |
| 345 | log.error("Subscriber with vlan {} on device {} " + |
| 346 | "on port {} failed upstream installation: {}", |
| 347 | subscriberVlan, deviceId, subscriberPort, upStatus); |
| 348 | } |
| 349 | }, oltInstallers); |
| 350 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 351 | } |
| 352 | |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 353 | private void processFilteringObjectives(DeviceId devId, PortNumber port, boolean install) { |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 354 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 355 | |
| 356 | FilteringObjective eapol = (install ? builder.permit() : builder.deny()) |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 357 | .withKey(Criteria.matchInPort(port)) |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 358 | .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType())) |
| 359 | .withMeta(DefaultTrafficTreatment.builder() |
| 360 | .setOutput(PortNumber.CONTROLLER).build()) |
| 361 | .fromApp(appId) |
| 362 | .withPriority(1000) |
| 363 | .add(new ObjectiveContext() { |
| 364 | @Override |
| 365 | public void onSuccess(Objective objective) { |
| 366 | log.info("Eapol filter for {} on {} installed.", |
| 367 | devId, port); |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public void onError(Objective objective, ObjectiveError error) { |
| 372 | log.info("Eapol filter for {} on {} failed because {}", |
| 373 | devId, port, error); |
| 374 | } |
| 375 | }); |
| 376 | |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 377 | flowObjectiveService.filter(devId, eapol); |
alshabib | 000b6fc | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 378 | |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 379 | } |
| 380 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 381 | private class InternalDeviceListener implements DeviceListener { |
| 382 | @Override |
| 383 | public void event(DeviceEvent event) { |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 384 | DeviceId devId = event.subject().id(); |
| 385 | if (!oltData.containsKey(devId)) { |
| 386 | log.debug("Device {} is not an OLT", devId); |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 387 | return; |
| 388 | } |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 389 | switch (event.type()) { |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 390 | //TODO: Port handling and bookkeeping should be inproved once |
| 391 | // olt firmware handles correct behaviour. |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 392 | case PORT_ADDED: |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 393 | if (!oltData.get(devId).uplink().equals(event.port().number()) && |
| 394 | event.port().isEnabled()) { |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 395 | processFilteringObjectives(devId, event.port().number(), true); |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 396 | } |
| 397 | break; |
| 398 | case PORT_REMOVED: |
| 399 | AccessDeviceData olt = oltData.get(devId); |
| 400 | unprovisionSubscriber(devId, olt.uplink(), |
| 401 | event.port().number(), |
| 402 | olt.vlan()); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 403 | if (!oltData.get(devId).uplink().equals(event.port().number()) && |
| 404 | event.port().isEnabled()) { |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 405 | processFilteringObjectives(devId, event.port().number(), false); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 406 | } |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 407 | break; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 408 | case PORT_UPDATED: |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 409 | if (oltData.get(devId).uplink().equals(event.port().number())) { |
| 410 | break; |
| 411 | } |
| 412 | if (event.port().isEnabled()) { |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 413 | processFilteringObjectives(devId, event.port().number(), true); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 414 | } else { |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 415 | processFilteringObjectives(devId, event.port().number(), false); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 416 | } |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 417 | break; |
| 418 | case DEVICE_ADDED: |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 419 | post(new AccessDeviceEvent( |
| 420 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 421 | null, null)); |
| 422 | break; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 423 | case DEVICE_REMOVED: |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 424 | post(new AccessDeviceEvent( |
| 425 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 426 | null, null)); |
| 427 | break; |
alshabib | 7c19001 | 2016-02-09 18:22:33 -0800 | [diff] [blame] | 428 | case DEVICE_AVAILABILITY_CHANGED: |
| 429 | if (deviceService.isAvailable(devId)) { |
| 430 | post(new AccessDeviceEvent( |
| 431 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 432 | null, null)); |
| 433 | } else { |
| 434 | post(new AccessDeviceEvent( |
| 435 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 436 | null, null)); |
| 437 | } |
| 438 | break; |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 439 | case DEVICE_UPDATED: |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 440 | case DEVICE_SUSPENDED: |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 441 | case PORT_STATS_UPDATED: |
| 442 | default: |
| 443 | return; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 448 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 449 | @Override |
| 450 | public void event(NetworkConfigEvent event) { |
| 451 | switch (event.type()) { |
| 452 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 453 | case CONFIG_ADDED: |
| 454 | case CONFIG_UPDATED: |
| 455 | if (event.configClass().equals(CONFIG_CLASS)) { |
| 456 | AccessDeviceConfig config = |
| 457 | networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS); |
| 458 | if (config != null) { |
| 459 | oltData.put(config.getOlt().deviceId(), config.getOlt()); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 460 | provisionDefaultFlows((DeviceId) event.subject()); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 461 | } |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 462 | } |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 463 | break; |
| 464 | case CONFIG_UNREGISTERED: |
| 465 | case CONFIG_REMOVED: |
| 466 | default: |
| 467 | break; |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 471 | |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 472 | private void provisionDefaultFlows(DeviceId deviceId) { |
| 473 | List<Port> ports = deviceService.getPorts(deviceId); |
| 474 | |
| 475 | ports.stream() |
| 476 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 477 | .filter(p -> p.isEnabled()) |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 478 | .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(), |
| 479 | p.number(), true)); |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 480 | |
| 481 | } |
| 482 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 483 | } |