Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
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)
Example #7
0
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)