blob: 47c1eaa4da6b9d43d7a44c9576a5eeb1eb3e57e2 [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 Nursimulu8ffb8932017-01-26 13:40:49 -050033from ext.voltha_rpc import VolthaRpc
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 Nursimulu8ffb8932017-01-26 13:40:49 -050036from netconf.grpc_client.nc_rpc_mapper import get_nc_rpc_mapper_instance
Khen Nursimulu3b212942016-12-12 23:09:53 -050037from lxml import etree
38
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050039log = structlog.get_logger()
Khen Nursimulua7b842a2016-12-03 23:28:42 -050040
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050041
Khen Nursimulua7b842a2016-12-03 23:28:42 -050042class RpcFactory:
Khen Nursimulue0d53f82016-12-14 11:05:44 -080043 instance = None
Khen Nursimulua7b842a2016-12-03 23:28:42 -050044
Khen Nursimulue0d53f82016-12-14 11:05:44 -080045 def __init__(self):
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050046 pass
Khen Nursimulua7b842a2016-12-03 23:28:42 -050047
Khen Nursimulue0d53f82016-12-14 11:05:44 -080048 def _get_key(self, namespace, service, name):
49 return ''.join([namespace, service, name])
Khen Nursimulu3b212942016-12-12 23:09:53 -050050
Khen Nursimulua4972742016-12-23 17:15:20 -050051 def get_attribute_value(self, name, attributes):
52 for tup in attributes.items():
53 if tup[0] == name:
54 return tup[1]
55
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050056 def get_filtered_attributes(self, names_to_filter_out, attributes):
57 result = []
58 for tup in attributes.items():
59 if tup[0] not in names_to_filter_out:
60 result.append((tup[0], tup[1]))
61 return result
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050062
Khen Nursimulua4972742016-12-23 17:15:20 -050063 # Parse a request (node is an ElementTree) and return a dictionary
Khen Nursimulua4972742016-12-23 17:15:20 -050064 def parse_xml_request(self, node):
65 request = {}
66 if not len(node):
67 return request
68 for elem in node.iter():
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050069 if elem.tag.find(qmap(C.NC)) != -1: # found
70 elem_name = elem.tag.replace(qmap(C.NC), "")
Khen Nursimulua4972742016-12-23 17:15:20 -050071 if elem_name == 'rpc':
72 request['type'] = 'rpc'
73 request['message_id'] = self.get_attribute_value(
74 'message-id', elem.attrib)
75 elif elem_name == 'filter':
76 request['filter'] = self.get_attribute_value('type',
77 elem.attrib)
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050078 # Get the metadata
79 request['metadata'] = self.get_filtered_attributes(
80 ['type'],
81 elem.attrib)
Khen Nursimulua4972742016-12-23 17:15:20 -050082 else:
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050083 request[
84 'command'] = elem_name # attribute is empty for now
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050085 elif elem.tag.find(qmap(C.VOLTHA)) != -1: # found
86 request['namespace'] = ns(C.VOLTHA)
Khen Nursimulua4972742016-12-23 17:15:20 -050087 if request.has_key('class'):
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050088 request['subclass'] = elem.tag.replace(qmap(C.VOLTHA), "")
Khen Nursimulua4972742016-12-23 17:15:20 -050089 else:
Khen Nursimulu5b7b3fb2017-01-13 16:00:00 -050090 elem_name = elem.tag.replace(qmap(C.VOLTHA), "")
91 request['class'] = elem_name
92 if not request.has_key('command'):
93 request['command'] = elem_name
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050094 request['metadata'] = self.get_filtered_attributes(
95 ['xmlns'],
96 elem.attrib)
97 elif elem.tag.find(qmap(C.HEALTH)) != -1: # found
98 request['namespace'] = ns(C.HEALTH)
99 if request.has_key('class'):
100 request['subclass'] = elem.tag.replace(qmap(C.HEALTH), "")
101 else:
102 elem_name = elem.tag.replace(qmap(C.HEALTH), "")
103 request['class'] = elem_name
104 if not request.has_key('command'):
105 request['command'] = elem_name
106 request['metadata'] = self.get_filtered_attributes(
107 ['xmlns'],
108 elem.attrib)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500109 elif elem.tag.find(qmap(C.NCM)) != -1: # found
110 request['namespace'] = ns(C.NCM)
111 elem_name = elem.tag.replace(qmap(C.NCM), "")
112 if elem_name == 'get-schema':
113 request['command'] = elem_name
114 request['class'] = elem_name
115 elif request.has_key('class'):
116 request['subclass'] = elem_name
117 elif elem_name == 'netconf-state':
118 request['command'] = 'get-schemas'
119 request['class'] = elem_name
120
Khen Nursimulua4972742016-12-23 17:15:20 -0500121 return request
122
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500123 def get_rpc_handler(self, rpc_node, msg, grpc_channel, session,
124 capabilities):
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800125 try:
Khen Nursimulua4972742016-12-23 17:15:20 -0500126 # Parse the request into a dictionary
127 log.info("rpc-node",
128 node=etree.tostring(rpc_node, pretty_print=True))
Khen Nursimulu3b212942016-12-12 23:09:53 -0500129
Khen Nursimulua4972742016-12-23 17:15:20 -0500130 request = self.parse_xml_request(rpc_node)
131 if not request:
132 log.error("request-bad-format")
133 raise ncerror.BadMsg(rpc_node)
Khen Nursimulu3b212942016-12-12 23:09:53 -0500134
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500135 log.info("parsed-request", request=request)
136
137 if not request.has_key('message_id'):
Khen Nursimulua4972742016-12-23 17:15:20 -0500138 log.error("request-no-message-id")
139 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500140
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500141 class_handler = self._get_rpc_handler(request['command'])
142
Khen Nursimulua4972742016-12-23 17:15:20 -0500143 if class_handler is not None:
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500144 return class_handler(request, rpc_node, grpc_channel, session,
145 capabilities)
Khen Nursimulua4972742016-12-23 17:15:20 -0500146
147 log.error("rpc-not-implemented", rpc=request['command'])
148
Khen Nursimulu5b7b3fb2017-01-13 16:00:00 -0500149
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500150 except ncerror.BadMsg as err:
151 log.info('ncerror.BadMsg')
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800152 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500153
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500154 except Exception as e:
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500155 log.exception('exception', e=e)
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500156 raise ncerror.ServerException(rpc_node, exception=e)
157
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500158 def _get_rpc_handler(self, command):
159 # If there is a generic mapping of that command then use it
160 rpc_mapper = get_nc_rpc_mapper_instance()
161 rpc = command.replace('-', '_')
162 if rpc_mapper.is_rpc_exist(rpc):
163 return VolthaRpc
164 else:
165 return self.rpc_class_handlers.get(command, None)
166
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800167 rpc_class_handlers = {
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800168 'get-config': GetConfig,
169 'get': Get,
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -0500170 'get-schemas': GetSchemas,
171 'get-schema': GetSchema,
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800172 'edit-config': EditConfig,
173 'copy-config': CopyConfig,
174 'delete-config': DeleteConfig,
175 'commit': Commit,
176 'lock': Lock,
177 'unlock': UnLock,
178 'close-session': CloseSession,
179 'kill-session': KillSession
180 }
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500181
Khen Nursimulu3b212942016-12-12 23:09:53 -0500182
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500183def get_rpc_factory_instance():
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800184 if RpcFactory.instance == None:
185 RpcFactory.instance = RpcFactory()
186 return RpcFactory.instance