def publish_measurement_group(sub_model, measurement_group, nf, change_type): """ Publishes an event for measurement group against nfs to MR Args: sub_model(SubscriptionModel): Subscription model object measurement_group (MeasurementGroupModel): Measurement group to publish nf (NetworkFunction): Network function to publish change_type (string): defines event type like CREATE or DELETE """ event_body = nf_service.create_nf_event_body(nf, change_type, sub_model) event_body['subscription'] = { "administrativeState": measurement_group.administrative_state, "subscriptionName": sub_model.subscription_name, "fileBasedGP": measurement_group.file_based_gp, "fileLocation": measurement_group.file_location, "measurementGroup": { "measurementGroupName": measurement_group.measurement_group_name, "measurementTypes": measurement_group.measurement_type, "managedObjectDNsBasic": measurement_group.managed_object_dns_basic } } logger.debug(f'Event Body: {event_body}') AppConfig.get_instance().publish_to_topic( MRTopic.POLICY_PM_PUBLISHER.value, event_body)
def execute(self): """ Processes AAI UPDATE events for each filtered xNFs where orchestration status is set to Active. """ self.app.app_context().push() logger.info('Polling MR for XNF AAI events.') try: aai_events = AppConfig.get_instance().get_from_topic( MRTopic.AAI_SUBSCRIBER.value, 'dcae_pmsh_aai_event') if aai_events is not None and len(aai_events) != 0: pmsh_nf_names = list(nf.nf_name for nf in NetworkFunction.get_all()) aai_events = [json.loads(e) for e in aai_events] xnf_events = [ e for e in aai_events if is_pnf_xnf(e['event-header']['entity-type']) ] new_nfs = [] for entry in xnf_events: logger.debug(f'AAI-EVENT entry: {entry}') aai_entity = entry['entity'] action = entry['event-header']['action'] entity_type = entry['event-header']['entity-type'] xnf_name = aai_entity['pnf-name'] if entity_type == XNFType.PNF.value \ else aai_entity['vnf-name'] if aai_entity['orchestration-status'] != 'Active': logger.info( f'Skipping XNF {xnf_name} as its orchestration-status ' f'is not "Active"') continue nf = NetworkFunction( nf_name=xnf_name, ipv4_address=aai_entity['ipaddress-v4-oam'], ipv6_address=aai_entity['ipaddress-v6-oam'], model_invariant_id=aai_entity['model-invariant-id'], model_version_id=aai_entity['model-version-id']) if action == AAIEvent.DELETE.value and xnf_name in pmsh_nf_names: logger.info( f'Delete event found for network function {nf.nf_name}' ) NetworkFunction.delete(nf_name=nf.nf_name) logger.info(f'{nf.nf_name} successfully deleted.') elif action == AAIEvent.UPDATE.value and \ xnf_name not in pmsh_nf_names and \ nf.set_nf_model_params(AppConfig.get_instance()): new_nfs.append(nf) if new_nfs: self.apply_nfs_to_subscriptions(new_nfs) except Exception as e: logger.error(f'Failed to process AAI event due to: {e}')
def poll_policy_topic(self): """ This method polls MR for response from policy. It checks whether the message is for the relevant subscription and then handles the response """ self.app.app_context().push() logger.info( 'Polling MR for XNF activation/deactivation policy response events.' ) try: response_data = AppConfig.get_instance(). \ get_from_topic(MRTopic.POLICY_PM_SUBSCRIBER.value, 'dcae_pmsh_policy_cl_input') for data in response_data: data = json.loads(data) measurement_group_name = data['status']['measurementGroupName'] subscription_name = data['status']['subscriptionName'] measurement_group = (MeasurementGroupModel.query.filter( MeasurementGroupModel.measurement_group_name == measurement_group_name, subscription_name == MeasurementGroupModel.subscription_name).one_or_none()) nf_name = data['status']['nfName'] response_message = data['status']['message'] if measurement_group: self._handle_response( measurement_group_name, measurement_group.administrative_state, nf_name, response_message) else: logger.info( f'Polled MR response provides missing measurement ' f'group name : {measurement_group_name}') except Exception as err: logger.error( f'Error trying to poll policy response topic on MR: {err}', exc_info=True)
def main(): try: try: app = create_app() app.app_context().push() db.create_all(app=app) pmsh_app_conf = AppConfig() except Exception as e: logger.error(f'Failed to get config and create application: {e}', exc_info=True) sys.exit(e) policy_response_handler = PolicyResponseHandler(app) policy_response_handler_thread = PeriodicTask( 25, policy_response_handler.poll_policy_topic) policy_response_handler_thread.name = 'policy_event_thread' logger.info('Start polling PMSH_CL_INPUT topic on DMaaP MR.') policy_response_handler_thread.start() aai_event_handler = AAIEventHandler(app) aai_event_handler_thread = PeriodicTask(20, aai_event_handler.execute) aai_event_handler_thread.name = 'aai_event_thread' aai_event_handler_thread.start() periodic_tasks = [ policy_response_handler_thread, aai_event_handler_thread ] signal(SIGTERM, ExitHandler(periodic_tasks=periodic_tasks)) launch_api_server(pmsh_app_conf) except Exception as e: logger.error(f'Failed to initialise PMSH: {e}', exc_info=True) sys.exit(e)
def capture_filtered_nfs(sub_name): """ Retrieves network functions from AAI client and returns a list of filtered NetworkFunctions using the Filter Args: sub_name (string): The name of subscription inorder to perform filtering Returns: list[NetworkFunction]: a list of filtered NetworkFunction Objects or an empty list if no network function is filtered. """ logger.info(f'Getting filtered nfs for subscription: {sub_name}') nf_filter = NetworkFunctionFilter.get_network_function_filter(sub_name) return aai_client.get_pmsh_nfs_from_aai(AppConfig.get_instance(), nf_filter)
def test_singleton_instance_is_accessible_using_class_method(self): my_singleton_instance = AppConfig.get_instance() self.assertIsNotNone(my_singleton_instance) self.assertIsInstance(my_singleton_instance, AppConfig)
def setUp(self): os.environ['AAI_SERVICE_PORT'] = '8443' db.create_all() self.app_conf = AppConfig()