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