Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [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 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 18 | import org.influxdb.InfluxDB; |
| 19 | import org.influxdb.InfluxDBFactory; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 20 | import org.influxdb.dto.BatchPoints; |
| 21 | import org.influxdb.dto.Point; |
| 22 | import org.onlab.packet.TpPort; |
| 23 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService; |
| 25 | import org.onosproject.openstacktelemetry.api.InfluxRecord; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 26 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacktelemetry.api.config.InfluxDbTelemetryConfig; |
| 28 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -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; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 39 | /** |
| 40 | * InfluxDB telemetry manager. |
| 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 42 | @Component(immediate = true, service = InfluxDbTelemetryAdminService.class) |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 43 | public class InfluxDbTelemetryManager implements InfluxDbTelemetryAdminService { |
| 44 | |
| 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 47 | private static final String FLOW_TYPE = "flowType"; |
| 48 | private static final String DEVICE_ID = "deviceId"; |
| 49 | private static final String INPUT_INTERFACE_ID = "inputInterfaceId"; |
| 50 | private static final String OUTPUT_INTERFACE_ID = "outputInterfaceId"; |
| 51 | |
| 52 | private static final String VLAN_ID = "vlanId"; |
| 53 | private static final String VXLAN_ID = "vxlanId"; |
| 54 | private static final String SRC_IP = "srcIp"; |
| 55 | private static final String DST_IP = "dstIp"; |
| 56 | private static final String SRC_PORT = "srcPort"; |
| 57 | private static final String DST_PORT = "dstPort"; |
| 58 | private static final String PROTOCOL = "protocol"; |
| 59 | private static final String SRC_MAC = "srcMac"; |
| 60 | private static final String DST_MAC = "dstMac"; |
| 61 | |
| 62 | private static final String STARTUP_TIME = "startupTime"; |
| 63 | private static final String FST_PKT_ARR_TIME = "fstPktArrTime"; |
| 64 | private static final String LST_PKT_OFFSET = "lstPktOffset"; |
| 65 | private static final String PREV_ACC_BYTES = "prevAccBytes"; |
| 66 | private static final String PREV_ACC_PKTS = "prevAccPkts"; |
| 67 | private static final String CURR_ACC_BYTES = "currAccBytes"; |
| 68 | private static final String CURR_ACC_PKTS = "currAccPkts"; |
| 69 | private static final String ERROR_PKTS = "errorPkts"; |
| 70 | private static final String DROP_PKTS = "dropPkts"; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 73 | protected OpenstackTelemetryService openstackTelemetryService; |
| 74 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 75 | private static final String INFLUX_PROTOCOL = "http"; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 76 | private InfluxDB producer = null; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 77 | private String database = null; |
| 78 | private String measurement = null; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 79 | |
| 80 | @Activate |
| 81 | protected void activate() { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 82 | |
| 83 | openstackTelemetryService.addTelemetryService(this); |
| 84 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 85 | log.info("Started"); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | protected void deactivate() { |
| 90 | stop(); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 91 | |
| 92 | openstackTelemetryService.removeTelemetryService(this); |
| 93 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 94 | log.info("Stopped"); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void start(TelemetryConfig config) { |
| 99 | if (producer != null) { |
| 100 | log.info("InfluxDB producer has already been started"); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | InfluxDbTelemetryConfig influxDbConfig = (InfluxDbTelemetryConfig) config; |
| 105 | |
| 106 | StringBuilder influxDbServerBuilder = new StringBuilder(); |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 107 | influxDbServerBuilder.append(INFLUX_PROTOCOL); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 108 | influxDbServerBuilder.append(":"); |
| 109 | influxDbServerBuilder.append("//"); |
| 110 | influxDbServerBuilder.append(influxDbConfig.address()); |
| 111 | influxDbServerBuilder.append(":"); |
| 112 | influxDbServerBuilder.append(influxDbConfig.port()); |
| 113 | |
| 114 | producer = InfluxDBFactory.connect(influxDbServerBuilder.toString(), |
| 115 | influxDbConfig.username(), influxDbConfig.password()); |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 116 | database = influxDbConfig.database(); |
| 117 | measurement = influxDbConfig.measurement(); |
| 118 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 119 | log.info("InfluxDB producer has Started"); |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 120 | |
| 121 | createDB(); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void stop() { |
| 126 | if (producer != null) { |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 127 | producer.close(); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 128 | producer = null; |
| 129 | } |
| 130 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 131 | log.info("InfluxDB producer has Stopped"); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void restart(TelemetryConfig config) { |
| 136 | stop(); |
| 137 | start(config); |
| 138 | } |
| 139 | |
| 140 | @Override |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 141 | public void publish(InfluxRecord<String, Set<FlowInfo>> record) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 142 | if (producer == null) { |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 143 | log.debug("InfluxDB telemetry service has not been enabled!"); |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 144 | return; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 145 | } |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 146 | |
| 147 | if (record.flowInfos().size() == 0) { |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 148 | log.debug("No record to publish"); |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
| 152 | log.debug("Publish {} stats records to InfluxDB", record.flowInfos().size()); |
| 153 | |
| 154 | BatchPoints batchPoints = BatchPoints.database(database).build(); |
| 155 | |
| 156 | for (FlowInfo flowInfo: record.flowInfos()) { |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 157 | Point.Builder pointBuilder = Point |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 158 | .measurement((measurement == null) ? record.measurement() : measurement) |
| 159 | .tag(FLOW_TYPE, String.valueOf(flowInfo.flowType())) |
| 160 | .tag(DEVICE_ID, flowInfo.deviceId().toString()) |
| 161 | .tag(INPUT_INTERFACE_ID, String.valueOf(flowInfo.inputInterfaceId())) |
| 162 | .tag(OUTPUT_INTERFACE_ID, String.valueOf(flowInfo.outputInterfaceId())) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 163 | .tag(VXLAN_ID, String.valueOf(flowInfo.vxlanId())) |
| 164 | .tag(SRC_IP, flowInfo.srcIp().toString()) |
| 165 | .tag(DST_IP, flowInfo.dstIp().toString()) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 166 | .tag(DST_PORT, getTpPort(flowInfo.dstPort())) |
| 167 | .tag(PROTOCOL, String.valueOf(flowInfo.protocol())) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 168 | .addField(STARTUP_TIME, flowInfo.statsInfo().startupTime()) |
| 169 | .addField(FST_PKT_ARR_TIME, flowInfo.statsInfo().fstPktArrTime()) |
| 170 | .addField(LST_PKT_OFFSET, flowInfo.statsInfo().lstPktOffset()) |
| 171 | .addField(PREV_ACC_BYTES, flowInfo.statsInfo().prevAccBytes()) |
| 172 | .addField(PREV_ACC_PKTS, flowInfo.statsInfo().prevAccPkts()) |
| 173 | .addField(CURR_ACC_BYTES, flowInfo.statsInfo().currAccBytes()) |
| 174 | .addField(CURR_ACC_PKTS, flowInfo.statsInfo().currAccPkts()) |
| 175 | .addField(ERROR_PKTS, flowInfo.statsInfo().errorPkts()) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 176 | .addField(DROP_PKTS, flowInfo.statsInfo().dropPkts()); |
| 177 | |
| 178 | if (flowInfo.vlanId() != null) { |
| 179 | pointBuilder.tag(VLAN_ID, flowInfo.vlanId().toString()); |
| 180 | } |
| 181 | |
| 182 | if (flowInfo.srcPort() != null) { |
| 183 | pointBuilder.tag(SRC_PORT, getTpPort(flowInfo.srcPort())); |
| 184 | } |
| 185 | |
| 186 | if (flowInfo.dstPort() != null) { |
| 187 | pointBuilder.tag(DST_PORT, getTpPort(flowInfo.dstPort())); |
| 188 | } |
| 189 | |
| 190 | batchPoints.point(pointBuilder.build()); |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 191 | } |
| 192 | producer.write(batchPoints); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public boolean isRunning() { |
| 197 | return producer != null; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 198 | } |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 199 | |
| 200 | private void createDB() { |
| 201 | if (producer.databaseExists(database)) { |
| 202 | log.debug("Database {} is already created", database); |
| 203 | } else { |
| 204 | producer.createDatabase(database); |
| 205 | log.debug("Database {} is created", database); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private String getTpPort(TpPort tpPort) { |
| 210 | if (tpPort == null) { |
| 211 | return ""; |
| 212 | } |
| 213 | return tpPort.toString(); |
| 214 | } |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 215 | } |