Newer
Older
from typing import Iterator
from common.Constants import DEFAULT_CONTEXT_UUID, DEFAULT_TOPOLOGY_UUID
#from common.exceptions.ServiceException import ServiceException
from common.metrics.Metrics import create_metrics, safe_and_metered_rpc_method
from common.orm.Database import Database
from context.proto.context_pb2 import \
Context, ContextEvent, ContextId, ContextIdList, ContextList, \
Device, DeviceEvent, DeviceId, DeviceIdList, DeviceList, \
Empty, \
Link, LinkEvent, LinkId, LinkIdList, LinkList, \
Service, ServiceEvent, ServiceId, ServiceIdList, ServiceList, \
Topology, TopologyEvent, TopologyId, TopologyIdList, TopologyList
from context.proto.context_pb2_grpc import ContextServiceServicer
#from .Tools import check_link_id_request, check_link_request
LOGGER = logging.getLogger(__name__)
SERVICE_NAME = 'Context'
METHOD_NAMES = [
'ListContextIds', 'ListContexts', 'GetContext', 'SetContext', 'RemoveContext', 'GetContextEvents',
'ListTopologyIds', 'ListTopologies', 'GetTopology', 'SetTopology', 'RemoveTopology', 'GetTopologyEvents',
'ListDeviceIds', 'ListDevices', 'GetDevice', 'SetDevice', 'RemoveDevice', 'GetDeviceEvents',
'ListLinkIds', 'ListLinks', 'GetLink', 'SetLink', 'RemoveLink', 'GetLinkEvents',
'ListServiceIds', 'ListServices', 'GetService', 'SetService', 'RemoveService', 'GetServiceEvents',
]
METRICS = create_metrics(SERVICE_NAME, METHOD_NAMES)
class ContextServiceServicerImpl(ContextServiceServicer):
LOGGER.debug('Creating Servicer...')
self.database = database
LOGGER.debug('Servicer Created')
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListContextIds(self, request: Empty, context : grpc.ServicerContext) -> ContextIdList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListContexts(self, request: Empty, context : grpc.ServicerContext) -> ContextList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetContext(self, request: ContextId, context : grpc.ServicerContext) -> Context:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def SetContext(self, request: Context, context : grpc.ServicerContext) -> ContextId:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def RemoveContext(self, request: ContextId, context : grpc.ServicerContext) -> Empty:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetContextEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[ContextEvent]:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListTopologyIds(self, request: ContextId, context : grpc.ServicerContext) -> TopologyIdList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListTopologies(self, request: ContextId, context : grpc.ServicerContext) -> TopologyList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetTopology(self, request: TopologyId, context : grpc.ServicerContext) -> Topology:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def SetTopology(self, request: Topology, context : grpc.ServicerContext) -> TopologyId:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def RemoveTopology(self, request: TopologyId, context : grpc.ServicerContext) -> Empty:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetTopologyEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[TopologyEvent]:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListDeviceIds(self, request: Empty, context : grpc.ServicerContext) -> DeviceIdList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListDevices(self, request: Empty, context : grpc.ServicerContext) -> DeviceList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetDevice(self, request: DeviceId, context : grpc.ServicerContext) -> Device:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def SetDevice(self, request: Device, context : grpc.ServicerContext) -> DeviceId:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def RemoveDevice(self, request: DeviceId, context : grpc.ServicerContext) -> Empty:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetDeviceEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[DeviceEvent]:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListLinkIds(self, request: Empty, context : grpc.ServicerContext) -> LinkIdList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListLinks(self, request: Empty, context : grpc.ServicerContext) -> LinkList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetLink(self, request: LinkId, context : grpc.ServicerContext) -> Link:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def SetLink(self, request: Link, context : grpc.ServicerContext) -> LinkId:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def RemoveLink(self, request: LinkId, context : grpc.ServicerContext) -> Empty:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetLinkEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[LinkEvent]:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListServiceIds(self, request: ContextId, context : grpc.ServicerContext) -> ServiceIdList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListServices(self, request: ContextId, context : grpc.ServicerContext) -> ServiceList:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetService(self, request: ServiceId, context : grpc.ServicerContext) -> Service:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def SetService(self, request: Service, context : grpc.ServicerContext) -> ServiceId:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def RemoveService(self, request: ServiceId, context : grpc.ServicerContext) -> Empty:
pass
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetServiceEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[ServiceEvent]:
pass
# @safe_and_metered_rpc_method(METRICS, LOGGER)
# def GetTopology(self, request : Empty, grpc_context : grpc.ServicerContext) -> Topology:
# # ----- Validate request data and pre-conditions -----------------------------------------------------------
# db_context = self.database.context(DEFAULT_CONTEXT_UUID).create()
# db_topology = db_context.topology(DEFAULT_TOPOLOGY_UUID).create()
#
# # ----- Retrieve data from the database --------------------------------------------------------------------
# json_topology = db_topology.dump()
#
# # ----- Compose reply --------------------------------------------------------------------------------------
# return Topology(**json_topology)
#
# def CreaAddLink(self, request : Link, grpc_context : grpc.ServicerContext) -> LinkId:
# ADDLINK_COUNTER_STARTED.inc()
# try:
# LOGGER.debug('AddLink request: {}'.format(str(request)))
#
# # ----- Validate request data and pre-conditions -----------------------------------------------------------
# link_id, db_endpoints = check_link_request('AddLink', request, self.database, LOGGER)
#
# # ----- Implement changes in the database ------------------------------------------------------------------
# db_context = self.database.context(DEFAULT_CONTEXT_UUID).create()
# db_topology = db_context.topology(DEFAULT_TOPOLOGY_UUID).create()
# db_link = db_topology.link(link_id).create()
# for db_endpoint in db_endpoints:
# link_endpoint_id = '{}/{}'.format(
# db_endpoint.device_uuid, db_endpoint.endpoint_uuid)
# db_link.endpoint(link_endpoint_id).create(db_endpoint)
#
# # ----- Compose reply --------------------------------------------------------------------------------------
# reply = LinkId(**db_link.dump_id())
# LOGGER.debug('AddLink reply: {}'.format(str(reply)))
# ADDLINK_COUNTER_COMPLETED.inc()
# return reply
# except ServiceException as e:
# LOGGER.exception('AddLink exception')
# ADDLINK_COUNTER_FAILED.inc()
# grpc_context.abort(e.code, e.details)
# except Exception as e: # pragma: no cover
# LOGGER.exception('AddLink exception')
# ADDLINK_COUNTER_FAILED.inc()
# grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))
#
# @DELETELINK_HISTOGRAM_DURATION.time()
# def DeleteLink(self, request : LinkId, grpc_context : grpc.ServicerContext) -> Empty:
# DELETELINK_COUNTER_STARTED.inc()
# try:
# LOGGER.debug('DeleteLink request: {}'.format(str(request)))
#
# # ----- Validate request data and pre-conditions -----------------------------------------------------------
# link_id = check_link_id_request('DeleteLink', request, self.database, LOGGER)
#
# # ----- Implement changes in the database ------------------------------------------------------------------
# db_context = self.database.context(DEFAULT_CONTEXT_UUID).create()
# db_topology = db_context.topology(DEFAULT_TOPOLOGY_UUID).create()
# db_topology.link(link_id).delete()
#
# # ----- Compose reply --------------------------------------------------------------------------------------
# reply = Empty()
# LOGGER.debug('DeleteLink reply: {}'.format(str(reply)))
# DELETELINK_COUNTER_COMPLETED.inc()
# return reply
# except ServiceException as e:
# LOGGER.exception('DeleteLink exception')
# DELETELINK_COUNTER_FAILED.inc()
# grpc_context.abort(e.code, e.details)
# except Exception as e: # pragma: no cover
# LOGGER.exception('DeleteLink exception')
# DELETELINK_COUNTER_FAILED.inc()
# grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))