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