Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 17 | |
| 18 | |
Ilayda Ozdemir | 2fccbce | 2021-02-23 15:36:47 +0000 | [diff] [blame^] | 19 | import com.google.common.collect.Lists; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onlab.util.SafeRecurringTask; |
| 22 | import org.onosproject.cfg.ComponentConfigService; |
Ilayda Ozdemir | 0414dde | 2020-06-27 23:18:07 +0000 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.LeadershipService; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 25 | import org.onosproject.event.AbstractListenerManager; |
| 26 | import org.onosproject.mcast.api.McastRoute; |
| 27 | import org.onosproject.mcast.api.MulticastRouteService; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 28 | import org.opencord.cordmcast.CordMcastStatistics; |
| 29 | import org.opencord.cordmcast.CordMcastStatisticsEvent; |
| 30 | import org.opencord.cordmcast.CordMcastStatisticsEventListener; |
| 31 | import org.opencord.cordmcast.CordMcastStatisticsService; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 32 | import org.osgi.service.component.ComponentContext; |
| 33 | import org.osgi.service.component.annotations.Modified; |
| 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Deactivate; |
| 36 | import org.osgi.service.component.annotations.Component; |
| 37 | import org.osgi.service.component.annotations.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ilayda Ozdemir | 2fccbce | 2021-02-23 15:36:47 +0000 | [diff] [blame^] | 39 | import org.osgi.service.component.annotations.ReferencePolicy; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.Dictionary; |
| 43 | import java.util.Set; |
| 44 | import java.util.List; |
| 45 | import java.util.ArrayList; |
| 46 | import java.util.Properties; |
Ilayda Ozdemir | 0414dde | 2020-06-27 23:18:07 +0000 | [diff] [blame] | 47 | import java.util.Objects; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 48 | |
| 49 | import java.util.concurrent.Executors; |
| 50 | import java.util.concurrent.ScheduledExecutorService; |
| 51 | import java.util.concurrent.ScheduledFuture; |
| 52 | import java.util.concurrent.TimeUnit; |
| 53 | |
| 54 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 55 | import static org.onlab.util.Tools.get; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 56 | import static org.opencord.cordmcast.impl.OsgiPropertyConstants.EVENT_GENERATION_PERIOD; |
| 57 | import static org.opencord.cordmcast.impl.OsgiPropertyConstants.EVENT_GENERATION_PERIOD_DEFAULT; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 58 | |
| 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
| 61 | /** |
| 62 | * For managing CordMcastStatisticsEvent. |
| 63 | */ |
| 64 | @Component(immediate = true, property = { EVENT_GENERATION_PERIOD + ":Integer=" + EVENT_GENERATION_PERIOD_DEFAULT, }) |
| 65 | public class CordMcastStatisticsManager |
| 66 | extends AbstractListenerManager<CordMcastStatisticsEvent, CordMcastStatisticsEventListener> |
| 67 | implements CordMcastStatisticsService { |
Ilayda Ozdemir | 2fccbce | 2021-02-23 15:36:47 +0000 | [diff] [blame^] | 68 | private static final String MCAST_NOT_RUNNING = "Multicast is not running."; |
Ilayda Ozdemir | 0414dde | 2020-06-27 23:18:07 +0000 | [diff] [blame] | 69 | private static final String CORD_MCAST_STATISTICS_LEADERSHIP = "cord-mcast-statistics-leadership"; |
| 70 | |
Ilayda Ozdemir | 2fccbce | 2021-02-23 15:36:47 +0000 | [diff] [blame^] | 71 | @Reference(cardinality = ReferenceCardinality.OPTIONAL, |
| 72 | bind = "bindMcastRouteService", |
| 73 | unbind = "unbindMcastRouteService", |
| 74 | policy = ReferencePolicy.DYNAMIC) |
| 75 | protected volatile MulticastRouteService mcastService; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 78 | protected ComponentConfigService componentConfigService; |
| 79 | |
Ilayda Ozdemir | 0414dde | 2020-06-27 23:18:07 +0000 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 81 | protected ClusterService clusterService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 84 | protected LeadershipService leadershipService; |
| 85 | |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Multicast Statistics generation time interval. |
| 89 | **/ |
| 90 | private int eventGenerationPeriodInSeconds = EVENT_GENERATION_PERIOD_DEFAULT; |
| 91 | private final Logger log = getLogger(getClass()); |
| 92 | private ScheduledFuture<?> scheduledFuture = null; |
| 93 | private ScheduledExecutorService executor; |
| 94 | |
| 95 | private VlanId vlanId; |
Esin Karaman | e489001 | 2020-04-19 11:58:54 +0000 | [diff] [blame] | 96 | private VlanId innerVlanId; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 97 | |
| 98 | @Activate |
| 99 | public void activate(ComponentContext context) { |
Ilayda Ozdemir | 0414dde | 2020-06-27 23:18:07 +0000 | [diff] [blame] | 100 | leadershipService.runForLeadership(CORD_MCAST_STATISTICS_LEADERSHIP); |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 101 | eventDispatcher.addSink(CordMcastStatisticsEvent.class, listenerRegistry); |
| 102 | executor = Executors.newScheduledThreadPool(1); |
| 103 | componentConfigService.registerProperties(getClass()); |
| 104 | modified(context); |
| 105 | log.info("CordMcastStatisticsManager activated."); |
| 106 | } |
| 107 | |
| 108 | @Modified |
| 109 | public void modified(ComponentContext context) { |
| 110 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 111 | try { |
| 112 | String s = get(properties, EVENT_GENERATION_PERIOD); |
| 113 | eventGenerationPeriodInSeconds = |
| 114 | isNullOrEmpty(s) ? EVENT_GENERATION_PERIOD_DEFAULT : Integer.parseInt(s.trim()); |
| 115 | } catch (NumberFormatException ne) { |
| 116 | log.error("Unable to parse configuration parameter for eventGenerationPeriodInSeconds", ne); |
| 117 | eventGenerationPeriodInSeconds = EVENT_GENERATION_PERIOD_DEFAULT; |
| 118 | } |
| 119 | if (scheduledFuture != null) { |
| 120 | scheduledFuture.cancel(true); |
| 121 | } |
| 122 | scheduledFuture = executor.scheduleAtFixedRate(SafeRecurringTask.wrap(this::publishEvent), |
| 123 | 0, eventGenerationPeriodInSeconds, TimeUnit.SECONDS); |
| 124 | } |
| 125 | |
| 126 | @Deactivate |
| 127 | public void deactivate() { |
| 128 | eventDispatcher.removeSink(CordMcastStatisticsEvent.class); |
| 129 | scheduledFuture.cancel(true); |
| 130 | executor.shutdown(); |
Ilayda Ozdemir | 0414dde | 2020-06-27 23:18:07 +0000 | [diff] [blame] | 131 | leadershipService.withdraw(CORD_MCAST_STATISTICS_LEADERSHIP); |
| 132 | log.info("CordMcastStatisticsManager deactivated."); |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Ilayda Ozdemir | 2fccbce | 2021-02-23 15:36:47 +0000 | [diff] [blame^] | 135 | protected void bindMcastRouteService(MulticastRouteService service) { |
| 136 | mcastService = service; |
| 137 | log.info("Multicast route service binds to onos."); |
| 138 | } |
| 139 | |
| 140 | protected void unbindMcastRouteService(MulticastRouteService service) { |
| 141 | mcastService = null; |
| 142 | log.info("Multicast route service unbinds from onos."); |
| 143 | } |
| 144 | |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 145 | public List<CordMcastStatistics> getMcastDetails() { |
Ilayda Ozdemir | 2fccbce | 2021-02-23 15:36:47 +0000 | [diff] [blame^] | 146 | if (mcastService == null) { |
| 147 | log.warn(MCAST_NOT_RUNNING); |
| 148 | return Lists.newArrayList(); |
| 149 | } |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 150 | List<CordMcastStatistics> mcastData = new ArrayList<CordMcastStatistics>(); |
| 151 | Set<McastRoute> routes = mcastService.getRoutes(); |
| 152 | routes.forEach(route -> { |
| 153 | mcastData.add(new CordMcastStatistics(route.group(), |
| 154 | route.source().isEmpty() ? "*" : route.source().get().toString(), |
Esin Karaman | e489001 | 2020-04-19 11:58:54 +0000 | [diff] [blame] | 155 | vlanId, innerVlanId)); |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 156 | }); |
| 157 | return mcastData; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void setVlanValue(VlanId vlanValue) { |
| 162 | vlanId = vlanValue; |
| 163 | } |
| 164 | |
Esin Karaman | e489001 | 2020-04-19 11:58:54 +0000 | [diff] [blame] | 165 | @Override |
| 166 | public void setInnerVlanValue(VlanId innerVlanValue) { |
| 167 | innerVlanId = innerVlanValue; |
| 168 | } |
| 169 | |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 170 | /** |
| 171 | * pushing mcast stat data as event. |
| 172 | */ |
| 173 | protected void publishEvent() { |
Ilayda Ozdemir | 0414dde | 2020-06-27 23:18:07 +0000 | [diff] [blame] | 174 | // Only publish events if we are the cluster leader for Igmp-stats |
| 175 | if (!Objects.equals(leadershipService.getLeader(CORD_MCAST_STATISTICS_LEADERSHIP), |
| 176 | clusterService.getLocalNode().id())) { |
| 177 | log.debug("This is not leader of : {}", CORD_MCAST_STATISTICS_LEADERSHIP); |
| 178 | return; |
| 179 | } |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 180 | log.debug("pushing cord mcast event to kafka"); |
| 181 | List<CordMcastStatistics> routeList = getMcastDetails(); |
| 182 | routeList.forEach(mcastStats -> { |
| 183 | log.debug("Group: " + |
| 184 | (mcastStats.getGroupAddress() != null ? mcastStats.getGroupAddress().toString() : "null") + |
| 185 | " | Source: " + |
| 186 | (mcastStats.getSourceAddress() != null ? mcastStats.getSourceAddress().toString() : "null") + |
| 187 | " | Vlan: " + |
Esin Karaman | e489001 | 2020-04-19 11:58:54 +0000 | [diff] [blame] | 188 | (mcastStats.getVlanId() != null ? mcastStats.getVlanId().toString() : "null") + |
| 189 | " | InnerVlan: " + |
| 190 | (mcastStats.getInnerVlanId() != null ? mcastStats.getInnerVlanId().toString() : "null")); |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 191 | }); |
| 192 | post(new CordMcastStatisticsEvent(CordMcastStatisticsEvent.Type.STATUS_UPDATE, routeList)); |
| 193 | } |
| 194 | } |