Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
A mock P4Runtime server.
"""
import logging
from concurrent import futures
import grpc
from p4.v1 import p4runtime_pb2_grpc
from .device_p4 import(
DEVICE_P4_ADDRESS, DEVICE_P4_PORT,
DEVICE_P4_WORKERS, DEVICE_P4_GRACE_PERIOD)
from .mock_p4runtime_servicer_impl import MockP4RuntimeServicerImpl
LOGGER = logging.getLogger(__name__)
class MockP4RuntimeService:
"""
P4Runtime server for testing purposes.
"""
def __init__(
self, address=DEVICE_P4_ADDRESS, port=DEVICE_P4_PORT,
max_workers=DEVICE_P4_WORKERS,
grace_period=DEVICE_P4_GRACE_PERIOD):
self.address = address
self.port = port
self.endpoint = f'{self.address}:{self.port}'
self.max_workers = max_workers
self.grace_period = grace_period
self.server = None
self.servicer = None
def start(self):
"""
Start the P4Runtime server.
"""
LOGGER.info(
'Starting P4Runtime service on %s with max_workers: %s',
str(self.endpoint), str(self.max_workers))
self.server = grpc.server(
futures.ThreadPoolExecutor(max_workers=self.max_workers))
self.servicer = MockP4RuntimeServicerImpl()
p4runtime_pb2_grpc.add_P4RuntimeServicer_to_server(
self.servicer, self.server)
_ = self.server.add_insecure_port(self.endpoint)
LOGGER.info('Listening on %s...', str(self.endpoint))
self.server.start()
LOGGER.debug('P4Runtime service started')
def stop(self):
"""
Stop the P4Runtime server.
"""
LOGGER.debug(
'Stopping P4Runtime service (grace period %d seconds...',
self.grace_period)
self.server.stop(self.grace_period)
LOGGER.debug('P4Runtime service stopped')