First part of onap rename
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / openecomp / appc / metricservice / policy / impl / ScheduledPublishingPolicyImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.metricservice.policy.impl;
26
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.Properties;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.ScheduledExecutorService;
32 import java.util.concurrent.ThreadFactory;
33 import java.util.concurrent.TimeUnit;
34
35 import org.onap.appc.configuration.Configuration;
36 import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.appc.exceptions.APPCException;
38 import org.onap.appc.metricservice.MetricRegistry;
39 import org.onap.appc.metricservice.MetricService;
40 import org.onap.appc.metricservice.Publisher;
41 import org.onap.appc.metricservice.metric.Metric;
42 import org.onap.appc.metricservice.policy.ScheduledPublishingPolicy;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45
46
47 public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy {
48     private long startTime;
49     private long period;
50     private Publisher[] publishers;
51     private Metric[] metrics;
52     private MetricRegistry metricRegistry;
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ScheduledPublishingPolicyImpl.class);
54     private ScheduledExecutorService scheduleExecutor;
55     private Configuration configuration;
56
57     public ScheduledPublishingPolicyImpl(long startTime, long period, Publisher[] publishers, Metric[] metrics) {
58         this.startTime = startTime;
59         this.period = period;
60         this.publishers = publishers;
61         this.metrics = metrics;
62         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
63     }
64
65     public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) {
66         configuration = ConfigurationFactory.getConfiguration();
67         Properties properties=configuration.getProperties();
68         if(properties!=null){
69             if(properties.getProperty("schedule.policy.metric.period")!=null && properties.getProperty("schedule.policy.metric.start.time")!=null){
70                 this.startTime = getConfigStartTime(properties);
71                 this.period = getConfigPeriod(properties);
72                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
73             }else if(properties.getProperty("schedule.policy.metric.period")!=null){
74                 this.startTime=1;
75                 this.period=getConfigPeriod(properties);
76                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
77
78             }else if(properties.getProperty("schedule.policy.metric.period")==null && properties.getProperty("schedule.policy.metric.start.time")!=null){
79                 this.startTime=getConfigStartTime("00:00:00",properties);
80                 this.period=(24*60*60*1000)-1;
81                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
82
83             }else{
84                 logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
85                 this.startTime = 1;
86                 this.period = 100000;
87                 logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
88
89             }
90         } else  {
91             logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
92             this.startTime = 1;
93             this.period = 100000;
94             logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
95         }
96         this.publishers = publishers;
97         this.metrics = metrics;
98         this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
99     }
100
101     private long getConfigPeriod(Properties properties) {
102         String period=properties.getProperty("schedule.policy.metric.period");
103         logger.info("Metric period : " +period);
104         long periodInMs=Integer.parseInt(period)*1000;
105         logger.info("Metric period in long : " +periodInMs);
106         return periodInMs;
107     }
108
109     private long getTimeInMs(String time) {
110         String[] strings=time.split(":");
111         if(strings.length==3) {
112             long hour = Integer.parseInt(strings[0]) * 60 * 60 * 1000;
113             long min = Integer.parseInt(strings[1]) * 60 * 1000;
114             long sec = Integer.parseInt(strings[2]) * 1000;
115             return hour+min+sec;
116         }else{
117             return 0;
118         }
119
120     }
121
122
123
124     private long getConfigStartTime(Properties properties) {
125         String startTime=properties.getProperty("schedule.policy.metric.start.time");
126         if(startTime!=null){
127             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
128             long period=getConfigPeriod(properties);
129             if(timeDiff>=0){
130                 return timeDiff;
131             }else{
132                 return period-((timeDiff*-1)%period);
133             }
134         }
135         return 0;
136     }
137
138     private long getConfigStartTime(String startTime,Properties properties) {
139         if(startTime!=null){
140             long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
141             long period=getConfigPeriod(properties);
142             if(timeDiff>=0){
143                 return timeDiff%period;
144             }else{
145                 return period-((timeDiff*-1)%period);
146             }
147         }
148         return 0;
149     }
150     @Override
151     public void onMetricChange(Metric metric) throws APPCException {
152         //TODO
153     }
154
155     @Override
156     public Metric[] metrics() {
157         return metrics;
158     }
159
160     @Override
161     public void init() {
162         Properties properties=configuration.getProperties();
163         boolean isMetricEnabled=false;
164         if(properties!=null){
165             String metricProperty=properties.getProperty("metric.enabled");
166             if(metricProperty!=null){
167                 isMetricEnabled=Boolean.valueOf(metricProperty);
168             }
169         }
170         if(isMetricEnabled){
171             logger.info("Metric Service is enabled, hence policies getting scheduled");
172             for(final Publisher publisher:this.getPublishers()){
173                 scheduleExecutor.scheduleWithFixedDelay(new Runnable()
174                 {
175                     public void run() {
176                         try {
177                             publisher.publish(metricRegistry, metrics);
178                             reset();
179                         } catch (RuntimeException ex) {
180                             logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", publisher.getClass().getSimpleName(), ex);
181                         }
182                     }
183                 }
184                         , startTime, period, TimeUnit.MILLISECONDS);
185             }
186         }else{
187             logger.info("Metric Service is not enabled, hence policies not getting scheduled");
188
189         }
190     }
191
192     @Override
193     public long getStartTime() {
194         return this.startTime;
195     }
196
197     @Override
198     public long getPeriod() {
199         return this.period;
200     }
201
202     @Override
203     public Publisher[] getPublishers() {
204         return this.publishers;
205     }
206
207     @Override
208     public void reset() {
209         for(Metric metric:this.metrics){
210             metric.reset();}
211     }
212
213
214     private  ThreadFactory getThreadFactory(final boolean isDaemon){
215         return new ThreadFactory() {
216             public Thread newThread(Runnable r) {
217                 Thread t = Executors.defaultThreadFactory().newThread(r);
218                 t.setDaemon(isDaemon);
219                 return t;
220             }
221         };
222     }
223
224 }