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