<s> """<STR_LIT>""" <EOL> import json <EOL> import os <EOL> import ApiConstants <EOL> class MockMoirai ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , input_queue , event_queue , api_config ) : <EOL> self . atropos_data_queue = input_queue <EOL> self . atropos_event_queue = event_queue <EOL> self . api_config = api_config <EOL> self . event_map = { } <EOL> self . service_map = { } <EOL> def start_moirai ( self ) : <EOL> print "<STR_LIT>" <EOL> self . api_fifo_read ( ) <EOL> def api_fifo_read ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if not os . path . exists ( self . api_config [ '<STR_LIT>' ] ) : <EOL> os . mkfifo ( self . api_config [ '<STR_LIT>' ] ) <EOL> print ( "<STR_LIT>" ) <EOL> self . fifo_fd = open ( self . api_config [ '<STR_LIT>' ] ) <EOL> except ( IOError , OSError ) : <EOL> print ( "<STR_LIT>" ) <EOL> raise <EOL> while True : <EOL> skip_iteration_flag = False <EOL> request_json = self . fifo_fd . readline ( ) <EOL> if request_json : <EOL> try : <EOL> request_map = json . loads ( request_json ) <EOL> except ValueError : <EOL> print ( "<STR_LIT>" ) <EOL> print ( request_json ) <EOL> continue <EOL> required_keys = [ ApiConstants . FIFO_ENDPOINT_KEY , <EOL> ApiConstants . COMMAND_ID_KEY , <EOL> ApiConstants . COMMAND_KEY , <EOL> ApiConstants . ARGS_KEY , <EOL> ApiConstants . METHOD_KEY ] <EOL> for arr_key in required_keys : <EOL> if arr_key not in request_map . keys ( ) : <EOL> print ( "<STR_LIT>" <EOL> . format ( arr_key ) ) <EOL> skip_iteration_flag = True <EOL> if skip_iteration_flag : <EOL> continue <EOL> fifo_endpoint = request_map [ ApiConstants . FIFO_ENDPOINT_KEY ] <EOL> command_id = request_map [ ApiConstants . COMMAND_ID_KEY ] <EOL> command = request_map [ ApiConstants . COMMAND_KEY ] <EOL> args = request_map [ ApiConstants . ARGS_KEY ] <EOL> method = request_map [ ApiConstants . METHOD_KEY ] <EOL> if command not in ApiConstants . API_COMMANDS : <EOL> output_msg = { ApiConstants . COMMAND_ID_KEY : command_id , <EOL> ApiConstants . COMMAND_OUTPUT_KEY : <EOL> "<STR_LIT>" } <EOL> else : <EOL> if method == "<STR_LIT:GET>" : <EOL> moirai_function = ( ApiConstants . <EOL> MOIRAI_GET_COMMAND_PREFIX + <EOL> command ) <EOL> else : <EOL> moirai_function = ( ApiConstants . <EOL> MOIRAI_OTHER_COMMAND_PREFIX + <EOL> command ) <EOL> try : <EOL> command_handler = getattr ( self , moirai_function ) <EOL> command_output = command_handler ( args ) <EOL> output_msg = { ApiConstants . COMMAND_ID_KEY : command_id , <EOL> ApiConstants . COMMAND_OUTPUT_KEY : <EOL> command_output } <EOL> except : <EOL> output_msg = { ApiConstants . COMMAND_ID_KEY : command_id , <EOL> ApiConstants . COMMAND_OUTPUT_KEY : <EOL> "<STR_LIT>" } <EOL> with open ( fifo_endpoint , '<STR_LIT:w>' ) as output_fd : <EOL> output_fd . write ( json . dumps ( output_msg ) ) <EOL> def update_service_map ( self ) : <EOL> """<STR_LIT>""" <EOL> while not self . atropos_data_queue . empty ( ) : <EOL> service , servers , plan = self . atropos_data_queue . get ( ) <EOL> if service in self . service_map . keys ( ) : <EOL> print ( "<STR_LIT>" + service + <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . service_map [ service ] = ( servers , plan ) <EOL> def update_event_map ( self ) : <EOL> """<STR_LIT>""" <EOL> while not self . atropos_event_queue . empty ( ) : <EOL> ( service , failure_name , trigger_time , node_name , <EOL> trigger_status ) = self . atropos_event_queue . get ( ) <EOL> if service in self . event_map . keys ( ) : <EOL> self . event_map [ service ] . append ( [ failure_name , trigger_time , <EOL> node_name , trigger_status ] ) <EOL> else : <EOL> self . event_map [ service ] = [ [ failure_name , trigger_time , <EOL> node_name , trigger_status ] ] <EOL> def api_get_list ( self , args ) : <EOL> """<STR_LIT>""" <EOL> self . update_service_map ( ) <EOL> return json . dumps ( self . service_map . keys ( ) ) <EOL> def api_get_plan ( self , args ) : <EOL> """<STR_LIT>""" <EOL> self . update_service_map ( ) <EOL> service_name = args [ '<STR_LIT>' ] <EOL> servers , plan = self . service_map [ service_name ] <EOL> return json . dumps ( plan ) <EOL> def api_get_servers ( self , args ) : <EOL> """<STR_LIT>""" <EOL> self . update_service_map ( ) <EOL> service_name = args [ '<STR_LIT>' ] <EOL> servers , plan = self . service_map [ service_name ] <EOL> return json . dumps ( servers ) <EOL> def api_get_events ( self , args ) : <EOL> """<STR_LIT>""" <EOL> self . update_event_map ( ) <EOL> service_name = args [ '<STR_LIT>' ] <EOL> if service_name not in self . event_map . keys ( ) : <EOL> return json . dumps ( [ ( ) ] ) <EOL> return json . dumps ( self . event_map [ service_name ] ) </s>
