blob: 2624ed67bc2f5bb0e168f4982bdc09f579aac6ec [file] [log] [blame]
Zsolt Haraszti66862032016-11-28 14:28:39 -08001# Copyright 2016 the original author or authors.
2#
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
267 def ActivateDevice(self, request, context):
268 log.info('grpc-request', request=request)
269 # TODO dispatching to local instead of passing it to leader
270 return self.dispatcher.dispatch(
271 self.instance_id,
272 VolthaLocalServiceStub,
273 'ActivateDevice',
274 request,
275 context)
276
277 @twisted_async
278 def ListDevicePorts(self, request, context):
279 log.info('grpc-request', request=request)
280
281 try:
282 instance_id = self.dispatcher.instance_id_by_device_id(
283 request.id
284 )
285 except KeyError:
286 context.set_details(
287 'Device \'{}\' not found'.format(request.id))
288 context.set_code(StatusCode.NOT_FOUND)
289 return Ports()
290
291 return self.dispatcher.dispatch(
292 instance_id,
293 VolthaLocalServiceStub,
294 'ListDevicePorts',
295 request,
296 context)
297
298 @twisted_async
299 def ListDeviceFlows(self, request, context):
300 log.info('grpc-request', request=request)
301
302 try:
303 instance_id = self.dispatcher.instance_id_by_device_id(
304 request.id
305 )
306 except KeyError:
307 context.set_details(
308 'Device \'{}\' not found'.format(request.id))
309 context.set_code(StatusCode.NOT_FOUND)
310 return Flows()
311
312 return self.dispatcher.dispatch(
313 instance_id,
314 VolthaLocalServiceStub,
315 'ListDeviceFlows',
316 request,
317 context)
318
319 @twisted_async
320 def ListDeviceFlowGroups(self, request, context):
321 log.info('grpc-request', request=request)
322
323 try:
324 instance_id = self.dispatcher.instance_id_by_device_id(
325 request.id
326 )
327 except KeyError:
328 context.set_details(
329 'Device \'{}\' not found'.format(request.id))
330 context.set_code(StatusCode.NOT_FOUND)
331 return FlowGroups()
332
333 return self.dispatcher.dispatch(
334 instance_id,
335 VolthaLocalServiceStub,
336 'ListDeviceFlowGroups',
337 request,
338 context)
339
340 @twisted_async
341 def ListDeviceTypes(self, request, context):
342 log.info('grpc-request', request=request)
343 # we always deflect this to the local instance, as we assume
344 # they all loaded the same adapters, supporting the same device
345 # types
346 return self.dispatcher.dispatch(
347 self.instance_id,
348 VolthaLocalServiceStub,
349 'ListDeviceTypes',
350 request,
351 context)
352
353 @twisted_async
354 def GetDeviceType(self, request, context):
355 log.info('grpc-request', request=request)
356 # we always deflect this to the local instance, as we assume
357 # they all loaded the same adapters, supporting the same device
358 # types
359 return self.dispatcher.dispatch(
360 self.instance_id,
361 VolthaLocalServiceStub,
362 'GetDeviceType',
363 request,
364 context)
365
366 @twisted_async
367 def ListDeviceGroups(self, request, context):
368 log.warning('temp-limited-implementation')
369 # TODO dispatching to local instead of collecting all
370 return self.dispatcher.dispatch(
371 self.instance_id,
372 VolthaLocalServiceStub,
373 'ListDeviceGroups',
374 Empty(),
375 context)
376
377 @twisted_async
378 def GetDeviceGroup(self, request, context):
379 log.warning('temp-limited-implementation')
380 # TODO dispatching to local instead of collecting all
381 return self.dispatcher.dispatch(
382 self.instance_id,
383 VolthaLocalServiceStub,
384 'GetDeviceGroup',
385 request,
386 context)
387
388