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 com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.onap.dmaap.mr.client.MRClientFactory;
27 import org.onap.dmaap.mr.client.MRConsumer;
28 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public abstract class DMaaPVESMsgConsumerImpl implements DMaaPVESMsgConsumer, DMaaPVESMsgValidator {
34 private static final Logger LOG = LoggerFactory.getLogger(DMaaPVESMsgConsumerImpl.class);
35 private static final String DEFAULT_SDNRUSER = "admin";
36 private static final String DEFAULT_SDNRPASSWD = "admin";
38 private final String name = this.getClass().getSimpleName();
39 private Properties properties = null;
40 private MRConsumer consumer = null;
41 private boolean running = false;
42 private boolean ready = false;
43 private int fetchPause = 5000; // Default pause between fetch - 5 seconds
44 private int timeout = 15000; // Default timeout - 15 seconds
45 protected final GeneralConfig generalConfig;
47 protected DMaaPVESMsgConsumerImpl(GeneralConfig generalConfig) {
48 this.generalConfig = generalConfig;
52 * Thread to fetch messages from the DMaaP topic. Waits for the messages to arrive on the topic until a certain timeout and returns.
53 * If no data arrives on the topic, sleeps for a certain time period before checking again
62 boolean noData = true;
63 MRConsumerResponse consumerResponse = null;
64 consumerResponse = consumer.fetchWithReturnConsumerResponse(timeout, -1);
65 for (String msg : consumerResponse.getActualMessages()) {
67 LOG.debug("{} received ActualMessage from DMaaP VES Message topic {}", name,msg);
68 if(isMessageValid(msg)) {
74 LOG.debug("{} received ResponseCode: {}", name, consumerResponse.getResponseCode());
75 LOG.debug("{} received ResponseMessage: {}", name, consumerResponse.getResponseMessage());
76 if ((consumerResponse.getResponseCode() == null)
77 && (consumerResponse.getResponseMessage().contains("SocketTimeoutException"))) {
78 LOG.warn("Client timeout while waiting for response from Server {}",
79 consumerResponse.getResponseMessage());
83 } catch (JsonProcessingException jsonProcessingException) {
84 LOG.warn("Failed to convert message to JsonNode: {}", jsonProcessingException.getMessage());
85 } catch (InvalidMessageException invalidMessageException) {
86 LOG.warn("Message is invalid because of: {}", invalidMessageException.getMessage());
87 } catch (Exception e) {
88 LOG.error("Caught exception reading from DMaaP VES Message Topic", e);
96 public boolean isMessageValid(String message) {
100 protected JsonNode convertMessageToJsonNode(String message) throws JsonProcessingException {
101 return new ObjectMapper().readTree(message);
105 * Create a consumer by specifying properties containing information such as topic name, timeout, URL etc
108 public void init(Properties properties) {
112 String timeoutStr = properties.getProperty("timeout");
113 LOG.debug("timeoutStr: {}", timeoutStr);
115 if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
116 timeout = parseTimeOutValue(timeoutStr);
119 String fetchPauseStr = properties.getProperty("fetchPause");
120 LOG.debug("fetchPause(Str): {}",fetchPauseStr);
121 if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
122 fetchPause = parseFetchPause(fetchPauseStr);
124 LOG.debug("fetchPause: {} ",fetchPause);
126 this.consumer = MRClientFactory.createConsumer(properties);
128 } catch (Exception e) {
129 LOG.error("Error initializing DMaaP VES Message consumer from file {} {}",properties, e);
133 private int parseTimeOutValue(String timeoutStr) {
135 return Integer.parseInt(timeoutStr);
136 } catch (NumberFormatException e) {
137 LOG.error("Non-numeric value specified for timeout ({})",timeoutStr);
142 private int parseFetchPause(String fetchPauseStr) {
144 return Integer.parseInt(fetchPauseStr);
145 } catch (NumberFormatException e) {
146 LOG.error("Non-numeric value specified for fetchPause ({})",fetchPauseStr);
151 private void pauseThread() throws InterruptedException {
152 if (fetchPause > 0) {
153 LOG.debug("No data received from fetch. Pausing {} ms before retry", fetchPause);
154 Thread.sleep(fetchPause);
156 LOG.debug("No data received from fetch. No fetch pause specified - retrying immediately");
161 public boolean isReady() {
166 public boolean isRunning() {
170 public String getProperty(String name) {
171 return properties.getProperty(name, "");
175 public void stopConsumer() {
180 public String getBaseUrl() {
181 return generalConfig.getBaseUrl();
184 public String getSDNRUser() {
185 return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
188 public String getSDNRPasswd() {
189 return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;