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 GeniRecord
15 from sfa.util.policy import Policy
16 from sfa.util.prefixTree import prefixTree
17 from sfa.util.rspec import *
18 from sfa.util.sfaticket import *
19 from sfa.util.debug import log
20 from sfa.server.registry import Registries
21 from sfa.server.aggregate import Aggregates
22 import sfa.plc.peers as peers
24 def delete_slice(api, hrn, origin_hrn=None):
25 credential = api.getCredential()
26 aggregates = Aggregates(api)
27 for aggregate in aggregates:
29 # request hash is optional so lets try the call without it
31 aggregates[aggregate].delete_slice(credential, hrn, origin_hrn)
34 print >> log, "%s" % (traceback.format_exc())
35 print >> log, "Error calling delete slice at aggregate %s" % aggregate
38 def create_slice(api, hrn, rspec, origin_hrn=None):
41 spec.parseString(rspec)
42 slicename = hrn_to_pl_slicename(hrn)
43 specDict = spec.toDict()
44 if specDict.has_key('RSpec'): specDict = specDict['RSpec']
45 if specDict.has_key('start_time'): start_time = specDict['start_time']
47 if specDict.has_key('end_time'): end_time = specDict['end_time']
51 aggregates = Aggregates(api)
52 credential = api.getCredential()
53 # split the netspecs into individual rspecs
54 netspecs = spec.getDictsByTagName('NetSpec')
55 for netspec in netspecs:
56 net_hrn = netspec['name']
57 resources = {'start_time': start_time, 'end_time': end_time, 'networks': netspec}
58 resourceDict = {'RSpec': resources}
59 tempspec.parseDict(resourceDict)
60 rspecs[net_hrn] = tempspec.toxml()
62 #print "rspecs:", rspecs.keys()
63 #print "aggregates:", aggregates.keys()
64 # send each rspec to the appropriate aggregate/sm
65 for net_hrn in rspecs:
67 # if we are directly connected to the aggregate then we can just
68 # send them the rspec. if not, then we may be connected to an sm
69 # thats connected to the aggregate
70 if net_hrn in aggregates:
71 # send the whloe rspec to the local aggregate
72 if net_hrn in [api.hrn]:
73 aggregates[net_hrn].create_slice(credential, hrn, rspec, \
76 aggregates[net_hrn].create_slice(credential, hrn, \
77 rspecs[net_hrn], origin_hrn)
79 # lets forward this rspec to a sm that knows about the network
80 for aggregate in aggregates:
81 network_found = aggregates[aggregate].get_aggregates(credential, net_hrn)
83 aggregates[aggregate].create_slice(credential, hrn, \
84 rspecs[net_hrn], origin_hrn)
87 print >> log, "Error creating slice %(hrn)s at aggregate %(net_hrn)s" % \
92 def get_ticket(api, slice_hrn, rspec, origin_hrn=None):
94 # get the netspecs contained within the clients rspec
95 client_rspec = RSpec(xml=rspec)
96 netspecs = client_rspec.getDictsByTagName('NetSpec')
98 # create an rspec for each individual rspec
101 for netspec in netspecs:
102 net_hrn = netspec['name']
103 resources = {'start_time': 0, 'end_time': 0 ,
105 resourceDict = {'RSpec': resources}
106 temp_rspec.parseDict(resourceDict)
107 rspecs[net_hrn] = temp_rspec.toxml()
109 # send the rspec to the appropiate aggregate/sm
110 aggregates = Aggregates(api)
111 credential = api.getCredential()
113 for net_hrn in rspecs:
115 # if we are directly connected to the aggregate then we can just
116 # send them the request. if not, then we may be connected to an sm
117 # thats connected to the aggregate
118 if net_hrn in aggregates:
119 ticket = aggregates[net_hrn].get_ticket(credential, slice_hrn, \
120 rspecs[net_hrn], origin_hrn)
121 tickets[net_hrn] = ticket
123 # lets forward this rspec to a sm that knows about the network
124 for agg in aggregates:
125 network_found = aggregates[agg].get_aggregates(credential, net_hrn)
127 ticket = aggregates[aggregate].get_ticket(credential, \
128 slice_hrn, rspecs[net_hrn], origin_hrn)
129 tickets[aggregate] = ticket
131 print >> log, "Error getting ticket for %(slice_hrn)s at aggregate %(net_hrn)s" % \
134 # create a new ticket
135 new_ticket = SfaTicket(subject = slice_hrn)
136 new_ticket.set_gid_caller(api.auth.client_gid)
141 # merge data from aggregate ticket into new ticket
142 for agg_ticket in tickets.values():
143 agg_ticket = SfaTicket(string=agg_ticket)
144 object_gid = agg_ticket.get_gid_object()
145 new_ticket.set_gid_object(object_gid)
146 new_ticket.set_issuer(key=api.key, subject=api.hrn)
147 new_ticket.set_pubkey(object_gid.get_pubkey())
149 #new_ticket.set_attributes(data)
150 tmp_rspec.parseString(agg_ticket.get_rspec())
151 networks.extend([{'NetSpec': tmp_rspec.getDictsByTagName('NetSpec')}])
153 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
154 resources = {'networks': networks, 'start_time': 0, 'duration': 0}
155 resourceDict = {'RSpec': resources}
156 tmp_rspec.parseDict(resourceDict)
157 new_ticket.set_rspec(tmp_rspec.toxml())
161 return new_ticket.save_to_string(save_parents=True)
163 def start_slice(api, hrn):
164 slicename = hrn_to_pl_slicename(hrn)
165 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
167 raise RecordNotFound(hrn)
169 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
170 attribute_id = attreibutes[0]['slice_attribute_id']
171 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
175 def stop_slice(api, hrn):
176 slicename = hrn_to_pl_slicename(hrn)
177 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
179 raise RecordNotFound(hrn)
180 slice_id = slices[0]['slice_id']
181 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
182 attribute_id = attributes[0]['slice_attribute_id']
183 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
186 def reset_slice(api, hrn):
187 # XX not implemented at this interface
191 # XX just import the legacy module and excute that until
192 # we transition the code to this module
193 from sfa.plc.slices import Slices
198 def get_rspec(api, hrn=None, origin_hrn=None):
199 from sfa.plc.nodes import Nodes
200 nodes = Nodes(api, origin_hrn=origin_hrn)
202 rspec = nodes.get_rspec(hrn)
205 rspec = nodes['rspec']
210 Returns the request context required by sfatables. At some point, this mechanism should be changed
211 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
212 return the basic information needed in a dict.
214 def fetch_context(slice_hrn, user_hrn, contexts):
215 base_context = {'sfa':{'user':{'hrn':user_hrn}}}
220 r.parseFile(sys.argv[1])
222 create_slice(None,'plc.princeton.tmacktestslice',rspec)
224 if __name__ == "__main__":