Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.opencord.kafka.integrations; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
| 28 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 29 | import org.opencord.dhcpl2relay.DhcpL2RelayListener; |
| 30 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
| 31 | import org.opencord.kafka.EventBusService; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.time.Instant; |
| 36 | |
| 37 | /** |
| 38 | * Listens for DHCP L2 relay events and pushes them on a Kafka bus. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | public class DhcpL2RelayKafkaIntegration { |
| 42 | |
| 43 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected EventBusService eventBusService; |
| 47 | |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, |
| 49 | bind = "bindDhcpL2RelayService", |
| 50 | unbind = "unbindDhcpL2RelayService") |
| 51 | protected DhcpL2RelayService dhcpL2RelayService; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 52 | |
| 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 Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 65 | 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 Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 89 | log.info("Started"); |
| 90 | } |
| 91 | |
| 92 | @Deactivate |
| 93 | public void deactivate() { |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 94 | 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 | } |