Esempio n. 1
0
def get_model_data(model_id):
    with app.app_context():
        try:
            debug_log.debug(
                "getting model data given model_id = {}".format(model_id))
            d = dict()
            connection = get_db()
            cursor = connection.cursor(buffered=True)
            query = "SELECT model_id, model_content, description, " \
                    "solver_type  FROM optim_model_data WHERE model_id = %s"
            values = (model_id, )
            cursor.execute(query, values)
            if cursor is None:
                return 400, "FAILED"
            else:
                rows = cursor.fetchone()
                if rows is not None:
                    index = 0
                    for row in rows:
                        d[index] = row
                        index = index + 1
                    return 200, d
                else:
                    close_db()
                    return 500, "NOT_FOUND"
        except Exception:
            error_log.error("error for request_id: {} - {}".format(
                model_id, traceback.format_exc()))
            close_db()
            return 500, "FAILED"
Esempio n. 2
0
def cleanup(file_tup):
    for f in file_tup:
        try:
            delete_file_folder(f)
        except Exception as e:
            error_log.error("Failed deleting the file {} - {}".format(
                f, traceback.format_exc()))
Esempio n. 3
0
    def process_slice_selection_opt(self):
        """Process the slice selection request from the API layer"""
        req_info = self.request_json['requestInfo']
        rc = get_rest_client(self.request_json, service='so')

        try:
            if self.model_type == 'NSSI' \
                    and self.request_json['sliceProfile'].get('resourceSharingLevel', "") \
                    in ['not-shared', 'non-shared']:
                final_response = self.response_processor.get_slice_selection_response([])

            else:
                final_response = self.do_slice_selection()

        except Exception as ex:
            error_log.error("Error for {} {}".format(req_info.get('requestId'),
                                                     traceback.format_exc()))
            error_message = str(ex)
            final_response = self.response_processor.process_error_response(error_message)

        try:
            rc.request(json=final_response, noresponse=True)
        except RequestException:
            error_log.error("Error sending asynchronous notification for {} {}".format(req_info['request_id'],
                                                                                       traceback.format_exc()))
Esempio n. 4
0
def internal_failure(error):
    """Returned when unexpected coding errors occur during initial synchronous processing

    """
    error_log.error("Synchronous error for request id {} {}".format(g.request_id, traceback.format_exc()))
    response = Response(internal_error_message, content_type='application/json; charset=utf-8')
    response.status_code = 500
    return response
Esempio n. 5
0
def authenticate(uid, passwd):
    try:
        perms = get_aaf_permissions(uid, passwd)
        return has_valid_role(perms)
    except Exception as exp:
        error_log.error("Error Authenticating the user {} : {}: ".format(
            uid, exp))
    return False
Esempio n. 6
0
def handle_request_exception(e):
    """Returns a detailed synchronous message to the calling client when osdf fails due to a remote call to another system

    """
    error_log.error("Synchronous error for request id {} {}".format(g.request_id, traceback.format_exc()))
    err_msg = request_exception_to_json_body(e)
    response = Response(err_msg, content_type='application/json; charset=utf-8')
    response.status_code = 400
    return response
Esempio n. 7
0
def handle_business_exception(e):
    """An exception explicitly raised due to some business rule

    """
    error_log.error("Synchronous error for request id {} {}"
                    .format(g.request_id, traceback.format_exc()))
    err_msg = ERROR_TEMPLATE.render(description=str(e))
    response = Response(err_msg, content_type='application/json; charset=utf-8')
    response.status_code = 400
    return response
Esempio n. 8
0
def log_response_data(response):
    status_value = ''
    try:
        status_value = map_status_value(response)
        log_message(response.get_data(as_text=True), status_value, 'EXIT')
    except Exception:
        try:
            set_default_audit_mdc(request, status_value, 'EXIT')
            audit_log.info(response.get_data(as_text=True))
        except Exception:
            set_error_details(300, 'Internal Error')
            error_log.error("Error logging the response data due to {}".format(traceback.format_exc()))
Esempio n. 9
0
def process_pci_optimation(request_json, osdf_config, flat_policies):
    """Process a PCI request from a Client (build config-db, policy and  API call, make the call, return result)

    :param req_object: Request parameters from the client
    :param osdf_config: Configuration specific to OSDF application (core + deployment)
    :param flat_policies: policies related to pci (fetched based on request)
    :return: response from PCI Opt
    """
    try:
        mdc_from_json(request_json)
        rc = get_rest_client(request_json, service="pcih")
        req_id = request_json["requestInfo"]["requestId"]
        cell_info_list, network_cell_info = config_request(request_json, osdf_config, flat_policies)
        pci_response = get_solutions(cell_info_list, network_cell_info, request_json)

        metrics_log.info(MH.inside_worker_thread(req_id))
    except Exception as err:
        error_log.error("Error for {} {}".format(req_id, traceback.format_exc()))

        try:
            body = build_json_error_body(err)
            metrics_log.info(MH.sending_response(req_id, "ERROR"))
            rc.request(json=body, noresponse=True)
        except RequestException:
            MDC.put('requestID', req_id)
            error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
        raise err

    try:
        metrics_log.info(MH.calling_back_with_body(req_id, rc.url, pci_response))
        error_log.error("pci response: {}".format(str(pci_response)))
        rc.request(json=pci_response, noresponse=True)
    except RequestException:  # can't do much here but log it and move on
        error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
Esempio n. 10
0
def build_solution_list(cell_info_list, network_cell_info, request_json):
    status = "success"
    req_id = request_json["requestInfo"]["requestId"]
    pci_solutions = []
    anr_solutions = []
    try:
        opt_solution = optimize(network_cell_info, cell_info_list, request_json)
        if opt_solution == 'UNSATISFIABLE':
            status = 'inconsistent input'
            return status, pci_solutions, anr_solutions
        else:
            pci_solutions = build_pci_solution(network_cell_info, opt_solution['pci'])
            anr_solutions = build_anr_solution(network_cell_info, opt_solution.get('removables', {}))
    except RuntimeError:
        error_log.error("Failed finding solution for {} {}".format(req_id, traceback.format_exc()))
        status = "failed"
    return status, pci_solutions, anr_solutions
Esempio n. 11
0
def handle_data_error(e):
    """Returns a detailed message to the calling client when the initial synchronous message is invalid

    """
    error_log.error("Synchronous error for request id {} {}".format(g.request_id, traceback.format_exc()))

    body_dictionary = {
        "serviceException": {
            "text": BAD_CLIENT_REQUEST_MESSAGE,
            "exceptionMessage": str(e.errors),
            "errorType": "InvalidClientRequest"
        }
    }

    body_as_json = json.dumps(body_dictionary)
    response = Response(body_as_json, content_type='application/json; charset=utf-8')
    response.status_code = 400
    return response
Esempio n. 12
0
    def get_ho_details(self, cell_id):
        service_id = self.config['DES']['service_id']
        request_data = self.config['DES']['filter']
        request_data['cell_id'] = cell_id
        try:
            result = des.extract_data(service_id, json.dumps(request_data))
        except DESException as e:
            error_log.error("Error while calling DES {}".format(e))
            return 0, 0

        if not result:
            return 0, 0

        ho_list = []
        for pm_data in result:
            ho = pm_data['overallHoAtt']
            ho_list.append(ho)

        return sum(ho_list) / len(ho_list), ho_list[0]
Esempio n. 13
0
def py_solver(py_content, opt_info):
    py_file = '/tmp/custom_heuristics_{}.py'.format(
        datetime.timestamp(datetime.now()))
    with open(py_file, "wt") as f:
        f.write(py_content)
    if opt_info['optData'].get('json'):
        data_content = json.dumps(opt_info['optData']['json'])
        input_file = '/tmp/optim_engine_{}.json'.format(
            datetime.timestamp(datetime.now()))
    elif opt_info['optData'].get('text'):
        data_content = opt_info['optData']['text']
        input_file = '/tmp/optim_engine_{}.txt'.format(
            datetime.timestamp(datetime.now()))
    with open(input_file, "wt") as f:
        f.write(data_content)

    output_file = '/tmp/opteng_output_{}.json'.format(
        datetime.timestamp(datetime.now()))

    command = ['python', py_file, input_file, output_file]

    try:
        p = subprocess.run(command,
                           stderr=subprocess.STDOUT,
                           stdout=subprocess.PIPE)

        debug_log.debug('Process return code {}'.format(p.returncode))
        if p.returncode > 0:
            error_log.error('Process return code {} {}'.format(
                p.returncode, p.stdout))
            return 'error', {}
        with open(output_file) as file:
            data = file.read()
            return 'success', json.loads(data)

    except Exception as e:
        error_log.error("Error running optimizer {}".format(
            traceback.format_exc()))
        return 'error', {}
    finally:
        cleanup((input_file, output_file, py_file))
Esempio n. 14
0
def create_model_data(model_api):
    with app.app_context():
        try:
            model_info = model_api['modelInfo']
            model_id = model_info['modelId']
            debug_log.debug("persisting model_api {}".format(model_id))
            connection = get_db()
            cursor = connection.cursor(buffered=True)
            query = "SELECT model_id FROM optim_model_data WHERE model_id = %s"
            values = (model_id, )
            cursor.execute(query, values)
            if cursor.fetchone() is None:
                query = "INSERT INTO optim_model_data (model_id, model_content, description, solver_type) VALUES " \
                        "(%s, %s, %s, %s)"
                values = (model_id, model_info['modelContent'],
                          model_info.get('description'), model_info['solver'])
                cursor.execute(query, values)
                g.pg.commit()

                debug_log.debug(
                    "A record successfully inserted for request_id: {}".format(
                        model_id))
                return retrieve_model_data(model_id)
                close_db()
            else:
                query = "UPDATE optim_model_data SET model_content = %s, description = %s, solver_type = %s where " \
                        "model_id = %s "
                values = (model_info['modelContent'],
                          model_info.get('description'), model_info['solver'],
                          model_id)
                cursor.execute(query, values)
                g.pg.commit()

                return retrieve_model_data(model_id)
                close_db()
        except Exception as err:
            error_log.error("error for request_id: {} - {}".format(
                model_id, traceback.format_exc()))
            close_db()
            raise BusinessException(err)
Esempio n. 15
0
def delete_model_data(model_id):
    with app.app_context():
        try:
            debug_log.debug(
                "deleting model data given model_id = {}".format(model_id))
            connection = get_db()
            cursor = connection.cursor(buffered=True)
            query = "delete from optim_model_data WHERE model_id = %s"
            values = (model_id, )
            cursor.execute(query, values)
            g.pg.commit()
            close_db()
            resp = {
                "statusMessage":
                "model data for modelId {} deleted".format(model_id)
            }
            return build_response(json.dumps(resp), 200)
        except Exception as err:
            error_log.error("error deleting model_id: {} - {}".format(
                model_id, traceback.format_exc()))
            close_db()
            raise BusinessException(err)
Esempio n. 16
0
    def process_nst_selection(self):
        """Process a PCI request from a Client (build config-db, policy and  API call, make the call, return result)

            :param req_object: Request parameters from the client
            :param osdf_config: Configuration specific to OSDF application (core + deployment)
            :return: response from NST Opt
        """
        try:
            rest_client = get_rest_client(self.request_json, service='so')
            solution = self.get_nst_solution()
        except Exception as err:
            error_log.error("Error for {} {}".format(
                self.request_info.get('requestId'), traceback.format_exc()))
            error_message = str(err)
            solution = self.error_response(error_message)

        try:
            rest_client.request(json=solution, noresponse=True)
        except RequestException:
            error_log.error(
                "Error sending asynchronous notification for {} {}".format(
                    self.request_info['requestId'], traceback.format_exc()))
Esempio n. 17
0
def get_all_models():
    with app.app_context():
        try:
            debug_log.debug("getting all model data".format())
            connection = get_db()
            cursor = connection.cursor(buffered=True)
            query = "SELECT model_id, model_content, description, solver_type  FROM optim_model_data"

            cursor.execute(query)
            if cursor is None:
                return 400, "FAILED"
            else:
                rows = cursor.fetchall()
                if rows is not None:
                    return 200, rows
                else:
                    close_db()
                    return 500, "NOT_FOUND"
        except Exception:
            error_log.error("error for request_id:  {}".format(
                traceback.format_exc()))
            close_db()
            return 500, "FAILED"
Esempio n. 18
0
    def do_slice_selection(self):
        req_info = self.request_json['requestInfo']
        app_info = self.slice_config['app_info'][self.model_type]
        mdc_from_json(self.request_json)
        requirements = self.request_json.get(app_info['requirements_field'], {})
        model_info = self.request_json.get(app_info['model_info'])
        model_name = model_info['name']
        policies = self.get_app_policies(model_name, app_info['app_name'])
        request_parameters = self.get_request_parameters(requirements, model_info)

        demands = [
            {
                "resourceModuleName": model_name,
                "resourceModelInfo": {}
            }
        ]

        try:
            template_fields = {
                'location_enabled': False,
                'version': '2020-08-13'
            }
            resp = conductor.request(req_info, demands, request_parameters, {}, template_fields,
                                     self.osdf_config, policies)
        except RequestException as e:
            resp = e.response.json()
            error = resp['plans'][0]['message']
            if isinstance(error, list) and "Unable to find any" in error[0]:
                return self.response_processor.get_slice_selection_response([])
            error_log.error('Error from conductor {}'.format(error))
            return self.response_processor.process_error_response(error)

        debug_log.debug("Response from conductor {}".format(str(resp)))
        recommendations = resp["plans"][0].get("recommendations")
        subnets = [subnet['domainType'] for subnet in self.request_json['subnetCapabilities']] \
            if self.request_json.get('subnetCapabilities') else []
        return self.response_processor.process_response(recommendations, model_info, subnets, self.model_type)
Esempio n. 19
0
    def get_conductor(self, req_info, request_parameters, policies,
                      model_name):
        demands = [{"resourceModuleName": model_name, "resourceModelInfo": {}}]

        try:
            template_fields = {
                'location_enabled': False,
                'version': '2020-08-13'
            }
            resp = conductor.request(req_info, demands, request_parameters, {},
                                     template_fields, self.osdf_config,
                                     policies)
        except RequestException as e:
            resp = e.response.json()
            error = resp['plans'][0]['message']
            if "Unable to find any" in error:
                return self.get_nst_selection_response([])
            error_log.error('Error from conductor {}'.format(error))
            return self.error_response(error)
        debug_log.debug(
            "Response from conductor in get_conductor method {}".format(
                str(resp)))
        recommendations = resp["plans"][0].get("recommendations")
        return self.process_response(recommendations, model_name)
Esempio n. 20
0
def process_placement_opt(request_json, policies, osdf_config):
    """Perform the work for placement optimization (e.g. call SDC artifact and make conductor request)

    NOTE: there is scope to make the requests to policy asynchronous to speed up overall performance
    :param request_json: json content from original request
    :param policies: flattened policies corresponding to this request
    :param osdf_config: configuration specific to OSDF app
    :param prov_status: provStatus retrieved from Subscriber policy
    :return: None, but make a POST to callback URL
    """

    try:
        mdc_from_json(request_json)
        rc = get_rest_client(request_json, service="so")
        req_id = request_json["requestInfo"]["requestId"]
        transaction_id = request_json['requestInfo']['transactionId']

        metrics_log.info(MH.inside_worker_thread(req_id))
        license_info = None
        if request_json.get('licenseInfo', {}).get('licenseDemands'):
            license_info = license_optim(request_json)

        # Conductor only handles placement, only call Conductor if placementDemands exist
        if request_json.get('placementInfo', {}).get('placementDemands'):
            metrics_log.info(MH.requesting("placement/conductor", req_id))
            req_info = request_json['requestInfo']
            demands = request_json['placementInfo']['placementDemands']
            request_parameters = request_json['placementInfo'][
                'requestParameters']
            service_info = request_json['serviceInfo']
            template_fields = {
                'location_enabled': True,
                'version': '2017-10-10'
            }
            resp = conductor.request(req_info, demands, request_parameters,
                                     service_info, template_fields,
                                     osdf_config, policies)
            if resp["plans"][0].get("recommendations"):
                placement_response = conductor_response_processor(
                    resp, req_id, transaction_id)
            else:  # "solved" but no solutions found
                placement_response = conductor_no_solution_processor(
                    resp, req_id, transaction_id)
            if license_info:  # Attach license solution if it exists
                placement_response['solutionInfo'][
                    'licenseInfo'] = license_info
        else:  # License selection only scenario
            placement_response = {
                "transactionId": transaction_id,
                "requestId": req_id,
                "requestStatus": "completed",
                "statusMessage": "License selection completed successfully",
                "solutionInfo": {
                    "licenseInfo": license_info
                }
            }
    except Exception as err:
        error_log.error("Error for {} {}".format(req_id,
                                                 traceback.format_exc()))

        try:
            body = build_json_error_body(err)
            metrics_log.info(MH.sending_response(req_id, "ERROR"))
            rc.request(json=body, noresponse=True)
        except RequestException:
            error_log.error(
                "Error sending asynchronous notification for {} {}".format(
                    req_id, traceback.format_exc()))
        return

    try:
        metrics_log.info(
            MH.calling_back_with_body(req_id, rc.url, placement_response))
        rc.request(json=placement_response, noresponse=True)
    except RequestException:  # can't do much here but log it and move on
        error_log.error(
            "Error sending asynchronous notification for {} {}".format(
                req_id, traceback.format_exc()))