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.Properties;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import org.onap.dmaap.mr.client.MRClientFactory;
25 import org.onap.dmaap.mr.client.MRConsumer;
26 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public abstract class DMaaPVESMsgConsumerImpl implements DMaaPVESMsgConsumer, DMaaPVESMsgValidator {
32 private static final Logger LOG = LoggerFactory.getLogger(DMaaPVESMsgConsumerImpl.class);
33 private static final String DEFAULT_SDNRUSER = "admin";
34 private static final String DEFAULT_SDNRPASSWD = "admin";
36 private final String name = this.getClass().getSimpleName();
37 private Properties properties = null;
38 private MRConsumer consumer = null;
39 private boolean running = false;
40 private boolean ready = false;
41 private int fetchPause = 5000; // Default pause between fetch - 5 seconds
42 private int timeout = 15000; // Default timeout - 15 seconds
43 protected final GeneralConfig generalConfig;
45 protected DMaaPVESMsgConsumerImpl(GeneralConfig generalConfig) {
46 this.generalConfig = generalConfig;
50 * Thread to fetch messages from the DMaaP topic. Waits for the messages to arrive on the topic until a certain timeout and returns.
51 * If no data arrives on the topic, sleeps for a certain time period before checking again
60 boolean noData = true;
61 MRConsumerResponse consumerResponse = null;
62 consumerResponse = consumer.fetchWithReturnConsumerResponse(timeout, -1);
63 for (String msg : consumerResponse.getActualMessages()) {
65 LOG.debug("{} received ActualMessage from DMaaP VES Message topic {}", name,msg);
66 if(isMessageValid(msg)) {
72 LOG.debug("{} received ResponseCode: {}", name, consumerResponse.getResponseCode());
73 LOG.debug("{} received ResponseMessage: {}", name, consumerResponse.getResponseMessage());
74 if ((consumerResponse.getResponseCode() == null)
75 && (consumerResponse.getResponseMessage().contains("SocketTimeoutException"))) {
76 LOG.warn("Client timeout while waiting for response from Server {}",
77 consumerResponse.getResponseMessage());
81 } catch (JsonProcessingException jsonProcessingException) {
82 LOG.warn("Failed to convert message to JsonNode: {}", jsonProcessingException.getMessage());
83 } catch (Exception e) {
84 LOG.error("Caught exception reading from DMaaP VES Message Topic", e);
92 public boolean isMessageValid(String message) {
97 * Create a consumer by specifying properties containing information such as topic name, timeout, URL etc
100 public void init(Properties properties) {
104 String timeoutStr = properties.getProperty("timeout");
105 LOG.debug("timeoutStr: {}", timeoutStr);
107 if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
108 timeout = parseTimeOutValue(timeoutStr);
111 String fetchPauseStr = properties.getProperty("fetchPause");
112 LOG.debug("fetchPause(Str): {}",fetchPauseStr);
113 if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
114 fetchPause = parseFetchPause(fetchPauseStr);
116 LOG.debug("fetchPause: {} ",fetchPause);
118 this.consumer = MRClientFactory.createConsumer(properties);
120 } catch (Exception e) {
121 LOG.error("Error initializing DMaaP VES Message consumer from file {} {}",properties, e);
125 private int parseTimeOutValue(String timeoutStr) {
127 return Integer.parseInt(timeoutStr);
128 } catch (NumberFormatException e) {
129 LOG.error("Non-numeric value specified for timeout ({})",timeoutStr);
134 private int parseFetchPause(String fetchPauseStr) {
136 return Integer.parseInt(fetchPauseStr);
137 } catch (NumberFormatException e) {
138 LOG.error("Non-numeric value specified for fetchPause ({})",fetchPauseStr);
143 private void pauseThread() throws InterruptedException {
144 if (fetchPause > 0) {
145 LOG.debug("No data received from fetch. Pausing {} ms before retry", fetchPause);
146 Thread.sleep(fetchPause);
148 LOG.debug("No data received from fetch. No fetch pause specified - retrying immediately");
153 public boolean isReady() {
158 public boolean isRunning() {
162 public String getProperty(String name) {
163 return properties.getProperty(name, "");
167 public void stopConsumer() {
172 public String getBaseUrl() {
173 return generalConfig.getBaseUrl();
176 public String getSDNRUser() {
177 return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
180 public String getSDNRPasswd() {
181 return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;