Merge branch 'master' of ssh://git.onelab.eu/git/sfa
[sfa.git] / sfa / managers / aggregate_manager_vini.py
1 import datetime
2 import time
3 import traceback
4 import sys
5
6 from types import StringTypes
7 from sfa.util.xrn import urn_to_hrn, Xrn
8 from sfa.util.plxrn import hrn_to_pl_slicename
9 from sfa.util.faults import *
10 from sfa.util.record import SfaRecord
11 from sfa.util.policy import Policy
12 from sfa.util.record import *
13 from sfa.trust.sfaticket import SfaTicket
14 from sfa.server.registry import Registries
15 from sfa.plc.slices import Slices
16 import sfa.plc.peers as peers
17 from sfa.managers.vini.vini_network import *
18 from sfa.plc.vini_aggregate import ViniAggregate
19 from sfa.rspecs.version_manager import VersionManager
20 from sfa.plc.api import SfaAPI
21 from sfa.plc.slices import *
22 from sfa.managers.aggregate_manager_pl import __get_registry_objects, __get_hostnames
23 from sfa.util.version import version_core
24 from sfa.util.callids import Callids
25
26 # VINI aggregate is almost identical to PLC aggregate for many operations, 
27 # so lets just import the methods form the PLC manager
28 from sfa.managers.aggregate_manager_pl import (
29 start_slice, stop_slice, RenewSliver, reset_slice, ListSlices, get_ticket, SliverStatus)
30
31
32 def GetVersion(api):
33     xrn=Xrn(api.hrn)
34     return version_core({'interface':'aggregate',
35                          'testbed':'myplc.vini',
36                          'hrn':xrn.get_hrn(),
37                          })
38
39 def DeleteSliver(api, xrn, creds, call_id):
40     if Callids().already_handled(call_id): return ""
41     (hrn, type) = urn_to_hrn(xrn)
42     slicename = hrn_to_pl_slicename(hrn)
43     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
44     if not slices:
45         return 1
46     slice = slices[0]
47
48     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
49     return 1
50
51 def CreateSliver(api, xrn, creds, xml, users, call_id):
52     """
53     Verify HRN and initialize the slice record in PLC if necessary.
54     """
55
56     if Callids().already_handled(call_id): return ""
57
58     hrn, type = urn_to_hrn(xrn)
59     peer = None
60     reg_objects = __get_registry_objects(xrn, creds, users)
61     slices = Slices(api)
62     peer = slices.get_peer(hrn)
63     sfa_peer = slices.get_sfa_peer(hrn)
64     registries = Registries(api)
65     registry = registries[api.hrn]
66     credential = api.getCredential()
67     site_id, remote_site_id = slices.verify_site(registry, credential, hrn, 
68                                                  peer, sfa_peer, reg_objects)
69     slice = slices.verify_slice(registry, credential, hrn, site_id, 
70                                 remote_site_id, peer, sfa_peer, reg_objects)
71
72     network = ViniNetwork(api)
73
74     slice = network.get_slice(api, hrn)
75     current = __get_hostnames(slice.get_nodes())
76
77     network.addRSpec(xml, "/var/www/html/schemas/vini.rng")
78     #network.addRSpec(xml, "/root/SVN/sfa/trunk/sfa/managers/vini/vini.rng")
79     request = __get_hostnames(network.nodesWithSlivers())
80     
81     # remove nodes not in rspec
82     deleted_nodes = list(set(current).difference(request))
83
84     # add nodes from rspec
85     added_nodes = list(set(request).difference(current))
86
87     api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes) 
88     api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
89     network.updateSliceTags()
90
91     # xxx - check this holds enough data for the client to understand what's happened
92     return network.toxml()
93
94 def ListResources(api, creds, options,call_id):
95     if Callids().already_handled(call_id): return ""
96     # get slice's hrn from options
97     xrn = options.get('geni_slice_urn', '')
98     hrn, type = urn_to_hrn(xrn)
99
100     version_manager = VersionManager()
101     # get the rspec's return format from options
102     rspec_version = version_manager.get_version(options.get('rspec_version'))
103     version_string = "rspec_%s" % (rspec_version.to_string())
104     
105     # look in cache first
106     if api.cache and not xrn:
107         rspec = api.cache.get(version_string)
108         if rspec:
109             api.logger.info("aggregate.ListResources: returning cached value for hrn %s"%hrn)
110             return rspec
111
112     aggregate = ViniAggregate(api, options) 
113     rspec =  aggregate.get_rspec(slice_xrn=xrn, version=rspec_version)
114            
115     # cache the result
116     if api.cache and not xrn:
117         api.cache.add('nodes', rspec)
118
119     return rspec
120
121 def main():
122     api = SfaAPI()
123     """
124     #rspec = ListResources(api, None, None,)
125     rspec = ListResources(api, "plc.princeton.iias", None, 'vini_test')
126     print rspec
127     """
128     f = open(sys.argv[1])
129     xml = f.read()
130     f.close()
131     CreateSliver(api, "plc.princeton.iias", xml, 'call-id-iias')
132
133 if __name__ == "__main__":
134     main()