]> git.basschouten.com Git - openhab-addons.git/blob
d935ca090f09355acff42d141c9eb846d220c42a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.influxdb.internal;
14
15 import java.util.Collections;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.StringJoiner;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Contains this addon configurable parameters
27  *
28  * @author Joan Pujol Espinar - Initial contribution
29  */
30 @NonNullByDefault
31 public class InfluxDBConfiguration {
32     public static final String URL_PARAM = "url";
33     public static final String TOKEN_PARAM = "token";
34     public static final String USER_PARAM = "user";
35     public static final String PASSWORD_PARAM = "password";
36     public static final String DATABASE_PARAM = "db";
37     public static final String RETENTION_POLICY_PARAM = "retentionPolicy";
38     public static final String VERSION_PARAM = "version";
39     public static final String REPLACE_UNDERSCORE_PARAM = "replaceUnderscore";
40     public static final String ADD_CATEGORY_TAG_PARAM = "addCategoryTag";
41     public static final String ADD_LABEL_TAG_PARAM = "addLabelTag";
42     public static final String ADD_TYPE_TAG_PARAM = "addTypeTag";
43     public static InfluxDBConfiguration NO_CONFIGURATION = new InfluxDBConfiguration(Collections.emptyMap());
44     private final Logger logger = LoggerFactory.getLogger(InfluxDBConfiguration.class);
45     private final String url;
46     private final String user;
47     private final String password;
48     private final String token;
49     private final String databaseName;
50     private final String retentionPolicy;
51     private final InfluxDBVersion version;
52
53     private final boolean replaceUnderscore;
54     private final boolean addCategoryTag;
55     private final boolean addTypeTag;
56     private final boolean addLabelTag;
57
58     public InfluxDBConfiguration(Map<String, Object> config) {
59         url = (String) config.getOrDefault(URL_PARAM, "http://127.0.0.1:8086");
60         user = (String) config.getOrDefault(USER_PARAM, "openhab");
61         password = (String) config.getOrDefault(PASSWORD_PARAM, "");
62         token = (String) config.getOrDefault(TOKEN_PARAM, "");
63         databaseName = (String) config.getOrDefault(DATABASE_PARAM, "openhab");
64         retentionPolicy = (String) config.getOrDefault(RETENTION_POLICY_PARAM, "autogen");
65         version = parseInfluxVersion((String) config.getOrDefault(VERSION_PARAM, InfluxDBVersion.V1.name()));
66
67         replaceUnderscore = getConfigBooleanValue(config, REPLACE_UNDERSCORE_PARAM, false);
68         addCategoryTag = getConfigBooleanValue(config, ADD_CATEGORY_TAG_PARAM, false);
69         addLabelTag = getConfigBooleanValue(config, ADD_LABEL_TAG_PARAM, false);
70         addTypeTag = getConfigBooleanValue(config, ADD_TYPE_TAG_PARAM, false);
71     }
72
73     private static boolean getConfigBooleanValue(Map<String, Object> config, String key, boolean defaultValue) {
74         Object object = config.get(key);
75         if (object instanceof Boolean) {
76             return (Boolean) object;
77         } else if (object instanceof String) {
78             return "true".equalsIgnoreCase((String) object);
79         } else {
80             return defaultValue;
81         }
82     }
83
84     private InfluxDBVersion parseInfluxVersion(@Nullable String value) {
85         try {
86             return Optional.ofNullable(value).map(InfluxDBVersion::valueOf).orElse(InfluxDBVersion.UNKNOWN);
87         } catch (RuntimeException e) {
88             logger.warn("Invalid version {}", value);
89             return InfluxDBVersion.UNKNOWN;
90         }
91     }
92
93     public boolean isValid() {
94         boolean hasVersion = version != InfluxDBVersion.UNKNOWN;
95         boolean hasCredentials = false;
96         if (version == InfluxDBVersion.V1) {
97             hasCredentials = !user.isBlank() && !password.isBlank();
98         } else if (version == InfluxDBVersion.V2) {
99             hasCredentials = !token.isBlank() || (!user.isBlank() && !password.isBlank());
100         }
101         boolean hasDatabase = !databaseName.isBlank();
102         boolean hasRetentionPolicy = !retentionPolicy.isBlank();
103
104         boolean valid = hasVersion && hasCredentials && hasDatabase && hasRetentionPolicy;
105         if (valid) {
106             return true;
107         } else {
108             String msg = "InfluxDB configuration isn't valid. Addon won't work: ";
109             StringJoiner reason = new StringJoiner(",");
110             if (!hasVersion) {
111                 reason.add("Unknown version");
112             } else {
113                 if (!hasCredentials) {
114                     reason.add("No credentials");
115                 }
116                 if (!hasDatabase) {
117                     reason.add("No database name / organization defined");
118                 }
119                 if (!hasRetentionPolicy) {
120                     reason.add("No retention policy / bucket defined");
121                 }
122             }
123             logger.warn("{} {}", msg, reason);
124             return false;
125         }
126     }
127
128     public String getUrl() {
129         return url;
130     }
131
132     public String getToken() {
133         return token;
134     }
135
136     public String getDatabaseName() {
137         return databaseName;
138     }
139
140     public String getRetentionPolicy() {
141         return retentionPolicy;
142     }
143
144     public boolean isReplaceUnderscore() {
145         return replaceUnderscore;
146     }
147
148     public boolean isAddCategoryTag() {
149         return addCategoryTag;
150     }
151
152     public boolean isAddTypeTag() {
153         return addTypeTag;
154     }
155
156     public boolean isAddLabelTag() {
157         return addLabelTag;
158     }
159
160     public String getUser() {
161         return user;
162     }
163
164     public String getPassword() {
165         return password;
166     }
167
168     public InfluxDBVersion getVersion() {
169         return version;
170     }
171
172     @Override
173     public String toString() {
174         String sb = "InfluxDBConfiguration{" + "url='" + url + '\'' + ", user='" + user + '\'' + ", password='"
175                 + password.length() + " chars" + '\'' + ", token='" + token.length() + " chars" + '\''
176                 + ", databaseName='" + databaseName + '\'' + ", retentionPolicy='" + retentionPolicy + '\''
177                 + ", version=" + version + ", replaceUnderscore=" + replaceUnderscore + ", addCategoryTag="
178                 + addCategoryTag + ", addTypeTag=" + addTypeTag + ", addLabelTag=" + addLabelTag + '}';
179         return sb;
180     }
181
182     public int getTokenLength() {
183         return token.length();
184     }
185
186     public char[] getTokenAsCharArray() {
187         return token.toCharArray();
188     }
189 }