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 $
9 from types import StringTypes
10 from sfa.util.namespace 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 SfaRecord
15 from sfa.util.policy import Policy
16 from sfa.util.record import *
17 from sfa.util.sfaticket import SfaTicket
18 from sfa.server.registry import Registries
19 from sfa.util.debug import log
20 from sfa.plc.slices import Slices
21 import sfa.plc.peers as peers
22 from sfa.managers.vini.vini_network import *
23 from sfa.plc.api import SfaAPI
24 from sfa.plc.slices import *
25 from sfa.managers.aggregate_manager_pl import __get_registry_objects, __get_hostnames
27 # VINI aggregate is almost identical to PLC aggregate for many operations,
28 # so lets just import the methods form the PLC manager
30 from sfa.managers.aggregate_manager_pl import (
31 start_slice, stop_slice, renew_slice, reset_slice, get_slices, get_ticket)
36 version['geni_api'] = 1
40 def slice_status(api, slice_xrn, creds):
42 result['geni_urn'] = slice_xrn
43 result['geni_status'] = 'unknown'
44 result['geni_resources'] = {}
48 def delete_slice(api, xrn, creds):
49 hrn, type = urn_to_hrn(xrn)
50 slicename = hrn_to_pl_slicename(hrn)
51 slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
56 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
59 def create_slice(api, xrn, creds, xml, users):
61 Verify HRN and initialize the slice record in PLC if necessary.
64 hrn, type = urn_to_hrn(xrn)
66 reg_objects = __get_registry_objects(slice_xrn, creds, users)
68 peer = slices.get_peer(hrn)
69 sfa_peer = slices.get_sfa_peer(hrn)
70 registries = Registries(api)
71 registry = registries[api.hrn]
72 credential = api.getCredential()
73 site_id, remote_site_id = slices.verify_site(registry, credential, hrn,
74 peer, sfa_peer, reg_objects)
75 slice = slices.verify_slice(registry, credential, hrn, site_id,
76 remote_site_id, peer, sfa_peer, reg_objects)
78 network = ViniNetwork(api)
80 slice = network.get_slice(api, hrn)
81 current = __get_hostnames(slice.get_nodes())
83 network.addRSpec(xml, "/var/www/html/schemas/vini.rng")
84 #network.addRSpec(xml, "/root/SVN/sfa/trunk/sfa/managers/vini/vini.rng")
85 request = __get_hostnames(network.nodesWithSlivers())
87 # remove nodes not in rspec
88 deleted_nodes = list(set(current).difference(request))
90 # add nodes from rspec
91 added_nodes = list(set(request).difference(current))
93 api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes)
94 api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
95 network.updateSliceTags()
97 # print network.toxml()
101 def get_rspec(api, creds, options):
102 # get slice's hrn from options
103 xrn = options.get('geni_slice_urn', None)
104 hrn, type = urn_to_hrn(xrn)
106 # look in cache first
107 if api.cache and not xrn:
108 rspec = api.cache.get('nodes')
112 network = ViniNetwork(api)
114 if network.get_slice(api, hrn):
117 rspec = network.toxml()
120 if api.cache and not xrn:
121 api.cache.add('nodes', rspec)
128 #rspec = get_rspec(api, None, None)
129 rspec = get_rspec(api, "plc.princeton.iias", None)
132 f = open(sys.argv[1])
135 create_slice(api, "plc.princeton.iias", xml)
137 if __name__ == "__main__":