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