Updating licenses in all files
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / openecomp / appc / metricservice / metric / impl / DispatchingFuntionMetricImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.metricservice.metric.impl;
24
25 import java.text.SimpleDateFormat;
26 import java.util.Calendar;
27 import java.util.HashMap;
28 import java.util.TimeZone;
29 import java.util.concurrent.atomic.AtomicLong;
30
31 import org.openecomp.appc.metricservice.metric.DispatchingFuntionMetric;
32 import org.openecomp.appc.metricservice.metric.MetricType;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35
36
37 public class DispatchingFuntionMetricImpl implements DispatchingFuntionMetric {
38     private String name;
39     private MetricType metricType;
40     private AtomicLong acceptedRequested = new AtomicLong();
41     private AtomicLong rejectedRequest = new AtomicLong();
42
43     private final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
44     private final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("YYYY-MM-dd:HH:mm:ss");
45
46     private String lastResetTime = dateTimeFormat.format(Calendar.getInstance().getTime());
47     private static final EELFLogger logger = EELFManager.getInstance().getLogger(DispatchingFuntionMetricImpl.class);
48
49     public DispatchingFuntionMetricImpl(String name, MetricType metricType, long acceptedRequested,
50                                         long rejectedRequest) {
51         this.name = name;
52         this.metricType = metricType;
53         this.acceptedRequested.set(acceptedRequested);
54         this.rejectedRequest.set(rejectedRequest);
55     }
56
57     @Override
58     public void incrementAcceptedRequest() {
59         this.acceptedRequested.incrementAndGet();
60     }
61
62     @Override
63     public void incrementRejectedRequest() {
64         this.rejectedRequest.incrementAndGet();
65     }
66
67     @Override
68     public String value() {
69         logger.debug("Value is getting calculated for metric :" + this.name);
70         try {
71             Calendar cal = Calendar.getInstance();
72             cal.setTimeZone(TimeZone.getTimeZone("UTC"));
73             String date = dateFormat.format(cal.getTime());
74             String value = date + "[" + acceptedRequested.get() + "," + rejectedRequest.get() + "]" + "@"
75                     + (acceptedRequested.get() + rejectedRequest.get());
76             logger.debug("Current value of the metric " + this.name + " :" + value);
77             return value;
78
79         } catch (Exception e) {
80             logger.debug("Cant format the date.",e);
81         }
82         return null;
83
84     }
85
86     @Override
87     public String name() {
88         return this.name;
89     }
90
91     @Override
92     public void reset() {
93         this.acceptedRequested.set(0);
94         this.rejectedRequest.set(0);
95         Calendar cal = Calendar.getInstance();
96         lastResetTime = dateTimeFormat.format(cal.getTime());
97     }
98
99     @Override
100     public MetricType type() {
101         return this.metricType;
102     }
103
104     @Override
105     public HashMap<String, String> getMetricsOutput() {
106         HashMap<String, String> dispatcherMetricResult = new HashMap<>();
107         dispatcherMetricResult.put("Total Received messages",
108                 Long.toString(acceptedRequested.get() + rejectedRequest.get()));
109         dispatcherMetricResult.put("Total Rejected messages", Long.toString(rejectedRequest.get()));
110         return dispatcherMetricResult;
111     }
112
113     @Override
114     public String toString() {
115         return this.value();
116     }
117
118     @Override
119     public String getLastModified() {
120         return lastResetTime;
121     }
122 }