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