Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 the original author or authors. |
| 4 | # |
| 5 | # Code adapted from https://github.com/choppsv1/netconf |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | import structlog |
| 20 | from base.commit import Commit |
| 21 | from base.copy_config import CopyConfig |
| 22 | from base.delete_config import DeleteConfig |
| 23 | from base.discard_changes import DiscardChanges |
| 24 | from base.edit_config import EditConfig |
| 25 | from base.get import Get |
| 26 | from base.get_config import GetConfig |
| 27 | from base.lock import Lock |
| 28 | from base.unlock import UnLock |
| 29 | from base.close_session import CloseSession |
| 30 | from base.kill_session import KillSession |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 31 | from ext.get_voltha import GetVoltha |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 32 | from netconf import NSMAP, qmap |
| 33 | import netconf.nc_common.error as ncerror |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 34 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 35 | log = structlog.get_logger() |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 36 | from lxml import etree |
| 37 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 38 | |
| 39 | class RpcFactory: |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 40 | instance = None |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 41 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 42 | def __init__(self): |
| 43 | self.rpc_map = {} |
| 44 | # TODO: This will be loaded after the yang modules have been |
| 45 | # generated from proto files |
| 46 | self.register_rpc('{urn:opencord:params:xml:ns:voltha:ietf-voltha}', |
| 47 | 'VolthaGlobalService', 'GetVoltha', GetVoltha) |
| 48 | self.register_rpc('{urn:opencord:params:xml:ns:voltha:ietf-voltha}', |
| 49 | 'any', 'any', GetVoltha) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 50 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 51 | def _get_key(self, namespace, service, name): |
| 52 | return ''.join([namespace, service, name]) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 53 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 54 | def register_rpc(self, namespace, service, name, klass): |
| 55 | key = self._get_key(namespace, service, name) |
| 56 | if key not in self.rpc_map.keys(): |
| 57 | self.rpc_map[key] = klass |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 58 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 59 | def get_handler(self, namespace, service, name): |
| 60 | key = self._get_key(namespace, service, name) |
| 61 | if key in self.rpc_map.keys(): |
| 62 | return self.rpc_map[key] |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 63 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 64 | def get_rpc_handler(self, rpc_node, msg, grpc_channel, session): |
| 65 | try: |
| 66 | msg_id = rpc_node.get('message-id') |
| 67 | log.info("Received-rpc-message-id", msg_id=msg_id) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 68 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 69 | except (TypeError, ValueError): |
| 70 | raise ncerror.SessionError(msg, |
| 71 | "No valid message-id attribute found") |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 72 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 73 | log.info("rpc-node", node=etree.tostring(rpc_node, pretty_print=True)) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 74 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 75 | # Get the first child of rpc as the method name |
| 76 | rpc_method = rpc_node.getchildren() |
| 77 | if len(rpc_method) != 1: |
| 78 | log.error("badly-formatted-rpc-method", msg_id=msg_id) |
| 79 | raise ncerror.BadMsg(rpc_node) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 80 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 81 | rpc_method = rpc_method[0] |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 82 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 83 | rpc_name = rpc_method.tag.replace(qmap('nc'), "") |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 84 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 85 | log.info("rpc-request", rpc=rpc_name) |
| 86 | class_handler = self.rpc_class_handlers.get(rpc_name, None) |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 87 | if class_handler is not None: |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 88 | return class_handler(rpc_node, rpc_method, None, grpc_channel, |
| 89 | session) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 90 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 91 | log.error("rpc-not-implemented", rpc=rpc_name) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 92 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 93 | rpc_class_handlers = { |
| 94 | 'getvoltha': GetVoltha, |
| 95 | 'get-config': GetConfig, |
| 96 | 'get': Get, |
| 97 | 'edit-config': EditConfig, |
| 98 | 'copy-config': CopyConfig, |
| 99 | 'delete-config': DeleteConfig, |
| 100 | 'commit': Commit, |
| 101 | 'lock': Lock, |
| 102 | 'unlock': UnLock, |
| 103 | 'close-session': CloseSession, |
| 104 | 'kill-session': KillSession |
| 105 | } |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 106 | |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 107 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 108 | def get_rpc_factory_instance(): |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 109 | if RpcFactory.instance == None: |
| 110 | RpcFactory.instance = RpcFactory() |
| 111 | return RpcFactory.instance |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 112 | |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 113 | |
| 114 | if __name__ == '__main__': |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 115 | fac = get_rpc_factory_instance() |
| 116 | fac.register_rpc('urn:opencord:params:xml:ns:voltha:ietf-voltha', |
| 117 | 'VolthaGlobalService', 'GetVoltha', GetVoltha) |
| 118 | rpc = fac.get_handler('urn:opencord:params:xml:ns:voltha:ietf-voltha', |
| 119 | 'VolthaGlobalService', 'GetVoltha') |
| 120 | # rpc = fac.rpc_class_handlers.get('getvoltha', None) |
| 121 | print rpc(None, None, None, None) |