Multiple DBAAS Redis standalone groups

Added support to have one or more Redis standalone DB groups (like
The Redis Sentinel group in previous ece67088 commit). The DB
groups can be used to distribute SDL DB operations to different
standalone DB instances.

Release version 1.2.1

Issue-ID: RIC-699
Change-Id: Ib7da0200298c8c016dee05743b815c07d04baa76
Signed-off-by: Petri Ovaska <petri.ovaska@nokia.com>
diff --git a/src/configurationreader.cpp b/src/configurationreader.cpp
index 18945b6..1870a87 100644
--- a/src/configurationreader.cpp
+++ b/src/configurationreader.cpp
@@ -195,6 +195,19 @@
         for (const auto &namespaceConfigurationMapItem : namespaceConfigurationMap )
             parseNsConfiguration(namespaceConfigurations, namespaceConfigurationMapItem.first, namespaceConfigurationMapItem.second.first, namespaceConfigurationMapItem.second.second);
     }
+
+    void appendDBPortToAddrList(std::string& addresses, const std::string& port)
+    {
+        size_t base(0);
+        auto pos = addresses.find(',', base);
+        while (std::string::npos != pos)
+        {
+            addresses.insert(pos, ":" + port);
+            base = pos + 2 + port.size();
+            pos = addresses.find(',', base);
+        }
+        addresses.append(":" + port);
+    }
 }
 
 ConfigurationReader::ConfigurationReader(std::shared_ptr<Logger> logger):
@@ -306,24 +319,44 @@
         if (sourceForDatabaseConfiguration == dbHostEnvVariableName)
         {
             // NOTE: Redis cluster is not currently configurable via environment variables.
-            if (sentinelPortEnvVariableValue.empty())
+            std::string dbHostAddrs;
+            if (!dbHostEnvVariableValue.empty() && sentinelPortEnvVariableValue.empty() && dbClusterAddrListEnvVariableValue.empty())
             {
                 validateAndSetDbType("redis-standalone", databaseConfiguration, sourceForDatabaseConfiguration);
-                if (dbPortEnvVariableValue.empty())
-                    parseDatabaseServersConfigurationFromString(databaseConfiguration, dbHostEnvVariableValue, sourceForDatabaseConfiguration);
-                else
-                    parseDatabaseServersConfigurationFromString(databaseConfiguration, dbHostEnvVariableValue + ":" + dbPortEnvVariableValue, sourceForDatabaseConfiguration);
+                dbHostAddrs = dbHostEnvVariableValue;
+            }
+            else if (!dbHostEnvVariableValue.empty() && !sentinelPortEnvVariableValue.empty() && dbClusterAddrListEnvVariableValue.empty())
+            {
+                validateAndSetDbType("redis-sentinel", databaseConfiguration, sourceForDatabaseConfiguration);
+                dbHostAddrs = dbHostEnvVariableValue;
+            }
+            else if (sentinelPortEnvVariableValue.empty() && !dbClusterAddrListEnvVariableValue.empty())
+            {
+                validateAndSetDbType("sdl-standalone-cluster", databaseConfiguration, sourceForDatabaseConfiguration);
+                dbHostAddrs = dbClusterAddrListEnvVariableValue;
+            }
+            else if (!sentinelPortEnvVariableValue.empty() && !dbClusterAddrListEnvVariableValue.empty())
+            {
+                validateAndSetDbType("sdl-sentinel-cluster", databaseConfiguration, sourceForDatabaseConfiguration);
+                dbHostAddrs = dbClusterAddrListEnvVariableValue;
             }
             else
             {
-                if (dbClusterAddrListEnvVariableValue.empty())
-                    validateAndSetDbType("redis-sentinel", databaseConfiguration, sourceForDatabaseConfiguration);
-                else {
-                    validateAndSetDbType("sdl-cluster", databaseConfiguration, sourceForDatabaseConfiguration);
-                    parseDatabaseServersConfigurationFromString(databaseConfiguration,
-                                                                dbClusterAddrListEnvVariableValue,
-                                                                dbClusterAddrListEnvVariableName);
-                }
+                std::ostringstream os;
+                os << "Configuration error in " << sourceForDatabaseConfiguration << ": "
+                   << "Missing environment variable configuration!";
+                throw Exception(os.str());
+            }
+
+            if (!dbPortEnvVariableValue.empty())
+                appendDBPortToAddrList(dbHostAddrs, dbPortEnvVariableValue);
+            parseDatabaseServersConfigurationFromString(databaseConfiguration,
+                                                        dbHostAddrs,
+                                                        sourceForDatabaseConfiguration);
+            auto dbType = databaseConfiguration.getDbType();
+            if (DatabaseConfiguration::DbType::REDIS_SENTINEL == dbType ||
+                DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER == dbType)
+            {
                 databaseConfiguration.checkAndApplySentinelAddress(dbHostEnvVariableValue + ":" + sentinelPortEnvVariableValue);
                 databaseConfiguration.checkAndApplySentinelMasterName(sentinelMasterNameEnvVariableValue);
             }