Adding initial test cases, include enumeration conversion, address review comments
Change-Id: I59a19f80289464c934ad16d2d2ce1c78a6ba6f2c
diff --git a/experiments/plugin/protobuf.py b/experiments/plugin/protobuf.py
index 433ae30..fc177b7 100644
--- a/experiments/plugin/protobuf.py
+++ b/experiments/plugin/protobuf.py
@@ -17,13 +17,24 @@
"""pyang plugin to convert a yang schema to a protobuf schema
- - very basic support for leaf, leaf-list, containers, list
+ - basic support for leaf, leaf-list, containers, list
+
+ - this plugin requires pyang to be present and is run using pyang as
+ follows:
+
+ $ pyang --plugindir /voltha/experiments/plugin -f protobuf -o
+ <protofile> -p /voltha/tests/utests/netconf/yang
+ /voltha/tests/utests/netconf/yang/<yang file>
+
+ - pyang validates the yanhg definition first and then invoke this plugin
+ to convert the yang model into protobuf.
"""
from pyang import plugin, statements, error
from pyang.util import unique_prefixes
+
# Register the Protobuf plugin
def pyang_plugin_init():
plugin.register_plugin(ProtobufPlugin())
@@ -42,37 +53,54 @@
def set_headers(self, module_name):
self.headers.append('syntax = "proto3";')
- self.headers.append('package {};'.format(module_name))
+ self.headers.append('package {};'.format(module_name.replace('-',
+ '_')))
def _print_container(self, container, out, level=0):
spaces = ' ' * level
out.append(''.join([spaces, 'message {} '.format(container.name)]))
out.append(''.join('{\n'))
- for idx, l in enumerate(container.leafs):
- leafspaces = ''.join([spaces, ' '])
- out.append(''.join([leafspaces, '{}{} {} = {} ;\n'.format(
- 'repeated ' if l.leaf_list else '',
- l.type,
- l.name,
- idx + 1)]))
+ self._print_leaf(container.leafs, out, spaces)
+
+ for l in container.ylist:
+ self._print_list(l, out, level)
for inner in container.containers:
self._print_container(inner, out, level + 1)
out.append(''.join([spaces, '}\n']))
- def _print_list(self, ylist, out):
+ def _print_list(self, ylist, out, level=0):
+ spaces = ' ' * level
out.append('message {} '.format(ylist.name))
out.append('{\n')
- for idx, l in enumerate(ylist.leafs):
- leafspaces = ' '
- out.append(''.join([leafspaces, '{}{} {} = {} ;\n'.format(
- 'repeated ' if l.leaf_list else '',
- l.type,
- l.name,
- idx + 1)]))
+ self._print_leaf(ylist.leafs, out, spaces)
+
+ for l in ylist.ylist:
+ self._print_list(l, out, level + 1)
+
out.append('}\n')
+ def _print_leaf(self, leafs, out, spaces):
+ for idx, l in enumerate(leafs):
+ leafspaces = ''.join([spaces, ' '])
+ if l.type == "enum":
+ out.append(''.join([leafspaces, 'enum {}\n'.format(l.name)]))
+ out.append(''.join([leafspaces, '{\n']))
+ self._print_enumeration(l.enumeration, out, leafspaces)
+ out.append(''.join([leafspaces, '}\n']))
+ else:
+ out.append(''.join([leafspaces, '{}{} {} = {} ;\n'.format(
+ 'repeated ' if l.leaf_list else '',
+ l.type,
+ l.name,
+ idx + 1)]))
+
+ def _print_enumeration(self, yang_enum, out, spaces):
+ enumspaces = ''.join([spaces, ' '])
+ for idx, e in enumerate(yang_enum):
+ out.append(''.join([enumspaces, '{}\n'.format(e)]))
+
def print_proto(self):
out = []
for h in self.headers:
@@ -80,13 +108,16 @@
out.append('\n')
for m in self.messages:
out.append('{}\n'.format(m))
- out.append('\n')
+ if self.messages:
+ out.append('\n')
for l in self.ylist:
self._print_list(l, out)
- out.append('\n')
+ if self.ylist:
+ out.append('\n')
for c in self.containers:
self._print_container(c, out)
- out.append('\n')
+ if self.containers:
+ out.append('\n')
return out
@@ -97,12 +128,15 @@
self.containers = []
self.enums = []
self.leafs = []
+ self.ylist = []
class YangList():
def __init__(self):
self.name = None
self.leafs = []
+ self.containers = []
+ self.ylist = []
class YangLeaf():
@@ -110,9 +144,15 @@
self.name = None
self.type = None
self.leaf_list = False
+ self.enumeration = []
self.description = None
+class YangEnumeration():
+ def __init__(self):
+ self.value = []
+
+
class ProtobufPlugin(plugin.PyangPlugin):
def add_output_format(self, fmts):
self.multiple_modules = True
@@ -127,13 +167,43 @@
self.real_prefix = unique_prefixes(ctx)
for m in modules:
+ # m.pprint()
+ # statements.print_tree(m)
proto = Protobuf()
proto.set_headers(m.i_modulename)
+ self.process_substatements(m, proto, None)
self.process_children(m, proto, None)
out = proto.print_proto()
for i in out:
fd.write(i)
+ def process_substatements(self, node, parent, pmod):
+ """Process all substmts.
+ """
+ for st in node.substmts:
+ if st.keyword in ["rpc", "notification"]: continue
+ if st.keyword in ["choice", "case"]:
+ self.process_substatements(st, parent, pmod)
+ continue
+
+ if st.i_module.i_modulename == pmod:
+ nmod = pmod
+ else:
+ nmod = st.i_module.i_modulename
+
+ if st.keyword in ["container", "grouping"]:
+ c = YangContainer()
+ c.name = st.arg
+ self.process_substatements(st, c, nmod)
+ parent.containers.append(c)
+ elif st.keyword == "list":
+ l = YangList()
+ l.name = st.arg
+ self.process_substatements(st, l, nmod)
+ parent.ylist.append(l)
+ elif st.keyword in ["leaf", "leaf-list"]:
+ self.process_leaf(st, parent, st.keyword == "leaf-list")
+
def process_children(self, node, parent, pmod):
"""Process all children of `node`, except "rpc" and "notification".
"""
@@ -144,14 +214,9 @@
continue
if ch.i_module.i_modulename == pmod:
nmod = pmod
- nodename = ch.arg
- print pmod, nodename
else:
nmod = ch.i_module.i_modulename
- nodename = "%s:%s" % (nmod, ch.arg)
- ndata = [ch.keyword]
- if ch.keyword == "container":
- print ch.keyword
+ if ch.keyword in ["container", "grouping"]:
c = YangContainer()
c.name = ch.arg
self.process_children(ch, c, nmod)
@@ -170,33 +235,35 @@
leaf = YangLeaf()
leaf.name = node.arg
leaf.type = self.get_protobuf_type(node.search_one("type"))
+ if leaf.type == "enum":
+ self.process_enumeration(node, leaf)
# leaf.type = self.base_type(node.search_one("type"))
leaf.description = node.search_one("description")
leaf.leaf_list = leaf_list
parent.leafs.append(leaf)
+ def process_enumeration(self, node, leaf):
+ enumeration_dict = {}
+ start_node = None
+ for child in node.substmts:
+ if child.keyword == "type":
+ start_node = child;
+ break
+
+ for enum in start_node.search('enum'):
+ val = enum.search_one('value')
+ if val is not None:
+ enumeration_dict[enum.arg] = int(val.arg)
+ else:
+ enumeration_dict[enum.arg] = '0'
+
+ for key, value in enumerate(enumeration_dict):
+ leaf.enumeration.append('{} = {} ;'.format(value, key))
+
def get_protobuf_type(self, type):
- protobuf_types_map = dict(
- binary='Any',
- bits='bytes',
- boolean='bool',
- decimal64='sint64',
- empty='string',
- int8='int32',
- int16='int32',
- int32='int32',
- int64='int64',
- string='string',
- uint8='uint32',
- uint16='uint32',
- uint32='uint32',
- uint64='uint64',
- union='OneOf',
- enumeration='enum'
- )
type = self.base_type(type)
- if protobuf_types_map[type]:
- return protobuf_types_map[type]
+ if type in self.protobuf_types_map.keys():
+ return self.protobuf_types_map[type]
else:
return type
@@ -213,7 +280,28 @@
if type.arg == "decimal64":
return [type.arg, int(type.search_one("fraction-digits").arg)]
elif type.arg == "union":
- return [type.arg,
- [self.base_type(x) for x in type.i_type_spec.types]]
+ # TODO convert union properly
+ return type.arg
+ # return [type.arg,
+ # [self.base_type(x) for x in type.i_type_spec.types]]
else:
return type.arg
+
+ protobuf_types_map = dict(
+ binary='Any',
+ bits='bytes',
+ boolean='bool',
+ decimal64='sint64',
+ empty='string',
+ int8='int32',
+ int16='int32',
+ int32='int32',
+ int64='int64',
+ string='string',
+ uint8='uint32',
+ uint16='uint32',
+ uint32='uint32',
+ uint64='uint64',
+ union='string', # TODO : not correct mapping
+ enumeration='enum'
+ )