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 $
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
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})
31 # determine if this is a peer slice
32 peer = peers.get_peer(api, hrn)
34 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
35 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
37 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
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
46 slices.create_slice_aggregate(hrn, rspec)
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)
56 # make sure we get a local slice record
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)
63 raise RecordNotFound(slice_hrn)
66 slivers = Slices(api).get_slivers(slice_hrn)
68 raise SliverDoesNotExist(slice_hrn)
73 'timestamp': int(time.time()),
74 'initscripts': initscripts,
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))
91 return new_ticket.save_to_string(save_parents=True)
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'])
98 raise RecordNotFound(hrn)
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" )
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'])
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")
118 def reset_slice(api, xrn):
119 # XX not implemented at this interface
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
128 return [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slices['hrn']]
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)
135 rspec = nodes.get_rspec(xrn)
138 rspec = nodes['rspec']
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.
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}}}
155 r.parseFile(sys.argv[1])
157 create_slice(None,'plc.princeton.tmacktestslice',rspec)
159 if __name__ == "__main__":