Read list type of environment variables
[ric-plt/sdlpy.git] / ricsdl-package / ricsdl / backend / redis.py
index 1c549db..822afcf 100755 (executable)
@@ -1,5 +1,5 @@
 # Copyright (c) 2019 AT&T Intellectual Property.
-# Copyright (c) 2018-2019 Nokia.
+# Copyright (c) 2018-2022 Nokia.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ import redis
 from redis import Redis
 from redis.sentinel import Sentinel
 from redis.lock import Lock
-from redis._compat import nativestr
+from redis.utils import str_if_bytes
 from redis import exceptions as redis_exceptions
 from ricsdl.configuration import _Configuration
 from ricsdl.exceptions import (
@@ -69,7 +69,7 @@ class PubSub(redis.client.PubSub):
 
         Adapted from: https://github.com/andymccurdy/redis-py/blob/master/redis/client.py
         """
-        message_type = nativestr(response[0])
+        message_type = str_if_bytes(response[0])
         if message_type == 'pmessage':
             message = {
                 'type': message_type,
@@ -370,29 +370,26 @@ class RedisBackend(DbBackendAbc):
     def __create_redis_clients(self, config):
         clients = list()
         cfg_params = config.get_params()
-        if cfg_params.db_cluster_addr_list is None:
-            clients.append(self.__create_legacy_redis_client(cfg_params))
-        else:
-            for addr in cfg_params.db_cluster_addr_list.split(","):
-                client = self.__create_redis_client(cfg_params, addr)
-                clients.append(client)
-        return clients
+        for i, addr in enumerate(cfg_params.db_cluster_addrs):
+            port = cfg_params.db_ports[i] if i < len(cfg_params.db_ports) else ""
+            sport = cfg_params.db_sentinel_ports[i] if i < len(cfg_params.db_sentinel_ports) else ""
+            name = cfg_params.db_sentinel_master_names[i] if i < len(cfg_params.db_sentinel_master_names) else ""
 
-    def __create_legacy_redis_client(self, cfg_params):
-        return self.__create_redis_client(cfg_params, cfg_params.db_host)
+            client = self.__create_redis_client(addr, port, sport, name)
+            clients.append(client)
+        return clients
 
-    def __create_redis_client(self, cfg_params, addr):
+    def __create_redis_client(self, addr, port, sentinel_port, master_name):
         new_sentinel = None
         new_redis = None
-        if cfg_params.db_sentinel_port is None:
-            new_redis = Redis(host=addr, port=cfg_params.db_port, db=0, max_connections=20)
+        if len(sentinel_port) == 0:
+            new_redis = Redis(host=addr, port=port, db=0, max_connections=20)
         else:
-            sentinel_node = (addr, cfg_params.db_sentinel_port)
-            master_name = cfg_params.db_sentinel_master_name
+            sentinel_node = (addr, sentinel_port)
             new_sentinel = Sentinel([sentinel_node])
             new_redis = new_sentinel.master_for(master_name)
 
-        new_redis.set_response_callback('SETIE', lambda r: r and nativestr(r) == 'OK' or False)
+        new_redis.set_response_callback('SETIE', lambda r: r and str_if_bytes(r) == 'OK' or False)
         new_redis.set_response_callback('DELIE', lambda r: r and int(r) == 1 or False)
 
         redis_pubsub = PubSub(self.event_separator, new_redis.connection_pool, ignore_subscribe_messages=True)