2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * Copyright (C) 2017 Amdocs
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  23 package org.openecomp.appc.metricservice.policy.impl;
 
  25 import java.text.SimpleDateFormat;
 
  26 import java.util.Date;
 
  27 import java.util.Properties;
 
  28 import java.util.concurrent.Executors;
 
  29 import java.util.concurrent.ScheduledExecutorService;
 
  30 import java.util.concurrent.ThreadFactory;
 
  31 import java.util.concurrent.TimeUnit;
 
  33 import org.openecomp.appc.configuration.Configuration;
 
  34 import org.openecomp.appc.configuration.ConfigurationFactory;
 
  35 import org.openecomp.appc.exceptions.APPCException;
 
  36 import org.openecomp.appc.metricservice.MetricRegistry;
 
  37 import org.openecomp.appc.metricservice.MetricService;
 
  38 import org.openecomp.appc.metricservice.Publisher;
 
  39 import org.openecomp.appc.metricservice.metric.Metric;
 
  40 import org.openecomp.appc.metricservice.policy.ScheduledPublishingPolicy;
 
  41 import com.att.eelf.configuration.EELFLogger;
 
  42 import com.att.eelf.configuration.EELFManager;
 
  45 public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy {
 
  46     private long startTime;
 
  48     private Publisher[] publishers;
 
  49     private Metric[] metrics;
 
  50     private MetricRegistry metricRegistry;
 
  51     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ScheduledPublishingPolicyImpl.class);
 
  52     private ScheduledExecutorService scheduleExecutor;
 
  53     private Configuration configuration;
 
  55     public ScheduledPublishingPolicyImpl(long startTime, long period, Publisher[] publishers, Metric[] metrics) {
 
  56         this.startTime = startTime;
 
  58         this.publishers = publishers;
 
  59         this.metrics = metrics;
 
  60         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
 
  63     public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) {
 
  64         configuration = ConfigurationFactory.getConfiguration();
 
  65         Properties properties=configuration.getProperties();
 
  67             if(properties.getProperty("schedule.policy.metric.period")!=null && properties.getProperty("schedule.policy.metric.start.time")!=null){
 
  68                 this.startTime = getConfigStartTime(properties);
 
  69                 this.period = getConfigPeriod(properties);
 
  70                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  71             }else if(properties.getProperty("schedule.policy.metric.period")!=null){
 
  73                 this.period=getConfigPeriod(properties);
 
  74                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  76             }else if(properties.getProperty("schedule.policy.metric.period")==null && properties.getProperty("schedule.policy.metric.start.time")!=null){
 
  77                 this.startTime=getConfigStartTime("00:00:00",properties);
 
  78                 this.period=(24*60*60*1000)-1;
 
  79                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  82                 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
 
  85                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  89             logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
 
  92             logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
 
  94         this.publishers = publishers;
 
  95         this.metrics = metrics;
 
  96         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
 
  99     private long getConfigPeriod(Properties properties) {
 
 100         String period=properties.getProperty("schedule.policy.metric.period");
 
 101         logger.info("Metric period : " +period);
 
 102         long periodInMs=Integer.parseInt(period)*1000;
 
 103         logger.info("Metric period in long : " +periodInMs);
 
 107     private long getTimeInMs(String time) {
 
 108         String[] strings=time.split(":");
 
 109         if(strings.length==3) {
 
 110             long hour = Integer.parseInt(strings[0]) * 60 * 60 * 1000;
 
 111             long min = Integer.parseInt(strings[1]) * 60 * 1000;
 
 112             long sec = Integer.parseInt(strings[2]) * 1000;
 
 122     private long getConfigStartTime(Properties properties) {
 
 123         String startTime=properties.getProperty("schedule.policy.metric.start.time");
 
 125             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
 
 126             long period=getConfigPeriod(properties);
 
 130                 return period-((timeDiff*-1)%period);
 
 136     private long getConfigStartTime(String startTime,Properties properties) {
 
 138             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
 
 139             long period=getConfigPeriod(properties);
 
 141                 return timeDiff%period;
 
 143                 return period-((timeDiff*-1)%period);
 
 149     public void onMetricChange(Metric metric) throws APPCException {
 
 154     public Metric[] metrics() {
 
 160         Properties properties=configuration.getProperties();
 
 161         boolean isMetricEnabled=false;
 
 162         if(properties!=null){
 
 163             String metricProperty=properties.getProperty("metric.enabled");
 
 164             if(metricProperty!=null){
 
 165                 isMetricEnabled=Boolean.valueOf(metricProperty);
 
 169             logger.info("Metric Service is enabled, hence policies getting scheduled");
 
 170             for(final Publisher publisher:this.getPublishers()){
 
 171                 scheduleExecutor.scheduleWithFixedDelay(new Runnable()
 
 175                             publisher.publish(metricRegistry, metrics);
 
 177                         } catch (RuntimeException ex) {
 
 178                             logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", publisher.getClass().getSimpleName(), ex);
 
 182                         , startTime, period, TimeUnit.MILLISECONDS);
 
 185             logger.info("Metric Service is not enabled, hence policies not getting scheduled");
 
 191     public long getStartTime() {
 
 192         return this.startTime;
 
 196     public long getPeriod() {
 
 201     public Publisher[] getPublishers() {
 
 202         return this.publishers;
 
 206     public void reset() {
 
 207         for(Metric metric:this.metrics){
 
 212     private  ThreadFactory getThreadFactory(final boolean isDaemon){
 
 213         return new ThreadFactory() {
 
 214             public Thread newThread(Runnable r) {
 
 215                 Thread t = Executors.defaultThreadFactory().newThread(r);
 
 216                 t.setDaemon(isDaemon);