ee9797a323ddc0f93e780c45ded8d8ccb76e9136
[sfa.git] / sfa / rspecs / elements / versions / pgv2SliverType.py
1 from sfa.rspecs.elements.element import Element
2 from sfa.rspecs.elements.sliver import Sliver
3 from sfa.rspecs.elements.versions.pgv2DiskImage import PGv2DiskImage
4 from sfa.rspecs.elements.versions.plosv1FWRule import PLOSv1FWRule
5
6 class PGv2SliverType:
7
8     @staticmethod
9     def add_slivers(xml, slivers):
10         if not slivers:
11             return 
12         if not isinstance(slivers, list):
13             slivers = [slivers]
14         for sliver in slivers: 
15             sliver_elem = xml.add_element('sliver_type')
16             if sliver.get('type'):
17                 sliver_elem.set('name', sliver['type'])
18             attrs = ['client_id', 'cpus', 'memory', 'storage']
19             for attr in attrs:
20                 if sliver.get(attr):
21                     sliver_elem.set(attr, sliver[attr])
22             
23             images = sliver.get('disk_image')
24             if images and isinstance(images, list):
25                 PGv2DiskImage.add_images(sliver_elem, images)      
26             fw_rules = sliver.get('fw_rules')
27             if fw_rules and isinstance(fw_rules, list):
28                 PLOSv1FWRule.add_rules(sliver_elem, fw_rules)
29             PGv2SliverType.add_sliver_attributes(sliver_elem, sliver.get('tags', []))
30     
31     @staticmethod
32     def add_sliver_attributes(xml, attributes):
33         if attributes: 
34             for attribute in attributes:
35                 if attribute['name'] == 'initscript':
36                     xml.add_element('{%s}initscript' % xml.namespaces['planetlab'], name=attribute['value'])
37                 elif attribute['tagname'] == 'flack_info':
38                     attrib_elem = xml.add_element('{%s}info' % self.namespaces['flack'])
39                     attrib_dict = eval(tag['value'])
40                     for (key, value) in attrib_dict.items():
41                         attrib_elem.set(key, value)
42     @staticmethod
43     def get_slivers(xml, filter=None):
44         if filter is None: filter={}
45         xpath = './default:sliver_type | ./sliver_type'
46         sliver_elems = xml.xpath(xpath)
47         slivers = []
48         for sliver_elem in sliver_elems:
49             sliver = Sliver(sliver_elem.attrib,sliver_elem)
50             if 'component_id' in xml.attrib:     
51                 sliver['component_id'] = xml.attrib['component_id']
52             if 'name' in sliver_elem.attrib:
53                 sliver['type'] = sliver_elem.attrib['name']
54             sliver['disk_image'] = PGv2DiskImage.get_images(sliver_elem)
55             sliver['fw_rules'] = PLOSv1FWRule.get_rules(sliver_elem)
56             slivers.append(sliver)
57         return slivers
58
59     @staticmethod
60     def get_sliver_attributes(xml, filter=None):
61         if filter is None: filter={}
62         return []