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