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