fix missing module imports
[sfa.git] / plc / slicemgr.py
1 ##
2 # SliceMgr is a GeniServer that implements the Slice interface at PLC
3
4 import tempfile
5 import os
6 import time
7 import sys
8
9 from util.hierarchy import Hierarchy
10 from util.trustedroot import TrustedRootList
11 from util.cert import Keypair, Certificate
12 from util.gid import GID
13 from util.geniserver import GeniServer
14 from util.record import GeniRecord
15 from util.genitable import GeniTable
16 from util.geniticket import Ticket
17 from util.excep import *
18 from util.misc import *
19
20 from util.config import *
21
22 ##
23 # SliceMgr class extends GeniServer class
24
25 class SliceMgr(GeniServer):
26     ##
27     # Create a new slice manager object.
28     #
29     # @param ip the ip address to listen on
30     # @param port the port to listen on
31     # @param key_file private key filename of registry
32     # @param cert_file certificate filename containing public key (could be a GID file)
33
34     def __init__(self, ip, port, key_file, cert_file):
35         GeniServer.__init__(self, ip, port, key_file, cert_file)
36
37         # get PL account settings from config module
38         self.pl_auth = get_pl_auth()
39
40         # connect to planetlab
41         if "Url" in self.pl_auth:
42             self.connect_remote_shell()
43         else:
44             self.connect_local_shell()
45
46     ##
47     # Connect to a remote shell via XMLRPC
48
49     def connect_remote_shell(self):
50         import remoteshell
51         self.shell = remoteshell.RemoteShell()
52
53     ##
54     # Connect to a local shell via local API functions
55
56     def connect_local_shell(self):
57         import PLC.Shell
58         self.shell = PLC.Shell.Shell(globals = globals())
59
60     ##
61     # Register the server RPCs for the slice interface
62
63     def register_functions(self):
64         GeniServer.register_functions(self)
65         # slice interface
66         self.server.register_function(self.create_slice)
67         self.server.register_function(self.get_ticket)
68         self.server.register_function(self.redeem_ticket)
69         self.server.register_function(self.start_slice)
70         self.server.register_function(self.stop_slice)
71         self.server.register_function(self.reset_slice)
72         self.server.register_function(self.delete_slice)
73         self.server.register_function(self.get_slice_resources)
74         self.server.register_function(self.list_slices)
75         self.server.register_function(self.list_nodes)
76
77     ##
78     # create_slice: Create (instantiate) a slice. 
79     #
80     # @param cred credential string
81     # @param name name of the slice to retrieve a ticket for
82     # @param rspec resource specification dictionary
83     #
84     # @return the string representation of a ticket object
85
86     def create_slice(self, cred, name, rspec):
87         self.decode_authentication(cred, "createslice")
88         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
89         # extract per-aggregate netspec from rspec
90         # call create_slice on each aggregate
91
92     ##
93     # get_ticket: Retrieve a ticket. 
94     #
95     # This operation is not supported as part of a slice manager
96     #
97     # @param cred credential string
98     # @param name name of the slice to retrieve a ticket for
99     # @param rspec resource specification dictionary
100     #
101
102     def get_ticket(self, cred, name, rspec):
103         return anything
104
105     ##
106     # redeem_ticket: Redeem a ticket. 
107     #
108     # This operation is not supported as part of a slice manager
109     #
110     # @param cred credential string
111     # @param name name of the slice to retrieve a ticket for
112     # @param rspec resource specification dictionary
113     #
114
115     def redeem_ticket(self, cred, name, rspec):
116         return anything
117
118     ##
119     # stop_slice: Stop a slice.
120     #
121     # @param cred a credential identifying the caller (callerGID) and the slice
122     #     (objectGID)
123
124     def stop_slice(self, cred_str):
125         self.decode_authentication(cred_str, "stopslice")
126         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
127         # call stop_slice on each aggregate that hosts the slice
128
129     ##
130     # start_slice: Start a slice.
131     #
132     # @param cred a credential identifying the caller (callerGID) and the slice
133     #     (objectGID)
134
135     def start_slice(self, cred_str):
136         self.decode_authentication(cred_str, "startslice")
137         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
138         # call start_slice on each aggregate that hosts the slice
139
140     ##
141     # reset_slice: Reset a slice.
142     #
143     # @param cred a credential identifying the caller (callerGID) and the slice
144     #     (objectGID)
145
146     def reset_slice(self, cred_str):
147         self.decode_authentication(cred_str, "resetslice")
148         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
149         # call reset_slice on each aggregate that hosts the slice
150
151     ##
152     # delete_slice: Delete a slice.
153     #
154     # @param cred a credential identifying the caller (callerGID) and the slice
155     #     (objectGID)
156
157     def delete_slice(self, cred_str):
158         self.decode_authentication(cred_str, "deleteslice")
159         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
160         # call delete_slice on each aggregate that hosts the slice
161
162     ##
163     # get_slice_resources: Get resources allocated to slice
164     #
165     # @param cred a credential identifying the caller (callerGID) and the slice
166     #     (objectGID)
167
168     def get_slice_resources(self, cred_str):
169         self.decode_authentication(cred_str, "getsliceresources")
170         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
171         # call get_resources on each aggregate that hosts the slice
172         # merge returned netspecs into one big rspec
173
174     ##
175     # list_slices: List hosted slices.
176     #
177     # @param cred a credential identifying the caller (callerGID)
178
179     def list_slices(self, cred_str):
180         self.decode_authentication(cred_str, "listslices")
181         # probably have this information cached, so return that
182         # otherwise, call list_slices on all peer aggregates
183
184     ##
185     # list_nodes: List available nodes.
186     #
187     # @param cred a credential identifying the caller (callerGID)
188
189     def list_nodes(self, cred_str):
190         self.decode_authentication(cred_str, "listslices")
191         # probably have this information cached, so return that
192         # otherwise, call list_nodes on all peer aggregates