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
22 from sfa.plc.network import *
23 from sfa.plc.api import SfaAPI
24 from sfa.plc.slices import *
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})
34 # determine if this is a peer slice
35 peer = peers.get_peer(api, hrn)
37 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
38 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
40 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
43 def __get_hostnames(nodes):
46 hostnames.append(node.hostname)
49 def create_slice(api, xrn, xml):
50 hrn, type = urn_to_hrn(xrn)
54 Verify HRN and initialize the slice record in PLC if necessary.
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,
64 slice = slices.verify_slice(registry, credential, hrn, site_id,
65 remote_site_id, peer, sfa_peer)
67 network = Network(api)
69 slice = network.get_slice(api, hrn)
70 current = __get_hostnames(slice.get_nodes())
72 network.addRSpec(xml, "/var/www/html/schemas/pl.rng")
73 request = __get_hostnames(network.nodesWithSlivers())
75 # remove nodes not in rspec
76 deleted_nodes = list(set(current).difference(request))
78 # add nodes from rspec
79 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 registries = Registries(api)
102 registry = registries[api.hrn]
103 credential = api.getCredential()
104 records = registry.resolve(credential, xrn)
106 # make sure we get a local slice record
108 for tmp_record in records:
109 if tmp_record['type'] == 'slice' and \
110 not tmp_record['peer_authority']:
111 record = SliceRecord(dict=tmp_record)
113 raise RecordNotFound(slice_hrn)
116 slivers = Slices(api).get_slivers(slice_hrn)
118 raise SliverDoesNotExist(slice_hrn)
123 'timestamp': int(time.time()),
124 'initscripts': initscripts,
129 object_gid = record.get_gid_object()
130 new_ticket = SfaTicket(subject = object_gid.get_subject())
131 new_ticket.set_gid_caller(api.auth.client_gid)
132 new_ticket.set_gid_object(object_gid)
133 new_ticket.set_issuer(key=api.key, subject=api.hrn)
134 new_ticket.set_pubkey(object_gid.get_pubkey())
135 new_ticket.set_attributes(data)
136 new_ticket.set_rspec(rspec)
137 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
141 return new_ticket.save_to_string(save_parents=True)
143 def start_slice(api, xrn):
144 hrn, type = urn_to_hrn(xrn)
145 slicename = hrn_to_pl_slicename(hrn)
146 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
148 raise RecordNotFound(hrn)
150 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
151 attribute_id = attreibutes[0]['slice_attribute_id']
152 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
156 def stop_slice(api, xrn):
157 hrn, type = urn_to_hrn(xrn)
158 slicename = hrn_to_pl_slicename(hrn)
159 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
161 raise RecordNotFound(hrn)
162 slice_id = slices[0]['slice_id']
163 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
164 attribute_id = attributes[0]['slice_attribute_id']
165 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
168 def reset_slice(api, xrn):
169 # XX not implemented at this interface
173 # XX just import the legacy module and excute that until
174 # we transition the code to this module
175 from sfa.plc.slices import Slices
178 return [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slices['hrn']]
181 def get_rspec(api, xrn=None, origin_hrn=None):
182 hrn, type = urn_to_hrn(xrn)
183 network = Network(api)
185 network.get_slice(api, hrn)
189 return network.toxml()
192 Returns the request context required by sfatables. At some point, this
193 mechanism should be changed to refer to "contexts", which is the
194 information that sfatables is requesting. But for now, we just return
195 the basic information needed in a dict.
197 def fetch_context(slice_xrn, user_xrn, contexts):
198 slice_hrn, type = urn_to_hrn(slice_xrn)
199 user_hrn, type = urn_to_hrn(user_xrn)
200 base_context = {'sfa':{'user':{'hrn':user_hrn}}}
206 rspec = get_rspec(api, "plc.princeton.sapan", None)
207 #rspec = get_rspec(api, "plc.princeton.coblitz", None)
208 #rspec = get_rspec(api, "plc.pl.sirius", None)
211 f = open(sys.argv[1])
214 create_slice(api, "plc.princeton.sapan", xml)
216 if __name__ == "__main__":