renamed create_slice into CreateSliver on the managers side
[sfa.git] / sfa / managers / aggregate_manager_pl.py
index 5cb2d3c..9e0aa27 100644 (file)
@@ -1,28 +1,35 @@
-### $Id: slices.py 15842 2009-11-22 09:56:13Z anil $
-### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/plc/slices.py $
-
 import datetime
 import time
 import traceback
 import sys
-
+import re
 from types import StringTypes
-from sfa.util.namespace import *
+
+from sfa.util.faults import *
+from sfa.util.xrn import get_authority, hrn_to_urn, urn_to_hrn, Xrn
+from sfa.util.plxrn import slicename_to_hrn, hrn_to_pl_slicename, hostname_to_urn
 from sfa.util.rspec import *
 from sfa.util.specdict import *
-from sfa.util.faults import *
 from sfa.util.record import SfaRecord
 from sfa.util.policy import Policy
 from sfa.util.record import *
 from sfa.util.sfaticket import SfaTicket
-from sfa.util.debug import log
 from sfa.plc.slices import Slices
 from sfa.trust.credential import Credential
 import sfa.plc.peers as peers
 from sfa.plc.network import *
 from sfa.plc.api import SfaAPI
 from sfa.plc.slices import *
+from sfa.util.version import version_core
+from sfa.util.sfatime import utcparse
+from sfa.util.callids import Callids
 
+def GetVersion(api):
+    xrn=Xrn(api.hrn)
+    return version_core({'interface':'aggregate',
+                         'testbed':'myplc',
+                         'hrn':xrn.get_hrn(),
+                         })
 
 def __get_registry_objects(slice_xrn, creds, users):
     """
@@ -37,26 +44,35 @@ def __get_registry_objects(slice_xrn, creds, users):
     reg_objects = None
 
     if users:
+        # dont allow special characters in the site login base
+        #only_alphanumeric = re.compile('[^a-zA-Z0-9]+')
+        #login_base = only_alphanumeric.sub('', hrn_auth[:20]).lower()
+        slicename = hrn_to_pl_slicename(hrn)
+        login_base = slicename.split('_')[0]
         reg_objects = {}
-
         site = {}
         site['site_id'] = 0
-        site['name'] = 'geni.%s' % hrn_auth
+        site['name'] = 'geni.%s' % login_base 
         site['enabled'] = True
         site['max_slices'] = 100
 
         # Note:
         # Is it okay if this login base is the same as one already at this myplc site?
         # Do we need uniqueness?  Should use hrn_auth instead of just the leaf perhaps?
-        site['login_base'] = get_leaf(hrn_auth)
-        site['abbreviated_name'] = hrn
+        site['login_base'] = login_base
+        site['abbreviated_name'] = login_base
         site['max_slivers'] = 1000
         reg_objects['site'] = site
 
         slice = {}
-        slice['expires'] = int(mktime(Credential(string=creds[0]).get_lifetime().timetuple()))
+        
+        extime = Credential(string=creds[0]).get_expiration()
+        # If the expiration time is > 60 days from now, set the expiration time to 60 days from now
+        if extime > datetime.datetime.utcnow() + datetime.timedelta(days=60):
+            extime = datetime.datetime.utcnow() + datetime.timedelta(days=60)
+        slice['expires'] = int(time.mktime(extime.timetuple()))
         slice['hrn'] = hrn
-        slice['name'] = site['login_base'] + "_" +  get_leaf(hrn)
+        slice['name'] = hrn_to_pl_slicename(hrn)
         slice['url'] = hrn
         slice['description'] = hrn
         slice['pointer'] = 0
@@ -66,7 +82,7 @@ def __get_registry_objects(slice_xrn, creds, users):
         for user in users:
             user['key_ids'] = []
             hrn, _ = urn_to_hrn(user['urn'])
-            user['email'] = hrn + "@geni.net"
+            user['email'] = hrn_to_pl_slicename(hrn) + "@geni.net"
             user['first_name'] = hrn
             user['last_name'] = hrn
             reg_objects['users'][user['email']] = user
@@ -79,25 +95,71 @@ def __get_hostnames(nodes):
         hostnames.append(node.hostname)
     return hostnames
 
-
-
-def get_version():
-    version = {}
-    version['geni_api'] = 1
-    return version
-
 def slice_status(api, slice_xrn, creds):
+    hrn, type = urn_to_hrn(slice_xrn)
+    # find out where this slice is currently running
+    api.logger.info(hrn)
+    slicename = hrn_to_pl_slicename(hrn)
+    
+    slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids','person_ids','name','expires'])
+    if len(slices) == 0:        
+        raise Exception("Slice %s not found (used %s as slicename internally)" % slice_xrn, slicename)
+    slice = slices[0]
+    
+    nodes = api.plshell.GetNodes(api.plauth, slice['node_ids'],
+                                    ['hostname', 'site_id', 'boot_state', 'last_contact'])
+    site_ids = [node['site_id'] for node in nodes]
+    sites = api.plshell.GetSites(api.plauth, site_ids, ['site_id', 'login_base'])
+    sites_dict = {}
+    for site in sites:
+        sites_dict[site['site_id']] = site['login_base']
+
+    # XX remove me
+    #api.logger.info(slice_xrn)
+    #api.logger.info(slice)
+    #api.logger.info(nodes)
+    # XX remove me
+
     result = {}
-    result['geni_urn'] = slice_xrn
-    result['geni_status'] = 'unknown'
-    result['geni_resources'] = {}
+    top_level_status = 'unknown'
+    if nodes:
+        top_level_status = 'ready'
+    result['geni_urn'] = Xrn(slice_xrn, 'slice').get_urn()
+    result['pl_login'] = slice['name']
+    result['pl_expires'] = datetime.datetime.fromtimestamp(slice['expires']).ctime()
+    
+    resources = []
+    for node in nodes:
+        res = {}
+        res['pl_hostname'] = node['hostname']
+        res['pl_boot_state'] = node['boot_state']
+        res['pl_last_contact'] = node['last_contact']
+        if not node['last_contact'] is None:
+            res['pl_last_contact'] = datetime.datetime.fromtimestamp(node['last_contact']).ctime()
+        res['geni_urn'] = hostname_to_urn(api.hrn, sites_dict[node['site_id']], node['hostname'])
+        if node['boot_state'] == 'boot':
+            res['geni_status'] = 'ready'
+        else:
+            res['geni_status'] = 'failed'
+            top_level_staus = 'failed' 
+            
+        res['geni_error'] = ''
+
+        resources.append(res)
+        
+    result['geni_status'] = top_level_status
+    result['geni_resources'] = resources
+    # XX remove me
+    #api.logger.info(result)
+    # XX remove me
     return result
 
-def create_slice(api, slice_xrn, creds, rspec, users):
+def CreateSliver(api, slice_xrn, creds, rspec, users, call_id):
     """
     Create the sliver[s] (slice) at this aggregate.    
     Verify HRN and initialize the slice record in PLC if necessary.
     """
+    if Callids().already_handled(call_id): return False
 
     reg_objects = __get_registry_objects(slice_xrn, creds, users)
 
@@ -108,15 +170,16 @@ def create_slice(api, slice_xrn, creds, rspec, users):
     sfa_peer = slices.get_sfa_peer(hrn)
     registry = api.registries[api.hrn]
     credential = api.getCredential()
-    site_id, remote_site_id = slices.verify_site(registry, credential, hrn, 
-                                                 peer, sfa_peer, reg_objects)
-
-    slice = slices.verify_slice(registry, credential, hrn, site_id, 
-                                remote_site_id, peer, sfa_peer, reg_objects)
+    (site_id, remote_site_id) = slices.verify_site(registry, credential, hrn, 
+                                                   peer, sfa_peer, reg_objects)
 
+    slice_record = slices.verify_slice(registry, credential, hrn, site_id, 
+                                       remote_site_id, peer, sfa_peer, reg_objects)
+     
     network = Network(api)
 
     slice = network.get_slice(api, hrn)
+    slice.peer_id = slice_record['peer_slice_id']
     current = __get_hostnames(slice.get_nodes())
     
     network.addRSpec(rspec, api.config.SFA_AGGREGATE_RSPEC_SCHEMA)
@@ -127,45 +190,49 @@ def create_slice(api, slice_xrn, creds, rspec, users):
 
     # add nodes from rspec
     added_nodes = list(set(request).difference(current))
-    
-    if peer:
-        api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
 
-    api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes) 
-    api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
+    try:
+        if peer:
+            api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
+
+        api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes) 
+        api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
 
-    network.updateSliceTags()
+        network.updateSliceTags()
 
-    if peer:
-        api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer, 
-                                     slice.peer_id)
+    finally:
+        if peer:
+            api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer, 
+                                         slice.peer_id)
 
     # print network.toxml()
 
     return True
 
 
-def renew_slice(api, xrn, creds, exipration_time):
+def renew_slice(api, xrn, creds, expiration_time):
     hrn, type = urn_to_hrn(xrn)
     slicename = hrn_to_pl_slicename(hrn)
     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
     if not slices:
         raise RecordNotFound(hrn)
     slice = slices[0]
-    slice['expires'] = expiration_time
-    api.plshell.UpdateSlice(api.plauth, slice['slice_id'], slice)
+    requested_time = utcparse(expiration_time)
+    record = {'expires': int(time.mktime(requested_time.timetuple()))}
+    api.plshell.UpdateSlice(api.plauth, slice['slice_id'], record)
     return 1         
 
-def start_slice(api, xrn):
+def start_slice(api, xrn, creds):
     hrn, type = urn_to_hrn(xrn)
     slicename = hrn_to_pl_slicename(hrn)
     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
     if not slices:
         raise RecordNotFound(hrn)
-    slice_id = slices[0]
-    attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
-    attribute_id = attributes[0]['slice_attribute_id']
-    api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
+    slice_id = slices[0]['slice_id']
+    slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'}, ['slice_tag_id'])
+    # just remove the tag if it exists
+    if slice_tags:
+        api.plshell.DeleteSliceTag(api.plauth, slice_tags[0]['slice_tag_id'])
 
     return 1
  
@@ -176,9 +243,12 @@ def stop_slice(api, xrn, creds):
     if not slices:
         raise RecordNotFound(hrn)
     slice_id = slices[0]['slice_id']
-    attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
-    attribute_id = attributes[0]['slice_attribute_id']
-    api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
+    slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'})
+    if not slice_tags:
+        api.plshell.AddSliceTag(api.plauth, slice_id, 'enabled', '0')
+    elif slice_tags[0]['value'] != "0":
+        tag_id = attributes[0]['slice_tag_id']
+        api.plshell.UpdateSliceTag(api.plauth, tag_id, '0')
     return 1
 
 def reset_slice(api, xrn):
@@ -195,14 +265,16 @@ def delete_slice(api, xrn, creds):
 
     # determine if this is a peer slice
     peer = peers.get_peer(api, hrn)
-    if peer:
-        api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
-    api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
-    if peer:
-        api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
+    try:
+        if peer:
+            api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
+        api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
+    finally:
+        if peer:
+            api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
     return 1
 
-def get_slices(api):
+def get_slices(api, creds):
     # look in cache first
     if api.cache:
         slices = api.cache.get('slices')
@@ -220,20 +292,20 @@ def get_slices(api):
 
     return slice_urns
     
-def get_rspec(api, creds, options):
+# xxx Thierry : caching at the aggregate level sounds wrong...
+caching=True
+#caching=False
+def ListResources(api, creds, options,call_id):
+    if Callids().already_handled(call_id): return ""
     # get slice's hrn from options
-    xrn = options.get('geni_slice_urn', None)
-    hrn, type = urn_to_hrn(xrn)
+    xrn = options.get('geni_slice_urn', '')
+    (hrn, type) = urn_to_hrn(xrn)
 
-    # get hrn of the original caller
-    origin_hrn = options.get('origin_hrn', None)
-    if not origin_hrn:
-        origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
-    
     # look in cache first
-    if api.cache and not xrn:
+    if caching and api.cache and not xrn:
         rspec = api.cache.get('nodes')
         if rspec:
+            api.logger.info("aggregate.ListResources: returning cached value for hrn %s"%hrn)
             return rspec 
 
     network = Network(api)
@@ -244,13 +316,15 @@ def get_rspec(api, creds, options):
     rspec = network.toxml()
 
     # cache the result
-    if api.cache and not xrn:
+    if caching and api.cache and not xrn:
         api.cache.add('nodes', rspec)
 
     return rspec
 
 
-def get_ticket(api, xrn, rspec, origin_hrn=None, reg_objects=None):
+def get_ticket(api, xrn, creds, rspec, users):
+
+    reg_objects = __get_registry_objects(xrn, creds, users)
 
     slice_hrn, type = urn_to_hrn(xrn)
     slices = Slices(api)
@@ -260,9 +334,9 @@ def get_ticket(api, xrn, rspec, origin_hrn=None, reg_objects=None):
     # get the slice record
     registry = api.registries[api.hrn]
     credential = api.getCredential()
-    records = registry.resolve(credential, xrn)
+    records = registry.Resolve(xrn, credential)
 
-    # similar to create_slice, we must verify that the required records exist
+    # similar to CreateSliver, we must verify that the required records exist
     # at this aggregate before we can issue a ticket
     site_id, remote_site_id = slices.verify_site(registry, credential, slice_hrn,
                                                  peer, sfa_peer, reg_objects)
@@ -311,15 +385,15 @@ def get_ticket(api, xrn, rspec, origin_hrn=None, reg_objects=None):
 def main():
     api = SfaAPI()
     """
-    rspec = get_rspec(api, "plc.princeton.sapan", None)
-    #rspec = get_rspec(api, "plc.princeton.coblitz", None)
-    #rspec = get_rspec(api, "plc.pl.sirius", None)
+    rspec = ListResources(api, "plc.princeton.sapan", None, 'pl_test_sapan')
+    #rspec = ListResources(api, "plc.princeton.coblitz", None, 'pl_test_coblitz')
+    #rspec = ListResources(api, "plc.pl.sirius", None, 'pl_test_sirius')
     print rspec
     """
     f = open(sys.argv[1])
     xml = f.read()
     f.close()
-    create_slice(api, "plc.princeton.sapan", xml)
+    CreateSliver(api, "plc.princeton.sapan", xml, 'CreateSliver_sapan')
 
 if __name__ == "__main__":
     main()