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