added support for urn name format. urn is the default name format used over the wire
[sfa.git] / sfa / managers / aggregate_manager_pl.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
23 def delete_slice(api, xrn):
24     hrn, type = urn_to_hrn(xrn)
25     slicename = hrn_to_pl_slicename(hrn)
26     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
27     if not slices:
28         return 1
29     slice = slices[0]
30
31     # determine if this is a peer slice
32     peer = peers.get_peer(api, hrn)
33     if peer:
34         api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
35     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
36     if peer:
37         api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
38     return 1
39
40 def create_slice(api, xrn, rspec):
41     hrn, type = urn_to_hrn(xrn)
42     # XX just import the legacy module and excute that until
43     # we transition the code to this module
44     from sfa.plc.slices import Slices
45     slices = Slices(api)
46     slices.create_slice_aggregate(hrn, rspec)
47
48 def get_ticket(api, xrn, rspec, origin_hrn=None):
49     slice_hrn, type = urn_to_hrn(xrn)
50     # the the slice record
51     registries = Registries(api)
52     registry = registries[api.hrn]
53     credential = api.getCredential()
54     records = registry.resolve(credential, xrn)
55     
56     # make sure we get a local slice record
57     record = None  
58     for tmp_record in records:
59         if tmp_record['type'] == 'slice' and \
60            not tmp_record['peer_authority']:
61             record = SliceRecord(dict=tmp_record)
62     if not record:
63         raise RecordNotFound(slice_hrn)
64
65     # get sliver info
66     slivers = Slices(api).get_slivers(slice_hrn)
67     if not slivers:
68         raise SliverDoesNotExist(slice_hrn)
69     
70     # get initscripts
71     initscripts = None
72     data = {
73         'timestamp': int(time.time()),
74         'initscripts': initscripts,
75         'slivers': slivers
76     }
77
78     # create the ticket
79     object_gid = record.get_gid_object()
80     new_ticket = SfaTicket(subject = object_gid.get_subject())
81     new_ticket.set_gid_caller(api.auth.client_gid)
82     new_ticket.set_gid_object(object_gid)
83     new_ticket.set_issuer(key=api.key, subject=api.hrn)
84     new_ticket.set_pubkey(object_gid.get_pubkey())
85     new_ticket.set_attributes(data)
86     new_ticket.set_rspec(rspec)
87     #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
88     new_ticket.encode()
89     new_ticket.sign()
90     
91     return new_ticket.save_to_string(save_parents=True)
92
93 def start_slice(api, xrn):
94     hrn, type = urn_to_hrn(xrn)
95     slicename = hrn_to_pl_slicename(hrn)
96     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
97     if not slices:
98         raise RecordNotFound(hrn)
99     slice_id = slices[0]
100     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
101     attribute_id = attreibutes[0]['slice_attribute_id']
102     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
103
104     return 1
105  
106 def stop_slice(api, xrn):
107     hrn, type = urn_to_hrn(xrn)
108     slicename = hrn_to_pl_slicename(hrn)
109     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
110     if not slices:
111         raise RecordNotFound(hrn)
112     slice_id = slices[0]['slice_id']
113     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
114     attribute_id = attributes[0]['slice_attribute_id']
115     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
116     return 1
117
118 def reset_slice(api, xrn):
119     # XX not implemented at this interface
120     return 1
121
122 def get_slices(api):
123     # XX just import the legacy module and excute that until
124     # we transition the code to this module
125     from sfa.plc.slices import Slices
126     slices = Slices(api)
127     slices.refresh()
128     return [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slices['hrn']]
129      
130  
131 def get_rspec(api, xrn=None, origin_hrn=None):
132     from sfa.plc.nodes import Nodes
133     nodes = Nodes(api, origin_hrn=origin_hrn)
134     if hrn:
135         rspec = nodes.get_rspec(xrn)
136     else:
137         nodes.refresh()
138         rspec = nodes['rspec'] 
139
140     return rspec
141
142 """
143 Returns the request context required by sfatables. At some point, this mechanism should be changed
144 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
145 return the basic information needed in a dict.
146 """
147 def fetch_context(slice_xrn, user_xrn, contexts):
148     slice_hrn = urn_to_hrn(slice_xrn)[0]
149     user_hrn = urn_to_hrn(user_xrn)[0]
150     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
151     return base_context
152
153 def main():
154     r = RSpec()
155     r.parseFile(sys.argv[1])
156     rspec = r.toDict()
157     create_slice(None,'plc.princeton.tmacktestslice',rspec)
158
159 if __name__ == "__main__":
160     main()