blob: 7a4487253c452dcbd26e21785825973435ee843c [file] [log] [blame]
Jonathan Hart501f7882018-07-24 14:39:57 -07001/*
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 */
16package org.opencord.kafka.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.apache.kafka.clients.producer.KafkaProducer;
26import org.apache.kafka.clients.producer.ProducerRecord;
27import org.apache.kafka.common.serialization.StringSerializer;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30import org.onosproject.net.config.ConfigFactory;
31import org.onosproject.net.config.NetworkConfigEvent;
32import org.onosproject.net.config.NetworkConfigListener;
33import org.onosproject.net.config.NetworkConfigRegistry;
34import org.onosproject.net.config.basics.SubjectFactories;
35import org.opencord.kafka.EventBusService;
36import org.slf4j.Logger;
37
38import java.util.Properties;
39import java.util.concurrent.ExecutorService;
40
41import static com.google.common.base.Preconditions.checkNotNull;
42import static java.util.concurrent.Executors.newSingleThreadExecutor;
43import static org.onlab.util.Tools.groupedThreads;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * Sends events to a Kafka event bus.
48 */
49@Service
50@Component(immediate = true)
51public class KafkaIntegration implements EventBusService {
52
53 private final Logger log = getLogger(getClass());
54 private static final Class<KafkaConfig>
55 KAFKA_CONFIG_CLASS = KafkaConfig.class;
56
57 private static final String APP_NAME = "org.opencord.kafka";
58 private ApplicationId appId;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected CoreService coreService;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected NetworkConfigRegistry configRegistry;
65
66 private static StringSerializer stringSerializer = new StringSerializer();
67
68 private KafkaProducer<String, String> kafkaProducer;
69
70 private InternalNetworkConfigListener configListener =
71 new InternalNetworkConfigListener();
72
73 private final ExecutorService executor = newSingleThreadExecutor(
74 groupedThreads(this.getClass().getSimpleName(), "events", log));
75
76 private ConfigFactory<ApplicationId, KafkaConfig> kafkaConfigFactory =
77 new ConfigFactory<ApplicationId, KafkaConfig>(
78 SubjectFactories.APP_SUBJECT_FACTORY, KAFKA_CONFIG_CLASS,
79 "kafka") {
80 @Override
81 public KafkaConfig createConfig() {
82 return new KafkaConfig();
83 }
84 };
85
86 private static final String BOOTSTRAP_SERVERS = "bootstrap.servers";
87 private static final String RETRIES = "retries";
88 private static final String RECONNECT_BACKOFF = "reconnect.backoff.ms";
89 private static final String INFLIGHT_REQUESTS =
90 "max.in.flight.requests.per.connection";
91 private static final String ACKS = "acks";
92 private static final String KEY_SERIALIZER = "key.serializer";
93 private static final String VALUE_SERIALIZER = "value.serializer";
94 private static final String STRING_SERIALIZER =
95 stringSerializer.getClass().getCanonicalName();
96
97 private static final String TIMESTAMP = "timestamp";
98
99 @Activate
100 public void activate() {
101 appId = coreService.registerApplication(APP_NAME);
102 configRegistry.registerConfigFactory(kafkaConfigFactory);
103 configRegistry.addListener(configListener);
104
105 configure();
106
107 log.info("Started");
108 }
109
110 @Deactivate
111 public void deactivate() {
112 configRegistry.removeListener(configListener);
113 configRegistry.unregisterConfigFactory(kafkaConfigFactory);
114
115 executor.shutdownNow();
116
117 shutdownKafka();
118 log.info("Stopped");
119 }
120
121 private void configure() {
122 KafkaConfig config =
123 configRegistry.getConfig(appId, KAFKA_CONFIG_CLASS);
124 if (config == null) {
125 log.info("Kafka configuration not present");
126 return;
127 }
128 configure(config);
129 }
130
131 private void configure(KafkaConfig config) {
132 checkNotNull(config);
133
134 Properties properties = new Properties();
135 properties.put(BOOTSTRAP_SERVERS, config.getBootstrapServers());
136 properties.put(RETRIES, config.getRetries());
137 properties.put(RECONNECT_BACKOFF, config.getReconnectBackoff());
138 properties.put(INFLIGHT_REQUESTS, config.getInflightRequests());
139 properties.put(ACKS, config.getAcks());
140 properties.put(KEY_SERIALIZER, STRING_SERIALIZER);
141 properties.put(VALUE_SERIALIZER, STRING_SERIALIZER);
142
143 startKafka(properties);
144 }
145
146 private void unconfigure() {
147 shutdownKafka();
148 }
149
150 private void startKafka(Properties properties) {
151 shutdownKafka();
152
153 // Kafka client doesn't play nice with the default OSGi classloader
154 // This workaround temporarily changes the thread's classloader so that
155 // the Kafka client can load the serializer classes.
156 ClassLoader original = Thread.currentThread().getContextClassLoader();
157 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
158 try {
159 kafkaProducer = new KafkaProducer<>(properties);
160 } finally {
161 Thread.currentThread().setContextClassLoader(original);
162 }
163 }
164
165 private void shutdownKafka() {
166 if (kafkaProducer != null) {
167 kafkaProducer.close();
168 kafkaProducer = null;
169 }
170 }
171
172 private void logException(Exception e) {
173 if (e != null) {
174 log.error("Exception while sending to Kafka", e);
175 }
176 }
177
178 @Override
179 public void send(String topic, JsonNode data) {
180 if (kafkaProducer == null) {
181 return;
182 }
183
184 if (log.isTraceEnabled()) {
185 log.trace("Sending event to Kafka: {}", data.toString());
186 }
187
188 kafkaProducer.send(new ProducerRecord<>(topic, data.toString()),
189 (r, e) -> logException(e));
190 }
191
192 private class InternalNetworkConfigListener implements NetworkConfigListener {
193
194 @Override
195 public void event(NetworkConfigEvent event) {
196 switch (event.type()) {
197 case CONFIG_ADDED:
198 case CONFIG_UPDATED:
199 configure((KafkaConfig) event.config().get());
200 break;
201 case CONFIG_REMOVED:
202 unconfigure();
203 break;
204 case CONFIG_REGISTERED:
205 case CONFIG_UNREGISTERED:
206 default:
207 break;
208 }
209 }
210
211 @Override
212 public boolean isRelevant(NetworkConfigEvent event) {
213 return event.configClass().equals(KAFKA_CONFIG_CLASS);
214 }
215 }
216}