blob: 8d2f0fd95c1ccaf5ab24bc1cfd1333ed0b270ea5 [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.GrpcTelemetryConfig;
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.GRPC;
Jian Li2cf1c0b2018-06-07 11:28:56 +090030
31/**
32 * A configuration file contains gRPC telemetry parameters.
33 */
34public final class DefaultGrpcTelemetryConfig implements GrpcTelemetryConfig {
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 USE_PLAINTEXT = "usePlaintext";
39 protected static final String MAX_INBOUND_MSG_SIZE = "maxInboundMsgSize";
40 protected static final String CONFIG_MAP = "configMap";
41
Jian Li2cf1c0b2018-06-07 11:28:56 +090042 private final String address;
43 private final int port;
44 private final boolean usePlaintext;
45 private final int maxInboundMsgSize;
46 private final Map<String, Object> configMap;
47
48 private DefaultGrpcTelemetryConfig(String address, int port,
49 boolean usePlaintext, int maxInboundMsgSize,
50 Map<String, Object> configMap) {
51 this.address = address;
52 this.port = port;
53 this.usePlaintext = usePlaintext;
54 this.maxInboundMsgSize = maxInboundMsgSize;
55 this.configMap = configMap;
56 }
57
58 @Override
59 public String address() {
60 return address;
61 }
62
63 @Override
64 public int port() {
65 return port;
66 }
67
68 @Override
69 public boolean usePlaintext() {
70 return usePlaintext;
71 }
72
73 @Override
74 public int maxInboundMsgSize() {
75 return maxInboundMsgSize;
76 }
77
78 @Override
79 public Map<String, Object> configMap() {
80 if (configMap != null) {
81 return ImmutableMap.copyOf(configMap);
82 } else {
83 return Maps.newConcurrentMap();
84 }
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92
93 if (obj instanceof DefaultGrpcTelemetryConfig) {
94 final DefaultGrpcTelemetryConfig other = (DefaultGrpcTelemetryConfig) obj;
95 return Objects.equals(this.address, other.address) &&
96 Objects.equals(this.port, other.port) &&
97 Objects.equals(this.usePlaintext, other.usePlaintext) &&
98 Objects.equals(this.maxInboundMsgSize, other.maxInboundMsgSize) &&
99 Objects.equals(this.configMap, other.configMap);
100 }
101 return false;
102 }
103
104 @Override
105 public int hashCode() {
106 return Objects.hash(address, port, usePlaintext, maxInboundMsgSize, configMap);
107 }
108
Jian Li52c11222018-06-07 11:39:17 +0900109 @Override
Jian Li3db90852018-06-10 22:29:16 +0900110 public String toString() {
111 return toStringHelper(this)
Jian Li69600e02018-12-24 13:21:18 +0900112 .add(ADDRESS, address)
113 .add(PORT, port)
114 .add(USE_PLAINTEXT, usePlaintext)
115 .add(MAX_INBOUND_MSG_SIZE, maxInboundMsgSize)
116 .add(CONFIG_MAP, configMap)
Jian Li3db90852018-06-10 22:29:16 +0900117 .toString();
118 }
119
120 @Override
Jian Li69600e02018-12-24 13:21:18 +0900121 public TelemetryConfigProperties.Builder createBuilder() {
Jian Li52c11222018-06-07 11:39:17 +0900122 return new DefaultBuilder();
123 }
124
Jian Li2cf1c0b2018-06-07 11:28:56 +0900125 /**
Jian Li69600e02018-12-24 13:21:18 +0900126 * Builds a gRPC telemetry config from telemetry config instance.
127 *
128 * @param config telemetry config
129 * @return gRPC telemetry config
130 */
131 public static GrpcTelemetryConfig fromTelemetryConfig(TelemetryConfig config) {
132 if (config.type() != GRPC) {
133 return null;
134 }
135
136 return new DefaultBuilder()
137 .withAddress(config.getProperty(ADDRESS))
138 .withPort(Integer.valueOf(config.getProperty(PORT)))
139 .withUsePlaintext(Boolean.valueOf(config.getProperty(USE_PLAINTEXT)))
140 .withMaxInboundMsgSize(Integer.valueOf(config.getProperty(MAX_INBOUND_MSG_SIZE)))
141 .build();
142 }
143
144 /**
Jian Li2cf1c0b2018-06-07 11:28:56 +0900145 * Builder class of DefaultKafkaTelemetryConfig.
146 */
Jian Li52c11222018-06-07 11:39:17 +0900147 public static final class DefaultBuilder implements Builder {
Jian Li2cf1c0b2018-06-07 11:28:56 +0900148
149 private String address;
150 private int port;
151 private boolean usePlaintext;
152 private int maxInboundMsgSize;
153 private Map<String, Object> configMap;
154
155 @Override
156 public Builder withAddress(String address) {
157 this.address = address;
158 return this;
159 }
160
161 @Override
162 public Builder withPort(int port) {
163 this.port = port;
164 return this;
165 }
166
167 @Override
168 public Builder withUsePlaintext(boolean usePlaintext) {
169 this.usePlaintext = usePlaintext;
170 return this;
171 }
172
173 @Override
174 public Builder withMaxInboundMsgSize(int maxInboundMsgSize) {
175 this.maxInboundMsgSize = maxInboundMsgSize;
176 return this;
177 }
178
179 @Override
180 public Builder withConfigMap(Map<String, Object> configMap) {
181 this.configMap = configMap;
182 return this;
183 }
184
185 @Override
186 public GrpcTelemetryConfig build() {
187 checkNotNull(address, "gRPC server address cannot be null");
188
189 return new DefaultGrpcTelemetryConfig(address, port, usePlaintext,
190 maxInboundMsgSize, configMap);
191 }
192 }
Jian Li69600e02018-12-24 13:21:18 +0900193}