2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.netconfsimulator.kafka;
23 import lombok.extern.slf4j.Slf4j;
24 import org.apache.kafka.clients.consumer.Consumer;
25 import org.apache.kafka.clients.consumer.ConsumerRecords;
26 import org.apache.kafka.common.TopicPartition;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.kafka.core.ConsumerFactory;
29 import org.springframework.stereotype.Service;
31 import java.time.Instant;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
38 public class StoreService {
40 private static final String CONFIG_TOPIC = "config";
41 private static final long CONSUMING_DURATION_IN_MS = 1000;
43 private ConsumerFactory<String, String> consumerFactory;
44 static final List<String> TOPICS_TO_SUBSCRIBE = Collections.singletonList(CONFIG_TOPIC);
47 StoreService(ConsumerFactory<String, String> consumerFactory) {
48 this.consumerFactory = consumerFactory;
51 List<MessageDTO> getAllMessages() {
52 List<MessageDTO> messages = new ArrayList<>();
53 String clientID = Long.toString(Instant.now().getEpochSecond());
54 try (Consumer<String, String> consumer = consumerFactory.createConsumer(clientID, clientID)) {
55 consumer.subscribe(TOPICS_TO_SUBSCRIBE);
56 ConsumerRecords<String, String> consumerRecords = consumer.poll(CONSUMING_DURATION_IN_MS);
57 consumerRecords.forEach(
59 messages.add(new MessageDTO(consumerRecord.timestamp(), consumerRecord.value())));
60 log.debug(String.format("consumed %d messages", consumerRecords.count()));
65 List<MessageDTO> getLastMessages(long offset) {
66 List<MessageDTO> messages = new ArrayList<>();
67 try (Consumer<String, String> consumer = createConsumer(offset)) {
68 ConsumerRecords<String, String> consumerRecords = consumer.poll(CONSUMING_DURATION_IN_MS);
69 consumerRecords.forEach(consumerRecord ->
70 messages.add(new MessageDTO(consumerRecord.timestamp(), consumerRecord.value())));
75 private Consumer<String, String> createConsumer(long offsetFromLastIndex) {
76 String clientID = Long.toString(Instant.now().getEpochSecond());
77 Consumer<String, String> consumer = consumerFactory.createConsumer(clientID, clientID);
78 consumer.subscribe(TOPICS_TO_SUBSCRIBE);
79 seekConsumerTo(consumer, offsetFromLastIndex);
83 private void seekConsumerTo(Consumer<String, String> consumer, long offsetFromLastIndex) {
84 consumer.seekToEnd(consumer.assignment());
85 consumer.poll(CONSUMING_DURATION_IN_MS);
86 TopicPartition topicPartition = consumer.assignment().iterator().next();
87 long topicCurrentSize = consumer.position(topicPartition);
88 long indexToSeek = offsetFromLastIndex > topicCurrentSize ? 0 : topicCurrentSize - offsetFromLastIndex;
89 consumer.seek(topicPartition, indexToSeek);