def boolean_get(self, key, check_args=True, args_optional=False, default=False): ''' Get a key, and check the argument list and global configuration, in that order, for a corresponding value, which should be type boolean. If one is not found, return default. ''' arg_key = key.lower().replace("-", "_") no_arg_key = "no_%s" % arg_key config_key = key.lower().replace("_", "-") in_args = arg_key in self.args if args_optional else check_args if in_args: if no_arg_key not in self.args and arg_key not in self.args: raise ReadError("%s and %s not defined in args for boolean argument '%s'" % (arg_key, no_arg_key, key)) if no_arg_key in self.args and arg_key not in self.args: raise ReadError("%s not defined in args for boolean argument '%s'" % (arg_key, key)) if arg_key in self.args and no_arg_key not in self.args: raise ReadError("%s not defined in args for boolean argument '%s'" % (no_arg_key, key)) if in_args and util.boolean_get(self.args[arg_key]) == True: return True elif in_args and util.boolean_get(self.args[no_arg_key]) == False: return False elif self.config and config_key in self.config and self.config[config_key] is not None: return util.boolean_get(self.config[config_key]) else: return default
def read(log, log_level, conf=None, config=None): ''' Parse a configuration, file or dictionary, and return an overlay object. ''' # If specified, read the overlay configuration file. if conf: config = util.config(conf) elif config: config = copy.deepcopy(config) else: raise NoOverlayConfigError() # Get the overlay configuration. section = config["overlay"] # Fetch just enough configuration to start an overlay logger. name = util.name_get(section["name"]) lg = logger.create(log, log_level, "l3overlay", name) lg.start() # Global overlay configuration. enabled = util.boolean_get(section["enabled"]) if "enabled" in section else True asn = util.integer_get(section["asn"], minval=0, maxval=65535) linknet_pool = util.ip_network_get(section["linknet-pool"]) fwbuilder_script_file = section["fwbuilder-script"] if "fwbuilder-script" in section else None # Generate the list of nodes, sorted numerically. nodes = [] for key, value in section.items(): if key.startswith("node-"): node = util.list_get(value, length=2, pattern="\\s") nodes.append((util.name_get(node[0]), util.ip_address_get(node[1]))) if not nodes: raise NoNodeListError(name) # Get the node object for this node from the list of nodes. this_node = next((n for n in nodes if n[0] == util.name_get(section["this-node"])), None) if not this_node: raise MissingThisNodeError(name, util.name_get(section["this-node"])) # Static interfaces. interfaces = [] for s, c in config.items(): if s.startswith("static"): interfaces.append(interface.read(lg, s, c)) elif s == "DEFAULT" or s == "overlay": continue else: raise UnsupportedSectionTypeError(name, s) # Return overlay object. return Overlay(lg, name, enabled, asn, linknet_pool, fwbuilder_script_file, nodes, this_node, interfaces)
def read(logger, name, config): ''' Create a static bgp from the given configuration object. ''' neighbor = util.ip_address_get(config["neighbor"]) local = util.ip_address_get(config["local"]) if "local" in config else None local_asn = util.integer_get(config["local-asn"], minval=0, maxval=65535) if "local-asn" in config else None neighbor_asn = util.integer_get(config["neighbor-asn"], minval=0, maxval=65535) if "neighbor-asn" in config else None bfd = util.boolean_get(config["bfd"]) if "bfd" in config else False ttl_security = util.boolean_get(config["ttl-security"]) if "ttl-security" in config else False description = config["description"] if "description" in config else None import_prefixes = [util.bird_prefix_get(v) for k, v in config.items() if k.startswith("import-prefix")] return BGP(logger, name, neighbor, local, local_asn, neighbor_asn, bfd, ttl_security, import_prefixes)