Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
| 18 | Dispatcher is responsible to dispatch incoming "global" gRPC requests |
| 19 | to the respective Voltha instance (leader, peer instance, local). Local |
| 20 | calls are forwarded to the LocalHandler. |
| 21 | """ |
| 22 | import structlog |
| 23 | |
| 24 | from voltha.protos.voltha_pb2 import VolthaLocalServiceStub |
| 25 | |
| 26 | log = structlog.get_logger() |
| 27 | |
| 28 | |
| 29 | class Dispatcher(object): |
| 30 | |
| 31 | def __init__(self, core, instance_id): |
| 32 | self.core = core |
| 33 | self.instance_id = instance_id |
| 34 | self.local_handler = None |
| 35 | |
| 36 | def start(self): |
| 37 | log.debug('starting') |
| 38 | self.local_handler = self.core.get_local_handler() |
| 39 | log.info('started') |
| 40 | return self |
| 41 | |
| 42 | def stop(self): |
| 43 | log.debug('stopping') |
| 44 | log.info('stopped') |
| 45 | |
| 46 | def dispatch(self, instance_id, stub, method_name, input, context): |
| 47 | log.debug('dispatch', instance_id=instance_id, stub=stub, |
| 48 | _method_name=method_name, input=input) |
| 49 | # special case if instance_id is us |
| 50 | if instance_id == self.instance_id: |
| 51 | # for now, we assume it is always the local stub |
| 52 | assert stub == VolthaLocalServiceStub |
| 53 | method = getattr(self.local_handler, method_name) |
| 54 | log.debug('dispatching', method=method) |
| 55 | res = method(input, context=context) |
| 56 | log.debug('dispatch-success', res=res) |
| 57 | return res |
| 58 | |
| 59 | else: |
| 60 | log.warning('no-real-dispatch-yet') |
| 61 | raise KeyError() |
| 62 | |
| 63 | def instance_id_by_logical_device_id(self, logical_device_id): |
| 64 | log.warning('temp-mapping-logical-device-id') |
| 65 | # TODO no true dispatchong yet, we blindly map everything to self |
| 66 | return self.instance_id |
| 67 | |
| 68 | def instance_id_by_device_id(self, device_id): |
| 69 | log.warning('temp-mapping-logical-device-id') |
| 70 | # TODO no true dispatchong yet, we blindly map everything to self |
| 71 | return self.instance_id |