autopep8
[sfa.git] / sfa / rspecs / elements / versions / ofeliav1Port.py
1 from sfa.util.xrn import Xrn
2 from sfa.util.xml import XmlElement
3
4 from sfa.rspecs.elements.element import Element
5 from sfa.rspecs.elements.port import Port
6
7
8 class Ofeliav1Port:
9
10     @staticmethod
11     def add_portrs(xml, ports):
12         raise Exception("not implemented yet")
13         if not ports:
14             return
15         if not isinstance(ports, list):
16             ports = [ports]
17         for port in ports:
18             port_elem = xml.add_instance('port', port, ['name'])
19             tags = port.get('tags', [])
20             if tags:
21                 for tag in tags:
22                     Ofeliav1Port.add_port_attribute(
23                         port_elem, tag['tagname'], tag['value'])
24
25     @staticmethod
26     def add_port_attribute(xml, name, value):
27         raise Exception("not implemented yet")
28         elem = xml.add_element(name)
29         elem.set_text(value)
30
31     @staticmethod
32     def get_port_attributes(xml):
33         attribs = []
34         for elem in xml.iterchildren():
35             if elem.tag not in Port.fields:
36                 xml_element = XmlElement(elem, xml.namespaces)
37                 instance = Element(fields=xml_element, element=elem)
38                 instance['name'] = elem.tag
39                 instance['value'] = elem.text
40                 attribs.append(instance)
41         return attribs
42
43     @staticmethod
44     def get_ports(xml, filter=None):
45         if filter is None:
46             filter = {}
47         xpath = './openflow:port | ./port'
48         port_elems = xml.xpath(xpath)
49         ports = []
50         for port_elem in port_elems:
51             port = Port(port_elem.attrib, port_elem)
52             # if 'component_id' in xml.attrib:
53             #    port['component_id'] = xml.attrib['component_id']
54             #port['tags'] = Ofeliav1Port.get_port_attributes(port_elem)
55             ports.append(port)
56         return ports