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)
Ejemplo n.º 2
0
def post_subscription(body):
    """
    Creates a subscription

    Args:
        body (dict): subscription request body to save.

    Returns:
        Success : NoContent, 201
        Invalid Data : Invalid message, 400
        Duplicate Data : Duplicate field detail, 409

    Raises:
        Error: If anything fails in the server.
    """
    response = NoContent, HTTPStatus.CREATED.value
    try:
        subscription_service.create_subscription(body['subscription'])
    except DuplicateDataException as e:
        logger.error(
            f'Failed to create subscription for '
            f'{body["subscription"]["subscriptionName"]} due to duplicate data: {e}',
            exc_info=True)
        response = e.args[0], HTTPStatus.CONFLICT.value
    except InvalidDataException as e:
        logger.error(
            f'Failed to create subscription for '
            f'{body["subscription"]["subscriptionName"]} due to invalid data: {e}',
            exc_info=True)
        response = e.args[0], HTTPStatus.BAD_REQUEST.value
    return response
 def test_missing_measurement_grp_name(self):
     subscription = self.create_test_subs('xtraPM-All-gNB-R2B-fail', '')
     try:
         subscription_service.create_subscription(
             json.loads(subscription)['subscription'])
     except InvalidDataException as exception:
         self.assertEqual(exception.args[0],
                          "No value provided for measurement group name")
 def test_perform_validation_existing_sub(self):
     try:
         subscription = create_subscription_data('ExtraPM-All-gNB-R2B')
         db.session.add(subscription)
         db.session.commit()
         subscription_service.create_subscription(
             json.loads(self.subscription_request)['subscription'])
     except DuplicateDataException as exception:
         self.assertEqual(
             exception.args[0],
             "subscription Name: ExtraPM-All-gNB-R2B already exists.")
 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_missing_administrative_state(self):
     subscription = json.loads(
         self.create_test_subs('sub-fail', 'measurement_grp_name-fail'))
     mes_grp = subscription['subscription']['measurementGroups'][0][
         'measurementGroup']
     mes_grp['administrativeState'] = ''
     try:
         subscription_service.create_subscription(
             subscription['subscription'])
     except InvalidDataException as exception:
         self.assertEqual(exception.args[0],
                          "No value provided for administrative state")
 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_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_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_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 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)