Turn off debugging stmt
[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.util.debug import log
20 from sfa.plc.slices import Slices
21 import sfa.plc.peers as peers
22 from sfa.managers.vini.vini_network import *
23 from sfa.plc.api import SfaAPI
24 from sfa.plc.slices import *
25
26 def delete_slice(api, xrn):
27     hrn, type = urn_to_hrn(xrn)
28     slicename = hrn_to_pl_slicename(hrn)
29     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
30     if not slices:
31         return 1
32     slice = slices[0]
33
34     # determine if this is a peer slice
35     peer = peers.get_peer(api, hrn)
36     if peer:
37         api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
38     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
39     if peer:
40         api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
41     return 1
42
43 def __get_hostnames(nodes):
44     hostnames = []
45     for node in nodes:
46         hostnames.append(node.hostname)
47     return hostnames
48     
49 def create_slice(api, xrn, xml):
50     hrn, type = urn_to_hrn(xrn)
51     peer = None
52
53     """
54     Verify HRN and initialize the slice record in PLC if necessary.
55     """
56     slices = Slices(api)
57     peer = slices.get_peer(hrn)
58     sfa_peer = slices.get_sfa_peer(hrn)
59     registries = Registries(api)
60     registry = registries[api.hrn]
61     credential = api.getCredential()
62     site_id, remote_site_id = slices.verify_site(registry, credential, hrn, 
63                                                  peer, sfa_peer)
64     slice = slices.verify_slice(registry, credential, hrn, site_id, 
65                                 remote_site_id, peer, sfa_peer)
66
67     network = ViniNetwork(api)
68
69     slice = network.get_slice(api, hrn)
70     current = __get_hostnames(slice.get_nodes())
71
72     network.addRSpec(xml, "/var/www/html/schemas/vini.rng")
73     #network.addRSpec(xml, "/root/SVN/sfa/trunk/sfa/managers/vini/vini.rng")
74     request = __get_hostnames(network.nodesWithSlivers())
75     
76     # remove nodes not in rspec
77     deleted_nodes = list(set(current).difference(request))
78
79     # add nodes from rspec
80     added_nodes = list(set(request).difference(current))
81
82     if peer:
83         api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
84
85     api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes) 
86     api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
87
88     network.updateSliceTags()
89
90     if peer:
91         api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer, 
92                                      slice.peer_id)
93
94     # print network.toxml()
95
96     return True
97
98
99 def get_ticket(api, xrn, rspec, origin_hrn=None):
100     slice_hrn, type = urn_to_hrn(xrn)
101     # the the slice record
102     registries = Registries(api)
103     registry = registries[api.hrn]
104     credential = api.getCredential()
105     records = registry.resolve(credential, xrn)
106     
107     # make sure we get a local slice record
108     record = None  
109     for tmp_record in records:
110         if tmp_record['type'] == 'slice' and \
111            not tmp_record['peer_authority']:
112             record = SliceRecord(dict=tmp_record)
113     if not record:
114         raise RecordNotFound(slice_hrn)
115
116     # get sliver info
117     slivers = Slices(api).get_slivers(slice_hrn)
118     if not slivers:
119         raise SliverDoesNotExist(slice_hrn)
120     
121     # get initscripts
122     initscripts = []
123     data = {
124         'timestamp': int(time.time()),
125         'initscripts': initscripts,
126         'slivers': slivers
127     }
128
129     # create the ticket
130     object_gid = record.get_gid_object()
131     new_ticket = SfaTicket(subject = object_gid.get_subject())
132     new_ticket.set_gid_caller(api.auth.client_gid)
133     new_ticket.set_gid_object(object_gid)
134     new_ticket.set_issuer(key=api.key, subject=api.hrn)
135     new_ticket.set_pubkey(object_gid.get_pubkey())
136     new_ticket.set_attributes(data)
137     new_ticket.set_rspec(rspec)
138     #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
139     new_ticket.encode()
140     new_ticket.sign()
141     
142     return new_ticket.save_to_string(save_parents=True)
143
144 def start_slice(api, xrn):
145     hrn, type = urn_to_hrn(xrn)
146     slicename = hrn_to_pl_slicename(hrn)
147     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
148     if not slices:
149         raise RecordNotFound(hrn)
150     slice_id = slices[0]
151     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
152     attribute_id = attreibutes[0]['slice_attribute_id']
153     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
154
155     return 1
156  
157 def stop_slice(api, xrn):
158     hrn, type = urn_to_hrn(xrn)
159     slicename = hrn_to_pl_slicename(hrn)
160     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
161     if not slices:
162         raise RecordNotFound(hrn)
163     slice_id = slices[0]['slice_id']
164     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
165     attribute_id = attributes[0]['slice_attribute_id']
166     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
167     return 1
168
169 def reset_slice(api, xrn):
170     # XX not implemented at this interface
171     return 1
172
173 def get_slices(api):
174     # XX just import the legacy module and excute that until
175     # we transition the code to this module
176     from sfa.plc.slices import Slices
177     slices = Slices(api)
178     slices.refresh()
179     return [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slices['hrn']]
180      
181  
182 def get_rspec(api, xrn=None, origin_hrn=None):
183     hrn, type = urn_to_hrn(xrn)
184     network = ViniNetwork(api)
185     if (hrn):
186         network.get_slice(api, hrn)
187         if slice:
188             network.addSlice()
189
190     return network.toxml()
191
192 """
193 Returns the request context required by sfatables. At some point, this
194 mechanism should be changed to refer to "contexts", which is the
195 information that sfatables is requesting. But for now, we just return
196 the basic information needed in a dict.
197 """
198 def fetch_context(slice_xrn, user_xrn, contexts):
199     slice_hrn, type = urn_to_hrn(slice_xrn)
200     user_hrn, type = urn_to_hrn(user_xrn)
201     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
202     return base_context
203
204 def main():
205     api = SfaAPI()
206     """
207     #rspec = get_rspec(api, None, None)
208     rspec = get_rspec(api, "plc.princeton.iias", None)
209     print rspec
210     """
211     f = open(sys.argv[1])
212     xml = f.read()
213     f.close()
214     create_slice(api, "plc.princeton.iias", xml)
215
216 if __name__ == "__main__":
217     main()