omf.py 4.04 KB
Newer Older
Quan Zhou's avatar
Quan Zhou committed
1
import dateutil.parser
2
from datetime import datetime, timedelta, timezone
Quan Zhou's avatar
Quan Zhou committed
3
from myslicelib.util.sfaparser import SfaParser
4

Quan Zhou's avatar
Quan Zhou committed
5
class Omf(SfaParser):
6

7
    def lease_parser(self, rspec, source):
Quan Zhou's avatar
Quan Zhou committed
8
9
10
11
12
13
14
15
16
        result = []
        el = rspec.find('{http://www.geni.net/resources/rspec/3}node')

        leases = rspec.findall('{http://nitlab.inf.uth.gr/schema/sfa/rspec/1}lease')

        if not leases:
            return result

        nodes = rspec.findall('{http://www.geni.net/resources/rspec/3}node')    
17
        epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
Quan Zhou's avatar
Quan Zhou committed
18
19
        for lease in leases:
            dt = dateutil.parser.parse(lease.attrib['valid_from'])
20
            start_time = (dt - epoch) // timedelta(seconds=1)
Quan Zhou's avatar
Quan Zhou committed
21
            dt = dateutil.parser.parse(lease.attrib['valid_until'])
22
            end_time = (dt - epoch) // timedelta(seconds=1)
Quan Zhou's avatar
Quan Zhou committed
23
24
25
            duration = end_time - start_time
            l = {            
                'lease_id': lease.attrib['id'],
26
                'parser':self.__class__.__name__.lower(),
Quan Zhou's avatar
Quan Zhou committed
27
28
29
                'start_time': start_time,
                'end_time': end_time,
                'duration': duration,
Loic Baron's avatar
Loic Baron committed
30
                'resources': [],
31
                'testbed': source,
Quan Zhou's avatar
Quan Zhou committed
32
33
34
35
36
37
38
39
            }        
            for node in nodes:
                leases_in_node = node.findall('{http://nitlab.inf.uth.gr/schema/sfa/rspec/1}lease_ref')
                if not leases_in_node:
                    continue
                else:
                    for lease_in_node in leases_in_node:
                        if l['lease_id'] == lease_in_node.attrib['id_ref']:
Loic Baron's avatar
Loic Baron committed
40
                            l['resources'].append(node.attrib['component_id'])
Quan Zhou's avatar
Quan Zhou committed
41
            result.append(l)
Loic Baron's avatar
Loic Baron committed
42
43
        return result

44
    def resource_parser(self, rspec, source):
Quan Zhou's avatar
Quan Zhou committed
45
46
47
        result = []
        el = rspec.find('{http://www.geni.net/resources/rspec/3}node')

Loic Baron's avatar
Loic Baron committed
48
49
50
51
52
        for node in rspec.findall('{http://www.geni.net/resources/rspec/3}node'):
            resource = {
                'type' : 'node',
                'id': node.attrib['component_id'],
                'name': node.attrib['component_name'],
53
                'manager': node.attrib['component_manager_id'],
54
                'testbed': source,
Loic Baron's avatar
Loic Baron committed
55
56
57
58
                'exclusive': node.attrib['exclusive'],
                'hardware_types': [],
                'interfaces': [],
                'sliver_types': [],
59
                'services': [],
60
                'parser':self.__class__.__name__.lower(),
Loic Baron's avatar
Loic Baron committed
61
62
63
                'technologies':['Wireless','Wifi'],
            }
            for element in list(node):
64
65
                if 'hardware_type' in element.tag:
                    resource['hardware_types'].append(element.attrib['name'])
Loic Baron's avatar
Loic Baron committed
66
67
                if 'location' in element.tag:
                    resource['location'] = element.attrib
68
69
70

                if 'available' in element.tag:
                    resource['available'] = element.attrib['now']
71
72
73
74

                if 'services' in element.tag:
                    login = element.find('{http://www.geni.net/resources/rspec/3}login')
                    resource['services'].append({'login':login.attrib})
Loic Baron's avatar
Loic Baron committed
75
76
77
78

            if 'location' not in resource or resource['location'] is None:
                    resource['location'] = self.get_location(node.attrib['component_id'])

Pauline's avatar
Pauline committed
79
80
81
            if len(resource['hardware_types']) == 0:
                resource['hardware_types'].append('PC-Icarus')
            
Loic Baron's avatar
Loic Baron committed
82
            result.append(resource)
Quan Zhou's avatar
Quan Zhou committed
83
84
85
86

            # TODO channel?
        return result

87
88
89
90
91
92
93
    def get_location(self, id):
        if 'omf:nitos' in id:
            return {'city':'Volos', 'country':'Greece'}
        if 'omf:paris' in id:
            return {'city':'Paris', 'country':'France'}
        return None

Quan Zhou's avatar
Quan Zhou committed
94
95
96
97
98
99
100
101
102
103
# node

#  'component_id="urn:publicid:IDN+omf:paris.fit-nitos.fr+interface+node41:if1" '
#  'component_name="node41:if1" role="experimental"/>\n'
#  '  </node>\n'
# channel

#  '  <ol:channel '
#  'component_id="urn:publicid:IDN+omf:paris.fit-nitos.fr+channel+channel1" '
#  'component_manager_id="urn:publicid:IDN+omf:paris.fit-nitos.fr+authority+cm" '
Loic Baron's avatar
Loic Baron committed
104
#  'component_name="channel1" frequency="2.412GHz"/>\n'