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