5fddff5a2af1223a377f4af66310a93216b547bc
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.netconfsimulator.kafka;
22
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;
30
31 import java.time.Instant;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35
36 @Slf4j
37 @Service
38 public class StoreService {
39
40     private static final String CONFIG_TOPIC = "config";
41     private static final long CONSUMING_DURATION_IN_MS = 1000;
42
43     private ConsumerFactory<String, String> consumerFactory;
44     static final List<String> TOPICS_TO_SUBSCRIBE = Collections.singletonList(CONFIG_TOPIC);
45
46     @Autowired
47     StoreService(ConsumerFactory<String, String> consumerFactory) {
48         this.consumerFactory = consumerFactory;
49     }
50
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(
58                 consumerRecord ->
59                     messages.add(new MessageDTO(consumerRecord.timestamp(), consumerRecord.value())));
60             log.debug(String.format("consumed %d messages", consumerRecords.count()));
61             }
62         return messages;
63     }
64
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())));
71         }
72         return messages;
73     }
74
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);
80         return consumer;
81     }
82
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);
90     }
91 }