2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Update Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=======================================================
25 package org.onap.ccsdk.features.sdnr.wt.mountpointstateprovider.impl;
27 import java.io.IOException;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Properties;
31 import java.util.concurrent.TimeUnit;
33 import org.json.JSONObject;
34 import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration;
35 import org.onap.dmaap.mr.client.MRBatchingPublisher;
36 import org.onap.dmaap.mr.client.MRClientFactory;
37 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class MountpointStatePublisher implements Runnable {
44 private static final Logger LOG = LoggerFactory.getLogger(MountpointStatePublisher.class);
45 public static final List<JSONObject> stateObjects = new LinkedList<JSONObject>();
46 static MRBatchingPublisher pub;
47 Properties publisherProperties = new Properties();
48 static boolean closePublisher = false; //Set this to true in the "Close" method of MountpointStateProviderImpl
49 private int fetchPause = 5000; // Default pause between fetch - 5 seconds
52 public MountpointStatePublisher(Configuration config) {
56 public void initialize(Configuration config) {
57 LOG.info("In initializePublisher method of MountpointStatePublisher");
58 GeneralConfig generalCfg = (GeneralConfig) config;
60 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_TRANSPORTTYPE, generalCfg.getTransportType());
61 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_HOST_PORT, generalCfg.getHostPort());
62 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_CONTENTTYPE, generalCfg.getContenttype());
63 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_TOPIC, generalCfg.getTopic());
64 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_MAXBATCHSIZE, generalCfg.getMaxBatchSize());
65 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_MAXAGEMS, generalCfg.getMaxAgeMs());
66 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_MESSAGESENTTHREADOCCURANCE,
67 generalCfg.getMessageSentThreadOccurrence());
69 createPublisher(publisherProperties);
72 public MRBatchingPublisher createPublisher(Properties publisherProperties) {
75 pub = MRClientFactory.createBatchingPublisher(publisherProperties, false);
77 } catch (IOException e) {
78 LOG.info("Exception while creating a publisher", e);
84 public void publishMessage(MRBatchingPublisher pub, String msg) {
85 LOG.info("Publishing message {} - ", msg);
88 } catch (IOException e) {
89 LOG.info("Exception while publishing a mesage ", e);
93 public MRBatchingPublisher getPublisher() {
99 while (!closePublisher) {
101 if (stateObjects.size() > 0) {
102 JSONObject obj = ((LinkedList<JSONObject>) stateObjects).removeFirst();
103 publishMessage(getPublisher(), obj.toString());
107 } catch (Exception ex) {
108 LOG.error("Exception while publishing message, ignoring and continuing ... ", ex);
111 MRPublisherResponse res = pub.sendBatchWithResponse(); // As per dmaap-client code understanding, this need not be called but for some reason the messages are not pushed unless this is called
112 LOG.debug("Response message = {} ", res.toString());
116 private void pauseThread() throws InterruptedException {
117 if (fetchPause > 0) {
118 LOG.debug("No data yet to publish. Pausing {} ms before retry ", fetchPause);
119 Thread.sleep(fetchPause);
121 LOG.debug("No data yet to publish. No fetch pause specified - retrying immediately");
125 public static void stopPublisher() throws IOException, InterruptedException {
126 closePublisher = true;
127 pub.close(100, TimeUnit.MILLISECONDS); // Send any remaining messages and close