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
11 RSPEC_TMP_FILE_PREFIX = "/tmp/max_rspec"
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')
24 if code is None: code = 0
25 if text[-1:] == '\n': text = text[:-1]
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 \
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")
49 # get stripped down slice id/name plc:maxpl:xi_slice1 --> xi_slice1
50 def get_short_slice_id(cred, hrn):
53 slice_id = hrn[hrn.rfind('+')+1:]
55 slice_id = hrn[hrn.rfind(':')+1:]
62 def get_xml_by_tag(text, tag):
63 indx1 = text.find('<'+tag)
64 indx2 = text.find('/'+tag+'>')
66 if indx1!=-1 and indx2>indx1:
67 xml = text[indx1:indx2+len(tag)+2]
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)
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)
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)
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>"
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>"
107 def start_slice(api, xrn, cred):
108 # service not supported
111 def stop_slice(api, xrn, cred):
112 # service not supported
115 def reset_slices(api, xrn):
116 # service not supported
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.
124 def fetch_context(slice_hrn, user_hrn, contexts):
125 base_context = {'sfa':{'user':{'hrn':user_hrn}}}
128 create_slice(api, "plc.maxpl.test000", None, rspec_xml, None)