Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onosproject.openstacktelemetry.api.TelemetryConfigProvider; |
| 21 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * Default telemetry configuration provider implementation. |
| 30 | */ |
| 31 | public class DefaultTelemetryConfigProvider implements TelemetryConfigProvider { |
| 32 | |
| 33 | protected final Map<String, TelemetryConfig> configs = Maps.newConcurrentMap(); |
| 34 | |
| 35 | @Override |
| 36 | public Set<TelemetryConfig> getTelemetryConfigs() { |
| 37 | return ImmutableSet.copyOf(configs.values()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Adds the specified configuration to the provider. If a configuration with |
| 42 | * the name does not exist yet, the specified one will be added. Otherwise, |
| 43 | * the existing configuration will be merged with the new one and the result will |
| 44 | * be registered. |
| 45 | * |
| 46 | * @param config telemetry configuration to be provided |
| 47 | * @return registered configuration |
| 48 | */ |
| 49 | public TelemetryConfig addConfig(TelemetryConfig config) { |
| 50 | return configs.compute(config.name(), (name, oldConfig) -> |
| 51 | oldConfig == null ? config : oldConfig.merge(config)); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Removes the specified configuration from the provider. |
| 56 | * |
| 57 | * @param config telemetry configuration |
| 58 | */ |
| 59 | public void removeConfig(TelemetryConfig config) { |
| 60 | configs.remove(config.name()); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public String toString() { |
| 65 | return toStringHelper(this).add("configs", configs).toString(); |
| 66 | } |
| 67 | } |