Add code coverage for metric-service-policy code
[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  * Modifications (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.metricservice.metric.impl;
27
28 import java.text.SimpleDateFormat;
29 import java.util.Calendar;
30 import java.util.HashMap;
31 import java.util.TimeZone;
32 import java.util.concurrent.atomic.AtomicLong;
33
34 import org.onap.appc.metricservice.metric.DispatchingFuntionMetric;
35 import org.onap.appc.metricservice.metric.MetricType;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38
39
40 public class DispatchingFuntionMetricImpl implements DispatchingFuntionMetric {
41     private String name;
42     private MetricType metricType;
43     private AtomicLong acceptedRequested = new AtomicLong();
44     private AtomicLong rejectedRequest = 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(DispatchingFuntionMetricImpl.class);
51
52     public DispatchingFuntionMetricImpl(String name, MetricType metricType, long acceptedRequested,
53                                         long rejectedRequest) {
54         this.name = name;
55         this.metricType = metricType;
56         this.acceptedRequested.set(acceptedRequested);
57         this.rejectedRequest.set(rejectedRequest);
58     }
59
60     @Override
61     public void incrementAcceptedRequest() {
62         this.acceptedRequested.incrementAndGet();
63     }
64
65     @Override
66     public void incrementRejectedRequest() {
67         this.rejectedRequest.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 + "[" + acceptedRequested.get() + "," + rejectedRequest.get() + "]" + "@"
78                     + (acceptedRequested.get() + rejectedRequest.get());
79             logger.debug("Current value of the metric " + this.name + " :" + value);
80             return value;
81
82         } catch (Exception e) {
83             logger.debug("Cant format the date.", e);
84         }
85         return null;
86
87     }
88
89     @Override
90     public String name() {
91         return this.name;
92     }
93
94     @Override
95     public void reset() {
96         this.acceptedRequested.set(0);
97         this.rejectedRequest.set(0);
98         Calendar cal = Calendar.getInstance();
99         lastResetTime = dateTimeFormat.format(cal.getTime());
100     }
101
102     @Override
103     public MetricType type() {
104         return this.metricType;
105     }
106
107     @Override
108     public HashMap<String, String> getMetricsOutput() {
109         HashMap<String, String> dispatcherMetricResult = new HashMap<>();
110         dispatcherMetricResult.put("Total Received messages",
111                 Long.toString(acceptedRequested.get() + rejectedRequest.get()));
112         dispatcherMetricResult.put("Total Rejected messages", Long.toString(rejectedRequest.get()));
113         return dispatcherMetricResult;
114     }
115
116     @Override
117     public String toString() {
118         return this.value();
119     }
120
121     @Override
122     public String getLastModified() {
123         return lastResetTime;
124     }
125 }