Beispiel #1
0
 def __init__(self,
              networking=None,
              config=None,
              workingdir=None,
              domain_id=None,
              loglevel="all",
              logname=None):
     self.networking = networking
     self.core = networking.core
     self.stats = networking.core.stats
     self.logger = logger.get_logger(key="data_handler",
                                     level=loglevel,
                                     logname=logname)
     self.domain_id = domain_id
     self.domain_id_str = bbclib.convert_id_to_string(domain_id)
     self.config = config
     self.working_dir = workingdir
     self.storage_root = os.path.join(self.working_dir, self.domain_id_str)
     if not os.path.exists(self.storage_root):
         os.makedirs(self.storage_root, exist_ok=True)
     self.use_external_storage = self._storage_setup()
     self.replication_strategy = DataHandler.REPLICATION_ALL
     self.upgraded_from = DB_VERSION
     self.db_adaptors = list()
     self._db_setup()
Beispiel #2
0
    def create_domain(self, domain_id=ZEROS, config=None):
        """Create domain and register user in the domain

        Args:
            domain_id (bytes): target domain_id to create
            config (dict): configuration for the domain
        Returns:
            bool:
        """
        if domain_id in self.domains:
            return False

        conf = self.config.get_domain_config(domain_id, create_if_new=True)
        if config is not None:
            conf.update(config)
        if 'node_id' not in conf or conf['node_id'] == "":
            node_id = bbclib.get_random_id()
            conf['node_id'] = bbclib.convert_id_to_string(node_id)
            self.config.update_config()
        else:
            node_id = bbclib.convert_idstring_to_bytes(conf.get('node_id'))

        self.domains[domain_id] = dict()
        self.domains[domain_id]['node_id'] = node_id
        self.domains[domain_id]['name'] = node_id.hex()[:4]
        self.domains[domain_id]['neighbor'] = NeighborInfo(network=self, domain_id=domain_id, node_id=node_id,
                                                           my_info=self._get_my_nodeinfo(node_id))
        self.domains[domain_id]['topology'] = TopologyManagerBase(network=self, domain_id=domain_id, node_id=node_id,
                                                                  logname=self.logname, loglevel=self.loglevel)
        self.domains[domain_id]['user'] = UserMessageRouting(self, domain_id, logname=self.logname,
                                                             loglevel=self.loglevel)
        self.get_domain_keypair(domain_id)

        workingdir = self.config.get_config()['workingdir']
        if domain_id == ZEROS:
            self.domains[domain_id]['data'] = DataHandlerDomain0(self, domain_id=domain_id, logname=self.logname,
                                                                 loglevel=self.loglevel)
            self.domain0manager = Domain0Manager(self, node_id=node_id, logname=self.logname, loglevel=self.loglevel)
        else:
            self.domains[domain_id]['data'] = DataHandler(self, config=conf, workingdir=workingdir,
                                                          domain_id=domain_id, logname=self.logname,
                                                          loglevel=self.loglevel)

        self.domains[domain_id]['repair'] = RepairManager(self, domain_id, workingdir=workingdir,
                                                          logname=self.logname, loglevel=self.loglevel)

        if self.domain0manager is not None:
            self.domain0manager.update_domain_belong_to()
            for dm in self.domains.keys():
                if dm != ZEROS:
                    self.domains[dm]['neighbor'].my_info.update(domain0=True)
            self.domains[domain_id]['topology'].update_refresh_timer_entry(1)
        self.stats.update_stats_increment("network", "num_domains", 1)
        self.logger.info("Domain %s is created" % (domain_id.hex()))
        return True
Beispiel #3
0
    def get_domain_config(self, domain_id, create_if_new=False):
        """Return the part of specified domain_id in the config dictionary"""
        domain_id_str = bbclib.convert_id_to_string(domain_id)
        conf = self.read_config()
        if 'domains' in conf and domain_id_str in conf['domains']:
            self.config['domains'][domain_id_str] = conf['domains'][domain_id_str]

        if create_if_new and domain_id_str not in self.config['domains']:
            self.config['domains'][domain_id_str] = copy.deepcopy(self.config['domain_default'])
        if domain_id_str in self.config['domains']:
            return self.config['domains'][domain_id_str]
        return None
Beispiel #4
0
 def save_all_static_node_list(self):
     """Save all static nodes in the config file"""
     self.logger.info("Saving the neighbor list")
     for domain_id in self.domains.keys():
         conf = self.config.get_domain_config(domain_id)
         conf['static_node'] = dict()
         for node_id, nodeinfo in self.domains[domain_id]['neighbor'].nodeinfo_list.items():
             if nodeinfo.is_static:
                 nid = bbclib.convert_id_to_string(node_id)
                 info = _convert_to_string([nodeinfo.ipv4, nodeinfo.ipv6, nodeinfo.port])
                 conf['static_node'][nid] = info
     self.config.update_config()
     self.logger.info("Done...")
Beispiel #5
0
 def remove_domain_config(self, domain_id):
     """Remove the part of specified domain_id in the config dictionary"""
     domain_id_str = bbclib.convert_id_to_string(domain_id)
     if domain_id_str in self.config['domains']:
         del self.config['domains'][domain_id_str]
         self.update_config()