import sfa.server.aggregate.Aggregates
[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, caller_cred=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, caller_cred)
32             success = True
33         except:
34             print >> log, "%s" % (traceback.format_exc())
35             print >> log, "Error calling list nodes 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 = self.api.key.compute_hash(arg_list)
42                 aggregates[aggregate].delete_slice(credential, hrn, request_hash, caller_cred)
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, caller_cred=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     # send each rspec to the appropriate aggregate/sm
75     caller_cred = self.caller_cred
76     for net_hrn in rspecs:
77         try:
78             # if we are directly connected to the aggregate then we can just 
79             # send them the rspec. if not, then we may be connected to an sm 
80             # thats connected to the aggregate
81             if net_hrn in aggregates:
82                 # send the whloe rspec to the local aggregate
83                 if net_hrn in [api.hrn]:
84                     try:
85                         request_hash = None
86                         aggregates[net_hrn].create_slice(credential, hrn, \
87                                         rspec, request_hash, caller_cred)
88                     except:
89                         arg_list = [credential,hrn,rspec]
90                         request_hash = api.key.compute_hash(arg_list)
91                         aggregates[net_hrn].create_slice(credential, hrn, \
92                                         rspec, request_hash, caller_cred)
93                 else:
94                     try:
95                         request_hash = None
96                         aggregates[net_hrn].create_slice(credential, hrn, \
97                                 rspecs[net_hrn], request_hash, caller_cred)
98                     except:
99                         arg_list = [credential,hrn,rspecs[net_hrn]]
100                         request_hash = api.key.compute_hash(arg_list)
101                         aggregates[net_hrn].create_slice(credential, hrn, \
102                                 rspecs[net_hrn], request_hash, caller_cred)
103             else:
104                 # lets forward this rspec to a sm that knows about the network
105                 arg_list = [credential, net_hrn]
106                 request_hash = api.compute_hash(arg_list)
107                 for aggregate in aggregates:
108                     try:
109                         network_found = aggregates[aggregate].get_aggregates(credential, net_hrn)
110                     except:
111                         network_found = aggregates[aggregate].get_aggregates(credential, net_hrn, request_hash)
112                     if network_networks:
113                         try:
114                             request_hash = None
115                             aggregates[aggregate].create_slice(credential, hrn, \
116                                     rspecs[net_hrn], request_hash, caller_cred)
117                         except:
118                             arg_list = [credential, hrn, rspecs[net_hrn]]
119                             request_hash = api.key.compute_hash(arg_list)
120                             aggregates[aggregate].create_slice(credential, hrn, \
121                                     rspecs[net_hrn], request_hash, caller_cred)
122
123         except:
124             print >> log, "Error creating slice %(hrn)s at aggregate %(net_hrn)s" % \
125                            locals()
126             traceback.print_exc()
127         return 1
128
129 def start_slice(api, hrn, caller_cred=None):
130     slicename = hrn_to_pl_slicename(hrn)
131     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
132     if not slices:
133         raise RecordNotFound(hrn)
134     slice_id = slices[0]
135     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
136     attribute_id = attreibutes[0]['slice_attribute_id']
137     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
138
139     return 1
140  
141 def stop_slice(api, hrn, caller_cred):
142     slicename = hrn_to_pl_slicename(hrn)
143     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
144     if not slices:
145         raise RecordNotFound(hrn)
146     slice_id = slices[0]['slice_id']
147     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
148     attribute_id = attributes[0]['slice_attribute_id']
149     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
150     return 1
151
152 def reset_slice(api, hrn, caller_cred):
153     # XX not implemented at this interface
154     return 1
155
156 def get_slices(api):
157     # XX just import the legacy module and excute that until
158     # we transition the code to this module
159     from sfa.plc.slices import Slices
160     slices = Slices(api)
161     slices.refresh()
162     return slices['hrn']
163      
164