def write_event_schedule(event_schedule, path, encoding='utf-8', prettyprint=True): """Write an event schedule object to an XML file. Parameters ---------- event_schedule : EventSchedule The event schedule to write path : str The path of the output XML file encoding : str, optional The desired encoding of the output file prettyprint : bool, optional Specify whether the XML file should be written with indentation for improved human readability """ head = ET.Element("event-schedule") for name, value in event_schedule.attrib.items(): prop = ET.SubElement(head, "property") prop.attrib['name'] = str(name) prop.attrib['type'] = util.xml_type(value) prop.text = str(value) for time, event_props in event_schedule: event = ET.SubElement(head, "event") event.attrib['time'] = str(time) for name, value in event_props.items(): prop = ET.SubElement(event, "property") prop.attrib['name'] = str(name) prop.attrib['type'] = util.xml_type(value) prop.text = str(value) if prettyprint: util.xml_indent(head) ET.ElementTree(head).write(path, encoding=encoding)
def write_traffic_matrix(traffic_matrix, path, encoding='utf-8', prettyprint=True): """ Write a TrafficMatrix or a TrafficMatrixSequence object to an XML file. This function can be use to either persistently store a traffic matrix for later use or to export it to an FNSS adapter for a simulator or an API for another programming language. Parameters ---------- traffic_matrix : TrafficMatrix or TrafficMatrixSequence The traffic matrix to save path : str The path where the file will be saved encoding : str, optional The desired encoding of the output file prettyprint : bool, optional Specify whether the XML file should be written with indentation for improved human readability """ head = ET.Element("traffic-matrix") if isinstance(traffic_matrix, TrafficMatrix): head.attrib['type'] = 'single' matrices = [traffic_matrix] elif isinstance(traffic_matrix, TrafficMatrixSequence): for name, value in traffic_matrix.attrib: prop = ET.SubElement(head, "property") prop.attrib['name'] = str(name) prop.attrib['type'] = util.xml_type(value) prop.text = str(value) head.attrib['type'] = 'sequence' matrices = traffic_matrix.matrix else: raise ValueError('traffic_matrix parameter must be either a ' / 'TrafficMatrix or a TrafficMatrixSequence instance') for matrix in matrices: time = ET.SubElement(head, "time") time.attrib['seq'] = str(matrices.index(matrix)) for name, value in matrix.attrib.items(): prop = ET.SubElement(time, "property") prop.attrib['name'] = str(name) prop.attrib['type'] = util.xml_type(value) prop.text = str(value) for o in matrix.flow: origin = ET.SubElement(time, "origin") origin.attrib['id'] = str(o) origin.attrib['id.type'] = util.xml_type(o) for d in matrix.flow[o]: volume = matrix.flow[o][d] destination = ET.SubElement(origin, "destination") destination.attrib['id'] = str(d) destination.attrib['id.type'] = util.xml_type(d) destination.text = str(volume) if prettyprint: util.xml_indent(head) ET.ElementTree(head).write(path, encoding=encoding)
def write_event_schedule(event_schedule, path, encoding='utf-8', prettyprint=True): """ Write an event schedule object to an XML file. """ head = ET.Element("event-schedule") for name, value in event_schedule.attrib.items(): prop = ET.SubElement(head, "property") prop.attrib['name'] = str(name) prop.attrib['type'] = util.xml_type(value) prop.text = str(value) for time, event_props in event_schedule: event = ET.SubElement(head, "event") event.attrib['time'] = str(time) for name, value in event_props.items(): prop = ET.SubElement(event, "property") prop.attrib['name'] = str(name) prop.attrib['type'] = util.xml_type(value) prop.text = str(value) if prettyprint: util.xml_indent(head) ET.ElementTree(head).write(path, encoding=encoding)
def write_topology(topology, path, encoding='utf-8', prettyprint=True): """ Writes a topology object on an XML file Parameters ---------- topology : Topology The topology object to write path : str The file ob which the topology will be written encoding : str, optional The encoding of the target file prettyprint : bool, optional Indent the XML code in the output file """ head = ET.Element('topology') head.attrib['linkdefault'] = 'directed' if topology.is_directed() \ else 'undirected' for name, value in topology.graph.items(): prop = ET.SubElement(head, 'property') prop.attrib['name'] = name prop.attrib['type'] = util.xml_type(value) prop.text = str(value) for v in topology.nodes_iter(): node = ET.SubElement(head, 'node') node.attrib['id'] = str(v) node.attrib['id.type'] = util.xml_type(v) for name, value in topology.node[v].items(): if name is 'stack': stack_name, stack_props = topology.node[v]['stack'] stack = ET.SubElement(node, 'stack') stack.attrib['name'] = stack_name stack.attrib['name.type'] = util.xml_type(stack_name) for prop_name, prop_value in stack_props.items(): prop = ET.SubElement(stack, 'property') prop.attrib['name'] = prop_name prop.attrib['type'] = util.xml_type(prop_value) prop.text = str(prop_value) elif name is 'application': for application_name, application_props in \ topology.node[v]['application'].items(): application = ET.SubElement(node, 'application') application.attrib['name'] = application_name application.attrib['name.type'] = \ util.xml_type(application_name) for prop_name, prop_value in application_props.items(): prop = ET.SubElement(application, 'property') prop.attrib['name'] = prop_name prop.attrib['type'] = util.xml_type(prop_value) prop.text = str(prop_value) else: prop = ET.SubElement(node, 'property') prop.attrib['name'] = name prop.attrib['type'] = util.xml_type(value) prop.text = str(value) for u, v in topology.edges_iter(): link = ET.SubElement(head, 'link') from_node = ET.SubElement(link, 'from') from_node.attrib['type'] = util.xml_type(u) from_node.text = str(u) to_node = ET.SubElement(link, 'to') to_node.attrib['type'] = util.xml_type(v) to_node.text = str(v) for name, value in topology.edge[u][v].items(): prop = ET.SubElement(link, 'property') prop.attrib['name'] = name prop.attrib['type'] = util.xml_type(value) prop.text = str(value) if prettyprint: util.xml_indent(head) ET.ElementTree(head).write(path, encoding=encoding)
def write_topology(topology, path, encoding='utf-8', prettyprint=True): """Write a topology object on an XML file Parameters ---------- topology : Topology The topology object to write path : str The file ob which the topology will be written encoding : str, optional The encoding of the target file prettyprint : bool, optional Indent the XML code in the output file """ head = ET.Element('topology') head.attrib['linkdefault'] = 'directed' if topology.is_directed() \ else 'undirected' for name, value in topology.graph.items(): prop = ET.SubElement(head, 'property') prop.attrib['name'] = name prop.attrib['type'] = util.xml_type(value) prop.text = str(value) for v in topology.nodes(): node = ET.SubElement(head, 'node') node.attrib['id'] = str(v) node.attrib['id.type'] = util.xml_type(v) for name, value in topology.node[v].items(): if name is 'stack': stack_name, stack_props = topology.node[v]['stack'] stack = ET.SubElement(node, 'stack') stack.attrib['name'] = stack_name stack.attrib['name.type'] = util.xml_type(stack_name) for prop_name, prop_value in stack_props.items(): prop = ET.SubElement(stack, 'property') prop.attrib['name'] = prop_name prop.attrib['type'] = util.xml_type(prop_value) prop.text = str(prop_value) elif name is 'application': for application_name, application_props in \ topology.node[v]['application'].items(): application = ET.SubElement(node, 'application') application.attrib['name'] = application_name application.attrib['name.type'] = \ util.xml_type(application_name) for prop_name, prop_value in application_props.items(): prop = ET.SubElement(application, 'property') prop.attrib['name'] = prop_name prop.attrib['type'] = util.xml_type(prop_value) prop.text = str(prop_value) else: prop = ET.SubElement(node, 'property') prop.attrib['name'] = name prop.attrib['type'] = util.xml_type(value) prop.text = str(value) for u, v in topology.edges(): link = ET.SubElement(head, 'link') from_node = ET.SubElement(link, 'from') from_node.attrib['type'] = util.xml_type(u) from_node.text = str(u) to_node = ET.SubElement(link, 'to') to_node.attrib['type'] = util.xml_type(v) to_node.text = str(v) for name, value in topology.adj[u][v].items(): prop = ET.SubElement(link, 'property') prop.attrib['name'] = name prop.attrib['type'] = util.xml_type(value) prop.text = str(value) if prettyprint: util.xml_indent(head) ET.ElementTree(head).write(path, encoding=encoding)
def to_jfed(topology, path, testbed="wall1.ilabt.iminds.be", encoding="utf-8", prettyprint=True): """Convert a topology object into an RSPEC file for jFed Parameters ---------- topology : Topology The topology object path : str The file to which the RSPEC will be written testbed : str, optional URI of the testbed to use encoding : str, optional The encoding of the target file prettyprint : bool, optional Indent the XML code in the output file Notes ----- It currently supports only undirected topologies, if a topology is directed it is converted to undirected """ if topology.is_directed(): topology = topology.to_undirected() topology = nx.convert_node_labels_to_integers(topology) if 'capacity_unit' in topology.graph: capacity_norm = units.capacity_units[ topology.graph['capacity_unit']] / units.capacity_units['Kbps'] if 'delay_unit' in topology.graph: delay_norm = units.time_units[ topology.graph['delay_unit']] / units.time_units['ms'] delays = get_delays(topology) capacities = get_capacities(topology) # Node positions (randomly generated) pos = nx.random_layout(topology) # Create mapping between links and interface IDs if_names = {} for v in topology.edge: next_hops = sorted(topology.edge[v].keys()) if_names[v] = dict((next_hops[i], i) for i in range(len(next_hops))) head = ET.Element('rspec') head.attrib["generated_by"] = "FNSS" head.attrib[ 'xsi:schemaLocation'] = "http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/request.xsd" head.attrib['xmlns'] = "http://www.geni.net/resources/rspec/3" head.attrib["xmlns:jFed"] = "http://jfed.iminds.be/rspec/ext/jfed/1" head.attrib[ "xmlns:jFedBonfire"] = "http://jfed.iminds.be/rspec/ext/jfed-bonfire/1" head.attrib[ "xmlns:delay"] = "http://www.protogeni.net/resources/rspec/ext/delay/1" head.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance" # Iterate over nodes for v in topology.nodes_iter(): node = ET.SubElement(head, 'node') node.attrib['client_id'] = "node%s" % str(v) node.attrib[ 'component_manager_id'] = "urn:publicid:IDN+%s+authority+cm" % testbed node.attrib["exclusive"] = "true" sliver_type = ET.SubElement(node, 'sliver_type') sliver_type.attrib['name'] = topology.node[v][ 'sliver_type'] if 'sliver_type' in topology.node[v] else 'raw-pc' location = ET.SubElement(node, 'jFed:location') x, y = pos[v] location.attrib['x'] = str(1000 * x) location.attrib['y'] = str(500 * y) for if_name in if_names[v].values(): interface = ET.SubElement(node, 'interface') interface.attrib['client_id'] = "node%s:if%s" % (str(v), str(if_name)) # The convention in jFed is to identify links with "linkX" where X is an # integer but making sure that links and nodes have different integers link_id = topology.number_of_nodes() - 1 for u, v in topology.edges_iter(): link_id += 1 link = ET.SubElement(head, 'link') link.attrib['client_id'] = "link%s" % str(link_id) component_manager = ET.SubElement(link, 'component_manager') component_manager.attrib[ 'name'] = "urn:publicid:IDN+%s+authority+cm" % testbed u_if = "node%s:if%s" % (str(u), str(if_names[u][v])) v_if = "node%s:if%s" % (str(v), str(if_names[v][u])) for source, dest in ((u_if, v_if), (v_if, u_if)): prop = ET.SubElement(link, 'property') prop.attrib["source_id"] = source prop.attrib["dest_id"] = dest if (u, v) in delays: prop.attrib['latency'] = str(delay_norm * delays[(u, v)]) if (u, v) in capacities: prop.attrib['capacity'] = str(capacity_norm * capacities[(u, v)]) interface_ref = ET.SubElement(link, 'interface_ref') interface_ref.attrib['client_id'] = source if prettyprint: util.xml_indent(head) ET.ElementTree(head).write(path, encoding=encoding)
def to_jfed(topology, path, testbed="wall1.ilabt.iminds.be", encoding="utf-8", prettyprint=True): """Convert a topology object into an RSPEC file for jFed Parameters ---------- topology : Topology The topology object path : str The file to which the RSPEC will be written testbed : str, optional URI of the testbed to use encoding : str, optional The encoding of the target file prettyprint : bool, optional Indent the XML code in the output file Notes ----- It currently supports only undirected topologies, if a topology is directed it is converted to undirected """ if topology.is_directed(): topology = topology.to_undirected() topology = nx.convert_node_labels_to_integers(topology) if 'capacity_unit' in topology.graph: capacity_norm = units.capacity_units[topology.graph['capacity_unit']] / units.capacity_units['Kbps'] if 'delay_unit' in topology.graph: delay_norm = units.time_units[topology.graph['delay_unit']] / units.time_units['ms'] delays = get_delays(topology) capacities = get_capacities(topology) # Node positions (randomly generated) pos = nx.random_layout(topology) # Create mapping between links and interface IDs if_names = {} for v in topology.adj: next_hops = sorted(topology.adj[v].keys()) if_names[v] = {next_hop: i for i, next_hop in enumerate(next_hops)} head = ET.Element('rspec') head.attrib["generated_by"] = "FNSS" head.attrib['xsi:schemaLocation'] = "http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/request.xsd" head.attrib['xmlns'] = "http://www.geni.net/resources/rspec/3" head.attrib["xmlns:jFed"] = "http://jfed.iminds.be/rspec/ext/jfed/1" head.attrib["xmlns:jFedBonfire"] = "http://jfed.iminds.be/rspec/ext/jfed-bonfire/1" head.attrib["xmlns:delay"] = "http://www.protogeni.net/resources/rspec/ext/delay/1" head.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance" # Iterate over nodes for v in topology.nodes(): node = ET.SubElement(head, 'node') node.attrib['client_id'] = "node%s" % str(v) node.attrib['component_manager_id'] = "urn:publicid:IDN+%s+authority+cm" % testbed node.attrib["exclusive"] = "true" sliver_type = ET.SubElement(node, 'sliver_type') sliver_type.attrib['name'] = topology.node[v]['sliver_type'] if 'sliver_type' in topology.node[v] else 'raw-pc' location = ET.SubElement(node, 'jFed:location') x, y = pos[v] location.attrib['x'] = str(1000 * x) location.attrib['y'] = str(500 * y) for if_name in if_names[v].values(): interface = ET.SubElement(node, 'interface') interface.attrib['client_id'] = "node%s:if%s" % (str(v), str(if_name)) # The convention in jFed is to identify links with "linkX" where X is an # integer but making sure that links and nodes have different integers link_id = topology.number_of_nodes() - 1 for u, v in topology.edges(): link_id += 1 link = ET.SubElement(head, 'link') link.attrib['client_id'] = "link%s" % str(link_id) component_manager = ET.SubElement(link, 'component_manager') component_manager.attrib['name'] = "urn:publicid:IDN+%s+authority+cm" % testbed u_if = "node%s:if%s" % (str(u), str(if_names[u][v])) v_if = "node%s:if%s" % (str(v), str(if_names[v][u])) for source, dest in ((u_if, v_if), (v_if, u_if)): prop = ET.SubElement(link, 'property') prop.attrib["source_id"] = source prop.attrib["dest_id"] = dest if (u, v) in delays: prop.attrib['latency'] = str(delay_norm * delays[(u, v)]) if (u, v) in capacities: prop.attrib['capacity'] = str(capacity_norm * capacities[(u, v)]) interface_ref = ET.SubElement(link, 'interface_ref') interface_ref.attrib['client_id'] = source if prettyprint: util.xml_indent(head) ET.ElementTree(head).write(path, encoding=encoding)