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