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; |
Marcos Aurelio Carrero | 30db23c | 2019-12-04 16:09:49 -0300 | [diff] [blame] | 22 | import org.onosproject.cluster.ClusterService; |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 23 | import org.onosproject.net.AnnotationKeys; |
| 24 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 25 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
| 26 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 27 | import org.opencord.dhcpl2relay.DhcpL2RelayListener; |
| 28 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
| 29 | import org.opencord.kafka.EventBusService; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 35 | import org.osgi.service.component.annotations.ReferencePolicy; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 36 | |
| 37 | import java.time.Instant; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 38 | import java.util.concurrent.atomic.AtomicReference; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Listens for DHCP L2 relay events and pushes them on a Kafka bus. |
| 42 | */ |
| 43 | @Component(immediate = true) |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 44 | public class DhcpL2RelayKafkaIntegration extends AbstractKafkaIntegration { |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 45 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 47 | protected EventBusService eventBusService; |
| 48 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Marcos Aurelio Carrero | 30db23c | 2019-12-04 16:09:49 -0300 | [diff] [blame] | 50 | protected ClusterService clusterService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 53 | protected DeviceService deviceService; |
| 54 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.OPTIONAL, |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 56 | policy = ReferencePolicy.DYNAMIC, |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 57 | bind = "bindDhcpL2RelayService", |
| 58 | unbind = "unbindDhcpL2RelayService") |
Carmelo Cascone | 69eec92 | 2019-11-15 21:34:02 -0800 | [diff] [blame] | 59 | protected volatile DhcpL2RelayService ignore; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 60 | private final AtomicReference<DhcpL2RelayService> dhcpL2RelayServiceRef = new AtomicReference<>(); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 61 | |
| 62 | private final DhcpL2RelayListener listener = new InternalDhcpL2RelayListener(); |
| 63 | |
Marcos Aurelio Carrero | 30db23c | 2019-12-04 16:09:49 -0300 | [diff] [blame] | 64 | // topics |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 65 | protected static final String TOPIC = "dhcp.events"; |
| 66 | protected static final String DHCP_STATS_TOPIC = "onos.dhcp.stats.kpis"; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 67 | |
| 68 | private static final String TIMESTAMP = "timestamp"; |
| 69 | private static final String DEVICE_ID = "deviceId"; |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 70 | private static final String PORT_NUMBER = "portNumber"; |
| 71 | private static final String SERIAL_NUMBER = "serialNumber"; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 72 | private static final String TYPE = "type"; |
| 73 | private static final String MESSAGE_TYPE = "messageType"; |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 74 | private static final String MAC_ADDRESS = "macAddress"; |
| 75 | private static final String IP_ADDRESS = "ipAddress"; |
| 76 | |
Marcos Aurelio Carrero | 30db23c | 2019-12-04 16:09:49 -0300 | [diff] [blame] | 77 | // dhcp stats event params |
| 78 | static final String CONNECT_POINT = "connectPoint"; |
| 79 | static final String INSTANCE_ID = "instance_id"; |
| 80 | static final String METRICS = "metrics"; |
| 81 | static final String SUBSCRIBER_ID = "subscriberId"; |
| 82 | static final String SUBSCRIBER_INFO = "subscriberInfo"; |
| 83 | static final String TS = "ts"; |
| 84 | static final String TITLE = "title"; |
| 85 | |
| 86 | static final String GLOBAL_STATS_TITLE = "DHCP_L2_Relay_stats"; |
| 87 | static final String PER_SUBSCRIBER_STATS_TITLE = "DHCP_L2_Relay_stats_Per_Subscriber"; |
| 88 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 89 | protected void bindDhcpL2RelayService(DhcpL2RelayService incomingService) { |
| 90 | bindAndAddListener(incomingService, dhcpL2RelayServiceRef, listener); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 93 | protected void unbindDhcpL2RelayService(DhcpL2RelayService outgoingService) { |
| 94 | unbindAndRemoveListener(outgoingService, dhcpL2RelayServiceRef, listener); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 97 | @Activate |
| 98 | public void activate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 99 | log.info("Started DhcpL2RelayKafkaIntegration"); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Deactivate |
| 103 | public void deactivate() { |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 104 | unbindDhcpL2RelayService(dhcpL2RelayServiceRef.get()); |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 105 | log.info("Stopped DhcpL2RelayKafkaIntegration"); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private void handle(DhcpL2RelayEvent event) { |
Marcos Aurelio Carrero | 30db23c | 2019-12-04 16:09:49 -0300 | [diff] [blame] | 109 | switch (event.type()) { |
| 110 | case STATS_UPDATE: |
| 111 | // pushes the stats based on the received event (per subscriber or global) on a Kafka bus |
| 112 | if (event.getSubscriberId() != null && event.subject() != null) { |
| 113 | eventBusService.send(DHCP_STATS_TOPIC, serializeStat(event, PER_SUBSCRIBER_STATS_TITLE)); |
| 114 | } else { |
| 115 | eventBusService.send(DHCP_STATS_TOPIC, serializeStat(event, GLOBAL_STATS_TITLE)); |
| 116 | } |
| 117 | log.trace("Writing to kafka topic:{}, type:{}", DHCP_STATS_TOPIC, |
| 118 | DhcpL2RelayEvent.Type.STATS_UPDATE.toString()); |
| 119 | break; |
| 120 | default: |
| 121 | eventBusService.send(TOPIC, serialize(event)); |
| 122 | log.trace("Writing to kafka topic:{}, type:{}", TOPIC, event.type().toString()); |
| 123 | break; |
| 124 | } |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private JsonNode serialize(DhcpL2RelayEvent event) { |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 128 | |
| 129 | String sn = deviceService.getPort(event.subject().location()).annotations().value(AnnotationKeys.PORT_NAME); |
| 130 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 131 | ObjectMapper mapper = new ObjectMapper(); |
| 132 | ObjectNode dhcpEvent = mapper.createObjectNode(); |
| 133 | DhcpAllocationInfo allocationInfo = event.subject(); |
| 134 | dhcpEvent.put(TYPE, event.type().toString()); |
| 135 | dhcpEvent.put(TIMESTAMP, Instant.now().toString()); |
| 136 | dhcpEvent.put(DEVICE_ID, event.connectPoint().deviceId().toString()); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 137 | dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString()); |
Matteo Scandolo | 580fb7f | 2019-04-17 15:33:15 -0700 | [diff] [blame] | 138 | dhcpEvent.put(SERIAL_NUMBER, sn); |
| 139 | dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString()); |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 140 | dhcpEvent.put(MAC_ADDRESS, allocationInfo.macAddress().toString()); |
| 141 | dhcpEvent.put(IP_ADDRESS, allocationInfo.ipAddress().toString()); |
| 142 | return dhcpEvent; |
| 143 | } |
| 144 | |
Marcos Aurelio Carrero | 30db23c | 2019-12-04 16:09:49 -0300 | [diff] [blame] | 145 | /** |
| 146 | * Returns a Json object that represents the DHCP L2 Relay stats. |
| 147 | * |
| 148 | * @param event DHCP L2 Relay event used for stats. |
| 149 | * @param title Describes the type of the received stats event (per subscriber or global). |
| 150 | */ |
| 151 | private JsonNode serializeStat(DhcpL2RelayEvent event, String title) { |
| 152 | ObjectMapper mapper = new ObjectMapper(); |
| 153 | ObjectNode statsEvent = mapper.createObjectNode(); |
| 154 | Long ts = Instant.now().getEpochSecond(); |
| 155 | |
| 156 | // metrics for global and per subscriber stats |
| 157 | ObjectNode metrics = mapper.createObjectNode(); |
| 158 | metrics.put(event.getCountersEntry().getKey(), event.getCountersEntry().getValue().longValue()); |
| 159 | |
| 160 | statsEvent.put(INSTANCE_ID, clusterService.getLocalNode().id().toString()); |
| 161 | statsEvent.put(TITLE, title); |
| 162 | statsEvent.put(TS, ts); |
| 163 | statsEvent.put(METRICS, metrics); |
| 164 | |
| 165 | // specific metrics for per subscriber stats |
| 166 | if (event.getSubscriberId() != null && event.subject() != null) { |
| 167 | String sn = deviceService.getDevice(event.subject().location().deviceId()).serialNumber(); |
| 168 | ObjectNode subscriberInfo = mapper.createObjectNode(); |
| 169 | |
| 170 | statsEvent.put(SERIAL_NUMBER, sn); |
| 171 | subscriberInfo.put(SUBSCRIBER_ID, event.getSubscriberId()); |
| 172 | subscriberInfo.put(CONNECT_POINT, event.subject().location().toString()); |
| 173 | subscriberInfo.put(MAC_ADDRESS, event.subject().macAddress().toString()); |
| 174 | |
| 175 | statsEvent.put(SUBSCRIBER_INFO, subscriberInfo); |
| 176 | } |
| 177 | |
| 178 | return statsEvent; |
| 179 | } |
| 180 | |
Jonathan Hart | 2aad779 | 2018-07-31 15:09:17 -0400 | [diff] [blame] | 181 | private class InternalDhcpL2RelayListener implements |
| 182 | DhcpL2RelayListener { |
| 183 | |
| 184 | @Override |
| 185 | public void event(DhcpL2RelayEvent dhcpL2RelayEvent) { |
| 186 | handle(dhcpL2RelayEvent); |
| 187 | } |
| 188 | } |
| 189 | } |