Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.opencord.kafka.integrations; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.opencord.aaa.AuthenticationEvent; |
| 28 | import org.opencord.aaa.AuthenticationEventListener; |
| 29 | import org.opencord.aaa.AuthenticationService; |
| 30 | import org.opencord.kafka.EventBusService; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 34 | import java.time.Instant; |
| 35 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Listens for AAA events and pushes them on a Kafka bus. |
| 38 | */ |
| 39 | @Component(immediate = true) |
| 40 | public 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 Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 54 | private static final String TIMESTAMP = "timestamp"; |
Jonathan Hart | f54e5ba | 2018-07-31 14:57:22 -0400 | [diff] [blame] | 55 | private static final String DEVICE_ID = "deviceId"; |
| 56 | private static final String PORT_NUMBER = "portNumber"; |
| 57 | private static final String AUTHENTICATION_STATE = "authenticationState"; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 58 | |
| 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 Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 78 | authEvent.put(TIMESTAMP, Instant.now().toString()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 79 | 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 | } |