Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [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.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 23 | import org.osgi.service.component.annotations.Activate; |
| 24 | import org.osgi.service.component.annotations.Component; |
| 25 | import org.osgi.service.component.annotations.Deactivate; |
| 26 | import org.osgi.service.component.annotations.Reference; |
| 27 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.device.DeviceEvent; |
| 30 | import org.onosproject.net.device.DeviceListener; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.device.PortStatistics; |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 34 | import org.opencord.kafka.EventBusService; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.time.Instant; |
| 39 | import java.util.Iterator; |
| 40 | import java.util.List; |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Listens for access device events and pushes them on a Kafka bus. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | public class DeviceKafkaIntegration { |
| 48 | |
| 49 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 52 | protected EventBusService eventBusService; |
| 53 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 55 | protected DeviceService deviceService; |
| 56 | |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 57 | protected final DeviceListener listener = new InternalDeviceListener(); |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 58 | |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 59 | protected static final String TOPIC = "onos.kpis"; |
| 60 | protected static final String PORT_EVENT_TOPIC = "onos.events.port"; |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 61 | |
| 62 | // event fields |
| 63 | private static final String TIMESTAMP = "timestamp"; |
| 64 | private static final String DEVICE_ID = "deviceId"; |
| 65 | private static final String PORTS = "ports"; |
| 66 | private static final String PORT_ID = "portId"; |
| 67 | private static final String PKT_RX = "pktRx"; |
| 68 | private static final String PKT_TX = "pktTx"; |
| 69 | private static final String BYTES_RX = "bytesRx"; |
| 70 | private static final String BYTES_TX = "bytesTx"; |
| 71 | private static final String PKT_RX_DROP = "pktRxDrp"; |
| 72 | private static final String PKT_TX_DROP = "pktTxDrp"; |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 73 | private static final String ENABLED = "enabled"; |
| 74 | private static final String SPEED = "speed"; |
| 75 | private static final String TYPE = "type"; |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 76 | |
| 77 | @Activate |
| 78 | public void activate() { |
| 79 | deviceService.addListener(listener); |
| 80 | log.info("Started"); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | public void deactivate() { |
| 85 | deviceService.removeListener(listener); |
| 86 | log.info("Stopped"); |
| 87 | } |
| 88 | |
| 89 | private void handle(List<PortStatistics> stats, DeviceId deviceId) { |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 90 | eventBusService.send(TOPIC, serializeStats(stats, deviceId)); |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 93 | private void handlePortUpdate(Port port, DeviceId deviceId) { |
| 94 | eventBusService.send(PORT_EVENT_TOPIC, serializePort(port, deviceId)); |
| 95 | } |
| 96 | |
| 97 | private JsonNode serializeStats(List<PortStatistics> stats, DeviceId deviceId) { |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 98 | |
| 99 | ObjectMapper mapper = new ObjectMapper(); |
| 100 | ObjectNode kpis = mapper.createObjectNode(); |
| 101 | ArrayNode ports = mapper.createArrayNode(); |
| 102 | |
| 103 | for (Iterator<PortStatistics> i = stats.iterator(); i.hasNext();) { |
| 104 | PortStatistics stat = i.next(); |
| 105 | |
| 106 | ObjectNode port = mapper.createObjectNode(); |
| 107 | port.put(PORT_ID, stat.portNumber().toString()); |
| 108 | port.put(PKT_RX, stat.packetsReceived()); |
| 109 | port.put(PKT_TX, stat.packetsSent()); |
| 110 | port.put(BYTES_RX, stat.bytesReceived()); |
| 111 | port.put(BYTES_TX, stat.bytesSent()); |
| 112 | port.put(PKT_RX_DROP, stat.packetsRxDropped()); |
| 113 | port.put(PKT_TX_DROP, stat.packetsTxDropped()); |
| 114 | |
| 115 | ports.add(port); |
| 116 | } |
| 117 | |
| 118 | kpis.put(TIMESTAMP, Instant.now().toString()); |
| 119 | kpis.put(PORTS, ports); |
| 120 | kpis.put(DEVICE_ID, deviceId.toString()); |
| 121 | |
| 122 | return kpis; |
| 123 | } |
| 124 | |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 125 | private JsonNode serializePort(Port port, DeviceId deviceId) { |
| 126 | |
| 127 | ObjectMapper mapper = new ObjectMapper(); |
| 128 | ObjectNode update = mapper.createObjectNode(); |
| 129 | |
| 130 | update.put(TIMESTAMP, Instant.now().toString()); |
| 131 | update.put(DEVICE_ID, deviceId.toString()); |
| 132 | update.put(PORT_ID, port.number().toString()); |
| 133 | update.put(ENABLED, port.isEnabled()); |
| 134 | update.put(SPEED, port.portSpeed()); |
| 135 | update.put(TYPE, port.type().toString()); |
| 136 | |
| 137 | return update; |
| 138 | } |
| 139 | |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 140 | private class InternalDeviceListener implements |
| 141 | DeviceListener { |
| 142 | |
| 143 | @Override |
| 144 | public void event(DeviceEvent deviceEvent) { |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 145 | final DeviceId deviceId; |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 146 | |
| 147 | if (deviceEvent.subject().manufacturer().contains("VOLTHA")) { |
| 148 | // TODO check the NNI port instead |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | log.trace("Got DeviceEvent: " + deviceEvent.type()); |
| 153 | switch (deviceEvent.type()) { |
| 154 | case PORT_STATS_UPDATED: |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 155 | deviceId = deviceEvent.subject().id(); |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 156 | final List<PortStatistics> stats = deviceService.getPortStatistics(deviceId); |
| 157 | handle(stats, deviceId); |
| 158 | break; |
Scott Baker | 900b48b | 2019-03-18 16:59:31 -0700 | [diff] [blame] | 159 | case PORT_UPDATED: |
| 160 | deviceId = deviceEvent.subject().id(); |
| 161 | final Port port = deviceEvent.port(); |
| 162 | handlePortUpdate(port, deviceId); |
| 163 | break; |
Matteo Scandolo | 8a01583 | 2018-10-09 14:54:11 -0700 | [diff] [blame] | 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |