blob: 2393b7348d1895fa561d61e77ec74aa99581062f [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;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.opencord.aaa.AuthenticationEvent;
28import org.opencord.aaa.AuthenticationEventListener;
29import org.opencord.aaa.AuthenticationService;
30import org.opencord.kafka.EventBusService;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34/**
35 * Listens for AAA events and pushes them on a Kafka bus.
36 */
37@Component(immediate = true)
38public class AaaKafkaIntegration {
39
40 public Logger log = LoggerFactory.getLogger(getClass());
41
42 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 protected EventBusService eventBusService;
44
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 protected AuthenticationService authenticationService;
47
48 private final AuthenticationEventListener listener = new InternalAuthenticationListener();
49
50 private static final String TOPIC = "authentication.events";
51
52 private static final String DEVICE_ID = "device_id";
53 private static final String PORT_NUMBER = "port_number";
54 private static final String AUTHENTICATION_STATE = "authentication_state";
55
56 @Activate
57 public void activate() {
58 authenticationService.addListener(listener);
59 log.info("Started");
60 }
61
62 @Deactivate
63 public void deactivate() {
64 authenticationService.removeListener(listener);
65 log.info("Stopped");
66 }
67
68 private void handle(AuthenticationEvent event) {
69 eventBusService.send(TOPIC, serialize(event));
70 }
71
72 private JsonNode serialize(AuthenticationEvent event) {
73 ObjectMapper mapper = new ObjectMapper();
74 ObjectNode authEvent = mapper.createObjectNode();
75 authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
76 authEvent.put(PORT_NUMBER, event.subject().port().toString());
77 authEvent.put(AUTHENTICATION_STATE, event.type().toString());
78 return authEvent;
79 }
80
81 private class InternalAuthenticationListener implements
82 AuthenticationEventListener {
83
84 @Override
85 public void event(AuthenticationEvent authenticationEvent) {
86 handle(authenticationEvent);
87 }
88 }
89}