Skip to content
Snippets Groups Projects
Select Git revision
  • d72559b19f235fd4f85125148df5750011d8fe1c
  • master default protected
  • release/6.0.0 protected
  • develop protected
  • feat/345-cttc-fix-ci-cd-and-unit-tests-for-dscm-pluggables
  • feat/349-new-monitoring-updates-for-optical-controller-integration
  • cnit_ofc26
  • ofc_polimi
  • CTTC-IMPLEMENT-NBI-CONNECTOR-NOS-ZTP
  • CTTC-TEST-SMARTNICS-6GMICROSDN-ZTP
  • feat/327-tid-new-service-to-ipowdm-controller-to-manage-transceivers-configuration-on-external-agent
  • cnit_tapi
  • feat/330-tid-pcep-component
  • feat/tid-newer-pcep-component
  • feat/116-ubi-updates-in-telemetry-backend-to-support-p4-in-band-network-telemetry
  • feat/292-cttc-implement-integration-test-for-ryu-openflow
  • cnit-p2mp-premerge
  • feat/325-tid-nbi-e2e-to-manage-e2e-path-computation
  • feat/326-tid-external-management-of-devices-telemetry-nbi
  • feat/324-tid-nbi-ietf_l3vpn-deploy-fail
  • feat/321-add-support-for-gnmi-configuration-via-proto
  • v6.0.0 protected
  • v5.0.0 protected
  • v4.0.0 protected
  • demo-dpiab-eucnc2024
  • v3.0.0 protected
  • v2.1.0 protected
  • v2.0.0 protected
  • v1.0.0 protected
29 results

ContextPolicy.java

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    DeviceService.py 2.39 KiB
    import grpc
    import logging
    from concurrent import futures
    from grpc_health.v1.health import HealthServicer, OVERALL_HEALTH
    from grpc_health.v1.health_pb2 import HealthCheckResponse
    from grpc_health.v1.health_pb2_grpc import add_HealthServicer_to_server
    from device.proto.device_pb2_grpc import add_DeviceServiceServicer_to_server
    from device.service.DeviceServiceServicerImpl import DeviceServiceServicerImpl
    from device.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD
    
    BIND_ADDRESS = '0.0.0.0'
    LOGGER = logging.getLogger(__name__)
    
    class DeviceService:
        def __init__(self, database, address=BIND_ADDRESS, port=GRPC_SERVICE_PORT, max_workers=GRPC_MAX_WORKERS,
                     grace_period=GRPC_GRACE_PERIOD):
            self.database = database
            self.address = address
            self.port = port
            self.endpoint = None
            self.max_workers = max_workers
            self.grace_period = grace_period
            self.device_servicer = None
            self.health_servicer = None
            self.pool = None
            self.server = None
    
        def start(self):
            self.endpoint = '{}:{}'.format(self.address, self.port)
            LOGGER.debug('Starting Service (tentative endpoint: {}, max_workers: {})...'.format(
                self.endpoint, self.max_workers))
    
            self.pool = futures.ThreadPoolExecutor(max_workers=self.max_workers)
            self.server = grpc.server(self.pool) # , interceptors=(tracer_interceptor,))
    
            self.device_servicer = DeviceServiceServicerImpl(self.database)
            add_DeviceServiceServicer_to_server(self.device_servicer, self.server)
    
            self.health_servicer = HealthServicer(
                experimental_non_blocking=True, experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
            add_HealthServicer_to_server(self.health_servicer, self.server)
    
            port = self.server.add_insecure_port(self.endpoint)
            self.endpoint = '{}:{}'.format(self.address, port)
            LOGGER.info('Listening on {}...'.format(self.endpoint))
            self.server.start()
            self.health_servicer.set(OVERALL_HEALTH, HealthCheckResponse.SERVING) # pylint: disable=maybe-no-member
    
            LOGGER.debug('Service started')
    
        def stop(self):
            LOGGER.debug('Stopping service (grace period {} seconds)...'.format(self.grace_period))
            self.health_servicer.enter_graceful_shutdown()
            self.server.stop(self.grace_period)
            LOGGER.debug('Service stopped')