04f4aa6e146a83941edd735d9ea454224ac96dfb
[sfa.git] / sfa / rspecs / elements / versions / nitosv1Channel.py
1 from sfa.util.sfalogging import logger
2 from sfa.util.xml import XpathFilter
3 from sfa.util.xrn import Xrn
4
5 from sfa.rspecs.elements.element import Element
6 from sfa.rspecs.elements.node import NodeElement
7 from sfa.rspecs.elements.sliver import Sliver
8 from sfa.rspecs.elements.location import Location
9 from sfa.rspecs.elements.hardware_type import HardwareType
10 from sfa.rspecs.elements.disk_image import DiskImage
11 from sfa.rspecs.elements.interface import Interface
12 from sfa.rspecs.elements.bwlimit import BWlimit
13 from sfa.rspecs.elements.pltag import PLTag
14 from sfa.rspecs.elements.versions.nitosv1Sliver import NITOSv1Sliver
15 from sfa.rspecs.elements.versions.nitosv1PLTag import NITOSv1PLTag
16 from sfa.rspecs.elements.versions.pgv2Services import PGv2Services
17 from sfa.rspecs.elements.lease import Lease
18 from sfa.rspecs.elements.spectrum import Spectrum
19 from sfa.rspecs.elements.channel import Channel
20
21
22 class NITOSv1Channel:
23
24     @staticmethod
25     def add_channels(xml, channels):
26
27         network_elems = xml.xpath('//network')
28         if len(network_elems) > 0:
29             network_elem = network_elems[0]
30         elif len(channels) > 0:
31             # dirty hack that handles no resource manifest rspec
32             network_urn = "omf"
33             network_elem = xml.add_element('network', name=network_urn)
34         else:
35             network_elem = xml
36
37 #        spectrum_elems = xml.xpath('//spectrum')
38 #        spectrum_elem = xml.add_element('spectrum')
39
40 #        if len(spectrum_elems) > 0:
41 #            spectrum_elem = spectrum_elems[0]
42 #        elif len(channels) > 0:
43 #            spectrum_elem = xml.add_element('spectrum')
44 #        else:
45 #            spectrum_elem = xml
46
47         spectrum_elem = network_elem.add_instance('spectrum', [])
48
49         channel_elems = []
50         for channel in channels:
51             channel_fields = ['channel_num',
52                               'frequency', 'standard', 'component_id']
53             channel_elem = spectrum_elem.add_instance(
54                 'channel', channel, channel_fields)
55             channel_elems.append(channel_elem)
56
57     @staticmethod
58     def get_channels(xml, filter=None):
59         if filter is None:
60             filter = {}
61         xpath = '//channel%s | //default:channel%s' % (
62             XpathFilter.xpath(filter), XpathFilter.xpath(filter))
63         channel_elems = xml.xpath(xpath)
64         return NITOSv1Channel.get_channel_objs(channel_elems)
65
66     @staticmethod
67     def get_channel_objs(channel_elems):
68         channels = []
69         for channel_elem in channel_elems:
70             channel = Channel(channel_elem.attrib, channel_elem)
71             channel['channel_num'] = channel_elem.attrib['channel_num']
72             channel['frequency'] = channel_elem.attrib['frequency']
73             channel['standard'] = channel_elem.attrib['standard']
74             channel['component_id'] = channel_elem.attrib['component_id']
75
76             channels.append(channel)
77         return channels