Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +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.RestTelemetryConfig; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame^] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * A configuration file contains REST telemetry parameters. |
| 31 | */ |
| 32 | public final class DefaultRestTelemetryConfig implements RestTelemetryConfig { |
| 33 | |
| 34 | private final String address; |
| 35 | private final int port; |
| 36 | private final String endpoint; |
| 37 | private final String method; |
| 38 | private final String requestMediaType; |
| 39 | private final String responseMediaType; |
| 40 | private final Map<String, Object> configMap; |
| 41 | |
| 42 | private DefaultRestTelemetryConfig(String address, int port, String endpoint, |
| 43 | String method, String requestMediaType, |
| 44 | String responseMediaType, |
| 45 | Map<String, Object> configMap) { |
| 46 | this.address = address; |
| 47 | this.port = port; |
| 48 | this.endpoint = endpoint; |
| 49 | this.method = method; |
| 50 | this.requestMediaType = requestMediaType; |
| 51 | this.responseMediaType = responseMediaType; |
| 52 | this.configMap = configMap; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public String address() { |
| 57 | return address; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int port() { |
| 62 | return port; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public String endpoint() { |
| 67 | return endpoint; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String method() { |
| 72 | return method; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String requestMediaType() { |
| 77 | return requestMediaType; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public String responseMediaType() { |
| 82 | return responseMediaType; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Map<String, Object> configMap() { |
| 87 | if (configMap != null) { |
| 88 | return ImmutableMap.copyOf(configMap); |
| 89 | } else { |
| 90 | return Maps.newConcurrentMap(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean equals(Object obj) { |
| 96 | if (this == obj) { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | if (obj instanceof DefaultRestTelemetryConfig) { |
| 101 | final DefaultRestTelemetryConfig other = (DefaultRestTelemetryConfig) obj; |
| 102 | return Objects.equals(this.address, other.address) && |
| 103 | Objects.equals(this.port, other.port) && |
| 104 | Objects.equals(this.endpoint, other.endpoint) && |
| 105 | Objects.equals(this.method, other.method) && |
| 106 | Objects.equals(this.requestMediaType, other.requestMediaType) && |
| 107 | Objects.equals(this.responseMediaType, other.responseMediaType) && |
| 108 | Objects.equals(this.configMap, other.configMap); |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public int hashCode() { |
| 115 | return Objects.hash(address, port, endpoint, method, requestMediaType, |
| 116 | responseMediaType, configMap); |
| 117 | } |
| 118 | |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 119 | @Override |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame^] | 120 | public String toString() { |
| 121 | return toStringHelper(this) |
| 122 | .add("address", address) |
| 123 | .add("port", port) |
| 124 | .add("endpoint", endpoint) |
| 125 | .add("method", method) |
| 126 | .add("requestMediaType", requestMediaType) |
| 127 | .add("responseMediaType", responseMediaType) |
| 128 | .add("configMap", configMap) |
| 129 | .toString(); |
| 130 | } |
| 131 | |
| 132 | @Override |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 133 | public TelemetryConfig.Builder createBuilder() { |
| 134 | return new DefaultBuilder(); |
| 135 | } |
| 136 | |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 137 | /** |
| 138 | * Builder class of DefaultRestTelemetryConfig. |
| 139 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 140 | public static final class DefaultBuilder implements Builder { |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 141 | |
| 142 | private String address; |
| 143 | private int port; |
| 144 | private String endpoint; |
| 145 | private String method; |
| 146 | private String requestMediaType; |
| 147 | private String responseMediaType; |
| 148 | private Map<String, Object> configMap; |
| 149 | |
| 150 | @Override |
| 151 | public Builder withAddress(String address) { |
| 152 | this.address = address; |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Builder withPort(int port) { |
| 158 | this.port = port; |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Builder withEndpoint(String endpoint) { |
| 164 | this.endpoint = endpoint; |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Builder withMethod(String method) { |
| 170 | this.method = method; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Builder withRequestMediaType(String mediaType) { |
| 176 | this.requestMediaType = mediaType; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Builder withResponseMediaType(String mediaType) { |
| 182 | this.responseMediaType = mediaType; |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Builder withConfigMap(Map<String, Object> configMap) { |
| 188 | this.configMap = configMap; |
| 189 | return this; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public RestTelemetryConfig build() { |
| 194 | checkNotNull(address, "REST server address cannot be null"); |
| 195 | checkNotNull(endpoint, "REST server endpoint cannot be null"); |
| 196 | |
| 197 | return new DefaultRestTelemetryConfig(address, port, endpoint, |
| 198 | method, requestMediaType, responseMediaType, configMap); |
| 199 | } |
| 200 | } |
| 201 | } |