X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fdmaap%2FDmaapMessageConsumer.java;h=e72fcf0b253fc9c8b65c2f3f4c17a370b0c2ff82;hb=dede1d28c8f37bb21fae806f00f8315b923670c1;hp=91f9ff22bfe049b582028e5cb3da1067eace8478;hpb=86b589317c67eb2350870c9c57c35eac3e9056f6;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumer.java b/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumer.java index 91f9ff22..e72fcf0b 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumer.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumer.java @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * O-RAN-SC * %% - * Copyright (C) 2019 Nordix Foundation + * Copyright (C) 2020 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,8 @@ public class DmaapMessageConsumer implements Runnable { private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumer.class); - final Duration TIME_BETWEEN_DMAAP_POLLS = Duration.ofSeconds(10); + private final static Duration TIME_BETWEEN_DMAAP_POLLS = Duration.ofSeconds(10); + private final ApplicationConfig applicationConfig; @Value("${server.port}") @@ -59,6 +60,10 @@ public class DmaapMessageConsumer implements Runnable { thread.start(); } + DmaapMessageConsumer(ApplicationConfig applicationConfig, boolean start) { + this.applicationConfig = applicationConfig; + } + private boolean isDmaapConfigured() { Properties consumerCfg = applicationConfig.getDmaapConsumerConfig(); Properties producerCfg = applicationConfig.getDmaapPublisherConfig(); @@ -77,39 +82,44 @@ public class DmaapMessageConsumer implements Runnable { } } } catch (Exception e) { - logger.warn("{}: cannot fetch because of ", this, e.getMessage(), e); + logger.warn("{}: cannot fetch because of {}", this, e.getMessage()); sleep(TIME_BETWEEN_DMAAP_POLLS); } } } - private Iterable fetchAllMessages() throws ServiceException, FileNotFoundException, IOException { + private Iterable fetchAllMessages() throws ServiceException, IOException { Properties dmaapConsumerProperties = this.applicationConfig.getDmaapConsumerConfig(); - MRConsumer consumer = MRClientFactory.createConsumer(dmaapConsumerProperties); + MRConsumer consumer = getMessageRouterConsumer(dmaapConsumerProperties); MRConsumerResponse response = consumer.fetchWithReturnConsumerResponse(); if (response == null || !"200".equals(response.getResponseCode())) { - throw new ServiceException("DMaaP NULL response received"); + String errorMessage = "DMaaP NULL response received"; + if (response != null) { + errorMessage = "Error respons " + response.getResponseCode() + " " + response.getResponseMessage() + + " from DMaaP."; + } + throw new ServiceException(errorMessage); } else { logger.debug("DMaaP consumer received {} : {}", response.getResponseCode(), response.getResponseMessage()); return response.getActualMessages(); } } - private void processMsg(String msg) throws Exception { + private void processMsg(String msg) throws IOException { logger.debug("Message Reveived from DMAAP : {}", msg); - createDmaapMessageHandler().handleDmaapMsg(msg); + getDmaapMessageHandler().handleDmaapMsg(msg); } - private DmaapMessageHandler createDmaapMessageHandler() throws FileNotFoundException, IOException { + private DmaapMessageHandler getDmaapMessageHandler() throws IOException { String agentBaseUrl = "http://localhost:" + this.localServerPort; - AsyncRestClient agentClient = new AsyncRestClient(agentBaseUrl); + AsyncRestClient agentClient = createRestClient(agentBaseUrl); Properties dmaapPublisherProperties = applicationConfig.getDmaapPublisherConfig(); - MRBatchingPublisher producer = MRClientFactory.createBatchingPublisher(dmaapPublisherProperties); + MRBatchingPublisher producer = getMessageRouterPublisher(dmaapPublisherProperties); - return new DmaapMessageHandler(producer, this.applicationConfig, agentClient); + return createDmaapMessageHandler(agentClient, producer); } - private boolean sleep(Duration duration) { + boolean sleep(Duration duration) { try { Thread.sleep(duration.toMillis()); return true; @@ -118,4 +128,21 @@ public class DmaapMessageConsumer implements Runnable { return false; } } + + MRConsumer getMessageRouterConsumer(Properties dmaapConsumerProperties) throws FileNotFoundException, IOException { + return MRClientFactory.createConsumer(dmaapConsumerProperties); + } + + DmaapMessageHandler createDmaapMessageHandler(AsyncRestClient agentClient, MRBatchingPublisher producer) { + return new DmaapMessageHandler(producer, agentClient); + } + + AsyncRestClient createRestClient(String agentBaseUrl) { + return new AsyncRestClient(agentBaseUrl); + } + + MRBatchingPublisher getMessageRouterPublisher(Properties dmaapPublisherProperties) + throws FileNotFoundException, IOException { + return MRClientFactory.createBatchingPublisher(dmaapPublisherProperties); + } }