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