blob: 36ebc3caf85ab0e2d87b693c1b2a4ba44ce819ad [file] [log] [blame]
Khen Nursimulu4c435252016-11-03 23:21:32 -04001#!/usr/bin/env python
2#
3# Copyright 2016 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#
17
18"""pyang plugin to convert a yang schema to a protobuf schema
19
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040020 - basic support for leaf, leaf-list, containers, list
21
22 - this plugin requires pyang to be present and is run using pyang as
23 follows:
24
25 $ pyang --plugindir /voltha/experiments/plugin -f protobuf -o
26 <protofile> -p /voltha/tests/utests/netconf/yang
27 /voltha/tests/utests/netconf/yang/<yang file>
28
29 - pyang validates the yanhg definition first and then invoke this plugin
30 to convert the yang model into protobuf.
Khen Nursimulu4c435252016-11-03 23:21:32 -040031
32"""
33
34from pyang import plugin, statements, error
35from pyang.util import unique_prefixes
36
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040037
Khen Nursimulu4c435252016-11-03 23:21:32 -040038# Register the Protobuf plugin
39def pyang_plugin_init():
40 plugin.register_plugin(ProtobufPlugin())
41
42
Khen Nursimulubf8bf282016-11-07 15:21:35 -050043class Protobuf(object):
44 def __init__(self, module_name):
45 self.module_name = module_name.replace('-', '_')
Khen Nursimulu4c435252016-11-03 23:21:32 -040046 self.tree = {}
47 self.containers = []
48 self.ylist = []
Khen Nursimulubf8bf282016-11-07 15:21:35 -050049 self.leafs = []
Khen Nursimulu4c435252016-11-03 23:21:32 -040050 self.enums = []
51 self.headers = []
52 self.services = []
53 self.rpcs = []
54
55 def set_headers(self, module_name):
56 self.headers.append('syntax = "proto3";')
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040057 self.headers.append('package {};'.format(module_name.replace('-',
58 '_')))
Khen Nursimulubf8bf282016-11-07 15:21:35 -050059 def _filter_duplicate_names(self, list_obj):
60 current_names = []
61 def _filter_dup(obj):
62 if obj.name not in current_names:
63 current_names.append(obj.name)
64 return True
65
66 return filter(_filter_dup, list_obj)
67
68 def _print_rpc(self, out, level=0):
69 spaces = ' ' * level
70 out.append(''.join([spaces, 'service {} '.format(self.module_name)]))
71 out.append(''.join('{\n'))
72
73 rpc_space = spaces + ' '
74 for rpc in self.rpcs:
75 out.append(''.join([rpc_space, 'rpc {}({}) returns({})'.format(
76 rpc.name.replace('-','_'),
77 rpc.input.replace('-','_'),
78 rpc.output.replace('-','_'))]))
79 out.append(''.join(' {}\n'))
80
81 out.append(''.join([spaces, '}\n']))
Khen Nursimulu4c435252016-11-03 23:21:32 -040082
83 def _print_container(self, container, out, level=0):
84 spaces = ' ' * level
Khen Nursimulubf8bf282016-11-07 15:21:35 -050085 out.append(''.join([spaces, 'message {} '.format(
86 container.name.replace('-','_'))]))
Khen Nursimulu4c435252016-11-03 23:21:32 -040087 out.append(''.join('{\n'))
Khen Nursimulubf8bf282016-11-07 15:21:35 -050088
89 self._print_leaf(container.leafs, out, spaces=spaces)
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040090
91 for l in container.ylist:
Khen Nursimulubf8bf282016-11-07 15:21:35 -050092 self._print_list(l, out, level + 1)
Khen Nursimulu4c435252016-11-03 23:21:32 -040093
94 for inner in container.containers:
95 self._print_container(inner, out, level + 1)
96
97 out.append(''.join([spaces, '}\n']))
98
Khen Nursimuluf8abbc92016-11-04 19:56:45 -040099 def _print_list(self, ylist, out, level=0):
100 spaces = ' ' * level
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500101 out.append(''.join([spaces, 'message {} '.format(ylist.name.replace(
102 '-', '_'))]))
103 out.append(''.join('{\n'))
104
105 self._print_leaf(ylist.leafs, out, spaces=spaces)
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400106
107 for l in ylist.ylist:
108 self._print_list(l, out, level + 1)
109
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500110 out.append(''.join([spaces, '}\n']))
Khen Nursimulu4c435252016-11-03 23:21:32 -0400111
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500112
113 def _print_leaf(self, leafs, out, spaces='', include_message=False):
114 leafspaces = ''.join([spaces, ' '])
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400115 for idx, l in enumerate(leafs):
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400116 if l.type == "enum":
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500117 out.append(''.join([leafspaces, 'enum {}\n'.format(
118 l.name.replace('-','_'))]))
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400119 out.append(''.join([leafspaces, '{\n']))
120 self._print_enumeration(l.enumeration, out, leafspaces)
121 out.append(''.join([leafspaces, '}\n']))
122 else:
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500123 if include_message:
124 out.append(''.join([spaces, 'message {} '.format(
125 l.name.replace('-','_'))]))
126 out.append(''.join([spaces, '{\n']))
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400127 out.append(''.join([leafspaces, '{}{} {} = {} ;\n'.format(
128 'repeated ' if l.leaf_list else '',
129 l.type,
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500130 l.name.replace('-', '_'),
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400131 idx + 1)]))
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500132 if include_message:
133 out.append(''.join([spaces, '}\n']))
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400134
135 def _print_enumeration(self, yang_enum, out, spaces):
136 enumspaces = ''.join([spaces, ' '])
137 for idx, e in enumerate(yang_enum):
138 out.append(''.join([enumspaces, '{}\n'.format(e)]))
139
Khen Nursimulu4c435252016-11-03 23:21:32 -0400140 def print_proto(self):
141 out = []
142 for h in self.headers:
143 out.append('{}\n'.format(h))
144 out.append('\n')
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500145
146 if self.leafs:
147 # Risk of duplicates if leaf was processed as both a
148 # children and a substatement. Filter duplicates.
149 self.leafs = self._filter_duplicate_names(self.leafs)
150 self._print_leaf(self.leafs, out, spaces='', include_message=True)
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400151 out.append('\n')
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500152
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400153 if self.ylist:
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500154 # remove duplicates
155 self.ylist = self._filter_duplicate_names(self.ylist)
156 for l in self.ylist:
157 self._print_list(l, out)
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400158 out.append('\n')
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500159
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400160 if self.containers:
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500161 # remove duplicates
162 self.containers = self._filter_duplicate_names(self.containers)
163 for c in self.containers:
164 self._print_container(c, out)
165
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400166 out.append('\n')
Khen Nursimulu4c435252016-11-03 23:21:32 -0400167
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500168 if self.rpcs:
169 self.rpcs = self._filter_duplicate_names(self.rpcs)
170 self._print_rpc(out)
171
Khen Nursimulu4c435252016-11-03 23:21:32 -0400172 return out
173
174
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500175class YangContainer(object):
Khen Nursimulu4c435252016-11-03 23:21:32 -0400176 def __init__(self):
177 self.name = None
178 self.containers = []
179 self.enums = []
180 self.leafs = []
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400181 self.ylist = []
Khen Nursimulu4c435252016-11-03 23:21:32 -0400182
183
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500184class YangList(object):
Khen Nursimulu4c435252016-11-03 23:21:32 -0400185 def __init__(self):
186 self.name = None
187 self.leafs = []
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400188 self.containers = []
189 self.ylist = []
Khen Nursimulu4c435252016-11-03 23:21:32 -0400190
191
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500192class YangLeaf(object):
Khen Nursimulu4c435252016-11-03 23:21:32 -0400193 def __init__(self):
194 self.name = None
195 self.type = None
196 self.leaf_list = False
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400197 self.enumeration = []
Khen Nursimulu4c435252016-11-03 23:21:32 -0400198 self.description = None
199
200
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500201class YangEnumeration(object):
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400202 def __init__(self):
203 self.value = []
204
205
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500206class YangRpc(object):
207 def __init__(self):
208 self.name = None
209 self.input = ''
210 self.output = ''
211
212
Khen Nursimulu4c435252016-11-03 23:21:32 -0400213class ProtobufPlugin(plugin.PyangPlugin):
214 def add_output_format(self, fmts):
215 self.multiple_modules = True
216 fmts['protobuf'] = self
217
218 def setup_fmt(self, ctx):
219 ctx.implicit_errors = False
220
221 def emit(self, ctx, modules, fd):
222 """Main control function.
223 """
224 self.real_prefix = unique_prefixes(ctx)
225
226 for m in modules:
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400227 # m.pprint()
228 # statements.print_tree(m)
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500229 proto = Protobuf(m.i_modulename)
Khen Nursimulu4c435252016-11-03 23:21:32 -0400230 proto.set_headers(m.i_modulename)
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400231 self.process_substatements(m, proto, None)
Khen Nursimulu4c435252016-11-03 23:21:32 -0400232 self.process_children(m, proto, None)
233 out = proto.print_proto()
234 for i in out:
235 fd.write(i)
236
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400237 def process_substatements(self, node, parent, pmod):
238 """Process all substmts.
239 """
240 for st in node.substmts:
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500241 if st.keyword in ["rpc"]:
242 self.process_rpc(st, parent)
243 if st.keyword in ["notification"]:
244 continue
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400245 if st.keyword in ["choice", "case"]:
246 self.process_substatements(st, parent, pmod)
247 continue
248
249 if st.i_module.i_modulename == pmod:
250 nmod = pmod
251 else:
252 nmod = st.i_module.i_modulename
253
254 if st.keyword in ["container", "grouping"]:
255 c = YangContainer()
256 c.name = st.arg
257 self.process_substatements(st, c, nmod)
258 parent.containers.append(c)
259 elif st.keyword == "list":
260 l = YangList()
261 l.name = st.arg
262 self.process_substatements(st, l, nmod)
263 parent.ylist.append(l)
264 elif st.keyword in ["leaf", "leaf-list"]:
265 self.process_leaf(st, parent, st.keyword == "leaf-list")
266
Khen Nursimulu4c435252016-11-03 23:21:32 -0400267 def process_children(self, node, parent, pmod):
268 """Process all children of `node`, except "rpc" and "notification".
269 """
270 for ch in node.i_children:
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500271 if ch.keyword in ["rpc"]:
272 self.process_rpc(ch, parent)
273 if ch.keyword in ["notification"]:
274 continue
Khen Nursimulu4c435252016-11-03 23:21:32 -0400275 if ch.keyword in ["choice", "case"]:
276 self.process_children(ch, parent, pmod)
277 continue
278 if ch.i_module.i_modulename == pmod:
279 nmod = pmod
Khen Nursimulu4c435252016-11-03 23:21:32 -0400280 else:
281 nmod = ch.i_module.i_modulename
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400282 if ch.keyword in ["container", "grouping"]:
Khen Nursimulu4c435252016-11-03 23:21:32 -0400283 c = YangContainer()
284 c.name = ch.arg
285 self.process_children(ch, c, nmod)
286 parent.containers.append(c)
287 # self.process_container(ch, p, nmod)
288 elif ch.keyword == "list":
289 l = YangList()
290 l.name = ch.arg
291 self.process_children(ch, l, nmod)
292 parent.ylist.append(l)
293 elif ch.keyword in ["leaf", "leaf-list"]:
294 self.process_leaf(ch, parent, ch.keyword == "leaf-list")
295
296 def process_leaf(self, node, parent, leaf_list=False):
297 # Leaf have specific sub statements
298 leaf = YangLeaf()
299 leaf.name = node.arg
300 leaf.type = self.get_protobuf_type(node.search_one("type"))
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400301 if leaf.type == "enum":
302 self.process_enumeration(node, leaf)
Khen Nursimulu4c435252016-11-03 23:21:32 -0400303 # leaf.type = self.base_type(node.search_one("type"))
304 leaf.description = node.search_one("description")
305 leaf.leaf_list = leaf_list
306 parent.leafs.append(leaf)
307
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400308 def process_enumeration(self, node, leaf):
309 enumeration_dict = {}
310 start_node = None
311 for child in node.substmts:
312 if child.keyword == "type":
313 start_node = child;
314 break
315
316 for enum in start_node.search('enum'):
317 val = enum.search_one('value')
318 if val is not None:
319 enumeration_dict[enum.arg] = int(val.arg)
320 else:
321 enumeration_dict[enum.arg] = '0'
322
323 for key, value in enumerate(enumeration_dict):
324 leaf.enumeration.append('{} = {} ;'.format(value, key))
325
Khen Nursimulubf8bf282016-11-07 15:21:35 -0500326 def process_rpc(self, node, parent):
327 yrpc = YangRpc()
328 yrpc.name = node.arg # name of rpc call
329 # look for input node
330 input_node = node.search_one("input")
331 if input_node and input_node.substmts:
332 self.process_children(input_node, parent, None)
333 # Get the first children - there should be only 1
334 yrpc.input = input_node.i_children[0].arg
335
336 output_node = node.search_one("output")
337 if output_node and output_node.substmts:
338 self.process_children(output_node, parent, None)
339 # Get the first children - there should be only 1
340 yrpc.output = output_node.i_children[0].arg
341 # print yrpc.ouput
342 # print yrpc.input
343 parent.rpcs.append(yrpc)
344
Khen Nursimulu4c435252016-11-03 23:21:32 -0400345 def get_protobuf_type(self, type):
Khen Nursimulu4c435252016-11-03 23:21:32 -0400346 type = self.base_type(type)
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400347 if type in self.protobuf_types_map.keys():
348 return self.protobuf_types_map[type]
Khen Nursimulu4c435252016-11-03 23:21:32 -0400349 else:
350 return type
351
352 def base_type(self, type):
353 """Return the base type of `type`."""
354 while 1:
355 if type.arg == "leafref":
356 node = type.i_type_spec.i_target_node
357 elif type.i_typedef is None:
358 break
359 else:
360 node = type.i_typedef
361 type = node.search_one("type")
362 if type.arg == "decimal64":
363 return [type.arg, int(type.search_one("fraction-digits").arg)]
364 elif type.arg == "union":
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400365 # TODO convert union properly
366 return type.arg
367 # return [type.arg,
368 # [self.base_type(x) for x in type.i_type_spec.types]]
Khen Nursimulu4c435252016-11-03 23:21:32 -0400369 else:
370 return type.arg
Khen Nursimuluf8abbc92016-11-04 19:56:45 -0400371
372 protobuf_types_map = dict(
373 binary='Any',
374 bits='bytes',
375 boolean='bool',
376 decimal64='sint64',
377 empty='string',
378 int8='int32',
379 int16='int32',
380 int32='int32',
381 int64='int64',
382 string='string',
383 uint8='uint32',
384 uint16='uint32',
385 uint32='uint32',
386 uint64='uint64',
387 union='string', # TODO : not correct mapping
388 enumeration='enum'
389 )