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