2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.dynamodb.internal;
15 import java.util.Arrays;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
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;
30 * Configuration for DynamoDB connections
32 * @author Sami Salonen - Initial contribution
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;
43 private static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBConfig.class);
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;
56 * @param config persistence service configuration
57 * @return DynamoDB configuration. Returns null in case of configuration errors
59 public static @Nullable DynamoDBConfig fromConfig(Map<String, Object> config) {
61 String regionName = (String) config.get("region");
62 if (regionName == null) {
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(",")));
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);
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");
90 credentials = new ProfilesConfigFile(profilesConfigFile).getCredentials(profile);
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;
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;
105 createTable = Boolean.parseBoolean(createTableParam);
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;
114 readCapacityUnits = Long.parseLong(readCapacityUnitsParam);
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;
123 writeCapacityUnits = Long.parseLong(writeCapacityUnitsParam);
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;
132 bufferCommitIntervalMillis = Long.parseLong(bufferCommitIntervalMillisParam);
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;
141 bufferSize = Integer.parseInt(bufferSizeParam);
144 return new DynamoDBConfig(region, credentials, table, createTable, readCapacityUnits, writeCapacityUnits,
145 bufferCommitIntervalMillis, bufferSize);
146 } catch (Exception e) {
147 LOGGER.error("Error with configuration", e);
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;
164 public AWSCredentials getCredentials() {
168 public String getTablePrefix() {
172 public Regions getRegion() {
176 public boolean isCreateTable() {
180 public long getReadCapacityUnits() {
181 return readCapacityUnits;
184 public long getWriteCapacityUnits() {
185 return writeCapacityUnits;
188 public long getBufferCommitIntervalMillis() {
189 return bufferCommitIntervalMillis;
192 public int getBufferSize() {