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