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