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