Ofelia OpenFlow RSpec parsing support
[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 class Ofeliav1Port:
8
9     @staticmethod
10     def add_portrs(xml, ports):
11         raise Exception, "not implemented yet"
12         if not ports:
13             return
14         if not isinstance(ports, list):
15             ports = [ports]
16         for port in ports:
17             port_elem = xml.add_instance('port', port, ['name'])
18             tags = port.get('tags', [])
19             if tags:
20                 for tag in tags:
21                     Ofeliav1Port.add_port_attribute(port_elem, tag['tagname'], tag['value'])
22
23     @staticmethod
24     def add_port_attribute(xml, name, value):
25         raise Exception, "not implemented yet"
26         elem = xml.add_element(name)
27         elem.set_text(value)
28     
29     @staticmethod
30     def get_port_attributes(xml):
31         attribs = []
32         for elem in xml.iterchildren():
33             if elem.tag not in Port.fields:
34                 xml_element = XmlElement(elem, xml.namespaces)
35                 instance = Element(fields=xml_element, element=elem)
36                 instance['name'] = elem.tag
37                 instance['value'] = elem.text
38                 attribs.append(instance)
39         return attribs 
40                 
41     @staticmethod
42     def get_ports(xml, filter={}):
43         xpath = './openflow:port | ./port'
44         port_elems = xml.xpath(xpath)
45         ports = []
46         for port_elem in port_elems:
47             port = Port(port_elem.attrib,port_elem)
48             #if 'component_id' in xml.attrib:     
49             #    port['component_id'] = xml.attrib['component_id']
50             #port['tags'] = Ofeliav1Port.get_port_attributes(port_elem)
51             ports.append(port)
52         return ports           
53