Fix for casting/subclassing in MetricRegistry
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / onap / appc / metricservice / impl / MetricRegistryImpl.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 Copyright (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.impl;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.concurrent.ConcurrentHashMap;
32 import org.onap.appc.metricservice.MetricRegistry;
33 import org.onap.appc.metricservice.metric.Counter;
34 import org.onap.appc.metricservice.metric.Metric;
35 import org.onap.appc.metricservice.metric.MetricBuilderFactory;
36 import org.onap.appc.metricservice.metric.impl.MetricBuilderFactoryImpl;
37 import org.onap.appc.metricservice.policy.PolicyBuilderFactory;
38 import org.onap.appc.metricservice.policy.PublishingPolicy;
39 import org.onap.appc.metricservice.policy.impl.PolicyBuilderFactoryImpl;
40
41
42 public class MetricRegistryImpl implements MetricRegistry {
43     private String name;
44     // Map can contain Counters, DispatchingFunctionMetrics and DMaapRequestCounterMetrics
45     // and there are methods to retrieve only the 'Counter' types
46     private Map<String, Metric> concurrentMetricMap = new ConcurrentHashMap<>();
47
48     public MetricRegistryImpl(String name) {
49         this.name = name;
50     }
51
52     @Override
53     public boolean register(Metric metric) {
54         return (concurrentMetricMap.putIfAbsent(metric.name(), metric) == null);
55     }
56
57     @Override
58     public void attach(PublishingPolicy publishPolicy) {
59 //TODO
60     }
61
62     @Override
63     public MetricBuilderFactory metricBuilderFactory() {
64         return new MetricBuilderFactoryImpl();
65     }
66
67     @Override
68     public PolicyBuilderFactory policyBuilderFactory() {
69         return new PolicyBuilderFactoryImpl() ;
70     }
71
72     @Override
73     public Counter counter(String value) {
74         Metric metric = concurrentMetricMap.get(value);
75         if (metric instanceof Counter) {
76             return (Counter)metric;
77         }
78         else return null;
79     }
80
81     @Override
82     public Counter[] counters() {
83         List<Counter> counterList = new ArrayList<>();
84         for (Metric m: concurrentMetricMap.values()) {
85             if (m instanceof Counter) {
86                 counterList.add((Counter) m);
87             }
88         }
89         return counterList.toArray(new Counter[counterList.size()]);
90     }
91
92     @Override
93     public Metric[] metrics() {
94         java.util.Collection<Metric> var = concurrentMetricMap.values();
95         return var.toArray(new Metric[var.size()]);
96     }
97
98     @Override
99     public Metric metric(String metricName) {
100         return concurrentMetricMap.get(metricName);
101     }
102
103     @Override
104     public void dispose() {
105         concurrentMetricMap.clear();
106     }
107 }