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