import os
import xmlrpclib

from sfa.util.faults import *
from sfa.util.xrn import urn_to_hrn
from sfa.util.plxrn import hrn_to_pl_slicename
from sfa.util.sfaticket import SfaTicket
from sfa.util.version import version_core

def GetVersion(api):
    return version_core({'interface':'component',
                         'testbed':'myplc'})

def init_server():
    from sfa.server import sfa_component_setup
    # get current trusted gids
    try:
        sfa_component_setup.get_trusted_certs()
    except:
        # our keypair may be old, try refreshing
        sfa_component_setup.get_node_key()
        sfa_component_setup.get_credential(force=True)
        sfa_component_setup.get_trusted_certs()

def slice_status(api, slice_xrn, creds):
    result = {}
    result['geni_urn'] = slice_xrn
    result['geni_status'] = 'unknown'
    result['geni_resources'] = {}
    return result
           
def start_slice(api, xrn, creds):
    hrn, type = urn_to_hrn(xrn)
    slicename = hrn_to_pl_slicename(hrn)
    api.nodemanger.Start(slicename)

def stop_slice(api, xrn, creds):
    hrn, type = urn_to_hrn(xrn)
    slicename = hrn_to_pl_slicename(hrn)
    api.nodemanager.Stop(slicename)

def delete_slice(api, xrn, creds):
    hrn, type = urn_to_hrn(xrn)
    slicename = hrn_to_pl_slicename(hrn)
    api.nodemanager.Destroy(slicename)

def reset_slice(api, xrn):
    hrn, type = urn_to_hrn(xrn)
    slicename = hrn_to_pl_slicename(hrn)
    if not api.sliver_exists(slicename):
        raise SliverDoesNotExist(slicename)
    api.nodemanager.ReCreate(slicename)
 
def get_slices(api):
    # this returns a tuple, the data we want is at index 1 
    xids = api.nodemanager.GetXIDs()
    # unfortunately the data we want is given to us as 
    # a string but we really want it as a dict
    # lets eval it
    slices = eval(xids[1])
    return slices.keys()

def redeem_ticket(api, ticket_string):
    ticket = SfaTicket(string=ticket_string)
    ticket.decode()
    hrn = ticket.attributes['slivers'][0]['hrn']
    slicename = hrn_to_pl_slicename(hrn)
    if not api.sliver_exists(slicename):
        raise SliverDoesNotExist(slicename)

    # convert ticket to format nm is used to
    nm_ticket = xmlrpclib.dumps((ticket.attributes,), methodresponse=True)
    api.nodemanager.AdminTicket(nm_ticket)
    


@@ -69,76 +69,26 @@
 til.
-xrn import urn_to_hrn%0Afrom sfa.util.plxrn import hrn_to_pl_slicename
+plxrn import PlXrn
 %0Afro
@@ -883,59 +883,45 @@
     
-hrn, type = urn_to_hrn(xrn)%0A    slicename = hrn_to_
+slicename = PlXrn(xrn, type='slice').
 pl_s
@@ -921,35 +921,32 @@
 ').pl_slicename(
-hrn
 )%0A    api.nodema
@@ -1009,59 +1009,45 @@
     
-hrn, type = urn_to_hrn(xrn)%0A    slicename = hrn_to_
+slicename = PlXrn(xrn, type='slice').
 pl_s
@@ -1047,35 +1047,32 @@
 ').pl_slicename(
-hrn
 )%0A    api.nodema
@@ -1137,59 +1137,45 @@
     
-hrn, type = urn_to_hrn(xrn)%0A    slicename = hrn_to_
+slicename = PlXrn(xrn, type='slice').
 pl_s
@@ -1179,27 +1179,24 @@
 l_slicename(
-hrn
 )%0A    api.no
@@ -1260,59 +1260,45 @@
     
-hrn, type = urn_to_hrn(xrn)%0A    slicename = hrn_to_
+slicename = PlXrn(xrn, type='slice').
 pl_s
@@ -1298,35 +1298,32 @@
 ').pl_slicename(
-hrn
 )%0A    if not api
@@ -1901,15 +1901,20 @@
 e = 
-hrn_to_
+PlXrn (hrn).
 pl_s
@@ -1922,19 +1922,16 @@
 icename(
-hrn
 )%0A    if

use (pl)xrn only