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=======================================================
24 package org.onap.ccsdk.features.sdnr.wt.mountpointstateprovider.impl;
26 import java.io.IOException;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.concurrent.TimeUnit;
31 import org.json.JSONObject;
32 import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration;
33 import org.onap.dmaap.mr.client.MRBatchingPublisher;
34 import org.onap.dmaap.mr.client.MRClientFactory;
35 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class MountpointStatePublisherMain {
41 private static final Logger LOG = LoggerFactory.getLogger(MountpointStatePublisherMain.class);
42 private Thread thread = null;
43 private MRBatchingPublisher pub = null;
44 private List<JSONObject> stateObjects = new LinkedList<JSONObject>();
45 private Properties publisherProperties = new Properties();
46 private boolean closePublisher = false;
47 private int publishPause = 5000; // Default pause between fetch - 5 seconds
49 public MountpointStatePublisherMain(Configuration config) {
53 public void initialize(Configuration config) {
54 LOG.info("In initializePublisher method of MountpointStatePublisher");
55 GeneralConfig generalCfg = (GeneralConfig) config;
57 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_TRANSPORTTYPE, generalCfg.getTransportType());
58 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_HOST_PORT, generalCfg.getHostPort());
59 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_CONTENTTYPE, generalCfg.getContenttype());
60 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_TOPIC, generalCfg.getTopic());
61 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_MAXBATCHSIZE, generalCfg.getMaxBatchSize());
62 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_MAXAGEMS, generalCfg.getMaxAgeMs());
63 publisherProperties.put(GeneralConfig.PROPERTY_KEY_PUBLISHER_MESSAGESENTTHREADOCCURANCE,
64 generalCfg.getMessageSentThreadOccurrence());
66 createPublisher(publisherProperties);
69 public MRBatchingPublisher createPublisher(Properties publisherProperties) {
72 pub = MRClientFactory.createBatchingPublisher(publisherProperties, false);
74 } catch (IOException e) {
75 LOG.info("Exception while creating a publisher", e);
80 public void publishMessage(MRBatchingPublisher pub, String msg) {
81 LOG.info("Publishing message {} - ", msg);
84 } catch (IOException e) {
85 LOG.info("Exception while publishing a mesage ", e);
89 public MRBatchingPublisher getPublisher() {
94 thread = new Thread(new MountpointStatePublisher());
98 public void stop() throws IOException, InterruptedException {
99 closePublisher = true;
100 getPublisher().close(100, TimeUnit.MILLISECONDS); // Send any remaining messages and close)
103 private void pauseThread() throws InterruptedException {
104 if (publishPause > 0) {
105 LOG.debug("No data yet to publish. Pausing {} ms before retry ", publishPause);
106 Thread.sleep(publishPause);
108 LOG.debug("No data yet to publish. No fetch pause specified - retrying immediately");
112 public void addToPublish(JSONObject publishObj) {
113 getStateObjects().add(publishObj);
116 public List<JSONObject> getStateObjects() {
120 public class MountpointStatePublisher implements Runnable {
124 while (!closePublisher) {
126 if (getStateObjects().size() > 0) {
127 JSONObject obj = ((LinkedList<JSONObject>) getStateObjects()).removeFirst();
128 publishMessage(getPublisher(), obj.toString());
132 } catch (Exception ex) {
133 LOG.error("Exception while publishing message, ignoring and continuing ... ", ex);
136 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
137 LOG.debug("Response message = {} ", res.toString());