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