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 |
| 34 | log = structlog.get_logger() |
| 35 | |
| 36 | class RpcFactory: |
| 37 | |
| 38 | instance = None |
| 39 | |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 40 | def get_rpc_handler(self, rpc_node, msg, grpc_channel, session): |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 41 | try: |
| 42 | msg_id = rpc_node.get('message-id') |
| 43 | log.info("Received-rpc-message-id", msg_id=msg_id) |
| 44 | |
| 45 | except (TypeError, ValueError): |
| 46 | raise ncerror.SessionError(msg, |
| 47 | "No valid message-id attribute found") |
| 48 | |
| 49 | # Get the first child of rpc as the method name |
| 50 | rpc_method = rpc_node.getchildren() |
| 51 | if len(rpc_method) != 1: |
| 52 | log.error("badly-formatted-rpc-method", msg_id=msg_id) |
| 53 | raise ncerror.BadMsg(rpc_node) |
| 54 | |
| 55 | rpc_method = rpc_method[0] |
| 56 | |
| 57 | rpc_name = rpc_method.tag.replace(qmap('nc'), "") |
| 58 | |
| 59 | log.info("rpc-request", rpc=rpc_name) |
| 60 | |
| 61 | class_handler = self.rpc_class_handlers.get(rpc_name, None) |
| 62 | if class_handler is not None: |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 63 | return class_handler(rpc_node, rpc_method, grpc_channel, session) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 64 | |
| 65 | log.error("rpc-not-implemented", rpc=rpc_name) |
| 66 | |
| 67 | |
| 68 | rpc_class_handlers = { |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 69 | 'getvoltha' : GetVoltha, |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 70 | 'get-config': GetConfig, |
| 71 | 'get': Get, |
| 72 | 'edit-config': EditConfig, |
| 73 | 'copy-config': CopyConfig, |
| 74 | 'delete-config': DeleteConfig, |
| 75 | 'commit': Commit, |
| 76 | 'lock': Lock, |
| 77 | 'unlock': UnLock, |
| 78 | 'close-session': CloseSession, |
| 79 | 'kill-session': KillSession |
| 80 | } |
| 81 | |
| 82 | |
| 83 | def get_rpc_factory_instance(): |
| 84 | if RpcFactory.instance == None: |
| 85 | RpcFactory.instance = RpcFactory() |
| 86 | return RpcFactory.instance |
| 87 | |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 88 | |
| 89 | if __name__ == '__main__': |
| 90 | fac = get_rpc_factory_instance() |
| 91 | rpc = fac.rpc_class_handlers.get('getvoltha', None) |
| 92 | print rpc(None,None,None) |