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