From f5f13c4f6b6fe3b4d98e349dfd7db59339803436 Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Sun, 19 Feb 2017 12:35:04 +0200 Subject: push addional code Change-Id: Ia427bb3460cda3a896f8faced2de69eaf3807b74 Signed-off-by: Michael Lando --- .../openecomp/core/dao/impl/CassandraBaseDao.java | 50 +++++ .../core/dao/impl/UniqueValueCassandraDaoImpl.java | 65 ++++++ .../core/dao/impl/UniqueValueDaoFactoryImpl.java | 33 +++ .../cassandra/CassandraNoSqlDbFactoryImpl.java | 43 ++++ .../impl/cassandra/CassandraNoSqlDbImpl.java | 104 +++++++++ .../impl/cassandra/CassandraSessionFactory.java | 124 +++++++++++ .../core/nosqldb/util/CassandraUtils.java | 92 ++++++++ .../core/nosqldb/util/ConfigurationManager.java | 234 +++++++++++++++++++++ 8 files changed, 745 insertions(+) create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/CassandraBaseDao.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueCassandraDaoImpl.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueDaoFactoryImpl.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbFactoryImpl.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbImpl.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java') 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 new file mode 100644 index 0000000000..1b3dbc4c6f --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/CassandraBaseDao.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * 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); + + @Override + public void create(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 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 new file mode 100644 index 0000000000..31dd602400 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueCassandraDaoImpl.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * 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 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); + + + @Override + protected Mapper getMapper() { + return mapper; + } + + @Override + protected Object[] getKeys(UniqueValueEntity entity) { + return new Object[]{entity.getType(), entity.getValue()}; + } + + @Override + public Collection list(UniqueValueEntity entity) { + return accessor.listAll().all(); + } + + @Accessor + interface UniqueValueAccessor { + + @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 new file mode 100644 index 0000000000..b717fcbee1 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/dao/impl/UniqueValueDaoFactoryImpl.java @@ -0,0 +1,33 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * 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; + } +} 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 new file mode 100644 index 0000000000..2922925171 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbFactoryImpl.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * 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() { + + + return new CassandraNoSqlDbImpl(ReferenceHolder.CASSANDRA); + } + + protected void stop() { + ReferenceHolder.CASSANDRA.close(); + } + + private static class ReferenceHolder { + 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 new file mode 100644 index 0000000000..bcac5f8d79 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraNoSqlDbImpl.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.nosqldb.impl.cassandra; + +import com.datastax.driver.core.BoundStatement; +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 org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.util.CassandraUtils; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +class CassandraNoSqlDbImpl implements NoSqlDb { + + private final Session session; + private final String keySpace; + private final MappingManager mappingManager; + + + 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()); + } + + StringBuilder sb = new StringBuilder(); + sb.append("insert into ") + .append(tableName) + .append(" (") + .append(CommonMethods.arrayToCommaSeparatedString(colNames)) + .append(") values (") + .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length)) + .append(")"); + System.out.println(sb.toString()); + PreparedStatement prepared = session.prepare(sb.toString()); + + BoundStatement bound; + bound = prepared.bind(values); + session.execute(bound); + + } + + @Override + public ResultSet execute(String statement) { + return session.execute(statement); + } + + @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 + public MappingManager getMappingManager() { + return mappingManager; + } + +} 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 new file mode 100644 index 0000000000..1b98f095b5 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java @@ -0,0 +1,124 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.nosqldb.impl.cassandra; + +import com.google.common.base.Optional; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.SSLOptions; +import com.datastax.driver.core.Session; + +import org.openecomp.core.nosqldb.util.CassandraUtils; + +import java.io.FileInputStream; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; + +public class CassandraSessionFactory { + public static Session getSession() { + return ReferenceHolder.CASSANDRA; + } + + /** + * New cassandra session session. + * + * @return the session + */ + public static Session newCassandraSession() { + Cluster.Builder builder = Cluster.builder(); + String[] addresses = CassandraUtils.getAddresses(); + for (String address : addresses) { + builder.addContactPoint(address); + } + Boolean isSsl = CassandraUtils.isSsl(); + if (isSsl) { + builder.withSSL(getSslOptions().get()); + } + int port = CassandraUtils.getCassandraPort(); + if (port > 0) { + builder.withPort(port); + } + //Check if user/pass + Boolean isAuthenticate = CassandraUtils.isAuthenticate(); + if (isAuthenticate) { + builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword()); + } + + Cluster cluster = builder.build(); + String keyStore = CassandraUtils.getKeySpace(); + return cluster.connect(keyStore); + } + + private static Optional getSslOptions() { + Optional truststorePath = Optional.of(CassandraUtils.getTruststore()); + Optional truststorePassword = Optional.of(CassandraUtils.getTruststorePassword()); + + if (truststorePath.isPresent() && truststorePassword.isPresent()) { + SSLContext context; + try { + context = getSslContext(truststorePath.get(), truststorePassword.get()); + } catch (UnrecoverableKeyException | KeyManagementException + | NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e0) { + throw new RuntimeException(e0); + } + String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"}; + return Optional.of(new SSLOptions(context, css)); + } + return Optional.absent(); + } + + private static SSLContext getSslContext(String truststorePath, String truststorePassword) + throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, + UnrecoverableKeyException, KeyManagementException { + FileInputStream tsf = null; + SSLContext ctx = null; + try { + tsf = new FileInputStream(truststorePath); + ctx = SSLContext.getInstance("SSL"); + + KeyStore ts = KeyStore.getInstance("JKS"); + ts.load(tsf, truststorePassword.toCharArray()); + TrustManagerFactory tmf = + TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(ts); + + ctx.init(null, tmf.getTrustManagers(), new SecureRandom()); + } catch (Exception e0) { + e0.printStackTrace(); + } finally { + tsf.close(); + + } + return ctx; + } + + private static class ReferenceHolder { + private static final Session CASSANDRA = newCassandraSession(); + } +} 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 new file mode 100644 index 0000000000..2a88d0e521 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * 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.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +public class CassandraUtils { + + + 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 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) { + InputStream statementJson = FileUtils.getFileInputStream(CASSANDRA_STATEMENT_DEFINITION_FILE); + statementMap = JsonUtil.json2Object(statementJson, Map.class); + } + 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 String getTruststorePassword() { + return ConfigurationManager.getInstance().getTruststorePassword(); + + } + + public static int getCassandraPort() { + return ConfigurationManager.getInstance().getSslPort(); + + } + + public static boolean isSsl() { + return ConfigurationManager.getInstance().isSsl(); + + } + + public static boolean isAuthenticate() { + return ConfigurationManager.getInstance().isAuthenticate(); + } +} 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 new file mode 100644 index 0000000000..a1c0b528fb --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java @@ -0,0 +1,234 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.nosqldb.util; + +import org.openecomp.core.utilities.yaml.YamlUtil; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * The type Configuration manager. + */ +public class ConfigurationManager { + + private static final String CONFIGURATION_YAML_FILE = "configuration.yaml"; + private static final String cassandraKey = "cassandraConfig"; + 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_PORT = "cassandra.port"; + 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 cassandraHostsKey = "cassandraHosts"; + private static final String cassandraPortKey = "port"; + private static final String cassandraUsernameKey = "username"; + private static final String cassandraPasswordKey = "password"; + private static final String cassandraAuthenticateKey = "authenticate"; + private static final String cassandraSSLKey = "ssl"; + private static final String cassandraTruststorePathKey = "truststorePath"; + private static final String cassandraTruststorePasswordKey = "truststorePassword"; + private static ConfigurationManager instance = null; + private final LinkedHashMap cassandraConfiguration; + + + private ConfigurationManager() { + YamlUtil yamlUtil = new YamlUtil(); + String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); + InputStream yamlAsString; + if (configurationYamlFile != null) { + yamlAsString = getConfigFileIs(configurationYamlFile); + } else { + //Load from resources + yamlAsString = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE); + } + Map> configurationMap = yamlUtil.yamlToMap(yamlAsString); + cassandraConfiguration = configurationMap.get(cassandraKey); + + } + + /** + * Gets instance. + * + * @return the instance + */ + public static ConfigurationManager getInstance() { + if (instance == null) { + instance = new ConfigurationManager(); + } + return instance; + } + + /** + * Get addresses string [ ]. + * + * @return the string [ ] + */ + public String[] getAddresses() { + + String addresses = System.getProperty(CASSANDRA_ADDRESSES); + if (addresses != null) { + return addresses.split(","); + } + List locAddresses = (ArrayList) cassandraConfiguration.get(cassandraHostsKey); + String[] addressesArray; + addressesArray = (String[]) locAddresses.toArray(new String[locAddresses.size()]); + return addressesArray; + + } + + /** + * Gets key space. + * + * @return the key space + */ + public String getKeySpace() { + String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE); + if (keySpace == null) { + //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey); + //if (keySpace == null) + keySpace = DEFAULT_KEYSPACE_NAME; + } + return keySpace; + } + + /** + * Gets username. + * + * @return the username + */ + public String getUsername() { + String username = System.getProperty(CASSANDRA_USER); + if (username == null) { + username = (String) cassandraConfiguration.get(cassandraUsernameKey); + } + return username; + } + + /** + * Gets password. + * + * @return the password + */ + public String getPassword() { + String password = System.getProperty(CASSANDRA_PASSWORD); + if (password == null) { + password = (String) cassandraConfiguration.get(cassandraPasswordKey); + } + return password; + } + + /** + * Gets truststore path. + * + * @return the truststore path + */ + public String getTruststorePath() { + String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); + if (truststorePath == null) { + truststorePath = (String) cassandraConfiguration.get(cassandraTruststorePathKey); + } + return truststorePath; + } + + /** + * Gets truststore password. + * + * @return the truststore password + */ + public String getTruststorePassword() { + String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); + if (truststorePassword == null) { + truststorePassword = (String) cassandraConfiguration.get(cassandraTruststorePasswordKey); + } + return truststorePassword; + } + + /** + * Gets ssl port. + * + * @return the ssl port + */ + public int getSslPort() { + int port; + String sslPort = System.getProperty(CASSANDRA_PORT); + if (sslPort == null) { + sslPort = (String) cassandraConfiguration.get(cassandraPortKey); + if (sslPort == null) { + sslPort = "0"; + } + } + port = Integer.valueOf(sslPort); + return port; + } + + + /** + * Is ssl boolean. + * + * @return the boolean + */ + public boolean isSsl() { + return getBooleanResult(CASSANDRA_SSL, cassandraSSLKey); + } + + /** + * Is authenticate boolean. + * + * @return the boolean + */ + public boolean isAuthenticate() { + return getBooleanResult(CASSANDRA_AUTHENTICATE, cassandraAuthenticateKey); + } + + 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; + } + + private InputStream getConfigFileIs(String file) { + InputStream is = null; + try { + is = new FileInputStream(file); + } catch (FileNotFoundException e0) { + e0.printStackTrace(); + } + return is; + } +} -- cgit 1.2.3-korg