[DCAEGEN2] PMSH Create Subscription public API
[dcaegen2/services.git] / components / pm-subscription-handler / pmsh_service / mod / api / services / nf_service.py
1 # ============LICENSE_START===================================================
2 #  Copyright (C) 2021 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     """
35     logger.info(f'Getting filtered nfs for subscription: {sub_name}')
36     nf_filter = NetworkFunctionFilter.get_network_function_filter(sub_name)
37     return aai_client.get_pmsh_nfs_from_aai(AppConfig.get_instance(), nf_filter)
38
39
40 def create_nf_event_body(nf, change_type):
41     """
42     Creates a network function event body to publish on MR
43
44     Args:
45         nf (NetworkFunction): the Network function to include in the event.
46         change_type (string): define the change type to be applied on node
47     Returns:
48         dict: network function event body to publish on MR.
49     """
50     app_conf = AppConfig.get_instance()
51     return {'nfName': nf.nf_name,
52             'ipAddress': nf.ipv4_address if nf.ipv6_address in (None, '')
53             else nf.ipv6_address,
54             'blueprintName': nf.sdnc_model_name,
55             'blueprintVersion': nf.sdnc_model_version,
56             'policyName': app_conf.operational_policy_name,
57             'changeType': change_type,
58             'closedLoopControlName': app_conf.control_loop_name}
59
60
61 def save_nf(nf):
62     """
63     Saves the network function request to the db
64     Args:
65         nf (NetworkFunction) : requested network function to save
66     """
67     network_function = NetworkFunctionModel(nf_name=nf.nf_name,
68                                             ipv4_address=nf.ipv4_address,
69                                             ipv6_address=nf.ipv6_address,
70                                             model_invariant_id=nf.model_invariant_id,
71                                             model_version_id=nf.model_version_id,
72                                             model_name=nf.model_name,
73                                             sdnc_model_name=nf.sdnc_model_name,
74                                             sdnc_model_version=nf.sdnc_model_version)
75     db.session.add(network_function)