removed imports on sfa.util.rspec that is gone
[sfa.git] / sfa / managers / aggregate_manager_max.py
index e586ffb..d7d3776 100644 (file)
-#!/usr/bin/python
-
-from sfa.util.rspec import RSpec
-import sys
-import pdb
-from sfa.util.xrn import urn_to_hrn, hrn_to_urn, get_authority
-from sfa.util.plxrn import hrn_to_pl_slicename
-from sfa.util.plxrn import hrn_to_pl_slicename
-from sfa.util.rspec import *
-from sfa.util.specdict import *
-from sfa.util.faults import *
-from sfa.util.storage import *
-from sfa.util.policy import Policy
-from sfa.server.aggregate import Aggregates
-from sfa.server.registry import Registries
-from sfa.util.faults import *
-
-import xml.dom.minidom
-
-SFA_MAX_CONF_FILE = '/etc/sfa/max_allocations'
-SFA_MAX_DEFAULT_RSPEC = '/etc/sfa/max_physical.xml'
-SFA_MAX_CANNED_RSPEC = '/etc/sfa/max_physical_canned.xml'
-
-topology = {}
-
-class SfaOutOfResource(SfaFault):
-    def __init__(self, interface):
-        faultString = "Interface " + interface + " not available"
-        SfaFault.__init__(self, 100, faultString, '')
-
-class SfaNoPairRSpec(SfaFault):
-    def __init__(self, interface, interface2):
-        faultString = "Interface " + interface + " should be paired with " + interface2
-        SfaFault.__init__(self, 100, faultString, '')
-
-# Returns a mapping from interfaces to the nodes they lie on and their peer interfaces
-# i -> node,i_peer
-
-def get_interface_map():
-    r = RSpec()
-    r.parseFile(SFA_MAX_DEFAULT_RSPEC)
-    rspec = r.toDict()
-    capacity = rspec['rspec']['capacity']
-    netspec = capacity[0]['netspec'][0]
-    linkdefs = {}
-    for n in netspec['nodespec']:
-        ifspecs = n['ifspec']
-        nodename = n['node']
-        for i in ifspecs:
-            ifname = i['name']
-            linkid = i['linkid']
-
-            if (linkdefs.has_key(linkid)):
-                linkdefs[linkid].extend([(nodename,ifname)])
-            else:
-                linkdefs[linkid]=[(nodename,ifname)]
-    
-    # topology maps interface x interface -> link,node1,node2
-    topology={}
-
-    for k in linkdefs.keys():
-        (n1,i1) = linkdefs[k][0]
-        (n2,i2) = linkdefs[k][1]
-
-        topology[i1] = (n1, i2)
-        topology[i2] = (n2, i1)
-        
-
-    return topology    
-
-    
-def allocations_to_rspec(allocations):
-    rspec = xml.dom.minidom.parse(SFA_MAX_DEFAULT_RSPEC)
-    req = rspec.firstChild.appendChild(rspec.createElement("request"))
-    for (iname,ip) in allocations:
-        ifspec = req.appendChild(rspec.createElement("ifspec"))
-        ifspec.setAttribute("name","tns:"+iname)
-        ifspec.setAttribute("ip",ip)
-
-    return rspec.toxml()
-        
-    
-def if_endpoints(ifs):
-    nodes=[]
-    for l in ifs:
-        nodes.extend(topology[l][0])
-    return nodes
-
-def lock_state_file():
-    # Noop for demo
-    return True
-
-def unlock_state_file():
-    return True
-    # Noop for demo
-
-def read_alloc_dict():
-    alloc_dict={}
-    rows = open(SFA_MAX_CONF_FILE).read().split('\n')
-    for r in rows:
-        columns = r.split(' ')
-        if (len(columns)==2):
-            hrn = columns[0]
-            allocs = columns[1].split(',')
-            ipallocs = map(lambda alloc:alloc.split('/'), allocs)
-            alloc_dict[hrn]=ipallocs
-    return alloc_dict
-
-def commit_alloc_dict(d):
-    f = open(SFA_MAX_CONF_FILE, 'w')
-    for hrn in d.keys():
-        columns = d[hrn]
-        ipcolumns = map(lambda x:"/".join(x), columns)
-        row = hrn+' '+','.join(ipcolumns)+'\n'
-        f.write(row)
-    f.close()
-
-def collapse_alloc_dict(d):
-    ret = []
-    for k in d.keys():
-        ret.extend(d[k])
-    return ret
-
-
-def alloc_links(api, hrn, links_to_add, links_to_drop):
-    slicename=hrn_to_pl_slicename(hrn)
-    for (iface,ip) in links_to_add:
-        node = topology[iface][0][0]
-        try:
-            api.plshell.AddSliceTag(api.plauth, slicename, "ip_addresses", ip, node)
-            api.plshell.AddSliceTag(api.plauth, slicename, "vsys", "getvlan", node)
-        except Exception: 
-            # Probably a duplicate tag. XXX July 21
-            pass
-    return True
-
-def alloc_nodes(api,hrn, requested_ifs):
-    requested_nodes = if_endpoints(requested_ifs)
-    create_slice_max_aggregate(api, hrn, requested_nodes)
-
-# Taken from slices.py
-
-def create_slice_max_aggregate(api, hrn, nodes):
-    # Get the slice record 
-    global topology
-    topology = get_interface_map()
-    slice = {}
-    registries = Registries(api)
-    registry = registries[api.hrn]
-    credential = api.getCredential()
-    urn = hrn_to_urn(hrn, 'slice')
-    records = registry.Resolve(urn, credential)
-    for record in records:
-        if record.get_type() in ['slice']:
-            slice = record.as_dict()
-    if not slice:
-        raise RecordNotFound(hrn)   
-
-    # Make sure slice exists at plc, if it doesnt add it
-    slicename = hrn_to_pl_slicename(hrn)
-    slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
-    if not slices:
-        parts = slicename.split("_")
-        login_base = parts[0]
-        # if site doesnt exist add it
-        sites = api.plshell.GetSites(api.plauth, [login_base])
-        if not sites:
-            authority = get_authority(hrn)
-            authority_urn = hrn_to_urn(authority, 'authority')
-            site_records = registry.Resolve(authority_urn, credential)
-            site_record = {}
-            if not site_records:
-                raise RecordNotFound(authority)
-            site_record = site_records[0]
-            site = site_record.as_dict()
-                
-            # add the site
-            site.pop('site_id')
-            site_id = api.plshell.AddSite(api.plauth, site)
-        else:
-            site = sites[0]
-            
-        slice_fields = {}
-        slice_keys = ['name', 'url', 'description']
-        for key in slice_keys:
-            if key in slice and slice[key]:
-                slice_fields[key] = slice[key]  
-        api.plshell.AddSlice(api.plauth, slice_fields)
-        slice = slice_fields
-        slice['node_ids'] = 0
-    else:
-        slice = slices[0]    
-
-    # get the list of valid slice users from the registry and make 
-    # they are added to the slice 
-    researchers = record.get('researcher', [])
-    for researcher in researchers:
-        person_record = {}
-        researcher_urn = hrn_to_urn(researcher, 'user')
-        person_records = registry.Resolve(researcher_urn, credential)
-        for record in person_records:
-            if record.get_type() in ['user']:
-                person_record = record
-        if not person_record:
-            pass
-        person_dict = person_record.as_dict()
-        persons = api.plshell.GetPersons(api.plauth, [person_dict['email']],
-                                         ['person_id', 'key_ids'])
-
-        # Create the person record 
-        if not persons:
-            person_id=api.plshell.AddPerson(api.plauth, person_dict)
-
-            # The line below enables the user account on the remote aggregate
-            # soon after it is created.
-            # without this the user key is not transfered to the slice
-            # (as GetSlivers returns key of only enabled users),
-            # which prevents the user from login to the slice.
-            # We may do additional checks before enabling the user.
-
-            api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
-            key_ids = []
-        else:
-            key_ids = persons[0]['key_ids']
-
-        api.plshell.AddPersonToSlice(api.plauth, person_dict['email'],
-                                     slicename)        
-
-        # Get this users local keys
-        keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
-        keys = [key['key'] for key in keylist]
-
-        # add keys that arent already there 
-        for personkey in person_dict['keys']:
-            if personkey not in keys:
-                key = {'key_type': 'ssh', 'key': personkey}
-                api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
-
-    # find out where this slice is currently running
-    nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'],
-                                    ['hostname'])
-    hostnames = [node['hostname'] for node in nodelist]
-
-    # remove nodes not in rspec
-    deleted_nodes = list(set(hostnames).difference(nodes))
-    # add nodes from rspec
-    added_nodes = list(set(nodes).difference(hostnames))
-
-    api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes) 
-    api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
-
-    return 1
-
-
-def get_rspec(api, creds, options):
-    # get slice's hrn from options
-    xrn = options.get('geni_slice_urn', '')
-    hrn, type = urn_to_hrn(xrn)
-    # Eg. config line:
-    # plc.princeton.sapan vlan23,vlan45
-
-    allocations = read_alloc_dict()
-    if (hrn and allocations.has_key(hrn)):
-            ret_rspec = allocations_to_rspec(allocations[hrn])
-    else:
-        ret_rspec = open(SFA_MAX_CANNED_RSPEC).read()
-
-    return (ret_rspec)
-
-
-def create_slice(api, xrn, creds, rspec_xml, users):
-    global topology
-    hrn = urn_to_hrn(xrn)[0]
-    topology = get_interface_map()
-
-    # Check if everything in rspec is either allocated by hrn
-    # or not allocated at all.
-    r = RSpec()
-    r.parseString(rspec_xml)
-    rspec = r.toDict()
-
-    lock_state_file()
-
-    allocations = read_alloc_dict()
-    requested_allocations = rspec_to_allocations (rspec)
-    current_allocations = collapse_alloc_dict(allocations)
-    try:
-        current_hrn_allocations=allocations[hrn]
-    except KeyError:
-        current_hrn_allocations=[]
-
-    # Check request against current allocations
-    requested_interfaces = map(lambda(elt):elt[0], requested_allocations)
-    current_interfaces = map(lambda(elt):elt[0], current_allocations)
-    current_hrn_interfaces = map(lambda(elt):elt[0], current_hrn_allocations)
-
-    for a in requested_interfaces:
-        if (a not in current_hrn_interfaces and a in current_interfaces):
-            raise SfaOutOfResource(a)
-        if (topology[a][1] not in requested_interfaces):
-            raise SfaNoPairRSpec(a,topology[a][1])
-    # Request OK
-
-    # Allocations to delete
-    allocations_to_delete = []
-    for a in current_hrn_allocations:
-        if (a not in requested_allocations):
-            allocations_to_delete.extend([a])
-
-    # Ok, let's do our thing
-    alloc_nodes(api, hrn, requested_interfaces)
-    alloc_links(api, hrn, requested_allocations, allocations_to_delete)
-    allocations[hrn] = requested_allocations
-    commit_alloc_dict(allocations)
-
-    unlock_state_file()
-
-    return True
-
-def rspec_to_allocations(rspec):
-    ifs = []
-    try:
-        ifspecs = rspec['rspec']['request'][0]['ifspec']
-        for l in ifspecs:
-            ifs.extend([(l['name'].replace('tns:',''),l['ip'])])
-    except KeyError:
-        # Bad RSpec
-        pass
-    return ifs
-
-def main():
-    t = get_interface_map()
-    r = RSpec()
-    rspec_xml = open(sys.argv[1]).read()
-    #get_rspec(None,'foo')
-    create_slice(None, "plc.princeton.sap0", rspec_xml)
-    
-if __name__ == "__main__":
-    main()
+from sfa.plc.slices import Slices\r
+from sfa.server.registry import Registries\r
+from sfa.util.xrn import urn_to_hrn, hrn_to_urn, get_authority, Xrn\r
+from sfa.util.plxrn import hrn_to_pl_slicename\r
+from sfa.util.sfalogging import logger\r
+from sfa.util.faults import *\r
+from sfa.util.config import Config\r
+from sfa.util.sfatime import utcparse\r
+from sfa.util.callids import Callids\r
+from sfa.util.version import version_core\r
+from sfa.rspecs.rspec_version import RSpecVersion\r
+from sfa.rspecs.sfa_rspec import sfa_rspec_version\r
+from sfa.rspecs.rspec_parser import parse_rspec\r
+from sfa.managers.aggregate_manager_pl import __get_registry_objects, ListSlices\r
+import os\r
+import time\r
+import re\r
+\r
+RSPEC_TMP_FILE_PREFIX = "/tmp/max_rspec"\r
+\r
+# execute shell command and return both exit code and text output\r
+def shell_execute(cmd, timeout):\r
+    pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')\r
+    pipe = os.popen(cmd + ' 2>&1', 'r')\r
+    text = ''\r
+    while timeout:\r
+        line = pipe.read()\r
+        text += line\r
+        time.sleep(1)\r
+        timeout = timeout-1\r
+    code = pipe.close()\r
+    if code is None: code = 0\r
+    if text[-1:] == '\n': text = text[:-1]\r
+    return code, text\r
+\r
+"""\r
+ call AM API client with command like in the following example:\r
+ cd aggregate_client; java -classpath AggregateWS-client-api.jar:lib/* \\r
+      net.geni.aggregate.client.examples.CreateSliceNetworkClient \\r
+      ./repo https://geni:8443/axis2/services/AggregateGENI \\r
+      ... params ...\r
+"""\r
+\r
+def call_am_apiclient(client_app, params, timeout):\r
+    (client_path, am_url) = Config().get_max_aggrMgr_info()\r
+    sys_cmd = "cd " + client_path + "; java -classpath AggregateWS-client-api.jar:lib/* net.geni.aggregate.client.examples." + client_app + " ./repo " + am_url + " " + ' '.join(params)\r
+    ret = shell_execute(sys_cmd, timeout)\r
+    logger.debug("shell_execute cmd: %s returns %s" % (sys_cmd, ret))\r
+    return ret\r
+\r
+# save request RSpec xml content to a tmp file\r
+def save_rspec_to_file(rspec):\r
+    path = RSPEC_TMP_FILE_PREFIX + "_" + time.strftime('%Y%m%dT%H:%M:%S', time.gmtime(time.time())) +".xml"\r
+    file = open(path, "w")\r
+    file.write(rspec)\r
+    file.close()\r
+    return path\r
+\r
+# get stripped down slice id/name plc.maxpl.xislice1 --> maxpl_xislice1\r
+def get_plc_slice_id(cred, xrn):\r
+    (hrn, type) = urn_to_hrn(xrn)\r
+    slice_id = hrn.find(':')\r
+    sep = '.'\r
+    if hrn.find(':') != -1:\r
+        sep=':'\r
+    elif hrn.find('+') != -1:\r
+        sep='+'\r
+    else:\r
+        sep='.'\r
+    slice_id = hrn.split(sep)[-2] + '_' + hrn.split(sep)[-1]\r
+    return slice_id\r
+\r
+# extract xml \r
+def get_xml_by_tag(text, tag):\r
+    indx1 = text.find('<'+tag)\r
+    indx2 = text.find('/'+tag+'>')\r
+    xml = None\r
+    if indx1!=-1 and indx2>indx1:\r
+        xml = text[indx1:indx2+len(tag)+2]\r
+    return xml\r
+\r
+def prepare_slice(api, slice_xrn, creds, users):\r
+    reg_objects = __get_registry_objects(slice_xrn, creds, users)\r
+    (hrn, type) = urn_to_hrn(slice_xrn)\r
+    slices = Slices(api)\r
+    peer = slices.get_peer(hrn)\r
+    sfa_peer = slices.get_sfa_peer(hrn)\r
+    slice_record=None\r
+    if users:\r
+        slice_record = users[0].get('slice_record', {})\r
+    registry = api.registries[api.hrn]\r
+    credential = api.getCredential()\r
+    # ensure site record exists\r
+    site = slices.verify_site(hrn, slice_record, peer, sfa_peer)\r
+    # ensure slice record exists\r
+    slice = slices.verify_slice(hrn, slice_record, peer, sfa_peer)\r
+    # ensure person records exists\r
+    persons = slices.verify_persons(hrn, slice, users, peer, sfa_peer)\r
+\r
+def parse_resources(text, slice_xrn):\r
+    resources = []\r
+    urn = hrn_to_urn(slice_xrn, 'sliver')\r
+    plc_slice = re.search("Slice Status => ([^\n]+)", text)\r
+    if plc_slice.group(1) != 'NONE':\r
+        res = {}\r
+        res['geni_urn'] = urn + '_plc_slice'\r
+        res['geni_error'] = ''\r
+        res['geni_status'] = 'unknown'\r
+        if plc_slice.group(1) == 'CREATED':\r
+            res['geni_status'] = 'ready'\r
+        resources.append(res)\r
+    vlans = re.findall("GRI => ([^\n]+)\n\t  Status => ([^\n]+)", text)\r
+    for vlan in vlans:\r
+        res = {}\r
+        res['geni_error'] = ''\r
+        res['geni_urn'] = urn + '_vlan_' + vlan[0]\r
+        if vlan[1] == 'ACTIVE':\r
+            res['geni_status'] = 'ready'\r
+        elif vlan[1] == 'FAILED':\r
+            res['geni_status'] = 'failed'\r
+        else:\r
+            res['geni_status'] = 'configuring'\r
+        resources.append(res)\r
+    return resources\r
+\r
+def slice_status(api, slice_xrn, creds):\r
+    urn = hrn_to_urn(slice_xrn, 'slice')\r
+    result = {}\r
+    top_level_status = 'unknown'\r
+    slice_id = get_plc_slice_id(creds, urn)\r
+    (ret, output) = call_am_apiclient("QuerySliceNetworkClient", [slice_id,], 5)\r
+    # parse output into rspec XML\r
+    if output.find("Unkown Rspec:") > 0:\r
+        top_level_staus = 'failed'\r
+        result['geni_resources'] = ''\r
+    else:\r
+        has_failure = 0\r
+        all_active = 0\r
+        if output.find("Status => FAILED") > 0:\r
+            top_level_staus = 'failed'\r
+        elif (    output.find("Status => ACCEPTED") > 0 or output.find("Status => PENDING") > 0\r
+               or output.find("Status => INSETUP") > 0 or output.find("Status => INCREATE") > 0\r
+             ):\r
+            top_level_status = 'configuring'\r
+        else:\r
+            top_level_status = 'ready'\r
+        result['geni_resources'] = parse_resources(output, slice_xrn)\r
+    result['geni_urn'] = urn\r
+    result['geni_status'] = top_level_status\r
+    return result\r
+\r
+def create_slice(api, xrn, cred, rspec, users):\r
+    indx1 = rspec.find("<RSpec")\r
+    indx2 = rspec.find("</RSpec>")\r
+    if indx1 > -1 and indx2 > indx1:\r
+        rspec = rspec[indx1+len("<RSpec type=\"SFA\">"):indx2-1]\r
+    rspec_path = save_rspec_to_file(rspec)\r
+    prepare_slice(api, xrn, cred, users)\r
+    slice_id = get_plc_slice_id(cred, xrn)\r
+    sys_cmd = "sed -i \"s/rspec id=\\\"[^\\\"]*/rspec id=\\\"" +slice_id+ "/g\" " + rspec_path + ";sed -i \"s/:rspec=[^:'<\\\" ]*/:rspec=" +slice_id+ "/g\" " + rspec_path\r
+    ret = shell_execute(sys_cmd, 1)\r
+    sys_cmd = "sed -i \"s/rspec id=\\\"[^\\\"]*/rspec id=\\\"" + rspec_path + "/g\""\r
+    ret = shell_execute(sys_cmd, 1)\r
+    (ret, output) = call_am_apiclient("CreateSliceNetworkClient", [rspec_path,], 3)\r
+    # parse output ?\r
+    rspec = "<RSpec type=\"SFA\"> Done! </RSpec>"\r
+    return True\r
+\r
+def delete_slice(api, xrn, cred):\r
+    slice_id = get_plc_slice_id(cred, xrn)\r
+    (ret, output) = call_am_apiclient("DeleteSliceNetworkClient", [slice_id,], 3)\r
+    # parse output ?\r
+    return 1\r
+\r
+\r
+def get_rspec(api, cred, slice_urn):\r
+    logger.debug("#### called max-get_rspec")\r
+    #geni_slice_urn: urn:publicid:IDN+plc:maxpl+slice+xi_rspec_test1\r
+    if slice_urn == None:\r
+        (ret, output) = call_am_apiclient("GetResourceTopology", ['all', '\"\"'], 5)\r
+    else:\r
+        slice_id = get_plc_slice_id(cred, slice_urn)\r
+        (ret, output) = call_am_apiclient("GetResourceTopology", ['all', slice_id,], 5)\r
+    # parse output into rspec XML\r
+    if output.find("No resouce found") > 0:\r
+        rspec = "<RSpec type=\"SFA\"> <Fault>No resource found</Fault> </RSpec>"\r
+    else:\r
+        comp_rspec = get_xml_by_tag(output, 'computeResource')\r
+        logger.debug("#### computeResource %s" % comp_rspec)\r
+        topo_rspec = get_xml_by_tag(output, 'topology')\r
+        logger.debug("#### topology %s" % topo_rspec)\r
+        rspec = "<RSpec type=\"SFA\"> <network name=\"" + Config().get_interface_hrn() + "\">";\r
+        if comp_rspec != None:\r
+            rspec = rspec + get_xml_by_tag(output, 'computeResource')\r
+        if topo_rspec != None:\r
+            rspec = rspec + get_xml_by_tag(output, 'topology')\r
+        rspec = rspec + "</network> </RSpec>"\r
+    return (rspec)\r
+\r
+def start_slice(api, xrn, cred):\r
+    # service not supported\r
+    return None\r
+\r
+def stop_slice(api, xrn, cred):\r
+    # service not supported\r
+    return None\r
+\r
+def reset_slices(api, xrn):\r
+    # service not supported\r
+    return None\r
+\r
+"""\r
+    GENI AM API Methods\r
+"""\r
+\r
+def GetVersion(api):\r
+    xrn=Xrn(api.hrn)\r
+    request_rspec_versions = [dict(sfa_rspec_version)]\r
+    ad_rspec_versions = [dict(sfa_rspec_version)]\r
+    #TODO: MAX-AM specific\r
+    version_more = {'interface':'aggregate',\r
+                    'testbed':'myplc',\r
+                    'hrn':xrn.get_hrn(),\r
+                    'request_rspec_versions': request_rspec_versions,\r
+                    'ad_rspec_versions': ad_rspec_versions,\r
+                    'default_ad_rspec': dict(sfa_rspec_version)\r
+                    }\r
+    return version_core(version_more)\r
+\r
+def SliverStatus(api, slice_xrn, creds, call_id):\r
+    if Callids().already_handled(call_id): return {}\r
+    return slice_status(api, slice_xrn, creds)\r
+\r
+def CreateSliver(api, slice_xrn, creds, rspec_string, users, call_id):\r
+    if Callids().already_handled(call_id): return ""\r
+    #TODO: create real CreateSliver response rspec\r
+    ret = create_slice(api, slice_xrn, creds, rspec_string, users)\r
+    if ret:\r
+        return get_rspec(api, creds, slice_xrn)\r
+    else:\r
+        return "<?xml version=\"1.0\" ?> <RSpec type=\"SFA\"> Error! </RSpec>"\r
+\r
+def DeleteSliver(api, xrn, creds, call_id):\r
+    if Callids().already_handled(call_id): return ""\r
+    return delete_slice(api, xrn, creds)\r
+\r
+# no caching\r
+def ListResources(api, creds, options,call_id):\r
+    if Callids().already_handled(call_id): return ""\r
+    # version_string = "rspec_%s" % (rspec_version.get_version_name())\r
+    slice_urn = options.get('geni_slice_urn')\r
+    return get_rspec(api, creds, slice_urn)\r
+\r
+"""\r
+Returns the request context required by sfatables. At some point, this mechanism should be changed\r
+to refer to "contexts", which is the information that sfatables is requesting. But for now, we just\r
+return the basic information needed in a dict.\r
+"""\r
+def fetch_context(slice_hrn, user_hrn, contexts):\r
+    base_context = {'sfa':{'user':{'hrn':user_hrn}}}\r
+    return base_context\r
+    api = SfaAPI()\r
+    create_slice(api, "plc.maxpl.test000", None, rspec_xml, None)\r
+\r