blob: e1ca65af79a3b907bcce5198eb51e6cb51a0b6e6 [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;
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 Hart2aad7792018-07-31 15:09:17 -040030import org.opencord.dhcpl2relay.DhcpAllocationInfo;
31import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
32import org.opencord.dhcpl2relay.DhcpL2RelayListener;
33import org.opencord.dhcpl2relay.DhcpL2RelayService;
34import org.opencord.kafka.EventBusService;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import java.time.Instant;
39
40/**
41 * Listens for DHCP L2 relay events and pushes them on a Kafka bus.
42 */
43@Component(immediate = true)
44public class DhcpL2RelayKafkaIntegration {
45
46 public Logger log = LoggerFactory.getLogger(getClass());
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected EventBusService eventBusService;
50
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected DeviceService deviceService;
53
Matteo Scandolo03f13c12019-03-20 14:38:12 -070054 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
Matteo Scandolod50a4d32019-04-24 12:10:21 -070055 policy = ReferencePolicy.DYNAMIC,
Matteo Scandolo03f13c12019-03-20 14:38:12 -070056 bind = "bindDhcpL2RelayService",
57 unbind = "unbindDhcpL2RelayService")
58 protected DhcpL2RelayService dhcpL2RelayService;
Jonathan Hart2aad7792018-07-31 15:09:17 -040059
60 private final DhcpL2RelayListener listener = new InternalDhcpL2RelayListener();
61
62 private static final String TOPIC = "dhcp.events";
63
64 private static final String TIMESTAMP = "timestamp";
65 private static final String DEVICE_ID = "deviceId";
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070066 private static final String PORT_NUMBER = "portNumber";
67 private static final String SERIAL_NUMBER = "serialNumber";
Jonathan Hart2aad7792018-07-31 15:09:17 -040068 private static final String TYPE = "type";
69 private static final String MESSAGE_TYPE = "messageType";
Jonathan Hart2aad7792018-07-31 15:09:17 -040070 private static final String MAC_ADDRESS = "macAddress";
71 private static final String IP_ADDRESS = "ipAddress";
72
Matteo Scandolo03f13c12019-03-20 14:38:12 -070073 protected void bindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
74 if (this.dhcpL2RelayService == null) {
75 log.info("Binding DhcpL2RelayService");
76 this.dhcpL2RelayService = dhcpL2RelayService;
77 log.info("Adding listener on DhcpL2RelayService");
78 dhcpL2RelayService.addListener(listener);
79 } else {
80 log.warn("Trying to bind DhcpL2RelayService but it is already bound");
81 }
82 }
83
84 protected void unbindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
85 if (this.dhcpL2RelayService == dhcpL2RelayService) {
86 log.info("Unbinding DhcpL2RelayService");
87 this.dhcpL2RelayService = null;
88 log.info("Removing listener on DhcpL2RelayService");
89 dhcpL2RelayService.removeListener(listener);
90 } else {
91 log.warn("Trying to unbind DhcpL2RelayService but it is already unbound");
92 }
93 }
94
Jonathan Hart2aad7792018-07-31 15:09:17 -040095 @Activate
96 public void activate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -070097 log.info("Started DhcpL2RelayKafkaIntegration");
Jonathan Hart2aad7792018-07-31 15:09:17 -040098 }
99
100 @Deactivate
101 public void deactivate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -0700102 log.info("Stopped DhcpL2RelayKafkaIntegration");
Jonathan Hart2aad7792018-07-31 15:09:17 -0400103 }
104
105 private void handle(DhcpL2RelayEvent event) {
106 eventBusService.send(TOPIC, serialize(event));
107 }
108
109 private JsonNode serialize(DhcpL2RelayEvent event) {
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700110
111 String sn = deviceService.getPort(event.subject().location()).annotations().value(AnnotationKeys.PORT_NAME);
112
Jonathan Hart2aad7792018-07-31 15:09:17 -0400113 ObjectMapper mapper = new ObjectMapper();
114 ObjectNode dhcpEvent = mapper.createObjectNode();
115 DhcpAllocationInfo allocationInfo = event.subject();
116 dhcpEvent.put(TYPE, event.type().toString());
117 dhcpEvent.put(TIMESTAMP, Instant.now().toString());
118 dhcpEvent.put(DEVICE_ID, event.connectPoint().deviceId().toString());
Jonathan Hart2aad7792018-07-31 15:09:17 -0400119 dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString());
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700120 dhcpEvent.put(SERIAL_NUMBER, sn);
121 dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString());
Jonathan Hart2aad7792018-07-31 15:09:17 -0400122 dhcpEvent.put(MAC_ADDRESS, allocationInfo.macAddress().toString());
123 dhcpEvent.put(IP_ADDRESS, allocationInfo.ipAddress().toString());
124 return dhcpEvent;
125 }
126
127 private class InternalDhcpL2RelayListener implements
128 DhcpL2RelayListener {
129
130 @Override
131 public void event(DhcpL2RelayEvent dhcpL2RelayEvent) {
132 handle(dhcpL2RelayEvent);
133 }
134 }
135}