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; |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 24 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
| 25 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 26 | import org.opencord.dhcpl2relay.DhcpL2RelayListener; |
| 27 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
| 28 | import org.opencord.kafka.EventBusService; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 34 | import org.osgi.service.component.annotations.ReferencePolicy; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 35 | |
| 36 | import java.time.Instant; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 37 | import java.util.concurrent.atomic.AtomicReference; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Listens for DHCP L2 relay events and pushes them on a Kafka bus. |
| 41 | */ |
| 42 | @Component(immediate = true) |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 43 | public class DhcpL2RelayKafkaIntegration extends AbstractKafkaIntegration { |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 44 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 46 | protected EventBusService eventBusService; |
| 47 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 49 | protected DeviceService deviceService; |
| 50 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 51 | @Reference(cardinality = ReferenceCardinality.OPTIONAL, |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 52 | policy = ReferencePolicy.DYNAMIC, |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 53 | bind = "bindDhcpL2RelayService", |
| 54 | unbind = "unbindDhcpL2RelayService") |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 55 | volatile protected DhcpL2RelayService ignore; |
| 56 | private final AtomicReference<DhcpL2RelayService> dhcpL2RelayServiceRef = new AtomicReference<>(); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 57 | |
| 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 Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 64 | private static final String PORT_NUMBER = "portNumber"; |
| 65 | private static final String SERIAL_NUMBER = "serialNumber"; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 66 | private static final String TYPE = "type"; |
| 67 | private static final String MESSAGE_TYPE = "messageType"; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 68 | private static final String MAC_ADDRESS = "macAddress"; |
| 69 | private static final String IP_ADDRESS = "ipAddress"; |
| 70 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 71 | protected void bindDhcpL2RelayService(DhcpL2RelayService incomingService) { |
| 72 | bindAndAddListener(incomingService, dhcpL2RelayServiceRef, listener); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 75 | protected void unbindDhcpL2RelayService(DhcpL2RelayService outgoingService) { |
| 76 | unbindAndRemoveListener(outgoingService, dhcpL2RelayServiceRef, listener); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 79 | @Activate |
| 80 | public void activate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 81 | log.info("Started DhcpL2RelayKafkaIntegration"); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Deactivate |
| 85 | public void deactivate() { |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame^] | 86 | unbindDhcpL2RelayService(dhcpL2RelayServiceRef.get()); |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 87 | log.info("Stopped DhcpL2RelayKafkaIntegration"); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private void handle(DhcpL2RelayEvent event) { |
| 91 | eventBusService.send(TOPIC, serialize(event)); |
| 92 | } |
| 93 | |
| 94 | private JsonNode serialize(DhcpL2RelayEvent event) { |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 95 | |
| 96 | String sn = deviceService.getPort(event.subject().location()).annotations().value(AnnotationKeys.PORT_NAME); |
| 97 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 98 | ObjectMapper mapper = new ObjectMapper(); |
| 99 | ObjectNode dhcpEvent = mapper.createObjectNode(); |
| 100 | DhcpAllocationInfo allocationInfo = event.subject(); |
| 101 | dhcpEvent.put(TYPE, event.type().toString()); |
| 102 | dhcpEvent.put(TIMESTAMP, Instant.now().toString()); |
| 103 | dhcpEvent.put(DEVICE_ID, event.connectPoint().deviceId().toString()); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 104 | dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString()); |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 105 | dhcpEvent.put(SERIAL_NUMBER, sn); |
| 106 | dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString()); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 107 | dhcpEvent.put(MAC_ADDRESS, allocationInfo.macAddress().toString()); |
| 108 | dhcpEvent.put(IP_ADDRESS, allocationInfo.ipAddress().toString()); |
| 109 | return dhcpEvent; |
| 110 | } |
| 111 | |
| 112 | private class InternalDhcpL2RelayListener implements |
| 113 | DhcpL2RelayListener { |
| 114 | |
| 115 | @Override |
| 116 | public void event(DhcpL2RelayEvent dhcpL2RelayEvent) { |
| 117 | handle(dhcpL2RelayEvent); |
| 118 | } |
| 119 | } |
| 120 | } |