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 | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.Maps; |
| 19 | import com.google.common.collect.Sets; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 20 | import org.apache.kafka.clients.producer.KafkaProducer; |
| 21 | import org.apache.kafka.clients.producer.Producer; |
| 22 | import org.apache.kafka.clients.producer.ProducerRecord; |
| 23 | import org.apache.kafka.clients.producer.RecordMetadata; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 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.KafkaTelemetryAdminService; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 26 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 27 | import org.onosproject.openstacktelemetry.api.TelemetryCodec; |
| 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.KafkaTelemetryConfig; |
| 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.nio.ByteBuffer; |
| 40 | import java.util.Map; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 41 | import java.util.Properties; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 42 | import java.util.Set; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 43 | import java.util.concurrent.Future; |
| 44 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 45 | import static org.onosproject.openstacktelemetry.api.Constants.KAFKA_SCHEME; |
| 46 | import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType.KAFKA; |
| 47 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.fromTelemetryConfig; |
| 48 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 49 | /** |
| 50 | * Kafka telemetry manager. |
| 51 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | @Component(immediate = true, service = KafkaTelemetryAdminService.class) |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 53 | public class KafkaTelemetryManager implements KafkaTelemetryAdminService { |
| 54 | |
| 55 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 57 | private static final String CODEC_PREFIX = "org.onosproject.openstacktelemetry.codec."; |
| 58 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 59 | private static final String BOOTSTRAP_SERVERS = "bootstrap.servers"; |
| 60 | private static final String RETRIES = "retries"; |
| 61 | private static final String ACKS = "acks"; |
| 62 | private static final String BATCH_SIZE = "batch.size"; |
| 63 | private static final String LINGER_MS = "linger.ms"; |
| 64 | private static final String MEMORY_BUFFER = "buffer.memory"; |
| 65 | private static final String KEY_SERIALIZER = "key.serializer"; |
| 66 | private static final String VALUE_SERIALIZER = "value.serializer"; |
| 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 69 | protected OpenstackTelemetryService openstackTelemetryService; |
| 70 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 72 | protected TelemetryConfigService telemetryConfigService; |
| 73 | |
| 74 | private Map<String, Producer<String, byte[]>> producers = Maps.newConcurrentMap(); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 75 | |
| 76 | @Activate |
| 77 | protected void activate() { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 78 | |
| 79 | openstackTelemetryService.addTelemetryService(this); |
| 80 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 81 | log.info("Started"); |
| 82 | } |
| 83 | |
| 84 | @Deactivate |
| 85 | protected void deactivate() { |
| 86 | stop(); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 87 | |
| 88 | openstackTelemetryService.removeTelemetryService(this); |
| 89 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 90 | log.info("Stopped"); |
| 91 | } |
| 92 | |
| 93 | @Override |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 94 | public void start() { |
| 95 | telemetryConfigService.getConfigsByType(KAFKA).forEach(c -> { |
| 96 | KafkaTelemetryConfig kafkaConfig = fromTelemetryConfig(c); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 97 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 98 | if (kafkaConfig != null && !c.name().equals(KAFKA_SCHEME) && c.enabled()) { |
| 99 | StringBuilder kafkaServerBuilder = new StringBuilder(); |
| 100 | kafkaServerBuilder.append(kafkaConfig.address()); |
| 101 | kafkaServerBuilder.append(":"); |
| 102 | kafkaServerBuilder.append(kafkaConfig.port()); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 103 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 104 | // Configure Kafka server properties |
| 105 | Properties prop = new Properties(); |
| 106 | prop.put(BOOTSTRAP_SERVERS, kafkaServerBuilder.toString()); |
| 107 | prop.put(RETRIES, kafkaConfig.retries()); |
| 108 | prop.put(ACKS, kafkaConfig.requiredAcks()); |
| 109 | prop.put(BATCH_SIZE, kafkaConfig.batchSize()); |
| 110 | prop.put(LINGER_MS, kafkaConfig.lingerMs()); |
| 111 | prop.put(MEMORY_BUFFER, kafkaConfig.memoryBuffer()); |
| 112 | prop.put(KEY_SERIALIZER, kafkaConfig.keySerializer()); |
| 113 | prop.put(VALUE_SERIALIZER, kafkaConfig.valueSerializer()); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 114 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 115 | producers.put(c.name(), new KafkaProducer<>(prop)); |
| 116 | } |
| 117 | }); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 118 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 119 | log.info("Kafka producer has Started"); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void stop() { |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 124 | if (!producers.isEmpty()) { |
| 125 | producers.values().forEach(Producer::close); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 126 | } |
| 127 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 128 | producers.clear(); |
| 129 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 130 | log.info("Kafka producer has Stopped"); |
| 131 | } |
| 132 | |
| 133 | @Override |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 134 | public void restart() { |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 135 | stop(); |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 136 | start(); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 140 | public Set<Future<RecordMetadata>> publish(Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 141 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 142 | if (producers == null || producers.isEmpty()) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 143 | log.debug("Kafka telemetry service has not been enabled!"); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 144 | return null; |
| 145 | } |
| 146 | |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 147 | log.debug("Send telemetry record to kafka server..."); |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 148 | Set<Future<RecordMetadata>> futureSet = Sets.newHashSet(); |
| 149 | producers.forEach((k, v) -> { |
| 150 | TelemetryConfig config = telemetryConfigService.getConfig(k); |
| 151 | KafkaTelemetryConfig kafkaConfig = |
| 152 | fromTelemetryConfig(config); |
| 153 | |
| 154 | try { |
| 155 | Class codecClazz = Class.forName(CODEC_PREFIX + kafkaConfig.codec()); |
| 156 | TelemetryCodec codec = (TelemetryCodec) codecClazz.newInstance(); |
| 157 | |
| 158 | ByteBuffer buffer = codec.encode(flowInfos); |
| 159 | ProducerRecord record = new ProducerRecord<>( |
| 160 | kafkaConfig.topic(), kafkaConfig.key(), buffer.array()); |
| 161 | futureSet.add(v.send(record)); |
| 162 | } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { |
| 163 | log.warn("Failed to send telemetry record due to {}", e); |
| 164 | } |
| 165 | }); |
| 166 | return futureSet; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 167 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 168 | |
| 169 | @Override |
| 170 | public boolean isRunning() { |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 171 | return !producers.isEmpty(); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 172 | } |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 173 | } |