blob: a5977a500f0c51111bd01ea0f64a8a87e313f25c [file] [log] [blame]
Jian Li2cf1c0b2018-06-07 11:28:56 +09001/*
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 */
16package org.onosproject.openstacktelemetry.config;
17
18import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.Maps;
20import org.onosproject.openstacktelemetry.api.config.RestTelemetryConfig;
Jian Li52c11222018-06-07 11:39:17 +090021import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li69600e02018-12-24 13:21:18 +090022import org.onosproject.openstacktelemetry.api.config.TelemetryConfigProperties;
Jian Li2cf1c0b2018-06-07 11:28:56 +090023
24import java.util.Map;
25import java.util.Objects;
26
Jian Li3db90852018-06-10 22:29:16 +090027import static com.google.common.base.MoreObjects.toStringHelper;
Jian Li2cf1c0b2018-06-07 11:28:56 +090028import static com.google.common.base.Preconditions.checkNotNull;
Jian Li69600e02018-12-24 13:21:18 +090029import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType.REST;
Jian Li2cf1c0b2018-06-07 11:28:56 +090030
31/**
32 * A configuration file contains REST telemetry parameters.
33 */
34public final class DefaultRestTelemetryConfig implements RestTelemetryConfig {
35
Jian Li69600e02018-12-24 13:21:18 +090036 protected static final String ADDRESS = "address";
37 protected static final String PORT = "port";
38 protected static final String ENDPOINT = "endpoint";
39 protected static final String METHOD = "method";
40 protected static final String REQUEST_MEDIA_TYPE = "requestMediaType";
41 protected static final String RESPONSE_MEDIA_TYPE = "responseMediaType";
42 protected static final String CONFIG_MAP = "configMap";
43
Jian Li2cf1c0b2018-06-07 11:28:56 +090044 private final String address;
45 private final int port;
46 private final String endpoint;
47 private final String method;
48 private final String requestMediaType;
49 private final String responseMediaType;
50 private final Map<String, Object> configMap;
51
52 private DefaultRestTelemetryConfig(String address, int port, String endpoint,
53 String method, String requestMediaType,
54 String responseMediaType,
55 Map<String, Object> configMap) {
56 this.address = address;
57 this.port = port;
58 this.endpoint = endpoint;
59 this.method = method;
60 this.requestMediaType = requestMediaType;
61 this.responseMediaType = responseMediaType;
62 this.configMap = configMap;
63 }
64
65 @Override
66 public String address() {
67 return address;
68 }
69
70 @Override
71 public int port() {
72 return port;
73 }
74
75 @Override
76 public String endpoint() {
77 return endpoint;
78 }
79
80 @Override
81 public String method() {
82 return method;
83 }
84
85 @Override
86 public String requestMediaType() {
87 return requestMediaType;
88 }
89
90 @Override
91 public String responseMediaType() {
92 return responseMediaType;
93 }
94
95 @Override
96 public Map<String, Object> configMap() {
97 if (configMap != null) {
98 return ImmutableMap.copyOf(configMap);
99 } else {
100 return Maps.newConcurrentMap();
101 }
102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109
110 if (obj instanceof DefaultRestTelemetryConfig) {
111 final DefaultRestTelemetryConfig other = (DefaultRestTelemetryConfig) obj;
112 return Objects.equals(this.address, other.address) &&
113 Objects.equals(this.port, other.port) &&
114 Objects.equals(this.endpoint, other.endpoint) &&
115 Objects.equals(this.method, other.method) &&
116 Objects.equals(this.requestMediaType, other.requestMediaType) &&
117 Objects.equals(this.responseMediaType, other.responseMediaType) &&
118 Objects.equals(this.configMap, other.configMap);
119 }
120 return false;
121 }
122
123 @Override
124 public int hashCode() {
125 return Objects.hash(address, port, endpoint, method, requestMediaType,
126 responseMediaType, configMap);
127 }
128
Jian Li52c11222018-06-07 11:39:17 +0900129 @Override
Jian Li3db90852018-06-10 22:29:16 +0900130 public String toString() {
131 return toStringHelper(this)
Jian Li69600e02018-12-24 13:21:18 +0900132 .add(ADDRESS, address)
133 .add(PORT, port)
134 .add(ENDPOINT, endpoint)
135 .add(METHOD, method)
136 .add(REQUEST_MEDIA_TYPE, requestMediaType)
137 .add(RESPONSE_MEDIA_TYPE, responseMediaType)
138 .add(CONFIG_MAP, configMap)
Jian Li3db90852018-06-10 22:29:16 +0900139 .toString();
140 }
141
142 @Override
Jian Li69600e02018-12-24 13:21:18 +0900143 public TelemetryConfigProperties.Builder createBuilder() {
Jian Li52c11222018-06-07 11:39:17 +0900144 return new DefaultBuilder();
145 }
146
Jian Li2cf1c0b2018-06-07 11:28:56 +0900147 /**
Jian Li69600e02018-12-24 13:21:18 +0900148 * Builds a REST telemetry config from telemetry config instance.
149 *
150 * @param config telemetry config
151 * @return REST telemetry config
152 */
153 public static RestTelemetryConfig fromTelemetryConfig(TelemetryConfig config) {
154 if (config.type() != REST) {
155 return null;
156 }
157
158 return new DefaultBuilder()
159 .withAddress(config.getProperty(ADDRESS))
160 .withPort(Integer.valueOf(config.getProperty(PORT)))
161 .withEndpoint(config.getProperty(ENDPOINT))
162 .withMethod(config.getProperty(METHOD))
163 .withRequestMediaType(config.getProperty(REQUEST_MEDIA_TYPE))
164 .withResponseMediaType(config.getProperty(RESPONSE_MEDIA_TYPE))
165 .build();
166 }
167
168 /**
Jian Li2cf1c0b2018-06-07 11:28:56 +0900169 * Builder class of DefaultRestTelemetryConfig.
170 */
Jian Li52c11222018-06-07 11:39:17 +0900171 public static final class DefaultBuilder implements Builder {
Jian Li2cf1c0b2018-06-07 11:28:56 +0900172
173 private String address;
174 private int port;
175 private String endpoint;
176 private String method;
177 private String requestMediaType;
178 private String responseMediaType;
179 private Map<String, Object> configMap;
180
181 @Override
182 public Builder withAddress(String address) {
183 this.address = address;
184 return this;
185 }
186
187 @Override
188 public Builder withPort(int port) {
189 this.port = port;
190 return this;
191 }
192
193 @Override
194 public Builder withEndpoint(String endpoint) {
195 this.endpoint = endpoint;
196 return this;
197 }
198
199 @Override
200 public Builder withMethod(String method) {
201 this.method = method;
202 return this;
203 }
204
205 @Override
206 public Builder withRequestMediaType(String mediaType) {
207 this.requestMediaType = mediaType;
208 return this;
209 }
210
211 @Override
212 public Builder withResponseMediaType(String mediaType) {
213 this.responseMediaType = mediaType;
214 return this;
215 }
216
217 @Override
218 public Builder withConfigMap(Map<String, Object> configMap) {
219 this.configMap = configMap;
220 return this;
221 }
222
223 @Override
224 public RestTelemetryConfig build() {
225 checkNotNull(address, "REST server address cannot be null");
226 checkNotNull(endpoint, "REST server endpoint cannot be null");
227
228 return new DefaultRestTelemetryConfig(address, port, endpoint,
229 method, requestMediaType, responseMediaType, configMap);
230 }
231 }
Jian Li69600e02018-12-24 13:21:18 +0900232}