Moving all files to root directory
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / openecomp / appc / metricservice / metric / impl / DispatchingFuntionMetricImpl.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.DispatchingFuntionMetric;
30 import org.openecomp.appc.metricservice.metric.MetricType;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34
35 public class DispatchingFuntionMetricImpl implements DispatchingFuntionMetric {
36     private  String name;
37     private  MetricType metricType;
38     private long acceptedRequested;
39     private long rejectedRequest;
40     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
41     private static final EELFLogger logger = EELFManager.getInstance().getLogger(DmaapRequestCounterMetricImpl.class);
42
43     public DispatchingFuntionMetricImpl(String name, MetricType metricType, long acceptedRequested, long rejectedRequest) {
44         this.name = name;
45         this.metricType = metricType;
46         this.acceptedRequested = acceptedRequested;
47         this.rejectedRequest = rejectedRequest;
48     }
49
50     @Override
51     public void incrementAcceptedRequest() {
52         this.acceptedRequested+=1;
53     }
54
55     @Override
56     public void incrementRejectedRequest() {
57         this.rejectedRequest+=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+"["+acceptedRequested+","+rejectedRequest+"]"+"@"+(acceptedRequested+rejectedRequest);
68             logger.debug("Current value of the metric "+this.name+" :"+value);
69             return value ;
70
71         }catch (Exception e){
72             logger.debug("Cant format the date.");
73         }
74         return  null;
75
76     }
77
78     @Override
79     public String name() {
80         return this.name;
81     }
82
83     @Override
84     public void reset() {
85         this.acceptedRequested=0;
86         this.rejectedRequest=0;
87     }
88
89     @Override
90     public MetricType type() {
91         return this.metricType;
92     }
93     @Override
94     public String toString() {
95         return this.value();
96     }
97 }