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.util.LinkedList;
 
  27 import java.util.List;
 
  28 import org.eclipse.jdt.annotation.NonNull;
 
  29 import org.json.JSONObject;
 
  30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService;
 
  31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
 
  32 import org.slf4j.Logger;
 
  33 import org.slf4j.LoggerFactory;
 
  35 public class MountpointStatePublisher implements Runnable {
 
  37     private static final Logger LOG = LoggerFactory.getLogger(MountpointStatePublisher.class);
 
  38     private List<JSONObject> stateObjects = new LinkedList<JSONObject>();
 
  39     private boolean publish = true;
 
  40     private int publishPause = 5000; // Default pause between fetch - 5 seconds
 
  41     private VESCollectorService vesCollectorService;
 
  43     public MountpointStatePublisher(@NonNull VESCollectorService vesCollectorService) {
 
  44         this.vesCollectorService = vesCollectorService;
 
  47     public void addToPublish(JSONObject publishObj) {
 
  48         getStateObjects().add(publishObj);
 
  51     public synchronized List<JSONObject> getStateObjects() {
 
  59     private void pauseThread() throws InterruptedException {
 
  60         if (publishPause > 0) {
 
  61             LOG.debug("No data yet to publish.  Pausing {} ms before retry ", publishPause);
 
  62             Thread.sleep(publishPause);
 
  64             LOG.debug("No data yet to publish. No publish pause specified - retrying immediately");
 
  69     public String createVESMessage(JSONObject msg, VESCollectorCfgService vesCfg) {
 
  70         MountpointStateVESMessageFormatter vesFormatter = new MountpointStateVESMessageFormatter(vesCfg);
 
  71         String vesMsg = vesFormatter.createVESMessage(msg);
 
  79                 if (getStateObjects().size() > 0) {
 
  80                     JSONObject obj = ((LinkedList<JSONObject>) getStateObjects()).removeFirst();
 
  81                     String vesMsg = createVESMessage(obj, vesCollectorService.getConfig());
 
  82                     this.vesCollectorService.publishVESMessage(vesMsg);
 
  86             } catch (Exception ex) {
 
  87                 LOG.error("Exception while publishing message, ignoring and continuing ... ", ex);