summaryrefslogtreecommitdiffstats
path: root/components/pm-subscription-handler/pmsh_service/mod/aai_client.py
blob: fff91e7e911df92c3c5ba11f0087c7f03ec626f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# ============LICENSE_START===================================================
#  Copyright (C) 2019-2020 Nordix Foundation.
# ============================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=====================================================
import json
from os import environ

import requests
from requests.auth import HTTPBasicAuth

import mod.network_function
import mod.pmsh_utils
from mod import logger


def get_pmsh_nfs_from_aai(app_conf):
    """
    Returns the Network Functions from AAI related to the Subscription.

    Args:
        app_conf (AppConfig): the AppConfig object.

    Returns:
        NetworkFunctions (list): list of NetworkFunctions.

    Raises:
        RuntimeError: if AAI Network Function data cannot be retrieved.
    """
    aai_nf_data = _get_all_aai_nf_data(app_conf)
    if aai_nf_data:
        nfs = _filter_nf_data(aai_nf_data, app_conf)
    else:
        raise RuntimeError('Failed to get data from AAI')
    return nfs


def _get_all_aai_nf_data(app_conf):
    """
    Return queried nf data from the AAI service.

    Args:
        app_conf (AppConfig): the AppConfig object.

    Returns:
        dict: the json response from AAI query, else None.
    """
    nf_data = None
    try:
        session = requests.Session()
        aai_named_query_endpoint = f'{_get_aai_service_url()}{"/query"}'
        logger.info('Fetching XNFs from AAI.')
        headers = _get_aai_request_headers()
        data = """
                {'start':
                    ['network/pnfs',
                    'network/generic-vnfs']
                }"""
        params = {'format': 'simple', 'nodesOnly': 'true'}
        response = session.put(aai_named_query_endpoint, headers=headers,
                               auth=HTTPBasicAuth(app_conf.aaf_creds.get('aaf_id'),
                                                  app_conf.aaf_creds.get('aaf_pass')),
                               data=data, params=params,
                               verify=(app_conf.ca_cert_path if app_conf.enable_tls else False),
                               cert=(app_conf.cert_params if app_conf.enable_tls else None))
        response.raise_for_status()
        if response.ok:
            nf_data = json.loads(response.text)
            logger.info('Successfully fetched XNFs from AAI')
            logger.debug(f'XNFs from AAI: {nf_data}')
    except Exception as e:
        logger.error(f'Failed to get XNFs from AAI: {e}', exc_info=True)
    return nf_data


def _get_aai_service_url():
    """
    Returns the URL of the AAI kubernetes service.

    Returns:
        str: the AAI k8s service URL.

    Raises:
        KeyError: if AAI env var not found.
    """
    try:
        aai_ssl_port = environ['AAI_SERVICE_PORT']
        return f'https://aai:{aai_ssl_port}/aai/v21'
    except KeyError as e:
        logger.error(f'Failed to get AAI env var: {e}', exc_info=True)
        raise


@mod.pmsh_utils.mdc_handler
def _get_aai_request_headers(**kwargs):
    return {'accept': 'application/json',
            'content-type': 'application/json',
            'x-fromappid': 'dcae-pmsh',
            'x-transactionid': kwargs['request_id'],
            'InvocationID': kwargs['invocation_id'],
            'RequestID': kwargs['request_id']}


def _filter_nf_data(nf_data, app_conf):
    """
    Returns a list of filtered NetworkFunctions using the nf_filter.

    Args:
        nf_data (dict): the nf json data from AAI.
        app_conf (AppConfig): the AppConfig object.

    Returns:
        NetworkFunction (list): a list of filtered NetworkFunction Objects.

    Raises:
        KeyError: if AAI data cannot be parsed.
    """
    nf_list = []
    try:
        for nf in nf_data['results']:
            if nf['properties'].get('orchestration-status') != 'Active':
                continue
            name_identifier = 'pnf-name' if nf['node-type'] == 'pnf' else 'vnf-name'
            new_nf = mod.network_function.NetworkFunction(
                nf_name=nf['properties'].get(name_identifier),
                ip_address=nf['properties'].get('ipaddress-v4-oam'),
                model_invariant_id=nf['properties'].get('model-invariant-id'),
                model_version_id=nf['properties'].get('model-version-id'))
            if not new_nf.set_nf_model_params(app_conf):
                continue
            if app_conf.nf_filter.is_nf_in_filter(new_nf):
                nf_list.append(new_nf)
    except KeyError as e:
        logger.error(f'Failed to parse AAI data: {e}', exc_info=True)
        raise
    return nf_list


def get_aai_model_data(app_conf, model_invariant_id, model_version_id, nf_name):
    """
    Get the sdnc_model info for the xNF from AAI.

    Args:
        model_invariant_id (str): the AAI model-invariant-id
        model_version_id (str): the AAI model-version-id
        app_conf (AppConfig): the AppConfig object.
        nf_name (str): The xNF name.

    Returns:
        json (dict): the sdnc_model json object.

    Raises:
        Exception: if AAI model data cannot be retrieved.
    """
    try:
        session = requests.Session()
        aai_model_ver_endpoint = \
            f'{_get_aai_service_url()}/service-design-and-creation/models/model/' \
            f'{model_invariant_id}/model-vers/model-ver/{model_version_id}'

        logger.info(f'Fetching sdnc-model info for xNF: {nf_name} from AAI.')
        headers = _get_aai_request_headers()
        response = session.get(aai_model_ver_endpoint, headers=headers,
                               auth=HTTPBasicAuth(app_conf.aaf_creds.get('aaf_id'),
                                                  app_conf.aaf_creds.get('aaf_pass')),
                               verify=(app_conf.ca_cert_path if app_conf.enable_tls else False),
                               cert=(app_conf.cert_params if app_conf.enable_tls else None))
        response.raise_for_status()
        if response.ok:
            data = json.loads(response.text)
            logger.debug(f'Successfully fetched sdnc-model info from AAI: {data}')
            return data
    except Exception:
        raise