blob: 5ece99bcf9999e1dc80e9fd508fd2a7ea053dd4e [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 Li2cf1c0b2018-06-07 11:28:56 +090022
23import java.util.Map;
24import java.util.Objects;
25
Jian Li3db90852018-06-10 22:29:16 +090026import static com.google.common.base.MoreObjects.toStringHelper;
Jian Li2cf1c0b2018-06-07 11:28:56 +090027import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * A configuration file contains gRPC telemetry parameters.
31 */
32public final class DefaultGrpcTelemetryConfig implements GrpcTelemetryConfig {
33
34 private final String address;
35 private final int port;
36 private final boolean usePlaintext;
37 private final int maxInboundMsgSize;
38 private final Map<String, Object> configMap;
39
40 private DefaultGrpcTelemetryConfig(String address, int port,
41 boolean usePlaintext, int maxInboundMsgSize,
42 Map<String, Object> configMap) {
43 this.address = address;
44 this.port = port;
45 this.usePlaintext = usePlaintext;
46 this.maxInboundMsgSize = maxInboundMsgSize;
47 this.configMap = configMap;
48 }
49
50 @Override
51 public String address() {
52 return address;
53 }
54
55 @Override
56 public int port() {
57 return port;
58 }
59
60 @Override
61 public boolean usePlaintext() {
62 return usePlaintext;
63 }
64
65 @Override
66 public int maxInboundMsgSize() {
67 return maxInboundMsgSize;
68 }
69
70 @Override
71 public Map<String, Object> configMap() {
72 if (configMap != null) {
73 return ImmutableMap.copyOf(configMap);
74 } else {
75 return Maps.newConcurrentMap();
76 }
77 }
78
79 @Override
80 public boolean equals(Object obj) {
81 if (this == obj) {
82 return true;
83 }
84
85 if (obj instanceof DefaultGrpcTelemetryConfig) {
86 final DefaultGrpcTelemetryConfig other = (DefaultGrpcTelemetryConfig) obj;
87 return Objects.equals(this.address, other.address) &&
88 Objects.equals(this.port, other.port) &&
89 Objects.equals(this.usePlaintext, other.usePlaintext) &&
90 Objects.equals(this.maxInboundMsgSize, other.maxInboundMsgSize) &&
91 Objects.equals(this.configMap, other.configMap);
92 }
93 return false;
94 }
95
96 @Override
97 public int hashCode() {
98 return Objects.hash(address, port, usePlaintext, maxInboundMsgSize, configMap);
99 }
100
Jian Li52c11222018-06-07 11:39:17 +0900101 @Override
Jian Li3db90852018-06-10 22:29:16 +0900102 public String toString() {
103 return toStringHelper(this)
104 .add("address", address)
105 .add("port", port)
106 .add("usePlaintext", usePlaintext)
107 .add("maxInboundMsgSize", maxInboundMsgSize)
108 .add("configMap", configMap)
109 .toString();
110 }
111
112 @Override
Jian Li52c11222018-06-07 11:39:17 +0900113 public TelemetryConfig.Builder createBuilder() {
114 return new DefaultBuilder();
115 }
116
Jian Li2cf1c0b2018-06-07 11:28:56 +0900117 /**
118 * Builder class of DefaultKafkaTelemetryConfig.
119 */
Jian Li52c11222018-06-07 11:39:17 +0900120 public static final class DefaultBuilder implements Builder {
Jian Li2cf1c0b2018-06-07 11:28:56 +0900121
122 private String address;
123 private int port;
124 private boolean usePlaintext;
125 private int maxInboundMsgSize;
126 private Map<String, Object> configMap;
127
128 @Override
129 public Builder withAddress(String address) {
130 this.address = address;
131 return this;
132 }
133
134 @Override
135 public Builder withPort(int port) {
136 this.port = port;
137 return this;
138 }
139
140 @Override
141 public Builder withUsePlaintext(boolean usePlaintext) {
142 this.usePlaintext = usePlaintext;
143 return this;
144 }
145
146 @Override
147 public Builder withMaxInboundMsgSize(int maxInboundMsgSize) {
148 this.maxInboundMsgSize = maxInboundMsgSize;
149 return this;
150 }
151
152 @Override
153 public Builder withConfigMap(Map<String, Object> configMap) {
154 this.configMap = configMap;
155 return this;
156 }
157
158 @Override
159 public GrpcTelemetryConfig build() {
160 checkNotNull(address, "gRPC server address cannot be null");
161
162 return new DefaultGrpcTelemetryConfig(address, port, usePlaintext,
163 maxInboundMsgSize, configMap);
164 }
165 }
166}