<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import traceback <EOL> from pyon . core import bootstrap <EOL> from pyon . core . bootstrap import IonObject <EOL> from pyon . core import exception as iex <EOL> from pyon . ion . event import EventPublisher <EOL> from pyon . ion . resource import RT , PRED , LCS <EOL> from pyon . util . log import log <EOL> from pyon . util . containers import get_ion_ts <EOL> from interface . services . isimple_resource_agent import BaseSimpleResourceAgent , SimpleResourceAgentProcessClient <EOL> from interface . services . coi . iresource_registry_service import ResourceRegistryServiceProcessClient <EOL> class SimpleResourceAgent ( BaseSimpleResourceAgent ) : <EOL> """<STR_LIT>""" <EOL> process_type = "<STR_LIT>" <EOL> COMMAND_EVENT_TYPE = "<STR_LIT>" <EOL> ORIGIN_TYPE = "<STR_LIT>" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SimpleResourceAgent , self ) . __init__ ( * args , ** kwargs ) <EOL> self . agent_id = None <EOL> self . agent_def_id = None <EOL> self . resource_id = None <EOL> self . resource_type = None <EOL> def _on_init ( self ) : <EOL> log . debug ( "<STR_LIT>" % ( self . _proc_name , self . resource_id ) ) <EOL> self . _event_publisher = EventPublisher ( ) <EOL> def _on_quit ( self ) : <EOL> pass <EOL> def _get_process_org_governance_name ( self ) : <EOL> '''<STR_LIT>''' <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> org_governance_name = self . org_governance_name <EOL> log . debug ( "<STR_LIT>" + org_governance_name ) <EOL> else : <EOL> org_governance_name = self . container . governance_controller . system_root_org_name <EOL> log . debug ( "<STR_LIT>" + org_governance_name ) <EOL> return org_governance_name <EOL> def negotiate ( self , resource_id = "<STR_LIT>" , sap_in = None ) : <EOL> pass <EOL> def execute ( self , resource_id = "<STR_LIT>" , command = None ) : <EOL> return self . _execute ( "<STR_LIT>" , command ) <EOL> def execute_agent ( self , resource_id = "<STR_LIT>" , command = None ) : <EOL> return self . _execute ( "<STR_LIT>" , command ) <EOL> def _execute ( self , cprefix , command ) : <EOL> if not command : <EOL> raise iex . BadRequest ( "<STR_LIT>" ) <EOL> if not command . command : <EOL> raise iex . BadRequest ( "<STR_LIT>" ) <EOL> cmd_res = IonObject ( "<STR_LIT>" , command_id = command . command_id , command = command . command ) <EOL> cmd_func = getattr ( self , cprefix + str ( command . command ) , None ) <EOL> if cmd_func : <EOL> cmd_res . ts_execute = get_ion_ts ( ) <EOL> try : <EOL> res = cmd_func ( * command . args , ** command . kwargs ) <EOL> cmd_res . status = <NUM_LIT:0> <EOL> cmd_res . result = res <EOL> except iex . IonException as ex : <EOL> cmd_res . status = getattr ( ex , '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> cmd_res . result = str ( ex ) <EOL> log . warn ( "<STR_LIT>" % ( command . command , traceback . format_exc ( ) ) ) <EOL> else : <EOL> log . info ( "<STR_LIT>" % ( command . command ) ) <EOL> ex = iex . NotFound ( "<STR_LIT>" % command . command ) <EOL> cmd_res . status = iex . NotFound . status_code <EOL> cmd_res . result = str ( ex ) <EOL> sub_type = "<STR_LIT>" % ( command . command , cmd_res . status ) <EOL> event_data = self . _post_execute_event_hook ( event_type = self . COMMAND_EVENT_TYPE , <EOL> origin = self . resource_id , origin_type = self . ORIGIN_TYPE , <EOL> sub_type = sub_type , command = str ( command . command ) , <EOL> result = str ( cmd_res . result ) ) <EOL> post_event = self . _event_publisher . publish_event ( ** event_data ) <EOL> return cmd_res <EOL> def _post_execute_event_hook ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return kwargs <EOL> def get_capabilities ( self , resource_id = "<STR_LIT>" , capability_types = [ ] ) : <EOL> capability_types = capability_types or [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> cap_list = [ ] <EOL> if "<STR_LIT>" in capability_types : <EOL> cap_list . extend ( [ ( "<STR_LIT>" , cap ) for cap in self . _get_agent_conv_types ( ) ] ) <EOL> if "<STR_LIT>" in capability_types : <EOL> cap_list . extend ( [ ( "<STR_LIT>" , cap ) for cap in self . _get_agent_commands ( ) ] ) <EOL> if "<STR_LIT>" in capability_types : <EOL> cap_list . extend ( [ ( "<STR_LIT>" , cap ) for cap in self . _get_agent_params ( ) ] ) <EOL> if "<STR_LIT>" in capability_types : <EOL> cap_list . extend ( [ ( "<STR_LIT>" , cap ) for cap in self . _get_resource_commands ( ) ] ) <EOL> if "<STR_LIT>" in capability_types : <EOL> cap_list . extend ( [ ( "<STR_LIT>" , cap ) for cap in self . _get_resource_params ( ) ] ) <EOL> return cap_list <EOL> def set_param ( self , resource_id = "<STR_LIT>" , name = '<STR_LIT>' , value = '<STR_LIT>' ) : <EOL> if not hasattr ( self , "<STR_LIT>" % name ) : <EOL> raise iex . NotFound ( '<STR_LIT>' % name ) <EOL> pvalue = getattr ( self , "<STR_LIT>" % name ) <EOL> setattr ( self , "<STR_LIT>" % name , value ) <EOL> return pvalue <EOL> def get_param ( self , resource_id = "<STR_LIT>" , name = '<STR_LIT>' ) : <EOL> try : <EOL> return getattr ( self , "<STR_LIT>" % name ) <EOL> except AttributeError : <EOL> raise iex . NotFound ( '<STR_LIT>' % name ) <EOL> def set_agent_param ( self , resource_id = "<STR_LIT>" , name = '<STR_LIT>' , value = '<STR_LIT>' ) : <EOL> if not hasattr ( self , "<STR_LIT>" % name ) : <EOL> raise iex . NotFound ( '<STR_LIT>' % name ) <EOL> pvalue = getattr ( self , "<STR_LIT>" % name ) <EOL> setattr ( self , "<STR_LIT>" % name , value ) <EOL> return pvalue <EOL> def get_agent_param ( self , resource_id = "<STR_LIT>" , name = '<STR_LIT>' ) : <EOL> try : <EOL> return getattr ( self , "<STR_LIT>" % name ) <EOL> except AttributeError : <EOL> raise iex . NotFound ( '<STR_LIT>' % name ) <EOL> def _get_agent_conv_types ( self ) : <EOL> return [ ] <EOL> def _get_agent_params ( self ) : <EOL> return self . _get_names ( self , "<STR_LIT>" ) <EOL> def _get_agent_commands ( self ) : <EOL> return self . _get_names ( self , "<STR_LIT>" ) <EOL> def _get_resource_params ( self ) : <EOL> return self . _get_names ( self , "<STR_LIT>" ) <EOL> def _get_resource_commands ( self ) : <EOL> return self . _get_names ( self , "<STR_LIT>" ) <EOL> def _get_names ( self , obj , prefix ) : <EOL> return [ name [ len ( prefix ) : ] for name in dir ( obj ) if name . startswith ( prefix ) ] <EOL> class UserAgent ( SimpleResourceAgent ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> SimpleResourceAgent . __init__ ( self ) <EOL> self . resource_type = RT . ActorIdentity <EOL> class SimpleResourceAgentClient ( SimpleResourceAgentProcessClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource_id , * args , ** kwargs ) : <EOL> assert resource_id , "<STR_LIT>" <EOL> self . resource_id = resource_id <EOL> if not '<STR_LIT:name>' in kwargs : <EOL> process_id = self . _get_agent_process_id ( self . resource_id ) <EOL> if process_id : <EOL> kwargs [ '<STR_LIT:name>' ] = process_id <EOL> log . debug ( "<STR_LIT>" % ( process_id , self . resource_id ) ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" % self . resource_id ) <EOL> raise iex . NotFound ( "<STR_LIT>" % self . resource_id ) <EOL> assert "<STR_LIT:name>" in kwargs , "<STR_LIT>" <EOL> SimpleResourceAgentProcessClient . __init__ ( self , * args , ** kwargs ) <EOL> def negotiate ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . negotiate ( self . resource_id , * args , ** kwargs ) <EOL> def get_capabilities ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . get_capabilities ( self . resource_id , * args , ** kwargs ) <EOL> def execute ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . execute ( self . resource_id , * args , ** kwargs ) <EOL> def get_param ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . get_param ( self . resource_id , * args , ** kwargs ) <EOL> def set_param ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . set_param ( self . resource_id , * args , ** kwargs ) <EOL> def emit ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . emit ( self . resource_id , * args , ** kwargs ) <EOL> def execute_agent ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . execute_agent ( self . resource_id , * args , ** kwargs ) <EOL> def get_agent_param ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . get_agent_param ( self . resource_id , * args , ** kwargs ) <EOL> def set_agent_param ( self , * args , ** kwargs ) : <EOL> return super ( SimpleResourceAgentClient , self ) . set_agent_param ( self . resource_id , * args , ** kwargs ) <EOL> @ classmethod <EOL> def _get_agent_process_id ( cls , resource_id ) : <EOL> agent_procs = bootstrap . container_instance . directory . find_by_value ( '<STR_LIT>' , '<STR_LIT>' , resource_id ) <EOL> if agent_procs : <EOL> if len ( agent_procs ) > <NUM_LIT:1> : <EOL> log . warn ( "<STR_LIT>" % ( <EOL> resource_id , agent_procs ) ) <EOL> agent_id = agent_procs [ <NUM_LIT:0> ] . key <EOL> return str ( agent_id ) <EOL> return None </s>
