blob: 1044adceb75343426727207a1cb2c0f05388f730 [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;
Esin Karamane4890012020-04-19 11:58:54 +000081 private VlanId innerVlanId;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000082
83 @Activate
84 public void activate(ComponentContext context) {
85 eventDispatcher.addSink(CordMcastStatisticsEvent.class, listenerRegistry);
86 executor = Executors.newScheduledThreadPool(1);
87 componentConfigService.registerProperties(getClass());
88 modified(context);
89 log.info("CordMcastStatisticsManager activated.");
90 }
91
92 @Modified
93 public void modified(ComponentContext context) {
94 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
95 try {
96 String s = get(properties, EVENT_GENERATION_PERIOD);
97 eventGenerationPeriodInSeconds =
98 isNullOrEmpty(s) ? EVENT_GENERATION_PERIOD_DEFAULT : Integer.parseInt(s.trim());
99 } catch (NumberFormatException ne) {
100 log.error("Unable to parse configuration parameter for eventGenerationPeriodInSeconds", ne);
101 eventGenerationPeriodInSeconds = EVENT_GENERATION_PERIOD_DEFAULT;
102 }
103 if (scheduledFuture != null) {
104 scheduledFuture.cancel(true);
105 }
106 scheduledFuture = executor.scheduleAtFixedRate(SafeRecurringTask.wrap(this::publishEvent),
107 0, eventGenerationPeriodInSeconds, TimeUnit.SECONDS);
108 }
109
110 @Deactivate
111 public void deactivate() {
112 eventDispatcher.removeSink(CordMcastStatisticsEvent.class);
113 scheduledFuture.cancel(true);
114 executor.shutdown();
115 }
116
117 public List<CordMcastStatistics> getMcastDetails() {
118 List<CordMcastStatistics> mcastData = new ArrayList<CordMcastStatistics>();
119 Set<McastRoute> routes = mcastService.getRoutes();
120 routes.forEach(route -> {
121 mcastData.add(new CordMcastStatistics(route.group(),
122 route.source().isEmpty() ? "*" : route.source().get().toString(),
Esin Karamane4890012020-04-19 11:58:54 +0000123 vlanId, innerVlanId));
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000124 });
125 return mcastData;
126 }
127
128 @Override
129 public void setVlanValue(VlanId vlanValue) {
130 vlanId = vlanValue;
131 }
132
Esin Karamane4890012020-04-19 11:58:54 +0000133 @Override
134 public void setInnerVlanValue(VlanId innerVlanValue) {
135 innerVlanId = innerVlanValue;
136 }
137
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000138 /**
139 * pushing mcast stat data as event.
140 */
141 protected void publishEvent() {
142 log.debug("pushing cord mcast event to kafka");
143 List<CordMcastStatistics> routeList = getMcastDetails();
144 routeList.forEach(mcastStats -> {
145 log.debug("Group: " +
146 (mcastStats.getGroupAddress() != null ? mcastStats.getGroupAddress().toString() : "null") +
147 " | Source: " +
148 (mcastStats.getSourceAddress() != null ? mcastStats.getSourceAddress().toString() : "null") +
149 " | Vlan: " +
Esin Karamane4890012020-04-19 11:58:54 +0000150 (mcastStats.getVlanId() != null ? mcastStats.getVlanId().toString() : "null") +
151 " | InnerVlan: " +
152 (mcastStats.getInnerVlanId() != null ? mcastStats.getInnerVlanId().toString() : "null"));
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000153 });
154 post(new CordMcastStatisticsEvent(CordMcastStatisticsEvent.Type.STATUS_UPDATE, routeList));
155 }
156}