2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.metricservice.policy.impl;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Properties;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.ThreadFactory;
32 import java.util.concurrent.TimeUnit;
34 import org.onap.appc.configuration.Configuration;
35 import org.onap.appc.configuration.ConfigurationFactory;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.metricservice.MetricRegistry;
38 import org.onap.appc.metricservice.MetricService;
39 import org.onap.appc.metricservice.Publisher;
40 import org.onap.appc.metricservice.metric.Metric;
41 import org.onap.appc.metricservice.policy.ScheduledPublishingPolicy;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
46 public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy {
47 private long startTime;
49 private Publisher[] publishers;
50 private Metric[] metrics;
51 private MetricRegistry metricRegistry;
52 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ScheduledPublishingPolicyImpl.class);
53 private ScheduledExecutorService scheduleExecutor;
54 private Configuration configuration;
56 public ScheduledPublishingPolicyImpl(long startTime, long period, Publisher[] publishers, Metric[] metrics) {
57 this.startTime = startTime;
59 this.publishers = publishers;
60 this.metrics = metrics;
61 this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
64 public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) {
65 configuration = ConfigurationFactory.getConfiguration();
66 Properties properties=configuration.getProperties();
68 if(properties.getProperty("schedule.policy.metric.period")!=null && properties.getProperty("schedule.policy.metric.start.time")!=null){
69 this.startTime = getConfigStartTime(properties);
70 this.period = getConfigPeriod(properties);
71 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
72 }else if(properties.getProperty("schedule.policy.metric.period")!=null){
74 this.period=getConfigPeriod(properties);
75 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
77 }else if(properties.getProperty("schedule.policy.metric.period")==null && properties.getProperty("schedule.policy.metric.start.time")!=null){
78 this.startTime=getConfigStartTime("00:00:00",properties);
79 this.period=(24*60*60*1000l)-1;
80 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
83 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
86 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
90 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
93 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
95 this.publishers = publishers;
96 this.metrics = metrics;
97 this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
100 private long getConfigPeriod(Properties properties) {
101 String period=properties.getProperty("schedule.policy.metric.period");
102 logger.info("Metric period : " +period);
103 long periodInMs=Integer.parseInt(period)*1000l;
104 logger.info("Metric period in long : " +periodInMs);
108 private long getTimeInMs(String time) {
109 String[] strings=time.split(":");
110 if(strings.length==3) {
111 long hour = Integer.parseInt(strings[0]) * 60 * 60 * 1000l;
112 long min = Integer.parseInt(strings[1]) * 60 * 1000l;
113 long sec = Integer.parseInt(strings[2]) * 1000l;
123 private long getConfigStartTime(Properties properties) {
124 String startTime=properties.getProperty("schedule.policy.metric.start.time");
126 long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
127 long period=getConfigPeriod(properties);
131 return period-((timeDiff*-1)%period);
137 private long getConfigStartTime(String startTime,Properties properties) {
139 long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
140 long period=getConfigPeriod(properties);
142 return timeDiff%period;
144 return period-((timeDiff*-1)%period);
150 public void onMetricChange(Metric metric) throws APPCException {
155 public Metric[] metrics() {
161 Properties properties=configuration.getProperties();
162 boolean isMetricEnabled=false;
163 if(properties!=null){
164 String metricProperty=properties.getProperty("metric.enabled");
165 if(metricProperty!=null){
166 isMetricEnabled=Boolean.valueOf(metricProperty);
170 logger.info("Metric Service is enabled, hence policies getting scheduled");
171 for(final Publisher publisher:this.getPublishers()){
172 scheduleExecutor.scheduleWithFixedDelay(new Runnable()
176 publisher.publish(metricRegistry, metrics);
178 } catch (RuntimeException ex) {
179 logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", publisher.getClass().getSimpleName(), ex);
183 , startTime, period, TimeUnit.MILLISECONDS);
186 logger.info("Metric Service is not enabled, hence policies not getting scheduled");
192 public long getStartTime() {
193 return this.startTime;
197 public long getPeriod() {
202 public Publisher[] getPublishers() {
203 return this.publishers;
207 public void reset() {
208 for(Metric metric:this.metrics){
213 private ThreadFactory getThreadFactory(final boolean isDaemon){
214 return new ThreadFactory() {
215 public Thread newThread(Runnable r) {
216 Thread t = Executors.defaultThreadFactory().newThread(r);
217 t.setDaemon(isDaemon);