AMs also expose their hrn in GetVersion
[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.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, slice_status)
29
30
31 def GetVersion(api):
32     xrn=Xrn(api.hrn)
33     return version_core({'interface':'aggregate',
34                          'testbed':'myplc.vini',
35                          'hrn':xrn.get_hrn(),
36                          })
37
38 def delete_slice(api, xrn, creds):
39     hrn, type = urn_to_hrn(xrn)
40     slicename = hrn_to_pl_slicename(hrn)
41     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
42     if not slices:
43         return 1
44     slice = slices[0]
45
46     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
47     return 1
48
49 def create_slice(api, xrn, creds, xml, users):
50     """
51     Verify HRN and initialize the slice record in PLC if necessary.
52     """
53
54     hrn, type = urn_to_hrn(xrn)
55     peer = None
56     reg_objects = __get_registry_objects(xrn, creds, users)
57     slices = Slices(api)
58     peer = slices.get_peer(hrn)
59     sfa_peer = slices.get_sfa_peer(hrn)
60     registries = Registries(api)
61     registry = registries[api.hrn]
62     credential = api.getCredential()
63     site_id, remote_site_id = slices.verify_site(registry, credential, hrn, 
64                                                  peer, sfa_peer, reg_objects)
65     slice = slices.verify_slice(registry, credential, hrn, site_id, 
66                                 remote_site_id, peer, sfa_peer, reg_objects)
67
68     network = ViniNetwork(api)
69
70     slice = network.get_slice(api, hrn)
71     current = __get_hostnames(slice.get_nodes())
72
73     network.addRSpec(xml, "/var/www/html/schemas/vini.rng")
74     #network.addRSpec(xml, "/root/SVN/sfa/trunk/sfa/managers/vini/vini.rng")
75     request = __get_hostnames(network.nodesWithSlivers())
76     
77     # remove nodes not in rspec
78     deleted_nodes = list(set(current).difference(request))
79
80     # add nodes from rspec
81     added_nodes = list(set(request).difference(current))
82
83     api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes) 
84     api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
85     network.updateSliceTags()
86
87     # print network.toxml()
88
89     return True
90
91 def get_rspec(api, creds, options):
92     # get slice's hrn from options
93     xrn = options.get('geni_slice_urn', '')
94     hrn, type = urn_to_hrn(xrn)
95     
96     # look in cache first
97     if api.cache and not xrn:
98         rspec = api.cache.get('nodes')
99         if rspec:
100             return rspec
101
102     network = ViniNetwork(api)
103     if (hrn):
104         if network.get_slice(api, hrn):
105             network.addSlice()
106
107     rspec =  network.toxml()
108
109     # cache the result
110     if api.cache and not xrn:
111         api.cache.add('nodes', rspec)
112
113     return rspec
114
115 def main():
116     api = SfaAPI()
117     """
118     #rspec = get_rspec(api, None, None)
119     rspec = get_rspec(api, "plc.princeton.iias", None)
120     print rspec
121     """
122     f = open(sys.argv[1])
123     xml = f.read()
124     f.close()
125     create_slice(api, "plc.princeton.iias", xml)
126
127 if __name__ == "__main__":
128     main()