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