8c3dd49c38c2b3f5a90ac3a5a567dd776ea21b9d
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / openecomp / appc / metricservice / policy / impl / ScheduledPublishingPolicyImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.metricservice.policy.impl;
23
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;
31
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;
42
43
44 public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy {
45     private long startTime;
46     private long period;
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;
53
54     public ScheduledPublishingPolicyImpl(long startTime, long period, Publisher[] publishers, Metric[] metrics) {
55         this.startTime = startTime;
56         this.period = period;
57         this.publishers = publishers;
58         this.metrics = metrics;
59         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
60     }
61
62     public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) {
63         configuration = ConfigurationFactory.getConfiguration();
64         Properties properties=configuration.getProperties();
65         if(properties!=null){
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){
71                 this.startTime=1;
72                 this.period=getConfigPeriod(properties);
73                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
74
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);
79
80             }else{
81                 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
82                 this.startTime = 1;
83                 this.period = 100000;
84                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
85
86             }
87         } else  {
88             logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
89             this.startTime = 1;
90             this.period = 100000;
91             logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
92         }
93         this.publishers = publishers;
94         this.metrics = metrics;
95         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
96     }
97
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);
103         return periodInMs;
104     }
105
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;
112             return hour+min+sec;
113         }else{
114             return 0;
115         }
116
117     }
118
119
120
121     private long getConfigStartTime(Properties properties) {
122         String startTime=properties.getProperty("schedule.policy.metric.start.time");
123         if(startTime!=null){
124             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
125             long period=getConfigPeriod(properties);
126             if(timeDiff>=0){
127                 return timeDiff;
128             }else{
129                 return period-((timeDiff*-1)%period);
130             }
131         }
132         return 0;
133     }
134
135     private long getConfigStartTime(String startTime,Properties properties) {
136         if(startTime!=null){
137             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
138             long period=getConfigPeriod(properties);
139             if(timeDiff>=0){
140                 return timeDiff%period;
141             }else{
142                 return period-((timeDiff*-1)%period);
143             }
144         }
145         return 0;
146     }
147     @Override
148     public void onMetricChange(Metric metric) throws APPCException {
149         //TODO
150     }
151
152     @Override
153     public Metric[] metrics() {
154         return metrics;
155     }
156
157     @Override
158     public void init() {
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);
165             }
166         }
167         if(isMetricEnabled){
168             logger.info("Metric Service is enabled, hence policies getting scheduled");
169             for(final Publisher publisher:this.getPublishers()){
170                 scheduleExecutor.scheduleWithFixedDelay(new Runnable()
171                 {
172                     public void run() {
173                         try {
174                             publisher.publish(metricRegistry, metrics);
175                             reset();
176                         } catch (RuntimeException ex) {
177                             logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", publisher.getClass().getSimpleName(), ex);
178                         }
179                     }
180                 }
181                         , startTime, period, TimeUnit.MILLISECONDS);
182             }
183         }else{
184             logger.info("Metric Service is not enabled, hence policies not getting scheduled");
185
186         }
187     }
188
189     @Override
190     public long getStartTime() {
191         return this.startTime;
192     }
193
194     @Override
195     public long getPeriod() {
196         return this.period;
197     }
198
199     @Override
200     public Publisher[] getPublishers() {
201         return this.publishers;
202     }
203
204     @Override
205     public void reset() {
206         for(Metric metric:this.metrics){
207             metric.reset();}
208     }
209
210
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);
216                 return t;
217             }
218         };
219     }
220
221 }