boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +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.config; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onosproject.openstacktelemetry.api.config.PrometheusTelemetryConfig; |
| 21 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * A configuration file contains Prometheus telemetry parameters. |
| 31 | */ |
| 32 | public final class DefaultPrometheusTelemetryConfig implements PrometheusTelemetryConfig { |
| 33 | |
| 34 | private final String address; |
| 35 | private final int port; |
| 36 | private final Map<String, Object> configMap; |
| 37 | |
| 38 | private DefaultPrometheusTelemetryConfig(String address, |
| 39 | int port, |
| 40 | Map<String, Object> configMap) { |
| 41 | this.address = address; |
| 42 | this.port = port; |
| 43 | this.configMap = configMap; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String address() { |
| 48 | return address; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public int port() { |
| 53 | return port; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Map<String, Object> configMap() { |
| 58 | if (configMap != null) { |
| 59 | return ImmutableMap.copyOf(configMap); |
| 60 | } else { |
| 61 | return Maps.newConcurrentMap(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean equals(Object obj) { |
| 67 | if (this == obj) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | if (obj instanceof DefaultPrometheusTelemetryConfig) { |
| 72 | final DefaultPrometheusTelemetryConfig other = (DefaultPrometheusTelemetryConfig) obj; |
| 73 | return Objects.equals(this.address, other.address) && |
| 74 | Objects.equals(this.port, other.port) && |
| 75 | Objects.equals(this.configMap, other.configMap); |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int hashCode() { |
| 82 | return Objects.hash(address, port, configMap); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return toStringHelper(this) |
| 88 | .add("address", address) |
| 89 | .add("port", port) |
| 90 | .add("configMap", configMap) |
| 91 | .toString(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public TelemetryConfig.Builder createBuilder() { |
| 96 | return new DefaultBuilder(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Builder class of DefaultPrometheusTelemetryConfig. |
| 101 | */ |
| 102 | public static final class DefaultBuilder implements Builder { |
| 103 | private String address; |
| 104 | private int port; |
| 105 | private Map<String, Object> configMap; |
| 106 | |
| 107 | @Override |
| 108 | public Builder withAddress(String address) { |
| 109 | this.address = address; |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Builder withPort(int port) { |
| 115 | this.port = port; |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public Builder withConfigMap(Map<String, Object> configMap) { |
| 121 | this.configMap = configMap; |
| 122 | return this; |
| 123 | } |
| 124 | @Override |
| 125 | public PrometheusTelemetryConfig build() { |
| 126 | checkNotNull(address, "Prometheus exporter binding address cannot be null"); |
| 127 | checkNotNull(configMap, "Config map cannot be null"); |
| 128 | return new DefaultPrometheusTelemetryConfig(address, port, configMap); |
| 129 | } |
| 130 | } |
| 131 | } |