blob: e68e3073bdea8b321f5e07a1f78f33603327ad4d [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
Jonathan Hart2aad7792018-07-31 15:09:17 -040034import java.time.Instant;
35
Jonathan Hart501f7882018-07-24 14:39:57 -070036/**
37 * Listens for AAA events and pushes them on a Kafka bus.
38 */
39@Component(immediate = true)
40public class AaaKafkaIntegration {
41
42 public Logger log = LoggerFactory.getLogger(getClass());
43
44 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected EventBusService eventBusService;
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected AuthenticationService authenticationService;
49
50 private final AuthenticationEventListener listener = new InternalAuthenticationListener();
51
52 private static final String TOPIC = "authentication.events";
53
Jonathan Hart2aad7792018-07-31 15:09:17 -040054 private static final String TIMESTAMP = "timestamp";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040055 private static final String DEVICE_ID = "deviceId";
56 private static final String PORT_NUMBER = "portNumber";
57 private static final String AUTHENTICATION_STATE = "authenticationState";
Jonathan Hart501f7882018-07-24 14:39:57 -070058
59 @Activate
60 public void activate() {
61 authenticationService.addListener(listener);
62 log.info("Started");
63 }
64
65 @Deactivate
66 public void deactivate() {
67 authenticationService.removeListener(listener);
68 log.info("Stopped");
69 }
70
71 private void handle(AuthenticationEvent event) {
72 eventBusService.send(TOPIC, serialize(event));
73 }
74
75 private JsonNode serialize(AuthenticationEvent event) {
76 ObjectMapper mapper = new ObjectMapper();
77 ObjectNode authEvent = mapper.createObjectNode();
Jonathan Hart2aad7792018-07-31 15:09:17 -040078 authEvent.put(TIMESTAMP, Instant.now().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -070079 authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
80 authEvent.put(PORT_NUMBER, event.subject().port().toString());
81 authEvent.put(AUTHENTICATION_STATE, event.type().toString());
82 return authEvent;
83 }
84
85 private class InternalAuthenticationListener implements
86 AuthenticationEventListener {
87
88 @Override
89 public void event(AuthenticationEvent authenticationEvent) {
90 handle(authenticationEvent);
91 }
92 }
93}