2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * =============================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.appc.metricservice.policy.impl;
 
  27 import java.text.SimpleDateFormat;
 
  28 import java.util.Date;
 
  29 import java.util.Properties;
 
  30 import java.util.concurrent.Executors;
 
  31 import java.util.concurrent.ScheduledExecutorService;
 
  32 import java.util.concurrent.ThreadFactory;
 
  33 import java.util.concurrent.TimeUnit;
 
  35 import org.onap.appc.configuration.Configuration;
 
  36 import org.onap.appc.configuration.ConfigurationFactory;
 
  37 import org.onap.appc.exceptions.APPCException;
 
  38 import org.onap.appc.metricservice.MetricRegistry;
 
  39 import org.onap.appc.metricservice.MetricService;
 
  40 import org.onap.appc.metricservice.Publisher;
 
  41 import org.onap.appc.metricservice.metric.Metric;
 
  42 import org.onap.appc.metricservice.policy.ScheduledPublishingPolicy;
 
  43 import com.att.eelf.configuration.EELFLogger;
 
  44 import com.att.eelf.configuration.EELFManager;
 
  47 public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy {
 
  48     private long startTime;
 
  50     private Publisher[] publishers;
 
  51     private Metric[] metrics;
 
  52     private MetricRegistry metricRegistry;
 
  53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ScheduledPublishingPolicyImpl.class);
 
  54     private ScheduledExecutorService scheduleExecutor;
 
  55     private Configuration configuration;
 
  57     public ScheduledPublishingPolicyImpl(long startTime, long period, Publisher[] publishers, Metric[] metrics) {
 
  58         this.startTime = startTime;
 
  60         this.publishers = publishers;
 
  61         this.metrics = metrics;
 
  62         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
 
  65     public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) {
 
  66         configuration = ConfigurationFactory.getConfiguration();
 
  67         Properties properties=configuration.getProperties();
 
  69             if(properties.getProperty("schedule.policy.metric.period")!=null && properties.getProperty("schedule.policy.metric.start.time")!=null){
 
  70                 this.startTime = getConfigStartTime(properties);
 
  71                 this.period = getConfigPeriod(properties);
 
  72                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  73             }else if(properties.getProperty("schedule.policy.metric.period")!=null){
 
  75                 this.period=getConfigPeriod(properties);
 
  76                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  78             }else if(properties.getProperty("schedule.policy.metric.period")==null && properties.getProperty("schedule.policy.metric.start.time")!=null){
 
  79                 this.startTime=getConfigStartTime("00:00:00",properties);
 
  80                 this.period=(24*60*60*1000l)-1;
 
  81                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  84                 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
 
  87                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  91             logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
 
  94             logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  96         this.publishers = publishers;
 
  97         this.metrics = metrics;
 
  98         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
 
 101     private long getConfigPeriod(Properties properties) {
 
 102         String period=properties.getProperty("schedule.policy.metric.period");
 
 103         logger.info("Metric period : " +period);
 
 104         long periodInMs=Integer.parseInt(period)*1000l;
 
 105         logger.info("Metric period in long : " +periodInMs);
 
 109     private long getTimeInMs(String time) {
 
 110         String[] strings=time.split(":");
 
 111         if(strings.length==3) {
 
 112             long hour = Integer.parseInt(strings[0]) * 60 * 60 * 1000l;
 
 113             long min = Integer.parseInt(strings[1]) * 60 * 1000l;
 
 114             long sec = Integer.parseInt(strings[2]) * 1000l;
 
 124     private long getConfigStartTime(Properties properties) {
 
 125         String startTime=properties.getProperty("schedule.policy.metric.start.time");
 
 127             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
 
 128             long period=getConfigPeriod(properties);
 
 132                 return period-((timeDiff*-1)%period);
 
 138     private long getConfigStartTime(String startTime,Properties properties) {
 
 140             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
 
 141             long period=getConfigPeriod(properties);
 
 143                 return timeDiff%period;
 
 145                 return period-((timeDiff*-1)%period);
 
 151     public void onMetricChange(Metric metric) throws APPCException {
 
 156     public Metric[] metrics() {
 
 162         Properties properties=configuration.getProperties();
 
 163         boolean isMetricEnabled=false;
 
 164         if(properties!=null){
 
 165             String metricProperty=properties.getProperty("metric.enabled");
 
 166             if(metricProperty!=null){
 
 167                 isMetricEnabled=Boolean.valueOf(metricProperty);
 
 171             logger.info("Metric Service is enabled, hence policies getting scheduled");
 
 172             for(final Publisher publisher:this.getPublishers()){
 
 173                 scheduleExecutor.scheduleWithFixedDelay(new Runnable()
 
 177                             publisher.publish(metricRegistry, metrics);
 
 179                         } catch (RuntimeException ex) {
 
 180                             logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", publisher.getClass().getSimpleName(), ex);
 
 184                         , startTime, period, TimeUnit.MILLISECONDS);
 
 187             logger.info("Metric Service is not enabled, hence policies not getting scheduled");
 
 193     public long getStartTime() {
 
 194         return this.startTime;
 
 198     public long getPeriod() {
 
 203     public Publisher[] getPublishers() {
 
 204         return this.publishers;
 
 208     public void reset() {
 
 209         for(Metric metric:this.metrics){
 
 214     private  ThreadFactory getThreadFactory(final boolean isDaemon){
 
 215         return new ThreadFactory() {
 
 216             public Thread newThread(Runnable r) {
 
 217                 Thread t = Executors.defaultThreadFactory().newThread(r);
 
 218                 t.setDaemon(isDaemon);