fix import for peers
[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 import sfa.plc.peers
21
22 def delete_slice(api, hrn, caller_cred=None):
23     credential = api.getCredential()
24     aggregates = Aggregates(api)
25     for aggregate in aggregates:
26         success = False
27         # request hash is optional so lets try the call without it
28         try:
29             request_hash=None
30             aggregates[aggregate].delete_slice(credential, hrn, request_hash, caller_cred)
31             success = True
32         except:
33             print >> log, "%s" % (traceback.format_exc())
34             print >> log, "Error calling list nodes at aggregate %s" % aggregate
35
36         # try sending the request hash if the previous call failed
37         if not success:
38             try:
39                 arg_list = [credential, hrn]
40                 request_hash = self.api.key.compute_hash(arg_list)
41                 aggregates[aggregate].delete_slice(credential, hrn, request_hash, caller_cred)
42                 success = True
43             except:
44                 print >> log, "%s" % (traceback.format_exc())
45                 print >> log, "Error calling list nodes at aggregate %s" % aggregate
46     return 1
47
48 def create_slice(api, hrn, rspec, caller_cred=None):
49     spec = RSpec()
50     tempspec = RSpec()
51     spec.parseString(rspec)
52     slicename = hrn_to_pl_slicename(hrn)
53     specDict = spec.toDict()
54     if specDict.has_key('RSpec'): specDict = specDict['RSpec']
55     if specDict.has_key('start_time'): start_time = specDict['start_time']
56     else: start_time = 0
57     if specDict.has_key('end_time'): end_time = specDict['end_time']
58     else: end_time = 0
59
60     rspecs = {}
61     aggregates = Aggregates(api)
62     credential = api.getCredential()
63
64     # split the netspecs into individual rspecs
65     netspecs = spec.getDictsByTagName('NetSpec')
66     for netspec in netspecs:
67         net_hrn = netspec['name']
68         resources = {'start_time': start_time, 'end_time': end_time, 'networks': netspec}
69         resourceDict = {'RSpec': resources}
70         tempspec.parseDict(resourceDict)
71         rspecs[net_hrn] = tempspec.toxml()
72
73     # send each rspec to the appropriate aggregate/sm
74     caller_cred = self.caller_cred
75     for net_hrn in rspecs:
76         try:
77             # if we are directly connected to the aggregate then we can just 
78             # send them the rspec. if not, then we may be connected to an sm 
79             # thats connected to the aggregate
80             if net_hrn in aggregates:
81                 # send the whloe rspec to the local aggregate
82                 if net_hrn in [api.hrn]:
83                     try:
84                         request_hash = None
85                         aggregates[net_hrn].create_slice(credential, hrn, \
86                                         rspec, request_hash, caller_cred)
87                     except:
88                         arg_list = [credential,hrn,rspec]
89                         request_hash = api.key.compute_hash(arg_list)
90                         aggregates[net_hrn].create_slice(credential, hrn, \
91                                         rspec, request_hash, caller_cred)
92                 else:
93                     try:
94                         request_hash = None
95                         aggregates[net_hrn].create_slice(credential, hrn, \
96                                 rspecs[net_hrn], request_hash, caller_cred)
97                     except:
98                         arg_list = [credential,hrn,rspecs[net_hrn]]
99                         request_hash = api.key.compute_hash(arg_list)
100                         aggregates[net_hrn].create_slice(credential, hrn, \
101                                 rspecs[net_hrn], request_hash, caller_cred)
102             else:
103                 # lets forward this rspec to a sm that knows about the network
104                 arg_list = [credential, net_hrn]
105                 request_hash = api.compute_hash(arg_list)
106                 for aggregate in aggregates:
107                     try:
108                         network_found = aggregates[aggregate].get_aggregates(credential, net_hrn)
109                     except:
110                         network_found = aggregates[aggregate].get_aggregates(credential, net_hrn, request_hash)
111                     if network_networks:
112                         try:
113                             request_hash = None
114                             aggregates[aggregate].create_slice(credential, hrn, \
115                                     rspecs[net_hrn], request_hash, caller_cred)
116                         except:
117                             arg_list = [credential, hrn, rspecs[net_hrn]]
118                             request_hash = api.key.compute_hash(arg_list)
119                             aggregates[aggregate].create_slice(credential, hrn, \
120                                     rspecs[net_hrn], request_hash, caller_cred)
121
122         except:
123             print >> log, "Error creating slice %(hrn)s at aggregate %(net_hrn)s" % \
124                            locals()
125             traceback.print_exc()
126         return 1
127
128 def start_slice(api, hrn, caller_cred=None):
129     slicename = hrn_to_pl_slicename(hrn)
130     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
131     if not slices:
132         raise RecordNotFound(hrn)
133     slice_id = slices[0]
134     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
135     attribute_id = attreibutes[0]['slice_attribute_id']
136     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
137
138     return 1
139  
140 def stop_slice(api, hrn, caller_cred):
141     slicename = hrn_to_pl_slicename(hrn)
142     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
143     if not slices:
144         raise RecordNotFound(hrn)
145     slice_id = slices[0]['slice_id']
146     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
147     attribute_id = attributes[0]['slice_attribute_id']
148     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
149     return 1
150
151 def reset_slice(api, hrn, caller_cred):
152     # XX not implemented at this interface
153     return 1
154
155 def get_slices(api):
156     # XX just import the legacy module and excute that until
157     # we transition the code to this module
158     from sfa.plc.slices import Slices
159     slices = Slices(api)
160     slices.refresh()
161     return slices['hrn']
162      
163