blob: f3ec0cc9905f78eb8fe3e7e3396259a3d55525e8 [file] [log] [blame]
Khen Nursimulua7b842a2016-12-03 23:28:42 -05001#!/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#
19import structlog
20from base.commit import Commit
21from base.copy_config import CopyConfig
22from base.delete_config import DeleteConfig
23from base.discard_changes import DiscardChanges
24from base.edit_config import EditConfig
25from base.get import Get
26from base.get_config import GetConfig
27from base.lock import Lock
28from base.unlock import UnLock
29from base.close_session import CloseSession
30from base.kill_session import KillSession
31from netconf import NSMAP, qmap
32import netconf.nc_common.error as ncerror
33log = structlog.get_logger()
34
35class 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
81def get_rpc_factory_instance():
82 if RpcFactory.instance == None:
83 RpcFactory.instance = RpcFactory()
84 return RpcFactory.instance
85