3d9f383ca9ea244b3e3b39068f8ba94036739fc2
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / onap / appc / metricservice / metric / impl / DispatchingFuntionMetricImpl.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.DispatchingFuntionMetric;
33 import org.onap.appc.metricservice.metric.MetricType;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37
38 public class DispatchingFuntionMetricImpl implements DispatchingFuntionMetric {
39     private String name;
40     private MetricType metricType;
41     private AtomicLong acceptedRequested = new AtomicLong();
42     private AtomicLong rejectedRequest = 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(DispatchingFuntionMetricImpl.class);
49
50     public DispatchingFuntionMetricImpl(String name, MetricType metricType, long acceptedRequested,
51                                         long rejectedRequest) {
52         this.name = name;
53         this.metricType = metricType;
54         this.acceptedRequested.set(acceptedRequested);
55         this.rejectedRequest.set(rejectedRequest);
56     }
57
58     @Override
59     public void incrementAcceptedRequest() {
60         this.acceptedRequested.incrementAndGet();
61     }
62
63     @Override
64     public void incrementRejectedRequest() {
65         this.rejectedRequest.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 + "[" + acceptedRequested.get() + "," + rejectedRequest.get() + "]" + "@"
76                     + (acceptedRequested.get() + rejectedRequest.get());
77             logger.debug("Current value of the metric " + this.name + " :" + value);
78             return value;
79
80         } catch (Exception e) {
81             logger.debug("Cant format the date.",e);
82         }
83         return null;
84
85     }
86
87     @Override
88     public String name() {
89         return this.name;
90     }
91
92     @Override
93     public void reset() {
94         this.acceptedRequested.set(0);
95         this.rejectedRequest.set(0);
96         Calendar cal = Calendar.getInstance();
97         lastResetTime = dateTimeFormat.format(cal.getTime());
98     }
99
100     @Override
101     public MetricType type() {
102         return this.metricType;
103     }
104
105     @Override
106     public HashMap<String, String> getMetricsOutput() {
107         HashMap<String, String> dispatcherMetricResult = new HashMap<>();
108         dispatcherMetricResult.put("Total Received messages",
109                 Long.toString(acceptedRequested.get() + rejectedRequest.get()));
110         dispatcherMetricResult.put("Total Rejected messages", Long.toString(rejectedRequest.get()));
111         return dispatcherMetricResult;
112     }
113
114     @Override
115     public String toString() {
116         return this.value();
117     }
118
119     @Override
120     public String getLastModified() {
121         return lastResetTime;
122     }
123 }