Second part of onap rename
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / openecomp / appc / metricservice / metric / impl / DispatchingFuntionMetricImpl.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.DispatchingFuntionMetric;
34 import org.onap.appc.metricservice.metric.MetricType;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38
39 public class DispatchingFuntionMetricImpl implements DispatchingFuntionMetric {
40     private String name;
41     private MetricType metricType;
42     private AtomicLong acceptedRequested = new AtomicLong();
43     private AtomicLong rejectedRequest = new AtomicLong();
44
45     private final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
46     private final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("YYYY-MM-dd:HH:mm:ss");
47
48     private String lastResetTime = dateTimeFormat.format(Calendar.getInstance().getTime());
49     private static final EELFLogger logger = EELFManager.getInstance().getLogger(DispatchingFuntionMetricImpl.class);
50
51     public DispatchingFuntionMetricImpl(String name, MetricType metricType, long acceptedRequested,
52                                         long rejectedRequest) {
53         this.name = name;
54         this.metricType = metricType;
55         this.acceptedRequested.set(acceptedRequested);
56         this.rejectedRequest.set(rejectedRequest);
57     }
58
59     @Override
60     public void incrementAcceptedRequest() {
61         this.acceptedRequested.incrementAndGet();
62     }
63
64     @Override
65     public void incrementRejectedRequest() {
66         this.rejectedRequest.incrementAndGet();
67     }
68
69     @Override
70     public String value() {
71         logger.debug("Value is getting calculated for metric :" + this.name);
72         try {
73             Calendar cal = Calendar.getInstance();
74             cal.setTimeZone(TimeZone.getTimeZone("UTC"));
75             String date = dateFormat.format(cal.getTime());
76             String value = date + "[" + acceptedRequested.get() + "," + rejectedRequest.get() + "]" + "@"
77                     + (acceptedRequested.get() + rejectedRequest.get());
78             logger.debug("Current value of the metric " + this.name + " :" + value);
79             return value;
80
81         } catch (Exception e) {
82             logger.debug("Cant format the date.",e);
83         }
84         return null;
85
86     }
87
88     @Override
89     public String name() {
90         return this.name;
91     }
92
93     @Override
94     public void reset() {
95         this.acceptedRequested.set(0);
96         this.rejectedRequest.set(0);
97         Calendar cal = Calendar.getInstance();
98         lastResetTime = dateTimeFormat.format(cal.getTime());
99     }
100
101     @Override
102     public MetricType type() {
103         return this.metricType;
104     }
105
106     @Override
107     public HashMap<String, String> getMetricsOutput() {
108         HashMap<String, String> dispatcherMetricResult = new HashMap<>();
109         dispatcherMetricResult.put("Total Received messages",
110                 Long.toString(acceptedRequested.get() + rejectedRequest.get()));
111         dispatcherMetricResult.put("Total Rejected messages", Long.toString(rejectedRequest.get()));
112         return dispatcherMetricResult;
113     }
114
115     @Override
116     public String toString() {
117         return this.value();
118     }
119
120     @Override
121     public String getLastModified() {
122         return lastResetTime;
123     }
124 }