X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=sfa%2Frspecs%2Fsfa_rspec.py;h=4f89cd8f47c092d7982a54ed6a7a11cfa9cba325;hb=fdf164124d89e20f657f4b3e3c0575f79032b3ff;hp=bc67489f10a32c1601f8f2686f7419e66af3250d;hpb=182c57b3675072b599b4b9de47ae801877e2fd40;p=sfa.git diff --git a/sfa/rspecs/sfa_rspec.py b/sfa/rspecs/sfa_rspec.py index bc67489f..4f89cd8f 100755 --- a/sfa/rspecs/sfa_rspec.py +++ b/sfa/rspecs/sfa_rspec.py @@ -11,15 +11,15 @@ class SfaRSpec(RSpec): xml = None header = '\n' namespaces = {} - + format = 'sfa' ################### # Parser ################### def get_network_elements(self): - return self.xml.xpath('//network', self.namespaces) + return self.xml.xpath('//network') def get_networks(self): - return self.xml.xpath('//network[@name]/@name', self.namespaces) + return self.xml.xpath('//network[@name]/@name') def get_node_element(self, hostname, network=None): if network: @@ -32,11 +32,11 @@ class SfaRSpec(RSpec): return None def get_node_elements(self): - return self.xml.xpath('//node', self.namespaces) + return self.xml.xpath('//node') def get_nodes(self, network=None): if network == None: - nodes = self.xml.xpath('//node/hostname/text()', self.namespaces) + nodes = self.xml.xpath('//node/hostname/text()') else: nodes = self.xml.xpath('//network[@name="%s"]//node/hostname/text()' % network, self.namespaces) return nodes @@ -154,32 +154,49 @@ class SfaRSpec(RSpec): # Builder ################## + def add_network(self, network): + network_tag = etree.SubElement(self.xml, 'network', id=network) + def add_nodes(self, nodes, network = None, no_dupes=False): if not isinstance(nodes, list): nodes = [nodes] for node in nodes: - if check_for_dupes and \ + if no_dupes and \ self.get_node_element(node['hostname']): # node already exists continue - - node_tag = etree.SubElement(self.xml, 'node') - if network: - node_tag.set('component_manager_uuid', network) + + network_tag = self.xml + if 'network' in node: + network = node['network'] + network_tags = self.xml.xpath('//network[@name="%s"]' % network) + if not network_tags: + network_tag = etree.SubElement(self.xml, 'network', name=network) + else: + network_tag = network_tags[0] + + node_tag = etree.SubElement(network_tag, 'node') + if 'network' in node: + node_tag.set('component_manager_id', network) if 'urn' in node: - node_tag.set('compinent_uuid', node['urn']) + node_tag.set('component_id', node['urn']) if 'site_urn' in node: - node_tag.set('site_uuid', node['site_urn']) + node_tag.set('site_id', node['site_urn']) if 'node_id' in node: node_tag.set('node_id', 'n'+str(node['node_id'])) if 'hostname' in node: hostname_tag = etree.SubElement(node_tag, 'hostname').text = node['hostname'] - if 'bw_unallocated' in node: - pass - if 'bw_limit' in node: - pass - + for interface in node['interfaces']: + if 'bwlimit' in node: + bwlimit = etree.SubElement(node_tag, 'bwlimit', units='kbps').tet = str(interface['bwlimit']/1000) + + def add_interfaces(self, interfaces): + pass + + def add_links(self, links): + pass + def add_slivers(self, hostnames, network=None, no_dupes=False): if not isinstance(hostnames, list): hostnames = [hostnames] @@ -197,10 +214,8 @@ class SfaRSpec(RSpec): for hostname in hostnames: node = self.get_node_element(hostname, network) sliver = node.find('sliver') - if sliver: + if sliver != None: node.remove(sliver) - - def add_default_sliver_attribute(self, name, value, network=None): if network: @@ -248,13 +263,42 @@ class SfaRSpec(RSpec): for vlink in vlinks: vlink.getparent().remove(vlink) + + def merge(self, in_rspec): + """ + Merge contents for specified rspec with current rspec + """ + + # just copy over all networks + current_networks = self.get_networks() + rspec = SfaRSpec(rspec=in_rspec) + networks = rspec.get_network_elements() + for network in networks: + current_network = network.get('name') + if not current_network in current_networks: + self.xml.append(network) + current_networks.append(current_network) + + + if __name__ == '__main__': rspec = SfaRSpec() nodes = [ - {'hostname': 'node1.planet-lab.org', + {'network': 'plc', + 'hostname': 'node1.planet-lab.org', + 'site_urn': 'urn:publicid:IDN+plc+authority+cm', + 'node_id': 1, + }, + {'network': 'plc', + 'hostname': 'node2.planet-lab.org', + 'site_urn': 'urn:publicid:IDN+plc+authority+cm', + 'node_id': 1, + }, + {'network': 'ple', + 'hostname': 'node1.planet-lab.eu', 'site_urn': 'urn:publicid:IDN+plc+authority+cm', 'node_id': 1, - } + }, ] rspec.add_nodes(nodes) print rspec