added type to class
[sfa.git] / sfa / rspecs / pg_rspec.py
1 #!/usr/bin/python 
2 from lxml import etree
3 from StringIO import StringIO
4 from sfa.rspecs.rspec import RSpec 
5 from sfa.util.xrn import *
6 from sfa.util.plxrn import hostname_to_urn
7 from sfa.util.config import Config  
8
9 class PGRSpec(RSpec):
10     xml = None
11     header = '<?xml version="1.0"?>\n'
12     namespaces = {'rspecv2':'http://www.protogeni.net/resources/rspec/0.2',
13                   'xsi': 'http://www.w3.org/2001/XMLSchema-instance'
14                  }
15     schemas =  {'xsi': 'http://www.protogeni.net/resources/rspec/0.2 http://www.protogeni.net/resources/rspec/0.2/ad.xsd'
16             }
17     type = 'pg'
18
19     def create(self, type="advertisement"):
20         RSpec.create(self)
21         for namespace in self.namespaces:
22             xmlns = "xmlns"
23             if namespace not in 'rspecv2':
24                 xmlns = xmlns + ":" + namespace
25             self.xml.set(xmlns, self.namespaces[namespace])
26         for schema in self.schemas:
27             self.xml.set(schema+":schemaLocation", self.schemas[schema])
28
29     def get_network(self):
30         network = None 
31         nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', self.namespaces)
32         if nodes:
33             network  = nodes[0].get('component_manager_uuid')
34         return network
35
36     def get_nodes(self, nodes_with_slivers=False):
37         nodes = self.xml.xpath('//rspecv2:node', self.namespaces)
38         return nodes
39
40     def add_nodes(self, nodes, check_for_dupes=False):
41         if not isinstance(nodes, list):
42             nodes = [nodes]
43         for node in nodes:
44             urn = ""
45             if check_for_dupes and \
46               self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, self.namespaces):
47                 # node already exists
48                 continue
49                 
50             node_tag = etree.SubElement(self.xml, 'node')
51             node_type_tag = etree.SubElement(node_tag, 'node_type', type_name='pcvm', type_slots='100')
52             available_tag = etree.SubElement(node_tag, 'available').text = 'true'
53             exclusive_tag = etree.SubElement(node_tag, 'exclusive').text = 'false'
54             location_tag = etree.SubElement(node_tag, 'location')
55             interface_tag = etree.SubElement(node_tag, 'interface')
56             
57
58     def add_slivers(self, slivers, check_for_dupes=False): 
59         pass
60
61     def add_links(self, links, check_for_dupes=False):
62         pass
63
64
65 if __name__ == '__main__':
66     rspec = PGRSpec()
67     rspec.add_nodes([1])
68     print rspec