2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.metricservice.impl;
28 import java.util.ArrayList;
29 import java.util.List;
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;
42 public class MetricRegistryImpl implements MetricRegistry {
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<>();
48 public MetricRegistryImpl(String name) {
53 public boolean register(Metric metric) {
54 return (concurrentMetricMap.putIfAbsent(metric.name(), metric) == null);
58 public void attach(PublishingPolicy publishPolicy) {
63 public MetricBuilderFactory metricBuilderFactory() {
64 return new MetricBuilderFactoryImpl();
68 public PolicyBuilderFactory policyBuilderFactory() {
69 return new PolicyBuilderFactoryImpl() ;
73 public Counter counter(String value) {
74 Metric metric = concurrentMetricMap.get(value);
75 if (metric instanceof Counter) {
76 return (Counter)metric;
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);
89 return counterList.toArray(new Counter[counterList.size()]);
93 public Metric[] metrics() {
94 java.util.Collection<Metric> var = concurrentMetricMap.values();
95 return var.toArray(new Metric[var.size()]);
99 public Metric metric(String metricName) {
100 return concurrentMetricMap.get(metricName);
104 public void dispose() {
105 concurrentMetricMap.clear();