def communicatewithManager(self, data, Ftype): # Implement a channel to communicate with manager
        self.params['data'] = data
        self.params['type'] = Ftype
        request_id = utils.generate_request_id()
        toSend = {
            'function':'configuration-generation',
            'parameters':self.params,
            'tenant_id':os.environ.get("AURORA_TENANT", config.CONFIG['tenant_info']['tenant_id']),
            'project_id':os.environ.get("AURORA_PROJECT", config.CONFIG['tenant_info']['project_id']),
            'user_id':os.environ.get("AURORA_USER", config.CONFIG['tenant_info']['user_id']),
            'request_id': request_id
        }

        manager_address = 'http://' + config.CONFIG['connection']['manager_host'] + ':' + config.CONFIG['connection']['manager_port']
        if toSend: #--help commands will not start the server
            response = json_sender.JSONSender().send_json(manager_address, toSend, request_id)
        return response
Beispiel #2
0
    def _send_to_server(self, sending_address, function, params):
        tenant_id = os.environ.get("AURORA_TENANT", config.CONFIG['tenant_info']['tenant_id'])
        project_id = os.environ.get("AURORA_PROJECT", config.CONFIG['tenant_info']['project_id'])
        user_id = os.environ.get("AURORA_USER", config.CONFIG['tenant_info']['user_id'])

        request_id = utils.generate_request_id()
        ## print params
        to_send = {
            'function':function,
            'parameters':params,
            'tenant_id': tenant_id,
            'project_id': project_id,
            'user_id': user_id,
            'request_id': request_id
        }
        ##FOR DEBUGGING PURPOSES
        #pprint(to_send)
        ##END DEBUG
        
        if to_send:
            message = json_sender.JSONSender().send_json(sending_address, to_send, request_id) # change back to 132.206.206.133:5554
        return message
Beispiel #3
0
    def request_translation(self, trans_request):
        translator = trans_request.translator
        source_lang = trans_request.translation_project.project.source_language
        target_lang = trans_request.translation_project.language
        if not translator.is_language_pair_supported(source_lang, target_lang):
            raise UnsupportedLanguagePair(translator, source_lang, target_lang)

        if translator.type == SERVERLAND:
            request_id = utils.generate_request_id()
            shortname = "wt_%s" % request_id
            src = utils.get_iso639_2(source_lang.code)
            tgt = utils.get_iso639_2(target_lang.code)
            contents = {
                "token": str(self.token),
                "shortname": str(shortname),
                "worker": str(translator.shortname),
                "source_language": str(src),
                "target_language": str(tgt),
            }
            source_file_id = "%s-%s-%s" % (request_id, source_lang.code, target_lang.code)
            store = Store.objects.get(translation_project=trans_request.translation_project)
            sentences = [unicode(unit) for unit in store.units]

            boundary = "-----" + mimetools.choose_boundary() + "-----"
            body = utils.generate_request_body(boundary, contents, source_file_id, sentences)
            header = utils.generate_request_header(boundary, body)

            response = self.request(str(self.url) + "requests/", method="POST", body=body, header=header)

            print "Submitted new request..."
            print "=> Response status:", response[0].status
            print "=> Reason:", response[0].reason
            print "=> Shortname:", shortname

            trans_request.external_id = shortname
            trans_request.status = STATUS_IN_PROGRESS
            trans_request.save()
Beispiel #4
0
 def setUp(self):
     self.filtered_tender_ids_queue = Queue(10)
     self.edrpou_codes_queue = Queue(10)
     self.db = Db(config)
     self.process_tracker = ProcessTracker(self.db)
     self.tender_id = uuid.uuid4().hex
     self.filtered_tender_ids_queue.put(self.tender_id)
     self.sleep_change_value = APIRateController()
     self.client = MagicMock()
     self.sna = event.Event()
     self.sna.set()
     self.worker = FilterTenders.spawn(self.client, self.filtered_tender_ids_queue, self.edrpou_codes_queue,
                                       self.process_tracker, self.sna, self.sleep_change_value)
     self.bid_ids = [uuid.uuid4().hex for _ in range(5)]
     self.qualification_ids = [uuid.uuid4().hex for _ in range(5)]
     self.award_ids = [uuid.uuid4().hex for _ in range(5)]
     self.request_ids = [generate_request_id() for _ in range(2)]
     self.response = ResponseMock({'X-Request-ID': self.request_ids[0]},
                                  munchify({'prev_page': {'offset': '123'},
                                            'next_page': {'offset': '1234'},
                                            'data': {'status': "active.pre-qualification",
                                                     'id': self.tender_id,
                                                     'procurementMethodType': 'aboveThresholdEU',
                                                     'awards': [self.awards(0, 0, 'pending', CODES[0])]}}))
Beispiel #5
0
def request_translation(translator, sentences, source_language,
                        target_language):
    """
    Request a MachineTranslator to perform a translation. The request process is implemented
    based on the type of MachineTranslator. For example, a SERVERLAND type uses a MT Server Land
    to request a translation.
    Preconditions:
        translator:    A MachineTranslator object.
        sentences:     A list of strings.
    Exceptions:
        UnsupportedLanguagePair:The target translator doesn't support the language pair.
        UndefinedTranslator:    When looking up the ServerlandHost for a MachineTranslator, if none exists (this should not happen).
        ServerlandConfigError:  The ServerlandHost has an error.
    """

    # Make sure that the translator supports the language pair
    if not translator.is_language_pair_supported(source_language,
                                                 target_language):
        raise UnsupportedLanguagePair(translator, source_language,
                                      target_language)

    # Generate a request id
    request_id = utils.generate_request_id()

    # Make sure that sentences is a list.
    sentences = utils.cast_to_list(sentences)
    #    if not isinstance(sentences, list):
    #        print "Retrieved only one sentence."
    #        sentences = [sentences]

    # One sentence per line, to make it easier for the translator to do its job.
    text = "\n".join(sentence for sentence in sentences)

    # Determine the machine translator type.
    if translator.type == SERVERLAND:
        # Get the ServerlandHost
        try:
            # TODO: Change this host reference. Currently, only one ServerLand host is allowed.
            serverland_host = translator.serverlandhost_set.all()[0]
        except IndexError:
            raise UndefinedTranslator(translator)

        source_file_id = "%s-%s-%s" % (request_id, source_language.code,
                                       target_language.code)

        try:
            #            print "Requesting the translation"
            #            print serverland_host.token, request_id, translator.shortname
            #            print utils.get_iso639_2(source_language.code), utils.get_iso639_2(target_language.code)
            #            print source_file_id
            #            print text

            # Request the translation.
            proxy = xmlrpclib.ServerProxy(serverland_host.url)
            result = proxy.create_translation(
                serverland_host.token,  # Authentication token
                request_id,  # Custom request ID
                translator.shortname,  # MT ServerLand worker
                utils.get_iso639_2(source_language.code
                                   ),  # Source language (in bibliographic)
                utils.get_iso639_2(target_language.code
                                   ),  # Target language (in bibliographic)
                source_file_id,  # Composite id
                text)  # Sentence(s) to translate

            # TODO: For now, just print the results.
            print result

            # TODO: Return the request_id
            return request_id
        except xmlrpclib.Fault as ex:
            raise ServerlandConfigError(serverland_host, ex)
Beispiel #6
0
def before_request():
    """Generate a unique request ID."""
    flask.g.request_id = utils.generate_request_id()
def request_translation(translator, sentences, source_language, target_language):
    """
    Request a MachineTranslator to perform a translation. The request process is implemented
    based on the type of MachineTranslator. For example, a SERVERLAND type uses a MT Server Land
    to request a translation.
    Preconditions:
        translator:    A MachineTranslator object.
        sentences:     A list of strings.
    Exceptions:
        UnsupportedLanguagePair:The target translator doesn't support the language pair.
        UndefinedTranslator:    When looking up the ServerlandHost for a MachineTranslator, if none exists (this should not happen).
        ServerlandConfigError:  The ServerlandHost has an error.
    """
    
    # Make sure that the translator supports the language pair
    if not translator.is_language_pair_supported(source_language, target_language):
        raise UnsupportedLanguagePair(translator, source_language, target_language)
    
    # Generate a request id
    request_id = utils.generate_request_id()
    
    # Make sure that sentences is a list.
    sentences = utils.cast_to_list(sentences)
#    if not isinstance(sentences, list):
#        print "Retrieved only one sentence."
#        sentences = [sentences]
    
    # One sentence per line, to make it easier for the translator to do its job.
    text = "\n".join(sentence for sentence in sentences)
        
    # Determine the machine translator type.
    if translator.type == SERVERLAND:
        # Get the ServerlandHost
        try:
            # TODO: Change this host reference. Currently, only one ServerLand host is allowed.
            serverland_host = translator.serverlandhost_set.all()[0]
        except IndexError:
            raise UndefinedTranslator(translator)
        
        source_file_id = "%s-%s-%s" % (request_id, source_language.code, target_language.code)
        
        try:
#            print "Requesting the translation"
#            print serverland_host.token, request_id, translator.shortname
#            print utils.get_iso639_2(source_language.code), utils.get_iso639_2(target_language.code)
#            print source_file_id
#            print text
            
            # Request the translation.
            proxy = xmlrpclib.ServerProxy(serverland_host.url)
            result = proxy.create_translation(serverland_host.token,            # Authentication token
                                     request_id,                                # Custom request ID
                                     translator.shortname,                      # MT ServerLand worker
                                     utils.get_iso639_2(source_language.code),  # Source language (in bibliographic)
                                     utils.get_iso639_2(target_language.code),  # Target language (in bibliographic)
                                     source_file_id,                            # Composite id
                                     text)                                      # Sentence(s) to translate
            
            # TODO: For now, just print the results.
            print result
            
            # TODO: Return the request_id
            return request_id
        except xmlrpclib.Fault as ex:
            raise ServerlandConfigError(serverland_host, ex)
Beispiel #8
0
import uuid

from simplejson import dumps
from gevent.queue import Queue
from bottle import response, request

from base import BaseServersTest, config
from bot.dfs.bridge.constants import tender_status, AWARD_STATUS
from bot.dfs.bridge.sleep_change_value import APIRateController
from bot.dfs.bridge.process_tracker import ProcessTracker
from bot.dfs.bridge.bridge import EdrDataBridge
from utils import generate_request_id, custom_sleep, sleep_until_done, is_working_filter

CODES = ('14360570', '0013823', '23494714')
award_ids = [uuid.uuid4().hex for _ in range(5)]
request_ids = [generate_request_id() for _ in range(2)]
bid_ids = [uuid.uuid4().hex for _ in range(5)]

s = 0


def setup_routing(app, func, path='/api/2.3/spore', method='GET'):
    global s
    s = 0
    app.route(path, method, func)


def response_spore():
    response.set_cookie("SERVER_ID",
                        ("a7afc9b1fc79e640f2487ba48243ca071c07a823d27"
                         "8cf9b7adf0fae467a524747e3c6c6973262130fac2b"
Beispiel #9
0
    def main(self, argv):
        if(len(argv) < 2):
            print('Error! Unexpected number of arguments.')
        else:
            function = argv[1] # Used for attrs function call
            manager_addr = config.CONFIG['connection']['manager_host']

            parser = AuroraArgumentParser()
            params = vars(parser.base_parser().parse_args(argv[1:]))

            #print 'function: %s' % function

            #For add-slice and modify, we need to load the JSON file
            if function == 'ap-slice-create':
                if not (params['file'] or params['hint']):                
                    print 'Please specify a file argument or hint!'
                    return
                elif params['file'] and not params['hint']:
                    try:
                        JFILE = open(os.path.join(CLIENT_DIR, 'json', params['file'][0]), 'r')
                        #print JFILE
                        file_content = json.load(JFILE)
                        params['file'] = file_content
                        JFILE.close()
                    except:
                        print('Error loading json file!')
                        sys.exit(-1)
            #Authenticate

#            try:
#                authInfo = self.authenticate()
#            except:
#                print 'Invalid Credentials!'
#                sys.exit(-1)       
            #if params.get('hint') is not None: #--"hint" token, store params['file'] for the third sent
             #   store = params['file'] 
             #   params['file']= None
                
            tenant_id = os.environ.get("AURORA_TENANT", config.CONFIG['tenant_info']['tenant_id'])
            project_id = os.environ.get("AURORA_PROJECT", config.CONFIG['tenant_info']['project_id'])
            user_id = os.environ.get("AURORA_USER", config.CONFIG['tenant_info']['user_id'])
            sending_address = 'http://' + config.CONFIG['connection']['manager_host'] + ':' + config.CONFIG['connection']['manager_port']
            #We will send in the following format: {function:"",parameters:""}
            msg = self._send_to_server(sending_address, function, params)

            try:
                arg_hint = params.get('hint')
                arg_ap = params.get('ap')

                if arg_hint is None:
                    sys.exit(0)
                if "location" in arg_hint or "location,slice-load" in arg_hint: # Once the token is 'hint', wait for users' reply
                    ap_locations = msg.get('ap_location')
                    if msg['ap_location'] == "": # in case of APs are full
                        print "Warning: No AP is currently available!!!"
                        sys.exit(0)

                    exitLoop = False
                    while not exitLoop:
                        if params.get('location') is None:
                            print ap_locations
                            print "Please Enter the location where the AP you want to access:"
                            print "(Enter 0 to leave otherwise)"

                            choice = raw_input()
                        else:
                            choice = params.get('location')[0]
                        if choice == '0':
                            exitLoop = True;
                        elif len(choice) > 0: # Unable to do a logic checking here due to the lack of data at the file
                            print "the location is " + choice
                            to_send = None
                            if params.get('location') is None:
                                ##############Send the information back again to the manager###############
                                params['location'] = [choice]
                                if arg_ap:
                                    params['favored_ap'] = arg_ap

                                request_id = utils.generate_request_id()
                                to_send = {
                                    'function':function,
                                    'parameters':params,
                                    'tenant_id': tenant_id,
                                    'project_id': project_id,
                                    'user_id': user_id,
                                    'request_id': request_id,
                                }
                            
                            if to_send:
                                message = json_sender.JSONSender().send_json(sending_address, to_send, request_id)
                            else:
                                message = msg

                            GEN_JSON_FNAME = 'gen_config.json'
                            GEN_JSON_FPATH = os.path.join(CLIEN_JSON_DIR, GEN_JSON_FNAME)

                            if message is not None: # Restore params['file'] and clean up params['hint'] to create a slice
                                if len(message['ap_location']) == 0: #There is no chosen ap
                                    print "Cannot locate an available AP based on provided information"
                                    break

                                params['hint'] = None
                                del params['location']
                                params['ap'] = [message['ap']]

                                if message['ssid'] is None:
                                    params['ssid'] = None
                                    ssid = None
                                    print "ssid is not provided or invalid"
                                else:
                                    ssid = params['ssid'][0]

                                if message['bridge']['flavor'] is None:
                                    bridge = None
                                    params['bridge'] = None
                                    print "bridge is not provided or invalid"
                                else:
                                    bridge = {}
                                    bridge['name'] = params['bridge'][0]

                                user_config = {
                                    'ap': params['ap'][0],
                                    'ssid': ssid,
                                    'bridge': bridge,
                                }

                                self.slice_json_generator = \
                                slice_json_generator.SliceJsonGenerator(
                                    user_config,
                                    GEN_JSON_FPATH,
                                    1, tenant_id, project_id
                                ) # Initialize the slice_json_generator

                                params['file'] = [GEN_JSON_FPATH,]

                                try:
                                    JFILE = open(os.path.join(CLIENT_DIR, 'json', params['file'][0]), 'r')
                                    #print JFILE
                                    file_content = json.load(JFILE)
                                    params['file'] = file_content
                                    JFILE.close()
                                except:
                                    print('Error loading json file!')
                                    sys.exit(-1)

                                message = self._send_to_server(sending_address, function, params)
                            exitLoop = True;
                        else:
                            exitLoop = True;
                            print "Invalid information"
            except Exception as e:
                traceback.print_exc(file=sys.stdout)
                print "Finshed the current statement"