blob: f314dbee1e94287f1613f5aac4bd9afae3accdcb [file] [log] [blame]
Khen Nursimulua7b842a2016-12-03 23:28:42 -05001#!/usr/bin/env python
2#
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -05003# Copyright 2017 the original author or authors.
Khen Nursimulua7b842a2016-12-03 23:28:42 -05004#
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
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050020from netconf.constants import Constants as C
Khen Nursimulua7b842a2016-12-03 23:28:42 -050021from base.commit import Commit
22from base.copy_config import CopyConfig
23from base.delete_config import DeleteConfig
Khen Nursimulua7b842a2016-12-03 23:28:42 -050024from 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 Nursimuluc9ef7c12017-01-04 20:40:53 -050031from ext.get_schemas import GetSchemas
32from ext.get_schema import GetSchema
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050033from ext.get_voltha import GetVoltha
Khen Nursimulua7b842a2016-12-03 23:28:42 -050034import netconf.nc_common.error as ncerror
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050035from netconf.nc_common.utils import qmap, ns
Khen Nursimulu3b212942016-12-12 23:09:53 -050036from lxml import etree
37
Khen Nursimulua4972742016-12-23 17:15:20 -050038
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050039log = structlog.get_logger()
Khen Nursimulua7b842a2016-12-03 23:28:42 -050040
41class RpcFactory:
Khen Nursimulue0d53f82016-12-14 11:05:44 -080042 instance = None
Khen Nursimulua7b842a2016-12-03 23:28:42 -050043
Khen Nursimulue0d53f82016-12-14 11:05:44 -080044 def __init__(self):
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050045 pass
Khen Nursimulua7b842a2016-12-03 23:28:42 -050046
Khen Nursimulue0d53f82016-12-14 11:05:44 -080047 def _get_key(self, namespace, service, name):
48 return ''.join([namespace, service, name])
Khen Nursimulu3b212942016-12-12 23:09:53 -050049
Khen Nursimulua4972742016-12-23 17:15:20 -050050 def get_attribute_value(self, name, attributes):
51 for tup in attributes.items():
52 if tup[0] == name:
53 return tup[1]
54
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050055
Khen Nursimulua4972742016-12-23 17:15:20 -050056 # Parse a request (node is an ElementTree) and return a dictionary
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050057 # TODO: This parser is specific for a GET/GET SCHEMAS request. Need to be
58 # it more generic
Khen Nursimulua4972742016-12-23 17:15:20 -050059 def parse_xml_request(self, node):
60 request = {}
61 if not len(node):
62 return request
63 for elem in node.iter():
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050064 if elem.tag.find(qmap(C.NC)) != -1: # found
65 elem_name = elem.tag.replace(qmap(C.NC), "")
Khen Nursimulua4972742016-12-23 17:15:20 -050066 if elem_name == 'rpc':
67 request['type'] = 'rpc'
68 request['message_id'] = self.get_attribute_value(
69 'message-id', elem.attrib)
70 elif elem_name == 'filter':
71 request['filter'] = self.get_attribute_value('type',
72 elem.attrib)
73 else:
74 request[
75 'command'] = elem_name # attribute is empty for now
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050076 elif elem.tag.find(qmap(C.VOLTHA)) != -1: # found
77 request['namespace'] = ns(C.VOLTHA)
Khen Nursimulua4972742016-12-23 17:15:20 -050078 if request.has_key('class'):
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050079 request['subclass'] = elem.tag.replace(qmap(C.VOLTHA),"")
Khen Nursimulua4972742016-12-23 17:15:20 -050080 else:
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050081 request['class'] = elem.tag.replace(qmap(C.VOLTHA), "")
82 elif elem.tag.find(qmap(C.NCM)) != -1: # found
83 request['namespace'] = ns(C.NCM)
84 elem_name = elem.tag.replace(qmap(C.NCM), "")
85 if elem_name == 'get-schema':
86 request['command'] = elem_name
87 request['class'] = elem_name
88 elif request.has_key('class'):
89 request['subclass'] = elem_name
90 elif elem_name == 'netconf-state':
91 request['command'] = 'get-schemas'
92 request['class'] = elem_name
93
Khen Nursimulua4972742016-12-23 17:15:20 -050094 return request
95
Khen Nursimulu3b212942016-12-12 23:09:53 -050096
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050097 def get_rpc_handler(self, rpc_node, msg, grpc_channel, session,
98 capabilities):
Khen Nursimulue0d53f82016-12-14 11:05:44 -080099 try:
Khen Nursimulua4972742016-12-23 17:15:20 -0500100 # Parse the request into a dictionary
101 log.info("rpc-node",
102 node=etree.tostring(rpc_node, pretty_print=True))
Khen Nursimulu3b212942016-12-12 23:09:53 -0500103
Khen Nursimulua4972742016-12-23 17:15:20 -0500104 request = self.parse_xml_request(rpc_node)
105 if not request:
106 log.error("request-bad-format")
107 raise ncerror.BadMsg(rpc_node)
Khen Nursimulu3b212942016-12-12 23:09:53 -0500108
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500109 log.info("parsed-request", request=request)
110
111 if not request.has_key('message_id'):
Khen Nursimulua4972742016-12-23 17:15:20 -0500112 log.error("request-no-message-id")
113 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500114
Khen Nursimulua4972742016-12-23 17:15:20 -0500115 class_handler = self.rpc_class_handlers.get(request['command'],
116 None)
117 if class_handler is not None:
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500118 return class_handler(request, rpc_node, grpc_channel, session,
119 capabilities)
Khen Nursimulua4972742016-12-23 17:15:20 -0500120
121 log.error("rpc-not-implemented", rpc=request['command'])
122
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500123 except ncerror.BadMsg as err:
124 log.info('ncerror.BadMsg')
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800125 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500126
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500127 except Exception as e:
128 raise ncerror.ServerException(rpc_node, exception=e)
129
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800130 rpc_class_handlers = {
131 'getvoltha': GetVoltha,
132 'get-config': GetConfig,
133 'get': Get,
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500134 'get-schemas': GetSchemas,
135 'get-schema': GetSchema,
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800136 'edit-config': EditConfig,
137 'copy-config': CopyConfig,
138 'delete-config': DeleteConfig,
139 'commit': Commit,
140 'lock': Lock,
141 'unlock': UnLock,
142 'close-session': CloseSession,
143 'kill-session': KillSession
144 }
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500145
Khen Nursimulu3b212942016-12-12 23:09:53 -0500146
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500147def get_rpc_factory_instance():
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800148 if RpcFactory.instance == None:
149 RpcFactory.instance = RpcFactory()
150 return RpcFactory.instance