blob: 9d4bf33ef1ebab3f6d799cdf3375e73b6befbc11 [file] [log] [blame]
Khen Nursimulu8ffb8932017-01-26 13:40:49 -05001#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17import structlog
18from netconf.nc_rpc.rpc import Rpc
19import netconf.nc_common.error as ncerror
20from twisted.internet.defer import inlineCallbacks, returnValue
21import dicttoxml
22from netconf.nc_common.utils import qmap, ns
23from netconf.constants import Constants as C
24from netconf.grpc_client.nc_rpc_mapper import \
25 get_nc_rpc_mapper_instance
26
27log = structlog.get_logger()
28
29
30class VolthaRpc(Rpc):
31 def __init__(self, request, request_xml, grpc_client, session,
32 capabilities):
33 super(VolthaRpc, self).__init__(request, request_xml, grpc_client,
34 session, capabilities)
35 self.service = None
36 self.method = None
37 self.metadata = None
38 self._extract_parameters()
39 if not self.rpc_response.is_error:
40 self._validate_parameters()
41
42 @inlineCallbacks
43 def execute(self):
44 if self.rpc_response.is_error:
45 returnValue(self.rpc_response)
46
47 log.info('voltha-rpc-request', session=self.session.session_id,
48 request=self.request)
49
50 # Execute the request
Khen Nursimulu3676b7c2017-01-31 13:48:38 -050051 res_dict, yang_options = yield self.grpc_client.invoke_voltha_rpc(
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050052 service=self.service,
53 method=self.method,
54 params=self.request['params'],
55 metadata=self.metadata)
56
57 # convert dict to xml
58 xml = dicttoxml.dicttoxml(res_dict, attr_type=True)
59 log.info('voltha-info', res=res_dict, xml=xml)
60
61 root_elem = self.get_root_element(xml)
62
63 # Build the yang response
64 self.rpc_response.node = self.rpc_response.build_yang_response(
Khen Nursimulu3676b7c2017-01-31 13:48:38 -050065 root_elem, self.request, yang_options=yang_options, custom_rpc=True)
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050066 self.rpc_response.is_error = False
67
68 returnValue(self.rpc_response)
69
70 def _validate_parameters(self):
71 log.info('validate-parameters', session=self.session.session_id)
72 # For now just validate that the command is presenf
73 if self.request:
74 try:
75 if self.request['command'] is None:
76 self.rpc_response.is_error = True
77 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
78 return
79
80 except Exception as e:
81 self.rpc_response.is_error = True
82 self.rpc_response.node = ncerror.ServerException(
83 self.request_xml)
84 return
85
Khen Nursimulub21bd642017-02-20 21:32:27 -050086 # Helper function to parse a complex xml into a dict
87 def _parse_params(self, node):
88 params = {}
89 if node is not None:
90 for r in node:
91 children = r.getchildren()
92 if children:
93 params[ r.tag.replace(qmap(C.VOLTHA), "")] = \
94 self._parse_params(children)
95 else:
96 params[
97 r.tag.replace(qmap(C.VOLTHA), "")] = r.text
98 return params
99
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500100 def _extract_parameters(self):
101 try:
102 rpc_node = self.request_xml.find(''.join(
103 [qmap(C.VOLTHA),
104 self.request['command']])
105 )
Khen Nursimulub21bd642017-02-20 21:32:27 -0500106
107 # Parse rpc the parameters
108 self.request['params'] = self._parse_params(rpc_node)
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500109
110 # Remove the subclass element in the request if it is present as
111 # it is not required for rpc calls
112 if self.request.has_key('subclass'):
113 self.request.pop('subclass', None)
114
115 # Extract the service and method from the rpc command
116 command = self.request['command'].split('-')
117 if len(command) != 2:
118 log.debug('invalid-format', command=self.request['command'])
119 raise
120
121 self.service = command[0]
122 self.method = command[1]
123 if self.request.has_key('metadata'):
124 self.metadata = self.request['metadata']
125
126 except Exception as e:
127 self.rpc_response.is_error = True
128 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
129 log.exception('params-parsing-error', xml=self.request_xml, e=e)