Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 1 | #!/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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 20 | - 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 | |
Khen Nursimulu | a6d9113 | 2016-11-23 11:43:09 -0500 | [diff] [blame] | 25 | $ pyang --plugindir /voltha/experiments/netconf/yang2proto -f proto -o |
Khen Nursimulu | 2ef4ce7 | 2016-11-23 11:37:38 -0500 | [diff] [blame] | 26 | <protofile> -p /voltha/experiments/netconf/tests/yang2proto |
| 27 | /voltha/experiments/netconf/tests/yang2proto/<yang file> |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 28 | |
Khen Nursimulu | 2ef4ce7 | 2016-11-23 11:37:38 -0500 | [diff] [blame] | 29 | - pyang validates the yang definition first and then invoke this plugin |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 30 | to convert the yang model into protobuf. |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 31 | |
| 32 | """ |
| 33 | |
| 34 | from pyang import plugin, statements, error |
| 35 | from pyang.util import unique_prefixes |
| 36 | |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 37 | |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 38 | # Register the Protobuf plugin |
| 39 | def pyang_plugin_init(): |
| 40 | plugin.register_plugin(ProtobufPlugin()) |
| 41 | |
| 42 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 43 | class Protobuf(object): |
| 44 | def __init__(self, module_name): |
| 45 | self.module_name = module_name.replace('-', '_') |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 46 | self.tree = {} |
| 47 | self.containers = [] |
| 48 | self.ylist = [] |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 49 | self.leafs = [] |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 50 | 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 57 | self.headers.append('package {};'.format(module_name.replace('-', |
| 58 | '_'))) |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 59 | 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 Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 82 | |
| 83 | def _print_container(self, container, out, level=0): |
| 84 | spaces = ' ' * level |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 85 | out.append(''.join([spaces, 'message {} '.format( |
| 86 | container.name.replace('-','_'))])) |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 87 | out.append(''.join('{\n')) |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 88 | |
| 89 | self._print_leaf(container.leafs, out, spaces=spaces) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 90 | |
| 91 | for l in container.ylist: |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 92 | self._print_list(l, out, level + 1) |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 93 | |
| 94 | for inner in container.containers: |
| 95 | self._print_container(inner, out, level + 1) |
| 96 | |
| 97 | out.append(''.join([spaces, '}\n'])) |
| 98 | |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 99 | def _print_list(self, ylist, out, level=0): |
| 100 | spaces = ' ' * level |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 101 | 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 106 | |
| 107 | for l in ylist.ylist: |
| 108 | self._print_list(l, out, level + 1) |
| 109 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 110 | out.append(''.join([spaces, '}\n'])) |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 111 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 112 | |
| 113 | def _print_leaf(self, leafs, out, spaces='', include_message=False): |
| 114 | leafspaces = ''.join([spaces, ' ']) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 115 | for idx, l in enumerate(leafs): |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 116 | if l.type == "enum": |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 117 | out.append(''.join([leafspaces, 'enum {}\n'.format( |
| 118 | l.name.replace('-','_'))])) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 119 | out.append(''.join([leafspaces, '{\n'])) |
| 120 | self._print_enumeration(l.enumeration, out, leafspaces) |
| 121 | out.append(''.join([leafspaces, '}\n'])) |
| 122 | else: |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 123 | if include_message: |
| 124 | out.append(''.join([spaces, 'message {} '.format( |
| 125 | l.name.replace('-','_'))])) |
| 126 | out.append(''.join([spaces, '{\n'])) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 127 | out.append(''.join([leafspaces, '{}{} {} = {} ;\n'.format( |
| 128 | 'repeated ' if l.leaf_list else '', |
| 129 | l.type, |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 130 | l.name.replace('-', '_'), |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 131 | idx + 1)])) |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 132 | if include_message: |
| 133 | out.append(''.join([spaces, '}\n'])) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 134 | |
| 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 Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 140 | def print_proto(self): |
| 141 | out = [] |
| 142 | for h in self.headers: |
| 143 | out.append('{}\n'.format(h)) |
| 144 | out.append('\n') |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 145 | |
| 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 151 | out.append('\n') |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 152 | |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 153 | if self.ylist: |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 154 | # remove duplicates |
| 155 | self.ylist = self._filter_duplicate_names(self.ylist) |
| 156 | for l in self.ylist: |
| 157 | self._print_list(l, out) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 158 | out.append('\n') |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 159 | |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 160 | if self.containers: |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 161 | # 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 166 | out.append('\n') |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 167 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 168 | if self.rpcs: |
| 169 | self.rpcs = self._filter_duplicate_names(self.rpcs) |
| 170 | self._print_rpc(out) |
| 171 | |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 172 | return out |
| 173 | |
| 174 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 175 | class YangContainer(object): |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 176 | def __init__(self): |
| 177 | self.name = None |
| 178 | self.containers = [] |
| 179 | self.enums = [] |
| 180 | self.leafs = [] |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 181 | self.ylist = [] |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 182 | |
| 183 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 184 | class YangList(object): |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 185 | def __init__(self): |
| 186 | self.name = None |
| 187 | self.leafs = [] |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 188 | self.containers = [] |
| 189 | self.ylist = [] |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 190 | |
| 191 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 192 | class YangLeaf(object): |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 193 | def __init__(self): |
| 194 | self.name = None |
| 195 | self.type = None |
| 196 | self.leaf_list = False |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 197 | self.enumeration = [] |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 198 | self.description = None |
| 199 | |
| 200 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 201 | class YangEnumeration(object): |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 202 | def __init__(self): |
| 203 | self.value = [] |
| 204 | |
| 205 | |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 206 | class YangRpc(object): |
| 207 | def __init__(self): |
| 208 | self.name = None |
| 209 | self.input = '' |
| 210 | self.output = '' |
| 211 | |
| 212 | |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 213 | class ProtobufPlugin(plugin.PyangPlugin): |
| 214 | def add_output_format(self, fmts): |
| 215 | self.multiple_modules = True |
Khen Nursimulu | 2ef4ce7 | 2016-11-23 11:37:38 -0500 | [diff] [blame] | 216 | fmts['proto'] = self |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 227 | # m.pprint() |
| 228 | # statements.print_tree(m) |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 229 | proto = Protobuf(m.i_modulename) |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 230 | proto.set_headers(m.i_modulename) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 231 | self.process_substatements(m, proto, None) |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 232 | self.process_children(m, proto, None) |
| 233 | out = proto.print_proto() |
| 234 | for i in out: |
| 235 | fd.write(i) |
| 236 | |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 237 | def process_substatements(self, node, parent, pmod): |
| 238 | """Process all substmts. |
| 239 | """ |
| 240 | for st in node.substmts: |
Khen Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 241 | if st.keyword in ["rpc"]: |
| 242 | self.process_rpc(st, parent) |
| 243 | if st.keyword in ["notification"]: |
| 244 | continue |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 245 | 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 Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 267 | 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 Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 271 | if ch.keyword in ["rpc"]: |
| 272 | self.process_rpc(ch, parent) |
| 273 | if ch.keyword in ["notification"]: |
| 274 | continue |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 275 | 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 Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 280 | else: |
| 281 | nmod = ch.i_module.i_modulename |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 282 | if ch.keyword in ["container", "grouping"]: |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 283 | 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 301 | if leaf.type == "enum": |
| 302 | self.process_enumeration(node, leaf) |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 303 | # 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 308 | 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 Nursimulu | bf8bf28 | 2016-11-07 15:21:35 -0500 | [diff] [blame] | 326 | 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 Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 345 | def get_protobuf_type(self, type): |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 346 | type = self.base_type(type) |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 347 | if type in self.protobuf_types_map.keys(): |
| 348 | return self.protobuf_types_map[type] |
Khen Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 349 | 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 Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 365 | # 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 Nursimulu | 4c43525 | 2016-11-03 23:21:32 -0400 | [diff] [blame] | 369 | else: |
| 370 | return type.arg |
Khen Nursimulu | f8abbc9 | 2016-11-04 19:56:45 -0400 | [diff] [blame] | 371 | |
| 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 | ) |