blob: eb13b8d82c5952cdfccabc98f8c777ec1f527e0b [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
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050031from ext.get_voltha import GetVoltha
Khen Nursimulua7b842a2016-12-03 23:28:42 -050032from netconf import NSMAP, qmap
33import netconf.nc_common.error as ncerror
34log = structlog.get_logger()
35
36class RpcFactory:
37
38 instance = None
39
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050040 def get_rpc_handler(self, rpc_node, msg, grpc_channel, session):
Khen Nursimulua7b842a2016-12-03 23:28:42 -050041 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 Nursimuluaaac7ee2016-12-11 22:03:52 -050063 return class_handler(rpc_node, rpc_method, grpc_channel, session)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050064
65 log.error("rpc-not-implemented", rpc=rpc_name)
66
67
68 rpc_class_handlers = {
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050069 'getvoltha' : GetVoltha,
Khen Nursimulua7b842a2016-12-03 23:28:42 -050070 '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
83def get_rpc_factory_instance():
84 if RpcFactory.instance == None:
85 RpcFactory.instance = RpcFactory()
86 return RpcFactory.instance
87
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050088
89if __name__ == '__main__':
90 fac = get_rpc_factory_instance()
91 rpc = fac.rpc_class_handlers.get('getvoltha', None)
92 print rpc(None,None,None)