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