summaryrefslogtreecommitdiffstats
path: root/components/pm-subscription-handler/pmsh_service/mod/aai_event_handler.py
blob: f8254e523abe9e5118118fc520777b9f1b0f06db (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
# ============LICENSE_START===================================================
#  Copyright (C) 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 enum import Enum

from mod import pmsh_logging as logger
from mod.network_function import NetworkFunction
from mod.subscription import NetworkFunctionFilter


class XNFType(Enum):
    PNF = 'pnf'
    VNF = 'vnf'


class AAIEvent(Enum):
    DELETE = 'DELETE'
    UPDATE = 'UPDATE'


class OrchestrationStatus(Enum):
    ACTIVE = 'Active'
    INVENTORIED = 'Inventoried'


def process_aai_events(mr_sub, subscription, mr_pub, app):
    """
    Processes AAI UPDATE events for each filtered xNFs where orchestration status is set to Active.

    Args:
        mr_sub (_MrSub): MR subscriber
        subscription (Subscription): The current subscription object
        mr_pub (_MrPub): MR publisher
        app (db): DB application
    """
    app.app_context().push()
    aai_events = mr_sub.get_from_topic('AAI-EVENT')

    if _aai_event_exists(aai_events):
        for entry in aai_events:
            logger.debug(f'AAI-EVENT entry: {entry}')
            entry = json.loads(entry)
            event_header = entry['event-header']
            aai_xnf = entry['entity']
            action = event_header['action']
            entity_type = event_header['entity-type']
            xnf_name = aai_xnf['pnf-name'] if entity_type == XNFType.PNF.value else aai_xnf[
                'vnf-name']
            new_status = aai_xnf['orchestration-status']

            if NetworkFunctionFilter(**subscription.nfFilter).is_nf_in_filter(xnf_name):
                _process_event(action, new_status, xnf_name, subscription, mr_pub)


def _process_event(action, new_status, xnf_name, subscription, mr_pub):
    if action == AAIEvent.UPDATE.value:
        logger.debug(f'Update event found for network function {xnf_name}')
        local_xnf = NetworkFunction.get(xnf_name)

        if local_xnf is None:
            logger.debug(f'Activating subscription for network function {xnf_name}')
            subscription.process_subscription([NetworkFunction(
                nf_name=xnf_name, orchestration_status=new_status)], mr_pub)
        else:
            logger.debug(f"Update Event for network function {xnf_name} will not be processed "
                         f" as it's state is set to {local_xnf.orchestration_status}.")
    elif action == AAIEvent.DELETE.value:
        logger.debug(f'Delete event found for network function {xnf_name}')
        NetworkFunction.delete(nf_name=xnf_name)
        logger.debug(f'{xnf_name} successfully deleted.')


def _aai_event_exists(aai_events):
    return aai_events is not None and len(aai_events) != 0