6 from types import StringTypes
8 from sfa.util.namespace import get_authority, urn_to_hrn, slicename_to_hrn, hrn_to_pl_slicename, hrn_to_urn
9 from sfa.util.rspec import *
10 from sfa.util.specdict import *
11 from sfa.util.faults import *
12 from sfa.util.record import SfaRecord
13 from sfa.util.policy import Policy
14 from sfa.util.record import *
15 from sfa.util.sfaticket import SfaTicket
16 from sfa.plc.slices import Slices
17 from sfa.trust.credential import Credential
18 import sfa.plc.peers as peers
19 from sfa.plc.network import *
20 from sfa.plc.api import SfaAPI
21 from sfa.plc.slices import *
24 def __get_registry_objects(slice_xrn, creds, users):
28 hrn, type = urn_to_hrn(slice_xrn)
30 hrn_auth = get_authority(hrn)
32 # Build up objects that an SFA registry would return if SFA
33 # could contact the slice's registry directly
37 # dont allow special characters in the site login base
38 #only_alphanumeric = re.compile('[^a-zA-Z0-9]+')
39 #login_base = only_alphanumeric.sub('', hrn_auth[:20]).lower()
40 slicename = hrn_to_pl_slicename(hrn)
41 login_base = slicename.split('_')[0]
46 site['name'] = 'geni.%s' % login_base
47 site['enabled'] = True
48 site['max_slices'] = 100
51 # Is it okay if this login base is the same as one already at this myplc site?
52 # Do we need uniqueness? Should use hrn_auth instead of just the leaf perhaps?
53 site['login_base'] = login_base
54 site['abbreviated_name'] = login_base
55 site['max_slivers'] = 1000
56 reg_objects['site'] = site
59 slice['expires'] = int(time.mktime(Credential(string=creds[0]).get_lifetime().timetuple()))
61 slice['name'] = hrn_to_pl_slicename(hrn)
63 slice['description'] = hrn
65 reg_objects['slice_record'] = slice
67 reg_objects['users'] = {}
70 hrn, _ = urn_to_hrn(user['urn'])
71 user['email'] = hrn_to_pl_slicename(hrn) + "@geni.net"
72 user['first_name'] = hrn
73 user['last_name'] = hrn
74 reg_objects['users'][user['email']] = user
78 def __get_hostnames(nodes):
81 hostnames.append(node.hostname)
86 version['geni_api'] = 1
90 def slice_status(api, slice_xrn, creds):
92 result['geni_urn'] = slice_xrn
93 result['geni_status'] = 'unknown'
94 result['geni_resources'] = {}
97 def create_slice(api, slice_xrn, creds, rspec, users):
99 Create the sliver[s] (slice) at this aggregate.
100 Verify HRN and initialize the slice record in PLC if necessary.
103 reg_objects = __get_registry_objects(slice_xrn, creds, users)
105 hrn, type = urn_to_hrn(slice_xrn)
108 peer = slices.get_peer(hrn)
109 sfa_peer = slices.get_sfa_peer(hrn)
110 registry = api.registries[api.hrn]
111 credential = api.getCredential()
112 site_id, remote_site_id = slices.verify_site(registry, credential, hrn,
113 peer, sfa_peer, reg_objects)
115 slice_record = slices.verify_slice(registry, credential, hrn, site_id,
116 remote_site_id, peer, sfa_peer, reg_objects)
118 network = Network(api)
120 slice = network.get_slice(api, hrn)
121 slice.peer_id = slice_record['peer_slice_id']
122 current = __get_hostnames(slice.get_nodes())
124 network.addRSpec(rspec, api.config.SFA_AGGREGATE_RSPEC_SCHEMA)
125 request = __get_hostnames(network.nodesWithSlivers())
127 # remove nodes not in rspec
128 deleted_nodes = list(set(current).difference(request))
130 # add nodes from rspec
131 added_nodes = list(set(request).difference(current))
135 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
137 api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes)
138 api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
140 network.updateSliceTags()
144 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer,
147 # print network.toxml()
152 def renew_slice(api, xrn, creds, exipration_time):
153 hrn, type = urn_to_hrn(xrn)
154 slicename = hrn_to_pl_slicename(hrn)
155 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
157 raise RecordNotFound(hrn)
159 slice['expires'] = expiration_time
160 api.plshell.UpdateSlice(api.plauth, slice['slice_id'], slice)
163 def start_slice(api, xrn, creds):
164 hrn, type = urn_to_hrn(xrn)
165 slicename = hrn_to_pl_slicename(hrn)
166 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
168 raise RecordNotFound(hrn)
169 slice_id = slices[0]['slice_id']
170 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'}, ['slice_tag_id'])
171 # just remove the tag if it exists
173 api.plshell.DeleteSliceTag(api.plauth, slice_tags[0]['slice_tag_id'])
177 def stop_slice(api, xrn, creds):
178 hrn, type = urn_to_hrn(xrn)
179 slicename = hrn_to_pl_slicename(hrn)
180 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
182 raise RecordNotFound(hrn)
183 slice_id = slices[0]['slice_id']
184 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'})
186 api.plshell.AddSliceTag(api.plauth, slice_id, 'enabled', '0')
187 elif slice_tags[0]['value'] != "0":
188 tag_id = attributes[0]['slice_tag_id']
189 api.plshell.UpdateSliceTag(api.plauth, tag_id, '0')
192 def reset_slice(api, xrn):
193 # XX not implemented at this interface
196 def delete_slice(api, xrn, creds):
197 hrn, type = urn_to_hrn(xrn)
198 slicename = hrn_to_pl_slicename(hrn)
199 slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
204 # determine if this is a peer slice
205 peer = peers.get_peer(api, hrn)
208 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
209 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
212 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
215 def get_slices(api, creds):
216 # look in cache first
218 slices = api.cache.get('slices')
223 slices = api.plshell.GetSlices(api.plauth, {'peer_id': None}, ['name'])
224 slice_hrns = [slicename_to_hrn(api.hrn, slice['name']) for slice in slices]
225 slice_urns = [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slice_hrns]
229 api.cache.add('slices', slice_urns)
233 def get_rspec(api, creds, options):
234 # get slice's hrn from options
235 xrn = options.get('geni_slice_urn', None)
236 hrn, type = urn_to_hrn(xrn)
238 # look in cache first
239 if api.cache and not xrn:
240 rspec = api.cache.get('nodes')
244 network = Network(api)
246 if network.get_slice(api, hrn):
249 rspec = network.toxml()
252 if api.cache and not xrn:
253 api.cache.add('nodes', rspec)
258 def get_ticket(api, xrn, creds, rspec, users):
260 reg_objects = __get_registry_objects(xrn, creds, users)
262 slice_hrn, type = urn_to_hrn(xrn)
264 peer = slices.get_peer(slice_hrn)
265 sfa_peer = slices.get_sfa_peer(slice_hrn)
267 # get the slice record
268 registry = api.registries[api.hrn]
269 credential = api.getCredential()
270 records = registry.Resolve(xrn, credential)
272 # similar to create_slice, we must verify that the required records exist
273 # at this aggregate before we can issue a ticket
274 site_id, remote_site_id = slices.verify_site(registry, credential, slice_hrn,
275 peer, sfa_peer, reg_objects)
276 slice = slices.verify_slice(registry, credential, slice_hrn, site_id,
277 remote_site_id, peer, sfa_peer, reg_objects)
279 # make sure we get a local slice record
281 for tmp_record in records:
282 if tmp_record['type'] == 'slice' and \
283 not tmp_record['peer_authority']:
284 record = SliceRecord(dict=tmp_record)
286 raise RecordNotFound(slice_hrn)
289 slivers = Slices(api).get_slivers(slice_hrn)
291 raise SliverDoesNotExist(slice_hrn)
296 'timestamp': int(time.time()),
297 'initscripts': initscripts,
302 object_gid = record.get_gid_object()
303 new_ticket = SfaTicket(subject = object_gid.get_subject())
304 new_ticket.set_gid_caller(api.auth.client_gid)
305 new_ticket.set_gid_object(object_gid)
306 new_ticket.set_issuer(key=api.key, subject=api.hrn)
307 new_ticket.set_pubkey(object_gid.get_pubkey())
308 new_ticket.set_attributes(data)
309 new_ticket.set_rspec(rspec)
310 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
314 return new_ticket.save_to_string(save_parents=True)
321 rspec = get_rspec(api, "plc.princeton.sapan", None)
322 #rspec = get_rspec(api, "plc.princeton.coblitz", None)
323 #rspec = get_rspec(api, "plc.pl.sirius", None)
326 f = open(sys.argv[1])
329 create_slice(api, "plc.princeton.sapan", xml)
331 if __name__ == "__main__":