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