William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 1 | /* |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 2 | * Copyright 2018- Cisco |
| 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. |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 15 | * |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 16 | */ |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 17 | package kafka; |
| 18 | import org.apache.kafka.clients.consumer.*; |
| 19 | import org.apache.kafka.clients.consumer.Consumer; |
| 20 | import org.apache.kafka.common.serialization.LongDeserializer; |
| 21 | import org.apache.kafka.common.serialization.StringDeserializer; |
| 22 | import org.apache.kafka.common.KafkaException; |
| 23 | import java.util.Collections; |
| 24 | import java.util.Properties; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | import org.slf4j.Marker; |
| 28 | import org.slf4j.MarkerFactory; |
| 29 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 30 | import java.time.Instant; |
| 31 | import java.time.Duration; |
| 32 | |
| 33 | import com.google.gson.JsonSyntaxException; |
| 34 | |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 35 | import javax.xml.ws.http.HTTPException; |
| 36 | |
| 37 | import java.util.concurrent.TimeUnit; |
| 38 | |
| 39 | import ves.*; |
| 40 | import config.Config; |
| 41 | |
| 42 | public class VolthaKafkaConsumer { |
| 43 | |
| 44 | private final Logger logger = LoggerFactory.getLogger("VolthaKafkaConsumer"); |
| 45 | private final String dataMarkerText = "DATA"; |
| 46 | private final Marker dataMarker = MarkerFactory.getMarker(dataMarkerText); |
| 47 | |
| 48 | private KafkaConsumer<Long, String> consumer; |
| 49 | |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 50 | private KafkaConsumerType type; |
| 51 | |
| 52 | public VolthaKafkaConsumer(KafkaConsumerType type) { |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 53 | logger.debug("VolthaKafkaConsumer constructor called"); |
| 54 | initVesAgent(); |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 55 | this.type = type; |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 56 | try { |
| 57 | consumer = createConsumer(); |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 58 | } catch (Exception e) { |
| 59 | logger.error("Error with Kafka: ", e); |
| 60 | logger.error("Retrying in 15 seconds."); |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 61 | //Don't try to resolve it here. Try again in the thread loo, in case this is a temporal issue |
| 62 | } |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private void initVesAgent() { |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 66 | VesAgent.initVes(); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private KafkaConsumer<Long, String> createConsumer() { |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 70 | logger.debug("Creating Kafka Consumer"); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 71 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 72 | String kafkaAddress = Config.getKafkaAddress() + ":" + Config.getKafkaPort(); |
| 73 | final Properties props = new Properties(); |
| 74 | props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, |
| 75 | kafkaAddress); |
| 76 | props.put(ConsumerConfig.GROUP_ID_CONFIG, |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 77 | "VesAgent"); |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 78 | props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, |
| 79 | LongDeserializer.class.getName()); |
| 80 | props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, |
| 81 | StringDeserializer.class.getName()); |
| 82 | props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, |
| 83 | false); |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 84 | // Create the consumer using props. |
| 85 | final KafkaConsumer<Long, String> consumer = |
| 86 | new KafkaConsumer<>(props); |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 87 | // Subscribe to the topic. |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 88 | switch (type) { |
| 89 | case ALARMS: |
| 90 | consumer.subscribe(Collections.singletonList(Config.getKafkaAlarmsTopic())); |
| 91 | break; |
| 92 | case KPIS: |
| 93 | consumer.subscribe(Collections.singletonList(Config.getKafkaKpisTopic())); |
| 94 | break; |
| 95 | } |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 96 | return consumer; |
| 97 | } |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 98 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 99 | public void runConsumer() throws InterruptedException { |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 100 | |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 101 | logger.debug("Starting Kafka Consumer"); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 102 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 103 | while (true) { |
| 104 | ConsumerRecords<Long, String> consumerRecords; |
| 105 | try { |
| 106 | if (consumer == null) { |
| 107 | this.consumer = createConsumer(); |
| 108 | } |
| 109 | consumerRecords = consumer.poll(20000); |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 110 | } catch (Exception e) { |
| 111 | logger.error("Error with kafka: ", e); |
| 112 | logger.error("Retrying in 15 seconds."); |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 113 | consumer = null; |
| 114 | TimeUnit.SECONDS.sleep(15); |
| 115 | continue; |
| 116 | } |
| 117 | logger.info("{} Records retrieved from poll.", consumerRecords.count()); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 118 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 119 | boolean commit = true; |
| 120 | try { |
| 121 | consumerRecords.forEach(record -> { |
| 122 | Instant start = Instant.now(); |
| 123 | logger.info(dataMarker, "Consumer Record:({}, {}, {}, {})\n", |
| 124 | record.key(), record.value(), |
| 125 | record.partition(), record.offset()); |
| 126 | logger.info("Attempting to send data to VES"); |
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 127 | boolean success = VesAgent.sendToVES(type, record.value()); |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 128 | if (!success) { |
| 129 | throw new HTTPException(0); |
| 130 | } else { |
| 131 | Instant finish = Instant.now(); |
| 132 | logger.info("Sent Ves Message. Took " + Duration.between(start, finish).toMillis() + " Milliseconds."); |
| 133 | } |
| 134 | }); |
| 135 | } catch (HTTPException e) { |
| 136 | logger.info("Ves message failed. Going back to polling."); |
| 137 | commit = false; |
| 138 | } catch (JsonSyntaxException e) { |
| 139 | logger.error("Json Syntax Exception: ", e); |
| 140 | } |
| 141 | if (commit) { |
| 142 | consumer.commitAsync(); |
| 143 | } |
| 144 | } |
| 145 | //consumer.close(); |
| 146 | //logger.debug("DONE"); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | } |