fixed
[sfa.git] / sfa / managers / aggregate_manager_max.py
1 from sfa.util.xrn import urn_to_hrn, hrn_to_urn, get_authority
2 from sfa.util.plxrn import hrn_to_pl_slicename
3 from sfa.util.plxrn import hrn_to_pl_slicename
4 from sfa.util.rspec import RSpec
5 from sfa.util.sfalogging import sfa_logger
6 from sfa.util.config import Config
7 from sfa.managers.aggregate_manager_pl import GetVersion
8 import os
9 import time
10
11 RSPEC_TMP_FILE_PREFIX = "/tmp/max_rspec"
12
13 # execute shell command and return both exit code and text output
14 def shell_execute(cmd, timeout):
15     pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
16     pipe = os.popen(cmd + ' 2>&1', 'r')
17     text = ''
18     while timeout:
19         line = pipe.read()
20         text += line
21         time.sleep(1)
22         timeout = timeout-1
23     code = pipe.close()
24     if code is None: code = 0
25     if text[-1:] == '\n': text = text[:-1]
26     return code, text
27
28 """
29  call AM API client with command like in the following example:
30  cd aggregate_client; java -classpath AggregateWS-client-api.jar:lib/* \
31       net.geni.aggregate.client.examples.CreateSliceNetworkClient \
32       ./repo https://geni:8443/axis2/services/AggregateGENI \
33       ... params ...
34 """
35
36 def call_am_apiclient(client_app, params, timeout):
37     (client_path, am_url) = Config().get_max_aggrMgr_info()
38     sys_cmd = "cd " + client_path + "; java -classpath AggregateWS-client-api.jar:lib/* net.geni.aggregate.client.examples." + client_app + " ./repo " + am_url + " " + ' '.join(params)
39     ret = shell_execute(sys_cmd, timeout)
40     sfa_logger().debug("shell_execute cmd: %s returns %s" % (sys_cmd, ret))
41 # save request RSpec xml content to a tmp file
42 def save_rspec_to_file(rspec):
43     path = RSPEC_TMP_FILE_PREFIX + "_" + time.strftime('%Y%m%dT%H:%M:%S', time.gmtime(time.time())) +".xml"
44     file = open(path, "w")
45     file.write(rspec)
46     file.close()
47     return path
48
49 # get stripped down slice id/name plc:maxpl:xi_slice1 --> xi_slice1
50 def get_short_slice_id(cred, hrn):
51     if hrn == None:
52         return None
53     slice_id = hrn[hrn.rfind('+')+1:]
54     if slice_id == None:
55         slice_id = hrn[hrn.rfind(':')+1:]
56     if slice_id == None:
57        return hrn
58        pass
59     return str(slice_id)
60
61 # extract xml 
62 def get_xml_by_tag(text, tag):
63     indx1 = text.find('<'+tag)
64     indx2 = text.find('/'+tag+'>')
65     xml = None
66     if indx1!=-1 and indx2>indx1:
67         xml = text[indx1:indx2+len(tag)+2]
68     return xml
69
70 def create_slice(api, xrn, cred, rspec, users):
71     indx1 = rspec.find("<RSpec")
72     indx2 = rspec.find("</RSpec>")
73     if indx1 > -1 and indx2 > indx1:
74         rspec = rspec[indx1+len("<RSpec type=\"SFA\">"):indx2-1]
75     rspec_path = save_rspec_to_file(rspec)
76     (ret, output) = call_am_apiclient("CreateSliceNetworkClient", [rspec_path,], 3)
77     # parse output ?
78     rspec = "<RSpec type=\"SFA\"> Done! </RSpec>"
79 def delete_slice(api, xrn, cred):
80     slice_id = get_short_slice_id(cred, xrn)
81     (ret, output) = call_am_apiclient("DeleteSliceNetworkClient", [slice_id,], 3)
82     # parse output ?
83 def get_rspec(api, cred, options):
84     #geni_slice_urn: urn:publicid:IDN+plc:maxpl+slice+xi_rspec_test1
85     urn = options.get('geni_slice_urn')
86     slice_id = get_short_slice_id(cred, urn)
87     if slice_id == None:
88         (ret, output) = call_am_apiclient("GetResourceTopology", ['all', '\"\"'], 5)
89         (ret, output) = call_am_apiclient("GetResourceTopology", ['all', slice_id,], 5)
90     # parse output into rspec XML
91     if output.find("No resouce found") > 0:
92         rspec = "<RSpec type=\"SFA\"> <Fault>No resource found</Fault> </RSpec>"
93     else:
94         comp_rspec = get_xml_by_tag(output, 'computeResource')
95         sfa_logger().debug("#### computeResource %s" % comp_rspec)
96         topo_rspec = get_xml_by_tag(output, 'topology')
97         sfa_logger().debug("#### topology %s" % topo_rspec)
98         rspec = "<RSpec type=\"SFA\"> <network name=\"" + Config().get_interface_hrn() + "\">";
99         if comp_rspec != None:
100             rspec = rspec + get_xml_by_tag(output, 'computeResource')
101         if topo_rspec != None:
102             rspec = rspec + get_xml_by_tag(output, 'topology')
103         rspec = rspec + "</network> </RSpec>"
104
105     return (rspec)
106
107 def start_slice(api, xrn, cred):
108     # service not supported
109     return None
110
111 def stop_slice(api, xrn, cred):
112     # service not supported
113     return None
114
115 def reset_slices(api, xrn):
116     # service not supported
117     return None
118
119 """
120 Returns the request context required by sfatables. At some point, this mechanism should be changed
121 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
122 return the basic information needed in a dict.
123 """
124 def fetch_context(slice_hrn, user_hrn, contexts):
125     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
126     return base_context
127     api = SfaAPI()
128     create_slice(api, "plc.maxpl.test000", None, rspec_xml, None)
129