passing the hrn of the initial caller instead of its credentail
[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.misc 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.debug import log
19 from sfa.server.registry import Registries
20 from sfa.server.aggregate import Aggregates
21 import sfa.plc.peers as peers
22
23 def delete_slice(api, hrn, origin_hrn=None):
24     credential = api.getCredential()
25     aggregates = Aggregates(api)
26     for aggregate in aggregates:
27         success = False
28         # request hash is optional so lets try the call without it
29         try:
30             request_hash=None
31             aggregates[aggregate].delete_slice(credential, hrn, request_hash, 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
37         # try sending the request hash if the previous call failed
38         if not success:
39             try:
40                 arg_list = [credential, hrn]
41                 request_hash = api.key.compute_hash(arg_list)
42                 aggregates[aggregate].delete_slice(credential, hrn, request_hash, origin_hrn)
43                 success = True
44             except:
45                 print >> log, "%s" % (traceback.format_exc())
46                 print >> log, "Error calling list nodes at aggregate %s" % aggregate
47     return 1
48
49 def create_slice(api, hrn, rspec, origin_hrn=None):
50     spec = RSpec()
51     tempspec = RSpec()
52     spec.parseString(rspec)
53     slicename = hrn_to_pl_slicename(hrn)
54     specDict = spec.toDict()
55     if specDict.has_key('RSpec'): specDict = specDict['RSpec']
56     if specDict.has_key('start_time'): start_time = specDict['start_time']
57     else: start_time = 0
58     if specDict.has_key('end_time'): end_time = specDict['end_time']
59     else: end_time = 0
60
61     rspecs = {}
62     aggregates = Aggregates(api)
63     credential = api.getCredential()
64
65     # split the netspecs into individual rspecs
66     netspecs = spec.getDictsByTagName('NetSpec')
67     for netspec in netspecs:
68         net_hrn = netspec['name']
69         resources = {'start_time': start_time, 'end_time': end_time, 'networks': netspec}
70         resourceDict = {'RSpec': resources}
71         tempspec.parseDict(resourceDict)
72         rspecs[net_hrn] = tempspec.toxml()
73
74     print "rspecs:", rspecs.keys()
75     print "aggregates:", aggregates.keys() 
76     # send each rspec to the appropriate aggregate/sm
77     for net_hrn in rspecs:
78         try:
79             # if we are directly connected to the aggregate then we can just 
80             # send them the rspec. if not, then we may be connected to an sm 
81             # thats connected to the aggregate
82             if net_hrn in aggregates:
83                 # send the whloe rspec to the local aggregate
84                 if net_hrn in [api.hrn]:
85                     try:
86                         request_hash = None
87                         aggregates[net_hrn].create_slice(credential, hrn, \
88                                         rspec, request_hash, origin_hrn)
89                     except:
90                         arg_list = [credential,hrn,rspec]
91                         request_hash = api.key.compute_hash(arg_list)
92                         aggregates[net_hrn].create_slice(credential, hrn, \
93                                         rspec, request_hash, origin_hrn)
94                 else:
95                     try:
96                         request_hash = None
97                         aggregates[net_hrn].create_slice(credential, hrn, \
98                                 rspecs[net_hrn], request_hash, origin_hrn)
99                     except:
100                         arg_list = [credential,hrn,rspecs[net_hrn]]
101                         request_hash = api.key.compute_hash(arg_list)
102                         aggregates[net_hrn].create_slice(credential, hrn, \
103                                 rspecs[net_hrn], request_hash, origin_hrn)
104             else:
105                 # lets forward this rspec to a sm that knows about the network
106                 arg_list = [credential, net_hrn]
107                 request_hash = api.key.compute_hash(arg_list)
108                 for aggregate in aggregates:
109                     try:
110                         network_found = aggregates[aggregate].get_aggregates(credential, net_hrn)
111                     except:
112                         network_found = aggregates[aggregate].get_aggregates(credential, net_hrn, request_hash)
113                     if network_networks:
114                         try:
115                             request_hash = None
116                             aggregates[aggregate].create_slice(credential, hrn, \
117                                     rspecs[net_hrn], request_hash, origin_hrn)
118                         except:
119                             arg_list = [credential, hrn, rspecs[net_hrn]]
120                             request_hash = api.key.compute_hash(arg_list)
121                             aggregates[aggregate].create_slice(credential, hrn, \
122                                     rspecs[net_hrn], request_hash, origin_hrn)
123
124         except:
125             print >> log, "Error creating slice %(hrn)s at aggregate %(net_hrn)s" % \
126                            locals()
127             traceback.print_exc()
128     return 1
129
130 def start_slice(api, hrn, origin_hrn=None):
131     slicename = hrn_to_pl_slicename(hrn)
132     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
133     if not slices:
134         raise RecordNotFound(hrn)
135     slice_id = slices[0]
136     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
137     attribute_id = attreibutes[0]['slice_attribute_id']
138     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
139
140     return 1
141  
142 def stop_slice(api, hrn, origin_hrn):
143     slicename = hrn_to_pl_slicename(hrn)
144     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
145     if not slices:
146         raise RecordNotFound(hrn)
147     slice_id = slices[0]['slice_id']
148     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
149     attribute_id = attributes[0]['slice_attribute_id']
150     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
151     return 1
152
153 def reset_slice(api, hrn, origin_hrn):
154     # XX not implemented at this interface
155     return 1
156
157 def get_slices(api):
158     # XX just import the legacy module and excute that until
159     # we transition the code to this module
160     from sfa.plc.slices import Slices
161     slices = Slices(api)
162     slices.refresh()
163     return slices['hrn']
164      
165 def get_rspec(api, hrn=None, origin_hrn=None):
166     from sfa.plc.nodes import Nodes
167     nodes = Nodes(api, origin_hrn=origin_hrn)
168     if hrn:
169         rspec = nodes.get_rspec(hrn)
170     else:
171         nodes.refresh()
172         rspec = nodes['rspec']
173
174     return rspec
175
176 """
177 Returns the request context required by sfatables. At some point, this mechanism should be changed
178 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
179 return the basic information needed in a dict.
180 """
181 def fetch_context(slice_hrn, user_hrn, contexts):
182     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
183     return base_context
184
185 def main():
186     r = RSpec()
187     r.parseFile(sys.argv[1])
188     rspec = r.toDict()
189     create_slice(None,'plc.princeton.tmacktestslice',rspec)
190
191 if __name__ == "__main__":
192     main()
193