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