ce463ed0ee6a50669fa4947a0853971cefbb3d0d
[dcaegen2/services.git] / components / pm-subscription-handler / pmsh_service / mod / api / services / nf_service.py
1 # ============LICENSE_START===================================================
2 #  Copyright (C) 2021-2022 Nordix Foundation.
3 # ============================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=====================================================
18
19 from mod import db, aai_client, logger
20 from mod.api.db_models import NetworkFunctionModel
21 from mod.pmsh_config import AppConfig
22 from mod.network_function import NetworkFunctionFilter
23
24
25 def capture_filtered_nfs(sub_name):
26     """
27     Retrieves network functions from AAI client and
28     returns a list of filtered NetworkFunctions using the Filter
29
30     Args:
31         sub_name (string): The name of subscription inorder to perform filtering
32     Returns:
33         list[NetworkFunction]: a list of filtered NetworkFunction Objects
34         or an empty list if no network function is filtered.
35     """
36     logger.info(f'Getting filtered nfs for subscription: {sub_name}')
37     nf_filter = NetworkFunctionFilter.get_network_function_filter(sub_name)
38     return aai_client.get_pmsh_nfs_from_aai(AppConfig.get_instance(), nf_filter)
39
40
41 def create_nf_event_body(nf, change_type, sub_model):
42     """
43     Creates a network function event body to publish on MR
44
45     Args:
46         nf (NetworkFunction): the Network function to include in the event.
47         change_type (string): define the change type to be applied on node
48         sub_model(SubscriptionModel): Subscription model object
49     Returns:
50         dict: network function event body to publish on MR.
51     """
52     return {'nfName': nf.nf_name,
53             'ipAddress': nf.ipv4_address if nf.ipv6_address in (None, '')
54             else nf.ipv6_address,
55             'blueprintName': nf.sdnc_model_name,
56             'blueprintVersion': nf.sdnc_model_version,
57             'operationalPolicyName': sub_model.operational_policy_name,
58             'changeType': change_type,
59             'controlLoopName': sub_model.control_loop_name}
60
61
62 def save_nf(nf):
63     """
64     Saves the network function request to the db
65     Args:
66         nf (NetworkFunction) : requested network function to save
67     """
68     network_function = NetworkFunctionModel(nf_name=nf.nf_name,
69                                             ipv4_address=nf.ipv4_address,
70                                             ipv6_address=nf.ipv6_address,
71                                             model_invariant_id=nf.model_invariant_id,
72                                             model_version_id=nf.model_version_id,
73                                             model_name=nf.model_name,
74                                             sdnc_model_name=nf.sdnc_model_name,
75                                             sdnc_model_version=nf.sdnc_model_version)
76     db.session.add(network_function)