def initialize_connector(self, ns): if not SonicDBConfig.isInit(): if multi_asic.is_multi_asic(): SonicDBConfig.load_sonic_global_db_config() else: SonicDBConfig.load_sonic_db_config() return SonicV2Connector(namespace=ns, use_unix_socket_path=True)
def test_multidb_ConfigDBConnector(): test_dir = os.path.dirname(os.path.abspath(__file__)) global_db_config = os.path.join(test_dir, 'redis_multi_db_ut_config', 'database_global.json') SonicDBConfig.load_sonic_global_db_config(global_db_config) config_db = ConfigDBConnector(use_unix_socket_path=True, namespace='asic1') assert config_db.namespace == 'asic1'
def main(): try: parser = argparse.ArgumentParser() parser.add_argument( '-o', dest='operation', metavar='operation (migrate, set_version, get_version)', type=str, required=False, choices=['migrate', 'set_version', 'get_version'], help='operation to perform [default: get_version]', default='get_version') parser.add_argument( '-s', dest='socket', metavar='unix socket', type=str, required=False, help='the unix socket that the desired database listens on', default=None) parser.add_argument( '-n', dest='namespace', metavar='asic namespace', type=str, required=False, help='The asic namespace whose DB instance we need to connect', default=None) args = parser.parse_args() operation = args.operation socket_path = args.socket namespace = args.namespace if args.namespace is not None: SonicDBConfig.load_sonic_global_db_config(namespace=args.namespace) else: SonicDBConfig.initialize() if socket_path: dbmgtr = DBMigrator(namespace, socket=socket_path) else: dbmgtr = DBMigrator(namespace) result = getattr(dbmgtr, operation)() if result: print(str(result)) except Exception as e: log.log_error('Caught exception: ' + str(e)) traceback.print_exc() print(str(e)) parser.print_help() sys.exit(1)
def __init__(self): self.yang_acl = None self.requested_session = None self.mirror_stage = None self.current_table = None self.tables_db_info = {} self.rules_db_info = {} self.rules_info = {} if multi_asic.is_multi_asic(): # Load global db config SonicDBConfig.load_sonic_global_db_config() else: SonicDBConfig.initialize() self.sessions_db_info = {} self.configdb = ConfigDBConnector() self.configdb.connect() self.statedb = SonicV2Connector(host="127.0.0.1") self.statedb.connect(self.statedb.STATE_DB) # For multi-npu architecture we will have both global and per front asic namespace. # Global namespace will be used for Control plane ACL which are via IPTables. # Per ASIC namespace will be used for Data and Everflow ACL's. # Global Configdb will have all ACL information for both Ctrl and Data/Evereflow ACL's # and will be used as souurce of truth for ACL modification to config DB which will be done to both Global DB and # front asic namespace self.per_npu_configdb = {} # State DB are used for to get mirror Session monitor port. # For multi-npu platforms each asic namespace can have different monitor port # dependinding on which route to session destination ip. So for multi-npu # platforms we get state db for all front asic namespace in addition to self.per_npu_statedb = {} # Getting all front asic namespace and correspding config and state DB connector namespaces = device_info.get_all_namespaces() for front_asic_namespaces in namespaces['front_ns']: self.per_npu_configdb[front_asic_namespaces] = ConfigDBConnector( use_unix_socket_path=True, namespace=front_asic_namespaces) self.per_npu_configdb[front_asic_namespaces].connect() self.per_npu_statedb[front_asic_namespaces] = SonicV2Connector( use_unix_socket_path=True, namespace=front_asic_namespaces) self.per_npu_statedb[front_asic_namespaces].connect( self.per_npu_statedb[front_asic_namespaces].STATE_DB) self.read_tables_info() self.read_rules_info() self.read_sessions_info() self.read_policers_info()
def connect(self, db, ns): try: if not SonicDBConfig.isInit(): if multi_asic.is_multi_asic(): SonicDBConfig.load_sonic_global_db_config() else: SonicDBConfig.load_sonic_db_config() self.conn = SonicV2Connector(namespace=ns, use_unix_socket_path=True) self.conn.connect(db) except Exception as e: verbose_print("RedisSource: Connection Failed\n" + str(e)) return False return True
def cli(ctx): """ Utility entry point. """ # Use the db object if given as input. db = None if ctx.obj is None else ctx.obj.cfgdb context = { "crm": Crm(db) } ctx.obj = context # Load the global config file database_global.json once. SonicDBConfig.load_sonic_global_db_config()
def cli(ctx): """ Utility entry point. """ # Use the db object if given as input. db = None if ctx.obj is None else ctx.obj.cfgdb # Note: SonicDBConfig may be already initialized in unit test, then skip if not SonicDBConfig.isInit(): if multi_asic.is_multi_asic(): # Load the global config file database_global.json once. SonicDBConfig.load_sonic_global_db_config() else: SonicDBConfig.initialize() context = {"crm": Crm(db)} ctx.obj = context
def fetch(self, req_orig): req = copy.deepcopy(req_orig) sep = "|" if req.db: sep = SonicDBConfig.getSeparator(req.db) key = req.table + sep + req.key_pattern if key in self.__key_cache: verbose_print("Cache Hit for Key: {}".format(key)) return self.__fetch_from_cache(key, req) else: verbose_print("Cache Miss for Key: {}".format(key)) req, fv_requested, ret_just_keys = self.__mutate_request(req) ret = self.m_engine.fetch(req) if ret["error"]: return ret self.__fill_cache(ret) return self.__mutate_response(ret, fv_requested, ret_just_keys)
def __static_checks(self): if not self.db and not self.file: return EXCEP_DICT["NO_SRC"] if self.db and self.file: return EXCEP_DICT["SRC_VAGUE"] if not self.db: try: with open(self.file) as f: json.load(f) except Exception as e: return EXCEP_DICT["FILE_R_EXEP"] + str(e) if not self.file and self.db not in SonicDBConfig.getDbList(): return EXCEP_DICT["INV_DB"] if not self.table: return EXCEP_DICT["NO_TABLE"] if not isinstance(self.return_fields, list): return EXCEP_DICT["BAD_FORMAT_RE_FIELDS"] if not self.just_keys and self.return_fields: return EXCEP_DICT["JUST_KEYS_COMPAT"] if self.field and not self.value: return EXCEP_DICT["NO_VALUE"] if self.ns != DEFAULT_NAMESPACE and self.ns not in multi_asic.get_namespace_list( ): return EXCEP_DICT["INV_NS"] + " Choose From {}".format( multi_asic.get_namespace_list()) verbose_print("MatchRequest Checks Passed") return ""
def prepare(request): SonicDBConfig.initialize(existing_file)
def get_separator(self, db): return SonicDBConfig.getSeparator("CONFIG_DB")
def get_sep(self, db): return SonicDBConfig.getSeparator(db)
from dump.helper import create_template_dict from dump.match_infra import MatchRequest from swsscommon.swsscommon import SonicDBConfig from dump.match_helper import fetch_acl_counter_oid from .executor import Executor CFG_DB_SEPARATOR = SonicDBConfig.getSeparator("CONFIG_DB") ASIC_DB_SEPARATOR = SonicDBConfig.getSeparator("ASIC_DB") class Acl_Rule(Executor): """ Debug Dump Plugin for ACL Rule Module """ ARG_NAME = "acl_rule_name" def __init__(self, match_engine=None): super().__init__(match_engine) def get_all_args(self, ns=""): req = MatchRequest(db="CONFIG_DB", table="ACL_RULE", key_pattern="*", ns=ns) ret = self.match_engine.fetch(req) acl_rules = ret["keys"] return [key.split(CFG_DB_SEPARATOR, 1)[-1] for key in acl_rules] def execute(self, params): self.ret_temp = create_template_dict(dbs=["CONFIG_DB", "ASIC_DB"]) try: