diff options
author | Mohammad Salehe <salehe@cs.toronto.edu> | 2019-02-08 11:58:35 -0500 |
---|---|---|
committer | Mohammad Salehe <salehe@cs.toronto.edu> | 2019-02-24 22:23:39 -0500 |
commit | ee0e4cef1eaba1691f2a69c94f9707a9dbcfcd63 (patch) | |
tree | 0ea14a449dd3e1c7cb9269dfba65bd650c8af23c /src/main/java/org/onap/music/datastore/MusicDataStore.java | |
parent | f6b5b60d066fa47ef520c58305aa6f28827f106b (diff) |
Make MusicCore non-staticdev-cassandra-only
Make MusicCore and other dependencies (MusicDataStoreHandle) non-static
For compatibility, MusicCore still has a singleton instance that is statically instantiated and can be used as needed by legacy users.
These changes should be made in calling sites (legacy users using singleton):
MusicDataStoreHandle.getDSHandle() -> MusicCore.getInstanceDSHandle()
Change-Id: I02b67f316dc21a1498157bc68cc32bf76d3ec01e
Issue-ID: MUSIC-148
Signed-off-by: Mohammad Salehe <salehe@cs.toronto.edu>
Diffstat (limited to 'src/main/java/org/onap/music/datastore/MusicDataStore.java')
-rw-r--r-- | src/main/java/org/onap/music/datastore/MusicDataStore.java | 104 |
1 files changed, 7 insertions, 97 deletions
diff --git a/src/main/java/org/onap/music/datastore/MusicDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java index 1ea10054..deb65edd 100644 --- a/src/main/java/org/onap/music/datastore/MusicDataStore.java +++ b/src/main/java/org/onap/music/datastore/MusicDataStore.java @@ -65,13 +65,6 @@ public class MusicDataStore { /** - * Constructs DataStore by connecting to local Cassandra - */ - public MusicDataStore() { - connectToLocalCassandraCluster(); - } - - /** * Constructs DataStore by providing existing cluster and session * @param cluster * @param session @@ -82,94 +75,6 @@ public class MusicDataStore { } /** - * Constructs DataStore by connecting to provided remote Cassandra - * @param remoteAddress - * @throws MusicServiceException - */ - public MusicDataStore(String remoteAddress) { - try { - connectToRemoteCassandraCluster(remoteAddress); - } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); - } - } - - private void createCassandraSession(String address) throws NoHostAvailableException { - cluster = Cluster.builder().withPort(9042) - .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd()) - .addContactPoint(address).build(); - Metadata metadata = cluster.getMetadata(); - logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster " - + metadata.getClusterName() + " at " + address); - session = cluster.connect(); - } - - /** - * - * @return - */ - private ArrayList<String> getAllPossibleLocalIps() { - ArrayList<String> allPossibleIps = new ArrayList<String>(); - try { - Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); - while (en.hasMoreElements()) { - NetworkInterface ni = (NetworkInterface) en.nextElement(); - Enumeration<InetAddress> ee = ni.getInetAddresses(); - while (ee.hasMoreElements()) { - InetAddress ia = (InetAddress) ee.nextElement(); - allPossibleIps.add(ia.getHostAddress()); - } - } - } catch (SocketException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.CONNCECTIVITYERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); - }catch(Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); - } - return allPossibleIps; - } - - /** - * This method iterates through all available local IP addresses and tries to connect to first successful one - */ - private void connectToLocalCassandraCluster() { - ArrayList<String> localAddrs = getAllPossibleLocalIps(); - localAddrs.add(0, "localhost"); - logger.info(EELFLoggerDelegate.applicationLogger, - "Connecting to cassa cluster: Iterating through possible ips:" - + getAllPossibleLocalIps()); - for (String address: localAddrs) { - try { - createCassandraSession(address); - break; - } catch (NoHostAvailableException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.HOSTUNAVAILABLE, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); - } - } - } - - /** - * This method connects to cassandra cluster on specific address. - * - * @param address - */ - private void connectToRemoteCassandraCluster(String address) throws MusicServiceException { - try { - createCassandraSession(address); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY, ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE); - throw new MusicServiceException( - "Error while connecting to Cassandra cluster.. " + ex.getMessage()); - } - } - - /** - * - */ - public void close() { - session.close(); - } - - /** * * @param keyspace * @param tableName @@ -203,7 +108,7 @@ public class MusicDataStore { * @param colType * @return */ - public Object getColValue(Row row, String colName, DataType colType) { + public static Object getColValue(Row row, String colName, DataType colType) { switch (colType.getName()) { case VARCHAR: @@ -237,7 +142,7 @@ public class MusicDataStore { return data; } - public boolean doesRowSatisfyCondition(Row row, Map<String, Object> condition) throws Exception { + public static boolean doesRowSatisfyCondition(Row row, Map<String, Object> condition) throws Exception { ColumnDefinitions colInfo = row.getColumnDefinitions(); for (Map.Entry<String, Object> entry : condition.entrySet()) { @@ -453,5 +358,10 @@ public class MusicDataStore { TimeMeasureInstance.instance().exit(); } } + + @Deprecated + public void close() { + session.close(); + } } |