blob: 4a16c8aad4e3c8e4775141da2e79332fb45487b5 [file] [log] [blame]
Shubham Sharma751e82e2020-03-02 06:05:41 +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 */
16
17package org.opencord.kafka.integrations;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.osgi.service.component.annotations.Activate;
23import org.osgi.service.component.annotations.Component;
24import org.osgi.service.component.annotations.Deactivate;
25import org.osgi.service.component.annotations.Reference;
26import org.osgi.service.component.annotations.ReferenceCardinality;
27import org.osgi.service.component.annotations.ReferencePolicy;
28import org.opencord.kafka.EventBusService;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.concurrent.atomic.AtomicReference;
33
34import org.opencord.igmpproxy.IgmpStatisticsEvent;
35import org.opencord.igmpproxy.IgmpStatisticsEventListener;
36import org.opencord.igmpproxy.IgmpStatisticsService;
37
38/**
39 * Listens for IGMP events and pushes them on a Kafka bus.
40 */
41
42@Component(immediate = true)
43public 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
78 protected void bindIgmpStatService(IgmpStatisticsService incomingService) {
79 bindAndAddListener(incomingService, igmpStatServiceRef, igmpStatisticsEventListener);
80 }
81
82 protected void unbindIgmpStatService(IgmpStatisticsService outgoingService) {
83 unbindAndRemoveListener(outgoingService, igmpStatServiceRef, igmpStatisticsEventListener);
84 }
85
86 @Activate
87 public void activate() {
88 log.info("Started IgmpKafkaIntegration");
89 }
90
91 @Deactivate
92 public void deactivate() {
93 log.info("Stopped IgmpKafkaIntegration");
94 }
95
96 private void handleStat(IgmpStatisticsEvent event) {
97 eventBusService.send(IGMP_STATISTICS_TOPIC, serializeStat(event));
98 if (log.isTraceEnabled()) {
99 log.trace("IGMPStatisticsEvent sent successfully");
100 }
101 }
102
103 private JsonNode serializeStat(IgmpStatisticsEvent event) {
104 ObjectMapper mapper = new ObjectMapper();
105 ObjectNode igmpStatEvent = mapper.createObjectNode();
106 igmpStatEvent.put(IGMP_JOIN_REQ, event.subject().getIgmpJoinReq());
107 igmpStatEvent.put(IGMP_SUCCESS_JOIN_REJOIN_REQ, event.subject().getIgmpSuccessJoinRejoinReq());
108 igmpStatEvent.put(IGMP_FAIL_JOIN_REQ, event.subject().getIgmpFailJoinReq());
109 igmpStatEvent.put(IGMP_LEAVE_REQ, event.subject().getIgmpLeaveReq());
110 igmpStatEvent.put(IGMP_DISCONNECT, event.subject().getIgmpDisconnect());
111 igmpStatEvent.put(IGMP_V3_MEMBERSHIP_QUERY, event.subject().getIgmpv3MembershipQuery());
112 igmpStatEvent.put(IGMP_V1_MEMBERSHIP_REPORT, event.subject().getIgmpv1MemershipReport());
113 igmpStatEvent.put(IGMP_V2_MEMBERSHIP_REPORT, event.subject().getIgmpv2MembershipReport());
114 igmpStatEvent.put(IGMP_V3_MEMBERSHIP_REPORT, event.subject().getIgmpv3MembershipReport());
115 igmpStatEvent.put(IGMP_V2_LEAVE_GROUP, event.subject().getIgmpv2LeaveGroup());
116 igmpStatEvent.put(TOTAL_MSG_RECEIVED, event.subject().getTotalMsgReceived());
117 igmpStatEvent.put(IGMP_MSG_RECEIVED, event.subject().getIgmpMsgReceived());
118 igmpStatEvent.put(INVALID_IGMP_MSG_RECEIVED, event.subject().getInvalidIgmpMsgReceived());
119 return igmpStatEvent;
120 }
121
122 public class InternalIgmpStatisticsListener implements
123 IgmpStatisticsEventListener {
124
125 @Override
126 public void event(IgmpStatisticsEvent igmpStatisticsEvent) {
127 handleStat(igmpStatisticsEvent);
128 }
129 }
130}