aboutsummaryrefslogtreecommitdiffstats
path: root/appc-metric/appc-metric-bundle/src/main/java/org/openecomp/appc/metricservice/policy/impl/ScheduledPublishingPolicyImpl.java
blob: 8933150f39b90f30ea71fd636a411574c3cb0cc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.metricservice.policy.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import org.onap.appc.configuration.Configuration;
import org.onap.appc.configuration.ConfigurationFactory;
import org.onap.appc.exceptions.APPCException;
import org.onap.appc.metricservice.MetricRegistry;
import org.onap.appc.metricservice.MetricService;
import org.onap.appc.metricservice.Publisher;
import org.onap.appc.metricservice.metric.Metric;
import org.onap.appc.metricservice.policy.ScheduledPublishingPolicy;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;


public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy {
    private long startTime;
    private long period;
    private Publisher[] publishers;
    private Metric[] metrics;
    private MetricRegistry metricRegistry;
    private static final EELFLogger logger = EELFManager.getInstance().getLogger(ScheduledPublishingPolicyImpl.class);
    private ScheduledExecutorService scheduleExecutor;
    private Configuration configuration;

    public ScheduledPublishingPolicyImpl(long startTime, long period, Publisher[] publishers, Metric[] metrics) {
        this.startTime = startTime;
        this.period = period;
        this.publishers = publishers;
        this.metrics = metrics;
        this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
    }

    public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) {
        configuration = ConfigurationFactory.getConfiguration();
        Properties properties=configuration.getProperties();
        if(properties!=null){
            if(properties.getProperty("schedule.policy.metric.period")!=null && properties.getProperty("schedule.policy.metric.start.time")!=null){
                this.startTime = getConfigStartTime(properties);
                this.period = getConfigPeriod(properties);
                logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
            }else if(properties.getProperty("schedule.policy.metric.period")!=null){
                this.startTime=1;
                this.period=getConfigPeriod(properties);
                logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);

            }else if(properties.getProperty("schedule.policy.metric.period")==null && properties.getProperty("schedule.policy.metric.start.time")!=null){
                this.startTime=getConfigStartTime("00:00:00",properties);
                this.period=(24*60*60*1000)-1;
                logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);

            }else{
                logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
                this.startTime = 1;
                this.period = 100000;
                logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);

            }
        } else  {
            logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms");
            this.startTime = 1;
            this.period = 100000;
            logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period);
        }
        this.publishers = publishers;
        this.metrics = metrics;
        this.scheduleExecutor= Executors.newSingleThreadScheduledExecutor(getThreadFactory(true));
    }

    private long getConfigPeriod(Properties properties) {
        String period=properties.getProperty("schedule.policy.metric.period");
        logger.info("Metric period : " +period);
        long periodInMs=Integer.parseInt(period)*1000;
        logger.info("Metric period in long : " +periodInMs);
        return periodInMs;
    }

    private long getTimeInMs(String time) {
        String[] strings=time.split(":");
        if(strings.length==3) {
            long hour = Integer.parseInt(strings[0]) * 60 * 60 * 1000;
            long min = Integer.parseInt(strings[1]) * 60 * 1000;
            long sec = Integer.parseInt(strings[2]) * 1000;
            return hour+min+sec;
        }else{
            return 0;
        }

    }



    private long getConfigStartTime(Properties properties) {
        String startTime=properties.getProperty("schedule.policy.metric.start.time");
        if(startTime!=null){
            long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
            long period=getConfigPeriod(properties);
            if(timeDiff>=0){
                return timeDiff;
            }else{
                return period-((timeDiff*-1)%period);
            }
        }
        return 0;
    }

    private long getConfigStartTime(String startTime,Properties properties) {
        if(startTime!=null){
            long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date())));
            long period=getConfigPeriod(properties);
            if(timeDiff>=0){
                return timeDiff%period;
            }else{
                return period-((timeDiff*-1)%period);
            }
        }
        return 0;
    }
    @Override
    public void onMetricChange(Metric metric) throws APPCException {
        //TODO
    }

    @Override
    public Metric[] metrics() {
        return metrics;
    }

    @Override
    public void init() {
        Properties properties=configuration.getProperties();
        boolean isMetricEnabled=false;
        if(properties!=null){
            String metricProperty=properties.getProperty("metric.enabled");
            if(metricProperty!=null){
                isMetricEnabled=Boolean.valueOf(metricProperty);
            }
        }
        if(isMetricEnabled){
            logger.info("Metric Service is enabled, hence policies getting scheduled");
            for(final Publisher publisher:this.getPublishers()){
                scheduleExecutor.scheduleWithFixedDelay(new Runnable()
                {
                    public void run() {
                        try {
                            publisher.publish(metricRegistry, metrics);
                            reset();
                        } catch (RuntimeException ex) {
                            logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", publisher.getClass().getSimpleName(), ex);
                        }
                    }
                }
                        , startTime, period, TimeUnit.MILLISECONDS);
            }
        }else{
            logger.info("Metric Service is not enabled, hence policies not getting scheduled");

        }
    }

    @Override
    public long getStartTime() {
        return this.startTime;
    }

    @Override
    public long getPeriod() {
        return this.period;
    }

    @Override
    public Publisher[] getPublishers() {
        return this.publishers;
    }

    @Override
    public void reset() {
        for(Metric metric:this.metrics){
            metric.reset();}
    }


    private  ThreadFactory getThreadFactory(final boolean isDaemon){
        return new ThreadFactory() {
            public Thread newThread(Runnable r) {
                Thread t = Executors.defaultThreadFactory().newThread(r);
                t.setDaemon(isDaemon);
                return t;
            }
        };
    }

}