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