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; |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 27 | import org.apache.felix.scr.annotations.ReferencePolicy; |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 28 | import org.onosproject.net.AnnotationKeys; |
| 29 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 30 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
| 31 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 32 | import org.opencord.dhcpl2relay.DhcpL2RelayListener; |
| 33 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
| 34 | import org.opencord.kafka.EventBusService; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.time.Instant; |
| 39 | |
| 40 | /** |
| 41 | * Listens for DHCP L2 relay events and pushes them on a Kafka bus. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public class DhcpL2RelayKafkaIntegration { |
| 45 | |
| 46 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected EventBusService eventBusService; |
| 50 | |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected DeviceService deviceService; |
| 53 | |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 55 | policy = ReferencePolicy.DYNAMIC, |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 56 | bind = "bindDhcpL2RelayService", |
| 57 | unbind = "unbindDhcpL2RelayService") |
| 58 | protected DhcpL2RelayService dhcpL2RelayService; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 59 | |
| 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 Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 66 | private static final String PORT_NUMBER = "portNumber"; |
| 67 | private static final String SERIAL_NUMBER = "serialNumber"; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 68 | private static final String TYPE = "type"; |
| 69 | private static final String MESSAGE_TYPE = "messageType"; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 70 | private static final String MAC_ADDRESS = "macAddress"; |
| 71 | private static final String IP_ADDRESS = "ipAddress"; |
| 72 | |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 73 | 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 Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 95 | @Activate |
| 96 | public void activate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 97 | log.info("Started DhcpL2RelayKafkaIntegration"); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame^] | 102 | log.info("Stopped DhcpL2RelayKafkaIntegration"); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private void handle(DhcpL2RelayEvent event) { |
| 106 | eventBusService.send(TOPIC, serialize(event)); |
| 107 | } |
| 108 | |
| 109 | private JsonNode serialize(DhcpL2RelayEvent event) { |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 110 | |
| 111 | String sn = deviceService.getPort(event.subject().location()).annotations().value(AnnotationKeys.PORT_NAME); |
| 112 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 113 | 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 Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 119 | dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString()); |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 120 | dhcpEvent.put(SERIAL_NUMBER, sn); |
| 121 | dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString()); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 122 | 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 | } |