blob: d67b6a1721efca1ceaffd703a0538149eabd41e4 [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;
Shubham Sharma620d97a2019-06-27 12:31:01 +000035import org.opencord.aaa.RadiusOperationalStatusEvent;
36import org.opencord.aaa.RadiusOperationalStatusEventListener;
37import org.opencord.aaa.RadiusOperationalStatusService;
Jonathan Hart501f7882018-07-24 14:39:57 -070038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
Shubham Sharma9188dde2019-06-20 07:16:08 +000041import com.fasterxml.jackson.databind.JsonNode;
42import com.fasterxml.jackson.databind.ObjectMapper;
43import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart2aad7792018-07-31 15:09:17 -040044
Jonathan Hart501f7882018-07-24 14:39:57 -070045/**
46 * Listens for AAA events and pushes them on a Kafka bus.
47 */
48@Component(immediate = true)
49public class AaaKafkaIntegration {
50
51 public Logger log = LoggerFactory.getLogger(getClass());
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected EventBusService eventBusService;
55
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected DeviceService deviceService;
58
Matteo Scandolo03f13c12019-03-20 14:38:12 -070059 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
Matteo Scandolod50a4d32019-04-24 12:10:21 -070060 policy = ReferencePolicy.DYNAMIC,
Matteo Scandolo03f13c12019-03-20 14:38:12 -070061 bind = "bindAuthenticationService",
62 unbind = "unbindAuthenticationService")
Jonathan Hart501f7882018-07-24 14:39:57 -070063 protected AuthenticationService authenticationService;
kartikey dubey6e903092019-05-22 13:35:28 +000064 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
65 policy = ReferencePolicy.DYNAMIC,
66 bind = "bindAuthenticationStatService",
67 unbind = "unbindAuthenticationStatService")
68 protected AuthenticationStatisticsService authenticationStatisticsService;
Jonathan Hart501f7882018-07-24 14:39:57 -070069
Shubham Sharma620d97a2019-06-27 12:31:01 +000070 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
71 bind = "bindRadiusOperationalStatusService",
72 unbind = "unbindRadiusOperationalStatusService")
73 protected RadiusOperationalStatusService radiusOperationalStatusService;
74
Jonathan Hart501f7882018-07-24 14:39:57 -070075 private final AuthenticationEventListener listener = new InternalAuthenticationListener();
kartikey dubey6e903092019-05-22 13:35:28 +000076 private final AuthenticationStatisticsEventListener authenticationStatisticsEventListener =
Shubham Sharma9188dde2019-06-20 07:16:08 +000077 new InternalAuthenticationStatisticsListner();
Shubham Sharma620d97a2019-06-27 12:31:01 +000078 private final RadiusOperationalStatusEventListener radiusOperationalStatusEventListener =
79 new InternalRadiusOperationalStatusEventListener();
Jonathan Hart501f7882018-07-24 14:39:57 -070080
kartikey dubey6e903092019-05-22 13:35:28 +000081 // topics
Jonathan Hart501f7882018-07-24 14:39:57 -070082 private static final String TOPIC = "authentication.events";
kartikey dubey6e903092019-05-22 13:35:28 +000083 private static final String AUTHENTICATION_STATISTICS_TOPIC = "onos.aaa.stats.kpis";
Shubham Sharma620d97a2019-06-27 12:31:01 +000084 private static final String RADIUS_OPERATION_STATUS_TOPIC = "radiusOperationalStatus.events";
kartikey dubey6e903092019-05-22 13:35:28 +000085 // auth event params
Jonathan Hart2aad7792018-07-31 15:09:17 -040086 private static final String TIMESTAMP = "timestamp";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040087 private static final String DEVICE_ID = "deviceId";
88 private static final String PORT_NUMBER = "portNumber";
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070089 private static final String SERIAL_NUMBER = "serialNumber";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040090 private static final String AUTHENTICATION_STATE = "authenticationState";
Jonathan Hart501f7882018-07-24 14:39:57 -070091
kartikey dubey6e903092019-05-22 13:35:28 +000092 // auth stats event params
93 private static final String ACCEPT_RESPONSES_RX = "acceptResponsesRx";
94 private static final String REJECT_RESPONSES_RX = "rejectResponsesRx";
95 private static final String CHALLENGE_RESPONSES_RX = "challengeResponsesRx";
96 private static final String ACCESS_REQUESTS_TX = "accessRequestsTx";
97 private static final String INVALID_VALIDATORS_RX = "invalidValidatorsRx";
98 private static final String UNKNOWN_TYPE_RX = "unknownTypeRx";
99 private static final String PENDING_REQUESTS = "pendingRequests";
100 private static final String DROPPED_RESPONSES_RX = "droppedResponsesRx";
101 private static final String MALFORMED_RESPONSES_RX = "malformedResponsesRx";
102 private static final String UNKNOWN_SERVER_RX = "unknownServerRx";
103 private static final String REQUEST_RTT_MILLIS = "requestRttMillis";
104 private static final String REQUEST_RE_TX = "requestReTx";
Shubham Sharma9188dde2019-06-20 07:16:08 +0000105 private static final String TIMED_OUT_PACKETS = "timedOutPackets";
Shubham Sharma0357efb2019-08-09 06:54:22 +0000106 private static final String EAPOL_LOGOFF_RX = "eapolLogoffRx";
107 private static final String EAPOL_RES_IDENTITY_MSG_TRANS = "eapolResIdentityMsgTrans";
108 private static final String EAPOL_AUTH_SUCCESS_TRANS = "eapolAuthSuccessTrans";
109 private static final String EAPOL_AUTH_FAILURE_TRANS = "eapolAuthFailureTrans";
110 private static final String EAPOL_START_REQ_TRANS = "eapolStartReqTrans";
111 private static final String EAP_PKT_TX_AUTH_CHOOSE_EAP = "eapPktTxauthChooseEap";
112 private static final String EAPOL_TRANS_RESP_NOT_NAK = "eapolTransRespNotNak";
Shubham Sharmaabe0a262019-09-16 10:07:02 +0000113 private static final String EAPOL_FRAMES_TX = "eapolFramesTx";
114 private static final String AUTH_STATE_IDLE = "authStateIdle";
115 private static final String REQUEST_ID_FRAMES_TX = "requestIdFramesTx";
116 private static final String REQUEST_EAP_FRAMES_TX = "requestEapFramesTx";
117 private static final String INVALID_PKT_TYPE = "invalidPktType";
118 private static final String INVALID_BODY_LENGTH = "invalidBodyLength";
119 private static final String VALID_EAPOL_FRAMES_RX = "validEapolFramesRx";
120 private static final String PENDING_RES_SUPPLICANT = "pendingResSupplicant";
121 private static final String RES_ID_EAP_FRAMES_RX = "resIdEapFramesRx";
kartikey dubey6e903092019-05-22 13:35:28 +0000122
Shubham Sharma620d97a2019-06-27 12:31:01 +0000123 private static final String OPERATIONAL_STATUS = "radiusOperationalStatus";
124
Matteo Scandolo03f13c12019-03-20 14:38:12 -0700125 protected void bindAuthenticationService(AuthenticationService authenticationService) {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700126 log.info("bindAuthenticationService");
Matteo Scandolo03f13c12019-03-20 14:38:12 -0700127 if (this.authenticationService == null) {
128 log.info("Binding AuthenticationService");
129 this.authenticationService = authenticationService;
130 log.info("Adding listener on AuthenticationService");
131 authenticationService.addListener(listener);
132 } else {
133 log.warn("Trying to bind AuthenticationService but it is already bound");
134 }
135 }
136
137 protected void unbindAuthenticationService(AuthenticationService authenticationService) {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700138 log.info("unbindAuthenticationService");
Matteo Scandolo03f13c12019-03-20 14:38:12 -0700139 if (this.authenticationService == authenticationService) {
140 log.info("Unbinding AuthenticationService");
141 this.authenticationService = null;
142 log.info("Removing listener on AuthenticationService");
143 authenticationService.removeListener(listener);
144 } else {
145 log.warn("Trying to unbind AuthenticationService but it is already unbound");
146 }
147 }
148
kartikey dubey6e903092019-05-22 13:35:28 +0000149 protected void bindAuthenticationStatService(AuthenticationStatisticsService authenticationStatisticsService) {
150 log.info("bindAuthenticationStatService");
151 if (this.authenticationStatisticsService == null) {
152 log.info("Binding AuthenticationStastService");
153 this.authenticationStatisticsService = authenticationStatisticsService;
154 log.info("Adding listener on AuthenticationStatService");
155 authenticationStatisticsService.addListener(authenticationStatisticsEventListener);
156 } else {
157 log.warn("Trying to bind AuthenticationStatService but it is already bound");
158 }
159 }
160
161 protected void unbindAuthenticationStatService(AuthenticationStatisticsService authenticationStatisticsService) {
162 log.info("unbindAuthenticationStatService");
163 if (this.authenticationStatisticsService == authenticationStatisticsService) {
164 log.info("Unbinding AuthenticationStatService");
165 this.authenticationStatisticsService = null;
166 log.info("Removing listener on AuthenticationStatService");
167 authenticationStatisticsService.removeListener(authenticationStatisticsEventListener);
168 } else {
169 log.warn("Trying to unbind AuthenticationStatService but it is already unbound");
170 }
171 }
172
Shubham Sharma620d97a2019-06-27 12:31:01 +0000173 protected void bindRadiusOperationalStatusService(
174 RadiusOperationalStatusService radiusOperationalStatusService) {
175 log.info("bindRadiusOperationalStatusService");
176 if (this.radiusOperationalStatusService == null) {
177 log.info("Binding RadiusOperationalStatusService");
178 this.radiusOperationalStatusService = radiusOperationalStatusService;
179 log.info("Adding listener on RadiusOperationalStatusService");
180 radiusOperationalStatusService.addListener(radiusOperationalStatusEventListener);
181 } else {
182 log.warn("Trying to bind radiusOperationalStatusService but it is already bound");
183 }
184 }
185
186 protected void unbindRadiusOperationalStatusService(
187 RadiusOperationalStatusService radiusOperationalStatusService) {
188 log.info("unbindRadiusOperationalStatusService");
189 if (this.radiusOperationalStatusService == radiusOperationalStatusService) {
190 log.info("Unbind RadiusOperationalStatusService");
191 this.radiusOperationalStatusService = null;
192 log.info("Removing listener on RadiusOperationalStatusService");
193 radiusOperationalStatusService.removeListener(radiusOperationalStatusEventListener);
194 } else {
195 log.warn("Trying to unbind radiusOperationalStatusService but it is already unbound");
196 }
197 }
198
Jonathan Hart501f7882018-07-24 14:39:57 -0700199 @Activate
200 public void activate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700201 log.info("Started AaaKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -0700202 }
203
204 @Deactivate
205 public void deactivate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700206 log.info("Stopped AaaKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -0700207 }
208
209 private void handle(AuthenticationEvent event) {
210 eventBusService.send(TOPIC, serialize(event));
211 }
212
kartikey dubey6e903092019-05-22 13:35:28 +0000213 private void handleStat(AuthenticationStatisticsEvent event) {
214 eventBusService.send(AUTHENTICATION_STATISTICS_TOPIC, serializeStat(event));
Matteo Scandoloeba419b2019-11-25 10:42:04 -0700215 log.trace("AuthenticationStatisticsEvent sent successfully");
kartikey dubey6e903092019-05-22 13:35:28 +0000216 }
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700217
Shubham Sharma620d97a2019-06-27 12:31:01 +0000218 private void handleOperationalStatus(RadiusOperationalStatusEvent event) {
219 eventBusService.send(RADIUS_OPERATION_STATUS_TOPIC, serializeOperationalStatus(event));
220 log.info("RadiusOperationalStatusEvent sent successfully");
221 }
222
kartikey dubey6e903092019-05-22 13:35:28 +0000223 private JsonNode serialize(AuthenticationEvent event) {
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700224 String sn = deviceService.getPort(event.subject()).annotations().value(AnnotationKeys.PORT_NAME);
225
Jonathan Hart501f7882018-07-24 14:39:57 -0700226 ObjectMapper mapper = new ObjectMapper();
227 ObjectNode authEvent = mapper.createObjectNode();
Jonathan Hart2aad7792018-07-31 15:09:17 -0400228 authEvent.put(TIMESTAMP, Instant.now().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700229 authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
230 authEvent.put(PORT_NUMBER, event.subject().port().toString());
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700231 authEvent.put(SERIAL_NUMBER, sn);
Jonathan Hart501f7882018-07-24 14:39:57 -0700232 authEvent.put(AUTHENTICATION_STATE, event.type().toString());
233 return authEvent;
234 }
235
kartikey dubey6e903092019-05-22 13:35:28 +0000236 private JsonNode serializeStat(AuthenticationStatisticsEvent event) {
Matteo Scandoloeba419b2019-11-25 10:42:04 -0700237 log.trace("Serializing AuthenticationStatisticsEvent");
kartikey dubey6e903092019-05-22 13:35:28 +0000238 ObjectMapper mapper = new ObjectMapper();
239 ObjectNode authMetricsEvent = mapper.createObjectNode();
240 authMetricsEvent.put(TIMESTAMP, Instant.now().toString());
241 authMetricsEvent.put(ACCEPT_RESPONSES_RX, event.subject().getAcceptResponsesRx());
242 authMetricsEvent.put(REJECT_RESPONSES_RX, event.subject().getRejectResponsesRx());
243 authMetricsEvent.put(CHALLENGE_RESPONSES_RX, event.subject().getChallengeResponsesRx());
244 authMetricsEvent.put(ACCESS_REQUESTS_TX, event.subject().getAccessRequestsTx());
245 authMetricsEvent.put(INVALID_VALIDATORS_RX, event.subject().getInvalidValidatorsRx());
246 authMetricsEvent.put(UNKNOWN_TYPE_RX, event.subject().getUnknownTypeRx());
247 authMetricsEvent.put(PENDING_REQUESTS, event.subject().getPendingRequests());
248 authMetricsEvent.put(DROPPED_RESPONSES_RX, event.subject().getDroppedResponsesRx());
249 authMetricsEvent.put(MALFORMED_RESPONSES_RX, event.subject().getMalformedResponsesRx());
250 authMetricsEvent.put(UNKNOWN_SERVER_RX, event.subject().getUnknownServerRx());
251 authMetricsEvent.put(REQUEST_RTT_MILLIS, event.subject().getRequestRttMilis());
252 authMetricsEvent.put(REQUEST_RE_TX, event.subject().getRequestReTx());
Shubham Sharma9188dde2019-06-20 07:16:08 +0000253 authMetricsEvent.put(TIMED_OUT_PACKETS, event.subject().getTimedOutPackets());
Shubham Sharma0357efb2019-08-09 06:54:22 +0000254 authMetricsEvent.put(EAPOL_LOGOFF_RX, event.subject().getEapolLogoffRx());
255 authMetricsEvent.put(EAPOL_RES_IDENTITY_MSG_TRANS, event.subject().getEapolResIdentityMsgTrans());
256 authMetricsEvent.put(EAPOL_AUTH_SUCCESS_TRANS, event.subject().getEapolAuthSuccessTrans());
257 authMetricsEvent.put(EAPOL_AUTH_FAILURE_TRANS, event.subject().getEapolAuthFailureTrans());
258 authMetricsEvent.put(EAPOL_START_REQ_TRANS, event.subject().getEapolStartReqTrans());
259 authMetricsEvent.put(EAP_PKT_TX_AUTH_CHOOSE_EAP, event.subject().getEapPktTxauthChooseEap());
260 authMetricsEvent.put(EAPOL_TRANS_RESP_NOT_NAK, event.subject().getEapolTransRespNotNak());
Shubham Sharmaabe0a262019-09-16 10:07:02 +0000261 authMetricsEvent.put(EAPOL_FRAMES_TX, event.subject().getEapolFramesTx());
262 authMetricsEvent.put(AUTH_STATE_IDLE, event.subject().getAuthStateIdle());
263 authMetricsEvent.put(REQUEST_ID_FRAMES_TX, event.subject().getRequestIdFramesTx());
264 authMetricsEvent.put(REQUEST_EAP_FRAMES_TX, event.subject().getReqEapFramesTx());
265 authMetricsEvent.put(INVALID_PKT_TYPE, event.subject().getInvalidPktType());
266 authMetricsEvent.put(INVALID_BODY_LENGTH, event.subject().getInvalidBodyLength());
267 authMetricsEvent.put(VALID_EAPOL_FRAMES_RX, event.subject().getValidEapolFramesRx());
268 authMetricsEvent.put(PENDING_RES_SUPPLICANT, event.subject().getPendingResSupp());
269 authMetricsEvent.put(RES_ID_EAP_FRAMES_RX, event.subject().getEapolattrIdentity());
kartikey dubey6e903092019-05-22 13:35:28 +0000270 return authMetricsEvent;
271 }
272
Shubham Sharma620d97a2019-06-27 12:31:01 +0000273 private JsonNode serializeOperationalStatus(RadiusOperationalStatusEvent event) {
274 log.info("Serializing RadiusOperationalStatusEvent");
275 ObjectMapper mapper = new ObjectMapper();
276 ObjectNode authMetricsEvent = mapper.createObjectNode();
277 authMetricsEvent.put(TIMESTAMP, Instant.now().toString());
278 log.info("---OPERATIONAL_STATUS----" + event.subject());
279 authMetricsEvent.put(OPERATIONAL_STATUS, event.subject());
280 return authMetricsEvent;
281 }
282
Jonathan Hart501f7882018-07-24 14:39:57 -0700283 private class InternalAuthenticationListener implements
Shubham Sharma9188dde2019-06-20 07:16:08 +0000284 AuthenticationEventListener {
Jonathan Hart501f7882018-07-24 14:39:57 -0700285 @Override
286 public void event(AuthenticationEvent authenticationEvent) {
287 handle(authenticationEvent);
288 }
289 }
kartikey dubey6e903092019-05-22 13:35:28 +0000290
291 private class InternalAuthenticationStatisticsListner implements
292 AuthenticationStatisticsEventListener {
293 @Override
294 public void event(AuthenticationStatisticsEvent authenticationStatisticsEvent) {
295 handleStat(authenticationStatisticsEvent);
296 }
297 }
Shubham Sharma620d97a2019-06-27 12:31:01 +0000298
299 private class InternalRadiusOperationalStatusEventListener implements
300 RadiusOperationalStatusEventListener {
301 @Override
302 public void event(RadiusOperationalStatusEvent radiusOperationalStatusEvent) {
303 handleOperationalStatus(radiusOperationalStatusEvent);
304 }
305
306 }
Shubham Sharmaabe0a262019-09-16 10:07:02 +0000307}