예제 #1
0
def execute_action(request, response_dict, config):
    headers = ResponseHelper.create_headers()
    request_data = request.json
    action_request = request_data.get("action").lower()

    creds = ResponseHelper.get_credentials(request_data, config)
    proxies = ResponseHelper.get_proxies(config)
    url = ResponseHelper.create_url(
        config=config,
        uri_path="/restconf/operations/A1-ADAPTER-API:" + action_request)
    #    ret_url = request.args.get('retURL')

    json_req = ast.literal_eval(request_data["action_data"]["jsonBody"])
    current_app.logger.info(
        "Requesting Url: {}, body: {}, auth: {}, proxies: {}".format(
            url, json_req, creds, proxies))
    try:
        res = requests.post(url,
                            proxies=proxies,
                            auth=creds,
                            headers=headers,
                            json=json_req)
        response = {"status_code": res.status_code, "result": res.json()}
    except (json.decoder.JSONDecodeError):
        response = {"status_code": res.status_code, "result": res.reason}
    except requests.exceptions.RequestException:
        response = {"status_code": 504, "result": "Something Happned"}
    finally:
        response_dict['vthResponse']['resultData'] = response
        #        if ret_url is not None:
        #            ResponseHelper.sendCallback(ret_url,response_dict)
        #            return '',200
        return response_dict
예제 #2
0
def put_policy_using_put(request, response_dict, config):
    json_data = request.get_json()
    creds = ResponseHelper.get_credentials(json_data, config)

    current_app.logger.info("creds: {}".format(creds))

    required = {'id', 'jsonBody', 'ric', 'service'}
    param_keys = {'id', 'ric', 'service'}
    optional = {"type"}
    data_keys = param_keys.copy()
    keys = set(json_data.keys())
    if not required <= keys:
        raise BadRequestException(
            406, "Request is missing required values {}".format(required))
    if optional <= keys: data_keys.update(optional)
    param = {}
    body = {}
    for key in data_keys:
        param[key] = json_data[key]
    body['jsonBody'] = json_data['jsonBody']

    url = ResponseHelper.create_url(config=config, uri_path="/policy")
    res = requests.put(url, auth=creds, params=param, json=body)
    response = {"status_code": res.status_code, "result": res.json()}
    response_dict['vthResponse']['resultData'] = response
    return response_dict
예제 #3
0
def get_policy_types_using_get(request, response_dict, config):
    json_data = request.get_json()
    creds = ResponseHelper.get_credentials(json_data, config)
    param = {'ric': json_data['ric'] if 'ric' in json_data else ""}

    url = ResponseHelper.create_url(config=config,
                                    uri_path="/a1-p/policytypes")
    res = requests.get(url, auth=creds, params=param)
    response = {"status_code": res.status_code, "result": res.json()}
    response_dict['vthResponse']['resultData'] = response
    return response_dict
예제 #4
0
def get_policy_using_get(request, response_dict, config):
    json_data = request.get_json()
    if 'id' not in json_data:
        raise BadRequestException(406, "Request is missing id")
    param = {'id': json_data['id']}
    creds = ResponseHelper.get_credentials(json_data, config)
    url = ResponseHelper.create_url(config=config, uri_path="/policy")
    res = requests.get(url, auth=creds, params=param)
    response = {"status_code": res.status_code, "result": res.json()}
    response_dict['vthResponse']['resultData'] = response

    return response_dict
예제 #5
0
def get_services_using_get(request, response_dict, config):
    json_data = request.get_json()
    #username = config['auth']['username'] if 'username' not in json_data else json_data['username']
    #password = config['auth']['password'] if 'password' not in json_data else json_data['password']
    #creds = (username, password)
    creds = ResponseHelper.get_credentials(json_data, config)
    current_app.logger.info("creds: {}".format(creds))

    param = {'name': json_data['name'] if 'name' in json_data else ""}

    response_dict['vthResponse']['resultData'] = param
    #api_response = requests.get(url, credentials=creds, params=param)
    return response_dict
예제 #6
0
def get_policy_ids_using_get(request, response_dict, config):
    json_data = request.get_json()
    creds = ResponseHelper.get_credentials(json_data, config)
    current_app.logger.info("creds: {}".format(creds))

    param = {
        "ric": json_data["ric"] if "ric" in json_data else "",
        "service": json_data["service"] if "service" in json_data else "",
        "type": json_data["type"] if "type" in json_data else ""
    }

    url = ResponseHelper.create_url(config=config, uri_path="/policy_ids")
    res = requests.get(url, auth=creds, params=param)
    response = {"status_code": res.status_code, "result": res.json()}
    response_dict['vthResponse']['resultData'] = response
    return response_dict
예제 #7
0
def get_policy_schemas_using_get(request, response_dict, config):
    json_data = request.get_json()
    #username = config['auth']['username'] if 'username' not in json_data else json_data['username']
    #password = config['auth']['password'] if 'password' not in json_data else json_data['password']
    #creds = (username, password)
    creds = ResponseHelper.get_credentials(json_data, config)
    current_app.logger.info("creds: {}".format(creds))

    param = {"ric": json_data['ric'] if 'ric' in json_data else ""}
    #api_response = requests.put(url, credentials=creds, params=param)

    url = ResponseHelper.create_url(config=config, uri_path="/policy_schemas")
    res = requests.get(url, auth=creds, params=param)
    response = {"status_code": res.status_code, "result": res.json()}
    response_dict['vthResponse']['resultData'] = response
    return response_dict
예제 #8
0
def execute_action(request, response_dict, config):
    headers = ResponseHelper.create_headers();
    request_data = request.json
    action_request = request_data.get("action").lower()
    method = request_data.get("method").upper()
    creds = ResponseHelper.get_credentials(request_data, config)

    proxies = ResponseHelper.get_proxies(config)
    action = "services/keepalive" if action_request == "keepalive" else action_request
    url = ResponseHelper.create_url(config=config, uri_path="/"+action)
#    ret_url = request.args.get('retURL')


    json_req = ast.literal_eval(request_data["action_data"]["jsonBody"])
    query_params = ast.literal_eval(request_data["action_data"]["query"])
    current_app.logger.info("Requesting Url: {}, params: {}, body: {}, auth: {}, proxies: {}".format(url, query_params, json_req, creds, proxies))
    try:
        if(method == "GET"):
            res = requests.get(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)
        elif(method == "POST"):
            res = requests.post(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)
        elif(method == "PUT"):
            res = requests.put(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)
        elif(method == "DELETE"):
            res = requests.delete(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)
        else: 
            raise BadRequestException(406, "Method Not Supported")
        response = {
                "status_code":res.status_code,
                "result": res.json()
                }
    except(json.decoder.JSONDecodeError):
        response = {
                "status_code":res.status_code,
                "result": res.reason
                }
    except requests.exceptions.RequestException:
        response = {
                "status_code":504,
                "result": "Something Happned"
                }
    finally:
        response_dict['vthResponse']['resultData'] = response
 #       if ret_url is not None:
 #           ResponseHelper.sendCallback(ret_url,response_dict)
 #           return '',200
        return response_dict
예제 #9
0
def delete_policy_using_delete(request, response_dict, config):
    json_data = request.get_json()
    creds = ResponseHelper.get_credentials(json_data, config)

    current_app.logger.info("creds: {}".format(creds))

    keys = set(json_data.keys())
    required = {'id'}
    if not required <= keys:
        raise BadRequestException(
            406, "Request is missing required values {}".format(required))
    param = {'id': json_data['id']}

    url = ResponseHelper.create_url(config=config, uri_path="/policy")
    res = requests.delete(url, auth=creds, params=param)
    response = {"status_code": res.status_code, "result": res.json()}
    response_dict['vthResponse']['resultData'] = response
    return response_dict
예제 #10
0
def get_policy_status_using_get(request, response_dict, config):
    json_data = request.get_json()
    #username = config['auth']['username'] if 'username' not in json_data else json_data['username']
    #password = config['auth']['password'] if 'password' not in json_data else json_data['password']
    #creds = (username, password)
    creds = ResponseHelper.get_credentials(json_data, config)
    current_app.logger.info("creds: {}".format(creds))

    keys = set(json_data.keys())
    required = {'id'}
    if not required <= keys:
        raise BadRequestException(
            406, "Request is missing required values {}".format(required))
    param = {"id": json_data["id"]}

    response_dict['vthResponse']['resultData'] = param
    #api_response = requests.get(url, credentials=creds, params=param)
    return response_dict
예제 #11
0
def get_policy_schema_using_get(request, response_dict, config):
    json_data = request.get_json()
    #username = config['auth']['username'] if 'username' not in json_data else json_data['username']
    #password = config['auth']['password'] if 'password' not in json_data else json_data['password']
    #creds = (username, password)
    creds = ResponseHelper.get_credentials(json_data, config)
    current_app.logger.info("creds: {}".format(creds))

    keys = set(json_data.keys())
    required = {'id'}
    if not required <= keys:
        raise BadRequestException(
            406, "Request is missing required values {}".format(required))
    param = {'id': json_data['id']}

    url = ResponseHelper.create_url(config=config, uri_path="/policy_schema")
    res = requests.get(url, auth=creds, params=param)
    response = {"status_code": res.status_code, "result": res.json()}
    response_dict['vthResponse']['resultData'] = response
    return response_dict