2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.dmaap;
23 import com.google.common.collect.Iterables;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
28 import java.io.IOException;
29 import java.time.Duration;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.oransc.policyagent.clients.AsyncRestClient;
34 import org.oransc.policyagent.configuration.ApplicationConfig;
35 import org.oransc.policyagent.exceptions.ServiceException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Component;
44 * The class fetches incoming requests from DMAAP. It uses the timeout parameter
45 * that lets the MessageRouter keep the connection with the Kafka open until
46 * requests are sent in.
49 * this service will regularly check the configuration and start polling DMaaP
50 * if the configuration is added. If the DMaaP configuration is removed, then
51 * the service will stop polling and resume checking for configuration.
54 * Each received request is processed by {@link DmaapMessageHandler}.
57 public class DmaapMessageConsumer {
59 protected static final Duration TIME_BETWEEN_DMAAP_RETRIES = Duration.ofSeconds(10);
61 private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumer.class);
63 private final ApplicationConfig applicationConfig;
65 private DmaapMessageHandler dmaapMessageHandler = null;
67 @Value("${server.http-port}")
68 private int localServerHttpPort;
71 public DmaapMessageConsumer(ApplicationConfig applicationConfig) {
72 this.applicationConfig = applicationConfig;
76 * Starts the consumer. If there is a DMaaP configuration, it will start polling
77 * for messages. Otherwise it will check regularly for the configuration.
79 * @return the running thread, for test purposes.
81 public Thread start() {
82 Thread thread = new Thread(this::messageHandlingLoop);
87 private void messageHandlingLoop() {
88 while (!isStopped()) {
90 if (isDmaapConfigured()) {
91 Iterable<String> dmaapMsgs = fetchAllMessages();
92 if (dmaapMsgs != null && Iterables.size(dmaapMsgs) > 0) {
93 logger.debug("Fetched all the messages from DMAAP and will start to process the messages");
94 for (String msg : dmaapMsgs) {
99 sleep(TIME_BETWEEN_DMAAP_RETRIES); // wait for configuration
101 } catch (Exception e) {
102 logger.warn("{}", e.getMessage());
103 sleep(TIME_BETWEEN_DMAAP_RETRIES);
108 protected boolean isStopped() {
112 protected boolean isDmaapConfigured() {
113 String producerTopicUrl = applicationConfig.getDmaapProducerTopicUrl();
114 String consumerTopicUrl = applicationConfig.getDmaapConsumerTopicUrl();
115 return (!producerTopicUrl.isEmpty() && !consumerTopicUrl.isEmpty());
118 private static List<String> parseMessages(String jsonString) {
119 JsonArray arrayOfMessages = JsonParser.parseString(jsonString).getAsJsonArray();
120 List<String> result = new ArrayList<>();
121 for (JsonElement element : arrayOfMessages) {
122 if (element.isJsonPrimitive()) {
123 result.add(element.getAsString());
125 String messageAsString = element.toString();
126 result.add(messageAsString);
132 protected Iterable<String> fetchAllMessages() throws ServiceException, IOException {
133 String topicUrl = this.applicationConfig.getDmaapConsumerTopicUrl();
134 AsyncRestClient consumer = getMessageRouterConsumer();
135 ResponseEntity<String> response = consumer.getForEntity(topicUrl).block();
136 logger.debug("DMaaP consumer received {} : {}", response.getStatusCode(), response.getBody());
137 if (response.getStatusCode().is2xxSuccessful()) {
138 return parseMessages(response.getBody());
140 throw new ServiceException("Cannot fetch because of Error respons: " + response.getStatusCode().toString()
141 + " " + response.getBody());
145 private void processMsg(String msg) throws IOException {
146 logger.debug("Message Reveived from DMAAP : {}", msg);
147 getDmaapMessageHandler().handleDmaapMsg(msg);
150 protected DmaapMessageHandler getDmaapMessageHandler() throws IOException {
151 if (this.dmaapMessageHandler == null) {
152 String agentBaseUrl = "http://localhost:" + this.localServerHttpPort;
153 AsyncRestClient agentClient = new AsyncRestClient(agentBaseUrl);
154 AsyncRestClient producer = new AsyncRestClient(this.applicationConfig.getDmaapProducerTopicUrl(),
155 this.applicationConfig.getWebClientConfig());
156 this.dmaapMessageHandler = new DmaapMessageHandler(producer, agentClient);
158 return this.dmaapMessageHandler;
161 protected void sleep(Duration duration) {
163 Thread.sleep(duration.toMillis());
164 } catch (Exception e) {
165 logger.error("Failed to put the thread to sleep", e);
169 protected AsyncRestClient getMessageRouterConsumer() {
170 return new AsyncRestClient("", this.applicationConfig.getWebClientConfig());