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 | |
| 19 | import org.onlab.packet.VlanId; |
| 20 | import org.onlab.util.SafeRecurringTask; |
| 21 | import org.onosproject.cfg.ComponentConfigService; |
| 22 | import org.onosproject.event.AbstractListenerManager; |
| 23 | import org.onosproject.mcast.api.McastRoute; |
| 24 | import org.onosproject.mcast.api.MulticastRouteService; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 25 | import org.opencord.cordmcast.CordMcastStatistics; |
| 26 | import org.opencord.cordmcast.CordMcastStatisticsEvent; |
| 27 | import org.opencord.cordmcast.CordMcastStatisticsEventListener; |
| 28 | import org.opencord.cordmcast.CordMcastStatisticsService; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 29 | import org.osgi.service.component.ComponentContext; |
| 30 | import org.osgi.service.component.annotations.Modified; |
| 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.Dictionary; |
| 39 | import java.util.Set; |
| 40 | import java.util.List; |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.Properties; |
| 43 | |
| 44 | import java.util.concurrent.Executors; |
| 45 | import java.util.concurrent.ScheduledExecutorService; |
| 46 | import java.util.concurrent.ScheduledFuture; |
| 47 | import java.util.concurrent.TimeUnit; |
| 48 | |
| 49 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 50 | import static org.onlab.util.Tools.get; |
Daniele Moro | 8ea9e10 | 2020-03-24 18:56:52 +0100 | [diff] [blame] | 51 | import static org.opencord.cordmcast.impl.OsgiPropertyConstants.EVENT_GENERATION_PERIOD; |
| 52 | import static org.opencord.cordmcast.impl.OsgiPropertyConstants.EVENT_GENERATION_PERIOD_DEFAULT; |
Arjun E K | abf9e6e | 2020-03-02 10:15:21 +0000 | [diff] [blame] | 53 | |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
| 56 | /** |
| 57 | * For managing CordMcastStatisticsEvent. |
| 58 | */ |
| 59 | @Component(immediate = true, property = { EVENT_GENERATION_PERIOD + ":Integer=" + EVENT_GENERATION_PERIOD_DEFAULT, }) |
| 60 | public class CordMcastStatisticsManager |
| 61 | extends AbstractListenerManager<CordMcastStatisticsEvent, CordMcastStatisticsEventListener> |
| 62 | implements CordMcastStatisticsService { |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 65 | protected MulticastRouteService mcastService; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 68 | protected ComponentConfigService componentConfigService; |
| 69 | |
| 70 | CordMcastStatisticsEventListener listener; |
| 71 | |
| 72 | /** |
| 73 | * Multicast Statistics generation time interval. |
| 74 | **/ |
| 75 | private int eventGenerationPeriodInSeconds = EVENT_GENERATION_PERIOD_DEFAULT; |
| 76 | private final Logger log = getLogger(getClass()); |
| 77 | private ScheduledFuture<?> scheduledFuture = null; |
| 78 | private ScheduledExecutorService executor; |
| 79 | |
| 80 | private VlanId vlanId; |
| 81 | |
| 82 | @Activate |
| 83 | public void activate(ComponentContext context) { |
| 84 | eventDispatcher.addSink(CordMcastStatisticsEvent.class, listenerRegistry); |
| 85 | executor = Executors.newScheduledThreadPool(1); |
| 86 | componentConfigService.registerProperties(getClass()); |
| 87 | modified(context); |
| 88 | log.info("CordMcastStatisticsManager activated."); |
| 89 | } |
| 90 | |
| 91 | @Modified |
| 92 | public void modified(ComponentContext context) { |
| 93 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 94 | try { |
| 95 | String s = get(properties, EVENT_GENERATION_PERIOD); |
| 96 | eventGenerationPeriodInSeconds = |
| 97 | isNullOrEmpty(s) ? EVENT_GENERATION_PERIOD_DEFAULT : Integer.parseInt(s.trim()); |
| 98 | } catch (NumberFormatException ne) { |
| 99 | log.error("Unable to parse configuration parameter for eventGenerationPeriodInSeconds", ne); |
| 100 | eventGenerationPeriodInSeconds = EVENT_GENERATION_PERIOD_DEFAULT; |
| 101 | } |
| 102 | if (scheduledFuture != null) { |
| 103 | scheduledFuture.cancel(true); |
| 104 | } |
| 105 | scheduledFuture = executor.scheduleAtFixedRate(SafeRecurringTask.wrap(this::publishEvent), |
| 106 | 0, eventGenerationPeriodInSeconds, TimeUnit.SECONDS); |
| 107 | } |
| 108 | |
| 109 | @Deactivate |
| 110 | public void deactivate() { |
| 111 | eventDispatcher.removeSink(CordMcastStatisticsEvent.class); |
| 112 | scheduledFuture.cancel(true); |
| 113 | executor.shutdown(); |
| 114 | } |
| 115 | |
| 116 | public List<CordMcastStatistics> getMcastDetails() { |
| 117 | List<CordMcastStatistics> mcastData = new ArrayList<CordMcastStatistics>(); |
| 118 | Set<McastRoute> routes = mcastService.getRoutes(); |
| 119 | routes.forEach(route -> { |
| 120 | mcastData.add(new CordMcastStatistics(route.group(), |
| 121 | route.source().isEmpty() ? "*" : route.source().get().toString(), |
| 122 | vlanId)); |
| 123 | }); |
| 124 | return mcastData; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setVlanValue(VlanId vlanValue) { |
| 129 | vlanId = vlanValue; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * pushing mcast stat data as event. |
| 134 | */ |
| 135 | protected void publishEvent() { |
| 136 | log.debug("pushing cord mcast event to kafka"); |
| 137 | List<CordMcastStatistics> routeList = getMcastDetails(); |
| 138 | routeList.forEach(mcastStats -> { |
| 139 | log.debug("Group: " + |
| 140 | (mcastStats.getGroupAddress() != null ? mcastStats.getGroupAddress().toString() : "null") + |
| 141 | " | Source: " + |
| 142 | (mcastStats.getSourceAddress() != null ? mcastStats.getSourceAddress().toString() : "null") + |
| 143 | " | Vlan: " + |
| 144 | (mcastStats.getVlanId() != null ? mcastStats.getVlanId().toString() : "null")); |
| 145 | }); |
| 146 | post(new CordMcastStatisticsEvent(CordMcastStatisticsEvent.Type.STATUS_UPDATE, routeList)); |
| 147 | } |
| 148 | } |