Shubham Sharma | 751e82e | 2020-03-02 06:05:41 +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 | |
| 17 | package org.opencord.kafka.integrations; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.osgi.service.component.annotations.Activate; |
| 23 | import org.osgi.service.component.annotations.Component; |
| 24 | import org.osgi.service.component.annotations.Deactivate; |
| 25 | import org.osgi.service.component.annotations.Reference; |
| 26 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 27 | import org.osgi.service.component.annotations.ReferencePolicy; |
| 28 | import org.opencord.kafka.EventBusService; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.concurrent.atomic.AtomicReference; |
| 33 | |
| 34 | import org.opencord.igmpproxy.IgmpStatisticsEvent; |
| 35 | import org.opencord.igmpproxy.IgmpStatisticsEventListener; |
| 36 | import org.opencord.igmpproxy.IgmpStatisticsService; |
| 37 | |
| 38 | /** |
| 39 | * Listens for IGMP events and pushes them on a Kafka bus. |
| 40 | */ |
| 41 | |
| 42 | @Component(immediate = true) |
| 43 | public class IgmpKafkaIntegration extends AbstractKafkaIntegration { |
| 44 | |
| 45 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 48 | protected EventBusService eventBusService; |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.OPTIONAL, |
| 51 | policy = ReferencePolicy.DYNAMIC, |
| 52 | bind = "bindIgmpStatService", |
| 53 | unbind = "unbindIgmpStatService") |
| 54 | protected volatile IgmpStatisticsService ignore; |
| 55 | private final AtomicReference<IgmpStatisticsService> igmpStatServiceRef = new AtomicReference<>(); |
| 56 | |
| 57 | private final IgmpStatisticsEventListener igmpStatisticsEventListener = |
| 58 | new InternalIgmpStatisticsListener(); |
| 59 | |
| 60 | //TOPIC |
| 61 | private static final String IGMP_STATISTICS_TOPIC = "onos.igmp.stats.kpis"; |
| 62 | |
| 63 | // IGMP stats event params |
| 64 | private static final String IGMP_JOIN_REQ = "igmpJoinReq"; |
| 65 | private static final String IGMP_SUCCESS_JOIN_REJOIN_REQ = "igmpSuccessJoinRejoinReq"; |
| 66 | private static final String IGMP_FAIL_JOIN_REQ = "igmpFailJoinReq"; |
| 67 | private static final String IGMP_LEAVE_REQ = "igmpLeaveReq"; |
| 68 | private static final String IGMP_DISCONNECT = "igmpDisconnect"; |
| 69 | private static final String IGMP_V3_MEMBERSHIP_QUERY = "igmpv3MembbershipQuery"; |
| 70 | private static final String IGMP_V1_MEMBERSHIP_REPORT = "igmpv1MembershipReport"; |
| 71 | private static final String IGMP_V2_MEMBERSHIP_REPORT = "igmpv2MembershipReport"; |
| 72 | private static final String IGMP_V3_MEMBERSHIP_REPORT = "igmpv3MembershipReport"; |
| 73 | private static final String IGMP_V2_LEAVE_GROUP = "igmpv2LeaveGroup"; |
| 74 | private static final String TOTAL_MSG_RECEIVED = "totalMsgReceived"; |
| 75 | private static final String IGMP_MSG_RECEIVED = "igmpMsgReceived"; |
| 76 | private static final String INVALID_IGMP_MSG_RECEIVED = "invalidIgmpMsgReceived"; |
| 77 | |
Sonal Kasliwal | 036c794 | 2020-03-18 12:11:20 +0000 | [diff] [blame] | 78 | private static final String UNKNOWN_IGMP_TYPE_PACKETS_RX_COUNTER = |
| 79 | "unknownIgmpTypePacketsRxCounter"; |
| 80 | private static final String REPORTS_RX_WITH_WRONG_MODE_COUNTER = |
| 81 | "reportsRxWithWrongModeCounter"; |
| 82 | private static final String FAIL_JOIN_REQ_INSUFF_PERMISSION_ACCESS_COUNTER = |
| 83 | "failJoinReqInsuffPermissionAccessCounter"; |
| 84 | private static final String FAIL_JOIN_REQ_UNKNOWN_MULTICAST_IP_COUNTER = |
| 85 | "failJoinReqUnknownMulticastIpCounter"; |
| 86 | private static final String UNCONFIGURED_GROUP_COUNTER = |
| 87 | "unconfiguredGroupCounter"; |
| 88 | private static final String VALID_IGMP_PACKET_COUNTER = |
| 89 | "validIgmpPacketCounter"; |
| 90 | private static final String IGMP_CHANNEL_JOIN_COUNTER = |
| 91 | "igmpChannelJoinCounter"; |
| 92 | private static final String CURRENT_GRP_NUM_COUNTER = |
| 93 | "currentGrpNumCounter"; |
| 94 | private static final String IGMP_VALID_CHECKSUM_COUNTER = |
| 95 | "igmpValidChecksumCounter"; |
| 96 | private static final String INVALID_IGMP_LENGTH = |
| 97 | "invalidIgmpLength"; |
| 98 | private static final String IGMP_GENERAL_MEMBERSHIP_QUERY = |
| 99 | "igmpGeneralMembershipQuery"; |
| 100 | private static final String IGMP_GRP_SPECIFIC_MEMBERSHIP_QUERY = |
| 101 | "igmpGrpSpecificMembershipQuery"; |
| 102 | private static final String IGMP_GRP_AND_SRC_SPECIFIC_MEMBERSHIP_QUERY = |
| 103 | "igmpGrpAndSrcSpecificMembershipQuery"; |
| 104 | |
Shubham Sharma | 751e82e | 2020-03-02 06:05:41 +0000 | [diff] [blame] | 105 | protected void bindIgmpStatService(IgmpStatisticsService incomingService) { |
| 106 | bindAndAddListener(incomingService, igmpStatServiceRef, igmpStatisticsEventListener); |
| 107 | } |
| 108 | |
| 109 | protected void unbindIgmpStatService(IgmpStatisticsService outgoingService) { |
| 110 | unbindAndRemoveListener(outgoingService, igmpStatServiceRef, igmpStatisticsEventListener); |
| 111 | } |
| 112 | |
| 113 | @Activate |
| 114 | public void activate() { |
| 115 | log.info("Started IgmpKafkaIntegration"); |
| 116 | } |
| 117 | |
| 118 | @Deactivate |
| 119 | public void deactivate() { |
| 120 | log.info("Stopped IgmpKafkaIntegration"); |
| 121 | } |
| 122 | |
| 123 | private void handleStat(IgmpStatisticsEvent event) { |
| 124 | eventBusService.send(IGMP_STATISTICS_TOPIC, serializeStat(event)); |
| 125 | if (log.isTraceEnabled()) { |
| 126 | log.trace("IGMPStatisticsEvent sent successfully"); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private JsonNode serializeStat(IgmpStatisticsEvent event) { |
| 131 | ObjectMapper mapper = new ObjectMapper(); |
| 132 | ObjectNode igmpStatEvent = mapper.createObjectNode(); |
| 133 | igmpStatEvent.put(IGMP_JOIN_REQ, event.subject().getIgmpJoinReq()); |
| 134 | igmpStatEvent.put(IGMP_SUCCESS_JOIN_REJOIN_REQ, event.subject().getIgmpSuccessJoinRejoinReq()); |
| 135 | igmpStatEvent.put(IGMP_FAIL_JOIN_REQ, event.subject().getIgmpFailJoinReq()); |
| 136 | igmpStatEvent.put(IGMP_LEAVE_REQ, event.subject().getIgmpLeaveReq()); |
| 137 | igmpStatEvent.put(IGMP_DISCONNECT, event.subject().getIgmpDisconnect()); |
| 138 | igmpStatEvent.put(IGMP_V3_MEMBERSHIP_QUERY, event.subject().getIgmpv3MembershipQuery()); |
| 139 | igmpStatEvent.put(IGMP_V1_MEMBERSHIP_REPORT, event.subject().getIgmpv1MemershipReport()); |
| 140 | igmpStatEvent.put(IGMP_V2_MEMBERSHIP_REPORT, event.subject().getIgmpv2MembershipReport()); |
| 141 | igmpStatEvent.put(IGMP_V3_MEMBERSHIP_REPORT, event.subject().getIgmpv3MembershipReport()); |
| 142 | igmpStatEvent.put(IGMP_V2_LEAVE_GROUP, event.subject().getIgmpv2LeaveGroup()); |
| 143 | igmpStatEvent.put(TOTAL_MSG_RECEIVED, event.subject().getTotalMsgReceived()); |
| 144 | igmpStatEvent.put(IGMP_MSG_RECEIVED, event.subject().getIgmpMsgReceived()); |
| 145 | igmpStatEvent.put(INVALID_IGMP_MSG_RECEIVED, event.subject().getInvalidIgmpMsgReceived()); |
Sonal Kasliwal | 036c794 | 2020-03-18 12:11:20 +0000 | [diff] [blame] | 146 | igmpStatEvent.put(UNKNOWN_IGMP_TYPE_PACKETS_RX_COUNTER, event.subject().getUnknownIgmpTypePacketsRxCounter()); |
| 147 | igmpStatEvent.put(REPORTS_RX_WITH_WRONG_MODE_COUNTER, event.subject().getReportsRxWithWrongModeCounter()); |
| 148 | igmpStatEvent.put(FAIL_JOIN_REQ_INSUFF_PERMISSION_ACCESS_COUNTER, |
| 149 | event.subject().getFailJoinReqInsuffPermissionAccessCounter()); |
| 150 | igmpStatEvent.put(FAIL_JOIN_REQ_UNKNOWN_MULTICAST_IP_COUNTER, |
| 151 | event.subject().getFailJoinReqUnknownMulticastIpCounter()); |
| 152 | igmpStatEvent.put(UNCONFIGURED_GROUP_COUNTER, event.subject().getUnconfiguredGroupCounter()); |
| 153 | igmpStatEvent.put(VALID_IGMP_PACKET_COUNTER, event.subject().getValidIgmpPacketCounter()); |
| 154 | igmpStatEvent.put(IGMP_CHANNEL_JOIN_COUNTER, event.subject().getIgmpChannelJoinCounter()); |
| 155 | igmpStatEvent.put(CURRENT_GRP_NUM_COUNTER, event.subject().getCurrentGrpNumCounter()); |
| 156 | igmpStatEvent.put(IGMP_VALID_CHECKSUM_COUNTER, event.subject().getIgmpValidChecksumCounter()); |
| 157 | igmpStatEvent.put(INVALID_IGMP_LENGTH, event.subject().getInvalidIgmpLength()); |
| 158 | igmpStatEvent.put(IGMP_GENERAL_MEMBERSHIP_QUERY, event.subject().getIgmpGeneralMembershipQuery()); |
| 159 | igmpStatEvent.put(IGMP_GRP_SPECIFIC_MEMBERSHIP_QUERY, event.subject().getIgmpGrpSpecificMembershipQuery()); |
| 160 | igmpStatEvent.put(IGMP_GRP_AND_SRC_SPECIFIC_MEMBERSHIP_QUERY, |
| 161 | event.subject().getIgmpGrpAndSrcSpecificMembershipQuery()); |
Shubham Sharma | 751e82e | 2020-03-02 06:05:41 +0000 | [diff] [blame] | 162 | return igmpStatEvent; |
| 163 | } |
| 164 | |
| 165 | public class InternalIgmpStatisticsListener implements |
| 166 | IgmpStatisticsEventListener { |
| 167 | |
| 168 | @Override |
| 169 | public void event(IgmpStatisticsEvent igmpStatisticsEvent) { |
| 170 | handleStat(igmpStatisticsEvent); |
| 171 | } |
| 172 | } |
| 173 | } |