2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.metricservice.policy.impl;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.Properties;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.ThreadFactory;
30 import java.util.concurrent.TimeUnit;
32 import org.openecomp.appc.configuration.Configuration;
33 import org.openecomp.appc.configuration.ConfigurationFactory;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.appc.metricservice.MetricRegistry;
36 import org.openecomp.appc.metricservice.MetricService;
37 import org.openecomp.appc.metricservice.Publisher;
38 import org.openecomp.appc.metricservice.metric.Metric;
39 import org.openecomp.appc.metricservice.policy.ScheduledPublishingPolicy;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
44 public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy {
45 private long startTime;
47 private Publisher[] publishers;
48 private Metric[] metrics;
49 private MetricRegistry metricRegistry;
50 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ScheduledPublishingPolicyImpl.class);
51 private ScheduledExecutorService scheduleExecutor;
52 private Configuration configuration;
54 public ScheduledPublishingPolicyImpl(long startTime, long period, Publisher[] publishers, Metric[] metrics) {
55 this.startTime = startTime;
57 this.publishers = publishers;
58 this.metrics = metrics;
59 this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
62 public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) {
63 configuration = ConfigurationFactory.getConfiguration();
64 Properties properties=configuration.getProperties();
66 if(properties.getProperty("schedule.policy.metric.period")!=null && properties.getProperty("schedule.policy.metric.start.time")!=null){
67 this.startTime = getConfigStartTime(properties);
68 this.period = getConfigPeriod(properties);
69 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
70 }else if(properties.getProperty("schedule.policy.metric.period")!=null){
72 this.period=getConfigPeriod(properties);
73 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
75 }else if(properties.getProperty("schedule.policy.metric.period")==null && properties.getProperty("schedule.policy.metric.start.time")!=null){
76 this.startTime=getConfigStartTime("00:00:00",properties);
77 this.period=(24*60*60*1000)-1;
78 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
81 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
84 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
88 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
91 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
93 this.publishers = publishers;
94 this.metrics = metrics;
95 this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
98 private long getConfigPeriod(Properties properties) {
99 String period=properties.getProperty("schedule.policy.metric.period");
100 logger.info("Metric period : " +period);
101 long periodInMs=Integer.parseInt(period)*1000;
102 logger.info("Metric period in long : " +periodInMs);
106 private long getTimeInMs(String time) {
107 String[] strings=time.split(":");
108 if(strings.length==3) {
109 long hour = Integer.parseInt(strings[0]) * 60 * 60 * 1000;
110 long min = Integer.parseInt(strings[1]) * 60 * 1000;
111 long sec = Integer.parseInt(strings[2]) * 1000;
121 private long getConfigStartTime(Properties properties) {
122 String startTime=properties.getProperty("schedule.policy.metric.start.time");
124 long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
125 long period=getConfigPeriod(properties);
129 return period-((timeDiff*-1)%period);
135 private long getConfigStartTime(String startTime,Properties properties) {
137 long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
138 long period=getConfigPeriod(properties);
140 return timeDiff%period;
142 return period-((timeDiff*-1)%period);
148 public void onMetricChange(Metric metric) throws APPCException {
153 public Metric[] metrics() {
159 Properties properties=configuration.getProperties();
160 boolean isMetricEnabled=false;
161 if(properties!=null){
162 String metricProperty=properties.getProperty("metric.enabled");
163 if(metricProperty!=null){
164 isMetricEnabled=Boolean.valueOf(metricProperty);
168 logger.info("Metric Service is enabled, hence policies getting scheduled");
169 for(final Publisher publisher:this.getPublishers()){
170 scheduleExecutor.scheduleWithFixedDelay(new Runnable()
174 publisher.publish(metricRegistry, metrics);
176 } catch (RuntimeException ex) {
177 logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", publisher.getClass().getSimpleName(), ex);
181 , startTime, period, TimeUnit.MILLISECONDS);
184 logger.info("Metric Service is not enabled, hence policies not getting scheduled");
190 public long getStartTime() {
191 return this.startTime;
195 public long getPeriod() {
200 public Publisher[] getPublishers() {
201 return this.publishers;
205 public void reset() {
206 for(Metric metric:this.metrics){
211 private ThreadFactory getThreadFactory(final boolean isDaemon){
212 return new ThreadFactory() {
213 public Thread newThread(Runnable r) {
214 Thread t = Executors.defaultThreadFactory().newThread(r);
215 t.setDaemon(isDaemon);