removed another bunch of references to geni
[sfa.git] / sfa / managers / slice_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.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
23
24 def delete_slice(api, hrn, origin_hrn=None):
25     credential = api.getCredential()
26     aggregates = Aggregates(api)
27     for aggregate in aggregates:
28         success = False
29         # request hash is optional so lets try the call without it
30         try:
31             aggregates[aggregate].delete_slice(credential, hrn, origin_hrn)
32             success = True
33         except:
34             print >> log, "%s" % (traceback.format_exc())
35             print >> log, "Error calling delete slice at aggregate %s" % aggregate
36     return 1
37
38 def create_slice(api, hrn, rspec, origin_hrn=None):
39     spec = RSpec()
40     tempspec = RSpec()
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']
46     else: start_time = 0
47     if specDict.has_key('end_time'): end_time = specDict['end_time']
48     else: end_time = 0
49
50     rspecs = {}
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()
61
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:
66         try:
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, \
74                                 origin_hrn)
75                 else:
76                     aggregates[net_hrn].create_slice(credential, hrn, \
77                                 rspecs[net_hrn], origin_hrn)
78             else:
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)
82                     if network_found:
83                         aggregates[aggregate].create_slice(credential, hrn, \
84                                     rspecs[net_hrn], origin_hrn)
85
86         except:
87             print >> log, "Error creating slice %(hrn)s at aggregate %(net_hrn)s" % \
88                            locals()
89             traceback.print_exc()
90     return 1
91
92 def get_ticket(api, slice_hrn, rspec, origin_hrn=None):
93     
94     # get the netspecs contained within the clients rspec
95     client_rspec = RSpec(xml=rspec)
96     netspecs = client_rspec.getDictsByTagName('NetSpec')
97     
98     # create an rspec for each individual rspec 
99     rspecs = {}
100     temp_rspec = RSpec()
101     for netspec in netspecs:
102         net_hrn = netspec['name']
103         resources = {'start_time': 0, 'end_time': 0 , 
104                      'network': netspec}
105         resourceDict = {'RSpec': resources}
106         temp_rspec.parseDict(resourceDict)
107         rspecs[net_hrn] = temp_rspec.toxml() 
108     
109     # send the rspec to the appropiate aggregate/sm
110     aggregates = Aggregates(api)
111     credential = api.getCredential()
112     tickets = {}
113     for net_hrn in rspecs:    
114         try:
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
122             else:
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)
126                     if network_found:
127                         ticket = aggregates[aggregate].get_ticket(credential, \
128                                         slice_hrn, rspecs[net_hrn], origin_hrn)
129                         tickets[aggregate] = ticket
130         except:
131             print >> log, "Error getting ticket for %(slice_hrn)s at aggregate %(net_hrn)s" % \
132                            locals()
133             
134     # create a new ticket
135     new_ticket = SfaTicket(subject = slice_hrn)
136     new_ticket.set_gid_caller(api.auth.client_gid)
137    
138     tmp_rspec = RSpec()
139     networks = []
140     valid_data = {} 
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())
148         
149         #new_ticket.set_attributes(data)
150         tmp_rspec.parseString(agg_ticket.get_rspec())
151         networks.extend([{'NetSpec': tmp_rspec.getDictsByTagName('NetSpec')}])
152     
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())
158         
159     new_ticket.encode()
160     new_ticket.sign()          
161     return new_ticket.save_to_string(save_parents=True)
162
163 def start_slice(api, hrn):
164     slicename = hrn_to_pl_slicename(hrn)
165     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
166     if not slices:
167         raise RecordNotFound(hrn)
168     slice_id = slices[0]
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" )
172
173     return 1
174  
175 def stop_slice(api, hrn):
176     slicename = hrn_to_pl_slicename(hrn)
177     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
178     if not slices:
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")
184     return 1
185
186 def reset_slice(api, hrn):
187     # XX not implemented at this interface
188     return 1
189
190 def get_slices(api):
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
194     slices = Slices(api)
195     slices.refresh()
196     return slices['hrn']
197      
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)
201     if hrn:
202         rspec = nodes.get_rspec(hrn)
203     else:
204         nodes.refresh()
205         rspec = nodes['rspec']
206
207     return rspec
208
209 """
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.
213 """
214 def fetch_context(slice_hrn, user_hrn, contexts):
215     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
216     return base_context
217
218 def main():
219     r = RSpec()
220     r.parseFile(sys.argv[1])
221     rspec = r.toDict()
222     create_slice(None,'plc.princeton.tmacktestslice',rspec)
223
224 if __name__ == "__main__":
225     main()
226