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