blob: 316032efb2830e644c6843797f13fb68f53b4014 [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;
Matteo Scandolod50a4d32019-04-24 12:10:21 -070027import org.apache.felix.scr.annotations.ReferencePolicy;
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070028import org.onosproject.net.AnnotationKeys;
29import org.onosproject.net.device.DeviceService;
Jonathan Hart501f7882018-07-24 14:39:57 -070030import org.opencord.aaa.AuthenticationEvent;
31import org.opencord.aaa.AuthenticationEventListener;
32import org.opencord.aaa.AuthenticationService;
33import org.opencord.kafka.EventBusService;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Jonathan Hart2aad7792018-07-31 15:09:17 -040037import java.time.Instant;
38
Jonathan Hart501f7882018-07-24 14:39:57 -070039/**
40 * Listens for AAA events and pushes them on a Kafka bus.
41 */
42@Component(immediate = true)
43public class AaaKafkaIntegration {
44
45 public Logger log = LoggerFactory.getLogger(getClass());
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected EventBusService eventBusService;
49
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected DeviceService deviceService;
52
Matteo Scandolo03f13c12019-03-20 14:38:12 -070053 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
Matteo Scandolod50a4d32019-04-24 12:10:21 -070054 policy = ReferencePolicy.DYNAMIC,
Matteo Scandolo03f13c12019-03-20 14:38:12 -070055 bind = "bindAuthenticationService",
56 unbind = "unbindAuthenticationService")
Jonathan Hart501f7882018-07-24 14:39:57 -070057 protected AuthenticationService authenticationService;
58
59 private final AuthenticationEventListener listener = new InternalAuthenticationListener();
60
61 private static final String TOPIC = "authentication.events";
62
Jonathan Hart2aad7792018-07-31 15:09:17 -040063 private static final String TIMESTAMP = "timestamp";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040064 private static final String DEVICE_ID = "deviceId";
65 private static final String PORT_NUMBER = "portNumber";
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070066 private static final String SERIAL_NUMBER = "serialNumber";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040067 private static final String AUTHENTICATION_STATE = "authenticationState";
Jonathan Hart501f7882018-07-24 14:39:57 -070068
Matteo Scandolo03f13c12019-03-20 14:38:12 -070069 protected void bindAuthenticationService(AuthenticationService authenticationService) {
Matteo Scandolod50a4d32019-04-24 12:10:21 -070070 log.info("bindAuthenticationService");
Matteo Scandolo03f13c12019-03-20 14:38:12 -070071 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 Scandolod50a4d32019-04-24 12:10:21 -070082 log.info("unbindAuthenticationService");
Matteo Scandolo03f13c12019-03-20 14:38:12 -070083 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 Hart501f7882018-07-24 14:39:57 -070093 @Activate
94 public void activate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -070095 log.info("Started AaaKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -070096 }
97
98 @Deactivate
99 public void deactivate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700100 log.info("Stopped AaaKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -0700101 }
102
103 private void handle(AuthenticationEvent event) {
104 eventBusService.send(TOPIC, serialize(event));
105 }
106
107 private JsonNode serialize(AuthenticationEvent event) {
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700108
109 String sn = deviceService.getPort(event.subject()).annotations().value(AnnotationKeys.PORT_NAME);
110
Jonathan Hart501f7882018-07-24 14:39:57 -0700111 ObjectMapper mapper = new ObjectMapper();
112 ObjectNode authEvent = mapper.createObjectNode();
Jonathan Hart2aad7792018-07-31 15:09:17 -0400113 authEvent.put(TIMESTAMP, Instant.now().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700114 authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
115 authEvent.put(PORT_NUMBER, event.subject().port().toString());
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700116 authEvent.put(SERIAL_NUMBER, sn);
Jonathan Hart501f7882018-07-24 14:39:57 -0700117 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}