blob: 2d60d7744c205d032ae09ec3b1947727100d6a9e [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#
Khen Nursimulua4972742016-12-23 17:15:20 -050019import structlog
20from lxml import etree
21import netconf.nc_common.error as ncerror
22
23log = structlog.get_logger()
24
Khen Nursimulua7b842a2016-12-03 23:28:42 -050025
26class RpcResponse():
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050027 def __init__(self, capabilities):
Khen Nursimulua7b842a2016-12-03 23:28:42 -050028 self.is_error = False
29 # if there is an error then the reply_node will contains an Error
30 # object
31 self.reply_node = None
Khen Nursimulua4972742016-12-23 17:15:20 -050032 self.close_session = False
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050033 self.capabilities = capabilities
34 self.custom_rpc = False
Khen Nursimulua4972742016-12-23 17:15:20 -050035
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050036 def build_xml_response(self, request, voltha_response, custom_rpc=False):
Khen Nursimulua4972742016-12-23 17:15:20 -050037 if request is None:
38 return
39 voltha_xml_string = etree.tostring(voltha_response)
40
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050041 # Remove the leading and trailing <yang> tags
Khen Nursimulua4972742016-12-23 17:15:20 -050042 if voltha_xml_string.startswith('<yang>'):
43 voltha_xml_string = voltha_xml_string[len('<yang>'):]
44 if voltha_xml_string.endswith('</yang>'):
45 voltha_xml_string = voltha_xml_string[:-len('</yang>')]
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050046 # Empty response
47 elif voltha_xml_string.startswith('<yang/>'):
Khen Nursimulub4e71472017-01-06 18:05:47 -050048 voltha_xml_string = ''
Khen Nursimulua4972742016-12-23 17:15:20 -050049
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050050 if not custom_rpc:
51 # Create the xml body as
52 if request.has_key('subclass'):
53 body = ''.join([
54 '<data>',
55 '<',
56 request['class'],
57 ' xmlns="',
58 request['namespace'],
59 '">',
60 '<',
61 request['subclass'],
62 '>',
63 voltha_xml_string,
64 '</',
65 request['subclass'],
66 '>',
67 '</',
68 request['class'],
69 '>',
70 '</data>'
71 ])
72 else:
73 body = ''.join([
74 '<data>',
75 '<',
76 request['class'],
77 ' xmlns="urn:opencord:params:xml:ns:voltha:ietf-voltha">',
78 voltha_xml_string,
79 '</',
80 request['class'],
81 '>',
82 '</data>'
83 ])
84 else: # custom_rpc
Khen Nursimulua4972742016-12-23 17:15:20 -050085 body = ''.join([
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050086 '<rpc-reply',
Khen Nursimulua4972742016-12-23 17:15:20 -050087 ' xmlns="urn:opencord:params:xml:ns:voltha:ietf-voltha">',
88 voltha_xml_string,
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050089 '</rpc-reply>',
Khen Nursimulua4972742016-12-23 17:15:20 -050090 ])
91
92 return etree.fromstring(body)
93
94 def add_node(self, new_node, tree):
95 if new_node.tag == 'ignore':
96 # We want only sub-elements
97 for elem in list(new_node):
98 tree.append(elem)
99 else:
100 tree.append(new_node)
101
102 def copy_basic_element(self, elm):
103 new_elem = etree.Element(elm.tag)
104 new_elem.text = elm.text
105 return new_elem
106
107 def process_inline_option(self, elem):
108 inline_option = False
109 inline_node_name = None
110 for elm in list(elem):
111 if elm.tag == 'yang_field_option':
112 inline_option = True
113 if elm.tag == 'name':
114 inline_node_name = elm.text
115 if not inline_option:
116 new_elem = etree.Element(elem.tag)
117 return new_elem, elem
118
119 # look for the node with the inline_node_name
120 for elm in list(elem):
121 if elm.tag == inline_node_name:
122 new_elem = etree.Element('ignore')
123 return new_elem, elm
124
125 def process_element(self, elem):
126 attrib = elem.get('type')
127 if (attrib == 'list'):
128 if list(elem) is None:
129 return self.copy_basic_element(elem)
Khen Nursimulub4e71472017-01-06 18:05:47 -0500130 if elem.tag == 'items':
131 new_elem = etree.Element('items')
132 else:
133 new_elem = etree.Element('ignore')
Khen Nursimulua4972742016-12-23 17:15:20 -0500134 for elm in list(elem):
135 elm.tag = elem.tag
136 if elm.get('type') in ['list', 'dict']:
137 self.add_node(self.process_element(elm), new_elem)
138 else:
139 new_elem.append(self.copy_basic_element(elm))
140 return new_elem
141 elif (attrib == 'dict'):
142 # Empty case
143 if list(elem) is None:
144 return self.copy_basic_element(elem)
145
146 # Process field option.
147 new_elem, elem = self.process_inline_option(elem)
148
149 for elm in list(elem):
150 if elm.get('type') in ['list', 'dict']:
151 self.add_node(self.process_element(elm), new_elem)
152 else:
153 new_elem.append(self.copy_basic_element(elm))
154 return new_elem
155 else:
156 return self.copy_basic_element(elem)
157
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500158 def to_yang_xml(self, from_xml, request, custom_rpc=False):
Khen Nursimulua4972742016-12-23 17:15:20 -0500159 # Parse from_xml as follows:
160 # 1. Any element having a list attribute shoud have each item move 1 level
161 # up and retag using the parent tag
162 # 2. Any element having a dict attribute and has a <yang_field_option>
163 # sub-element should have all it's items move to teh parent level
164 top = etree.Element('yang')
165 elms = list(from_xml)
166
167 # special case the xml contain a list type
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500168 if len(elms) == 1 and not custom_rpc:
Khen Nursimulua4972742016-12-23 17:15:20 -0500169 item = elms[0]
Khen Nursimulub4e71472017-01-06 18:05:47 -0500170 # TODO: Address name 'items' clash when a list name is actually
171 # 'items'.
Khen Nursimulua4972742016-12-23 17:15:20 -0500172 if item.get('type') == 'list':
Khen Nursimulub4e71472017-01-06 18:05:47 -0500173 if request.has_key('subclass'):
174 item.tag = request['subclass']
175 # remove the subclass element in request to avoid duplicate tag
176 del request['subclass']
177 else:
178 item.tag = 'ignore'
Khen Nursimulua4972742016-12-23 17:15:20 -0500179 self.add_node(self.process_element(item), top)
180 return top
181
182 # Process normally for all other cases
183 for elm in elms:
184 self.add_node(self.process_element(elm), top)
185
186 return top
187
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500188 # custom_rpc refers to custom RPCs different from Netconf default RPCs
189 # like get, get-config, edit-config, etc
190 def build_yang_response(self, root, request, custom_rpc=False):
Khen Nursimulua4972742016-12-23 17:15:20 -0500191 try:
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500192 self.custom_rpc = custom_rpc
193 yang_xml = self.to_yang_xml(root, request, custom_rpc)
Khen Nursimulua4972742016-12-23 17:15:20 -0500194 log.info('yang-xml', yang_xml=etree.tounicode(yang_xml,
195 pretty_print=True))
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500196 return self.build_xml_response(request, yang_xml, custom_rpc)
Khen Nursimulua4972742016-12-23 17:15:20 -0500197 except Exception as e:
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500198 log.exception('error-building-yang-response', request=request,
199 xml=etree.tostring(root))
Khen Nursimulua4972742016-12-23 17:15:20 -0500200 self.rpc_response.is_error = True
201 self.rpc_response.node = ncerror.BadMsg(request)
202 return