From d378c37fbd1ecec7b43394926f1ca32a695e07de Mon Sep 17 00:00:00 2001 From: vasraz Date: Mon, 22 Mar 2021 15:33:06 +0000 Subject: Reformat openecomp-be Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3449 Change-Id: I13e02322f8e00820cc5a1d85752caaeda9bf10d1 --- .../main/java/org/openecomp/core/dao/BaseDao.java | 1 - .../org/openecomp/core/dao/UniqueValueDao.java | 1 - .../openecomp/core/dao/UniqueValueDaoFactory.java | 1 - .../core/dao/types/UniqueValueEntity.java | 3 - .../org/openecomp/core/nosqldb/api/NoSqlDb.java | 2 - .../core/nosqldb/factory/NoSqlDbFactory.java | 3 - .../org/openecomp/core/util/UniqueValueUtil.java | 56 +-- .../openecomp/core/dao/impl/CassandraBaseDao.java | 42 +-- .../core/dao/impl/UniqueValueCassandraDaoImpl.java | 54 ++- .../core/dao/impl/UniqueValueDaoFactoryImpl.java | 16 +- .../cassandra/CassandraNoSqlDbFactoryImpl.java | 27 +- .../impl/cassandra/CassandraNoSqlDbImpl.java | 42 +-- .../impl/cassandra/CassandraSessionFactory.java | 77 ++-- .../core/nosqldb/util/CassandraUtils.java | 120 +++--- .../core/nosqldb/util/ConfigurationManager.java | 420 ++++++++++----------- 15 files changed, 376 insertions(+), 489 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/BaseDao.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/BaseDao.java index fa23b51742..e46cade267 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/BaseDao.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/BaseDao.java @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.dao; import java.util.Collection; diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDao.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDao.java index 36dfefa840..02db64ec66 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDao.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDao.java @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.dao; import org.openecomp.core.dao.types.UniqueValueEntity; diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDaoFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDaoFactory.java index 022ad8e09d..c653c8dc4f 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDaoFactory.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/UniqueValueDaoFactory.java @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.dao; import org.openecomp.core.factory.api.AbstractComponentFactory; diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/types/UniqueValueEntity.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/types/UniqueValueEntity.java index 24038b9b0c..d107c9f388 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/types/UniqueValueEntity.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/dao/types/UniqueValueEntity.java @@ -17,12 +17,10 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.dao.types; import com.datastax.driver.mapping.annotations.PartitionKey; import com.datastax.driver.mapping.annotations.Table; - import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -37,7 +35,6 @@ public class UniqueValueEntity { @PartitionKey private String type; - @PartitionKey(value = 1) private String value; } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/api/NoSqlDb.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/api/NoSqlDb.java index ff4661c44d..844ce88265 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/api/NoSqlDb.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/api/NoSqlDb.java @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.nosqldb.api; import com.datastax.driver.core.ResultSet; @@ -26,7 +25,6 @@ import com.datastax.driver.mapping.MappingManager; public interface NoSqlDb { //TODO: remove cassandra types from here!! (like done in SDC...). - void insert(String tableName, String[] colNames, Object[] values); ResultSet execute(String statement); diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/factory/NoSqlDbFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/factory/NoSqlDbFactory.java index 31b6f6a505..e86b64e1aa 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/factory/NoSqlDbFactory.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/nosqldb/factory/NoSqlDbFactory.java @@ -17,18 +17,15 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.nosqldb.factory; import org.openecomp.core.factory.api.AbstractComponentFactory; import org.openecomp.core.factory.api.AbstractFactory; import org.openecomp.core.nosqldb.api.NoSqlDb; - public abstract class NoSqlDbFactory extends AbstractComponentFactory { public static NoSqlDbFactory getInstance() { - return AbstractFactory.getInstance(NoSqlDbFactory.class); } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java index aded1fa99f..a70bc47864 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java @@ -13,11 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.core.util; import java.util.Optional; - import org.apache.commons.lang3.ArrayUtils; import org.openecomp.core.dao.UniqueValueDao; import org.openecomp.core.dao.types.UniqueValueEntity; @@ -31,13 +29,24 @@ public class UniqueValueUtil { private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION"; private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists."; private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_'; - private final UniqueValueDao uniqueValueDao; public UniqueValueUtil(UniqueValueDao uniqueValueDao) { this.uniqueValueDao = uniqueValueDao; } + private static Optional formatValue(String[] uniqueCombination) { + if (uniqueCombination == null || uniqueCombination.length == 0 || getValueWithoutContext(uniqueCombination) == null) { + return Optional.empty(); + } + uniqueCombination[uniqueCombination.length - 1] = getValueWithoutContext(uniqueCombination).toLowerCase(); + return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR)); + } + + private static String getValueWithoutContext(String... uniqueCombination) { + return uniqueCombination[uniqueCombination.length - 1]; + } + /** * Create unique value. * @@ -49,7 +58,6 @@ public class UniqueValueUtil { if (ArrayUtils.isNotEmpty(uniqueCombination)) { originalEntityName = uniqueCombination[uniqueCombination.length - 1]; } - Optional formattedValue = formatValue(uniqueCombination); if (formattedValue.isPresent()) { validateUniqueValue(type, formattedValue.get(), originalEntityName); @@ -64,9 +72,7 @@ public class UniqueValueUtil { * @param uniqueCombination the unique combination */ public void deleteUniqueValue(String type, String... uniqueCombination) { - formatValue(uniqueCombination).ifPresent( - formattedValue -> uniqueValueDao.delete(new UniqueValueEntity(type, formattedValue))); - + formatValue(uniqueCombination).ifPresent(formattedValue -> uniqueValueDao.delete(new UniqueValueEntity(type, formattedValue))); } /** @@ -77,11 +83,10 @@ public class UniqueValueUtil { * @param newValue the new value * @param uniqueContext the unique context */ - public void updateUniqueValue(String type, String oldValue, String newValue, - String... uniqueContext) { + public void updateUniqueValue(String type, String oldValue, String newValue, String... uniqueContext) { if (newValue == null || !newValue.equalsIgnoreCase(oldValue)) { - createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {newValue})); - deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {oldValue})); + createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue})); + deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue})); } } @@ -96,7 +101,6 @@ public class UniqueValueUtil { if (ArrayUtils.isNotEmpty(uniqueCombination)) { originalEntityName = uniqueCombination[uniqueCombination.length - 1]; } - Optional formattedValue = formatValue(uniqueCombination); if (formattedValue.isPresent()) { validateUniqueValue(type, formattedValue.get(), originalEntityName); @@ -105,12 +109,8 @@ public class UniqueValueUtil { private void validateUniqueValue(String type, String formattedValue, String originalEntityName) { if (isUniqueValueOccupied(type, formattedValue)) { - throw new CoreException(new ErrorCode.ErrorCodeBuilder() - .withCategory(ErrorCategory.APPLICATION) - .withId(UNIQUE_VALUE_VIOLATION) - .withMessage(String - .format(UNIQUE_VALUE_VIOLATION_MSG, type, originalEntityName)) - .build()); + throw new CoreException(new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(UNIQUE_VALUE_VIOLATION) + .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type, originalEntityName)).build()); } } @@ -120,28 +120,10 @@ public class UniqueValueUtil { * @return true if the unique value is occupied, false otherwise */ public boolean isUniqueValueOccupied(String type, String... uniqueCombination) { - return formatValue(uniqueCombination) - .map(formattedValue -> isUniqueValueOccupied(type, formattedValue)) - .orElse(false); + return formatValue(uniqueCombination).map(formattedValue -> isUniqueValueOccupied(type, formattedValue)).orElse(false); } private boolean isUniqueValueOccupied(String type, String formattedValue) { return uniqueValueDao.get(new UniqueValueEntity(type, formattedValue)) != null; } - - private static Optional formatValue(String[] uniqueCombination) { - if (uniqueCombination == null || uniqueCombination.length == 0 - || getValueWithoutContext(uniqueCombination) == null) { - return Optional.empty(); - } - - uniqueCombination[uniqueCombination.length - 1] = - getValueWithoutContext(uniqueCombination).toLowerCase(); - return Optional.of(CommonMethods - .arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR)); - } - - private static String getValueWithoutContext(String... uniqueCombination) { - return uniqueCombination[uniqueCombination.length - 1]; - } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/CassandraBaseDao.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/CassandraBaseDao.java index 1b3dbc4c6f..6205622cea 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/CassandraBaseDao.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/CassandraBaseDao.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,34 +17,34 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.dao.impl; import com.datastax.driver.mapping.Mapper; import org.openecomp.core.dao.BaseDao; public abstract class CassandraBaseDao implements BaseDao { - protected abstract Mapper getMapper(); - protected abstract Object[] getKeys(T entity); + protected abstract Mapper getMapper(); + + protected abstract Object[] getKeys(T entity); - @Override - public void create(T entity) { - getMapper().save(entity); - } + @Override + public void create(T entity) { + getMapper().save(entity); + } - @Override - public void update(T entity) { - getMapper().save(entity); - } + @Override + public void update(T entity) { + getMapper().save(entity); + } - @Override - public T get(T entity) { - return getMapper().get(getKeys(entity)); - } + @Override + public T get(T entity) { + return getMapper().get(getKeys(entity)); + } - @Override - public void delete(T entity) { - getMapper().delete(entity); - } + @Override + public void delete(T entity) { + getMapper().delete(entity); + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueCassandraDaoImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueCassandraDaoImpl.java index a2f34bd329..b80050b370 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueCassandraDaoImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueCassandraDaoImpl.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,49 +17,43 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.dao.impl; import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.Result; import com.datastax.driver.mapping.annotations.Accessor; import com.datastax.driver.mapping.annotations.Query; +import java.util.Collection; import org.openecomp.core.dao.UniqueValueDao; import org.openecomp.core.dao.types.UniqueValueEntity; import org.openecomp.core.nosqldb.api.NoSqlDb; import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; -import java.util.Collection; - -public class UniqueValueCassandraDaoImpl extends CassandraBaseDao implements - UniqueValueDao { - - private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); - private static Mapper mapper = - noSqlDb.getMappingManager().mapper(UniqueValueEntity.class); - private static UniqueValueAccessor accessor = - noSqlDb.getMappingManager().createAccessor(UniqueValueAccessor.class); +public class UniqueValueCassandraDaoImpl extends CassandraBaseDao implements UniqueValueDao { + private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static Mapper mapper = noSqlDb.getMappingManager().mapper(UniqueValueEntity.class); + private static UniqueValueAccessor accessor = noSqlDb.getMappingManager().createAccessor(UniqueValueAccessor.class); - @Override - protected Mapper getMapper() { - return mapper; - } + @Override + protected Mapper getMapper() { + return mapper; + } - @Override - protected Object[] getKeys(UniqueValueEntity entity) { - return new Object[]{entity.getType(), entity.getValue()}; - } + @Override + protected Object[] getKeys(UniqueValueEntity entity) { + return new Object[]{entity.getType(), entity.getValue()}; + } - @Override - public Collection list(UniqueValueEntity entity) { - return accessor.listAll().all(); - } + @Override + public Collection list(UniqueValueEntity entity) { + return accessor.listAll().all(); + } - @Accessor - interface UniqueValueAccessor { + @Accessor + interface UniqueValueAccessor { - @Query("select * from unique_value") - Result listAll(); - } + @Query("select * from unique_value") + Result listAll(); + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueDaoFactoryImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueDaoFactoryImpl.java index ac47de9b20..36c0b208c8 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueDaoFactoryImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueDaoFactoryImpl.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,17 +17,17 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.dao.impl; import org.openecomp.core.dao.UniqueValueDao; import org.openecomp.core.dao.UniqueValueDaoFactory; public class UniqueValueDaoFactoryImpl extends UniqueValueDaoFactory { - private static UniqueValueDao instance = new UniqueValueCassandraDaoImpl(); - @Override - public UniqueValueDao createInterface() { - return instance; - } + private static UniqueValueDao instance = new UniqueValueCassandraDaoImpl(); + + @Override + public UniqueValueDao createInterface() { + return instance; + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbFactoryImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbFactoryImpl.java index 134d341d05..b62689d955 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbFactoryImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbFactoryImpl.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,28 +17,25 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.nosqldb.impl.cassandra; - import com.datastax.driver.core.Session; import org.openecomp.core.nosqldb.api.NoSqlDb; import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; public class CassandraNoSqlDbFactoryImpl extends NoSqlDbFactory { - @Override - public NoSqlDb createInterface() { - + @Override + public NoSqlDb createInterface() { + return new CassandraNoSqlDbImpl(ReferenceHolder.CASSANDRA); + } - return new CassandraNoSqlDbImpl(ReferenceHolder.CASSANDRA); - } + protected void stop() { + ReferenceHolder.CASSANDRA.close(); + } - protected void stop() { - ReferenceHolder.CASSANDRA.close(); - } + private static class ReferenceHolder { - private static class ReferenceHolder { - private static final Session CASSANDRA = CassandraSessionFactory.getSession(); - } + private static final Session CASSANDRA = CassandraSessionFactory.getSession(); + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbImpl.java index 539fbcd92f..237c48d013 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbImpl.java @@ -12,18 +12,17 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -*/ - + */ package org.openecomp.core.nosqldb.impl.cassandra; -import com.datastax.driver.mapping.MappingManager; import com.datastax.driver.core.BoundStatement; import com.datastax.driver.core.Host; import com.datastax.driver.core.PreparedStatement; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Session; - - +import com.datastax.driver.mapping.MappingManager; +import java.util.Set; +import java.util.stream.Collectors; import org.openecomp.core.nosqldb.api.NoSqlDb; import org.openecomp.core.nosqldb.util.CassandraUtils; import org.openecomp.core.utilities.CommonMethods; @@ -33,49 +32,34 @@ import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import java.util.Set; -import java.util.stream.Collectors; - class CassandraNoSqlDbImpl implements NoSqlDb { private final Session session; private final String keySpace; private final MappingManager mappingManager; - private final Logger log = LoggerFactory.getLogger(this.getClass().getName()); - public CassandraNoSqlDbImpl(Session session) { this.session = session; this.keySpace = this.session.getLoggedKeyspace(); this.mappingManager = new MappingManager(this.session); - } @Override public void insert(String tableName, String[] colNames, Object[] values) { if (colNames.length != values.length) { - throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage( - "number of colmuns[" + colNames.length + "] is not equal to the number of values[" - + values.length + "].").withId("E0005").withCategory(ErrorCategory.APPLICATION) - .build()); + throw new CoreException((new ErrorCode.ErrorCodeBuilder()) + .withMessage("number of colmuns[" + colNames.length + "] is not equal to the number of values[" + values.length + "].") + .withId("E0005").withCategory(ErrorCategory.APPLICATION).build()); } - StringBuilder sb = new StringBuilder(); - sb.append("insert into ") - .append(tableName) - .append(" (") - .append(CommonMethods.arrayToCommaSeparatedString(colNames)) - .append(") values (") - .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length)) - .append(")"); + sb.append("insert into ").append(tableName).append(" (").append(CommonMethods.arrayToCommaSeparatedString(colNames)).append(") values (") + .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length)).append(")"); log.info(sb.toString()); PreparedStatement prepared = session.prepare(sb.toString()); - BoundStatement bound; bound = prepared.bind(values); session.execute(bound); - } @Override @@ -85,21 +69,18 @@ class CassandraNoSqlDbImpl implements NoSqlDb { @Override public ResultSet execute(String statementName, Object... values) { - String statement = CassandraUtils.getStatement(statementName); if (statement == null) { statement = statementName; } if (values != null) { PreparedStatement prepared = session.prepare(statement); - BoundStatement bound; bound = prepared.bind(values); return session.execute(bound); } else { return session.execute(statement); } - } @Override @@ -111,10 +92,9 @@ class CassandraNoSqlDbImpl implements NoSqlDb { public String getVersion() { try { Set allHosts = this.session.getCluster().getMetadata().getAllHosts(); - Set versions = allHosts.stream().map(host -> host.getCassandraVersion().toString()) - .collect(Collectors.toSet()); + Set versions = allHosts.stream().map(host -> host.getCassandraVersion().toString()).collect(Collectors.toSet()); return versions.stream().collect(Collectors.joining(",")); - } catch (Exception e){ + } catch (Exception e) { log.error("Failed to retrieve version", e); return "Failed to retrieve version"; } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java index a507017888..8f3d51d618 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.core.nosqldb.impl.cassandra; import com.datastax.driver.core.Cluster; @@ -22,22 +21,23 @@ import com.datastax.driver.core.QueryOptions; import com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions; import com.datastax.driver.core.SSLOptions; import com.datastax.driver.core.Session; - - -import com.datastax.driver.core.policies.*; -import org.openecomp.core.nosqldb.util.CassandraUtils; -import org.openecomp.sdc.common.errors.SdcConfigurationException; -import org.openecomp.sdc.common.session.SessionContextProviderFactory; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManagerFactory; +import com.datastax.driver.core.policies.ConstantReconnectionPolicy; +import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; +import com.datastax.driver.core.policies.DefaultRetryPolicy; +import com.datastax.driver.core.policies.LoadBalancingPolicy; +import com.datastax.driver.core.policies.TokenAwarePolicy; import java.io.FileInputStream; import java.security.KeyStore; import java.security.SecureRandom; import java.util.Objects; import java.util.Optional; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import org.openecomp.core.nosqldb.util.CassandraUtils; +import org.openecomp.sdc.common.errors.SdcConfigurationException; +import org.openecomp.sdc.common.session.SessionContextProviderFactory; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; public class CassandraSessionFactory { @@ -60,57 +60,42 @@ public class CassandraSessionFactory { String[] addresses = CassandraUtils.getAddresses(); int cassandraPort = CassandraUtils.getCassandraPort(); Long reconnectTimeout = CassandraUtils.getReconnectTimeout(); - Cluster.Builder builder = Cluster.builder(); - - if(null != reconnectTimeout) { - builder.withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectTimeout)) - .withRetryPolicy(DefaultRetryPolicy.INSTANCE); + if (null != reconnectTimeout) { + builder.withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectTimeout)).withRetryPolicy(DefaultRetryPolicy.INSTANCE); } - builder.withPort(cassandraPort); - for (String address : addresses) { builder.addContactPoint(address); } - //Check if ssl Boolean isSsl = CassandraUtils.isSsl(); if (isSsl) { builder.withSSL(getSslOptions()); } - //Check if user/pass Boolean isAuthenticate = CassandraUtils.isAuthenticate(); if (isAuthenticate) { builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword()); } - setConsistencyLevel(builder, addresses); - setLocalDataCenter(builder); - Cluster cluster = builder.build(); - String keyStore = SessionContextProviderFactory.getInstance().createInterface().get() - .getTenant(); - LOGGER.info("Cassandra client created hosts: {} port: {} SSL enabled: {} reconnectTimeout", - addresses, cassandraPort, isSsl, reconnectTimeout); + String keyStore = SessionContextProviderFactory.getInstance().createInterface().get().getTenant(); + LOGGER + .info("Cassandra client created hosts: {} port: {} SSL enabled: {} reconnectTimeout", addresses, cassandraPort, isSsl, reconnectTimeout); return cluster.connect(keyStore); } private static void setLocalDataCenter(Cluster.Builder builder) { String localDataCenter = CassandraUtils.getLocalDataCenter(); if (Objects.nonNull(localDataCenter)) { - LOGGER.info("localDatacenter was provided, setting Cassndra client to use datacenter: {} as local.", - localDataCenter); - - LoadBalancingPolicy tokenAwarePolicy = new TokenAwarePolicy( - DCAwareRoundRobinPolicy.builder().withLocalDc(localDataCenter).build()); + LOGGER.info("localDatacenter was provided, setting Cassndra client to use datacenter: {} as local.", localDataCenter); + LoadBalancingPolicy tokenAwarePolicy = new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(localDataCenter).build()); builder.withLoadBalancingPolicy(tokenAwarePolicy); } else { - LOGGER.info( - "localDatacenter was provided, the driver will use the datacenter of the first contact " + - "point that was reached at initialization"); + LOGGER.info("localDatacenter was provided, the driver will use the datacenter of the first contact " + + "point that was reached at initialization"); } } @@ -118,54 +103,42 @@ public class CassandraSessionFactory { if (addresses != null && addresses.length > 1) { String consistencyLevel = CassandraUtils.getConsistencyLevel(); if (Objects.nonNull(consistencyLevel)) { - LOGGER.info( - "consistencyLevel was provided, setting Cassandra client to use consistencyLevel: {}" + - " as " - , consistencyLevel); - builder.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf - (consistencyLevel))); + LOGGER.info("consistencyLevel was provided, setting Cassandra client to use consistencyLevel: {}" + " as ", consistencyLevel); + builder.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel))); } } } private static SSLOptions getSslOptions() { - Optional trustStorePath = Optional.ofNullable(CassandraUtils.getTruststore()); if (!trustStorePath.isPresent()) { throw new SdcConfigurationException("Missing configuration for Cassandra trustStorePath"); } - Optional trustStorePassword = Optional.ofNullable(CassandraUtils.getTruststorePassword()); if (!trustStorePassword.isPresent()) { throw new SdcConfigurationException("Missing configuration for Cassandra trustStorePassword"); } - SSLContext context = getSslContext(trustStorePath.get(), trustStorePassword.get()); String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"}; return RemoteEndpointAwareJdkSSLOptions.builder().withSSLContext(context).withCipherSuites(css).build(); } private static SSLContext getSslContext(String truststorePath, String trustStorePassword) { - try (FileInputStream tsf = new FileInputStream(truststorePath)) { - SSLContext ctx = SSLContext.getInstance("TLS"); - KeyStore ts = KeyStore.getInstance("JKS"); ts.load(tsf, trustStorePassword.toCharArray()); - TrustManagerFactory tmf = - TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ts); - ctx.init(null, tmf.getTrustManagers(), new SecureRandom()); return ctx; - } catch (Exception exception) { throw new SdcConfigurationException("Failed to get SSL Contexts for Cassandra connection", exception); } } private static class ReferenceHolder { + private static final Session CASSANDRA = newCassandraSession(); private ReferenceHolder() { diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java index 30bc0a72a3..d6840fedd1 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,90 +17,76 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.nosqldb.util; -import org.openecomp.core.utilities.file.FileUtils; -import org.openecomp.core.utilities.json.JsonUtil; - import java.util.HashMap; import java.util.Map; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.json.JsonUtil; public class CassandraUtils { + private static final String CASSANDRA_STATEMENT_DEFINITION_FILE = "cassandraStatements.json"; + private static Map statementMap = new HashMap<>(); - private static final String CASSANDRA_STATEMENT_DEFINITION_FILE = "cassandraStatements.json"; - private static Map statementMap = new HashMap<>(); - - public static String[] getAddresses() { - return ConfigurationManager.getInstance().getAddresses(); - } - - public static Long getReconnectTimeout() { - return ConfigurationManager.getInstance().getReconnectTimeout(); - } - - public static String getKeySpace() { - return ConfigurationManager.getInstance().getKeySpace(); - } - - /** - * Gets statement. - * - * @param statementName the statement name - * @return the statement - */ - public static String getStatement(String statementName) { - - if (statementMap.size() == 0) { - statementMap = FileUtils.readViaInputStream(CASSANDRA_STATEMENT_DEFINITION_FILE, - stream -> JsonUtil.json2Object(stream, Map.class)); + public static String[] getAddresses() { + return ConfigurationManager.getInstance().getAddresses(); } - return statementMap.get(statementName); - } - - public static String getUser() { - - return ConfigurationManager.getInstance().getUsername(); - } - - public static String getPassword() { - return ConfigurationManager.getInstance().getPassword(); - - } - - public static String getTruststore() { - return ConfigurationManager.getInstance().getTruststorePath(); - - } + public static Long getReconnectTimeout() { + return ConfigurationManager.getInstance().getReconnectTimeout(); + } - public static String getTruststorePassword() { - return ConfigurationManager.getInstance().getTruststorePassword(); + public static String getKeySpace() { + return ConfigurationManager.getInstance().getKeySpace(); + } - } + /** + * Gets statement. + * + * @param statementName the statement name + * @return the statement + */ + public static String getStatement(String statementName) { + if (statementMap.size() == 0) { + statementMap = FileUtils.readViaInputStream(CASSANDRA_STATEMENT_DEFINITION_FILE, stream -> JsonUtil.json2Object(stream, Map.class)); + } + return statementMap.get(statementName); + } - public static int getCassandraPort() { - return ConfigurationManager.getInstance().getCassandraPort(); + public static String getUser() { + return ConfigurationManager.getInstance().getUsername(); + } - } + public static String getPassword() { + return ConfigurationManager.getInstance().getPassword(); + } - public static boolean isSsl() { - return ConfigurationManager.getInstance().isSsl(); + public static String getTruststore() { + return ConfigurationManager.getInstance().getTruststorePath(); + } - } + public static String getTruststorePassword() { + return ConfigurationManager.getInstance().getTruststorePassword(); + } - public static boolean isAuthenticate() { - return ConfigurationManager.getInstance().isAuthenticate(); - } + public static int getCassandraPort() { + return ConfigurationManager.getInstance().getCassandraPort(); + } - public static String getConsistencyLevel() { + public static boolean isSsl() { + return ConfigurationManager.getInstance().isSsl(); + } - return ConfigurationManager.getInstance().getConsistencyLevel(); + public static boolean isAuthenticate() { + return ConfigurationManager.getInstance().isAuthenticate(); + } - } + public static String getConsistencyLevel() { + return ConfigurationManager.getInstance().getConsistencyLevel(); + } - public static String getLocalDataCenter() { - return ConfigurationManager.getInstance().getLocalDataCenter(); - } + public static String getLocalDataCenter() { + return ConfigurationManager.getInstance().getLocalDataCenter(); + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java index a1d5246eee..666901edfe 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,15 +17,8 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.core.nosqldb.util; -import org.apache.commons.collections4.CollectionUtils; -import org.onap.sdc.tosca.services.YamlUtil; -import org.openecomp.core.utilities.file.FileUtils; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; - import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -35,248 +28,241 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Function; +import org.apache.commons.collections4.CollectionUtils; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; /** * The type Configuration manager. */ public class ConfigurationManager { - static final String CONFIGURATION_YAML_FILE = "configuration.yaml"; - static private final Integer DEFAULT_CASSANDRA_PORT = 9042; - private static final String CASSANDRA = "cassandra"; - private static final String CASSANDRA_KEY = CASSANDRA + "Config"; - private static final String DEFAULT_KEYSPACE_NAME = "dox"; - private static final String CASSANDRA_ADDRESSES = CASSANDRA + ".addresses"; - private static final String CASSANDRA_DOX_KEY_STORE = CASSANDRA + ".dox.keystore"; - private static final String CASSANDRA_AUTHENTICATE = CASSANDRA + ".authenticate"; - private static final String CASSANDRA_USER = CASSANDRA + ".user"; - private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password"; - private static final String CASSANDRA_SSL = CASSANDRA + ".ssl"; - private static final String CASSANDRA_TRUSTSTORE = CASSANDRA + ".Truststore"; - private static final String CASSANDRA_TRUSTSTORE_PASSWORD = CASSANDRA + ".TruststorePassword"; - private static final String CASSANDRA_HOSTS_KEY = CASSANDRA + "Hosts"; - private static final String CASSANDRA_PORT_KEY = "cassandraPort"; - private static final String CASSANDRA_USERNAME_KEY = "username"; - private static final String CASSANDRA_RECONNECT_TIMEOUT = "reconnectTimeout"; - @SuppressWarnings("squid:S2068") - private static final String CASSANDRA_PASSWORD_KEY = "password"; - private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate"; - private static final String CASSANDRA_SSL_KEY = "ssl"; - private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath"; - @SuppressWarnings("squid:S2068") - private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword"; - private static final String CONSISTENCY_LEVEL = CASSANDRA + ".consistencyLevel"; - private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel"; - private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter"; - private static final String LOCAL_DATA_CENTER = CASSANDRA + ".localDataCenter"; - private static ConfigurationManager instance = null; - private final LinkedHashMap cassandraConfiguration; - - private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class); - - - private ConfigurationManager() { - - String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); - - Function>> reader = is -> { - YamlUtil yamlUtil = new YamlUtil(); - return yamlUtil.yamlToMap(is); - }; - - try { - - Map> configurationMap = configurationYamlFile != null - ? readFromFile(configurationYamlFile, reader) // load from file - : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource - cassandraConfiguration = configurationMap.get(CASSANDRA_KEY); - - } catch (IOException e) { - throw new RuntimeException("Failed to read configuration", e); + static final String CONFIGURATION_YAML_FILE = "configuration.yaml"; + static private final Integer DEFAULT_CASSANDRA_PORT = 9042; + private static final String CASSANDRA = "cassandra"; + private static final String CASSANDRA_KEY = CASSANDRA + "Config"; + private static final String DEFAULT_KEYSPACE_NAME = "dox"; + private static final String CASSANDRA_ADDRESSES = CASSANDRA + ".addresses"; + private static final String CASSANDRA_DOX_KEY_STORE = CASSANDRA + ".dox.keystore"; + private static final String CASSANDRA_AUTHENTICATE = CASSANDRA + ".authenticate"; + private static final String CASSANDRA_USER = CASSANDRA + ".user"; + private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password"; + private static final String CASSANDRA_SSL = CASSANDRA + ".ssl"; + private static final String CASSANDRA_TRUSTSTORE = CASSANDRA + ".Truststore"; + private static final String CASSANDRA_TRUSTSTORE_PASSWORD = CASSANDRA + ".TruststorePassword"; + private static final String CASSANDRA_HOSTS_KEY = CASSANDRA + "Hosts"; + private static final String CASSANDRA_PORT_KEY = "cassandraPort"; + private static final String CASSANDRA_USERNAME_KEY = "username"; + private static final String CASSANDRA_RECONNECT_TIMEOUT = "reconnectTimeout"; + @SuppressWarnings("squid:S2068") + private static final String CASSANDRA_PASSWORD_KEY = "password"; + private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate"; + private static final String CASSANDRA_SSL_KEY = "ssl"; + private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath"; + @SuppressWarnings("squid:S2068") + private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword"; + private static final String CONSISTENCY_LEVEL = CASSANDRA + ".consistencyLevel"; + private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel"; + private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter"; + private static final String LOCAL_DATA_CENTER = CASSANDRA + ".localDataCenter"; + private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class); + private static ConfigurationManager instance = null; + private final LinkedHashMap cassandraConfiguration; + + private ConfigurationManager() { + String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); + Function>> reader = is -> { + YamlUtil yamlUtil = new YamlUtil(); + return yamlUtil.yamlToMap(is); + }; + try { + Map> configurationMap = configurationYamlFile != null + ? readFromFile(configurationYamlFile, reader) // load from file + + : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource + cassandraConfiguration = configurationMap.get(CASSANDRA_KEY); + } catch (IOException e) { + throw new RuntimeException("Failed to read configuration", e); + } } - } - /** - * Gets instance. - * - * @return the instance - */ - public static ConfigurationManager getInstance() { - if (Objects.isNull(instance)) { - instance = new ConfigurationManager(); + /** + * Gets instance. + * + * @return the instance + */ + public static ConfigurationManager getInstance() { + if (Objects.isNull(instance)) { + instance = new ConfigurationManager(); + } + return instance; } - return instance; - } - - /** - * Get addresses string [ ]. - * - * @return the string [ ] - */ - public String[] getAddresses() { - String addresses = System.getProperty(CASSANDRA_ADDRESSES); - if (Objects.nonNull(addresses)) { - return addresses.split(","); + /** + * Get addresses string [ ]. + * + * @return the string [ ] + */ + public String[] getAddresses() { + String addresses = System.getProperty(CASSANDRA_ADDRESSES); + if (Objects.nonNull(addresses)) { + return addresses.split(","); + } + List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY); + if (CollectionUtils.isEmpty(lsAddresses)) { + LOG.info("No Cassandra hosts are defined."); + } + String[] addressesArray; + addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]); + return addressesArray; } - List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY); - if (CollectionUtils.isEmpty(lsAddresses)) { - LOG.info("No Cassandra hosts are defined."); - } - - String[] addressesArray; - addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]); - return addressesArray; - } - - /** - * Gets Cassandra port. - * - * @return the port - */ - public int getCassandraPort() { - Integer cassandraPort = (Integer) cassandraConfiguration.get(CASSANDRA_PORT_KEY); - if (Objects.isNull(cassandraPort)) { - cassandraPort = DEFAULT_CASSANDRA_PORT; + /** + * Gets Cassandra port. + * + * @return the port + */ + public int getCassandraPort() { + Integer cassandraPort = (Integer) cassandraConfiguration.get(CASSANDRA_PORT_KEY); + if (Objects.isNull(cassandraPort)) { + cassandraPort = DEFAULT_CASSANDRA_PORT; + } + return cassandraPort; } - return cassandraPort; - } - /** - * Gets Cassandra reconnection timeout - * - * @return - */ - public Long getReconnectTimeout() { - Integer cassandraReconnectTimeout = (Integer) cassandraConfiguration.get(CASSANDRA_RECONNECT_TIMEOUT); - if (Objects.isNull(cassandraReconnectTimeout)) { - LOG.info("No Cassandra reconnect timeout are defined."); - return null; + /** + * Gets Cassandra reconnection timeout + * + * @return + */ + public Long getReconnectTimeout() { + Integer cassandraReconnectTimeout = (Integer) cassandraConfiguration.get(CASSANDRA_RECONNECT_TIMEOUT); + if (Objects.isNull(cassandraReconnectTimeout)) { + LOG.info("No Cassandra reconnect timeout are defined."); + return null; + } + return cassandraReconnectTimeout.longValue(); } - return cassandraReconnectTimeout.longValue(); - } - /** - * Gets key space. - * - * @return the key space - */ - public String getKeySpace() { - String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE); - if (Objects.isNull(keySpace)) { - keySpace = DEFAULT_KEYSPACE_NAME; + /** + * Gets key space. + * + * @return the key space + */ + public String getKeySpace() { + String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE); + if (Objects.isNull(keySpace)) { + keySpace = DEFAULT_KEYSPACE_NAME; + } + return keySpace; } - return keySpace; - } - /** - * Gets username. - * - * @return the username - */ - public String getUsername() { - String username = System.getProperty(CASSANDRA_USER); - if (Objects.isNull(username)) { - username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY); + /** + * Gets username. + * + * @return the username + */ + public String getUsername() { + String username = System.getProperty(CASSANDRA_USER); + if (Objects.isNull(username)) { + username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY); + } + return username; } - return username; - } - /** - * Gets password. - * - * @return the password - */ - public String getPassword() { - String password = System.getProperty(CASSANDRA_PASSWORD); - if (Objects.isNull(password)) { - password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY); + /** + * Gets password. + * + * @return the password + */ + public String getPassword() { + String password = System.getProperty(CASSANDRA_PASSWORD); + if (Objects.isNull(password)) { + password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY); + } + return password; } - return password; - } - /** - * Gets truststore path. - * - * @return the truststore path - */ - public String getTruststorePath() { - String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); - if (Objects.isNull(truststorePath)) { - truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY); + /** + * Gets truststore path. + * + * @return the truststore path + */ + public String getTruststorePath() { + String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); + if (Objects.isNull(truststorePath)) { + truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY); + } + return truststorePath; } - return truststorePath; - } - /** - * Gets truststore password. - * - * @return the truststore password - */ - public String getTruststorePassword() { - String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); - if (Objects.isNull(truststorePassword)) { - truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY); + /** + * Gets truststore password. + * + * @return the truststore password + */ + public String getTruststorePassword() { + String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); + if (Objects.isNull(truststorePassword)) { + truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY); + } + return truststorePassword; } - return truststorePassword; - } - - /** - * Is ssl boolean. - * - * @return the boolean - */ - public boolean isSsl() { - return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY); - } - /** - * Is authenticate boolean. - * - * @return the boolean - */ - public boolean isAuthenticate() { - return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY); - } - - private Boolean getBooleanResult(String property, String key) { - Boolean res; - String value; - if (System.getProperty(property) == null) { - value = String.valueOf(cassandraConfiguration.get(key)); - } else { - value = System.getProperty(property); + /** + * Is ssl boolean. + * + * @return the boolean + */ + public boolean isSsl() { + return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY); } - res = Boolean.valueOf(value); - - return res; - } + /** + * Is authenticate boolean. + * + * @return the boolean + */ + public boolean isAuthenticate() { + return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY); + } - private T readFromFile(String file, Function reader) throws IOException { - try (InputStream is = new FileInputStream(file)) { - return reader.apply(is); + private Boolean getBooleanResult(String property, String key) { + Boolean res; + String value; + if (System.getProperty(property) == null) { + value = String.valueOf(cassandraConfiguration.get(key)); + } else { + value = System.getProperty(property); + } + res = Boolean.valueOf(value); + return res; } - } - public String getConsistencyLevel() { - String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL); - if (Objects.isNull(consistencyLevel)) { - consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY); + private T readFromFile(String file, Function reader) throws IOException { + try (InputStream is = new FileInputStream(file)) { + return reader.apply(is); + } } - if (Objects.isNull(consistencyLevel)) { - consistencyLevel = "LOCAL_QUORUM"; + public String getConsistencyLevel() { + String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL); + if (Objects.isNull(consistencyLevel)) { + consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY); + } + if (Objects.isNull(consistencyLevel)) { + consistencyLevel = "LOCAL_QUORUM"; + } + return consistencyLevel; } - return consistencyLevel; - } - public String getLocalDataCenter() { - String localDataCenter = System.getProperty(LOCAL_DATA_CENTER); - if (Objects.isNull(localDataCenter)) { - localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY); + public String getLocalDataCenter() { + String localDataCenter = System.getProperty(LOCAL_DATA_CENTER); + if (Objects.isNull(localDataCenter)) { + localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY); + } + return localDataCenter; } - return localDataCenter; - } } -- cgit 1.2.3-korg