blob: a78d25c622309c7b1ac717a396f9e7b435fffe76 [file] [log] [blame]
Jonathan Hart501f7882018-07-24 14:39:57 -07001/*
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;
Shubham Sharma9188dde2019-06-20 07:16:08 +000018import java.time.Instant;
19
Jonathan Hart501f7882018-07-24 14:39:57 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Matteo Scandolod50a4d32019-04-24 12:10:21 -070025import org.apache.felix.scr.annotations.ReferencePolicy;
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070026import org.onosproject.net.AnnotationKeys;
27import org.onosproject.net.device.DeviceService;
Jonathan Hart501f7882018-07-24 14:39:57 -070028import org.opencord.aaa.AuthenticationEvent;
29import org.opencord.aaa.AuthenticationEventListener;
30import org.opencord.aaa.AuthenticationService;
kartikey dubey6e903092019-05-22 13:35:28 +000031import org.opencord.aaa.AuthenticationStatisticsEvent;
32import org.opencord.aaa.AuthenticationStatisticsEventListener;
33import org.opencord.aaa.AuthenticationStatisticsService;
Shubham Sharma9188dde2019-06-20 07:16:08 +000034import org.opencord.kafka.EventBusService;
Jonathan Hart501f7882018-07-24 14:39:57 -070035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Shubham Sharma9188dde2019-06-20 07:16:08 +000038import com.fasterxml.jackson.databind.JsonNode;
39import com.fasterxml.jackson.databind.ObjectMapper;
40import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart2aad7792018-07-31 15:09:17 -040041
Jonathan Hart501f7882018-07-24 14:39:57 -070042/**
43 * Listens for AAA events and pushes them on a Kafka bus.
44 */
45@Component(immediate = true)
46public class AaaKafkaIntegration {
47
48 public Logger log = LoggerFactory.getLogger(getClass());
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected EventBusService eventBusService;
52
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected DeviceService deviceService;
55
Matteo Scandolo03f13c12019-03-20 14:38:12 -070056 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
Matteo Scandolod50a4d32019-04-24 12:10:21 -070057 policy = ReferencePolicy.DYNAMIC,
Matteo Scandolo03f13c12019-03-20 14:38:12 -070058 bind = "bindAuthenticationService",
59 unbind = "unbindAuthenticationService")
Jonathan Hart501f7882018-07-24 14:39:57 -070060 protected AuthenticationService authenticationService;
kartikey dubey6e903092019-05-22 13:35:28 +000061 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
62 policy = ReferencePolicy.DYNAMIC,
63 bind = "bindAuthenticationStatService",
64 unbind = "unbindAuthenticationStatService")
65 protected AuthenticationStatisticsService authenticationStatisticsService;
Jonathan Hart501f7882018-07-24 14:39:57 -070066
67 private final AuthenticationEventListener listener = new InternalAuthenticationListener();
kartikey dubey6e903092019-05-22 13:35:28 +000068 private final AuthenticationStatisticsEventListener authenticationStatisticsEventListener =
Shubham Sharma9188dde2019-06-20 07:16:08 +000069 new InternalAuthenticationStatisticsListner();
Jonathan Hart501f7882018-07-24 14:39:57 -070070
kartikey dubey6e903092019-05-22 13:35:28 +000071 // topics
Jonathan Hart501f7882018-07-24 14:39:57 -070072 private static final String TOPIC = "authentication.events";
kartikey dubey6e903092019-05-22 13:35:28 +000073 private static final String AUTHENTICATION_STATISTICS_TOPIC = "onos.aaa.stats.kpis";
Jonathan Hart501f7882018-07-24 14:39:57 -070074
kartikey dubey6e903092019-05-22 13:35:28 +000075 // auth event params
Jonathan Hart2aad7792018-07-31 15:09:17 -040076 private static final String TIMESTAMP = "timestamp";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040077 private static final String DEVICE_ID = "deviceId";
78 private static final String PORT_NUMBER = "portNumber";
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070079 private static final String SERIAL_NUMBER = "serialNumber";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040080 private static final String AUTHENTICATION_STATE = "authenticationState";
Jonathan Hart501f7882018-07-24 14:39:57 -070081
kartikey dubey6e903092019-05-22 13:35:28 +000082 // auth stats event params
83 private static final String ACCEPT_RESPONSES_RX = "acceptResponsesRx";
84 private static final String REJECT_RESPONSES_RX = "rejectResponsesRx";
85 private static final String CHALLENGE_RESPONSES_RX = "challengeResponsesRx";
86 private static final String ACCESS_REQUESTS_TX = "accessRequestsTx";
87 private static final String INVALID_VALIDATORS_RX = "invalidValidatorsRx";
88 private static final String UNKNOWN_TYPE_RX = "unknownTypeRx";
89 private static final String PENDING_REQUESTS = "pendingRequests";
90 private static final String DROPPED_RESPONSES_RX = "droppedResponsesRx";
91 private static final String MALFORMED_RESPONSES_RX = "malformedResponsesRx";
92 private static final String UNKNOWN_SERVER_RX = "unknownServerRx";
93 private static final String REQUEST_RTT_MILLIS = "requestRttMillis";
94 private static final String REQUEST_RE_TX = "requestReTx";
Shubham Sharma9188dde2019-06-20 07:16:08 +000095 private static final String TIMED_OUT_PACKETS = "timedOutPackets";
Shubham Sharma0357efb2019-08-09 06:54:22 +000096 private static final String EAPOL_LOGOFF_RX = "eapolLogoffRx";
97 private static final String EAPOL_RES_IDENTITY_MSG_TRANS = "eapolResIdentityMsgTrans";
98 private static final String EAPOL_AUTH_SUCCESS_TRANS = "eapolAuthSuccessTrans";
99 private static final String EAPOL_AUTH_FAILURE_TRANS = "eapolAuthFailureTrans";
100 private static final String EAPOL_START_REQ_TRANS = "eapolStartReqTrans";
101 private static final String EAP_PKT_TX_AUTH_CHOOSE_EAP = "eapPktTxauthChooseEap";
102 private static final String EAPOL_TRANS_RESP_NOT_NAK = "eapolTransRespNotNak";
kartikey dubey6e903092019-05-22 13:35:28 +0000103
Matteo Scandolo03f13c12019-03-20 14:38:12 -0700104 protected void bindAuthenticationService(AuthenticationService authenticationService) {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700105 log.info("bindAuthenticationService");
Matteo Scandolo03f13c12019-03-20 14:38:12 -0700106 if (this.authenticationService == null) {
107 log.info("Binding AuthenticationService");
108 this.authenticationService = authenticationService;
109 log.info("Adding listener on AuthenticationService");
110 authenticationService.addListener(listener);
111 } else {
112 log.warn("Trying to bind AuthenticationService but it is already bound");
113 }
114 }
115
116 protected void unbindAuthenticationService(AuthenticationService authenticationService) {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700117 log.info("unbindAuthenticationService");
Matteo Scandolo03f13c12019-03-20 14:38:12 -0700118 if (this.authenticationService == authenticationService) {
119 log.info("Unbinding AuthenticationService");
120 this.authenticationService = null;
121 log.info("Removing listener on AuthenticationService");
122 authenticationService.removeListener(listener);
123 } else {
124 log.warn("Trying to unbind AuthenticationService but it is already unbound");
125 }
126 }
127
kartikey dubey6e903092019-05-22 13:35:28 +0000128 protected void bindAuthenticationStatService(AuthenticationStatisticsService authenticationStatisticsService) {
129 log.info("bindAuthenticationStatService");
130 if (this.authenticationStatisticsService == null) {
131 log.info("Binding AuthenticationStastService");
132 this.authenticationStatisticsService = authenticationStatisticsService;
133 log.info("Adding listener on AuthenticationStatService");
134 authenticationStatisticsService.addListener(authenticationStatisticsEventListener);
135 } else {
136 log.warn("Trying to bind AuthenticationStatService but it is already bound");
137 }
138 }
139
140 protected void unbindAuthenticationStatService(AuthenticationStatisticsService authenticationStatisticsService) {
141 log.info("unbindAuthenticationStatService");
142 if (this.authenticationStatisticsService == authenticationStatisticsService) {
143 log.info("Unbinding AuthenticationStatService");
144 this.authenticationStatisticsService = null;
145 log.info("Removing listener on AuthenticationStatService");
146 authenticationStatisticsService.removeListener(authenticationStatisticsEventListener);
147 } else {
148 log.warn("Trying to unbind AuthenticationStatService but it is already unbound");
149 }
150 }
151
Jonathan Hart501f7882018-07-24 14:39:57 -0700152 @Activate
153 public void activate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700154 log.info("Started AaaKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -0700155 }
156
157 @Deactivate
158 public void deactivate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700159 log.info("Stopped AaaKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -0700160 }
161
162 private void handle(AuthenticationEvent event) {
163 eventBusService.send(TOPIC, serialize(event));
164 }
165
kartikey dubey6e903092019-05-22 13:35:28 +0000166 private void handleStat(AuthenticationStatisticsEvent event) {
167 eventBusService.send(AUTHENTICATION_STATISTICS_TOPIC, serializeStat(event));
Matteo Scandoloeba419b2019-11-25 10:42:04 -0700168 log.trace("AuthenticationStatisticsEvent sent successfully");
kartikey dubey6e903092019-05-22 13:35:28 +0000169 }
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700170
kartikey dubey6e903092019-05-22 13:35:28 +0000171 private JsonNode serialize(AuthenticationEvent event) {
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700172 String sn = deviceService.getPort(event.subject()).annotations().value(AnnotationKeys.PORT_NAME);
173
Jonathan Hart501f7882018-07-24 14:39:57 -0700174 ObjectMapper mapper = new ObjectMapper();
175 ObjectNode authEvent = mapper.createObjectNode();
Jonathan Hart2aad7792018-07-31 15:09:17 -0400176 authEvent.put(TIMESTAMP, Instant.now().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700177 authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
178 authEvent.put(PORT_NUMBER, event.subject().port().toString());
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700179 authEvent.put(SERIAL_NUMBER, sn);
Jonathan Hart501f7882018-07-24 14:39:57 -0700180 authEvent.put(AUTHENTICATION_STATE, event.type().toString());
181 return authEvent;
182 }
183
kartikey dubey6e903092019-05-22 13:35:28 +0000184 private JsonNode serializeStat(AuthenticationStatisticsEvent event) {
Matteo Scandoloeba419b2019-11-25 10:42:04 -0700185 log.trace("Serializing AuthenticationStatisticsEvent");
kartikey dubey6e903092019-05-22 13:35:28 +0000186 ObjectMapper mapper = new ObjectMapper();
187 ObjectNode authMetricsEvent = mapper.createObjectNode();
188 authMetricsEvent.put(TIMESTAMP, Instant.now().toString());
189 authMetricsEvent.put(ACCEPT_RESPONSES_RX, event.subject().getAcceptResponsesRx());
190 authMetricsEvent.put(REJECT_RESPONSES_RX, event.subject().getRejectResponsesRx());
191 authMetricsEvent.put(CHALLENGE_RESPONSES_RX, event.subject().getChallengeResponsesRx());
192 authMetricsEvent.put(ACCESS_REQUESTS_TX, event.subject().getAccessRequestsTx());
193 authMetricsEvent.put(INVALID_VALIDATORS_RX, event.subject().getInvalidValidatorsRx());
194 authMetricsEvent.put(UNKNOWN_TYPE_RX, event.subject().getUnknownTypeRx());
195 authMetricsEvent.put(PENDING_REQUESTS, event.subject().getPendingRequests());
196 authMetricsEvent.put(DROPPED_RESPONSES_RX, event.subject().getDroppedResponsesRx());
197 authMetricsEvent.put(MALFORMED_RESPONSES_RX, event.subject().getMalformedResponsesRx());
198 authMetricsEvent.put(UNKNOWN_SERVER_RX, event.subject().getUnknownServerRx());
199 authMetricsEvent.put(REQUEST_RTT_MILLIS, event.subject().getRequestRttMilis());
200 authMetricsEvent.put(REQUEST_RE_TX, event.subject().getRequestReTx());
Shubham Sharma9188dde2019-06-20 07:16:08 +0000201 authMetricsEvent.put(TIMED_OUT_PACKETS, event.subject().getTimedOutPackets());
Shubham Sharma0357efb2019-08-09 06:54:22 +0000202 authMetricsEvent.put(EAPOL_LOGOFF_RX, event.subject().getEapolLogoffRx());
203 authMetricsEvent.put(EAPOL_RES_IDENTITY_MSG_TRANS, event.subject().getEapolResIdentityMsgTrans());
204 authMetricsEvent.put(EAPOL_AUTH_SUCCESS_TRANS, event.subject().getEapolAuthSuccessTrans());
205 authMetricsEvent.put(EAPOL_AUTH_FAILURE_TRANS, event.subject().getEapolAuthFailureTrans());
206 authMetricsEvent.put(EAPOL_START_REQ_TRANS, event.subject().getEapolStartReqTrans());
207 authMetricsEvent.put(EAP_PKT_TX_AUTH_CHOOSE_EAP, event.subject().getEapPktTxauthChooseEap());
208 authMetricsEvent.put(EAPOL_TRANS_RESP_NOT_NAK, event.subject().getEapolTransRespNotNak());
kartikey dubey6e903092019-05-22 13:35:28 +0000209 return authMetricsEvent;
210 }
211
Jonathan Hart501f7882018-07-24 14:39:57 -0700212 private class InternalAuthenticationListener implements
Shubham Sharma9188dde2019-06-20 07:16:08 +0000213 AuthenticationEventListener {
Jonathan Hart501f7882018-07-24 14:39:57 -0700214 @Override
215 public void event(AuthenticationEvent authenticationEvent) {
216 handle(authenticationEvent);
217 }
218 }
kartikey dubey6e903092019-05-22 13:35:28 +0000219
220 private class InternalAuthenticationStatisticsListner implements
221 AuthenticationStatisticsEventListener {
222 @Override
223 public void event(AuthenticationStatisticsEvent authenticationStatisticsEvent) {
224 handleStat(authenticationStatisticsEvent);
225 }
226 }
227}