]> git.basschouten.com Git - openhab-addons.git/blob
d4ede0be63a09bfa7460b2f0f55cb7053d1ed6e5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.NonNull;
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, @Nullable Object> config) {
59         url = (@NonNull String) config.getOrDefault(URL_PARAM, "http://127.0.0.1:8086");
60         user = (@NonNull String) config.getOrDefault(USER_PARAM, "openhab");
61         password = (@NonNull String) config.getOrDefault(PASSWORD_PARAM, "");
62         token = (@NonNull String) config.getOrDefault(TOKEN_PARAM, "");
63         databaseName = (@NonNull String) config.getOrDefault(DATABASE_PARAM, "openhab");
64         retentionPolicy = (@NonNull String) config.getOrDefault(RETENTION_POLICY_PARAM, "autogen");
65         version = parseInfluxVersion(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, @Nullable Object> config, String key,
74             boolean defaultValue) {
75         Object object = config.get(key);
76         if (object instanceof Boolean) {
77             return (Boolean) object;
78         } else if (object instanceof String) {
79             return "true".equalsIgnoreCase((String) object);
80         } else {
81             return defaultValue;
82         }
83     }
84
85     private InfluxDBVersion parseInfluxVersion(@Nullable Object value) {
86         try {
87             return InfluxDBVersion.valueOf((String) value);
88         } catch (RuntimeException e) {
89             logger.warn("Invalid version {}", value);
90             return InfluxDBVersion.UNKNOWN;
91         }
92     }
93
94     public boolean isValid() {
95         boolean hasVersion = version != InfluxDBVersion.UNKNOWN;
96         boolean hasCredentials = false;
97         if (version == InfluxDBVersion.V1) {
98             hasCredentials = !user.isBlank() && !password.isBlank();
99         } else if (version == InfluxDBVersion.V2) {
100             hasCredentials = !token.isBlank() || (!user.isBlank() && !password.isBlank());
101         }
102         boolean hasDatabase = !databaseName.isBlank();
103         boolean hasRetentionPolicy = !retentionPolicy.isBlank();
104
105         boolean valid = hasVersion && hasCredentials && hasDatabase && hasRetentionPolicy;
106         if (valid) {
107             return true;
108         } else {
109             String msg = "InfluxDB configuration isn't valid. Addon won't work: ";
110             StringJoiner reason = new StringJoiner(",");
111             if (!hasVersion) {
112                 reason.add("Unknown version");
113             } else {
114                 if (!hasCredentials) {
115                     reason.add("No credentials");
116                 }
117                 if (!hasDatabase) {
118                     reason.add("No database name / organization defined");
119                 }
120                 if (!hasRetentionPolicy) {
121                     reason.add("No retention policy / bucket defined");
122                 }
123             }
124             logger.warn("{} {}", msg, reason);
125             return false;
126         }
127     }
128
129     public String getUrl() {
130         return url;
131     }
132
133     public String getToken() {
134         return token;
135     }
136
137     public String getDatabaseName() {
138         return databaseName;
139     }
140
141     public String getRetentionPolicy() {
142         return retentionPolicy;
143     }
144
145     public boolean isReplaceUnderscore() {
146         return replaceUnderscore;
147     }
148
149     public boolean isAddCategoryTag() {
150         return addCategoryTag;
151     }
152
153     public boolean isAddTypeTag() {
154         return addTypeTag;
155     }
156
157     public boolean isAddLabelTag() {
158         return addLabelTag;
159     }
160
161     public String getUser() {
162         return user;
163     }
164
165     public String getPassword() {
166         return password;
167     }
168
169     public InfluxDBVersion getVersion() {
170         return version;
171     }
172
173     @Override
174     public String toString() {
175         String sb = "InfluxDBConfiguration{" + "url='" + url + '\'' + ", user='" + user + '\'' + ", password='"
176                 + password.length() + " chars" + '\'' + ", token='" + token.length() + " chars" + '\''
177                 + ", databaseName='" + databaseName + '\'' + ", retentionPolicy='" + retentionPolicy + '\''
178                 + ", version=" + version + ", replaceUnderscore=" + replaceUnderscore + ", addCategoryTag="
179                 + addCategoryTag + ", addTypeTag=" + addTypeTag + ", addLabelTag=" + addLabelTag + '}';
180         return sb;
181     }
182
183     public int getTokenLength() {
184         return token.length();
185     }
186
187     public char[] getTokenAsCharArray() {
188         return token.toCharArray();
189     }
190 }