015e3ada1aea43f050709ffa1b778a90a694a227
[ccsdk/features.git] /
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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
16  * the License.
17  * ============LICENSE_END==========================================================================
18  */
19
20 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
21
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;
29
30 public abstract class DMaaPVESMsgConsumerImpl implements DMaaPVESMsgConsumer, DMaaPVESMsgValidator {
31
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";
35
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;
44
45     protected DMaaPVESMsgConsumerImpl(GeneralConfig generalConfig) {
46         this.generalConfig = generalConfig;
47     }
48
49     /*
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
52      */
53     @Override
54     public void run() {
55
56         if (ready) {
57             running = true;
58             while (running) {
59                 try {
60                     boolean noData = true;
61                     MRConsumerResponse consumerResponse = null;
62                     consumerResponse = consumer.fetchWithReturnConsumerResponse(timeout, -1);
63                     for (String msg : consumerResponse.getActualMessages()) {
64                         noData = false;
65                         LOG.debug("{} received ActualMessage from DMaaP VES Message topic {}", name,msg);
66                         if(isMessageValid(msg)) {
67                             processMsg(msg);
68                         }
69                     }
70
71                     if (noData) {
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());
78                         }
79                         pauseThread();
80                     }
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);
85                     running = false;
86                 }
87             }
88         }
89     }
90
91     @Override
92     public boolean isMessageValid(String message) {
93         return true;
94     }
95
96     /*
97      * Create a consumer by specifying  properties containing information such as topic name, timeout, URL etc
98      */
99     @Override
100     public void init(Properties properties) {
101
102         try {
103
104             String timeoutStr = properties.getProperty("timeout");
105             LOG.debug("timeoutStr: {}", timeoutStr);
106
107             if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
108                 timeout = parseTimeOutValue(timeoutStr);
109             }
110
111             String fetchPauseStr = properties.getProperty("fetchPause");
112             LOG.debug("fetchPause(Str): {}",fetchPauseStr);
113             if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
114                 fetchPause = parseFetchPause(fetchPauseStr);
115             }
116             LOG.debug("fetchPause: {} ",fetchPause);
117
118             this.consumer = MRClientFactory.createConsumer(properties);
119             ready = true;
120         } catch (Exception e) {
121             LOG.error("Error initializing DMaaP VES Message consumer from file {} {}",properties, e);
122         }
123     }
124
125     private int parseTimeOutValue(String timeoutStr) {
126         try {
127             return Integer.parseInt(timeoutStr);
128         } catch (NumberFormatException e) {
129             LOG.error("Non-numeric value specified for timeout ({})",timeoutStr);
130         }
131         return timeout;
132     }
133
134     private int parseFetchPause(String fetchPauseStr) {
135         try {
136             return Integer.parseInt(fetchPauseStr);
137         } catch (NumberFormatException e) {
138             LOG.error("Non-numeric value specified for fetchPause ({})",fetchPauseStr);
139         }
140         return fetchPause;
141     }
142
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);
147         } else {
148             LOG.debug("No data received from fetch.  No fetch pause specified - retrying immediately");
149         }
150     }
151
152     @Override
153     public boolean isReady() {
154         return ready;
155     }
156
157     @Override
158     public boolean isRunning() {
159         return running;
160     }
161
162     public String getProperty(String name) {
163         return properties.getProperty(name, "");
164     }
165
166     @Override
167     public void stopConsumer() {
168         running = false;
169     }
170
171
172     public String getBaseUrl() {
173         return generalConfig.getBaseUrl();
174     }
175
176     public String getSDNRUser() {
177         return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
178     }
179
180     public String getSDNRPasswd() {
181         return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
182     }
183 }