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