blob: a5715f48c18794abd9a12b7a02ae73b84e5ff8f0 [file] [log] [blame]
Arjun E Kabf9e6e2020-03-02 10:15:21 +00001/*
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 Moro8ea9e102020-03-24 18:56:52 +010016package org.opencord.cordmcast.impl;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000017
18
19import org.onlab.packet.VlanId;
20import org.onlab.util.SafeRecurringTask;
21import org.onosproject.cfg.ComponentConfigService;
22import org.onosproject.event.AbstractListenerManager;
23import org.onosproject.mcast.api.McastRoute;
24import org.onosproject.mcast.api.MulticastRouteService;
Daniele Moro8ea9e102020-03-24 18:56:52 +010025import org.opencord.cordmcast.CordMcastStatistics;
26import org.opencord.cordmcast.CordMcastStatisticsEvent;
27import org.opencord.cordmcast.CordMcastStatisticsEventListener;
28import org.opencord.cordmcast.CordMcastStatisticsService;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000029import org.osgi.service.component.ComponentContext;
30import org.osgi.service.component.annotations.Modified;
31import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Component;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
36import org.slf4j.Logger;
37
38import java.util.Dictionary;
39import java.util.Set;
40import java.util.List;
41import java.util.ArrayList;
42import java.util.Properties;
43
44import java.util.concurrent.Executors;
45import java.util.concurrent.ScheduledExecutorService;
46import java.util.concurrent.ScheduledFuture;
47import java.util.concurrent.TimeUnit;
48
49import static com.google.common.base.Strings.isNullOrEmpty;
50import static org.onlab.util.Tools.get;
Daniele Moro8ea9e102020-03-24 18:56:52 +010051import static org.opencord.cordmcast.impl.OsgiPropertyConstants.EVENT_GENERATION_PERIOD;
52import static org.opencord.cordmcast.impl.OsgiPropertyConstants.EVENT_GENERATION_PERIOD_DEFAULT;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000053
54import 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, })
60public 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}