blob: 7d52d293fc6d78c8d8154ce9105705932622f66f [file] [log] [blame]
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08001# Copyright 2017 the original author or authors.
Zsolt Haraszti66862032016-11-28 14:28:39 -08002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15import structlog
16from grpc import StatusCode
17from twisted.internet.defer import inlineCallbacks
18from twisted.internet.defer import returnValue
19
20from common.utils.grpc_utils import twisted_async
21from voltha.core.config.config_root import ConfigRoot
22from voltha.protos.voltha_pb2 import \
23 add_VolthaGlobalServiceServicer_to_server, VolthaLocalServiceStub, \
24 VolthaGlobalServiceServicer, Voltha, VolthaInstances, VolthaInstance, \
25 LogicalDevice, Ports, Flows, FlowGroups, Device
26from voltha.registry import registry
27from google.protobuf.empty_pb2 import Empty
28
29log = structlog.get_logger()
30
31
32class GlobalHandler(VolthaGlobalServiceServicer):
33
34 def __init__(self, dispatcher, instance_id, **init_kw):
35 self.dispatcher = dispatcher
36 self.instance_id = instance_id
37 self.init_kw = init_kw
38 self.root = None
39 self.stopped = False
40
41 def start(self):
42 log.debug('starting')
43 self.root = ConfigRoot(Voltha(**self.init_kw))
44 registry('grpc_server').register(
45 add_VolthaGlobalServiceServicer_to_server, self)
46 log.info('started')
47 return self
48
49 def stop(self):
50 log.debug('stopping')
51 self.stopped = True
52 log.info('stopped')
53
54 # gRPC service method implementations. BE CAREFUL; THESE ARE CALLED ON
55 # the gRPC threadpool threads.
56
57 @twisted_async
58 def GetVoltha(self, request, context):
59 log.info('grpc-request', request=request)
60 return self.root.get('/', depth=1)
61
62 @twisted_async
63 @inlineCallbacks
64 def ListVolthaInstances(self, request, context):
65 log.info('grpc-request', request=request)
66 items = yield registry('coordinator').get_members()
67 returnValue(VolthaInstances(items=items))
68
69 @twisted_async
70 def GetVolthaInstance(self, request, context):
71 log.info('grpc-request', request=request)
72 instance_id = request.id
73 try:
74 return self.dispatcher.dispatch(
75 instance_id,
76 VolthaLocalServiceStub,
77 'GetVolthaInstance',
78 Empty(),
79 context)
80 except KeyError:
81 context.set_details(
82 'Voltha instance \'{}\' not found'.format(instance_id))
83 context.set_code(StatusCode.NOT_FOUND)
84 return VolthaInstance()
85
86 @twisted_async
87 def ListLogicalDevices(self, request, context):
88 log.warning('temp-limited-implementation')
89 # TODO dispatching to local instead of collecting all
90 return self.dispatcher.dispatch(
91 self.instance_id,
92 VolthaLocalServiceStub,
93 'ListLogicalDevices',
94 Empty(),
95 context)
96
97 @twisted_async
98 def GetLogicalDevice(self, request, context):
99 log.info('grpc-request', request=request)
100
101 try:
102 instance_id = self.dispatcher.instance_id_by_logical_device_id(
103 request.id
104 )
105 except KeyError:
106 context.set_details(
107 'Logical device \'{}\' not found'.format(request.id))
108 context.set_code(StatusCode.NOT_FOUND)
109 return LogicalDevice()
110
111 return self.dispatcher.dispatch(
112 instance_id,
113 VolthaLocalServiceStub,
114 'GetLogicalDevice',
115 request,
116 context)
117
118 @twisted_async
119 def ListLogicalDevicePorts(self, request, context):
120 log.info('grpc-request', request=request)
121
122 try:
123 instance_id = self.dispatcher.instance_id_by_logical_device_id(
124 request.id
125 )
126 except KeyError:
127 context.set_details(
128 'Logical device \'{}\' not found'.format(request.id))
129 context.set_code(StatusCode.NOT_FOUND)
130 return Ports()
131
132 return self.dispatcher.dispatch(
133 instance_id,
134 VolthaLocalServiceStub,
135 'ListLogicalDevicePorts',
136 request,
137 context)
138
139 @twisted_async
140 def ListLogicalDeviceFlows(self, request, context):
141 log.info('grpc-request', request=request)
142
143 try:
144 instance_id = self.dispatcher.instance_id_by_logical_device_id(
145 request.id
146 )
147 except KeyError:
148 context.set_details(
149 'Logical device \'{}\' not found'.format(request.id))
150 context.set_code(StatusCode.NOT_FOUND)
151 return Flows()
152
153 return self.dispatcher.dispatch(
154 instance_id,
155 VolthaLocalServiceStub,
156 'ListLogicalDeviceFlows',
157 request,
158 context)
159
160 @twisted_async
161 def UpdateLogicalDeviceFlowTable(self, request, context):
162 log.info('grpc-request', request=request)
163
164 try:
165 instance_id = self.dispatcher.instance_id_by_logical_device_id(
166 request.id
167 )
168 except KeyError:
169 context.set_details(
170 'Logical device \'{}\' not found'.format(request.id))
171 context.set_code(StatusCode.NOT_FOUND)
172 return Empty()
173
174 return self.dispatcher.dispatch(
175 instance_id,
176 VolthaLocalServiceStub,
177 'UpdateLogicalDeviceFlowTable',
178 request,
179 context)
180
181 @twisted_async
182 def ListLogicalDeviceFlowGroups(self, request, context):
183 log.info('grpc-request', request=request)
184
185 try:
186 instance_id = self.dispatcher.instance_id_by_logical_device_id(
187 request.id
188 )
189 except KeyError:
190 context.set_details(
191 'Logical device \'{}\' not found'.format(request.id))
192 context.set_code(StatusCode.NOT_FOUND)
193 return FlowGroups()
194
195 return self.dispatcher.dispatch(
196 instance_id,
197 VolthaLocalServiceStub,
198 'ListLogicalDeviceFlowGroups',
199 request,
200 context)
201
202 @twisted_async
203 def UpdateLogicalDeviceFlowGroupTable(self, request, context):
204 log.info('grpc-request', request=request)
205
206 try:
207 instance_id = self.dispatcher.instance_id_by_logical_device_id(
208 request.id
209 )
210 except KeyError:
211 context.set_details(
212 'Logical device \'{}\' not found'.format(request.id))
213 context.set_code(StatusCode.NOT_FOUND)
214 return Empty()
215
216 return self.dispatcher.dispatch(
217 instance_id,
218 VolthaLocalServiceStub,
219 'UpdateLogicalDeviceFlowGroupTable',
220 request,
221 context)
222
223 @twisted_async
224 def ListDevices(self, request, context):
225 log.warning('temp-limited-implementation')
226 # TODO dispatching to local instead of collecting all
227 return self.dispatcher.dispatch(
228 self.instance_id,
229 VolthaLocalServiceStub,
230 'ListDevices',
231 request,
232 context)
233
234 @twisted_async
235 def GetDevice(self, request, context):
236 log.info('grpc-request', request=request)
237
238 try:
239 instance_id = self.dispatcher.instance_id_by_device_id(
240 request.id
241 )
242 except KeyError:
243 context.set_details(
244 'Device \'{}\' not found'.format(request.id))
245 context.set_code(StatusCode.NOT_FOUND)
246 return Device()
247
248 return self.dispatcher.dispatch(
249 instance_id,
250 VolthaLocalServiceStub,
251 'GetDevice',
252 request,
253 context)
254
255 @twisted_async
256 def CreateDevice(self, request, context):
257 log.info('grpc-request', request=request)
258 # TODO dispatching to local instead of passing it to leader
259 return self.dispatcher.dispatch(
260 self.instance_id,
261 VolthaLocalServiceStub,
262 'CreateDevice',
263 request,
264 context)
265
266 @twisted_async
Khen Nursimulud068d812017-03-06 11:44:18 -0500267 def EnableDevice(self, request, context):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800268 log.info('grpc-request', request=request)
Khen Nursimulud068d812017-03-06 11:44:18 -0500269
270 try:
271 instance_id = self.dispatcher.instance_id_by_device_id(
272 request.id
273 )
274 except KeyError:
275 context.set_details(
276 'Device \'{}\' not found'.format(request.id))
277 context.set_code(StatusCode.NOT_FOUND)
278 return Device()
279
Zsolt Haraszti66862032016-11-28 14:28:39 -0800280 return self.dispatcher.dispatch(
Khen Nursimulud068d812017-03-06 11:44:18 -0500281 instance_id,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800282 VolthaLocalServiceStub,
Khen Nursimulud068d812017-03-06 11:44:18 -0500283 'EnableDevice',
284 request,
285 context)
286
287
288 @twisted_async
289 def DisableDevice(self, request, context):
290 log.info('grpc-request', request=request)
291
292 try:
293 instance_id = self.dispatcher.instance_id_by_device_id(
294 request.id
295 )
296 except KeyError:
297 context.set_details(
298 'Device \'{}\' not found'.format(request.id))
299 context.set_code(StatusCode.NOT_FOUND)
300 return Device()
301
302 return self.dispatcher.dispatch(
303 instance_id,
304 VolthaLocalServiceStub,
305 'DisableDevice',
306 request,
307 context)
308
309 @twisted_async
310 def RebootDevice(self, request, context):
311 log.info('grpc-request', request=request)
312
313 try:
314 instance_id = self.dispatcher.instance_id_by_device_id(
315 request.id
316 )
317 except KeyError:
318 context.set_details(
319 'Device \'{}\' not found'.format(request.id))
320 context.set_code(StatusCode.NOT_FOUND)
321 return Device()
322
323 return self.dispatcher.dispatch(
324 instance_id,
325 VolthaLocalServiceStub,
326 'RebootDevice',
327 request,
328 context)
329
330 @twisted_async
331 def DeleteDevice(self, request, context):
332 log.info('grpc-request', request=request)
333
334 try:
335 instance_id = self.dispatcher.instance_id_by_device_id(
336 request.id
337 )
338 except KeyError:
339 context.set_details(
340 'Device \'{}\' not found'.format(request.id))
341 context.set_code(StatusCode.NOT_FOUND)
342 return Device()
343
344 return self.dispatcher.dispatch(
345 instance_id,
346 VolthaLocalServiceStub,
347 'DeleteDevice',
Zsolt Haraszti66862032016-11-28 14:28:39 -0800348 request,
349 context)
350
351 @twisted_async
352 def ListDevicePorts(self, request, context):
353 log.info('grpc-request', request=request)
354
355 try:
356 instance_id = self.dispatcher.instance_id_by_device_id(
357 request.id
358 )
359 except KeyError:
360 context.set_details(
361 'Device \'{}\' not found'.format(request.id))
362 context.set_code(StatusCode.NOT_FOUND)
363 return Ports()
364
365 return self.dispatcher.dispatch(
366 instance_id,
367 VolthaLocalServiceStub,
368 'ListDevicePorts',
369 request,
370 context)
371
372 @twisted_async
373 def ListDeviceFlows(self, request, context):
374 log.info('grpc-request', request=request)
375
376 try:
377 instance_id = self.dispatcher.instance_id_by_device_id(
378 request.id
379 )
380 except KeyError:
381 context.set_details(
382 'Device \'{}\' not found'.format(request.id))
383 context.set_code(StatusCode.NOT_FOUND)
384 return Flows()
385
386 return self.dispatcher.dispatch(
387 instance_id,
388 VolthaLocalServiceStub,
389 'ListDeviceFlows',
390 request,
391 context)
392
393 @twisted_async
394 def ListDeviceFlowGroups(self, request, context):
395 log.info('grpc-request', request=request)
396
397 try:
398 instance_id = self.dispatcher.instance_id_by_device_id(
399 request.id
400 )
401 except KeyError:
402 context.set_details(
403 'Device \'{}\' not found'.format(request.id))
404 context.set_code(StatusCode.NOT_FOUND)
405 return FlowGroups()
406
407 return self.dispatcher.dispatch(
408 instance_id,
409 VolthaLocalServiceStub,
410 'ListDeviceFlowGroups',
411 request,
412 context)
413
414 @twisted_async
415 def ListDeviceTypes(self, request, context):
416 log.info('grpc-request', request=request)
417 # we always deflect this to the local instance, as we assume
418 # they all loaded the same adapters, supporting the same device
419 # types
420 return self.dispatcher.dispatch(
421 self.instance_id,
422 VolthaLocalServiceStub,
423 'ListDeviceTypes',
424 request,
425 context)
426
427 @twisted_async
428 def GetDeviceType(self, request, context):
429 log.info('grpc-request', request=request)
430 # we always deflect this to the local instance, as we assume
431 # they all loaded the same adapters, supporting the same device
432 # types
433 return self.dispatcher.dispatch(
434 self.instance_id,
435 VolthaLocalServiceStub,
436 'GetDeviceType',
437 request,
438 context)
439
440 @twisted_async
441 def ListDeviceGroups(self, request, context):
442 log.warning('temp-limited-implementation')
443 # TODO dispatching to local instead of collecting all
444 return self.dispatcher.dispatch(
445 self.instance_id,
446 VolthaLocalServiceStub,
447 'ListDeviceGroups',
448 Empty(),
449 context)
450
451 @twisted_async
452 def GetDeviceGroup(self, request, context):
453 log.warning('temp-limited-implementation')
454 # TODO dispatching to local instead of collecting all
455 return self.dispatcher.dispatch(
456 self.instance_id,
457 VolthaLocalServiceStub,
458 'GetDeviceGroup',
459 request,
460 context)
461
462