2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt mountpoint-registrar
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
7 * =================================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. 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 distributed under the License
14 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15 * or implied. See the License for the specific language governing permissions and limitations under
17 * ============LICENSE_END==========================================================================
20 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
22 import java.util.List;
23 import java.util.Properties;
24 import java.util.function.Consumer;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.node.JsonNodeType;
29 import org.onap.dmaap.mr.client.MRClientFactory;
30 import org.onap.dmaap.mr.client.MRConsumer;
31 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public abstract class DMaaPVESMsgConsumerImpl implements DMaaPVESMsgConsumer {
37 private static final Logger LOG = LoggerFactory.getLogger(DMaaPVESMsgConsumerImpl.class);
38 private static final String DEFAULT_SDNRUSER = "admin";
39 private static final String DEFAULT_SDNRPASSWD = "admin";
41 private final String name = this.getClass().getSimpleName();
42 private Properties properties = null;
43 private MRConsumer consumer = null;
44 private boolean running = false;
45 private boolean ready = false;
46 private int fetchPause = 5000; // Default pause between fetch - 5 seconds
47 private int timeout = 15000; // Default timeout - 15 seconds
48 protected final GeneralConfig generalConfig;
50 protected DMaaPVESMsgConsumerImpl(GeneralConfig generalConfig) {
51 this.generalConfig = generalConfig;
55 * Thread to fetch messages from the DMaaP topic. Waits for the messages to arrive on the topic until a certain timeout and returns.
56 * If no data arrives on the topic, sleeps for a certain time period before checking again
65 boolean noData = true;
66 MRConsumerResponse consumerResponse = null;
67 consumerResponse = consumer.fetchWithReturnConsumerResponse(timeout, -1);
68 for (String msg : consumerResponse.getActualMessages()) {
70 LOG.debug("{} received ActualMessage from DMaaP VES Message topic {}", name,msg);
75 LOG.debug("{} received ResponseCode: {}", name, consumerResponse.getResponseCode());
76 LOG.debug("{} received ResponseMessage: {}", name, consumerResponse.getResponseMessage());
77 if ((consumerResponse.getResponseCode() == null)
78 && (consumerResponse.getResponseMessage().contains("SocketTimeoutException"))) {
79 LOG.warn("Client timeout while waiting for response from Server {}",
80 consumerResponse.getResponseMessage());
84 } catch (Exception e) {
85 LOG.error("Caught exception reading from DMaaP VES Message Topic", e);
93 * Create a consumer by specifying properties containing information such as topic name, timeout, URL etc
96 public void init(Properties properties) {
100 String timeoutStr = properties.getProperty("timeout");
101 LOG.debug("timeoutStr: {}", timeoutStr);
103 if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
104 timeout = parseTimeOutValue(timeoutStr);
107 String fetchPauseStr = properties.getProperty("fetchPause");
108 LOG.debug("fetchPause(Str): {}",fetchPauseStr);
109 if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
110 fetchPause = parseFetchPause(fetchPauseStr);
112 LOG.debug("fetchPause: {} ",fetchPause);
114 this.consumer = MRClientFactory.createConsumer(properties);
116 } catch (Exception e) {
117 LOG.error("Error initializing DMaaP VES Message consumer from file {} {}",properties, e);
121 private int parseTimeOutValue(String timeoutStr) {
123 return Integer.parseInt(timeoutStr);
124 } catch (NumberFormatException e) {
125 LOG.error("Non-numeric value specified for timeout ({})",timeoutStr);
130 private int parseFetchPause(String fetchPauseStr) {
132 return Integer.parseInt(fetchPauseStr);
133 } catch (NumberFormatException e) {
134 LOG.error("Non-numeric value specified for fetchPause ({})",fetchPauseStr);
139 private void pauseThread() throws InterruptedException {
140 if (fetchPause > 0) {
141 LOG.debug("No data received from fetch. Pausing {} ms before retry", fetchPause);
142 Thread.sleep(fetchPause);
144 LOG.debug("No data received from fetch. No fetch pause specified - retrying immediately");
149 public boolean isReady() {
154 public boolean isRunning() {
158 public String getProperty(String name) {
159 return properties.getProperty(name, "");
163 public void stopConsumer() {
168 public String getBaseUrl() {
169 return generalConfig.getBaseUrl();
172 public String getSDNRUser() {
173 return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
176 public String getSDNRPasswd() {
177 return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;