def getMayGroups(self): # 将组合分为两组,mayFill是可能将所有的虚拟机填下去,cantFill是不可能将虚拟机填下去(需要去除部分虚拟机) self.mayGroups = HMap() # 分别获取单独使用某一种物理机装 maxValSingle = 0 # 单个物理机最大的价值 numsSingle = {} # 单个物理机的数量,为后面获取可能的组合提供参考 for name, compuSpec in self.compusSpec.items(): dp = DP(compuSpec, self.ecssSpec, self.ecssNum) compuEcssInfo = dp.getCompuEcssInfo() val = self.calcuVal({name: compuEcssInfo}, True) maxValSingle = max(maxValSingle, val) numsSingle[name] = len(compuEcssInfo) mayGroup = {} # 第一个组合,全部为0 for name in self.compusSpec: mayGroup[name] = 0 compusName = self.compusSpec.keys() while self.updateMayGroup(mayGroup, numsSingle, compusName): val, canFill = self.calcuVal(mayGroup) # 如果组合的价值大于单个物理机最好的放置 if canFill and val >= maxValSingle: self.mayGroups[deepcopy(mayGroup)] = val self.mayGroups.sort()
def _dp_parser_v1(conf, config_file, logname): logger = get_logger(logname) config_path = _dp_config_path(config_file) # TODO: warn when the configuration contains meaningless elements # they are probably typos if 'dp_id' not in conf: logger.error('dp_id not configured in file %s', config_file) dp_id = conf['dp_id'] dp = DP(dp_id, conf) interfaces_conf = conf.pop('interfaces', {}) vlans_conf = conf.pop('vlans', {}) acls_conf = conf.pop('acls', {}) logger.info(str(dp)) vlans = {} for vid, vlan_conf in vlans_conf.iteritems(): vlans[vid] = VLAN(vid, dp_id, vlan_conf) for port_num, port_conf in interfaces_conf.iteritems(): dp.add_port(port_parser(dp_id, port_num, port_conf, vlans)) for acl_num, acl_conf in acls_conf.iteritems(): dp.add_acl(acl_num, acl_conf) for vlan in vlans.itervalues(): dp.add_vlan(vlan) try: dp.sanity_check() except AssertionError as err: logger.exception('Error in config file: %s', err) return None return ({config_path: config_file_hash(config_path)}, [dp])
def _dp_parser_v1(conf, config_file, logname): logger = get_logger(logname) # TODO: warn when the configuration contains meaningless elements # they are probably typos if 'dp_id' not in conf: logger.error('dp_id not configured in file {0}'.format(config_file)) dp_id = conf['dp_id'] dp = DP(dp_id, conf) interfaces_conf = conf.pop('interfaces', {}) vlans_conf = conf.pop('vlans', {}) acls_conf = conf.pop('acls', {}) logger.info(str(dp)) vlans = {} port = {} for vid, vlan_conf in vlans_conf.iteritems(): vlans[vid] = VLAN(vid, dp_id, vlan_conf) for port_num, port_conf in interfaces_conf.iteritems(): dp.add_port(port_parser(dp_id, port_num, port_conf, vlans)) for acl_num, acl_conf in acls_conf.iteritems(): dp.add_acl(acl_num, acl_conf) for vlan in vlans.itervalues(): dp.add_vlan(vlan) try: dp.sanity_check() except AssertionError as err: logger.exception("Error in config file: {0}".format(err)) return None return [dp]
def _dp_parser_v2(logger, acls_conf, dps_conf, routers_conf, vlans_conf): dps = [] vid_dp = {} for identifier, dp_conf in list(dps_conf.items()): try: dp = DP(identifier, dp_conf) dp.sanity_check() dp_id = dp.dp_id vlans = {} for vid, vlan_conf in list(vlans_conf.items()): vlans[vid] = VLAN(vid, dp_id, vlan_conf) acls = [] for acl_ident, acl_conf in list(acls_conf.items()): acls.append((acl_ident, ACL(acl_ident, acl_conf))) routers = [] for router_ident, router_conf in list(routers_conf.items()): routers.append((router_ident, Router(router_ident, router_conf))) if routers: assert len(routers) == 1, 'only one router supported' router_ident, router = routers[0] assert set(router.vlans) == set( vlans.keys()), 'only global routing supported' dp.add_router(router_ident, router) ports_conf = dp_conf.pop('interfaces', {}) ports = {} # as users can config port vlan by using vlan name, we store vid in # Port instance instead of vlan name for data consistency for port_num, port_conf in list(ports_conf.items()): port = port_parser(dp_id, port_num, port_conf, vlans) ports[port_num] = port if port.native_vlan is not None: vlan = vlans[port.native_vlan] port.native_vlan = vlan _dp_add_vlan(vid_dp, dp, vlan) if port.tagged_vlans is not None: tagged_vlans = [] for v_identifier in port.tagged_vlans: vlan = vlans[v_identifier] tagged_vlans.append(vlan) _dp_add_vlan(vid_dp, dp, vlan) port.tagged_vlans = tagged_vlans except AssertionError as err: logger.exception('Error in config file: %s', err) return None for port in list(ports.values()): dp.add_port(port) for acl_ident, acl in acls: dp.add_acl(acl_ident, acl) dps.append(dp) return dps
def _dp_parser_v2(conf, config_file, logname): logger = get_logger(logname) config_path = _dp_config_path(config_file) config_hashes = {} dps_conf = {} vlans_conf = {} acls_conf = {} if not _dp_include(config_hashes, None, config_path, dps_conf, vlans_conf, acls_conf, logname): logger.critical('error found while loading config file: %s', config_path) return None if not dps_conf: logger.critical('dps not configured in file: %s', config_path) return None dps = [] vid_dp = {} for identifier, dp_conf in dps_conf.iteritems(): ports_conf = dp_conf.pop('interfaces', {}) dp = DP(identifier, dp_conf) dp.sanity_check() dp_id = dp.dp_id vlans = {} ports = {} for vid, vlan_conf in vlans_conf.iteritems(): vlans[vid] = VLAN(vid, dp_id, vlan_conf) try: for port_num, port_conf in ports_conf.iteritems(): port = port_parser(dp_id, port_num, port_conf, vlans) ports[port_num] = port if port.native_vlan is not None: _dp_add_vlan(vid_dp, dp, vlans[port.native_vlan], logname) if port.tagged_vlans is not None: for vid in port.tagged_vlans: _dp_add_vlan(vid_dp, dp, vlans[vid], logname) except AssertionError as err: logger.exception('Error in config file: %s', err) return None for port in ports.itervalues(): dp.add_port(port) for a_identifier, acl_conf in acls_conf.iteritems(): # TODO: turn this into an object dp.add_acl(a_identifier, acl_conf) # Once the the datapath (dp) object is created containing everything from the yaml file it then appends to the datapaths object dps.append(dp) # returns an array of data paths return (config_hashes, dps)
def __init__(self, *args, **kwargs): super(Dashboard, self).__init__(*args, **kwargs) self.config_file = os.getenv('GAUGE_CONFIG', '/etc/ryu/faucet/gauge.conf') self.logger = self.create_logger() self.dps = {} with open(self.config_file, 'r') as config_file: for dp_conf_file in config_file: # config_file should be a list of faucet config filenames # separated by linebreaks dp = DP.parser(dp_conf_file.strip(), self.logname) try: dp.sanity_check() self.valve = valve_factory(dp) except AssertionError: self.logger.exception( "Error in config file {0}".format(dp_conf_file)) else: self.dps[dp.dp_id] = dp # Create dpset object for querying Ryu's DPSet application self.dpset = kwargs['dpset'] # dict of polling threads: # polling threads are indexed by dp_id and then by name # eg: self.pollers[0x1]['port_stats'] self.pollers = {} # dict of async event handlers self.handlers = {} self.redis_flush_thread = hub.spawn(self.redis_flush_request)
def parse_config(self, config_file, log_name): new_dp = DP.parser(config_file, log_name) if new_dp: try: new_dp.sanity_check() return new_dp except AssertionError: self.logger.exception("Error in config file:") return None
def __init__(self, *args, **kwargs): super(Gauge, self).__init__(*args, **kwargs) self.config_file = os.getenv( 'GAUGE_CONFIG', '/etc/sdx/gauge.conf') self.config_dir = os.path.dirname(self.config_file) self.exc_logfile = os.getenv( 'GAUGE_EXCEPTION_LOG', '/var/log/sdx/gauge_exception.log') self.logfile = os.getenv('GAUGE_LOG', '/var/log/sdx/gauge.log') # Setup logging self.logger = logging.getLogger(__name__) logger_handler = TimedRotatingFileHandler( self.logfile, when='midnight') log_fmt = '%(asctime)s %(name)-6s %(levelname)-8s %(message)s' date_fmt = '%b %d %H:%M:%S' default_formatter = logging.Formatter(log_fmt, date_fmt) logger_handler.setFormatter(default_formatter) self.logger.addHandler(logger_handler) self.logger.propagate = 0 # Set up separate logging for exceptions exc_logger = logging.getLogger(self.exc_logname) exc_logger_handler = logging.FileHandler(self.exc_logfile) exc_logger_handler.setFormatter( logging.Formatter(log_fmt, date_fmt)) exc_logger.addHandler(exc_logger_handler) exc_logger.propagate = 1 exc_logger.setLevel(logging.ERROR) # Prevent printing of the 'Starting new HTTP requests' messages logging.getLogger("urllib3").setLevel(logging.WARNING) self.dps = {} with open(self.config_file, 'r') as config_file: for dp_conf_file in config_file: # config_file should be a list of faucet config filenames # separated by linebreaks dp = DP.parser(os.path.join(self.config_dir, dp_conf_file.strip()), self.logname) try: dp.sanity_check() except AssertionError: self.logger.exception( "Error in config file {0}".format(dp_conf_file)) else: self.dps[dp.dp_id] = dp # Create dpset object for querying Ryu's DPSet application self.dpset = kwargs['dpset'] # dict of polling threads: # polling threads are indexed by dp_id and then by name # eg: self.pollers[0x1]['port_stats'] self.pollers = {} # dict of async event handlers self.handlers = {}
def __init__(self, *args, **kwargs): super(Faucet, self).__init__(*args, **kwargs) # There doesnt seem to be a sensible method of getting command line # options into ryu apps. Instead I am using the environment variable # FAUCET_CONFIG to allow this to be set, if it is not set it will # default to valve.yaml self.config_file = os.getenv( 'FAUCET_CONFIG', '/etc/opt/faucet/valve.yaml') self.logfile = os.getenv( 'FAUCET_LOG', '/var/log/faucet/faucet.log') self.exc_logfile = os.getenv( 'FAUCET_EXCEPTION_LOG', '/var/log/faucet/faucet_exception.log') # Set the signal handler for reloading config file signal.signal(signal.SIGHUP, self.signal_handler) # Create dpset object for querying Ryu's DPSet application self.dpset = kwargs['dpset'] # Setup logging self.logger = logging.getLogger(self.logname) logger_handler = TimedRotatingFileHandler( self.logfile, when='midnight') log_fmt = '%(asctime)s %(name)-6s %(levelname)-8s %(message)s' logger_handler.setFormatter( logging.Formatter(log_fmt, '%b %d %H:%M:%S')) self.logger.addHandler(logger_handler) self.logger.propagate = 0 self.logger.setLevel(logging.DEBUG) # Set up separate logging for exceptions exc_logger = logging.getLogger(self.exc_logname) exc_logger_handler = logging.FileHandler(self.exc_logfile) exc_logger_handler.setFormatter( logging.Formatter(log_fmt, '%b %d %H:%M:%S')) exc_logger.addHandler(exc_logger_handler) exc_logger.propagate = 1 exc_logger.setLevel(logging.CRITICAL) # Parse config file dp = DP.parser(self.config_file, self.logname) if dp: try: dp.sanity_check() except AssertionError: self.logger.exception("Error in config file:") # Load Valve self.valve = valve_factory(dp) if self.valve is None: self.logger.error("Hardware type not supported")
def _dp_parser_v2(conf, config_file, logname): logger = get_logger(logname) if 'dps' not in conf: logger.error("dps not configured in file: {0}".format(config_file)) return None vlans_conf = conf.pop('vlans', {}) acls_conf = conf.pop('acls', {}) dps = [] vid_dp = {} for identifier, dp_conf in conf['dps'].iteritems(): ports_conf = dp_conf.pop('interfaces', {}) dp = DP(identifier, dp_conf) dp.sanity_check() dp_id = dp.dp_id vlans = {} ports = {} for vid, vlan_conf in vlans_conf.iteritems(): vlans[vid] = VLAN(vid, dp_id, vlan_conf) try: for port_num, port_conf in ports_conf.iteritems(): port = port_parser(dp_id, port_num, port_conf, vlans) ports[port_num] = port if port.native_vlan is not None: _dp_add_vlan(vid_dp, dp, vlans[port.native_vlan], logname) if port.tagged_vlans is not None: for vid in port.tagged_vlans: _dp_add_vlan(vid_dp, dp, vlans[vid], logname) except AssertionError as err: logger.exception("Error in config file: {0}".format(err)) return None for port in ports.itervalues(): # now that all ports are created, handle mirroring rewriting if port.mirror is not None: port.mirror = ports[port.mirror].number dp.add_port(port) for a_identifier, acl_conf in acls_conf.iteritems(): # TODO: turn this into an object dp.add_acl(a_identifier, acl_conf) dps.append(dp) return dps
def _dp_parser_v2(conf, config_file, logname): logger = get_logger(logname) config_path = _dp_config_path(config_file) config_hashes = {} dps_conf = {} vlans_conf = {} acls_conf = {} if not _dp_include(config_hashes, None, config_path, dps_conf, vlans_conf, acls_conf, logname): logger.critical('error found while loading config file: %s', config_path) return None if not dps_conf: logger.critical('dps not configured in file: %s', config_path) return None dps = [] vid_dp = {} for identifier, dp_conf in dps_conf.iteritems(): ports_conf = dp_conf.pop('interfaces', {}) dp = DP(identifier, dp_conf) dp.sanity_check() dp_id = dp.dp_id vlans = {} ports = {} for vid, vlan_conf in vlans_conf.iteritems(): vlans[vid] = VLAN(vid, dp_id, vlan_conf) try: for port_num, port_conf in ports_conf.iteritems(): port = port_parser(dp_id, port_num, port_conf, vlans) ports[port_num] = port if port.native_vlan is not None: _dp_add_vlan(vid_dp, dp, vlans[port.native_vlan], logname) if port.tagged_vlans is not None: for vid in port.tagged_vlans: _dp_add_vlan(vid_dp, dp, vlans[vid], logname) except AssertionError as err: logger.exception('Error in config file: %s', err) return None for port in ports.itervalues(): dp.add_port(port) for a_identifier, acl_conf in acls_conf.iteritems(): # TODO: turn this into an object dp.add_acl(a_identifier, acl_conf) dps.append(dp) return (config_hashes, dps)
def test_reload_drop(self): """Test that after a config reload packets with invalid vlan tags are dropped. """ match = { 'in_port': 3, 'vlan_vid': 11|ofp.OFPVID_PRESENT} new_dp = DP.parser("tests/config/valve-test-reload.yaml") ofmsgs = self.valve.reload_config(new_dp) self.table.apply_ofmsgs(ofmsgs) self.assertFalse( self.table.is_output(match), msg='Output action when packet should be dropped after reload')
def test_reload_unknown_eth_dst_rule(self): """Test that packets with unkown eth dst addrs get flooded correctly after a config reload. They must be output to each currently running port on the associated vlan, with the correct vlan tagging.""" matches = [ {'in_port': 4}, {'in_port': 3, 'vlan_vid': 10|ofp.OFPVID_PRESENT}, { 'in_port': 3, 'vlan_vid': 10|ofp.OFPVID_PRESENT, 'eth_src': "00:00:00:00:00:01"}, {'in_port': 2, 'vlan_vid': 11|ofp.OFPVID_PRESENT}, { 'in_port': 2, 'vlan_vid': 11|ofp.OFPVID_PRESENT, 'eth_dst': "00:00:00:00:00:01"}, {'in_port': 2, 'vlan_vid': 10|ofp.OFPVID_PRESENT}, {'in_port': 1, 'eth_src': "00:00:00:00:00:03"}, {'in_port': 1}] dp = DP.parser("tests/config/valve-test-reload.yaml") ofmsgs = self.valve.reload_config(dp) self.table.apply_ofmsgs(ofmsgs) for match in matches: in_port = match['in_port'] if 'vlan_vid' in match: vlan = dp.vlans[match['vlan_vid'] & ~ofp.OFPVID_PRESENT] else: # if a tagged port arrives on an untagged interface, we can # ignore the label vlan = dp.get_native_vlan(in_port) # the ports that have not yet had packets output to them remaining_ports = set(range(1, 7)) for p in vlan.get_ports(): remaining_ports.discard(p.number) if p.number != in_port and p.running(): if vlan.port_is_tagged(p.number): vid = vlan.vid|ofp.OFPVID_PRESENT else: vid = 0 self.assertTrue( self.table.is_output(match, port=p.number, vlan=vid), msg="packet ({0}) not output correctly to port {1} on " "vlan {2} when flooding after reload".format(match, p.number, vid)) for p in remaining_ports: self.assertFalse( self.table.is_output(match, p), msg="packet output to port not on vlan after reload")
def setUp(self): dp = DP.parser("tests/config/valve-test.yaml") self.valve = OVSStatelessValve(dp) self.table = FakeOFTable() self.table.apply_ofmsgs(self.valve.datapath_connect(1, [1,2,3,4,5,6])) rcv_packet_ofmsgs = self.valve.rcv_packet( dp_id=1, in_port=1, vlan_vid=10, eth_src="00:00:00:00:00:01", eth_dst="00:00:00:00:00:02") self.table.apply_ofmsgs(rcv_packet_ofmsgs) rcv_packet_ofmsgs = self.valve.rcv_packet( dp_id=1, in_port=3, vlan_vid=11, eth_src="00:00:00:00:00:03", eth_dst="00:00:00:00:00:04") self.table.apply_ofmsgs(rcv_packet_ofmsgs)
def signal_handler(self, sigid, frame): if sigid == signal.SIGHUP: self.logger.info("Caught SIGHUP, reloading configuration") new_config_file = os.getenv('FAUCET_CONFIG', self.config_file) new_dp = DP.parser(new_config_file, self.logname) if new_dp: try: new_dp.sanity_check() except AssertionError: self.logger.exception("Error in config file:") flowmods = self.valve.reload_config(new_dp) ryudp = self.dpset.get(new_dp.dp_id) for f in flowmods: # OpenFlow Message objects in ryu require a ryu datapath # object f.datapath = ryudp ryudp.send_msg(f)
def test_reload_port_disable(self): """Test that when a port is disabled in a reload packets are not output to it. """ matches = [ {'in_port': 4}, { 'in_port': 2, 'vlan_vid': 11|ofp.OFPVID_PRESENT, 'eth_dst': "00:00:00:00:00:05"}] rcv_packet_ofmsgs = self.valve.rcv_packet( dp_id=1, in_port=5, vlan_vid=11, eth_src="00:00:00:00:00:05", eth_dst="00:00:00:00:00:06") self.table.apply_ofmsgs(rcv_packet_ofmsgs) dp = DP.parser("tests/config/valve-test-reload.yaml") ofmsgs = self.valve.reload_config(dp) self.table.apply_ofmsgs(ofmsgs) for match in matches: self.assertFalse( self.table.is_output(match, port=5), msg="packet output to disabled port")
break else: env.reset() print(ix) sleep(1) env.close() return sim_store if __name__ == "__main__": env = FrozenLakeEnv(is_slippery=True) env.num_actions = env.nA env.num_states = env.nS o = env.reset() dp = DP(env) for _ in range(100): dp.policy_eval() dp.policy_imp() dp.q_values = np.array([dp.q_values[s] for s in dp.q_values]) # exit() # plt.figure(figsize=(8, 8), num="dp_sv") # sns.heatmap(dp.state_values.reshape(4, 4), # cmap="Spectral", annot=True, cbar=False) # plt.figure(figsize=(8, 8), num="dp_q") # sns.heatmap(dp.q_values.reshape(16, 4), # cmap="Spectral", annot=True, cbar=False) # plt.show() # plt.show() birl = Birl(env.num_states) print("Running Sim")
def parse_config(self): with open('valve.yaml', 'r') as stream: self.conf = yaml.load(stream) # Set defaults self.conf.setdefault('default', {}) defaults = self.conf['default'] defaults.setdefault('table_miss', True) # Shall we install a table-miss rule? defaults.setdefault( 'smart_broadcast', True) # Shall we install a broadcast/multicast rules? defaults.setdefault('lowest_priority', 0) # Table-miss priority defaults.setdefault('priority_offset', 0) # How much to offset default priority by defaults.setdefault('low_priority', defaults['priority_offset'] + 9000) defaults.setdefault('high_priority', defaults['low_priority'] + 1) defaults.setdefault('highest_priority', defaults['high_priority'] + 98) defaults.setdefault('cookie', 3134985664) # Identification cookie value defaults.setdefault('stats_enable', False) # Shall we enable stats? defaults.setdefault('stats_interval', 30) # Stats reporting interval # Convert all acls to ACL objects self.fix_acls(self.conf) for dpid, dpconfig in self.conf.items(): if dpid in ('all', 'default', 'acls'): continue conf_all = [copy.deepcopy(self.conf['all']) ] if 'all' in self.conf else [{}] conf_def = copy.deepcopy( self.conf['default']) if 'default' in self.conf else {} conf_acls = copy.deepcopy( self.conf['acls']) if 'acls' in self.conf else {} # Merge dpconfig['all'] into conf_all if 'all' in dpconfig: conf_all.append(dpconfig['all']) del dpconfig['all'] # Merge dpconfig['default'] into conf_def if 'default' in dpconfig: for key, value in dpconfig['default'].items(): conf_def[key] = value if key == 'priority_offset': # apply priority offset conf_def['low_priority'] = value + 9000 conf_def[ 'high_priority'] = conf_def['low_priority'] + 1 conf_def['highest_priority'] = conf_def[ 'high_priority'] + 98 del dpconfig['default'] # Merge dpconfig['acls'] into conf_acls if 'acls' in dpconfig: for ip, acls in dpconfig['acls'].items(): conf_acls.setdefault(ip, []) conf_acls[ip].extend(x for x in acls if x not in conf_acls[ip]) del dpconfig['acls'] # Add datapath newdp = DP(dpid, dpconfig, conf_all, conf_def, conf_acls) self.dps[dpid] = newdp
def setUp(self): self.dp = DP.parser('config/testconfig.yaml')
def policy_walk(self): random_rewards = self.sample_random_rewards(self.num_states, 1, 1) env = FrozenLakeEnv(is_slippery=True, rewards=random_rewards) env.num_actions = env.nA env.num_states = env.nS o = env.reset() dp = DP(env) # for _ in range(1): # dp.policy_eval() # dp.policy_imp() dp.policy_iter() dp.q_values = np.array([dp.q_values[s] for s in dp.q_values]) pi = dp.policy # plt.figure(figsize=(8, 8),num="pi") # sns.heatmap(dp.policy.reshape(16, 4), # cmap="Spectral", annot=True, cbar=False) # plt.show() for _ in range(200): random_rewards = env.rewards new_rewards = self.mcmc_reward_step(random_rewards, step_size=0.5, r_max=1) # new_rewards = self.sample_random_rewards(self.num_states, 1, 1) env_new = FrozenLakeEnv(is_slippery=True, rewards=new_rewards) env_new.num_actions = env_new.nA env_new.num_states = env_new.nS # o = env_new.reset() dp_new = DP(env_new) # print(dp_new.policy) # dp_new = DP(env_new) # plt.figure(figsize=(8, 8),num="pi before imp") # sns.heatmap(dp.policy.reshape(16, 4), # cmap="Spectral", annot=True, cbar=False) dp_new.policy_iter() # dp_new.policy_imp() dp_new_q_values = np.array( [dp_new.q_values[s] for s in dp_new.q_values]) dp_new = DP(env_new) dp_new.policy = pi dp_new.q_values = dp_new_q_values # plt.figure(figsize=(8, 8),num="pi after imp") # sns.heatmap(dp.policy.reshape(16, 4), # cmap="Spectral", annot=True, cbar=False) # plt.figure(figsize=(8, 8),num="new q's") # sns.heatmap(dp_new.q_values.reshape(16, 4), # cmap="Spectral", annot=True, cbar=False) # plt.show() """ if "dp_q_values < dp_new_q_values": or if "dp_new_q_values(pi) < dp_new_q_values" (with this for now): """ if self.optimal_q_check(dp_new.q_values, pi): dp_new.policy_iter() pi_new = dp_new.policy """ prob_comparision = update env(rews) policy with prob ( min(1, ratio(posterioirs of dp,dp_new's policies))) """ # if posteriors_ratio(env_new,pi_new,env,pi,prior,) if np.random.random() < self.posteriors_ratio(dp, dp_new): print("update env and pi") # "porb comparision": env, pi = env_new, pi_new else: if np.random.random() < self.posteriors_ratio(dp, dp_new): # if "prob comparision": print("update env") env = env_new # break self.rewards_recovered = env.rewards
def _dp_parser_v2(logger, acls_conf, dps_conf, meters_conf, routers_conf, vlans_conf): dps = [] vid_dp = {} for identifier, dp_conf in list(dps_conf.items()): try: dp = DP(identifier, dp_conf) dp.sanity_check() dp_id = dp.dp_id vlans = {} for vlan_ident, vlan_conf in list(vlans_conf.items()): vlans[vlan_ident] = VLAN(vlan_ident, dp_id, vlan_conf) acls = [] for acl_ident, acl_conf in list(acls_conf.items()): acls.append((acl_ident, ACL(acl_ident, acl_conf))) for router_ident, router_conf in list(routers_conf.items()): router = Router(router_ident, router_conf) dp.add_router(router_ident, router) for meter_ident, meter_conf in list(meters_conf.items()): dp.meters[meter_ident] = Meter(meter_ident, meter_conf) ports_conf = dp_conf.pop('interfaces', {}) ports = {} # as users can config port vlan by using vlan name, we store vid in # Port instance instead of vlan name for data consistency for port_num, port_conf in list(ports_conf.items()): port = port_parser(dp_id, port_num, port_conf, vlans) ports[port_num] = port if port.native_vlan is not None: vlan = _get_vlan_by_identifier(dp_id, port.native_vlan, vlans) port.native_vlan = vlan _dp_add_vlan(vid_dp, dp, vlan) if port.tagged_vlans is not None: tagged_vlans = [] for vlan_ident in port.tagged_vlans: vlan = _get_vlan_by_identifier(dp_id, vlan_ident, vlans) tagged_vlans.append(vlan) _dp_add_vlan(vid_dp, dp, vlan) port.tagged_vlans = tagged_vlans except AssertionError as err: logger.exception('Error in config file: %s', err) return None for port in list(ports.values()): dp.add_port(port) for acl_ident, acl in acls: dp.add_acl(acl_ident, acl) dps.append(dp) return dps
def _dp_parser_v2(logger, acls_conf, dps_conf, meters_conf, routers_conf, vlans_conf): dps = [] vid_dp = collections.defaultdict(set) def _get_vlan_by_identifier(dp_id, vlan_ident, vlans): if vlan_ident in vlans: return vlans[vlan_ident] for vlan in list(vlans.values()): if int(vlan_ident) == vlan.vid: return vlan try: vid = int(vlan_ident, 0) except ValueError: assert False, 'VLAN VID value (%s) is invalid' % vlan_ident return vlans.setdefault(vlan_ident, VLAN(vid, dp_id)) def _dp_add_vlan(dp, vlan): if vlan not in dp.vlans: dp.add_vlan(vlan) vid_dp[vlan.vid].add(dp.name) if len(vid_dp[vlan.vid]) > 1: assert not vlan.bgp_routerid, ( 'DPs %s sharing a BGP speaker VLAN is unsupported' % (str.join(', ', vid_dp[vlan.vid]))) def _dp_parse_port(dp_id, p_identifier, port_conf, vlans): port = Port(p_identifier, port_conf) if port.native_vlan is not None: v_identifier = port.native_vlan vlan = _get_vlan_by_identifier(dp_id, v_identifier, vlans) port.native_vlan = vlan vlan.add_untagged(port) port_tagged_vlans = [] for v_identifier in port.tagged_vlans: vlan = _get_vlan_by_identifier(dp_id, v_identifier, vlans) port_tagged_vlans.append(vlan) vlan.add_tagged(port) port.tagged_vlans = port_tagged_vlans for vlan in port.vlans(): _dp_add_vlan(dp, vlan) return port def _dp_add_ports(dp, dp_conf, dp_id, vlans): ports_conf = dp_conf.pop('interfaces', {}) # as users can config port vlan by using vlan name, we store vid in # Port instance instead of vlan name for data consistency for port_num, port_conf in list(ports_conf.items()): port = _dp_parse_port(dp_id, port_num, port_conf, vlans) dp.add_port(port) try: for identifier, dp_conf in list(dps_conf.items()): dp = DP(identifier, dp_conf) dp.sanity_check() dp_id = dp.dp_id vlans = {} for vlan_ident, vlan_conf in list(vlans_conf.items()): vlans[vlan_ident] = VLAN(vlan_ident, dp_id, vlan_conf) acls = [] for acl_ident, acl_conf in list(acls_conf.items()): acls.append((acl_ident, ACL(acl_ident, acl_conf))) for router_ident, router_conf in list(routers_conf.items()): router = Router(router_ident, router_conf) dp.add_router(router_ident, router) for meter_ident, meter_conf in list(meters_conf.items()): dp.meters[meter_ident] = Meter(meter_ident, meter_conf) _dp_add_ports(dp, dp_conf, dp_id, vlans) for acl_ident, acl in acls: dp.add_acl(acl_ident, acl) dps.append(dp) for dp in dps: dp.finalize_config(dps) for dp in dps: dp.resolve_stack_topology(dps) except AssertionError as err: logger.exception('Error in config file: %s', err) return None return dps