def setUp(self):
        super().setUp()
        self.rc_service = RadioControllerService(SessionManager(self.engine))
        DBInitializer(SessionManager(self.engine)).initialize()

        self.unregistered_state = self.session.query(DBCbsdState).\
            filter(DBCbsdState.name == CbsdStates.UNREGISTERED.value).scalar()
    def setUp(self):
        super().setUp()
        self.amc_service = ActiveModeControllerService(
            SessionManager(self.engine), )
        DBInitializer(SessionManager(self.engine)).initialize()

        grant_states = {
            x.name: x.id
            for x in self.session.query(DBGrantState).all()
        }
        cbsd_states = {
            x.name: x.id
            for x in self.session.query(DBCbsdState).all()
        }
        request_types = {
            x.name: x.id
            for x in self.session.query(DBRequestType).all()
        }

        self.unregistered = cbsd_states[CbsdStates.UNREGISTERED.value]
        self.registered = cbsd_states[CbsdStates.REGISTERED.value]

        self.granted = grant_states[GrantStates.GRANTED.value]
        self.authorized = grant_states[GrantStates.AUTHORIZED.value]

        self.grant = request_types[RequestTypes.GRANT.value]
Exemplo n.º 3
0
    def setUp(self):
        super().setUp()
        self.dp_service = DPService(SessionManager(self.engine), self._now)
        DBInitializer(SessionManager(self.engine)).initialize()

        self.cbsd_states = {
            x.name: x.id for x in self.session.query(DBCbsdState).all()
        }
        self.grant_states = {
            x.name: x.id for x in self.session.query(DBGrantState).all()
        }
Exemplo n.º 4
0
 def setUp(self):
     super().setUp()
     DBInitializer(SessionManager(self.engine)).initialize()
     grpc_channel = grpc.insecure_channel(
         f"{config.GRPC_SERVICE}:{config.GRPC_PORT}",
     )
     self.dp_client = DPServiceStub(grpc_channel)
    def setUp(self):
        super().setUp()
        DBInitializer(SessionManager(self.engine)).initialize()

        self.cbsd_states = {
            state.name: state.id
            for state in self.session.query(DBCbsdState).all()
        }
        self.request_types = {
            req_type.name: req_type.id
            for req_type in self.session.query(DBRequestType).all()
        }

        self.rc_service = RadioControllerService(
            SessionManager(self.engine),
            cbsd_states_map=self.cbsd_states,
            request_types_map=self.request_types,
        )
Exemplo n.º 6
0
 def setUp(self):
     super().setUp()
     DBInitializer(SessionManager(self.engine)).initialize()
     grpc_channel = grpc.insecure_channel(
         f"{config.GRPC_SERVICE}:{config.GRPC_PORT}",
     )
     self.dp_client = DPServiceStub(grpc_channel)
     when_elastic_indexes_data()
     _delete_dp_elasticsearch_indices()
Exemplo n.º 7
0
def run():
    """
    Top-level function for radio controller
    """
    logger.info("Starting grpc server")
    config = get_config()
    scheduler = BackgroundScheduler()
    metricsd_client = get_metricsd_client()
    scheduler.add_job(
        process_metrics,
        args=[metricsd_client, config.SERVICE_HOSTNAME, "radio_controller"],
        trigger=IntervalTrigger(
            seconds=config.METRICS_PROCESSING_INTERVAL_SEC, ),
        max_instances=1,
        name="metrics_processing_job",
    )
    scheduler.start()

    logger.info(f"grpc port is: {config.GRPC_PORT}")
    db_engine = create_engine(
        url=config.SQLALCHEMY_DB_URI,
        encoding=config.SQLALCHEMY_DB_ENCODING,
        echo=config.SQLALCHEMY_ECHO,
        future=config.SQLALCHEMY_FUTURE,
        pool_size=config.SQLALCHEMY_ENGINE_POOL_SIZE,
        max_overflow=config.SQLALCHEMY_ENGINE_MAX_OVERFLOW,
    )
    session_manager = SessionManager(db_engine)
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    add_RadioControllerServicer_to_server(
        RadioControllerService(session_manager=session_manager),
        server,
    )
    add_ActiveModeControllerServicer_to_server(
        ActiveModeControllerService(session_manager=session_manager),
        server,
    )
    add_DPServiceServicer_to_server(
        DPService(
            session_manager=session_manager,
            now_func=datetime.now,
            fluentd_client=FluentdClient(),
        ),
        server,
    )
    server.add_insecure_port(f"[::]:{config.GRPC_PORT}")
    server.start()
    logger.info(f"GRPC Server started on port {config.GRPC_PORT}")

    def handle_sigterm(*_):
        logger.info("Received shutdown signal")
        all_rpcs_done_event = server.stop(30)
        all_rpcs_done_event.wait(30)
        logger.info("Shut down gracefully")

    signal(SIGTERM, handle_sigterm)
    server.wait_for_termination()
Exemplo n.º 8
0
 def setUp(self):
     super().setUp()
     grpc_channel = grpc.insecure_channel(
         f"{config.GRPC_SERVICE}:{config.GRPC_PORT}", )
     self.dp_client = DPServiceStub(grpc_channel)
     DBInitializer(SessionManager(self.engine)).initialize()
     # Indexing from previous test may not have been finished yet
     sleep(5)
     self._delete_dp_elasticsearch_indices()
Exemplo n.º 9
0
def main():
    config = get_config()
    db_engine = create_engine(
        url=config.SQLALCHEMY_DB_URI,
        encoding=config.SQLALCHEMY_DB_ENCODING,
        echo=config.SQLALCHEMY_ECHO,
        future=config.SQLALCHEMY_FUTURE,
        pool_size=config.SQLALCHEMY_ENGINE_POOL_SIZE,
        max_overflow=config.SQLALCHEMY_ENGINE_MAX_OVERFLOW,
    )
    session_manager = SessionManager(db_engine=db_engine)
    initializer = DBInitializer(session_manager)
    initializer.initialize()
Exemplo n.º 10
0
def run():
    """
    Top-level function for configuration controller
    """
    config = get_config()
    scheduler = BackgroundScheduler()
    db_engine = create_engine(
        url=config.SQLALCHEMY_DB_URI,
        encoding=config.SQLALCHEMY_DB_ENCODING,
        echo=config.SQLALCHEMY_ECHO,
        future=config.SQLALCHEMY_FUTURE,
        pool_size=config.SQLALCHEMY_ENGINE_POOL_SIZE,
        max_overflow=config.SQLALCHEMY_ENGINE_MAX_OVERFLOW,
    )
    session_manager = SessionManager(db_engine=db_engine)
    router = RequestRouter(
        sas_url=config.SAS_URL,
        rc_ingest_url=config.RC_INGEST_URL,
        cert_path=config.CC_CERT_PATH,
        ssl_key_path=config.CC_SSL_KEY_PATH,
        request_mapping=request_mapping,
        ssl_verify=config.SAS_CERT_PATH,
    )
    fluentd_client = FluentdClient()
    for request_type in RequestTypes:
        req_type = request_type.value
        response_type = request_response[req_type]
        consumer = RequestDBConsumer(
            request_type=req_type,
            request_processing_limit=config.REQUEST_PROCESSING_LIMIT,
        )
        processor = ResponseDBProcessor(
            response_type=response_type,
            process_responses_func=processor_strategies[req_type]["process_responses"],
            fluentd_client=fluentd_client,
        )

        scheduler.add_job(
            process_requests,
            args=[consumer, processor, router, session_manager, fluentd_client],
            trigger=IntervalTrigger(
                seconds=config.REQUEST_PROCESSING_INTERVAL_SEC,
            ),
            max_instances=1,
            name=f"{req_type}_job",
        )
    scheduler.start()

    while True:
        time.sleep(1)
Exemplo n.º 11
0
def run():
    """
    Top-level function for radio controller
    """
    logger.info("Starting grpc server")
    config = get_config()
    logger.info(f"grpc port is: {config.GRPC_PORT}")
    db_engine = create_engine(
        url=config.SQLALCHEMY_DB_URI,
        encoding=config.SQLALCHEMY_DB_ENCODING,
        echo=config.SQLALCHEMY_ECHO,
        future=config.SQLALCHEMY_FUTURE,
        pool_size=config.SQLALCHEMY_ENGINE_POOL_SIZE,
        max_overflow=config.SQLALCHEMY_ENGINE_MAX_OVERFLOW,
    )
    session_manager = SessionManager(db_engine)
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    add_RadioControllerServicer_to_server(
        RadioControllerService(session_manager=session_manager),
        server,
    )
    add_ActiveModeControllerServicer_to_server(
        ActiveModeControllerService(session_manager=session_manager),
        server,
    )
    add_DPServiceServicer_to_server(
        DPService(
            session_manager=session_manager,
            now_func=datetime.now,
            fluentd_client=FluentdClient(),
        ),
        server,
    )
    server.add_insecure_port(f"[::]:{config.GRPC_PORT}")
    server.start()
    logger.info(f"GRPC Server started on port {config.GRPC_PORT}")

    def handle_sigterm(*_):
        logger.info("Received shutdown signal")
        all_rpcs_done_event = server.stop(30)
        all_rpcs_done_event.wait(30)
        logger.info("Shut down gracefully")

    signal(SIGTERM, handle_sigterm)
    server.wait_for_termination()
Exemplo n.º 12
0
def process_requests(
        consumer: RequestDBConsumer,
        processor: ResponseDBProcessor,
        router: RequestRouter,
        session_manager: SessionManager,
        fluentd_client: FluentdClient,
) -> Optional[requests.Response]:
    """
    Process SAS requests
    """

    with session_manager.session_scope() as session:
        requests_map = consumer.get_pending_requests(session)
        requests_type = next(iter(requests_map))
        requests_list = requests_map[requests_type]

        if not requests_list:
            logger.debug(f"Received no {requests_type} requests.")
            return None

        no_of_requests = len(requests_list)
        logger.info(
            f'Processing {no_of_requests} {requests_type} requests',
        )
        bulked_sas_requests = merge_requests(requests_map)

        _log_requests_map(requests_map, fluentd_client)
        try:
            sas_response = router.post_to_sas(bulked_sas_requests)
            logger.info(
                f"Sent {bulked_sas_requests} to SAS and got the following response: {sas_response.content}",
            )
        except RequestRouterError as e:
            logging.error(f"Error posting request to SAS: {e}")
            return None

        logger.info(f"About to process responses {sas_response=}")
        processor.process_response(requests_list, sas_response, session)

        session.commit()

        return sas_response
Exemplo n.º 13
0
 def setUp(self):
     super().setUp()
     DBInitializer(SessionManager(self.engine)).initialize()
Exemplo n.º 14
0
 def setUp(self):
     super().setUp()
     self.initializer = DBInitializer(SessionManager(db_engine=self.engine))
Exemplo n.º 15
0
                if not s.query(DBCbsdState).filter(
                        DBCbsdState.name == state.value).first():
                    cbsd_state = DBCbsdState(name=state.value)
                    s.add(cbsd_state)
            s.commit()


def get_config() -> Config:
    """
    Get configuration for db service
    """
    app_config = os.environ.get('APP_CONFIG', 'ProductionConfig')
    config_module = importlib.import_module(
        '.'.join(f"magma.db_service.config.{app_config}".split('.')[:-1]), )
    config_class = getattr(config_module, app_config.split('.')[-1])

    return config_class()


if __name__ == '__main__':
    config = get_config()
    db_engine = create_engine(
        url=config.SQLALCHEMY_DB_URI,
        encoding=config.SQLALCHEMY_DB_ENCODING,
        echo=config.SQLALCHEMY_ECHO,
        future=config.SQLALCHEMY_FUTURE,
    )
    session_manager = SessionManager(db_engine=db_engine)
    initializer = DBInitializer(session_manager)
    initializer.initialize()