877fa6e310da0545df848b216e222165d3da99ae
[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.Date;
27 import java.util.TimeZone;
28
29 import org.openecomp.appc.metricservice.metric.MetricType;
30 import org.openecomp.appc.metricservice.metric.DmaapRequestCounterMetric;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34
35 public class DmaapRequestCounterMetricImpl implements DmaapRequestCounterMetric {
36
37     private  String name;
38     private  MetricType metricType;
39     private long recievedMessage;
40     private long publishedMessage;
41     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
42     private static final EELFLogger logger = EELFManager.getInstance().getLogger(DmaapRequestCounterMetricImpl.class);
43     public DmaapRequestCounterMetricImpl(String name, MetricType metricType, long recievedMessage, long publishedMessage) {
44         this.name = name;
45         this.metricType = metricType;
46         this.recievedMessage = recievedMessage;
47         this.publishedMessage=publishedMessage;
48     }
49
50     @Override
51     public void incrementRecievedMessage() {
52         this.recievedMessage+=1;
53     }
54
55     @Override
56     public void incrementPublishedMessage() {
57         this.publishedMessage+=1;
58     }
59
60     @Override
61     public String value() {
62         logger.debug("Value is getting calculated for metric :" + this.name);
63         try{
64             Calendar cal = Calendar.getInstance();
65             cal.setTimeZone(TimeZone.getTimeZone("UTC"));
66             String  date=dateFormat.format(cal.getTime());
67             String value=date+"["+recievedMessage+"],["+publishedMessage+"]";
68             logger.debug("Current value of the metric "+this.name+" :"+value);
69             return value;
70         }catch (Exception e){
71             logger.debug("Cant format the date.");
72         }
73         return null;
74     }
75
76     @Override
77     public String name() {
78         return this.name;
79     }
80
81     @Override
82     public void reset() {
83         this.recievedMessage=0;
84         this.publishedMessage=0;
85     }
86
87     @Override
88     public MetricType type() {
89         return this.metricType;
90     }
91
92     @Override
93     public String toString() {
94         return this.value();
95     }
96 }