Merge of new rebased code
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / openecomp / appc / metricservice / metric / impl / DispatchingFuntionMetricImpl.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.metric.impl;
23
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26 import java.util.HashMap;
27 import java.util.TimeZone;
28 import java.util.concurrent.atomic.AtomicLong;
29
30 import org.openecomp.appc.metricservice.metric.DispatchingFuntionMetric;
31 import org.openecomp.appc.metricservice.metric.MetricType;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34
35
36 public class DispatchingFuntionMetricImpl implements DispatchingFuntionMetric {
37     private String name;
38     private MetricType metricType;
39     private AtomicLong acceptedRequested = new AtomicLong();
40     private AtomicLong rejectedRequest = new AtomicLong();
41
42     private final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
43     private final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("YYYY-MM-dd:HH:mm:ss");
44
45     private String lastResetTime = dateTimeFormat.format(Calendar.getInstance().getTime());
46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(DispatchingFuntionMetricImpl.class);
47
48     public DispatchingFuntionMetricImpl(String name, MetricType metricType, long acceptedRequested,
49                                         long rejectedRequest) {
50         this.name = name;
51         this.metricType = metricType;
52         this.acceptedRequested.set(acceptedRequested);
53         this.rejectedRequest.set(rejectedRequest);
54     }
55
56     @Override
57     public void incrementAcceptedRequest() {
58         this.acceptedRequested.incrementAndGet();
59     }
60
61     @Override
62     public void incrementRejectedRequest() {
63         this.rejectedRequest.incrementAndGet();
64     }
65
66     @Override
67     public String value() {
68         logger.debug("Value is getting calculated for metric :" + this.name);
69         try {
70             Calendar cal = Calendar.getInstance();
71             cal.setTimeZone(TimeZone.getTimeZone("UTC"));
72             String date = dateFormat.format(cal.getTime());
73             String value = date + "[" + acceptedRequested.get() + "," + rejectedRequest.get() + "]" + "@"
74                     + (acceptedRequested.get() + rejectedRequest.get());
75             logger.debug("Current value of the metric " + this.name + " :" + value);
76             return value;
77
78         } catch (Exception e) {
79             logger.debug("Cant format the date.",e);
80         }
81         return null;
82
83     }
84
85     @Override
86     public String name() {
87         return this.name;
88     }
89
90     @Override
91     public void reset() {
92         this.acceptedRequested.set(0);
93         this.rejectedRequest.set(0);
94         Calendar cal = Calendar.getInstance();
95         lastResetTime = dateTimeFormat.format(cal.getTime());
96     }
97
98     @Override
99     public MetricType type() {
100         return this.metricType;
101     }
102
103     @Override
104     public HashMap<String, String> getMetricsOutput() {
105         HashMap<String, String> dispatcherMetricResult = new HashMap<>();
106         dispatcherMetricResult.put("Total Received messages",
107                 Long.toString(acceptedRequested.get() + rejectedRequest.get()));
108         dispatcherMetricResult.put("Total Rejected messages", Long.toString(rejectedRequest.get()));
109         return dispatcherMetricResult;
110     }
111
112     @Override
113     public String toString() {
114         return this.value();
115     }
116
117     @Override
118     public String getLastModified() {
119         return lastResetTime;
120     }
121 }