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.util.debug import log
19 from sfa.plc.slices import Slices
20 import sfa.plc.peers as peers
21 from sfa.plc.network import *
22 from sfa.plc.api import SfaAPI
23 from sfa.plc.slices import *
25 def delete_slice(api, xrn):
26 hrn, type = urn_to_hrn(xrn)
27 slicename = hrn_to_pl_slicename(hrn)
28 slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
33 # determine if this is a peer slice
34 peer = peers.get_peer(api, hrn)
36 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
37 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
39 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
42 def __get_hostnames(nodes):
45 hostnames.append(node.hostname)
48 def create_slice(api, xrn, xml):
50 Verify HRN and initialize the slice record in PLC if necessary.
53 hrn, type = urn_to_hrn(xrn)
56 peer = slices.get_peer(hrn)
57 sfa_peer = slices.get_sfa_peer(hrn)
58 registry = api.registries[api.hrn]
59 credential = api.getCredential()
60 site_id, remote_site_id = slices.verify_site(registry, credential, hrn,
62 slice = slices.verify_slice(registry, credential, hrn, site_id,
63 remote_site_id, peer, sfa_peer)
65 network = Network(api)
67 slice = network.get_slice(api, hrn)
68 current = __get_hostnames(slice.get_nodes())
70 network.addRSpec(xml, api.config.SFA_AGGREGATE_RSPEC_SCHEMA)
71 request = __get_hostnames(network.nodesWithSlivers())
73 # remove nodes not in rspec
74 deleted_nodes = list(set(current).difference(request))
76 # add nodes from rspec
77 added_nodes = list(set(request).difference(current))
82 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
84 api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes)
85 api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
87 network.updateSliceTags()
90 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer,
93 # print network.toxml()
98 def get_ticket(api, xrn, rspec, origin_hrn=None):
99 slice_hrn, type = urn_to_hrn(xrn)
100 # the the slice record
101 registry = api.registries[api.hrn]
102 credential = api.getCredential()
103 records = registry.resolve(credential, xrn)
105 # make sure we get a local slice record
107 for tmp_record in records:
108 if tmp_record['type'] == 'slice' and \
109 not tmp_record['peer_authority']:
110 record = SliceRecord(dict=tmp_record)
112 raise RecordNotFound(slice_hrn)
115 slivers = Slices(api).get_slivers(slice_hrn)
117 raise SliverDoesNotExist(slice_hrn)
122 'timestamp': int(time.time()),
123 'initscripts': initscripts,
128 object_gid = record.get_gid_object()
129 new_ticket = SfaTicket(subject = object_gid.get_subject())
130 new_ticket.set_gid_caller(api.auth.client_gid)
131 new_ticket.set_gid_object(object_gid)
132 new_ticket.set_issuer(key=api.key, subject=api.hrn)
133 new_ticket.set_pubkey(object_gid.get_pubkey())
134 new_ticket.set_attributes(data)
135 new_ticket.set_rspec(rspec)
136 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
140 return new_ticket.save_to_string(save_parents=True)
142 def start_slice(api, xrn):
143 hrn, type = urn_to_hrn(xrn)
144 slicename = hrn_to_pl_slicename(hrn)
145 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
147 raise RecordNotFound(hrn)
149 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
150 attribute_id = attributes[0]['slice_attribute_id']
151 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
155 def stop_slice(api, xrn):
156 hrn, type = urn_to_hrn(xrn)
157 slicename = hrn_to_pl_slicename(hrn)
158 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
160 raise RecordNotFound(hrn)
161 slice_id = slices[0]['slice_id']
162 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
163 attribute_id = attributes[0]['slice_attribute_id']
164 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
167 def reset_slice(api, xrn):
168 # XX not implemented at this interface
172 # look in cache first
174 slices = api.cache.get('slices')
179 slices = api.plshell.GetSlices(api.plauth, {'peer_id': None}, ['name'])
180 slice_hrns = [slicename_to_hrn(api.hrn, slice['name']) for slice in slices]
181 slice_urns = [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slice_hrns]
185 api.cache.add('slices', slice_urns)
189 def get_rspec(api, xrn=None, origin_hrn=None):
190 # look in cache first
191 if api.cache and not xrn:
192 rspec = api.cache.get('nodes')
196 hrn, type = urn_to_hrn(xrn)
197 network = Network(api)
199 if network.get_slice(api, hrn):
202 rspec = network.toxml()
205 if api.cache and not xrn:
206 api.cache.add('nodes', rspec)
211 Returns the request context required by sfatables. At some point, this
212 mechanism should be changed to refer to "contexts", which is the
213 information that sfatables is requesting. But for now, we just return
214 the basic information needed in a dict.
216 def fetch_context(slice_xrn, user_xrn, contexts):
217 slice_hrn, type = urn_to_hrn(slice_xrn)
218 user_hrn, type = urn_to_hrn(user_xrn)
219 base_context = {'sfa':{'user':{'hrn':user_hrn}, 'slice':{'hrn':slice_hrn}}}
225 rspec = get_rspec(api, "plc.princeton.sapan", None)
226 #rspec = get_rspec(api, "plc.princeton.coblitz", None)
227 #rspec = get_rspec(api, "plc.pl.sirius", None)
230 f = open(sys.argv[1])
233 create_slice(api, "plc.princeton.sapan", xml)
235 if __name__ == "__main__":