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