Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +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 | */ |
| 16 | |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame^] | 17 | package org.opencord.igmpproxy.impl; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 18 | |
| 19 | import org.osgi.service.component.annotations.Component; |
| 20 | import org.osgi.service.component.ComponentContext; |
| 21 | import org.osgi.service.component.annotations.Activate; |
| 22 | import org.onlab.util.SafeRecurringTask; |
| 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
| 25 | import org.onosproject.event.AbstractListenerManager; |
| 26 | import org.osgi.service.component.annotations.Deactivate; |
| 27 | import org.osgi.service.component.annotations.Modified; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 30 | |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame^] | 31 | import org.opencord.igmpproxy.IgmpStatistics; |
| 32 | import org.opencord.igmpproxy.IgmpStatisticsService; |
| 33 | import org.opencord.igmpproxy.IgmpStatisticsEvent; |
| 34 | import org.opencord.igmpproxy.IgmpStatisticsEventListener; |
| 35 | |
| 36 | import static org.opencord.igmpproxy.impl.OsgiPropertyConstants.STATISTICS_GENERATION_PERIOD; |
| 37 | import static org.opencord.igmpproxy.impl.OsgiPropertyConstants.STATISTICS_GENERATION_PERIOD_DEFAULT; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
| 40 | import java.util.Dictionary; |
| 41 | import java.util.concurrent.Executors; |
| 42 | import java.util.concurrent.ScheduledExecutorService; |
| 43 | import java.util.concurrent.ScheduledFuture; |
| 44 | import java.util.concurrent.TimeUnit; |
| 45 | |
| 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import com.google.common.base.Strings; |
| 49 | |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | /** |
| 53 | * |
| 54 | * Process the stats collected in Igmp proxy application. Publish to kafka onos. |
| 55 | * |
| 56 | */ |
| 57 | @Component(immediate = true, property = { |
| 58 | STATISTICS_GENERATION_PERIOD + ":Integer=" + STATISTICS_GENERATION_PERIOD_DEFAULT, |
| 59 | }) |
| 60 | public class IgmpStatisticsManager extends |
| 61 | AbstractListenerManager<IgmpStatisticsEvent, IgmpStatisticsEventListener> |
| 62 | implements IgmpStatisticsService { |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | private IgmpStatistics igmpStats; |
| 65 | |
| 66 | ScheduledExecutorService executorForIgmp; |
| 67 | private ScheduledFuture<?> publisherTask; |
| 68 | |
| 69 | protected int statisticsGenerationPeriodInSeconds = STATISTICS_GENERATION_PERIOD_DEFAULT; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 72 | protected ComponentConfigService cfgService; |
| 73 | |
| 74 | @Override |
| 75 | public IgmpStatistics getIgmpStats() { |
| 76 | return igmpStats; |
| 77 | } |
| 78 | |
| 79 | @Activate |
| 80 | public void activate(ComponentContext context) { |
| 81 | igmpStats = new IgmpStatistics(); |
| 82 | |
| 83 | eventDispatcher.addSink(IgmpStatisticsEvent.class, listenerRegistry); |
| 84 | executorForIgmp = Executors.newScheduledThreadPool(1); |
| 85 | cfgService.registerProperties(getClass()); |
| 86 | modified(context); |
| 87 | log.info("IgmpStatisticsManager Activated"); |
| 88 | } |
| 89 | |
| 90 | @Modified |
| 91 | public void modified(ComponentContext context) { |
| 92 | Dictionary<String, Object> properties = context.getProperties(); |
| 93 | |
| 94 | try { |
| 95 | String s = Tools.get(properties, STATISTICS_GENERATION_PERIOD); |
| 96 | statisticsGenerationPeriodInSeconds = Strings.isNullOrEmpty(s) ? |
| 97 | Integer.parseInt(STATISTICS_GENERATION_PERIOD) |
| 98 | : Integer.parseInt(s.trim()); |
| 99 | } catch (NumberFormatException ne) { |
| 100 | log.error("Unable to parse configuration parameter for eventGenerationPeriodInSeconds", ne); |
| 101 | statisticsGenerationPeriodInSeconds = STATISTICS_GENERATION_PERIOD_DEFAULT; |
| 102 | } |
| 103 | if (publisherTask != null) { |
| 104 | publisherTask.cancel(true); |
| 105 | } |
| 106 | publisherTask = executorForIgmp.scheduleAtFixedRate(SafeRecurringTask.wrap(this::publishStats), |
| 107 | 0, statisticsGenerationPeriodInSeconds, TimeUnit.SECONDS); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate() { |
| 112 | eventDispatcher.removeSink(IgmpStatisticsEvent.class); |
| 113 | publisherTask.cancel(true); |
| 114 | executorForIgmp.shutdown(); |
| 115 | cfgService.unregisterProperties(getClass(), false); |
| 116 | igmpStats = null; |
| 117 | log.info("IgmpStatisticsManager Deactivated"); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Publishes stats. |
| 122 | */ |
| 123 | private void publishStats() { |
| 124 | |
| 125 | if (log.isDebugEnabled()) { |
| 126 | log.debug("Notifying stats: {}", igmpStats); |
| 127 | log.debug("--IgmpDisconnect--" + igmpStats.getIgmpDisconnect()); |
| 128 | log.debug("--IgmpFailJoinReq--" + igmpStats.getIgmpFailJoinReq()); |
| 129 | log.debug("--IgmpJoinReq--" + igmpStats.getIgmpJoinReq()); |
| 130 | log.debug("--IgmpLeaveReq--" + igmpStats.getIgmpLeaveReq()); |
| 131 | log.debug("--IgmpMsgReceived--" + igmpStats.getIgmpMsgReceived()); |
| 132 | log.debug("--IgmpSuccessJoinRejoinReq--" + igmpStats.getIgmpSuccessJoinRejoinReq()); |
| 133 | log.debug("--Igmpv1MemershipReport--" + igmpStats.getIgmpv1MemershipReport()); |
| 134 | log.debug("--Igmpv2LeaveGroup--" + igmpStats.getIgmpv2LeaveGroup()); |
| 135 | log.debug("--Igmpv2MembershipReport--" + igmpStats.getIgmpv2MembershipReport()); |
| 136 | log.debug("--Igmpv3MembershipQuery--" + igmpStats.getIgmpv3MembershipQuery()); |
| 137 | log.debug("--Igmpv3MembershipReport--" + igmpStats.getIgmpv3MembershipReport()); |
| 138 | log.debug("--InvalidIgmpMsgReceived--" + igmpStats.getInvalidIgmpMsgReceived()); |
| 139 | log.debug("--TotalMsgReceived-- " + igmpStats.getTotalMsgReceived()); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 140 | log.debug("--UnknownIgmpTypePacketsRx--" + igmpStats.getUnknownIgmpTypePacketsRxCounter()); |
| 141 | log.debug("--ReportsRxWithWrongMode--" + igmpStats.getReportsRxWithWrongModeCounter()); |
| 142 | log.debug("--FailJoinReqInsuffPermission--" + igmpStats.getFailJoinReqInsuffPermissionAccessCounter()); |
| 143 | log.debug("--FailJoinReqUnknownMulticastIp--" + igmpStats.getFailJoinReqUnknownMulticastIpCounter()); |
| 144 | log.debug("--UnconfiguredGroupCounter--" + igmpStats.getUnconfiguredGroupCounter()); |
| 145 | log.debug("--ValidIgmpPacketCounter--" + igmpStats.getValidIgmpPacketCounter()); |
| 146 | log.debug("--IgmpChannelJoinCounter--" + igmpStats.getIgmpChannelJoinCounter()); |
| 147 | log.debug("--CurrentGrpNumCounter--" + igmpStats.getCurrentGrpNumCounter()); |
| 148 | log.debug("--IgmpValidChecksumCounter--" + igmpStats.getIgmpValidChecksumCounter()); |
| 149 | log.debug("--InvalidIgmpLength--" + igmpStats.getInvalidIgmpLength()); |
| 150 | log.debug("--IgmpGeneralMembershipQuery--" + igmpStats.getIgmpGeneralMembershipQuery()); |
| 151 | log.debug("--IgmpGrpSpecificMembershipQuery--" + igmpStats.getIgmpGrpSpecificMembershipQuery()); |
| 152 | log.debug("--IgmpGrpAndSrcSpecificMembershipQuery--" + igmpStats.getIgmpGrpAndSrcSpecificMembershipQuery()); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | post(new IgmpStatisticsEvent(IgmpStatisticsEvent.Type.STATS_UPDATE, igmpStats)); |
| 156 | } |
| 157 | |
| 158 | } |