blob: 3eca723580521396b6a5e861377ae6ae9d0bf604 [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
Matteo Scandolo03f13c12019-03-20 14:38:12 -070048 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
49 bind = "bindDhcpL2RelayService",
50 unbind = "unbindDhcpL2RelayService")
51 protected DhcpL2RelayService dhcpL2RelayService;
Jonathan Hart2aad7792018-07-31 15:09:17 -040052
53 private final DhcpL2RelayListener listener = new InternalDhcpL2RelayListener();
54
55 private static final String TOPIC = "dhcp.events";
56
57 private static final String TIMESTAMP = "timestamp";
58 private static final String DEVICE_ID = "deviceId";
59 private static final String TYPE = "type";
60 private static final String MESSAGE_TYPE = "messageType";
61 private static final String PORT_NUMBER = "portNumber";
62 private static final String MAC_ADDRESS = "macAddress";
63 private static final String IP_ADDRESS = "ipAddress";
64
Matteo Scandolo03f13c12019-03-20 14:38:12 -070065 protected void bindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
66 if (this.dhcpL2RelayService == null) {
67 log.info("Binding DhcpL2RelayService");
68 this.dhcpL2RelayService = dhcpL2RelayService;
69 log.info("Adding listener on DhcpL2RelayService");
70 dhcpL2RelayService.addListener(listener);
71 } else {
72 log.warn("Trying to bind DhcpL2RelayService but it is already bound");
73 }
74 }
75
76 protected void unbindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
77 if (this.dhcpL2RelayService == dhcpL2RelayService) {
78 log.info("Unbinding DhcpL2RelayService");
79 this.dhcpL2RelayService = null;
80 log.info("Removing listener on DhcpL2RelayService");
81 dhcpL2RelayService.removeListener(listener);
82 } else {
83 log.warn("Trying to unbind DhcpL2RelayService but it is already unbound");
84 }
85 }
86
Jonathan Hart2aad7792018-07-31 15:09:17 -040087 @Activate
88 public void activate() {
Jonathan Hart2aad7792018-07-31 15:09:17 -040089 log.info("Started");
90 }
91
92 @Deactivate
93 public void deactivate() {
Jonathan Hart2aad7792018-07-31 15:09:17 -040094 log.info("Stopped");
95 }
96
97 private void handle(DhcpL2RelayEvent event) {
98 eventBusService.send(TOPIC, serialize(event));
99 }
100
101 private JsonNode serialize(DhcpL2RelayEvent event) {
102 ObjectMapper mapper = new ObjectMapper();
103 ObjectNode dhcpEvent = mapper.createObjectNode();
104 DhcpAllocationInfo allocationInfo = event.subject();
105 dhcpEvent.put(TYPE, event.type().toString());
106 dhcpEvent.put(TIMESTAMP, Instant.now().toString());
107 dhcpEvent.put(DEVICE_ID, event.connectPoint().deviceId().toString());
108 dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString());
109 dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString());
110 dhcpEvent.put(MAC_ADDRESS, allocationInfo.macAddress().toString());
111 dhcpEvent.put(IP_ADDRESS, allocationInfo.ipAddress().toString());
112 return dhcpEvent;
113 }
114
115 private class InternalDhcpL2RelayListener implements
116 DhcpL2RelayListener {
117
118 @Override
119 public void event(DhcpL2RelayEvent dhcpL2RelayEvent) {
120 handle(dhcpL2RelayEvent);
121 }
122 }
123}