namespace module is gone, plxrn provides PL-specific translations
[sfa.git] / sfa / managers / aggregate_manager_vini.py
1 import datetime
2 import time
3 import traceback
4 import sys
5
6 from types import StringTypes
7 from sfa.util.xrn import urn_to_hrn
8 from sfa.util.plxrn import hrn_to_pl_slicename
9 from sfa.util.rspec import *
10 from sfa.util.specdict import *
11 from sfa.util.faults import *
12 from sfa.util.record import SfaRecord
13 from sfa.util.policy import Policy
14 from sfa.util.record import *
15 from sfa.util.sfaticket import SfaTicket
16 from sfa.server.registry import Registries
17 from sfa.plc.slices import Slices
18 import sfa.plc.peers as peers
19 from sfa.managers.vini.vini_network import *
20 from sfa.plc.api import SfaAPI
21 from sfa.plc.slices import *
22 from sfa.managers.aggregate_manager_pl import __get_registry_objects, __get_hostnames
23
24 # VINI aggregate is almost identical to PLC aggregate for many operations, 
25 # so lets just import the methods form the PLC manager
26
27 from sfa.managers.aggregate_manager_pl import (
28 start_slice, stop_slice, renew_slice, reset_slice, get_slices, get_ticket)
29
30
31 def get_version():
32     version = {}
33     version['geni_api'] = 1
34     version['sfa'] = 1
35     return version
36
37 def slice_status(api, slice_xrn, creds):
38     result = {}
39     result['geni_urn'] = slice_xrn
40     result['geni_status'] = 'unknown'
41     result['geni_resources'] = {}
42     return result
43
44
45 def delete_slice(api, xrn, creds):
46     hrn, type = urn_to_hrn(xrn)
47     slicename = hrn_to_pl_slicename(hrn)
48     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
49     if not slices:
50         return 1
51     slice = slices[0]
52
53     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
54     return 1
55
56 def create_slice(api, xrn, creds, xml, users):
57     """
58     Verify HRN and initialize the slice record in PLC if necessary.
59     """
60
61     hrn, type = urn_to_hrn(xrn)
62     peer = None
63     reg_objects = __get_registry_objects(slice_xrn, creds, users)
64     slices = Slices(api)
65     peer = slices.get_peer(hrn)
66     sfa_peer = slices.get_sfa_peer(hrn)
67     registries = Registries(api)
68     registry = registries[api.hrn]
69     credential = api.getCredential()
70     site_id, remote_site_id = slices.verify_site(registry, credential, hrn, 
71                                                  peer, sfa_peer, reg_objects)
72     slice = slices.verify_slice(registry, credential, hrn, site_id, 
73                                 remote_site_id, peer, sfa_peer, reg_objects)
74
75     network = ViniNetwork(api)
76
77     slice = network.get_slice(api, hrn)
78     current = __get_hostnames(slice.get_nodes())
79
80     network.addRSpec(xml, "/var/www/html/schemas/vini.rng")
81     #network.addRSpec(xml, "/root/SVN/sfa/trunk/sfa/managers/vini/vini.rng")
82     request = __get_hostnames(network.nodesWithSlivers())
83     
84     # remove nodes not in rspec
85     deleted_nodes = list(set(current).difference(request))
86
87     # add nodes from rspec
88     added_nodes = list(set(request).difference(current))
89
90     api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes) 
91     api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
92     network.updateSliceTags()
93
94     # print network.toxml()
95
96     return True
97
98 def get_rspec(api, creds, options):
99     # get slice's hrn from options
100     xrn = options.get('geni_slice_urn', '')
101     hrn, type = urn_to_hrn(xrn)
102     
103     # look in cache first
104     if api.cache and not xrn:
105         rspec = api.cache.get('nodes')
106         if rspec:
107             return rspec
108
109     network = ViniNetwork(api)
110     if (hrn):
111         if network.get_slice(api, hrn):
112             network.addSlice()
113
114     rspec =  network.toxml()
115
116     # cache the result
117     if api.cache and not xrn:
118         api.cache.add('nodes', rspec)
119
120     return rspec
121
122 def main():
123     api = SfaAPI()
124     """
125     #rspec = get_rspec(api, None, None)
126     rspec = get_rspec(api, "plc.princeton.iias", None)
127     print rspec
128     """
129     f = open(sys.argv[1])
130     xml = f.read()
131     f.close()
132     create_slice(api, "plc.princeton.iias", xml)
133
134 if __name__ == "__main__":
135     main()