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