def read_event_schedule(path): """Read event schedule from an XML file Parameters ---------- path : str The path to the event schedule XML file Returns ------- event_schedule : EventSchedule The parsed event schedule """ event_schedule = EventSchedule() tree = ET.parse(path) head = tree.getroot() for prop in head.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) event_schedule.attrib[name] = value # this is needed for not messing up the automatic sorting of the event list event_schedule.attrib['t_end'] = 0 for event in head.findall('event'): time = float(event.attrib['time']) event_prop = {} for prop in event.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) event_prop[name] = value event_schedule.add(time, event_prop, absolute_time=True) return event_schedule
def read_traffic_matrix(path, encoding='utf-8'): """ Parses a traffic matrix from a traffic matrix XML file. If the XML file contains more than one traffic matrix, it returns a TrafficMatrixSequence object, otherwise a TrafficMatrixObject. Parameters ---------- path: str The path of the XML file to parse encoding : str, optional The encoding of the file Returns ------- tm : TrafficMatrix or TrafficMatrixSequence """ def parse_single_matrix(head): """ Parses a single traffic matrix from the XML file """ traffic_matrix = TrafficMatrix() for prop in head.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) if name == 'volume_unit' and value not in capacity_units: raise ET.ParseError(\ 'Invalid volume_unit property in time node') traffic_matrix.attrib[name] = value for origin in head.findall('origin'): o = util.xml_cast_type(origin.attrib['id.type'], origin.attrib['id']) for destination in origin.findall('destination'): d = util.xml_cast_type(destination.attrib['id.type'], destination.attrib['id']) volume = float(destination.text) traffic_matrix.add_flow(o, d, volume) return traffic_matrix tree = ET.parse(path) head = tree.getroot() matrix_type = head.attrib['type'] if matrix_type == 'single': traffic_matrix = parse_single_matrix(head.find('time')) elif matrix_type == 'sequence': traffic_matrix = TrafficMatrixSequence() for prop in head.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) traffic_matrix.attrib[name] = value for matrix in head.findall('time'): traffic_matrix.append(parse_single_matrix(matrix)) else: raise ET.ParseError('Invalid TM type attribute in XML file') return traffic_matrix
def parse_single_matrix(head): """ Parses a single traffic matrix from the XML file """ traffic_matrix = TrafficMatrix() for prop in head.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) if name == 'volume_unit' and value not in capacity_units: raise ET.ParseError(\ 'Invalid volume_unit property in time node') traffic_matrix.attrib[name] = value for origin in head.findall('origin'): o = util.xml_cast_type(origin.attrib['id.type'], origin.attrib['id']) for destination in origin.findall('destination'): d = util.xml_cast_type(destination.attrib['id.type'], destination.attrib['id']) volume = float(destination.text) traffic_matrix.add_flow(o, d, volume) return traffic_matrix
def read_topology(path, encoding='utf-8'): """ Read a topology from an XML file and returns either a Topology or a DirectedTopology object Parameters ---------- path : str The path of the topology XML file to parse encoding : str, optional The encoding of the file Returns ------- topology: Topology or DirectedTopology """ tree = ET.parse(path) head = tree.getroot() topology = Topology() if head.attrib['linkdefault'] == 'undirected' \ else DirectedTopology() for prop in head.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) topology.graph[name] = value for node in head.findall('node'): v = util.xml_cast_type(node.attrib['id.type'], node.attrib['id']) topology.add_node(v) for prop in node.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) topology.node[v][name] = value if len(node.findall('stack')) > 0: if len(node.findall('stack')) > 1: raise ET.ParseError('Invalid topology. ' \ 'A node has more than one stack.') stack = node.findall('stack')[0] stack_name = util.xml_cast_type(stack.attrib['name.type'], stack.attrib['name']) stack_props = {} for prop in stack.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) stack_props[name] = value topology.node[v]['stack'] = (stack_name, stack_props) if len(node.findall('application')) > 0: topology.node[v]['application'] = {} for application in node.findall('application'): app_name = util.xml_cast_type(application.attrib['name.type'], application.attrib['name']) app_props = {} for prop in application.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) app_props[name] = value topology.node[v]['application'][app_name] = app_props for edge in head.findall('link'): u = util.xml_cast_type(edge.find('from').attrib['type'], edge.find('from').text) v = util.xml_cast_type(edge.find('to').attrib['type'], edge.find('to').text) topology.add_edge(u, v) for prop in edge.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) topology.edge[u][v][name] = value return topology
def read_topology(path, encoding='utf-8'): """Read a topology from an XML file and returns either a Topology or a DirectedTopology object Parameters ---------- path : str The path of the topology XML file to parse encoding : str, optional The encoding of the file Returns ------- topology: Topology or DirectedTopology The parsed topology """ tree = ET.parse(path) head = tree.getroot() topology = Topology() if head.attrib['linkdefault'] == 'undirected' \ else DirectedTopology() for prop in head.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) topology.graph[name] = value for node in head.findall('node'): v = util.xml_cast_type(node.attrib['id.type'], node.attrib['id']) topology.add_node(v) for prop in node.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) topology.node[v][name] = value if len(node.findall('stack')) > 0: if len(node.findall('stack')) > 1: raise ET.ParseError('Invalid topology. ' \ 'A node has more than one stack.') stack = node.findall('stack')[0] stack_name = util.xml_cast_type(stack.attrib['name.type'], stack.attrib['name']) stack_props = {} for prop in stack.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) stack_props[name] = value topology.node[v]['stack'] = (stack_name, stack_props) if len(node.findall('application')) > 0: topology.node[v]['application'] = {} for application in node.findall('application'): app_name = util.xml_cast_type(application.attrib['name.type'], application.attrib['name']) app_props = {} for prop in application.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) app_props[name] = value topology.node[v]['application'][app_name] = app_props for edge in head.findall('link'): u = util.xml_cast_type( edge.find('from').attrib['type'], edge.find('from').text) v = util.xml_cast_type( edge.find('to').attrib['type'], edge.find('to').text) topology.add_edge(u, v) for prop in edge.findall('property'): name = prop.attrib['name'] value = util.xml_cast_type(prop.attrib['type'], prop.text) topology.adj[u][v][name] = value return topology