blob: 786be8774e61c3da0d0d049d94334a1f21f48d15 [file] [log] [blame]
Jonathan Hart2aad7792018-07-31 15:09:17 -04001/*
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.dhcpl2relay.DhcpAllocationInfo;
28import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
29import org.opencord.dhcpl2relay.DhcpL2RelayListener;
30import org.opencord.dhcpl2relay.DhcpL2RelayService;
31import org.opencord.kafka.EventBusService;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.time.Instant;
36
37/**
38 * Listens for DHCP L2 relay events and pushes them on a Kafka bus.
39 */
40@Component(immediate = true)
41public class DhcpL2RelayKafkaIntegration {
42
43 public Logger log = LoggerFactory.getLogger(getClass());
44
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 protected EventBusService eventBusService;
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected DhcpL2RelayService authenticationService;
50
51 private final DhcpL2RelayListener listener = new InternalDhcpL2RelayListener();
52
53 private static final String TOPIC = "dhcp.events";
54
55 private static final String TIMESTAMP = "timestamp";
56 private static final String DEVICE_ID = "deviceId";
57 private static final String TYPE = "type";
58 private static final String MESSAGE_TYPE = "messageType";
59 private static final String PORT_NUMBER = "portNumber";
60 private static final String MAC_ADDRESS = "macAddress";
61 private static final String IP_ADDRESS = "ipAddress";
62
63 @Activate
64 public void activate() {
65 authenticationService.addListener(listener);
66 log.info("Started");
67 }
68
69 @Deactivate
70 public void deactivate() {
71 authenticationService.removeListener(listener);
72 log.info("Stopped");
73 }
74
75 private void handle(DhcpL2RelayEvent event) {
76 eventBusService.send(TOPIC, serialize(event));
77 }
78
79 private JsonNode serialize(DhcpL2RelayEvent event) {
80 ObjectMapper mapper = new ObjectMapper();
81 ObjectNode dhcpEvent = mapper.createObjectNode();
82 DhcpAllocationInfo allocationInfo = event.subject();
83 dhcpEvent.put(TYPE, event.type().toString());
84 dhcpEvent.put(TIMESTAMP, Instant.now().toString());
85 dhcpEvent.put(DEVICE_ID, event.connectPoint().deviceId().toString());
86 dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString());
87 dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString());
88 dhcpEvent.put(MAC_ADDRESS, allocationInfo.macAddress().toString());
89 dhcpEvent.put(IP_ADDRESS, allocationInfo.ipAddress().toString());
90 return dhcpEvent;
91 }
92
93 private class InternalDhcpL2RelayListener implements
94 DhcpL2RelayListener {
95
96 @Override
97 public void event(DhcpL2RelayEvent dhcpL2RelayEvent) {
98 handle(dhcpL2RelayEvent);
99 }
100 }
101}