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