alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | cf85aa8 | 2017-08-03 22:46:01 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [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 | */ |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 16 | package org.opencord.cordmcast.impl; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 17 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
alshabib | 09069c9 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 20 | import org.apache.commons.lang3.tuple.ImmutablePair; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.VlanId; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 24 | import org.onlab.util.KryoNamespace; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 25 | import org.onosproject.cfg.ComponentConfigService; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.LeadershipService; |
| 28 | import org.onosproject.cluster.NodeId; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 31 | import org.onosproject.mastership.MastershipService; |
| 32 | import org.onosproject.mcast.api.McastEvent; |
| 33 | import org.onosproject.mcast.api.McastListener; |
| 34 | import org.onosproject.mcast.api.McastRoute; |
| 35 | import org.onosproject.mcast.api.MulticastRouteService; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 37 | import org.onosproject.net.Device; |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 39 | import org.onosproject.net.HostId; |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 40 | import org.onosproject.net.PortNumber; |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 41 | import org.onosproject.net.config.ConfigFactory; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
| 43 | import org.onosproject.net.config.NetworkConfigListener; |
| 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 45 | import org.onosproject.net.config.basics.McastConfig; |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 46 | import org.onosproject.net.config.basics.SubjectFactories; |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 47 | import org.onosproject.net.device.DeviceEvent; |
| 48 | import org.onosproject.net.device.DeviceListener; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 49 | import org.onosproject.net.device.DeviceService; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 51 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 54 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 55 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 57 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 58 | import org.onosproject.net.flowobjective.NextObjective; |
| 59 | import org.onosproject.net.flowobjective.Objective; |
| 60 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 61 | import org.onosproject.net.flowobjective.ObjectiveError; |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 62 | import org.onosproject.net.group.GroupService; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 63 | import org.onosproject.store.serializers.KryoNamespaces; |
| 64 | import org.onosproject.store.service.ConsistentMap; |
| 65 | import org.onosproject.store.service.Serializer; |
| 66 | import org.onosproject.store.service.StorageService; |
| 67 | import org.onosproject.store.service.Versioned; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 68 | import org.opencord.cordmcast.CordMcastService; |
| 69 | import org.opencord.cordmcast.CordMcastStatisticsService; |
| 70 | import org.opencord.sadis.SadisService; |
| 71 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 72 | import org.osgi.service.component.ComponentContext; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 73 | import org.osgi.service.component.annotations.Activate; |
| 74 | import org.osgi.service.component.annotations.Component; |
| 75 | import org.osgi.service.component.annotations.Deactivate; |
| 76 | import org.osgi.service.component.annotations.Modified; |
| 77 | import org.osgi.service.component.annotations.Reference; |
| 78 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 79 | import org.slf4j.Logger; |
| 80 | |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 81 | import java.util.Dictionary; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 82 | import java.util.Map; |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 83 | import java.util.Objects; |
Jonathan Hart | 0c19496 | 2016-05-23 17:08:15 -0700 | [diff] [blame] | 84 | import java.util.Optional; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 85 | import java.util.Properties; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 86 | import java.util.Set; |
| 87 | import java.util.concurrent.ExecutorService; |
| 88 | import java.util.concurrent.locks.Lock; |
| 89 | import java.util.concurrent.locks.ReentrantLock; |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 90 | import java.util.function.Consumer; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 91 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 92 | import static com.google.common.base.Strings.isNullOrEmpty; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 93 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 94 | import static org.onlab.util.Tools.get; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 95 | import static org.onlab.util.Tools.groupedThreads; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 96 | import static org.opencord.cordmcast.impl.OsgiPropertyConstants.*; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 97 | import static org.slf4j.LoggerFactory.getLogger; |
| 98 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 99 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 100 | /** |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 101 | * CORD multicast provisioning application. Operates by listening to |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 102 | * events on the multicast rib and provisioning groups to program multicast |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 103 | * flows on the dataplane. |
| 104 | */ |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 105 | @Component(immediate = true, |
| 106 | property = { |
| 107 | VLAN_ENABLED + ":Boolean=" + DEFAULT_VLAN_ENABLED, |
| 108 | PRIORITY + ":Integer=" + DEFAULT_PRIORITY, |
| 109 | }) |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 110 | public class CordMcast implements CordMcastService { |
| 111 | private static final String APP_NAME = "org.opencord.mcast"; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 112 | |
Jonathan Hart | 0c19496 | 2016-05-23 17:08:15 -0700 | [diff] [blame] | 113 | private final Logger log = getLogger(getClass()); |
alshabib | 09069c9 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 114 | |
alshabib | 09069c9 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 115 | private static final int DEFAULT_PRIORITY = 500; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 116 | private static final short DEFAULT_MCAST_VLAN = 4000; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 117 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 119 | protected MulticastRouteService mcastService; |
| 120 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 122 | protected FlowObjectiveService flowObjectiveService; |
| 123 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 125 | protected CoreService coreService; |
| 126 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 128 | protected ComponentConfigService componentConfigService; |
| 129 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 131 | protected NetworkConfigRegistry networkConfig; |
| 132 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 134 | protected StorageService storageService; |
| 135 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 137 | protected MastershipService mastershipService; |
| 138 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 140 | public DeviceService deviceService; |
| 141 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 143 | private ClusterService clusterService; |
| 144 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 146 | private LeadershipService leadershipService; |
| 147 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 149 | protected SadisService sadisService; |
| 150 | |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 152 | protected CordMcastStatisticsService cordMcastStatisticsService; |
| 153 | |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 155 | protected GroupService groupService; |
| 156 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 157 | protected McastListener listener = new InternalMulticastListener(); |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 158 | |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 159 | private InternalNetworkConfigListener configListener = |
| 160 | new InternalNetworkConfigListener(); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 161 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 162 | private ConsistentMap<NextKey, NextContent> groups; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 163 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 164 | private ApplicationId appId; |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 165 | private ApplicationId coreAppId; |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 166 | private short mcastVlan = DEFAULT_MCAST_VLAN; |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 167 | private VlanId mcastInnerVlan = VlanId.NONE; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 168 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 169 | /** |
| 170 | * Whether to use VLAN for multicast traffic. |
| 171 | **/ |
alshabib | 09069c9 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 172 | private boolean vlanEnabled = DEFAULT_VLAN_ENABLED; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 173 | |
Carmelo Cascone | 995fd68 | 2019-11-14 14:22:39 -0800 | [diff] [blame] | 174 | /** |
| 175 | * Priority for multicast rules. |
| 176 | **/ |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 177 | private int priority = DEFAULT_PRIORITY; |
| 178 | |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 179 | private static final Class<McastConfig> CORD_MCAST_CONFIG_CLASS = |
| 180 | McastConfig.class; |
| 181 | |
| 182 | private ConfigFactory<ApplicationId, McastConfig> cordMcastConfigFactory = |
| 183 | new ConfigFactory<ApplicationId, McastConfig>( |
| 184 | SubjectFactories.APP_SUBJECT_FACTORY, CORD_MCAST_CONFIG_CLASS, "multicast") { |
| 185 | @Override |
| 186 | public McastConfig createConfig() { |
| 187 | return new McastConfig(); |
| 188 | } |
| 189 | }; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 190 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 191 | // lock to synchronize local operations |
| 192 | private final Lock mcastLock = new ReentrantLock(); |
| 193 | private void mcastLock() { |
| 194 | mcastLock.lock(); |
| 195 | } |
| 196 | private void mcastUnlock() { |
| 197 | mcastLock.unlock(); |
| 198 | } |
| 199 | private ExecutorService eventExecutor; |
| 200 | |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 201 | //Device listener to purge groups upon device disconnection. |
| 202 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 203 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 204 | @Activate |
Jonathan Hart | 435ffc4 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 205 | public void activate(ComponentContext context) { |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 206 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | 435ffc4 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 207 | modified(context); |
| 208 | |
Charles Chan | f867c4b | 2017-01-20 11:22:25 -0800 | [diff] [blame] | 209 | appId = coreService.registerApplication(APP_NAME); |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 210 | coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME); |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 211 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 212 | eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("cord/mcast", |
| 213 | "events-mcast-%d", log)); |
| 214 | |
| 215 | KryoNamespace.Builder groupsKryo = new KryoNamespace.Builder() |
| 216 | .register(KryoNamespaces.API) |
| 217 | .register(NextKey.class) |
| 218 | .register(NextContent.class); |
| 219 | groups = storageService |
| 220 | .<NextKey, NextContent>consistentMapBuilder() |
| 221 | .withName("cord-mcast-groups-store") |
| 222 | .withSerializer(Serializer.using(groupsKryo.build("CordMcast-Groups"))) |
| 223 | .build(); |
| 224 | |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 225 | networkConfig.registerConfigFactory(cordMcastConfigFactory); |
| 226 | networkConfig.addListener(configListener); |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 227 | mcastService.addListener(listener); |
| 228 | |
alshabib | 09069c9 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 229 | mcastService.getRoutes().stream() |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 230 | .map(r -> new ImmutablePair<>(r, mcastService.sinks(r))) |
alshabib | 09069c9 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 231 | .filter(pair -> pair.getRight() != null && !pair.getRight().isEmpty()) |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 232 | .forEach(pair -> pair.getRight().forEach(sink -> addSink(pair.getLeft(), |
| 233 | sink))); |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 234 | |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 235 | McastConfig config = networkConfig.getConfig(coreAppId, CORD_MCAST_CONFIG_CLASS); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 236 | updateConfig(config); |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 237 | deviceService.addListener(deviceListener); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 238 | log.info("Started"); |
| 239 | } |
| 240 | |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 241 | @Modified |
| 242 | public void modified(ComponentContext context) { |
| 243 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 244 | |
| 245 | String s = get(properties, VLAN_ENABLED); |
| 246 | vlanEnabled = isNullOrEmpty(s) ? DEFAULT_VLAN_ENABLED : Boolean.parseBoolean(s.trim()); |
| 247 | |
| 248 | try { |
| 249 | s = get(properties, PRIORITY); |
| 250 | priority = isNullOrEmpty(s) ? DEFAULT_PRIORITY : Integer.parseInt(s.trim()); |
| 251 | } catch (NumberFormatException ne) { |
| 252 | log.error("Unable to parse configuration parameter for priority", ne); |
| 253 | priority = DEFAULT_PRIORITY; |
| 254 | } |
Esin Karaman | e489001 | 2020-04-19 11:58:54 +0000 | [diff] [blame] | 255 | feedStatsServiceWithVlanConfigValues(); |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 256 | } |
| 257 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 258 | @Deactivate |
| 259 | public void deactivate() { |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 260 | deviceService.removeListener(deviceListener); |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 261 | componentConfigService.unregisterProperties(getClass(), false); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 262 | mcastService.removeListener(listener); |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 263 | networkConfig.removeListener(configListener); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 264 | networkConfig.unregisterConfigFactory(cordMcastConfigFactory); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 265 | eventExecutor.shutdown(); |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 266 | eventExecutor = null; |
| 267 | groups.clear(); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 268 | groups.destroy(); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 269 | log.info("Stopped"); |
| 270 | } |
| 271 | |
Esin Karaman | e489001 | 2020-04-19 11:58:54 +0000 | [diff] [blame] | 272 | /** |
| 273 | * Updates the stats service with current VLAN config values. |
| 274 | */ |
| 275 | private void feedStatsServiceWithVlanConfigValues() { |
| 276 | cordMcastStatisticsService.setVlanValue(assignedVlan()); |
| 277 | cordMcastStatisticsService.setInnerVlanValue(assignedInnerVlan()); |
| 278 | } |
| 279 | |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 280 | private void clearGroups() { |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 281 | mcastLock(); |
| 282 | try { |
| 283 | groups.keySet().forEach(groupInfo -> { |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 284 | NextContent next = groups.get(groupInfo).value(); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 285 | if (!isLocalLeader(groupInfo.getDevice())) { |
| 286 | return; |
| 287 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 288 | if (next != null) { |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 289 | //On Success of removing the fwd objective we remove also the group. |
| 290 | Consumer<Objective> onSuccess = (objective) -> { |
| 291 | log.debug("Successfully removed fwd objective for {} on {}, " + |
| 292 | "removing next objective {}", groupInfo.group, |
| 293 | groupInfo.getDevice(), next.getNextId()); |
| 294 | eventExecutor.submit(() -> flowObjectiveService.next(groupInfo.getDevice(), |
| 295 | nextObject(next.getNextId(), |
| 296 | null, |
| 297 | NextType.Remove, groupInfo.group))); |
| 298 | }; |
| 299 | |
| 300 | ObjectiveContext context = |
| 301 | new DefaultObjectiveContext(onSuccess, (objective, error) -> |
| 302 | log.warn("Failed to remove {} on {}: {}", |
| 303 | groupInfo.group, next.getNextId(), error)); |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 304 | // remove the flow rule |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 305 | flowObjectiveService.forward(groupInfo.getDevice(), |
| 306 | fwdObject(next.getNextId(), |
| 307 | groupInfo.group).remove(context)); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 308 | |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 309 | } |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 310 | }); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 311 | } finally { |
| 312 | mcastUnlock(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | private VlanId multicastVlan() { |
| 317 | return VlanId.vlanId(mcastVlan); |
| 318 | } |
| 319 | |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 320 | protected VlanId assignedVlan() { |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 321 | return vlanEnabled ? multicastVlan() : VlanId.NONE; |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 322 | } |
| 323 | |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 324 | protected VlanId assignedInnerVlan() { |
| 325 | return vlanEnabled ? mcastInnerVlan : VlanId.NONE; |
| 326 | } |
| 327 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 328 | private class InternalMulticastListener implements McastListener { |
| 329 | @Override |
| 330 | public void event(McastEvent event) { |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 331 | eventExecutor.execute(() -> { |
| 332 | switch (event.type()) { |
| 333 | case ROUTE_ADDED: |
| 334 | case ROUTE_REMOVED: |
| 335 | case SOURCES_ADDED: |
| 336 | break; |
| 337 | case SINKS_ADDED: |
| 338 | addSinks(event); |
| 339 | break; |
| 340 | case SINKS_REMOVED: |
| 341 | removeSinks(event); |
| 342 | break; |
| 343 | default: |
| 344 | log.warn("Unknown mcast event {}", event.type()); |
| 345 | } |
| 346 | }); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Processes previous, and new sinks then finds the sinks to be removed. |
| 352 | * @param prevSinks the previous sinks to be evaluated |
| 353 | * @param newSinks the new sinks to be evaluated |
| 354 | * @returnt the set of the sinks to be removed |
| 355 | */ |
| 356 | private Set<ConnectPoint> getSinksToBeRemoved(Map<HostId, Set<ConnectPoint>> prevSinks, |
| 357 | Map<HostId, Set<ConnectPoint>> newSinks) { |
| 358 | return getSinksToBeProcessed(prevSinks, newSinks); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /** |
| 363 | * Processes previous, and new sinks then finds the sinks to be added. |
| 364 | * @param newSinks the new sinks to be processed |
| 365 | * @param allPrevSinks all previous sinks |
| 366 | * @return the set of the sinks to be added |
| 367 | */ |
| 368 | private Set<ConnectPoint> getSinksToBeAdded(Map<HostId, Set<ConnectPoint>> newSinks, |
| 369 | Map<HostId, Set<ConnectPoint>> allPrevSinks) { |
| 370 | return getSinksToBeProcessed(newSinks, allPrevSinks); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Gets single-homed sinks that are in set1 but not in set2. |
| 375 | * @param sinkSet1 the first sink map |
| 376 | * @param sinkSet2 the second sink map |
| 377 | * @return a set containing all the single-homed sinks found in set1 but not in set2 |
| 378 | */ |
| 379 | private Set<ConnectPoint> getSinksToBeProcessed(Map<HostId, Set<ConnectPoint>> sinkSet1, |
| 380 | Map<HostId, Set<ConnectPoint>> sinkSet2) { |
| 381 | final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet(); |
| 382 | sinkSet1.forEach(((hostId, connectPoints) -> { |
| 383 | if (HostId.NONE.equals(hostId)) { |
| 384 | //assume all connect points associated with HostId.NONE are single homed sinks |
| 385 | sinksToBeProcessed.addAll(connectPoints); |
| 386 | return; |
| 387 | } |
| 388 | })); |
| 389 | Set<ConnectPoint> singleHomedSinksOfSet2 = sinkSet2.get(HostId.NONE) == null ? |
| 390 | Sets.newHashSet() : |
| 391 | sinkSet2.get(HostId.NONE); |
| 392 | return Sets.difference(sinksToBeProcessed, singleHomedSinksOfSet2); |
| 393 | }; |
| 394 | |
| 395 | |
| 396 | private void removeSinks(McastEvent event) { |
| 397 | mcastLock(); |
| 398 | try { |
| 399 | Set<ConnectPoint> sinksToBeRemoved = getSinksToBeRemoved(event.prevSubject().sinks(), |
| 400 | event.subject().sinks()); |
| 401 | sinksToBeRemoved.forEach(sink -> removeSink(event.subject().route().group(), sink)); |
| 402 | } finally { |
| 403 | mcastUnlock(); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | private void removeSink(IpAddress group, ConnectPoint sink) { |
| 408 | if (!isLocalLeader(sink.deviceId())) { |
| 409 | log.debug("Not the leader of {}. Skip sink_removed event for the sink {} and group {}", |
| 410 | sink.deviceId(), sink, group); |
| 411 | return; |
| 412 | } |
| 413 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 414 | Optional<SubscriberAndDeviceInformation> oltInfo = getSubscriberAndDeviceInformation(sink.deviceId()); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 415 | |
| 416 | if (!oltInfo.isPresent()) { |
| 417 | log.warn("Unknown OLT device : {}", sink.deviceId()); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | log.debug("Removing sink {} from the group {}", sink, group); |
| 422 | |
| 423 | NextKey key = new NextKey(sink.deviceId(), group); |
| 424 | groups.computeIfPresent(key, (k, v) -> { |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 425 | |
| 426 | Set<PortNumber> outPorts = Sets.newHashSet(v.getOutPorts()); |
| 427 | outPorts.remove(sink.port()); |
| 428 | |
| 429 | if (outPorts.isEmpty()) { |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 430 | log.debug("No more output ports for group {}, removing next and fwd objectives", group); |
| 431 | |
| 432 | //On Success of removing the fwd objective we remove also the group. |
| 433 | Consumer<Objective> onSuccess = (objective) -> { |
| 434 | log.debug("Successfully removed fwd objective for {} on {}, " + |
| 435 | "removing next objective {}", group, sink, v.getNextId()); |
| 436 | eventExecutor.execute(() -> { |
| 437 | //No port is needed since it's a remove Operation |
| 438 | flowObjectiveService.next(sink.deviceId(), nextObject(v.getNextId(), |
| 439 | null, |
| 440 | NextType.Remove, group)); |
| 441 | }); |
| 442 | }; |
| 443 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 444 | // this is the last sink |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 445 | ObjectiveContext context = new DefaultObjectiveContext(onSuccess, |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 446 | (objective, error) -> log.warn("Failed to remove {} on {}: {}", |
| 447 | group, sink, error)); |
| 448 | ForwardingObjective fwdObj = fwdObject(v.getNextId(), group).remove(context); |
| 449 | flowObjectiveService.forward(sink.deviceId(), fwdObj); |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 450 | } else { |
| 451 | log.debug("Group {} has remaining {} ports, removing just {} " + |
| 452 | "from it's sinks", group, outPorts, sink.port()); |
| 453 | flowObjectiveService.next(sink.deviceId(), nextObject(v.getNextId(), sink.port(), |
| 454 | NextType.RemoveFromExisting, group)); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 455 | } |
| 456 | // remove the whole entity if no out port exists in the port list |
| 457 | return outPorts.isEmpty() ? null : new NextContent(v.getNextId(), |
| 458 | ImmutableSet.copyOf(outPorts)); |
| 459 | }); |
| 460 | } |
| 461 | |
| 462 | private void addSinks(McastEvent event) { |
| 463 | mcastLock(); |
| 464 | try { |
| 465 | Set<ConnectPoint> sinksToBeAdded = getSinksToBeAdded(event.subject().sinks(), |
| 466 | event.prevSubject().sinks()); |
| 467 | sinksToBeAdded.forEach(sink -> addSink(event.subject().route(), sink)); |
| 468 | } finally { |
| 469 | mcastUnlock(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | private void addSink(McastRoute route, ConnectPoint sink) { |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 474 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 475 | if (!isLocalLeader(sink.deviceId())) { |
| 476 | log.debug("Not the leader of {}. Skip sink_added event for the sink {} and group {}", |
| 477 | sink.deviceId(), sink, route.group()); |
| 478 | return; |
| 479 | } |
| 480 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 481 | Optional<SubscriberAndDeviceInformation> oltInfo = getSubscriberAndDeviceInformation(sink.deviceId()); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 482 | |
| 483 | if (!oltInfo.isPresent()) { |
| 484 | log.warn("Unknown OLT device : {}", sink.deviceId()); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | log.debug("Adding sink {} to the group {}", sink, route.group()); |
| 489 | |
| 490 | NextKey key = new NextKey(sink.deviceId(), route.group()); |
| 491 | NextObjective newNextObj; |
| 492 | |
| 493 | boolean theFirstSinkOfGroup = false; |
| 494 | if (!groups.containsKey(key)) { |
| 495 | // First time someone request this mcast group via this device |
| 496 | Integer nextId = flowObjectiveService.allocateNextId(); |
| 497 | newNextObj = nextObject(nextId, sink.port(), NextType.AddNew, route.group()); |
| 498 | // Store the new port |
| 499 | groups.put(key, new NextContent(nextId, ImmutableSet.of(sink.port()))); |
| 500 | theFirstSinkOfGroup = true; |
| 501 | } else { |
| 502 | // This device already serves some subscribers of this mcast group |
| 503 | Versioned<NextContent> nextObj = groups.get(key); |
| 504 | if (nextObj.value().getOutPorts().contains(sink.port())) { |
| 505 | log.info("Group {} already serves the sink connected to {}", route.group(), sink); |
| 506 | return; |
| 507 | } |
| 508 | newNextObj = nextObject(nextObj.value().getNextId(), sink.port(), |
| 509 | NextType.AddToExisting, route.group()); |
| 510 | // add new port to the group |
| 511 | Set<PortNumber> outPorts = Sets.newHashSet(nextObj.value().getOutPorts()); |
| 512 | outPorts.add(sink.port()); |
| 513 | groups.put(key, new NextContent(newNextObj.id(), ImmutableSet.copyOf(outPorts))); |
| 514 | } |
| 515 | |
| 516 | ObjectiveContext context = new DefaultObjectiveContext( |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 517 | (objective) -> log.debug("Successfully add {} on {}/{}, vlan {}, inner vlan {}", |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 518 | route.group(), sink.deviceId(), sink.port().toLong(), |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 519 | assignedVlan(), assignedInnerVlan()), |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 520 | (objective, error) -> { |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 521 | log.warn("Failed to add {} on {}/{}, vlan {}, inner vlan {}: {}", |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 522 | route.group(), sink.deviceId(), sink.port().toLong(), assignedVlan(), |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 523 | assignedInnerVlan(), error); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 524 | }); |
| 525 | |
| 526 | flowObjectiveService.next(sink.deviceId(), newNextObj); |
| 527 | |
| 528 | if (theFirstSinkOfGroup) { |
| 529 | // create the necessary flow rule if this is the first sink request for the group |
| 530 | // on this device |
| 531 | flowObjectiveService.forward(sink.deviceId(), fwdObject(newNextObj.id(), |
| 532 | route.group()).add(context)); |
| 533 | } |
| 534 | } |
| 535 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 536 | /** |
| 537 | * Fetches device information associated with the device serial number from SADIS. |
| 538 | * |
| 539 | * @param serialNumber serial number of a device |
| 540 | * @return device information; an empty Optional otherwise. |
| 541 | */ |
| 542 | private Optional<SubscriberAndDeviceInformation> getSubscriberAndDeviceInformation(String serialNumber) { |
| 543 | long start = System.currentTimeMillis(); |
| 544 | try { |
| 545 | return Optional.ofNullable(sadisService.getSubscriberInfoService().get(serialNumber)); |
| 546 | } finally { |
| 547 | if (log.isDebugEnabled()) { |
| 548 | // SADIS may call remote systems to fetch device data and this calls can take a long time. |
| 549 | // This measurement is just for monitoring these kinds of situations. |
| 550 | log.debug("Device fetched from SADIS. Elapsed {} msec", System.currentTimeMillis() - start); |
| 551 | } |
| 552 | |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Fetches device information associated with the device serial number from SADIS. |
| 558 | * |
| 559 | * @param deviceId device id |
| 560 | * @return device information; an empty Optional otherwise. |
| 561 | */ |
| 562 | private Optional<SubscriberAndDeviceInformation> getSubscriberAndDeviceInformation(DeviceId deviceId) { |
| 563 | Device device = deviceService.getDevice(deviceId); |
| 564 | if (device == null || device.serialNumber() == null) { |
| 565 | return Optional.empty(); |
| 566 | } |
| 567 | return getSubscriberAndDeviceInformation(device.serialNumber()); |
| 568 | } |
| 569 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 570 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 571 | @Override |
| 572 | public void event(NetworkConfigEvent event) { |
| 573 | eventExecutor.execute(() -> { |
| 574 | switch (event.type()) { |
| 575 | |
| 576 | case CONFIG_ADDED: |
| 577 | case CONFIG_UPDATED: |
| 578 | if (event.configClass().equals(CORD_MCAST_CONFIG_CLASS)) { |
| 579 | McastConfig config = networkConfig.getConfig(coreAppId, CORD_MCAST_CONFIG_CLASS); |
| 580 | if (config != null) { |
| 581 | //TODO: Simply remove flows/groups, hosts will response period query |
| 582 | // and re-sent IGMP report, so the flows can be rebuild. |
| 583 | // However, better to remove and re-add mcast flow rules here |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 584 | if (vlanEnabled && (mcastVlan != config.egressVlan().toShort() || |
| 585 | !mcastInnerVlan.equals(config.egressInnerVlan()))) { |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 586 | clearGroups(); |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 587 | groups.clear(); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 588 | } |
| 589 | updateConfig(config); |
| 590 | } |
| 591 | } |
| 592 | break; |
| 593 | case CONFIG_REGISTERED: |
| 594 | case CONFIG_UNREGISTERED: |
| 595 | case CONFIG_REMOVED: |
| 596 | break; |
| 597 | default: |
| 598 | break; |
| 599 | } |
| 600 | }); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | private void updateConfig(McastConfig config) { |
| 605 | if (config == null) { |
| 606 | return; |
| 607 | } |
| 608 | log.debug("multicast config received: {}", config); |
| 609 | |
| 610 | if (config.egressVlan() != null) { |
| 611 | mcastVlan = config.egressVlan().toShort(); |
| 612 | } |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 613 | if (config.egressInnerVlan() != null) { |
| 614 | mcastInnerVlan = config.egressInnerVlan(); |
| 615 | } |
Esin Karaman | e489001 | 2020-04-19 11:58:54 +0000 | [diff] [blame] | 616 | feedStatsServiceWithVlanConfigValues(); |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | private class NextKey { |
| 620 | private DeviceId device; |
| 621 | private IpAddress group; |
| 622 | |
| 623 | public NextKey(DeviceId deviceId, IpAddress groupAddress) { |
| 624 | device = deviceId; |
| 625 | group = groupAddress; |
| 626 | } |
| 627 | |
| 628 | public DeviceId getDevice() { |
| 629 | return device; |
| 630 | } |
| 631 | |
| 632 | public int hashCode() { |
| 633 | return Objects.hash(this.device, this.group); |
| 634 | } |
| 635 | |
| 636 | public boolean equals(Object obj) { |
| 637 | if (this == obj) { |
| 638 | return true; |
| 639 | } else if (!(obj instanceof NextKey)) { |
| 640 | return false; |
| 641 | } else { |
| 642 | NextKey that = (NextKey) obj; |
| 643 | return this.getClass() == that.getClass() && |
| 644 | Objects.equals(this.device, that.device) && |
| 645 | Objects.equals(this.group, that.group); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private class NextContent { |
| 651 | private Integer nextId; |
| 652 | private Set<PortNumber> outPorts; |
| 653 | |
| 654 | public NextContent(Integer nextId, Set<PortNumber> outPorts) { |
| 655 | this.nextId = nextId; |
| 656 | this.outPorts = outPorts; |
| 657 | } |
| 658 | |
| 659 | public Integer getNextId() { |
| 660 | return nextId; |
| 661 | } |
| 662 | |
| 663 | public Set<PortNumber> getOutPorts() { |
| 664 | return ImmutableSet.copyOf(outPorts); |
| 665 | } |
| 666 | |
| 667 | public int hashCode() { |
| 668 | return Objects.hash(this.nextId, this.outPorts); |
| 669 | } |
| 670 | |
| 671 | public boolean equals(Object obj) { |
| 672 | if (this == obj) { |
| 673 | return true; |
| 674 | } else if (!(obj instanceof NextContent)) { |
| 675 | return false; |
| 676 | } else { |
| 677 | NextContent that = (NextContent) obj; |
| 678 | return this.getClass() == that.getClass() && |
| 679 | Objects.equals(this.nextId, that.nextId) && |
| 680 | Objects.equals(this.outPorts, that.outPorts); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 685 | private enum NextType { AddNew, AddToExisting, Remove, RemoveFromExisting }; |
| 686 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 687 | private NextObjective nextObject(Integer nextId, PortNumber port, |
| 688 | NextType nextType, IpAddress mcastIp) { |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 689 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 690 | // Build the meta selector with the fwd objective info |
| 691 | TrafficSelector.Builder metadata = DefaultTrafficSelector.builder() |
| 692 | .matchIPDst(mcastIp.toIpPrefix()); |
| 693 | |
| 694 | if (vlanEnabled) { |
| 695 | metadata.matchVlanId(multicastVlan()); |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 696 | |
| 697 | if (!mcastInnerVlan.equals(VlanId.NONE)) { |
| 698 | metadata.matchInnerVlanId(mcastInnerVlan); |
| 699 | } |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 702 | DefaultNextObjective.Builder builder = DefaultNextObjective.builder() |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 703 | .fromApp(appId) |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 704 | .withType(NextObjective.Type.BROADCAST) |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 705 | .withId(nextId) |
| 706 | .withMeta(metadata.build()); |
| 707 | |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 708 | if (port == null && !nextType.equals(NextType.Remove)) { |
| 709 | log.error("Port can't be null with operation {}", nextType); |
| 710 | return null; |
| 711 | } else if (port != null && !nextType.equals(NextType.Remove)) { |
| 712 | builder.addTreatment(DefaultTrafficTreatment.builder().setOutput(port).build()); |
| 713 | } |
| 714 | |
| 715 | ObjectiveContext context = new ObjectiveContext() { |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 716 | @Override |
| 717 | public void onSuccess(Objective objective) { |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 718 | log.debug("Success for operation {} on Next Objective {}", objective.id(), nextType); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | @Override |
| 722 | public void onError(Objective objective, ObjectiveError error) { |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 723 | log.debug("Next Objective {} failed, because {}", |
| 724 | objective.id(), |
| 725 | error); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 726 | } |
| 727 | }; |
| 728 | |
| 729 | switch (nextType) { |
| 730 | case AddNew: |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 731 | return builder.add(context); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 732 | case AddToExisting: |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 733 | return builder.addToExisting(context); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 734 | case Remove: |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 735 | return builder.remove(context); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 736 | case RemoveFromExisting: |
Andrea Campanella | 0365235 | 2020-05-06 16:12:00 +0200 | [diff] [blame] | 737 | return builder.removeFromExisting(context); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 738 | default: |
| 739 | return null; |
| 740 | } |
| 741 | } |
| 742 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 743 | private ForwardingObjective.Builder fwdObject(int nextId, IpAddress mcastIp) { |
| 744 | TrafficSelector.Builder mcast = DefaultTrafficSelector.builder() |
| 745 | .matchEthType(Ethernet.TYPE_IPV4) |
| 746 | .matchIPDst(mcastIp.toIpPrefix()); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 747 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 748 | //build the meta selector |
| 749 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 750 | if (vlanEnabled) { |
| 751 | metabuilder.matchVlanId(multicastVlan()); |
Esin Karaman | bb35a3b | 2020-03-18 13:53:24 +0000 | [diff] [blame] | 752 | |
| 753 | if (!mcastInnerVlan.equals(VlanId.NONE)) { |
| 754 | metabuilder.matchInnerVlanId(mcastInnerVlan); |
| 755 | } |
Jonathan Hart | 718c045 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 758 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 759 | .fromApp(appId) |
| 760 | .nextStep(nextId) |
| 761 | .makePermanent() |
| 762 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 763 | .withPriority(priority) |
| 764 | .withSelector(mcast.build()) |
| 765 | .withMeta(metabuilder.build()); |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 766 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 767 | return fwdBuilder; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 770 | // Custom-built function, when the device is not available we need a fallback mechanism |
| 771 | private boolean isLocalLeader(DeviceId deviceId) { |
| 772 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 773 | // When the device is available we just check the mastership |
| 774 | if (deviceService.isAvailable(deviceId)) { |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 775 | return false; |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 776 | } |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 777 | // Fallback with Leadership service - device id is used as topic |
| 778 | NodeId leader = leadershipService.runForLeadership( |
| 779 | deviceId.toString()).leaderNodeId(); |
| 780 | // Verify if this node is the leader |
| 781 | return clusterService.getLocalNode().id().equals(leader); |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 782 | } |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 783 | return true; |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 784 | } |
Esin Karaman | 39b2485 | 2019-08-28 13:57:30 +0000 | [diff] [blame] | 785 | |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 786 | private class InternalDeviceListener implements DeviceListener { |
| 787 | |
| 788 | @Override |
| 789 | public void event(DeviceEvent event) { |
| 790 | eventExecutor.execute(() -> { |
| 791 | DeviceId devId = event.subject().id(); |
Andrea Campanella | 86bee26 | 2020-05-18 20:15:01 +0200 | [diff] [blame] | 792 | if (!deviceService.isAvailable(devId) && |
| 793 | isLocalLeader(event.subject().id())) { |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 794 | if (deviceService.getPorts(devId).isEmpty()) { |
| 795 | log.info("Handling controlled device disconnection .. " |
| 796 | + "flushing all state for dev:{}", devId); |
| 797 | groupService.purgeGroupEntries(devId); |
| 798 | groups.keySet().iterator().forEachRemaining(groupInfo -> { |
| 799 | if (groupInfo.device.equals(devId)) { |
| 800 | log.debug("Removing next key {} from distributed mcast map", groupInfo.group); |
| 801 | groups.remove(groupInfo); |
| 802 | } |
| 803 | }); |
| 804 | } else { |
| 805 | log.info("Disconnected device has available ports .. " |
| 806 | + "assuming temporary disconnection, " |
| 807 | + "retaining state for device {}", devId); |
| 808 | } |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 809 | } |
| 810 | }); |
| 811 | |
| 812 | } |
| 813 | |
| 814 | @Override |
| 815 | public boolean isRelevant(DeviceEvent event) { |
Andrea Campanella | 86bee26 | 2020-05-18 20:15:01 +0200 | [diff] [blame] | 816 | return event.type().equals(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED); |
Andrea Campanella | c2f3ddd | 2020-05-18 11:09:11 +0200 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 820 | } |
ke han | f1709e8 | 2016-08-12 10:48:17 +0800 | [diff] [blame] | 821 | |
ke han | 9590c81 | 2017-02-28 15:02:26 +0800 | [diff] [blame] | 822 | |