alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | d6a135a | 2017-08-03 22:46:05 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 20 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 21 | import static org.onlab.util.Tools.get; |
| 22 | import static org.onlab.util.Tools.groupedThreads; |
| 23 | import static org.slf4j.LoggerFactory.getLogger; |
| 24 | |
| 25 | import java.util.AbstractMap; |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 26 | import java.util.Arrays; |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.Collection; |
| 29 | import java.util.Dictionary; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Optional; |
| 33 | import java.util.Properties; |
| 34 | import java.util.concurrent.CompletableFuture; |
| 35 | import java.util.concurrent.ExecutorService; |
| 36 | import java.util.concurrent.Executors; |
| 37 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 38 | import org.apache.felix.scr.annotations.Activate; |
| 39 | import org.apache.felix.scr.annotations.Component; |
| 40 | import org.apache.felix.scr.annotations.Deactivate; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 41 | import org.apache.felix.scr.annotations.Modified; |
| 42 | import org.apache.felix.scr.annotations.Property; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 43 | import org.apache.felix.scr.annotations.Reference; |
| 44 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 45 | import org.apache.felix.scr.annotations.Service; |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 46 | import org.onlab.packet.EthType; |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 47 | import org.onlab.packet.IPv4; |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 48 | import org.onlab.packet.IPv6; |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 49 | import org.onlab.packet.TpPort; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 50 | import org.onlab.packet.VlanId; |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 51 | import org.onlab.util.Tools; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 52 | import org.onosproject.cfg.ComponentConfigService; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 53 | import org.onosproject.core.ApplicationId; |
| 54 | import org.onosproject.core.CoreService; |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 55 | import org.onosproject.event.AbstractListenerManager; |
alshabib | 09753b5 | 2016-03-04 14:55:19 -0800 | [diff] [blame] | 56 | import org.onosproject.mastership.MastershipService; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 57 | import org.onosproject.net.AnnotationKeys; |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 58 | import org.onosproject.net.ConnectPoint; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 59 | import org.onosproject.net.Device; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 60 | import org.onosproject.net.DeviceId; |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 61 | import org.onosproject.net.Port; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 62 | import org.onosproject.net.PortNumber; |
| 63 | import org.onosproject.net.device.DeviceEvent; |
| 64 | import org.onosproject.net.device.DeviceListener; |
| 65 | import org.onosproject.net.device.DeviceService; |
| 66 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 67 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 68 | import org.onosproject.net.flow.TrafficSelector; |
| 69 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 70 | import org.onosproject.net.flow.criteria.Criteria; |
| 71 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 72 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 73 | import org.onosproject.net.flowobjective.FilteringObjective; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 74 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 75 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 76 | import org.onosproject.net.flowobjective.Objective; |
| 77 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 78 | import org.onosproject.net.flowobjective.ObjectiveError; |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 79 | import org.onosproject.store.serializers.KryoNamespaces; |
| 80 | import org.onosproject.store.service.ConsistentMultimap; |
| 81 | import org.onosproject.store.service.Serializer; |
| 82 | import org.onosproject.store.service.StorageService; |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 83 | import org.onosproject.net.meter.Band; |
| 84 | import org.onosproject.net.meter.DefaultBand; |
| 85 | import org.onosproject.net.meter.DefaultMeterRequest; |
| 86 | import org.onosproject.net.meter.Meter; |
| 87 | import org.onosproject.net.meter.MeterService; |
| 88 | import org.onosproject.net.meter.MeterListener; |
| 89 | import org.onosproject.net.meter.MeterRequest; |
| 90 | import org.onosproject.net.meter.MeterId; |
| 91 | import org.onosproject.net.meter.MeterEvent; |
alshabib | 36a4d73 | 2016-06-01 16:03:59 -0700 | [diff] [blame] | 92 | import org.opencord.olt.AccessDeviceEvent; |
| 93 | import org.opencord.olt.AccessDeviceListener; |
| 94 | import org.opencord.olt.AccessDeviceService; |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 95 | import org.opencord.olt.AccessSubscriberId; |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 96 | import org.opencord.sadis.BandwidthProfileInformation; |
| 97 | import org.opencord.sadis.BaseInformationService; |
| 98 | import org.opencord.sadis.SadisService; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 99 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 100 | import org.osgi.service.component.ComponentContext; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 101 | import org.slf4j.Logger; |
| 102 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 103 | import java.util.HashMap; |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 104 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 105 | import com.google.common.collect.ImmutableMap; |
| 106 | import com.google.common.collect.Maps; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 109 | * Provisions rules on access devices. |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 110 | */ |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 111 | @Service |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 112 | @Component(immediate = true) |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 113 | public class Olt |
| 114 | extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener> |
| 115 | implements AccessDeviceService { |
Charles Chan | 54f110f | 2017-01-20 11:22:42 -0800 | [diff] [blame] | 116 | private static final String APP_NAME = "org.opencord.olt"; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 117 | |
| 118 | private static final short DEFAULT_VLAN = 0; |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 119 | private static final String ADDITIONAL_VLANS = "additional-vlans"; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 120 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 121 | private final Logger log = getLogger(getClass()); |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected FlowObjectiveService flowObjectiveService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 09753b5 | 2016-03-04 14:55:19 -0800 | [diff] [blame] | 127 | protected MastershipService mastershipService; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 130 | protected DeviceService deviceService; |
| 131 | |
| 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 133 | protected CoreService coreService; |
| 134 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 136 | protected ComponentConfigService componentConfigService; |
| 137 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 139 | protected SadisService sadisService; |
| 140 | |
| 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 142 | protected MeterService meterService; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 143 | |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 145 | protected StorageService storageService; |
| 146 | |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 147 | @Property(name = "defaultVlan", intValue = DEFAULT_VLAN, |
| 148 | label = "Default VLAN RG<->ONU traffic") |
| 149 | private int defaultVlan = DEFAULT_VLAN; |
| 150 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 151 | @Property(name = "enableDhcpOnProvisioning", boolValue = true, |
| 152 | label = "Create the DHCP Flow rules when a subscriber is provisioned") |
| 153 | protected boolean enableDhcpOnProvisioning = false; |
| 154 | |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 155 | @Property(name = "enableDhcpV4", boolValue = true, |
| 156 | label = "Enable flows for DHCP v4") |
| 157 | protected boolean enableDhcpV4 = true; |
| 158 | |
| 159 | @Property(name = "enableDhcpV6", boolValue = true, |
| 160 | label = "Enable flows for DHCP v6") |
| 161 | protected boolean enableDhcpV6 = false; |
| 162 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 163 | @Property(name = "enableIgmpOnProvisioning", boolValue = false, |
| 164 | label = "Create IGMP Flow rules when a subscriber is provisioned") |
| 165 | protected boolean enableIgmpOnProvisioning = false; |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 166 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 167 | // needed because no implementation for meter statistics in Voltha yet |
| 168 | @Property(name = "deleteMeters", boolValue = false, |
| 169 | label = "Deleting Meters based on flow count statistics") |
| 170 | protected boolean deleteMeters = false; |
| 171 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 172 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 173 | private final MeterListener meterListener = new InternalMeterListener(); |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 174 | |
| 175 | private ApplicationId appId; |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 176 | protected BaseInformationService<SubscriberAndDeviceInformation> subsService; |
| 177 | private BaseInformationService<BandwidthProfileInformation> bpService; |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 178 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 179 | private Map<String, MeterId> bpInfoToMeter = new HashMap<>(); |
| 180 | |
| 181 | private ExecutorService oltInstallers = Executors.newFixedThreadPool(4, |
| 182 | groupedThreads("onos/olt-service", |
| 183 | "olt-installer-%d")); |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 184 | |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 185 | private ConsistentMultimap<ConnectPoint, Map.Entry<VlanId, VlanId>> additionalVlans; |
| 186 | |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 187 | protected ExecutorService eventExecutor; |
| 188 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 189 | private Map<ConnectPoint, SubscriberAndDeviceInformation> programmedSubs; |
| 190 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 191 | @Activate |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 192 | public void activate(ComponentContext context) { |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 193 | eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/olt", "events-%d", log)); |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 194 | modified(context); |
Charles Chan | 54f110f | 2017-01-20 11:22:42 -0800 | [diff] [blame] | 195 | appId = coreService.registerApplication(APP_NAME); |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 196 | componentConfigService.registerProperties(getClass()); |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 197 | programmedSubs = Maps.newConcurrentMap(); |
alshabib | c4dfe85 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 198 | |
alshabib | 8e4fd2f | 2016-01-12 15:55:53 -0800 | [diff] [blame] | 199 | eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry); |
| 200 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 201 | subsService = sadisService.getSubscriberInfoService(); |
| 202 | bpService = sadisService.getBandwidthProfileService(); |
| 203 | |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 204 | // look for all provisioned devices in Sadis and create EAPOL flows for the |
| 205 | // UNI ports |
| 206 | Iterable<Device> devices = deviceService.getDevices(); |
| 207 | for (Device d : devices) { |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 208 | checkAndCreateDeviceFlows(d); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 209 | } |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 210 | |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 211 | additionalVlans = storageService.<ConnectPoint, Map.Entry<VlanId, VlanId>>consistentMultimapBuilder() |
| 212 | .withName(ADDITIONAL_VLANS) |
| 213 | .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API), |
| 214 | AbstractMap.SimpleEntry.class)) |
| 215 | .build(); |
| 216 | |
alshabib | ba35749 | 2016-01-27 13:49:46 -0800 | [diff] [blame] | 217 | deviceService.addListener(deviceListener); |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 218 | meterService.addListener(meterListener); |
alshabib | ba35749 | 2016-01-27 13:49:46 -0800 | [diff] [blame] | 219 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 220 | log.info("Started with Application ID {}", appId.id()); |
| 221 | } |
| 222 | |
| 223 | @Deactivate |
| 224 | public void deactivate() { |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 225 | componentConfigService.unregisterProperties(getClass(), false); |
alshabib | 62e9ce7 | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 226 | deviceService.removeListener(deviceListener); |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 227 | meterService.removeListener(meterListener); |
Jonathan Hart | 5f1c814 | 2018-07-24 17:31:59 -0700 | [diff] [blame] | 228 | eventDispatcher.removeSink(AccessDeviceEvent.class); |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 229 | log.info("Stopped"); |
| 230 | } |
| 231 | |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 232 | @Modified |
| 233 | public void modified(ComponentContext context) { |
| 234 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 235 | |
| 236 | try { |
| 237 | String s = get(properties, "defaultVlan"); |
| 238 | defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim()); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 239 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 240 | Boolean o = Tools.isPropertyEnabled(properties, "enableDhcpOnProvisioning"); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 241 | if (o != null) { |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 242 | enableDhcpOnProvisioning = o; |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 243 | } |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 244 | |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 245 | Boolean v4 = Tools.isPropertyEnabled(properties, "enableDhcpV4"); |
| 246 | if (v4 != null) { |
| 247 | enableDhcpV4 = v4; |
| 248 | } |
| 249 | |
| 250 | Boolean v6 = Tools.isPropertyEnabled(properties, "enableDhcpV6"); |
| 251 | if (v6 != null) { |
| 252 | enableDhcpV6 = v6; |
| 253 | } |
| 254 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 255 | Boolean p = Tools.isPropertyEnabled(properties, "enableIgmpOnProvisioning"); |
| 256 | if (p != null) { |
| 257 | enableIgmpOnProvisioning = p; |
| 258 | } |
| 259 | |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 260 | log.info("DHCP Settings [enableDhcpOnProvisioning: {}, enableDhcpV4: {}, enableDhcpV6: {}]", |
| 261 | enableDhcpOnProvisioning, enableDhcpV4, enableDhcpV6); |
| 262 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 263 | Boolean d = Tools.isPropertyEnabled(properties, "deleteMeters"); |
| 264 | if (d != null) { |
| 265 | deleteMeters = d; |
| 266 | } |
| 267 | |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 268 | } catch (Exception e) { |
| 269 | defaultVlan = DEFAULT_VLAN; |
| 270 | } |
| 271 | } |
| 272 | |
alshabib | 32232c8 | 2016-02-25 17:57:24 -0500 | [diff] [blame] | 273 | @Override |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 274 | public boolean provisionSubscriber(ConnectPoint port) { |
Jonathan Hart | 94b9049 | 2018-04-24 14:02:25 -0700 | [diff] [blame] | 275 | checkNotNull(deviceService.getPort(port.deviceId(), port.port()), |
| 276 | "Invalid connect point"); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 277 | // Find the subscriber on this connect point |
| 278 | SubscriberAndDeviceInformation sub = getSubscriber(port); |
| 279 | if (sub == null) { |
| 280 | log.warn("No subscriber found for {}", port); |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 281 | return false; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 282 | } |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 283 | |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 284 | // Get the uplink port |
| 285 | Port uplinkPort = getUplinkPort(deviceService.getDevice(port.deviceId())); |
| 286 | if (uplinkPort == null) { |
| 287 | log.warn("No uplink port found for OLT device {}", port.deviceId()); |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 288 | return false; |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 291 | if (enableDhcpOnProvisioning) { |
Saurav Das | acc5eeb | 2018-10-11 10:58:01 -0700 | [diff] [blame] | 292 | processDhcpFilteringObjectives(port.deviceId(), port.port(), true, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 293 | true); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 294 | } |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 295 | log.info("Programming vlans for subscriber: {}", sub); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 296 | Optional<VlanId> defaultVlan = Optional.empty(); |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 297 | provisionVlans(port, uplinkPort.number(), defaultVlan, sub); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 298 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 299 | if (enableIgmpOnProvisioning) { |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 300 | processIgmpFilteringObjectives(port.deviceId(), port.port(), true); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 301 | } |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 302 | // cache subscriber info |
| 303 | programmedSubs.put(port, sub); |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 304 | return true; |
alshabib | b7a9e17 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 308 | public boolean removeSubscriber(ConnectPoint port) { |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 309 | // Get the subscriber connected to this port from the local cache |
| 310 | // as if we don't know about the subscriber there's no need to remove it |
| 311 | SubscriberAndDeviceInformation subscriber = programmedSubs.get(port); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 312 | if (subscriber == null) { |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 313 | log.warn("Subscriber on port {} was not previously programmed, no need to remove it", port); |
| 314 | return true; |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 317 | // Get the uplink port |
| 318 | Port uplinkPort = getUplinkPort(deviceService.getDevice(port.deviceId())); |
| 319 | if (uplinkPort == null) { |
| 320 | log.warn("No uplink port found for OLT device {}", port.deviceId()); |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 321 | return false; |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 324 | if (enableDhcpOnProvisioning) { |
Saurav Das | acc5eeb | 2018-10-11 10:58:01 -0700 | [diff] [blame] | 325 | processDhcpFilteringObjectives(port.deviceId(), port.port(), false, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 326 | true); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 329 | log.info("Removing programmed vlans for subscriber: {}", subscriber); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 330 | Optional<VlanId> defaultVlan = Optional.empty(); |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 331 | unprovisionSubscriber(port.deviceId(), uplinkPort.number(), port.port(), subscriber, defaultVlan); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 332 | |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 333 | if (enableIgmpOnProvisioning) { |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 334 | processIgmpFilteringObjectives(port.deviceId(), port.port(), false); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 335 | } |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 336 | |
| 337 | // Remove if there are any flows for the additional Vlans |
| 338 | Collection<? extends Map.Entry<VlanId, VlanId>> vlansList = additionalVlans.get(port).value(); |
| 339 | |
| 340 | // Remove the flows for the additional vlans for this subscriber |
| 341 | for (Map.Entry<VlanId, VlanId> vlans : vlansList) { |
| 342 | unprovisionTransparentFlows(port.deviceId(), uplinkPort.number(), port.port(), |
| 343 | vlans.getValue(), vlans.getKey()); |
| 344 | |
| 345 | // Remove it from the map also |
| 346 | additionalVlans.remove(port, vlans); |
| 347 | } |
| 348 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 349 | programmedSubs.remove(port); |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 350 | return true; |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 353 | @Override |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 354 | public boolean provisionSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) { |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 355 | // Check if we can find the connect point to which this subscriber is connected |
| 356 | ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString()); |
| 357 | if (subsPort == null) { |
| 358 | log.warn("ConnectPoint for {} not found", subscriberId); |
| 359 | return false; |
| 360 | } |
| 361 | |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 362 | if (!sTag.isPresent() && !cTag.isPresent()) { |
| 363 | return provisionSubscriber(subsPort); |
| 364 | } else if (sTag.isPresent() && cTag.isPresent()) { |
| 365 | Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId())); |
| 366 | if (uplinkPort == null) { |
| 367 | log.warn("No uplink port found for OLT device {}", subsPort.deviceId()); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | provisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), |
| 372 | cTag.get(), sTag.get()); |
| 373 | return true; |
| 374 | } else { |
| 375 | log.warn("Provisioning failed for subscriber: {}", subscriberId); |
| 376 | return false; |
| 377 | } |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 378 | } |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 379 | |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 380 | @Override |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 381 | public boolean removeSubscriber(AccessSubscriberId subscriberId, Optional<VlanId> sTag, Optional<VlanId> cTag) { |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 382 | // Check if we can find the connect point to which this subscriber is connected |
| 383 | ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString()); |
| 384 | if (subsPort == null) { |
| 385 | log.warn("ConnectPoint for {} not found", subscriberId); |
| 386 | return false; |
| 387 | } |
| 388 | |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 389 | if (!sTag.isPresent() && !cTag.isPresent()) { |
| 390 | return removeSubscriber(subsPort); |
| 391 | } else if (sTag.isPresent() && cTag.isPresent()) { |
| 392 | // Get the uplink port |
| 393 | Port uplinkPort = getUplinkPort(deviceService.getDevice(subsPort.deviceId())); |
| 394 | if (uplinkPort == null) { |
| 395 | log.warn("No uplink port found for OLT device {}", subsPort.deviceId()); |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | unprovisionTransparentFlows(subsPort.deviceId(), uplinkPort.number(), subsPort.port(), |
| 400 | cTag.get(), sTag.get()); |
| 401 | return true; |
| 402 | } else { |
| 403 | log.warn("Removing subscriber failed for: {}", subscriberId); |
| 404 | return false; |
| 405 | } |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public Collection<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> getSubscribers() { |
| 410 | ArrayList<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> subs = new ArrayList<>(); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 411 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 412 | // Get the subscribers for all the devices configured in sadis |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 413 | // If the port is UNI, is enabled and exists in Sadis then copy it |
| 414 | for (Device d : deviceService.getDevices()) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 415 | if (getOltInfo(d) == null) { |
| 416 | continue; // not an olt, or not configured in sadis |
| 417 | } |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 418 | for (Port p: deviceService.getPorts(d.id())) { |
| 419 | if (isUniPort(d, p) && p.isEnabled()) { |
| 420 | ConnectPoint cp = new ConnectPoint(d.id(), p.number()); |
| 421 | |
| 422 | SubscriberAndDeviceInformation sub = getSubscriber(cp); |
| 423 | if (sub != null) { |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 424 | Map.Entry<VlanId, VlanId> vlans = new AbstractMap.SimpleEntry(sub.sTag(), sub.cTag()); |
| 425 | subs.add(new AbstractMap.SimpleEntry(cp, vlans)); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return subs; |
Jonathan Hart | fd6c1b3 | 2016-03-08 14:09:09 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 435 | public ImmutableMap<ConnectPoint, SubscriberAndDeviceInformation> getProgSubs() { |
| 436 | return ImmutableMap.copyOf(programmedSubs); |
| 437 | } |
| 438 | |
| 439 | @Override |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 440 | public List<DeviceId> fetchOlts() { |
| 441 | // look through all the devices and find the ones that are OLTs as per Sadis |
| 442 | List<DeviceId> olts = new ArrayList<>(); |
| 443 | Iterable<Device> devices = deviceService.getDevices(); |
| 444 | for (Device d : devices) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 445 | if (getOltInfo(d) != null) { |
| 446 | // So this is indeed an OLT device |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 447 | olts.add(d.id()); |
| 448 | } |
| 449 | } |
| 450 | return olts; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 453 | /** |
| 454 | * Finds the connect point to which a subscriber is connected. |
| 455 | * |
| 456 | * @param id The id of the subscriber, this is the same ID as in Sadis |
| 457 | * @return Subscribers ConnectPoint if found else null |
| 458 | */ |
| 459 | private ConnectPoint findSubscriberConnectPoint(String id) { |
| 460 | |
| 461 | Iterable<Device> devices = deviceService.getDevices(); |
| 462 | for (Device d : devices) { |
| 463 | for (Port p : deviceService.getPorts(d.id())) { |
| 464 | log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id); |
| 465 | if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) { |
| 466 | log.debug("Found on device {} port {}", d.id(), p.number()); |
| 467 | return new ConnectPoint(d.id(), p.number()); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 474 | private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) { |
| 475 | if (bandwidthProfile == null) { |
| 476 | return null; |
| 477 | } |
| 478 | return bpService.get(bandwidthProfile); |
| 479 | } |
| 480 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 481 | private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 482 | PortNumber subscriberPort, SubscriberAndDeviceInformation subscriber, |
| 483 | Optional<VlanId> defaultVlan) { |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 484 | |
| 485 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 486 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 487 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 488 | VlanId deviceVlan = subscriber.sTag(); |
| 489 | VlanId subscriberVlan = subscriber.cTag(); |
| 490 | |
| 491 | MeterId upstreamMeterId = bpInfoToMeter.get(subscriber.upstreamBandwidthProfile()); |
| 492 | MeterId downstreamMeterId = bpInfoToMeter.get(subscriber.downstreamBandwidthProfile()); |
| 493 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 494 | ForwardingObjective.Builder upFwd = upBuilder(uplink, subscriberPort, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 495 | subscriberVlan, deviceVlan, |
| 496 | defaultVlan, upstreamMeterId, subscriber.technologyProfileId()); |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 497 | ForwardingObjective.Builder downFwd = downBuilder(uplink, subscriberPort, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 498 | subscriberVlan, deviceVlan, |
| 499 | defaultVlan, downstreamMeterId, subscriber.technologyProfileId()); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 500 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 501 | flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() { |
| 502 | @Override |
| 503 | public void onSuccess(Objective objective) { |
| 504 | upFuture.complete(null); |
| 505 | } |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 506 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 507 | @Override |
| 508 | public void onError(Objective objective, ObjectiveError error) { |
| 509 | upFuture.complete(error); |
| 510 | } |
| 511 | })); |
| 512 | |
| 513 | flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() { |
| 514 | @Override |
| 515 | public void onSuccess(Objective objective) { |
| 516 | downFuture.complete(null); |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void onError(Objective objective, ObjectiveError error) { |
| 521 | downFuture.complete(error); |
| 522 | } |
| 523 | })); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 524 | |
| 525 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 526 | if (upStatus == null && downStatus == null) { |
| 527 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_UNREGISTERED, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 528 | deviceId, |
| 529 | deviceVlan, |
| 530 | subscriberVlan)); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 531 | } else if (downStatus != null) { |
| 532 | log.error("Subscriber with vlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 533 | "on port {} failed downstream uninstallation: {}", |
| 534 | subscriberVlan, deviceId, subscriberPort, downStatus); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 535 | } else if (upStatus != null) { |
| 536 | log.error("Subscriber with vlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 537 | "on port {} failed upstream uninstallation: {}", |
| 538 | subscriberVlan, deviceId, subscriberPort, upStatus); |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 539 | } |
| 540 | }, oltInstallers); |
alshabib | b7a9e17 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 541 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 544 | private void provisionVlans(ConnectPoint port, PortNumber uplinkPort, Optional<VlanId> defaultVlan, |
| 545 | SubscriberAndDeviceInformation sub) { |
| 546 | |
| 547 | log.info("Provisioning vlans..."); |
| 548 | |
| 549 | DeviceId deviceId = port.deviceId(); |
| 550 | PortNumber subscriberPort = port.port(); |
| 551 | VlanId deviceVlan = sub.sTag(); |
| 552 | VlanId subscriberVlan = sub.cTag(); |
| 553 | int techProfId = sub.technologyProfileId(); |
| 554 | |
| 555 | BandwidthProfileInformation upstreamBpInfo = getBandwidthProfileInformation(sub.upstreamBandwidthProfile()); |
| 556 | BandwidthProfileInformation downstreamBpInfo = getBandwidthProfileInformation(sub.downstreamBandwidthProfile()); |
| 557 | |
| 558 | MeterId usptreamMeterId = createMeter(deviceId, upstreamBpInfo); |
| 559 | MeterId downstreamMeterId = createMeter(deviceId, downstreamBpInfo); |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 560 | |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 561 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 562 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 563 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 564 | ForwardingObjective.Builder upFwd = upBuilder(uplinkPort, subscriberPort, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 565 | subscriberVlan, deviceVlan, |
| 566 | defaultVlan, usptreamMeterId, techProfId); |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 567 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 568 | ForwardingObjective.Builder downFwd = downBuilder(uplinkPort, subscriberPort, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 569 | subscriberVlan, deviceVlan, |
| 570 | defaultVlan, downstreamMeterId, techProfId); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 571 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 572 | flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() { |
| 573 | @Override |
| 574 | public void onSuccess(Objective objective) { |
| 575 | upFuture.complete(null); |
| 576 | } |
| 577 | |
| 578 | @Override |
| 579 | public void onError(Objective objective, ObjectiveError error) { |
| 580 | upFuture.complete(error); |
| 581 | } |
| 582 | })); |
| 583 | |
alshabib | bf23a1f | 2016-01-14 17:27:11 -0800 | [diff] [blame] | 584 | flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() { |
| 585 | @Override |
| 586 | public void onSuccess(Objective objective) { |
| 587 | downFuture.complete(null); |
| 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public void onError(Objective objective, ObjectiveError error) { |
| 592 | downFuture.complete(error); |
| 593 | } |
| 594 | })); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 595 | |
| 596 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 597 | if (upStatus == null && downStatus == null) { |
| 598 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 599 | deviceId, |
| 600 | deviceVlan, |
| 601 | subscriberVlan)); |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 602 | |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 603 | } else if (downStatus != null) { |
| 604 | log.error("Subscriber with vlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 605 | "on port {} failed downstream installation: {}", |
| 606 | subscriberVlan, deviceId, subscriberPort, downStatus); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 607 | } else if (upStatus != null) { |
| 608 | log.error("Subscriber with vlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 609 | "on port {} failed upstream installation: {}", |
| 610 | subscriberVlan, deviceId, subscriberPort, upStatus); |
alshabib | 3ea8264 | 2016-01-12 18:06:53 -0800 | [diff] [blame] | 611 | } |
| 612 | }, oltInstallers); |
| 613 | |
Jonathan Hart | e533a42 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 616 | private MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo) { |
| 617 | |
| 618 | if (bpInfo == null) { |
| 619 | log.warn("Bandwidth profile information is not found"); |
| 620 | return null; |
| 621 | } |
| 622 | |
| 623 | MeterId meterId = bpInfoToMeter.get(bpInfo.id()); |
| 624 | if (meterId != null) { |
| 625 | log.info("Meter is already added. MeterId {}", meterId); |
| 626 | return meterId; |
| 627 | } |
| 628 | |
| 629 | List<Band> meterBands = createMeterBands(bpInfo); |
| 630 | |
| 631 | MeterRequest meterRequest = DefaultMeterRequest.builder() |
| 632 | .withBands(meterBands) |
| 633 | .withUnit(Meter.Unit.KB_PER_SEC) |
| 634 | .forDevice(deviceId) |
| 635 | .fromApp(appId) |
| 636 | .burst() |
| 637 | .add(); |
| 638 | |
| 639 | Meter meter = meterService.submit(meterRequest); |
| 640 | bpInfoToMeter.put(bpInfo.id(), meter.id()); |
| 641 | log.info("Meter is created. Meter Id {}", meter.id()); |
| 642 | return meter.id(); |
| 643 | } |
| 644 | |
| 645 | private List<Band> createMeterBands(BandwidthProfileInformation bpInfo) { |
| 646 | List<Band> meterBands = new ArrayList<>(); |
| 647 | |
| 648 | meterBands.add(createMeterBand(bpInfo.committedInformationRate(), bpInfo.committedBurstSize())); |
| 649 | meterBands.add(createMeterBand(bpInfo.exceededInformationRate(), bpInfo.exceededBurstSize())); |
| 650 | |
| 651 | if (bpInfo.assuredInformationRate() != 0) { |
| 652 | meterBands.add(createMeterBand(bpInfo.assuredInformationRate(), 0L)); |
| 653 | } |
| 654 | return meterBands; |
| 655 | } |
| 656 | |
| 657 | private Band createMeterBand(long rate, Long burst) { |
| 658 | return DefaultBand.builder() |
| 659 | .withRate(rate) //already Kbps |
| 660 | .burstSize(burst) // already Kbits |
| 661 | .ofType(Band.Type.DROP) // no matter |
| 662 | .build(); |
| 663 | } |
| 664 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 665 | private ForwardingObjective.Builder downBuilder(PortNumber uplinkPort, |
| 666 | PortNumber subscriberPort, |
| 667 | VlanId subscriberVlan, |
| 668 | VlanId deviceVlan, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 669 | Optional<VlanId> defaultVlan, |
| 670 | MeterId meterId, |
| 671 | int techProfId) { |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 672 | TrafficSelector downstream = DefaultTrafficSelector.builder() |
| 673 | .matchVlanId(deviceVlan) |
| 674 | .matchInPort(uplinkPort) |
| 675 | .matchInnerVlanId(subscriberVlan) |
| 676 | .build(); |
| 677 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 678 | TrafficTreatment.Builder downstreamTreatmentBuilder = DefaultTrafficTreatment.builder() |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 679 | .popVlan() |
| 680 | .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan))) |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 681 | .setOutput(subscriberPort); |
| 682 | |
| 683 | if (meterId != null) { |
| 684 | downstreamTreatmentBuilder.meter(meterId); |
| 685 | } |
| 686 | |
| 687 | if (techProfId != -1) { |
| 688 | downstreamTreatmentBuilder.transition(techProfId); |
| 689 | } |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 690 | |
| 691 | return DefaultForwardingObjective.builder() |
| 692 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 693 | .withPriority(1000) |
| 694 | .makePermanent() |
| 695 | .withSelector(downstream) |
| 696 | .fromApp(appId) |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 697 | .withTreatment(downstreamTreatmentBuilder.build()); |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | private ForwardingObjective.Builder upBuilder(PortNumber uplinkPort, |
| 701 | PortNumber subscriberPort, |
| 702 | VlanId subscriberVlan, |
| 703 | VlanId deviceVlan, |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 704 | Optional<VlanId> defaultVlan, |
| 705 | MeterId meterId, |
| 706 | int technologyProfileId) { |
| 707 | |
| 708 | |
| 709 | VlanId dVlan = defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)); |
| 710 | |
| 711 | if (subscriberVlan.toShort() == 4096) { |
| 712 | dVlan = subscriberVlan; |
| 713 | } |
| 714 | |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 715 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 716 | .matchVlanId(dVlan) |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 717 | .matchInPort(subscriberPort) |
| 718 | .build(); |
| 719 | |
| 720 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 721 | TrafficTreatment.Builder upstreamTreatmentBuilder = DefaultTrafficTreatment.builder() |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 722 | .pushVlan() |
| 723 | .setVlanId(subscriberVlan) |
| 724 | .pushVlan() |
| 725 | .setVlanId(deviceVlan) |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 726 | .setOutput(uplinkPort); |
| 727 | |
| 728 | if (meterId != null) { |
| 729 | upstreamTreatmentBuilder.meter(meterId); |
| 730 | } |
| 731 | |
| 732 | if (technologyProfileId != -1) { |
| 733 | upstreamTreatmentBuilder.transition(technologyProfileId); |
| 734 | |
| 735 | } |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 736 | |
| 737 | return DefaultForwardingObjective.builder() |
| 738 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 739 | .withPriority(1000) |
| 740 | .makePermanent() |
| 741 | .withSelector(upstream) |
| 742 | .fromApp(appId) |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 743 | .withTreatment(upstreamTreatmentBuilder.build()); |
alshabib | 4ceaed3 | 2016-03-03 18:00:58 -0800 | [diff] [blame] | 744 | } |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 745 | private void provisionTransparentFlows(DeviceId deviceId, PortNumber uplinkPort, |
| 746 | PortNumber subscriberPort, |
| 747 | VlanId innerVlan, |
| 748 | VlanId outerVlan) { |
| 749 | |
| 750 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 751 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 752 | |
| 753 | ForwardingObjective.Builder upFwd = transparentUpBuilder(uplinkPort, subscriberPort, |
| 754 | innerVlan, outerVlan); |
| 755 | |
| 756 | |
| 757 | ForwardingObjective.Builder downFwd = transparentDownBuilder(uplinkPort, subscriberPort, |
| 758 | innerVlan, outerVlan); |
| 759 | |
| 760 | ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort); |
| 761 | |
| 762 | additionalVlans.put(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan)); |
| 763 | |
| 764 | flowObjectiveService.forward(deviceId, upFwd.add(new ObjectiveContext() { |
| 765 | @Override |
| 766 | public void onSuccess(Objective objective) { |
| 767 | upFuture.complete(null); |
| 768 | } |
| 769 | |
| 770 | @Override |
| 771 | public void onError(Objective objective, ObjectiveError error) { |
| 772 | upFuture.complete(error); |
| 773 | } |
| 774 | })); |
| 775 | |
| 776 | flowObjectiveService.forward(deviceId, downFwd.add(new ObjectiveContext() { |
| 777 | @Override |
| 778 | public void onSuccess(Objective objective) { |
| 779 | downFuture.complete(null); |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public void onError(Objective objective, ObjectiveError error) { |
| 784 | downFuture.complete(error); |
| 785 | } |
| 786 | })); |
| 787 | |
| 788 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 789 | if (downStatus != null) { |
| 790 | log.error("Flow with innervlan {} and outerVlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 791 | "on port {} failed downstream installation: {}", |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 792 | innerVlan, outerVlan, deviceId, subscriberPort, downStatus); |
| 793 | } else if (upStatus != null) { |
| 794 | log.error("Flow with innerVlan {} and outerVlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 795 | "on port {} failed upstream installation: {}", |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 796 | innerVlan, outerVlan, deviceId, subscriberPort, upStatus); |
| 797 | } |
| 798 | }, oltInstallers); |
| 799 | |
| 800 | } |
| 801 | |
| 802 | private ForwardingObjective.Builder transparentDownBuilder(PortNumber uplinkPort, |
| 803 | PortNumber subscriberPort, |
| 804 | VlanId innerVlan, |
| 805 | VlanId outerVlan) { |
| 806 | TrafficSelector downstream = DefaultTrafficSelector.builder() |
| 807 | .matchVlanId(outerVlan) |
| 808 | .matchInPort(uplinkPort) |
| 809 | .matchInnerVlanId(innerVlan) |
| 810 | .build(); |
| 811 | |
| 812 | TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder() |
| 813 | .setOutput(subscriberPort) |
| 814 | .build(); |
| 815 | |
| 816 | return DefaultForwardingObjective.builder() |
| 817 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 818 | .withPriority(1000) |
| 819 | .makePermanent() |
| 820 | .withSelector(downstream) |
| 821 | .fromApp(appId) |
| 822 | .withTreatment(downstreamTreatment); |
| 823 | } |
| 824 | |
| 825 | private ForwardingObjective.Builder transparentUpBuilder(PortNumber uplinkPort, |
| 826 | PortNumber subscriberPort, |
| 827 | VlanId innerVlan, |
| 828 | VlanId outerVlan) { |
| 829 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
| 830 | .matchVlanId(outerVlan) |
| 831 | .matchInPort(subscriberPort) |
| 832 | .matchInnerVlanId(innerVlan) |
| 833 | .build(); |
| 834 | |
| 835 | TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder() |
| 836 | .setOutput(uplinkPort) |
| 837 | .build(); |
| 838 | |
| 839 | return DefaultForwardingObjective.builder() |
| 840 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 841 | .withPriority(1000) |
| 842 | .makePermanent() |
| 843 | .withSelector(upstream) |
| 844 | .fromApp(appId) |
| 845 | .withTreatment(upstreamTreatment); |
| 846 | } |
| 847 | |
| 848 | private void unprovisionTransparentFlows(DeviceId deviceId, PortNumber uplink, |
| 849 | PortNumber subscriberPort, VlanId innerVlan, |
| 850 | VlanId outerVlan) { |
| 851 | |
| 852 | ConnectPoint cp = new ConnectPoint(deviceId, subscriberPort); |
| 853 | |
| 854 | additionalVlans.remove(cp, new AbstractMap.SimpleEntry(outerVlan, innerVlan)); |
| 855 | |
| 856 | CompletableFuture<ObjectiveError> downFuture = new CompletableFuture(); |
| 857 | CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); |
| 858 | |
| 859 | ForwardingObjective.Builder upFwd = transparentUpBuilder(uplink, subscriberPort, |
| 860 | innerVlan, outerVlan); |
| 861 | ForwardingObjective.Builder downFwd = transparentDownBuilder(uplink, subscriberPort, |
| 862 | innerVlan, outerVlan); |
| 863 | |
| 864 | |
| 865 | flowObjectiveService.forward(deviceId, upFwd.remove(new ObjectiveContext() { |
| 866 | @Override |
| 867 | public void onSuccess(Objective objective) { |
| 868 | upFuture.complete(null); |
| 869 | } |
| 870 | |
| 871 | @Override |
| 872 | public void onError(Objective objective, ObjectiveError error) { |
| 873 | upFuture.complete(error); |
| 874 | } |
| 875 | })); |
| 876 | |
| 877 | flowObjectiveService.forward(deviceId, downFwd.remove(new ObjectiveContext() { |
| 878 | @Override |
| 879 | public void onSuccess(Objective objective) { |
| 880 | downFuture.complete(null); |
| 881 | } |
| 882 | |
| 883 | @Override |
| 884 | public void onError(Objective objective, ObjectiveError error) { |
| 885 | downFuture.complete(error); |
| 886 | } |
| 887 | })); |
| 888 | |
| 889 | upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> { |
| 890 | if (downStatus != null) { |
| 891 | log.error("Flow with innerVlan {} and outerVlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 892 | "on port {} failed downstream uninstallation: {}", |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 893 | innerVlan, outerVlan, deviceId, subscriberPort, downStatus); |
| 894 | } else if (upStatus != null) { |
| 895 | log.error("Flow with innerVlan {} and outerVlan {} on device {} " + |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 896 | "on port {} failed upstream uninstallation: {}", |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 897 | innerVlan, outerVlan, deviceId, subscriberPort, upStatus); |
| 898 | } |
| 899 | }, oltInstallers); |
| 900 | |
| 901 | } |
| 902 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 903 | private void processEapolFilteringObjectives(DeviceId devId, PortNumber port, boolean install) { |
alshabib | 09753b5 | 2016-03-04 14:55:19 -0800 | [diff] [blame] | 904 | if (!mastershipService.isLocalMaster(devId)) { |
| 905 | return; |
| 906 | } |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 907 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 908 | |
| 909 | FilteringObjective eapol = (install ? builder.permit() : builder.deny()) |
alshabib | 50d9fc5 | 2016-02-12 15:47:20 -0800 | [diff] [blame] | 910 | .withKey(Criteria.matchInPort(port)) |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 911 | .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType())) |
| 912 | .withMeta(DefaultTrafficTreatment.builder() |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 913 | .setOutput(PortNumber.CONTROLLER).build()) |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 914 | .fromApp(appId) |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 915 | .withPriority(10000) |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 916 | .add(new ObjectiveContext() { |
| 917 | @Override |
| 918 | public void onSuccess(Objective objective) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 919 | log.info("Eapol filter for {} on {} {}.", |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 920 | devId, port, (install) ? "installed" : "removed"); |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | @Override |
| 924 | public void onError(Objective objective, ObjectiveError error) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 925 | log.info("Eapol filter for {} on {} failed {} because {}", |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 926 | devId, port, (install) ? "installation" : "removal", |
| 927 | error); |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 928 | } |
| 929 | }); |
| 930 | |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 931 | flowObjectiveService.filter(devId, eapol); |
alshabib | 000b6fc | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 932 | |
alshabib | dec2e25 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 935 | /** |
| 936 | * Installs trap filtering objectives for particular traffic types on an |
| 937 | * NNI port. |
| 938 | * |
| 939 | * @param devId device ID |
| 940 | * @param port port number |
| 941 | * @param install true to install, false to remove |
| 942 | */ |
| 943 | private void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) { |
| 944 | processLldpFilteringObjective(devId, port, install); |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 945 | if (enableDhcpOnProvisioning) { |
| 946 | processDhcpFilteringObjectives(devId, port, install, false); |
| 947 | } |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | private void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) { |
| 951 | if (!mastershipService.isLocalMaster(devId)) { |
| 952 | return; |
| 953 | } |
| 954 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 955 | |
| 956 | FilteringObjective lldp = (install ? builder.permit() : builder.deny()) |
| 957 | .withKey(Criteria.matchInPort(port)) |
| 958 | .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType())) |
| 959 | .withMeta(DefaultTrafficTreatment.builder() |
| 960 | .setOutput(PortNumber.CONTROLLER).build()) |
| 961 | .fromApp(appId) |
| 962 | .withPriority(10000) |
| 963 | .add(new ObjectiveContext() { |
| 964 | @Override |
| 965 | public void onSuccess(Objective objective) { |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 966 | log.info("LLDP filter for device {} on port {} {}.", |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 967 | devId, port, (install) ? "installed" : "removed"); |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | @Override |
| 971 | public void onError(Objective objective, ObjectiveError error) { |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 972 | log.info("LLDP filter for device {} on port {} failed {} because {}", |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 973 | devId, port, (install) ? "installation" : "removal", |
| 974 | error); |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 975 | } |
| 976 | }); |
| 977 | |
| 978 | flowObjectiveService.filter(devId, lldp); |
| 979 | |
| 980 | } |
| 981 | |
Saurav Das | acc5eeb | 2018-10-11 10:58:01 -0700 | [diff] [blame] | 982 | /** |
| 983 | * Trap dhcp packets to the controller. |
| 984 | * |
| 985 | * @param devId the device identifier |
| 986 | * @param port the port for which this trap flow is designated |
| 987 | * @param install true to install the flow, false to remove the flow |
| 988 | * @param upstream true if trapped packets are flowing upstream towards |
| 989 | * server, false if packets are flowing dowstream towards client |
| 990 | */ |
| 991 | private void processDhcpFilteringObjectives(DeviceId devId, PortNumber port, |
| 992 | boolean install, |
| 993 | boolean upstream) { |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 994 | if (!mastershipService.isLocalMaster(devId)) { |
| 995 | return; |
| 996 | } |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 997 | |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 998 | if (enableDhcpV4) { |
| 999 | int udpSrc = (upstream) ? 68 : 67; |
| 1000 | int udpDst = (upstream) ? 67 : 68; |
| 1001 | |
| 1002 | EthType ethType = EthType.EtherType.IPV4.ethType(); |
| 1003 | byte protocol = IPv4.PROTOCOL_UDP; |
| 1004 | |
| 1005 | this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType, protocol, install); |
| 1006 | } |
| 1007 | |
| 1008 | if (enableDhcpV6) { |
| 1009 | int udpSrc = (upstream) ? 547 : 546; |
| 1010 | int udpDst = (upstream) ? 546 : 547; |
| 1011 | |
| 1012 | EthType ethType = EthType.EtherType.IPV6.ethType(); |
| 1013 | byte protocol = IPv6.PROTOCOL_UDP; |
| 1014 | |
| 1015 | this.addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType, protocol, install); |
| 1016 | } |
| 1017 | |
| 1018 | } |
| 1019 | |
| 1020 | private void addDhcpFilteringObjectives(DeviceId devId, |
| 1021 | PortNumber port, |
| 1022 | int udpSrc, |
| 1023 | int udpDst, |
| 1024 | EthType ethType, |
| 1025 | byte protocol, |
| 1026 | boolean install) { |
Saurav Das | acc5eeb | 2018-10-11 10:58:01 -0700 | [diff] [blame] | 1027 | |
| 1028 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1029 | FilteringObjective dhcpUpstream = (install ? builder.permit() : builder.deny()) |
| 1030 | .withKey(Criteria.matchInPort(port)) |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 1031 | .addCondition(Criteria.matchEthType(ethType)) |
| 1032 | .addCondition(Criteria.matchIPProtocol(protocol)) |
Saurav Das | acc5eeb | 2018-10-11 10:58:01 -0700 | [diff] [blame] | 1033 | .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc))) |
| 1034 | .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst))) |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1035 | .withMeta(DefaultTrafficTreatment.builder() |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1036 | .setOutput(PortNumber.CONTROLLER).build()) |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1037 | .fromApp(appId) |
Matt Jeanneret | 3f57926 | 2018-06-14 17:16:23 -0400 | [diff] [blame] | 1038 | .withPriority(10000) |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1039 | .add(new ObjectiveContext() { |
| 1040 | @Override |
| 1041 | public void onSuccess(Objective objective) { |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 1042 | log.info("DHCP {} filter for device {} on port {} {}.", |
| 1043 | (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6", |
| 1044 | devId, port, (install) ? "installed" : "removed"); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | @Override |
| 1048 | public void onError(Objective objective, ObjectiveError error) { |
Matteo Scandolo | 63460d1 | 2018-11-02 16:19:04 -0700 | [diff] [blame] | 1049 | log.info("DHCP {} filter for device {} on port {} failed {} because {}", |
| 1050 | (ethType.equals(EthType.EtherType.IPV4.ethType())) ? "v4" : "v6", |
| 1051 | devId, port, (install) ? "installation" : "removal", |
| 1052 | error); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1053 | } |
| 1054 | }); |
| 1055 | |
| 1056 | flowObjectiveService.filter(devId, dhcpUpstream); |
| 1057 | } |
| 1058 | |
| 1059 | private void processIgmpFilteringObjectives(DeviceId devId, PortNumber port, boolean install) { |
| 1060 | if (!mastershipService.isLocalMaster(devId)) { |
| 1061 | return; |
| 1062 | } |
| 1063 | |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1064 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1065 | |
| 1066 | builder = install ? builder.permit() : builder.deny(); |
| 1067 | |
| 1068 | FilteringObjective igmp = builder |
| 1069 | .withKey(Criteria.matchInPort(port)) |
| 1070 | .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType())) |
| 1071 | .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP)) |
| 1072 | .withMeta(DefaultTrafficTreatment.builder() |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1073 | .setOutput(PortNumber.CONTROLLER).build()) |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1074 | .fromApp(appId) |
| 1075 | .withPriority(10000) |
| 1076 | .add(new ObjectiveContext() { |
| 1077 | @Override |
| 1078 | public void onSuccess(Objective objective) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1079 | log.info("Igmp filter for {} on {} {}.", |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1080 | devId, port, (install) ? "installed" : "removed"); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | @Override |
| 1084 | public void onError(Objective objective, ObjectiveError error) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1085 | log.info("Igmp filter for {} on {} failed {} because {}.", |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1086 | devId, port, (install) ? "installation" : "removal", |
| 1087 | error); |
Amit Ghosh | 95e2f65 | 2017-08-23 12:49:46 +0100 | [diff] [blame] | 1088 | } |
| 1089 | }); |
| 1090 | |
| 1091 | flowObjectiveService.filter(devId, igmp); |
| 1092 | } |
| 1093 | |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1094 | /** |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 1095 | * Creates trap flows for device, including DHCP and LLDP trap on NNI and |
| 1096 | * EAPOL trap on the UNIs, if device is present in Sadis config. |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1097 | * |
| 1098 | * @param dev Device to look for |
| 1099 | */ |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 1100 | private void checkAndCreateDeviceFlows(Device dev) { |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1101 | // we create only for the ones we are master of |
| 1102 | if (!mastershipService.isLocalMaster(dev.id())) { |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1103 | return; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1104 | } |
| 1105 | // check if this device is provisioned in Sadis |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1106 | SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev); |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 1107 | log.debug("checkAndCreateDeviceFlows: deviceInfo {}", deviceInfo); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1108 | |
| 1109 | if (deviceInfo != null) { |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 1110 | // This is an OLT device as per Sadis, we create flows for UNI and NNI ports |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1111 | for (Port p : deviceService.getPorts(dev.id())) { |
| 1112 | if (isUniPort(dev, p)) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1113 | processEapolFilteringObjectives(dev.id(), p.number(), true); |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 1114 | } else { |
| 1115 | processNniFilteringObjectives(dev.id(), p.number(), true); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 1121 | |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1122 | /** |
| 1123 | * Get the uplink for of the OLT device. |
| 1124 | * |
| 1125 | * This assumes that the OLT has a single uplink port. When more uplink ports need to be supported |
| 1126 | * this logic needs to be changed |
| 1127 | * |
| 1128 | * @param dev Device to look for |
| 1129 | * @return The uplink Port of the OLT |
| 1130 | */ |
| 1131 | private Port getUplinkPort(Device dev) { |
| 1132 | // check if this device is provisioned in Sadis |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1133 | SubscriberAndDeviceInformation deviceInfo = getOltInfo(dev); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1134 | log.debug("getUplinkPort: deviceInfo {}", deviceInfo); |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1135 | if (deviceInfo == null) { |
| 1136 | log.warn("Device {} is not configured in SADIS .. cannot fetch device" |
| 1137 | + " info", dev.id()); |
| 1138 | return null; |
| 1139 | } |
| 1140 | // Return the port that has been configured as the uplink port of this OLT in Sadis |
| 1141 | for (Port p: deviceService.getPorts(dev.id())) { |
| 1142 | if (p.number().toLong() == deviceInfo.uplinkPort()) { |
| 1143 | log.debug("getUplinkPort: Found port {}", p); |
| 1144 | return p; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1148 | log.debug("getUplinkPort: No uplink port found for OLT {}", dev.id()); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1149 | return null; |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * Return the subscriber on a port. |
| 1154 | * |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 1155 | * @param cp ConnectPoint on which to find the subscriber |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1156 | * @return subscriber if found else null |
| 1157 | */ |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 1158 | SubscriberAndDeviceInformation getSubscriber(ConnectPoint cp) { |
| 1159 | Port port = deviceService.getPort(cp); |
| 1160 | checkNotNull(port, "Invalid connect point"); |
| 1161 | String portName = port.annotations().value(AnnotationKeys.PORT_NAME); |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 1162 | return subsService.get(portName); |
| 1163 | } |
| 1164 | |
| 1165 | private boolean isUniPort(Device d, Port p) { |
| 1166 | Port ulPort = getUplinkPort(d); |
| 1167 | if (ulPort != null) { |
| 1168 | return (ulPort.number().toLong() != p.number().toLong()); |
| 1169 | } |
| 1170 | return false; |
Jonathan Hart | 1d34c8b | 2018-05-05 15:37:28 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Jonathan Hart | 4c53800 | 2018-08-23 10:11:54 -0700 | [diff] [blame] | 1173 | private SubscriberAndDeviceInformation getOltInfo(Device dev) { |
| 1174 | String devSerialNo = dev.serialNumber(); |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1175 | return subsService.get(devSerialNo); |
Jonathan Hart | 4c53800 | 2018-08-23 10:11:54 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1178 | private class InternalDeviceListener implements DeviceListener { |
| 1179 | @Override |
| 1180 | public void event(DeviceEvent event) { |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1181 | eventExecutor.execute(() -> { |
| 1182 | DeviceId devId = event.subject().id(); |
| 1183 | Device dev = event.subject(); |
Jonathan Hart | 4c53800 | 2018-08-23 10:11:54 -0700 | [diff] [blame] | 1184 | |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1185 | if (event.type() == DeviceEvent.Type.PORT_STATS_UPDATED) { |
| 1186 | return; |
| 1187 | } |
Jonathan Hart | 4c53800 | 2018-08-23 10:11:54 -0700 | [diff] [blame] | 1188 | |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1189 | if (getOltInfo(dev) == null) { |
| 1190 | log.debug("No device info found, this is not an OLT"); |
| 1191 | return; |
| 1192 | } |
Jonathan Hart | 4c53800 | 2018-08-23 10:11:54 -0700 | [diff] [blame] | 1193 | |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1194 | log.debug("OLT got {} event for {}", event.type(), event.subject()); |
Jonathan Hart | 4c53800 | 2018-08-23 10:11:54 -0700 | [diff] [blame] | 1195 | |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1196 | switch (event.type()) { |
| 1197 | //TODO: Port handling and bookkeeping should be improved once |
| 1198 | // olt firmware handles correct behaviour. |
| 1199 | case PORT_ADDED: |
| 1200 | if (isUniPort(dev, event.port())) { |
| 1201 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port())); |
Jonathan Hart | 4c53800 | 2018-08-23 10:11:54 -0700 | [diff] [blame] | 1202 | |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1203 | if (event.port().isEnabled()) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1204 | processEapolFilteringObjectives(devId, event.port().number(), true); |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } else { |
| 1207 | checkAndCreateDeviceFlows(dev); |
| 1208 | } |
| 1209 | break; |
| 1210 | case PORT_REMOVED: |
| 1211 | if (isUniPort(dev, event.port())) { |
| 1212 | if (event.port().isEnabled()) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1213 | processEapolFilteringObjectives(devId, event.port().number(), false); |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1214 | removeSubscriber(new ConnectPoint(devId, event.port().number())); |
| 1215 | } |
| 1216 | |
| 1217 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, event.port())); |
| 1218 | } |
| 1219 | |
| 1220 | break; |
| 1221 | case PORT_UPDATED: |
| 1222 | if (!isUniPort(dev, event.port())) { |
| 1223 | break; |
| 1224 | } |
Jonathan Hart | 1d34c8b | 2018-05-05 15:37:28 -0700 | [diff] [blame] | 1225 | |
| 1226 | if (event.port().isEnabled()) { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1227 | processEapolFilteringObjectives(devId, event.port().number(), true); |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1228 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port())); |
| 1229 | } else { |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 1230 | processEapolFilteringObjectives(devId, event.port().number(), false); |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1231 | post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, event.port())); |
Jonathan Hart | 1d34c8b | 2018-05-05 15:37:28 -0700 | [diff] [blame] | 1232 | } |
alshabib | bb83aa2 | 2016-02-10 15:08:23 -0800 | [diff] [blame] | 1233 | break; |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1234 | case DEVICE_ADDED: |
alshabib | 7c19001 | 2016-02-09 18:22:33 -0800 | [diff] [blame] | 1235 | post(new AccessDeviceEvent( |
| 1236 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 1237 | null, null)); |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1238 | |
| 1239 | // Send UNI_ADDED events for all existing ports |
| 1240 | deviceService.getPorts(devId).stream() |
| 1241 | .filter(p -> isUniPort(dev, p)) |
| 1242 | .filter(Port::isEnabled) |
| 1243 | .forEach(p -> post(new AccessDeviceEvent( |
| 1244 | AccessDeviceEvent.Type.UNI_ADDED, devId, p))); |
| 1245 | |
Jonathan Hart | 403372d | 2018-08-22 11:44:13 -0700 | [diff] [blame] | 1246 | checkAndCreateDeviceFlows(dev); |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1247 | break; |
| 1248 | case DEVICE_REMOVED: |
| 1249 | deviceService.getPorts(devId).stream() |
| 1250 | .filter(p -> isUniPort(dev, p)) |
| 1251 | .forEach(p -> post(new AccessDeviceEvent( |
| 1252 | AccessDeviceEvent.Type.UNI_REMOVED, devId, p))); |
| 1253 | |
alshabib | 7c19001 | 2016-02-09 18:22:33 -0800 | [diff] [blame] | 1254 | post(new AccessDeviceEvent( |
| 1255 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 1256 | null, null)); |
Matteo Scandolo | 632f0fc | 2018-09-07 12:21:45 -0700 | [diff] [blame] | 1257 | break; |
| 1258 | case DEVICE_AVAILABILITY_CHANGED: |
| 1259 | if (deviceService.isAvailable(devId)) { |
| 1260 | post(new AccessDeviceEvent( |
| 1261 | AccessDeviceEvent.Type.DEVICE_CONNECTED, devId, |
| 1262 | null, null)); |
| 1263 | checkAndCreateDeviceFlows(dev); |
| 1264 | } else { |
| 1265 | post(new AccessDeviceEvent( |
| 1266 | AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId, |
| 1267 | null, null)); |
| 1268 | } |
| 1269 | break; |
| 1270 | case DEVICE_UPDATED: |
| 1271 | case DEVICE_SUSPENDED: |
| 1272 | case PORT_STATS_UPDATED: |
| 1273 | default: |
| 1274 | return; |
| 1275 | } |
| 1276 | }); |
alshabib | f0e7e70 | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } |
Gamze Abaka | 641fc07 | 2018-09-04 09:16:27 +0000 | [diff] [blame^] | 1279 | |
| 1280 | private class InternalMeterListener implements MeterListener { |
| 1281 | |
| 1282 | @Override |
| 1283 | public void event(MeterEvent meterEvent) { |
| 1284 | if (deleteMeters && MeterEvent.Type.METER_REFERENCE_COUNT_ZERO.equals(meterEvent.type())) { |
| 1285 | log.debug("Zero Count Meter Event is received. Meter is {}", meterEvent.subject()); |
| 1286 | Meter meter = meterEvent.subject(); |
| 1287 | if (meter != null && appId.equals(meter.appId())) { |
| 1288 | deleteMeter(meter.deviceId(), meter.id()); |
| 1289 | } |
| 1290 | } else if (MeterEvent.Type.METER_REMOVED.equals(meterEvent.type())) { |
| 1291 | log.debug("Meter removed event is received. Meter is {}", meterEvent.subject()); |
| 1292 | removeMeterFromBpMap(meterEvent.subject()); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | private void deleteMeter(DeviceId deviceId, MeterId meterId) { |
| 1297 | Meter meter = meterService.getMeter(deviceId, meterId); |
| 1298 | MeterRequest meterRequest = DefaultMeterRequest.builder() |
| 1299 | .withBands(meter.bands()) |
| 1300 | .withUnit(meter.unit()) |
| 1301 | .forDevice(deviceId) |
| 1302 | .fromApp(appId) |
| 1303 | .burst() |
| 1304 | .remove(); |
| 1305 | |
| 1306 | meterService.withdraw(meterRequest, meterId); |
| 1307 | |
| 1308 | } |
| 1309 | |
| 1310 | private void removeMeterFromBpMap(Meter meter) { |
| 1311 | for (Map.Entry<String, MeterId> entry : bpInfoToMeter.entrySet()) { |
| 1312 | if (entry.getValue().equals(meter.id())) { |
| 1313 | bpInfoToMeter.remove(entry.getKey()); |
| 1314 | log.info("Deleted from the internal map. Profile {} and Meter {}", entry.getKey(), meter.id()); |
| 1315 | break; |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | } |