blob: 867bcfecb136ac8116e84a0fa0ace6179f9784c3 [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 Scandolo580fb7f2019-04-17 15:33:15 -070027import org.onosproject.net.AnnotationKeys;
28import org.onosproject.net.device.DeviceService;
Jonathan Hart2aad7792018-07-31 15:09:17 -040029import org.opencord.dhcpl2relay.DhcpAllocationInfo;
30import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
31import org.opencord.dhcpl2relay.DhcpL2RelayListener;
32import org.opencord.dhcpl2relay.DhcpL2RelayService;
33import org.opencord.kafka.EventBusService;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import java.time.Instant;
38
39/**
40 * Listens for DHCP L2 relay events and pushes them on a Kafka bus.
41 */
42@Component(immediate = true)
43public class DhcpL2RelayKafkaIntegration {
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,
54 bind = "bindDhcpL2RelayService",
55 unbind = "unbindDhcpL2RelayService")
56 protected DhcpL2RelayService dhcpL2RelayService;
Jonathan Hart2aad7792018-07-31 15:09:17 -040057
58 private final DhcpL2RelayListener listener = new InternalDhcpL2RelayListener();
59
60 private static final String TOPIC = "dhcp.events";
61
62 private static final String TIMESTAMP = "timestamp";
63 private static final String DEVICE_ID = "deviceId";
Matteo Scandolo580fb7f2019-04-17 15:33:15 -070064 private static final String PORT_NUMBER = "portNumber";
65 private static final String SERIAL_NUMBER = "serialNumber";
Jonathan Hart2aad7792018-07-31 15:09:17 -040066 private static final String TYPE = "type";
67 private static final String MESSAGE_TYPE = "messageType";
Jonathan Hart2aad7792018-07-31 15:09:17 -040068 private static final String MAC_ADDRESS = "macAddress";
69 private static final String IP_ADDRESS = "ipAddress";
70
Matteo Scandolo03f13c12019-03-20 14:38:12 -070071 protected void bindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
72 if (this.dhcpL2RelayService == null) {
73 log.info("Binding DhcpL2RelayService");
74 this.dhcpL2RelayService = dhcpL2RelayService;
75 log.info("Adding listener on DhcpL2RelayService");
76 dhcpL2RelayService.addListener(listener);
77 } else {
78 log.warn("Trying to bind DhcpL2RelayService but it is already bound");
79 }
80 }
81
82 protected void unbindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
83 if (this.dhcpL2RelayService == dhcpL2RelayService) {
84 log.info("Unbinding DhcpL2RelayService");
85 this.dhcpL2RelayService = null;
86 log.info("Removing listener on DhcpL2RelayService");
87 dhcpL2RelayService.removeListener(listener);
88 } else {
89 log.warn("Trying to unbind DhcpL2RelayService but it is already unbound");
90 }
91 }
92
Jonathan Hart2aad7792018-07-31 15:09:17 -040093 @Activate
94 public void activate() {
Jonathan Hart2aad7792018-07-31 15:09:17 -040095 log.info("Started");
96 }
97
98 @Deactivate
99 public void deactivate() {
Jonathan Hart2aad7792018-07-31 15:09:17 -0400100 log.info("Stopped");
101 }
102
103 private void handle(DhcpL2RelayEvent event) {
104 eventBusService.send(TOPIC, serialize(event));
105 }
106
107 private JsonNode serialize(DhcpL2RelayEvent event) {
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700108
109 String sn = deviceService.getPort(event.subject().location()).annotations().value(AnnotationKeys.PORT_NAME);
110
Jonathan Hart2aad7792018-07-31 15:09:17 -0400111 ObjectMapper mapper = new ObjectMapper();
112 ObjectNode dhcpEvent = mapper.createObjectNode();
113 DhcpAllocationInfo allocationInfo = event.subject();
114 dhcpEvent.put(TYPE, event.type().toString());
115 dhcpEvent.put(TIMESTAMP, Instant.now().toString());
116 dhcpEvent.put(DEVICE_ID, event.connectPoint().deviceId().toString());
Jonathan Hart2aad7792018-07-31 15:09:17 -0400117 dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString());
Matteo Scandolo580fb7f2019-04-17 15:33:15 -0700118 dhcpEvent.put(SERIAL_NUMBER, sn);
119 dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString());
Jonathan Hart2aad7792018-07-31 15:09:17 -0400120 dhcpEvent.put(MAC_ADDRESS, allocationInfo.macAddress().toString());
121 dhcpEvent.put(IP_ADDRESS, allocationInfo.ipAddress().toString());
122 return dhcpEvent;
123 }
124
125 private class InternalDhcpL2RelayListener implements
126 DhcpL2RelayListener {
127
128 @Override
129 public void event(DhcpL2RelayEvent dhcpL2RelayEvent) {
130 handle(dhcpL2RelayEvent);
131 }
132 }
133}