blob: 72b4734be6da1a69c48c146a034f7a2f60c7823b [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()
Khen Nursimulu3b212942016-12-12 23:09:53 -050035from lxml import etree
36
Khen Nursimulua7b842a2016-12-03 23:28:42 -050037
38class RpcFactory:
39
40 instance = None
41
Khen Nursimulu3b212942016-12-12 23:09:53 -050042 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
49 def _get_key(self, namespace, service, name):
50 return ''.join([namespace,service,name])
51
52 def register_rpc(self, namespace, service, name, klass):
53 key = self._get_key(namespace, service, name)
54 if key not in self.rpc_map.keys():
55 self.rpc_map[key] = klass
56
57 def get_handler(self, namespace, service, name):
58 key = self._get_key(namespace, service, name)
59 if key in self.rpc_map.keys():
60 return self.rpc_map[key]
61
62
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050063 def get_rpc_handler(self, rpc_node, msg, grpc_channel, session):
Khen Nursimulua7b842a2016-12-03 23:28:42 -050064 try:
65 msg_id = rpc_node.get('message-id')
66 log.info("Received-rpc-message-id", msg_id=msg_id)
67
68 except (TypeError, ValueError):
69 raise ncerror.SessionError(msg,
70 "No valid message-id attribute found")
71
Khen Nursimulu3b212942016-12-12 23:09:53 -050072 log.info("rpc-node", node=etree.tostring(rpc_node, pretty_print=True))
73
Khen Nursimulua7b842a2016-12-03 23:28:42 -050074 # Get the first child of rpc as the method name
75 rpc_method = rpc_node.getchildren()
76 if len(rpc_method) != 1:
77 log.error("badly-formatted-rpc-method", msg_id=msg_id)
78 raise ncerror.BadMsg(rpc_node)
79
80 rpc_method = rpc_method[0]
81
Khen Nursimulu3b212942016-12-12 23:09:53 -050082 if rpc_method.prefix is None:
83 log.error("rpc-method-has-no-prefix", msg_id=msg_id)
84 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050085
Khen Nursimulu3b212942016-12-12 23:09:53 -050086 try:
87 # extract the namespace, service and name
88 namespace = ''.join(['{', rpc_method.nsmap[rpc_method.prefix], '}'])
89 # rpc_name = rpc_method.tag.replace(qmap('nc'), "")
90 rpc = rpc_method.tag.replace(namespace, "").split('-')
91 rpc_service = rpc[0]
92 rpc_name = rpc[1]
93 log.info("rpc-request",
94 namespace=namespace,
95 service=rpc_service,
96 name=rpc_name)
97 except Exception as e:
98 log.error("rpc-parsing-error", exception=repr(e))
99 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500100
Khen Nursimulu3b212942016-12-12 23:09:53 -0500101 class_handler = self.get_handler(namespace, rpc_service, rpc_name)
102
103 # class_handler = self.rpc_class_handlers.get(rpc_name, None)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500104 if class_handler is not None:
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -0500105 return class_handler(rpc_node, rpc_method, grpc_channel, session)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500106
107 log.error("rpc-not-implemented", rpc=rpc_name)
108
109
110 rpc_class_handlers = {
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -0500111 'getvoltha' : GetVoltha,
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500112 'get-config': GetConfig,
113 'get': Get,
114 'edit-config': EditConfig,
115 'copy-config': CopyConfig,
116 'delete-config': DeleteConfig,
117 'commit': Commit,
118 'lock': Lock,
119 'unlock': UnLock,
120 'close-session': CloseSession,
121 'kill-session': KillSession
122 }
123
124
Khen Nursimulu3b212942016-12-12 23:09:53 -0500125
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500126def get_rpc_factory_instance():
127 if RpcFactory.instance == None:
128 RpcFactory.instance = RpcFactory()
129 return RpcFactory.instance
130
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -0500131
132if __name__ == '__main__':
133 fac = get_rpc_factory_instance()
Khen Nursimulu3b212942016-12-12 23:09:53 -0500134 fac.register_rpc('urn:opencord:params:xml:ns:voltha:ietf-voltha',
135 'VolthaGlobalService', 'GetVoltha', GetVoltha)
136 rpc = fac.get_handler('urn:opencord:params:xml:ns:voltha:ietf-voltha',
137 'VolthaGlobalService', 'GetVoltha')
138 # rpc = fac.rpc_class_handlers.get('getvoltha', None)
139 print rpc(None,None,None, None)