blob: 488a338b02b8209c4581b4438758fbdd543a148b [file] [log] [blame]
Matteo Scandolo8a015832018-10-09 14:54:11 -07001/*
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
17package org.opencord.kafka.integrations;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.device.DeviceEvent;
30import org.onosproject.net.device.DeviceListener;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.device.PortStatistics;
33import org.opencord.kafka.EventBusService;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import java.time.Instant;
38import java.util.Iterator;
39import java.util.List;
40
41
42/**
43 * Listens for access device events and pushes them on a Kafka bus.
44 */
45@Component(immediate = true)
46public class DeviceKafkaIntegration {
47
48 public Logger log = LoggerFactory.getLogger(getClass());
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected EventBusService eventBusService;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected DeviceService deviceService;
55
56 private final DeviceListener listener = new InternalDeviceListener();
57
58 private static final String TOPIC = "onos.kpis";
59
60 // event fields
61 private static final String TIMESTAMP = "timestamp";
62 private static final String DEVICE_ID = "deviceId";
63 private static final String PORTS = "ports";
64 private static final String PORT_ID = "portId";
65 private static final String PKT_RX = "pktRx";
66 private static final String PKT_TX = "pktTx";
67 private static final String BYTES_RX = "bytesRx";
68 private static final String BYTES_TX = "bytesTx";
69 private static final String PKT_RX_DROP = "pktRxDrp";
70 private static final String PKT_TX_DROP = "pktTxDrp";
71
72
73 @Activate
74 public void activate() {
75 deviceService.addListener(listener);
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 deviceService.removeListener(listener);
82 log.info("Stopped");
83 }
84
85 private void handle(List<PortStatistics> stats, DeviceId deviceId) {
86 eventBusService.send(TOPIC, serialize(stats, deviceId));
87 }
88
89 private JsonNode serialize(List<PortStatistics> stats, DeviceId deviceId) {
90
91 ObjectMapper mapper = new ObjectMapper();
92 ObjectNode kpis = mapper.createObjectNode();
93 ArrayNode ports = mapper.createArrayNode();
94
95 for (Iterator<PortStatistics> i = stats.iterator(); i.hasNext();) {
96 PortStatistics stat = i.next();
97
98 ObjectNode port = mapper.createObjectNode();
99 port.put(PORT_ID, stat.portNumber().toString());
100 port.put(PKT_RX, stat.packetsReceived());
101 port.put(PKT_TX, stat.packetsSent());
102 port.put(BYTES_RX, stat.bytesReceived());
103 port.put(BYTES_TX, stat.bytesSent());
104 port.put(PKT_RX_DROP, stat.packetsRxDropped());
105 port.put(PKT_TX_DROP, stat.packetsTxDropped());
106
107 ports.add(port);
108 }
109
110 kpis.put(TIMESTAMP, Instant.now().toString());
111 kpis.put(PORTS, ports);
112 kpis.put(DEVICE_ID, deviceId.toString());
113
114 return kpis;
115 }
116
117 private class InternalDeviceListener implements
118 DeviceListener {
119
120 @Override
121 public void event(DeviceEvent deviceEvent) {
122
123 if (deviceEvent.subject().manufacturer().contains("VOLTHA")) {
124 // TODO check the NNI port instead
125 return;
126 }
127
128 log.trace("Got DeviceEvent: " + deviceEvent.type());
129 switch (deviceEvent.type()) {
130 case PORT_STATS_UPDATED:
131 final DeviceId deviceId = deviceEvent.subject().id();
132 final List<PortStatistics> stats = deviceService.getPortStatistics(deviceId);
133 handle(stats, deviceId);
134 break;
135 default:
136 break;
137 }
138 }
139 }
140}