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