blob: eff422ef57e30db15c415a6e4bf8cec47d7d24b9 [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
Sergio Slobodriana2eb52b2017-03-07 12:24:46 -0500372 #TODO: create the global PM config query function
373 @twisted_async
374 def ListDevicePmConfigs(self, request, context):
375 raise NotImplementedError('Method not implemented!')
376
377 #TODO: create the global PM config update function.
378 @twisted_async
379 def UpdateDevicePmConfigs(self, request, context):
380 raise NotImplementedError('Method not implemented!')
381
Zsolt Haraszti66862032016-11-28 14:28:39 -0800382 @twisted_async
383 def ListDeviceFlows(self, request, context):
384 log.info('grpc-request', request=request)
385
386 try:
387 instance_id = self.dispatcher.instance_id_by_device_id(
388 request.id
389 )
390 except KeyError:
391 context.set_details(
392 'Device \'{}\' not found'.format(request.id))
393 context.set_code(StatusCode.NOT_FOUND)
394 return Flows()
395
396 return self.dispatcher.dispatch(
397 instance_id,
398 VolthaLocalServiceStub,
399 'ListDeviceFlows',
400 request,
401 context)
402
403 @twisted_async
404 def ListDeviceFlowGroups(self, request, context):
405 log.info('grpc-request', request=request)
406
407 try:
408 instance_id = self.dispatcher.instance_id_by_device_id(
409 request.id
410 )
411 except KeyError:
412 context.set_details(
413 'Device \'{}\' not found'.format(request.id))
414 context.set_code(StatusCode.NOT_FOUND)
415 return FlowGroups()
416
417 return self.dispatcher.dispatch(
418 instance_id,
419 VolthaLocalServiceStub,
420 'ListDeviceFlowGroups',
421 request,
422 context)
423
424 @twisted_async
425 def ListDeviceTypes(self, request, context):
426 log.info('grpc-request', request=request)
427 # we always deflect this to the local instance, as we assume
428 # they all loaded the same adapters, supporting the same device
429 # types
430 return self.dispatcher.dispatch(
431 self.instance_id,
432 VolthaLocalServiceStub,
433 'ListDeviceTypes',
434 request,
435 context)
436
437 @twisted_async
438 def GetDeviceType(self, request, context):
439 log.info('grpc-request', request=request)
440 # we always deflect this to the local instance, as we assume
441 # they all loaded the same adapters, supporting the same device
442 # types
443 return self.dispatcher.dispatch(
444 self.instance_id,
445 VolthaLocalServiceStub,
446 'GetDeviceType',
447 request,
448 context)
449
450 @twisted_async
451 def ListDeviceGroups(self, request, context):
452 log.warning('temp-limited-implementation')
453 # TODO dispatching to local instead of collecting all
454 return self.dispatcher.dispatch(
455 self.instance_id,
456 VolthaLocalServiceStub,
457 'ListDeviceGroups',
458 Empty(),
459 context)
460
461 @twisted_async
462 def GetDeviceGroup(self, request, context):
463 log.warning('temp-limited-implementation')
464 # TODO dispatching to local instead of collecting all
465 return self.dispatcher.dispatch(
466 self.instance_id,
467 VolthaLocalServiceStub,
468 'GetDeviceGroup',
469 request,
470 context)
471
472