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; |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 27 | import org.apache.felix.scr.annotations.ReferencePolicy; |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 28 | import org.onosproject.net.AnnotationKeys; |
| 29 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 30 | import org.opencord.aaa.AuthenticationEvent; |
| 31 | import org.opencord.aaa.AuthenticationEventListener; |
| 32 | import org.opencord.aaa.AuthenticationService; |
| 33 | import org.opencord.kafka.EventBusService; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 37 | import java.time.Instant; |
| 38 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Listens for AAA events and pushes them on a Kafka bus. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | public class AaaKafkaIntegration { |
| 44 | |
| 45 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected EventBusService eventBusService; |
| 49 | |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected DeviceService deviceService; |
| 52 | |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 54 | policy = ReferencePolicy.DYNAMIC, |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 55 | bind = "bindAuthenticationService", |
| 56 | unbind = "unbindAuthenticationService") |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 57 | protected AuthenticationService authenticationService; |
| 58 | |
| 59 | private final AuthenticationEventListener listener = new InternalAuthenticationListener(); |
| 60 | |
| 61 | private static final String TOPIC = "authentication.events"; |
| 62 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 63 | private static final String TIMESTAMP = "timestamp"; |
Jonathan Hart | f54e5ba | 2018-07-31 14:57:22 -0400 | [diff] [blame] | 64 | private static final String DEVICE_ID = "deviceId"; |
| 65 | private static final String PORT_NUMBER = "portNumber"; |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 66 | private static final String SERIAL_NUMBER = "serialNumber"; |
Jonathan Hart | f54e5ba | 2018-07-31 14:57:22 -0400 | [diff] [blame] | 67 | private static final String AUTHENTICATION_STATE = "authenticationState"; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 68 | |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 69 | protected void bindAuthenticationService(AuthenticationService authenticationService) { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 70 | log.info("bindAuthenticationService"); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 71 | if (this.authenticationService == null) { |
| 72 | log.info("Binding AuthenticationService"); |
| 73 | this.authenticationService = authenticationService; |
| 74 | log.info("Adding listener on AuthenticationService"); |
| 75 | authenticationService.addListener(listener); |
| 76 | } else { |
| 77 | log.warn("Trying to bind AuthenticationService but it is already bound"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | protected void unbindAuthenticationService(AuthenticationService authenticationService) { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 82 | log.info("unbindAuthenticationService"); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 83 | if (this.authenticationService == authenticationService) { |
| 84 | log.info("Unbinding AuthenticationService"); |
| 85 | this.authenticationService = null; |
| 86 | log.info("Removing listener on AuthenticationService"); |
| 87 | authenticationService.removeListener(listener); |
| 88 | } else { |
| 89 | log.warn("Trying to unbind AuthenticationService but it is already unbound"); |
| 90 | } |
| 91 | } |
| 92 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 93 | @Activate |
| 94 | public void activate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 95 | log.info("Started AaaKafkaIntegration"); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | public void deactivate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 100 | log.info("Stopped AaaKafkaIntegration"); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private void handle(AuthenticationEvent event) { |
| 104 | eventBusService.send(TOPIC, serialize(event)); |
| 105 | } |
| 106 | |
| 107 | private JsonNode serialize(AuthenticationEvent event) { |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 108 | |
| 109 | String sn = deviceService.getPort(event.subject()).annotations().value(AnnotationKeys.PORT_NAME); |
| 110 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 111 | ObjectMapper mapper = new ObjectMapper(); |
| 112 | ObjectNode authEvent = mapper.createObjectNode(); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 113 | authEvent.put(TIMESTAMP, Instant.now().toString()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 114 | authEvent.put(DEVICE_ID, event.subject().deviceId().toString()); |
| 115 | authEvent.put(PORT_NUMBER, event.subject().port().toString()); |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 116 | authEvent.put(SERIAL_NUMBER, sn); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 117 | authEvent.put(AUTHENTICATION_STATE, event.type().toString()); |
| 118 | return authEvent; |
| 119 | } |
| 120 | |
| 121 | private class InternalAuthenticationListener implements |
| 122 | AuthenticationEventListener { |
| 123 | |
| 124 | @Override |
| 125 | public void event(AuthenticationEvent authenticationEvent) { |
| 126 | handle(authenticationEvent); |
| 127 | } |
| 128 | } |
| 129 | } |