blob: aa0ced1601365ba363153770058e088bbccb9207 [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
Khen Nursimulue0d53f82016-12-14 11:05:44 -080034
Khen Nursimulua7b842a2016-12-03 23:28:42 -050035log = structlog.get_logger()
Khen Nursimulu3b212942016-12-12 23:09:53 -050036from lxml import etree
37
Khen Nursimulua7b842a2016-12-03 23:28:42 -050038
39class RpcFactory:
Khen Nursimulue0d53f82016-12-14 11:05:44 -080040 instance = None
Khen Nursimulua7b842a2016-12-03 23:28:42 -050041
Khen Nursimulue0d53f82016-12-14 11:05:44 -080042 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 self.register_rpc('{urn:opencord:params:xml:ns:voltha:ietf-voltha}',
49 'any', 'any', GetVoltha)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050050
Khen Nursimulue0d53f82016-12-14 11:05:44 -080051 def _get_key(self, namespace, service, name):
52 return ''.join([namespace, service, name])
Khen Nursimulu3b212942016-12-12 23:09:53 -050053
Khen Nursimulue0d53f82016-12-14 11:05:44 -080054 def register_rpc(self, namespace, service, name, klass):
55 key = self._get_key(namespace, service, name)
56 if key not in self.rpc_map.keys():
57 self.rpc_map[key] = klass
Khen Nursimulu3b212942016-12-12 23:09:53 -050058
Khen Nursimulue0d53f82016-12-14 11:05:44 -080059 def get_handler(self, namespace, service, name):
60 key = self._get_key(namespace, service, name)
61 if key in self.rpc_map.keys():
62 return self.rpc_map[key]
Khen Nursimulu3b212942016-12-12 23:09:53 -050063
Khen Nursimulue0d53f82016-12-14 11:05:44 -080064 def get_rpc_handler(self, rpc_node, msg, grpc_channel, session):
65 try:
66 msg_id = rpc_node.get('message-id')
67 log.info("Received-rpc-message-id", msg_id=msg_id)
Khen Nursimulu3b212942016-12-12 23:09:53 -050068
Khen Nursimulue0d53f82016-12-14 11:05:44 -080069 except (TypeError, ValueError):
70 raise ncerror.SessionError(msg,
71 "No valid message-id attribute found")
Khen Nursimulu3b212942016-12-12 23:09:53 -050072
Khen Nursimulue0d53f82016-12-14 11:05:44 -080073 log.info("rpc-node", node=etree.tostring(rpc_node, pretty_print=True))
Khen Nursimulua7b842a2016-12-03 23:28:42 -050074
Khen Nursimulue0d53f82016-12-14 11:05:44 -080075 # Get the first child of rpc as the method name
76 rpc_method = rpc_node.getchildren()
77 if len(rpc_method) != 1:
78 log.error("badly-formatted-rpc-method", msg_id=msg_id)
79 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050080
Khen Nursimulue0d53f82016-12-14 11:05:44 -080081 rpc_method = rpc_method[0]
Khen Nursimulu3b212942016-12-12 23:09:53 -050082
Khen Nursimulu7626ce12016-12-21 11:51:46 -050083 rpc_name = rpc_method.tag.replace(qmap('nc'), "")
Khen Nursimulua7b842a2016-12-03 23:28:42 -050084
Khen Nursimulu7626ce12016-12-21 11:51:46 -050085 log.info("rpc-request", rpc=rpc_name)
86 class_handler = self.rpc_class_handlers.get(rpc_name, None)
Khen Nursimulue0d53f82016-12-14 11:05:44 -080087 if class_handler is not None:
Khen Nursimulu7626ce12016-12-21 11:51:46 -050088 return class_handler(rpc_node, rpc_method, None, grpc_channel,
89 session)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050090
Khen Nursimulue0d53f82016-12-14 11:05:44 -080091 log.error("rpc-not-implemented", rpc=rpc_name)
Khen Nursimulu3b212942016-12-12 23:09:53 -050092
Khen Nursimulue0d53f82016-12-14 11:05:44 -080093 rpc_class_handlers = {
94 'getvoltha': GetVoltha,
95 'get-config': GetConfig,
96 'get': Get,
97 'edit-config': EditConfig,
98 'copy-config': CopyConfig,
99 'delete-config': DeleteConfig,
100 'commit': Commit,
101 'lock': Lock,
102 'unlock': UnLock,
103 'close-session': CloseSession,
104 'kill-session': KillSession
105 }
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500106
Khen Nursimulu3b212942016-12-12 23:09:53 -0500107
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500108def get_rpc_factory_instance():
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800109 if RpcFactory.instance == None:
110 RpcFactory.instance = RpcFactory()
111 return RpcFactory.instance
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500112
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -0500113
114if __name__ == '__main__':
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800115 fac = get_rpc_factory_instance()
116 fac.register_rpc('urn:opencord:params:xml:ns:voltha:ietf-voltha',
117 'VolthaGlobalService', 'GetVoltha', GetVoltha)
118 rpc = fac.get_handler('urn:opencord:params:xml:ns:voltha:ietf-voltha',
119 'VolthaGlobalService', 'GetVoltha')
120 # rpc = fac.rpc_class_handlers.get('getvoltha', None)
121 print rpc(None, None, None, None)