def test_update_filter_with_new_del_nfs(self, mock_filter_call, mock_model_aai, mock_aai, mock_update_filter): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) mock_update_filter.return_value = None subscription = self.create_test_subs('sub_01', 'msg_01') subscription = json.loads(subscription)['subscription'] nf_filter = subscription['nfFilter'] mock_filter_call.return_value = NetworkFunctionFilter(**nf_filter) subscription_service.create_subscription(subscription) # Check existing network functions meas_group_nfs = db.session.query(NfMeasureGroupRelationalModel).filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'msg_01') \ .all() self.assertEqual(len(meas_group_nfs), 2) self.assertEqual(meas_group_nfs[0].nf_name, 'pnf201') self.assertEqual(meas_group_nfs[0].nf_measure_grp_status, MgNfState.PENDING_CREATE.value) self.assertEqual(meas_group_nfs[1].nf_name, 'pnf_33_ericsson') self.assertEqual(meas_group_nfs[1].nf_measure_grp_status, MgNfState.PENDING_CREATE.value) meas_grp = measurement_group_service.query_meas_group_by_name( 'sub_01', 'msg_01') self.assertEqual(meas_grp.administrative_state, 'UNLOCKED') # Creating test data for update filter function aai_response = self.aai_response_data.replace('pnf201', 'xnf111') mock_aai.return_value = json.loads(aai_response) nf_filter['nfNames'] = ["^vnf.*", "^xnf.*"] mock_filter_call.return_value = NetworkFunctionFilter(**nf_filter) subscription_service.update_filter('sub_01', nf_filter) self.assertTrue(mock_update_filter.called) # Check updated network functions after filter change meas_group_nfs = db.session.query(NfMeasureGroupRelationalModel).filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'msg_01') \ .all() self.assertEqual(meas_group_nfs[0].nf_name, 'pnf201') self.assertEqual(meas_group_nfs[0].nf_measure_grp_status, MgNfState.PENDING_DELETE.value) self.assertEqual(meas_group_nfs[1].nf_name, 'pnf_33_ericsson') self.assertEqual(meas_group_nfs[1].nf_measure_grp_status, MgNfState.PENDING_DELETE.value) self.assertEqual(meas_group_nfs[2].nf_name, 'xnf111') self.assertEqual(meas_group_nfs[2].nf_measure_grp_status, MgNfState.PENDING_CREATE.value) meas_grp = measurement_group_service.query_meas_group_by_name( 'sub_01', 'msg_01') self.assertEqual(meas_grp.administrative_state, 'FILTERING')
def test_publish_measurement_grp_to_nfs_failed(self, mock_filter_call, mock_logger, mock_publish, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) mock_publish.side_effect = Exception('Publish failed') subscription = self.create_test_subs('xtraPM-All-gNB-R2B-new2', 'msrmt_grp_name-new2') subscription = json.loads(subscription)['subscription'] sub_model = SubscriptionModel(subscription["subscriptionName"], subscription['operationalPolicyName'], subscription['controlLoopName'], 'LOCKED') measurement_grp = MeasurementGroupModel('subscription_name_1', 'msrmt_grp_name', 'UNLOCKED', 15, 'pm.xml', [], []) measurement2 = self.create_measurement_grp(measurement_grp, 'meas2', 'UNLOCKED') measurement3 = self.create_measurement_grp(measurement_grp, 'meas3', 'LOCKED') measurement_grps = [measurement_grp, measurement2, measurement3] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) filtered_nfs = nf_service.capture_filtered_nfs( subscription["subscriptionName"]) subscription_service.publish_measurement_grp_to_nfs( sub_model, filtered_nfs, measurement_grps) mock_logger.assert_called_with( 'Publish event failed for nf name, measure_grp_name, ' 'sub_name: pnf_33_ericsson,meas2, xtraPM-All-gNB-R2B-new2 ' 'with error: Publish failed')
def test_publish_measurement_grp_to_nfs(self, mock_filter_call, mock_publish, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) mock_publish.return_value = None subscription = self.create_test_subs('xtraPM-All-gNB-R2B-new2', 'msrmt_grp_name-new2') subscription = json.loads(subscription)['subscription'] sub_model = SubscriptionModel(subscription["subscriptionName"], subscription['operationalPolicyName'], subscription['controlLoopName'], 'LOCKED') measurement_grp = MeasurementGroupModel('subscription_name_1', 'msrmt_grp_name', 'UNLOCKED', 15, 'pm.xml', [], []) measurement2 = self.create_measurement_grp(measurement_grp, 'meas2', 'UNLOCKED') measurement3 = self.create_measurement_grp(measurement_grp, 'meas3', 'UNLOCKED') measurement_grps = [measurement_grp, measurement2, measurement3] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) filtered_nfs = nf_service.capture_filtered_nfs( subscription["subscriptionName"]) subscription_service.publish_measurement_grp_to_nfs( sub_model, filtered_nfs, measurement_grps) # Two unlocked measurement Group published self.assertEqual(mock_publish.call_count, 6)
def test_create_subscription(self, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = self.create_test_subs('xtraPM-All-gNB-R2B-new', 'msrmt_grp_name-new') subscription = json.loads(subscription)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) subscription_service.create_subscription(subscription) existing_subscription = (SubscriptionModel.query.filter( SubscriptionModel.subscription_name == 'xtraPM-All-gNB-R2B-new').one_or_none()) self.assertIsNotNone(existing_subscription) existing_measurement_grp = (MeasurementGroupModel.query.filter( MeasurementGroupModel.measurement_group_name == 'msrmt_grp_name-new', MeasurementGroupModel.subscription_name == 'xtraPM-All-gNB-R2B-new').one_or_none()) self.assertIsNotNone(existing_measurement_grp) msr_grp_nf_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'msrmt_grp_name-new')).all() for pubslished_event in msr_grp_nf_rel: self.assertEqual(pubslished_event.nf_measure_grp_status, MgNfState.PENDING_CREATE.value)
def test(self, test_name, nf_filter, nf_name, model_invariant_uuid, model_version_id, model_name, expected_result): nf_filter = NetworkFunctionFilter(**nf_filter) self.assertEqual( nf_filter.is_nf_in_filter( NetworkFunction(nf_name=nf_name, model_invariant_id=model_invariant_uuid, model_version_id=model_version_id, model_name=model_name)), expected_result)
def test_capture_filtered_nfs(self, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = json.loads(self.subscription_request)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) filtered_nfs = nf_service.capture_filtered_nfs( subscription["subscriptionName"]) self.assertEqual(len(filtered_nfs), 2) self.assertEqual(filtered_nfs[0].nf_name, 'pnf201') self.assertEqual(filtered_nfs[1].nf_name, 'pnf_33_ericsson')
def test_update_filter(self, mock_filter_call, mock_model_aai, mock_aai, mock_update_filter): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) mock_update_filter.return_value = None subscription = self.create_test_subs('sub_01', 'msg_01') subscription = json.loads(subscription)['subscription'] nf_filter = subscription['nfFilter'] mock_filter_call.return_value = NetworkFunctionFilter(**nf_filter) subscription_service.create_subscription(subscription) subscription_service.update_filter('sub_01', nf_filter) self.assertTrue(mock_update_filter.called)
def test_save_nf_new_nf(self, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = json.loads(self.subscription_request)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) nf = nf_service.capture_filtered_nfs( subscription["subscriptionName"])[0] nf.nf_name = 'newnf1' nf_service.save_nf(nf) network_function = NetworkFunctionModel.query.filter( NetworkFunctionModel.nf_name == nf.nf_name).one_or_none() self.assertIsNotNone(network_function)
def test_create_subscription_all_locked_msg_grp(self, mock_logger, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = self.create_test_subs('xtraPM-All-gNB-R2B-new2', 'msrmt_grp_name-new2') subscription = subscription.replace('UNLOCKED', 'LOCKED') subscription = json.loads(subscription)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) subscription_service.create_subscription(subscription) mock_logger.assert_called_with( 'All measurement groups are locked for subscription: ' 'xtraPM-All-gNB-R2B-new2, please verify/check' ' measurement groups.')
def test_aai_client_get_pm_sub_data_success(self, mock_put_session, mock_get_session, mock_get_sdnc_params): mock_put_session.return_value.status_code = 200 mock_put_session.return_value.text = self.aai_response_data mock_get_session.return_value.status_code = 200 mock_get_session.return_value.text = self.good_model_info mock_get_sdnc_params.return_value = True nf_filter = NetworkFunctionFilter( **self.subscription.network_filter.serialize()) xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf, nf_filter) self.assertEqual(self.subscription.subscription_name, 'ExtraPM-All-gNB-R2B') self.assertEqual( self.subscription.measurement_groups[0].administrative_state, 'UNLOCKED') self.assertEqual(len(xnfs), 3)
def test_save_filtered_nfs(self, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = self.create_test_subs('xtraPM-All-gNB-R2B-new', 'msrmt_grp_name-new') subscription = json.loads(subscription)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) filtered_nfs = nf_service.capture_filtered_nfs( subscription["subscriptionName"]) subscription_service.save_filtered_nfs(filtered_nfs) for nf in filtered_nfs: saved_nf = (NetworkFunctionModel.query.filter( NetworkFunctionModel.nf_name == nf.nf_name).one_or_none()) self.assertIsNotNone(saved_nf)
def test_apply_subscription_to_nfs(self, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = json.loads(self.subscription_request)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) filtered_nfs = nf_service.capture_filtered_nfs( subscription["subscriptionName"]) subscription_service.apply_subscription_to_nfs(filtered_nfs, 'xtraPM-All-gNB-R2B') for nf in filtered_nfs: saved_nf_sub_rel = (NfSubRelationalModel.query.filter( NfSubRelationalModel.subscription_name == 'xtraPM-All-gNB-R2B', NfSubRelationalModel.nf_name == nf.nf_name).one_or_none()) self.assertIsNotNone(saved_nf_sub_rel)
def test_filter_true_on_multiple_modelInvariantIDs(self): nf_filter = NetworkFunctionFilter( **{ "nfNames": [], "modelInvariantIDs": [ '5845y423-g654-6fju-po78-8n53154532k6', '7129e420-d396-4efb-af02-6b83499b12f8' ], "modelVersionIDs": [], "modelNames": [] }) self.assertTrue( nf_filter.is_nf_in_filter( NetworkFunction( nf_name='pnf1', model_invariant_id='7129e420-d396-4efb-af02-6b83499b12f8', model_version_id='e80a6ae3-cafd-4d24-850d-e14c084a5ca9', model_name='pnf_102')))
def test_create_subscription_service_on_aai_failed(self, mock_filter_call, mock_aai): mock_aai.side_effect = InvalidDataException(["AAI call failed"]) subscription = self.create_test_subs('xtraPM-All-gNB-R2B-fail', 'msrmt_grp_name-fail') subscription = json.loads(subscription)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) try: subscription_service.create_subscription(subscription) except InvalidDataException as exception: self.assertEqual(exception.args[0], ["AAI call failed"]) # Checking Rollback on AAI failure with subscription request saved existing_subscription = (SubscriptionModel.query.filter( SubscriptionModel.subscription_name == 'xtraPM-All-gNB-R2B-fail').one_or_none()) self.assertIsNotNone(existing_subscription)
def test_filter_false_on_modelInvariantIDs_being_false_and_pnfname_being_true( self): nf_filter = NetworkFunctionFilter( **{ "nfNames": ["^pnf.*", "^vnf.*"], "modelInvariantIDs": [ '5845y423-g654-6fju-po78-8n53154532k6', '7129e420-d396-4efb-af02-6b83499b12f8' ], "modelVersionIDs": [], "modelNames": [] }) self.assertFalse( nf_filter.is_nf_in_filter( NetworkFunction( nf_name='pnf1', model_invariant_id='WrongModelInvariantUUID', model_version_id='e80a6ae3-cafd-4d24-850d-e14c084a5ca9', model_name='pnf_102')))
def test_update_filter_invalid_duplicate_request(self, mock_filter_call, mock_model_aai, mock_aai, mock_update_filter): try: mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) mock_update_filter.return_value = None subscription = self.create_test_subs('sub_01', 'msg_01') subscription = json.loads(subscription)['subscription'] nf_filter = subscription['nfFilter'] mock_filter_call.return_value = NetworkFunctionFilter(**nf_filter) subscription_service.create_subscription(subscription) subscription_service.update_filter( "sub_01", json.loads('{"nfNames": "^pnf.*"}')) except InvalidDataException as invalidEx: self.assertEqual( invalidEx.args[0], "Duplicate nf filter update requested for subscription " "with sub name: sub_01")
def apply_nfs_to_subscriptions(new_nfs): """ Applies measurement groups to network functions identified by AAI event Args: new_nfs (list[NetworkFunction]): new network functions identified """ subscriptions = db.session.query(SubscriptionModel).all() if subscriptions: for subscription in subscriptions: try: nf_filter = NetworkFunctionFilter( **subscription.network_filter.serialize()) filtered_nfs = [] for nf in new_nfs: if nf_filter.is_nf_in_filter(nf): filtered_nfs.append(nf) if filtered_nfs: subscription_service.save_filtered_nfs(filtered_nfs) subscription_service. \ apply_subscription_to_nfs(filtered_nfs, subscription.subscription_name) unlocked_meas_grp = subscription_service. \ apply_measurement_grp_to_nfs(filtered_nfs, subscription.measurement_groups) if unlocked_meas_grp: subscription_service. \ publish_measurement_grp_to_nfs(subscription, filtered_nfs, unlocked_meas_grp) else: logger.error( f'All measurement groups are locked for subscription: ' f'{subscription.subscription_name}, ' f'please verify/check measurement groups.') db.session.commit() except Exception as e: logger.error( f'Failed to process AAI event for subscription: ' f'{subscription.subscription_name} due to: {e}') db.session.remove()
def test_create_nf_event_body(self, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = json.loads(self.subscription_request)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) nf = nf_service.capture_filtered_nfs( subscription["subscriptionName"])[0] sub_model = SubscriptionModel(subscription["subscriptionName"], subscription['operationalPolicyName'], subscription['controlLoopName'], 'LOCKED') event_body = nf_service.create_nf_event_body(nf, 'CREATE', sub_model) self.assertEqual(event_body['nfName'], nf.nf_name) self.assertEqual(event_body['ipAddress'], nf.ipv6_address) self.assertEqual(event_body['blueprintName'], nf.sdnc_model_name) self.assertEqual(event_body['blueprintVersion'], nf.sdnc_model_version) self.assertEqual(event_body['operationalPolicyName'], sub_model.operational_policy_name) self.assertEqual(event_body['changeType'], 'CREATE') self.assertEqual(event_body['controlLoopName'], sub_model.control_loop_name)
def test_apply_measurement_grp_to_nfs(self, mock_filter_call, mock_apply_nf, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) mock_apply_nf.return_value = None subscription = self.create_test_subs('xtraPM-All-gNB-R2B-new2', 'msrmt_grp_name-new2') subscription = json.loads(subscription)['subscription'] measurement_grp = MeasurementGroupModel('subscription_name_1', 'msrmt_grp_name', 'UNLOCKED', 15, 'pm.xml', [], []) measurement2 = self.create_measurement_grp(measurement_grp, 'meas2', 'UNLOCKED') unlocked_msgs = [measurement_grp, measurement2] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) filtered_nfs = nf_service.capture_filtered_nfs( subscription["subscriptionName"]) subscription_service.apply_measurement_grp_to_nfs( filtered_nfs, unlocked_msgs) # 2 measurement group with 2 nfs each contribute 4 calls self.assertEqual(mock_apply_nf.call_count, 4)
def test_create_subscription_service_failed_rollback( self, mock_filter_call, mock_model_aai, mock_aai, mock_publish): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) mock_publish.side_effect = InvalidDataException(["publish failed"]) subscription = self.create_test_subs('xtraPM-All-gNB-R2B-fail1', 'msrmt_grp_name-fail1') subscription = json.loads(subscription)['subscription'] mock_filter_call.return_value = NetworkFunctionFilter( **subscription["nfFilter"]) try: subscription_service.create_subscription(subscription) except InvalidDataException as exception: self.assertEqual(exception.args[0], ["AAI call failed"]) # Checking Rollback on publish failure with subscription and nfs captured existing_subscription = (SubscriptionModel.query.filter( SubscriptionModel.subscription_name == 'xtraPM-All-gNB-R2B-fail1').one_or_none()) self.assertIsNotNone(existing_subscription) saved_nf_sub_rel = (NfSubRelationalModel.query.filter( NfSubRelationalModel.subscription_name == 'xtraPM-All-gNB-R2B-fail1')) self.assertIsNotNone(saved_nf_sub_rel)
def test_post_subscription(self, mock_filter_call, mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) subscription = self.create_test_subs('xtraPM-All-gNB-R2B-post', 'msrmt_grp_name-post') subscription = json.loads(subscription) mock_filter_call.return_value = NetworkFunctionFilter( **subscription['subscription']["nfFilter"]) sub_name = subscription['subscription']['subscriptionName'] mes_grp = subscription['subscription']['measurementGroups'][0][ 'measurementGroup'] mes_grp_name = mes_grp['measurementGroupName'] response = post_subscription(subscription) subscription = (SubscriptionModel.query.filter( SubscriptionModel.subscription_name == sub_name).one_or_none()) self.assertIsNotNone(subscription) msr_grp_nf_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == mes_grp_name) ).all() for published_event in msr_grp_nf_rel: self.assertEqual(published_event.nf_measure_grp_status, MgNfState.PENDING_CREATE.value) self.assertEqual(response[1], 201)