blob: 560eaeb8a0d5b336ba7799ca1a3019b34b279ad9 [file] [log] [blame]
efiacor0080fa42020-09-08 16:26:50 +01001# ============LICENSE_START===================================================
ajay_dp001053579b2021-08-25 13:53:27 +05302# Copyright (C) 2020-2021 Nordix Foundation.
efiacor0080fa42020-09-08 16:26:50 +01003# ============================================================================
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=====================================================
raviteja.karumuri86f4eb22021-11-09 17:30:03 +000018import copy
efiacor0080fa42020-09-08 16:26:50 +010019import json
20import os
21from unittest import TestCase
22from unittest.mock import patch, MagicMock
23
24from mod import create_app, db
SagarSadcbd372021-12-09 16:59:51 +000025from mod.api.db_models import NetworkFunctionFilterModel, MeasurementGroupModel, \
raviteja.karumuric77c6d92021-12-02 19:13:27 +000026 SubscriptionModel, NetworkFunctionModel, NfSubRelationalModel
shivasubedi9aa0b662021-01-18 13:59:18 +000027from mod.network_function import NetworkFunctionFilter
efiacor0080fa42020-09-08 16:26:50 +010028from mod.pmsh_utils import AppConfig
ajay_dp001053579b2021-08-25 13:53:27 +053029from mod.pmsh_config import AppConfig as NewAppConfig
efiacor0080fa42020-09-08 16:26:50 +010030
31
shivasubedi9aa0b662021-01-18 13:59:18 +000032def get_pmsh_config(file_path='data/cbs_data_1.json'):
raviteja.karumuri7e304212021-11-24 13:10:09 +000033 """
34 Gets PMSH config from the JSON file
35
36 Args:
37 file_path (String): Name of the file with path
38
39 Returns
40 dict: Dictionary representation of the the service configuration
41 """
shivasubedi9aa0b662021-01-18 13:59:18 +000042 with open(os.path.join(os.path.dirname(__file__), file_path), 'r') as data:
efiacor0080fa42020-09-08 16:26:50 +010043 return json.load(data)
44
45
raviteja.karumuri7e304212021-11-24 13:10:09 +000046def create_subscription_data(subscription_name):
47 """
48 Creates subscription model object
49
50 Args:
51 subscription_name (String): Name of the Subscription
52
53 Returns
54 SubscriptionModel: single subscription model object
55 """
raviteja.karumuri86f4eb22021-11-09 17:30:03 +000056 nf_filter = NetworkFunctionFilterModel(subscription_name, '{^pnf.*,^vnf.*}',
57 '{}', '{}', '{}')
58 mg_first = MeasurementGroupModel(subscription_name, 'MG1', 'UNLOCKED', 15, '/pm/pm.xml',
59 '[{ "measurementType": "countera" }, '
60 '{ "measurementType": "counterb" }]',
61 '[{ "DN":"dna"},{"DN":"dnb"}]')
62 mg_second = copy.deepcopy(mg_first)
63 mg_second.measurement_group_name = 'MG2'
64 mg_second.administrative_state = 'LOCKED'
65 mg_list = [mg_first, mg_second]
66 subscription_model = SubscriptionModel(subscription_name, 'pmsh_operational_policy',
67 'pmsh_control_loop_name', 'LOCKED')
68 subscription_model.network_filter = nf_filter
69 subscription_model.measurement_groups = mg_list
egernug15dde942021-12-06 13:11:15 +000070 nf1 = NfSubRelationalModel(subscription_name, "pnf_101", "LOCKED")
71 nf2 = NfSubRelationalModel(subscription_name, "pnf_102", "LOCKED")
72 subscription_model.nfs = [nf1, nf2]
raviteja.karumuri86f4eb22021-11-09 17:30:03 +000073 return subscription_model
74
75
raviteja.karumuri7e304212021-11-24 13:10:09 +000076def create_multiple_subscription_data(subscription_names):
77 """
78 Creates a list of subscription model objects
79
80 Args:
81 subscription_names (List): Name of the Subscriptions
82
83 Returns
84 list (SubscriptionModel): of subscription model objects
85 """
86 subscriptions = []
87 for subscription_name in subscription_names:
88 subscriptions.append(create_subscription_data(subscription_name))
89 return subscriptions
90
91
raviteja.karumuric77c6d92021-12-02 19:13:27 +000092def create_multiple_network_function_data(nf_name_list):
93 """
94 Creates list of network function model objects
95
96 Args:
97 nf_name_list (list): Network function names
98
99 Returns
100 list: of network function model objects
101 """
102 nf_list = []
103 for nf_name in nf_name_list:
104 nf = NetworkFunctionModel(nf_name, '10.10.10.32', '2001:0db8:0:0:0:0:1428:57ab',
105 '687kj45-d396-4efb-af02-6b83499b12f8',
106 'e80a6ae3-cafd-4d24-850d-e14c084a5ca9',
107 'model_name', 'pm_control', '1.0.2')
108 nf_list.append(nf)
109 return nf_list
110
111
efiacor0080fa42020-09-08 16:26:50 +0100112class BaseClassSetup(TestCase):
113 app = None
114 app_context = None
115
116 @classmethod
117 @patch('mod.get_db_connection_url', MagicMock(return_value='sqlite://'))
118 @patch('mod.update_logging_config', MagicMock())
119 def setUpClass(cls):
120 os.environ['LOGGER_CONFIG'] = os.path.join(os.path.dirname(__file__), 'log_config.yaml')
121 os.environ['LOGS_PATH'] = '.'
122 cls.app = create_app()
123 cls.app_context = cls.app.app_context()
124 cls.app_context.push()
125
126 @patch('mod.pmsh_utils.AppConfig._get_pmsh_config', MagicMock(return_value=get_pmsh_config()))
127 def setUp(self):
128 os.environ['AAI_SERVICE_PORT'] = '8443'
129 db.create_all()
130 self.app_conf = AppConfig()
shivasubedi9aa0b662021-01-18 13:59:18 +0000131 self.app_conf.nf_filter = NetworkFunctionFilter(**self.app_conf.subscription.nfFilter)
efiacor0080fa42020-09-08 16:26:50 +0100132
ajay_dp001053579b2021-08-25 13:53:27 +0530133 @patch('mod.pmsh_config.AppConfig._get_config', MagicMock(return_value=get_pmsh_config()))
134 def setUpAppConf(self):
135 self.pmsh_app_conf = NewAppConfig()
136
efiacor0080fa42020-09-08 16:26:50 +0100137 def tearDown(self):
138 db.drop_all()
139
140 @classmethod
141 def tearDownClass(cls):
142 db.session.remove()