blob: a09cb41c94af8e94c394c031a612945ec0adae91 [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 Scandolo580fb7f2019-04-17 15:33:15 -070027import org.onosproject.net.AnnotationKeys;
28import org.onosproject.net.device.DeviceService;
Jonathan Hart501f7882018-07-24 14:39:57 -070029import org.opencord.aaa.AuthenticationEvent;
30import org.opencord.aaa.AuthenticationEventListener;
31import org.opencord.aaa.AuthenticationService;
32import org.opencord.kafka.EventBusService;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Jonathan Hart2aad7792018-07-31 15:09:17 -040036import java.time.Instant;
37
Jonathan Hart501f7882018-07-24 14:39:57 -070038/**
39 * Listens for AAA events and pushes them on a Kafka bus.
40 */
41@Component(immediate = true)
42public class AaaKafkaIntegration {
43
44 public Logger log = LoggerFactory.getLogger(getClass());
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected EventBusService eventBusService;
48
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected DeviceService deviceService;
51
Matteo Scandolo03f13c12019-03-20 14:38:12 -070052 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
53 bind = "bindAuthenticationService",
54 unbind = "unbindAuthenticationService")
Jonathan Hart501f7882018-07-24 14:39:57 -070055 protected AuthenticationService authenticationService;
56
57 private final AuthenticationEventListener listener = new InternalAuthenticationListener();
58
59 private static final String TOPIC = "authentication.events";
60
Jonathan Hart2aad7792018-07-31 15:09:17 -040061 private static final String TIMESTAMP = "timestamp";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040062 private static final String DEVICE_ID = "deviceId";
63 private static final String PORT_NUMBER = "portNumber";
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070064 private static final String SERIAL_NUMBER = "serialNumber";
Jonathan Hartf54e5ba2018-07-31 14:57:22 -040065 private static final String AUTHENTICATION_STATE = "authenticationState";
Jonathan Hart501f7882018-07-24 14:39:57 -070066
Matteo Scandolo03f13c12019-03-20 14:38:12 -070067 protected void bindAuthenticationService(AuthenticationService authenticationService) {
68 if (this.authenticationService == null) {
69 log.info("Binding AuthenticationService");
70 this.authenticationService = authenticationService;
71 log.info("Adding listener on AuthenticationService");
72 authenticationService.addListener(listener);
73 } else {
74 log.warn("Trying to bind AuthenticationService but it is already bound");
75 }
76 }
77
78 protected void unbindAuthenticationService(AuthenticationService authenticationService) {
79 if (this.authenticationService == authenticationService) {
80 log.info("Unbinding AuthenticationService");
81 this.authenticationService = null;
82 log.info("Removing listener on AuthenticationService");
83 authenticationService.removeListener(listener);
84 } else {
85 log.warn("Trying to unbind AuthenticationService but it is already unbound");
86 }
87 }
88
Jonathan Hart501f7882018-07-24 14:39:57 -070089 @Activate
90 public void activate() {
Jonathan Hart501f7882018-07-24 14:39:57 -070091 log.info("Started");
92 }
93
94 @Deactivate
95 public void deactivate() {
Jonathan Hart501f7882018-07-24 14:39:57 -070096 log.info("Stopped");
97 }
98
99 private void handle(AuthenticationEvent event) {
100 eventBusService.send(TOPIC, serialize(event));
101 }
102
103 private JsonNode serialize(AuthenticationEvent event) {
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700104
105 String sn = deviceService.getPort(event.subject()).annotations().value(AnnotationKeys.PORT_NAME);
106
Jonathan Hart501f7882018-07-24 14:39:57 -0700107 ObjectMapper mapper = new ObjectMapper();
108 ObjectNode authEvent = mapper.createObjectNode();
Jonathan Hart2aad7792018-07-31 15:09:17 -0400109 authEvent.put(TIMESTAMP, Instant.now().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700110 authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
111 authEvent.put(PORT_NUMBER, event.subject().port().toString());
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700112 authEvent.put(SERIAL_NUMBER, sn);
Jonathan Hart501f7882018-07-24 14:39:57 -0700113 authEvent.put(AUTHENTICATION_STATE, event.type().toString());
114 return authEvent;
115 }
116
117 private class InternalAuthenticationListener implements
118 AuthenticationEventListener {
119
120 @Override
121 public void event(AuthenticationEvent authenticationEvent) {
122 handle(authenticationEvent);
123 }
124 }
125}