blob: 150631bca3e833eb6c441da1272a4042253a21bd [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():
27 def __init__(self):
28 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
33
34 def build_xml_response(self, request, voltha_response):
35 if request is None:
36 return
37 voltha_xml_string = etree.tostring(voltha_response)
38
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050039 # Remove the leading and trailing <yang> tags
Khen Nursimulua4972742016-12-23 17:15:20 -050040 if voltha_xml_string.startswith('<yang>'):
41 voltha_xml_string = voltha_xml_string[len('<yang>'):]
42 if voltha_xml_string.endswith('</yang>'):
43 voltha_xml_string = voltha_xml_string[:-len('</yang>')]
44
45 # Create the xml body as
46 if request.has_key('subclass'):
47 body = ''.join([
48 '<data>',
49 '<',
50 request['class'],
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050051 ' xmlns="',
52 request['namespace'],
53 '">',
Khen Nursimulua4972742016-12-23 17:15:20 -050054 '<',
55 request['subclass'],
56 '>',
57 voltha_xml_string,
58 '</',
59 request['subclass'],
60 '>',
61 '</',
62 request['class'],
63 '>',
64 '</data>'
65 ])
66 else:
67 body = ''.join([
68 '<data>',
69 '<',
70 request['class'],
71 ' xmlns="urn:opencord:params:xml:ns:voltha:ietf-voltha">',
72 voltha_xml_string,
73 '</',
74 request['class'],
75 '>',
76 '</data>'
77 ])
78
79 return etree.fromstring(body)
80
81 def add_node(self, new_node, tree):
82 if new_node.tag == 'ignore':
83 # We want only sub-elements
84 for elem in list(new_node):
85 tree.append(elem)
86 else:
87 tree.append(new_node)
88
89 def copy_basic_element(self, elm):
90 new_elem = etree.Element(elm.tag)
91 new_elem.text = elm.text
92 return new_elem
93
94 def process_inline_option(self, elem):
95 inline_option = False
96 inline_node_name = None
97 for elm in list(elem):
98 if elm.tag == 'yang_field_option':
99 inline_option = True
100 if elm.tag == 'name':
101 inline_node_name = elm.text
102 if not inline_option:
103 new_elem = etree.Element(elem.tag)
104 return new_elem, elem
105
106 # look for the node with the inline_node_name
107 for elm in list(elem):
108 if elm.tag == inline_node_name:
109 new_elem = etree.Element('ignore')
110 return new_elem, elm
111
112 def process_element(self, elem):
113 attrib = elem.get('type')
114 if (attrib == 'list'):
115 if list(elem) is None:
116 return self.copy_basic_element(elem)
117 new_elem = etree.Element('ignore')
118 for elm in list(elem):
119 elm.tag = elem.tag
120 if elm.get('type') in ['list', 'dict']:
121 self.add_node(self.process_element(elm), new_elem)
122 else:
123 new_elem.append(self.copy_basic_element(elm))
124 return new_elem
125 elif (attrib == 'dict'):
126 # Empty case
127 if list(elem) is None:
128 return self.copy_basic_element(elem)
129
130 # Process field option.
131 new_elem, elem = self.process_inline_option(elem)
132
133 for elm in list(elem):
134 if elm.get('type') in ['list', 'dict']:
135 self.add_node(self.process_element(elm), new_elem)
136 else:
137 new_elem.append(self.copy_basic_element(elm))
138 return new_elem
139 else:
140 return self.copy_basic_element(elem)
141
142 def to_yang_xml(self, from_xml):
143 # Parse from_xml as follows:
144 # 1. Any element having a list attribute shoud have each item move 1 level
145 # up and retag using the parent tag
146 # 2. Any element having a dict attribute and has a <yang_field_option>
147 # sub-element should have all it's items move to teh parent level
148 top = etree.Element('yang')
149 elms = list(from_xml)
150
151 # special case the xml contain a list type
152 if len(elms) == 1:
153 item = elms[0]
154 if item.get('type') == 'list':
155 item.tag = 'ignore'
156 self.add_node(self.process_element(item), top)
157 return top
158
159 # Process normally for all other cases
160 for elm in elms:
161 self.add_node(self.process_element(elm), top)
162
163 return top
164
165 def build_yang_response(self, root, request):
166 try:
167 yang_xml = self.to_yang_xml(root)
168 log.info('yang-xml', yang_xml=etree.tounicode(yang_xml,
169 pretty_print=True))
170 return self.build_xml_response(request, yang_xml)
171 except Exception as e:
172 self.rpc_response.is_error = True
173 self.rpc_response.node = ncerror.BadMsg(request)
174 return