extend GetVersion to expose:
[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
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.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
25 # VINI aggregate is almost identical to PLC aggregate for many operations, 
26 # so lets just import the methods form the PLC manager
27 from sfa.managers.aggregate_manager_pl import (
28 start_slice, stop_slice, renew_slice, reset_slice, get_slices, get_ticket)
29
30
31 def GetVersion(api):
32     return version_core({'interface':'aggregate',
33                          'testbed':'myplc.vini'})
34
35 def slice_status(api, slice_xrn, creds):
36     result = {}
37     result['geni_urn'] = slice_xrn
38     result['geni_status'] = 'unknown'
39     result['geni_resources'] = {}
40     return result
41
42
43 def delete_slice(api, xrn, creds):
44     hrn, type = urn_to_hrn(xrn)
45     slicename = hrn_to_pl_slicename(hrn)
46     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
47     if not slices:
48         return 1
49     slice = slices[0]
50
51     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
52     return 1
53
54 def create_slice(api, xrn, creds, xml, users):
55     """
56     Verify HRN and initialize the slice record in PLC if necessary.
57     """
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     # print network.toxml()
93
94     return True
95
96 def get_rspec(api, creds, options):
97     # get slice's hrn from options
98     xrn = options.get('geni_slice_urn', '')
99     hrn, type = urn_to_hrn(xrn)
100     
101     # look in cache first
102     if api.cache and not xrn:
103         rspec = api.cache.get('nodes')
104         if rspec:
105             return rspec
106
107     network = ViniNetwork(api)
108     if (hrn):
109         if network.get_slice(api, hrn):
110             network.addSlice()
111
112     rspec =  network.toxml()
113
114     # cache the result
115     if api.cache and not xrn:
116         api.cache.add('nodes', rspec)
117
118     return rspec
119
120 def main():
121     api = SfaAPI()
122     """
123     #rspec = get_rspec(api, None, None)
124     rspec = get_rspec(api, "plc.princeton.iias", None)
125     print rspec
126     """
127     f = open(sys.argv[1])
128     xml = f.read()
129     f.close()
130     create_slice(api, "plc.princeton.iias", xml)
131
132 if __name__ == "__main__":
133     main()