def test_get_max_distance_in_network(self): node1 = lib.Struct("node1_mock", location=(10, 20)) node2 = lib.Struct("node2_mock", location=(13, 24)) node3 = lib.Struct("node3_mock", location=(12, 24)) nodes = {"node1": node1, "node2": node2, "node3": node3} max_distance = wwnetwork.get_max_distance_in_network(nodes, "node1", ["node2", "node3"]) self.assertEqual(max_distance, 5.0)
def parse_report(filename): """ Read and parse a Radiomobile report.txt file. >>> report = parse_report("report.txt") >>> report.nets >>> report.systems >>> report.units """ lines = open(filename).read().splitlines() splitted_lines = list( lib.split_iter(lines, lambda s: s.startswith("---"), skip_sep=True)) generated_on = parse_header(splitted_lines[0]) sections = dict((lib.keyify(key[0]), val) for (key, val) in lib.grouper(2, splitted_lines[1:])) units = parse_active_units(sections["active_units_information"]) report = lib.Struct("RadioMobileReport", generated_on=generated_on, general_information=sections["general_information"], units=units, systems=parse_systems(sections["systems"]), nets=parse_active_nets( sections["active_nets_information"], units)) return report
def add_interface_to_device_node(node, short_net_name, network, address): """Add a ns-3 interface (address) to node.devices[system_name].interfaces.""" attributes = network[node.name] device_key = get_device_key(short_net_name, attributes["system"]) interface = lib.Struct("Interface", address=address) logging.info("Add interface to node %s (%s)" % (node.name, str(address))) node.devices[device_key].interfaces.append(interface)
def parse_active_nets(lines, units): """Return an orderd dict with nets, each containing a list of links.""" nets_lines = list( lib.split_iter_of_consecutive(lines[1:], lambda s: not s.strip(), 2)) nets = odict() for net_lines in lib.strip_iter_items(nets_lines): info = lib.strip_list(net_lines) name = info[0].strip() block = list( lib.iter_block(lib.strip_list(info[1:]), r"Net members:", r"\s.*Quality =")) table, quality_line = block[:-2], block[-1] max_quality = int(re.search("Quality = (\d+)", quality_line).group(1)) grid_field = re.match("Net members:\s*(.*?)\s*Role:", table[0]).group(1) grid_fields = [ "Net members:", grid_field, "Role:", "System:", "Antenna:" ] rows = list( lib.parse_table(table, grid_fields, lambda s: not s.startswith('#'))) net_members = create_odict_from_items("net_member", "net_members", rows) links = [] for link in get_net_links(rows, grid_field, units): peers = (link["node1"].net_members, link["node2"].net_members) link = lib.Struct("Link", peers=peers, quality=link["quality"], distance=link["distance"]) links.append(link) nets[name] = lib.Struct("Network", name=name, net_members=net_members, links=links, max_quality=max_quality) return nets
def add_device_to_node(node, short_net_name, network, ns3_device, helper=None, phy_helper=None): """Add a ns-3 device to a node structure.""" attributes = network[node.name] device_key = get_device_key(short_net_name, attributes["system"]) device = lib.Struct("Device", ns3_device=ns3_device, helper=helper, phy_helper=phy_helper, interfaces=[], wimax_flow_services=[]) node.devices[device_key] = device
def create_network(netinfo): """Create a network Struct from a RadioMobile parsed text report.""" nodes = {} for name, attrs in netinfo["units"].iteritems(): node = lib.Struct("Node", name=name, location=attrs["location"], ns3_node=ns3.Node(), devices={}) nodes[name] = node ns3node_to_node = dict( (node.ns3_node.GetId(), node) for node in nodes.values()) def get_node_from_ns3node(ns3_node): return ns3node_to_node[ns3_node.GetId()] # Internet stack stack = ns3.InternetStackHelper() for name, node in nodes.iteritems(): stack.Install(node.ns3_node) networks = {} for net_index, (net_name, network) in enumerate(netinfo["networks"].iteritems()): # Nodes node = network["node"] node_member = node["name"] terminal_members = [ terminal["name"] for terminal in network["terminals"] ] ap_node = nodes[node_member].ns3_node sta_nodes = ns3.NodeContainer() for name in terminal_members: sta_nodes.Add(nodes[name].ns3_node) networks[name] = lib.Struct("network", node=node_member, terminals=terminal_members) mode = network["mode"] network_info = dict( (d["name"], d) for d in [network["node"]] + network["terminals"]) # Configure WiFi or WiMax devices if mode["standard"].startswith("wifi"): wifi_network(network_info, net_index, net_name, mode["wifi_mode"], nodes, get_node_from_ns3node, node_member, terminal_members) elif mode["standard"].startswith("wimax"): scheduler = getattr( ns3.WimaxHelper, "SCHED_TYPE_" + mode["wimax_scheduler"].upper()) wimax_network(network_info, net_index, net_name, nodes, get_node_from_ns3node, node_member, terminal_members, scheduler) else: raise ValueError, ("Network name must be 'name [wifi_with_ns3_mode" + "| wimax-scheduler]': %s") % ns3_mode # Mobility mobility = ns3.MobilityHelper() mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel") for member in [node_member] + terminal_members: node = nodes[member] allocator = ns3.ListPositionAllocator() position = tuple(node.location) + (0, ) #position = (0, 0, 0) # debug allocator.Add(ns3.Vector(*position)) mobility.SetPositionAllocator(allocator) mobility.Install(node.ns3_node) ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables() return lib.Struct("Network", nodes=nodes, networks=networks)
def _generator(): for delement in dictlst: dvalues = dict((k, v) for (k, v) in delement.iteritems()) yield (delement[key], lib.Struct(name, **dvalues))
def clean_node(node): items = dict( (k, v) for (k, v) in node.iteritems() if not k.startswith("#")) return lib.Struct("Node", **items)