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