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 | |
| 20 | - very basic support for leaf, leaf-list, containers, list |
| 21 | |
| 22 | """ |
| 23 | |
| 24 | from pyang import plugin, statements, error |
| 25 | from pyang.util import unique_prefixes |
| 26 | |
| 27 | # Register the Protobuf plugin |
| 28 | def pyang_plugin_init(): |
| 29 | plugin.register_plugin(ProtobufPlugin()) |
| 30 | |
| 31 | |
| 32 | class Protobuf(): |
| 33 | def __init__(self): |
| 34 | self.tree = {} |
| 35 | self.containers = [] |
| 36 | self.ylist = [] |
| 37 | self.messages = [] |
| 38 | self.enums = [] |
| 39 | self.headers = [] |
| 40 | self.services = [] |
| 41 | self.rpcs = [] |
| 42 | |
| 43 | def set_headers(self, module_name): |
| 44 | self.headers.append('syntax = "proto3";') |
| 45 | self.headers.append('package {};'.format(module_name)) |
| 46 | |
| 47 | def _print_container(self, container, out, level=0): |
| 48 | spaces = ' ' * level |
| 49 | out.append(''.join([spaces, 'message {} '.format(container.name)])) |
| 50 | out.append(''.join('{\n')) |
| 51 | for idx, l in enumerate(container.leafs): |
| 52 | leafspaces = ''.join([spaces, ' ']) |
| 53 | out.append(''.join([leafspaces, '{}{} {} = {} ;\n'.format( |
| 54 | 'repeated ' if l.leaf_list else '', |
| 55 | l.type, |
| 56 | l.name, |
| 57 | idx + 1)])) |
| 58 | |
| 59 | for inner in container.containers: |
| 60 | self._print_container(inner, out, level + 1) |
| 61 | |
| 62 | out.append(''.join([spaces, '}\n'])) |
| 63 | |
| 64 | def _print_list(self, ylist, out): |
| 65 | out.append('message {} '.format(ylist.name)) |
| 66 | out.append('{\n') |
| 67 | for idx, l in enumerate(ylist.leafs): |
| 68 | leafspaces = ' ' |
| 69 | out.append(''.join([leafspaces, '{}{} {} = {} ;\n'.format( |
| 70 | 'repeated ' if l.leaf_list else '', |
| 71 | l.type, |
| 72 | l.name, |
| 73 | idx + 1)])) |
| 74 | out.append('}\n') |
| 75 | |
| 76 | def print_proto(self): |
| 77 | out = [] |
| 78 | for h in self.headers: |
| 79 | out.append('{}\n'.format(h)) |
| 80 | out.append('\n') |
| 81 | for m in self.messages: |
| 82 | out.append('{}\n'.format(m)) |
| 83 | out.append('\n') |
| 84 | for l in self.ylist: |
| 85 | self._print_list(l, out) |
| 86 | out.append('\n') |
| 87 | for c in self.containers: |
| 88 | self._print_container(c, out) |
| 89 | out.append('\n') |
| 90 | |
| 91 | return out |
| 92 | |
| 93 | |
| 94 | class YangContainer(): |
| 95 | def __init__(self): |
| 96 | self.name = None |
| 97 | self.containers = [] |
| 98 | self.enums = [] |
| 99 | self.leafs = [] |
| 100 | |
| 101 | |
| 102 | class YangList(): |
| 103 | def __init__(self): |
| 104 | self.name = None |
| 105 | self.leafs = [] |
| 106 | |
| 107 | |
| 108 | class YangLeaf(): |
| 109 | def __init__(self): |
| 110 | self.name = None |
| 111 | self.type = None |
| 112 | self.leaf_list = False |
| 113 | self.description = None |
| 114 | |
| 115 | |
| 116 | class ProtobufPlugin(plugin.PyangPlugin): |
| 117 | def add_output_format(self, fmts): |
| 118 | self.multiple_modules = True |
| 119 | fmts['protobuf'] = self |
| 120 | |
| 121 | def setup_fmt(self, ctx): |
| 122 | ctx.implicit_errors = False |
| 123 | |
| 124 | def emit(self, ctx, modules, fd): |
| 125 | """Main control function. |
| 126 | """ |
| 127 | self.real_prefix = unique_prefixes(ctx) |
| 128 | |
| 129 | for m in modules: |
| 130 | proto = Protobuf() |
| 131 | proto.set_headers(m.i_modulename) |
| 132 | self.process_children(m, proto, None) |
| 133 | out = proto.print_proto() |
| 134 | for i in out: |
| 135 | fd.write(i) |
| 136 | |
| 137 | def process_children(self, node, parent, pmod): |
| 138 | """Process all children of `node`, except "rpc" and "notification". |
| 139 | """ |
| 140 | for ch in node.i_children: |
| 141 | if ch.keyword in ["rpc", "notification"]: continue |
| 142 | if ch.keyword in ["choice", "case"]: |
| 143 | self.process_children(ch, parent, pmod) |
| 144 | continue |
| 145 | if ch.i_module.i_modulename == pmod: |
| 146 | nmod = pmod |
| 147 | nodename = ch.arg |
| 148 | print pmod, nodename |
| 149 | else: |
| 150 | nmod = ch.i_module.i_modulename |
| 151 | nodename = "%s:%s" % (nmod, ch.arg) |
| 152 | ndata = [ch.keyword] |
| 153 | if ch.keyword == "container": |
| 154 | print ch.keyword |
| 155 | c = YangContainer() |
| 156 | c.name = ch.arg |
| 157 | self.process_children(ch, c, nmod) |
| 158 | parent.containers.append(c) |
| 159 | # self.process_container(ch, p, nmod) |
| 160 | elif ch.keyword == "list": |
| 161 | l = YangList() |
| 162 | l.name = ch.arg |
| 163 | self.process_children(ch, l, nmod) |
| 164 | parent.ylist.append(l) |
| 165 | elif ch.keyword in ["leaf", "leaf-list"]: |
| 166 | self.process_leaf(ch, parent, ch.keyword == "leaf-list") |
| 167 | |
| 168 | def process_leaf(self, node, parent, leaf_list=False): |
| 169 | # Leaf have specific sub statements |
| 170 | leaf = YangLeaf() |
| 171 | leaf.name = node.arg |
| 172 | leaf.type = self.get_protobuf_type(node.search_one("type")) |
| 173 | # leaf.type = self.base_type(node.search_one("type")) |
| 174 | leaf.description = node.search_one("description") |
| 175 | leaf.leaf_list = leaf_list |
| 176 | parent.leafs.append(leaf) |
| 177 | |
| 178 | def get_protobuf_type(self, type): |
| 179 | protobuf_types_map = dict( |
| 180 | binary='Any', |
| 181 | bits='bytes', |
| 182 | boolean='bool', |
| 183 | decimal64='sint64', |
| 184 | empty='string', |
| 185 | int8='int32', |
| 186 | int16='int32', |
| 187 | int32='int32', |
| 188 | int64='int64', |
| 189 | string='string', |
| 190 | uint8='uint32', |
| 191 | uint16='uint32', |
| 192 | uint32='uint32', |
| 193 | uint64='uint64', |
| 194 | union='OneOf', |
| 195 | enumeration='enum' |
| 196 | ) |
| 197 | type = self.base_type(type) |
| 198 | if protobuf_types_map[type]: |
| 199 | return protobuf_types_map[type] |
| 200 | else: |
| 201 | return type |
| 202 | |
| 203 | def base_type(self, type): |
| 204 | """Return the base type of `type`.""" |
| 205 | while 1: |
| 206 | if type.arg == "leafref": |
| 207 | node = type.i_type_spec.i_target_node |
| 208 | elif type.i_typedef is None: |
| 209 | break |
| 210 | else: |
| 211 | node = type.i_typedef |
| 212 | type = node.search_one("type") |
| 213 | if type.arg == "decimal64": |
| 214 | return [type.arg, int(type.search_one("fraction-digits").arg)] |
| 215 | elif type.arg == "union": |
| 216 | return [type.arg, |
| 217 | [self.base_type(x) for x in type.i_type_spec.types]] |
| 218 | else: |
| 219 | return type.arg |