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