]> git.basschouten.com Git - openhab-addons.git/blob
d88451bc3aa5a15142f8d5ecd5cfab54184c7401
[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.dynamodb.internal;
14
15 import java.util.Arrays;
16 import java.util.Map;
17 import java.util.stream.Collectors;
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 import com.amazonaws.auth.AWSCredentials;
25 import com.amazonaws.auth.BasicAWSCredentials;
26 import com.amazonaws.auth.profile.ProfilesConfigFile;
27 import com.amazonaws.regions.Regions;
28
29 /**
30  * Configuration for DynamoDB connections
31  *
32  * @author Sami Salonen - Initial contribution
33  */
34 @NonNullByDefault
35 public class DynamoDBConfig {
36     public static final String DEFAULT_TABLE_PREFIX = "openhab-";
37     public static final boolean DEFAULT_CREATE_TABLE_ON_DEMAND = true;
38     public static final long DEFAULT_READ_CAPACITY_UNITS = 1;
39     public static final long DEFAULT_WRITE_CAPACITY_UNITS = 1;
40     public static final long DEFAULT_BUFFER_COMMIT_INTERVAL_MILLIS = 1000;
41     public static final int DEFAULT_BUFFER_SIZE = 1000;
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBConfig.class);
44
45     private String tablePrefix = DEFAULT_TABLE_PREFIX;
46     private Regions region;
47     private AWSCredentials credentials;
48     private boolean createTable = DEFAULT_CREATE_TABLE_ON_DEMAND;
49     private long readCapacityUnits = DEFAULT_READ_CAPACITY_UNITS;
50     private long writeCapacityUnits = DEFAULT_WRITE_CAPACITY_UNITS;
51     private long bufferCommitIntervalMillis = DEFAULT_BUFFER_COMMIT_INTERVAL_MILLIS;
52     private int bufferSize = DEFAULT_BUFFER_SIZE;
53
54     /**
55      *
56      * @param config persistence service configuration
57      * @return DynamoDB configuration. Returns null in case of configuration errors
58      */
59     public static @Nullable DynamoDBConfig fromConfig(Map<String, Object> config) {
60         try {
61             String regionName = (String) config.get("region");
62             if (regionName == null) {
63                 return null;
64             }
65             final Regions region;
66             try {
67                 region = Regions.fromName(regionName);
68             } catch (IllegalArgumentException e) {
69                 LOGGER.error("Specify valid AWS region to use, got {}. Valid values include: {}", regionName, Arrays
70                         .asList(Regions.values()).stream().map(r -> r.getName()).collect(Collectors.joining(",")));
71                 return null;
72             }
73
74             AWSCredentials credentials;
75             String accessKey = (String) config.get("accessKey");
76             String secretKey = (String) config.get("secretKey");
77             if (accessKey != null && !accessKey.isBlank() && secretKey != null && !secretKey.isBlank()) {
78                 LOGGER.debug("accessKey and secretKey specified. Using those.");
79                 credentials = new BasicAWSCredentials(accessKey, secretKey);
80             } else {
81                 LOGGER.debug("accessKey and/or secretKey blank. Checking profilesConfigFile and profile.");
82                 String profilesConfigFile = (String) config.get("profilesConfigFile");
83                 String profile = (String) config.get("profile");
84                 if (profilesConfigFile == null || profilesConfigFile.isBlank() || profile == null
85                         || profile.isBlank()) {
86                     LOGGER.error("Specify either 1) accessKey and secretKey; or 2) profilesConfigFile and "
87                             + "profile for providing AWS credentials");
88                     return null;
89                 }
90                 credentials = new ProfilesConfigFile(profilesConfigFile).getCredentials(profile);
91             }
92
93             String table = (String) config.get("tablePrefix");
94             if (table == null || table.isBlank()) {
95                 LOGGER.debug("Using default table name {}", DEFAULT_TABLE_PREFIX);
96                 table = DEFAULT_TABLE_PREFIX;
97             }
98
99             final boolean createTable;
100             String createTableParam = (String) config.get("createTable");
101             if (createTableParam == null || createTableParam.isBlank()) {
102                 LOGGER.debug("Creating table on demand: {}", DEFAULT_CREATE_TABLE_ON_DEMAND);
103                 createTable = DEFAULT_CREATE_TABLE_ON_DEMAND;
104             } else {
105                 createTable = Boolean.parseBoolean(createTableParam);
106             }
107
108             final long readCapacityUnits;
109             String readCapacityUnitsParam = (String) config.get("readCapacityUnits");
110             if (readCapacityUnitsParam == null || readCapacityUnitsParam.isBlank()) {
111                 LOGGER.debug("Read capacity units: {}", DEFAULT_READ_CAPACITY_UNITS);
112                 readCapacityUnits = DEFAULT_READ_CAPACITY_UNITS;
113             } else {
114                 readCapacityUnits = Long.parseLong(readCapacityUnitsParam);
115             }
116
117             final long writeCapacityUnits;
118             String writeCapacityUnitsParam = (String) config.get("writeCapacityUnits");
119             if (writeCapacityUnitsParam == null || writeCapacityUnitsParam.isBlank()) {
120                 LOGGER.debug("Write capacity units: {}", DEFAULT_WRITE_CAPACITY_UNITS);
121                 writeCapacityUnits = DEFAULT_WRITE_CAPACITY_UNITS;
122             } else {
123                 writeCapacityUnits = Long.parseLong(writeCapacityUnitsParam);
124             }
125
126             final long bufferCommitIntervalMillis;
127             String bufferCommitIntervalMillisParam = (String) config.get("bufferCommitIntervalMillis");
128             if (bufferCommitIntervalMillisParam == null || bufferCommitIntervalMillisParam.isBlank()) {
129                 LOGGER.debug("Buffer commit interval millis: {}", DEFAULT_BUFFER_COMMIT_INTERVAL_MILLIS);
130                 bufferCommitIntervalMillis = DEFAULT_BUFFER_COMMIT_INTERVAL_MILLIS;
131             } else {
132                 bufferCommitIntervalMillis = Long.parseLong(bufferCommitIntervalMillisParam);
133             }
134
135             final int bufferSize;
136             String bufferSizeParam = (String) config.get("bufferSize");
137             if (bufferSizeParam == null || bufferSizeParam.isBlank()) {
138                 LOGGER.debug("Buffer size: {}", DEFAULT_BUFFER_SIZE);
139                 bufferSize = DEFAULT_BUFFER_SIZE;
140             } else {
141                 bufferSize = Integer.parseInt(bufferSizeParam);
142             }
143
144             return new DynamoDBConfig(region, credentials, table, createTable, readCapacityUnits, writeCapacityUnits,
145                     bufferCommitIntervalMillis, bufferSize);
146         } catch (Exception e) {
147             LOGGER.error("Error with configuration", e);
148             return null;
149         }
150     }
151
152     public DynamoDBConfig(Regions region, AWSCredentials credentials, String table, boolean createTable,
153             long readCapacityUnits, long writeCapacityUnits, long bufferCommitIntervalMillis, int bufferSize) {
154         this.region = region;
155         this.credentials = credentials;
156         this.tablePrefix = table;
157         this.createTable = createTable;
158         this.readCapacityUnits = readCapacityUnits;
159         this.writeCapacityUnits = writeCapacityUnits;
160         this.bufferCommitIntervalMillis = bufferCommitIntervalMillis;
161         this.bufferSize = bufferSize;
162     }
163
164     public AWSCredentials getCredentials() {
165         return credentials;
166     }
167
168     public String getTablePrefix() {
169         return tablePrefix;
170     }
171
172     public Regions getRegion() {
173         return region;
174     }
175
176     public boolean isCreateTable() {
177         return createTable;
178     }
179
180     public long getReadCapacityUnits() {
181         return readCapacityUnits;
182     }
183
184     public long getWriteCapacityUnits() {
185         return writeCapacityUnits;
186     }
187
188     public long getBufferCommitIntervalMillis() {
189         return bufferCommitIntervalMillis;
190     }
191
192     public int getBufferSize() {
193         return bufferSize;
194     }
195 }