summaryrefslogtreecommitdiffhomepage
path: root/tools/pyang_plugins/bgpyang2golang.py
blob: d86acb77072f7350fda898bf20ef3fdfe90980c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import sys
from pyang import plugin
from collections import namedtuple

_COPYRIGHT_NOTICE = """
// DO NOT EDIT
// generated by pyang using OpenConfig https://github.com/openconfig/public
//
// Copyright (C) 2014-2016 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.

"""

EQUAL_TYPE_LEAF = 0
EQUAL_TYPE_ARRAY = 1
EQUAL_TYPE_MAP = 2
EQUAL_TYPE_CONTAINER = 3


def pyang_plugin_init():
    plugin.register_plugin(GolangPlugin())


class GolangPlugin(plugin.PyangPlugin):

    def __init__(self, name=None):
        super(GolangPlugin, self).__init__(name=name)
        self.multiple_modules = True

    def add_output_format(self, fmts):
        fmts['golang'] = self

    def emit(self, ctx, modules, fd):
        ctx.golang_identity_map = {}
        ctx.golang_typedef_map = {}
        ctx.golang_struct_def = []
        ctx.golang_struct_names = {}

        ctx.emitted_type_names = {}

        ctx.prefix_rel = {}
        ctx.module_deps = []
        for m in modules:
            check_module_deps(ctx, m)

        # visit yang statements
        visit_modules(ctx)

        # emit bgp_configs
        emit_go(ctx, fd)


def visit_modules(ctx):
    # visit typedef and identity
    for mod in ctx.module_deps:
        visit_typedef(ctx, mod)
        visit_identity(ctx, mod)

    # visit container
    for mod in ctx.module_deps:
        visit_children(ctx, mod, mod.i_children)


def emit_go(ctx, fd):
    ctx.golang_struct_def.reverse()
    done = set()

    # emit
    generate_header(ctx, fd)
    generate_common_functions(ctx, fd)

    for mod in ctx.module_deps:
        if mod not in _module_excluded:
            emit_typedef(ctx, mod, fd)
            emit_identity(ctx, mod, fd)

    for struct in ctx.golang_struct_def:
        struct_name = struct.uniq_name
        if struct_name in done:
            continue
        emit_class_def(ctx, struct, struct_name, struct.module_prefix, fd)
        done.add(struct_name)


def check_module_deps(ctx, mod):
    own_prefix = mod.i_prefix
    for k, v in mod.i_prefixes.items():
        mod = ctx.get_module(v[0])
        if mod is None:
            continue

        if mod.i_prefix != own_prefix:
            check_module_deps(ctx, mod)

        ctx.prefix_rel[mod.i_prefix] = k
        if (mod not in ctx.module_deps
                and mod.i_modulename not in _module_excluded):
            ctx.module_deps.append(mod)


def dig_leafref(type_obj):
    reftype = type_obj.i_type_spec.i_target_node.search_one('type')
    if is_leafref(reftype):
        return dig_leafref(reftype)
    else:
        return reftype


def emit_class_def(ctx, stmt, struct_name, prefix, fd):
    if len(stmt.i_children) == 1 and is_list(stmt.i_children[0]):
        return

    print('// struct for container %s:%s' % (prefix, stmt.arg), file=fd)
    print('type %s struct {' % convert_to_golang(struct_name), file=fd)

    equal_elems = []

    for child in stmt.i_children:

        if child.path in _path_exclude:
            continue

        container_or_list_name = child.uniq_name
        val_name_go = convert_to_golang(child.arg)
        child_prefix = get_orig_prefix(child.i_orig_module)
        tag_name = child.uniq_name.lower()
        equal_type = EQUAL_TYPE_LEAF
        equal_data = None
        print('  // original -> %s:%s' % (child_prefix, container_or_list_name), file=fd)

        # case leaf
        if is_leaf(child):
            type_obj = child.search_one('type')
            type_name = type_obj.arg

            # case identityref
            if is_identityref(type_obj):
                emit_type_name = convert_to_golang(type_obj.search_one('base').arg.split(':')[-1])

            # case leafref
            elif is_leafref(type_obj):
                if type_obj.search_one('path').arg.startswith('../config'):
                    continue
                t = dig_leafref(type_obj)
                if is_translation_required(t):
                    print('  //%s:%s\'s original type is %s' % (child_prefix, container_or_list_name, t.arg), file=fd)
                    emit_type_name = translate_type(t.arg)
                elif is_identityref(t):
                    emit_type_name = convert_to_golang(t.search_one('base').arg.split(':')[-1])
                else:
                    emit_type_name = t.arg

            # case embeded enumeration
            elif is_enum(type_obj):
                emit_type_name = val_name_go

            # case translation required
            elif is_translation_required(type_obj):
                print('  //%s:%s\'s original type is %s' % (child_prefix, container_or_list_name, type_name), file=fd)
                emit_type_name = translate_type(type_name)

            # case other primitives
            elif is_builtin_type(type_obj):
                emit_type_name = type_name

            # default
            else:
                base_module = type_obj.i_orig_module.i_prefix
                t = lookup_typedef(ctx, base_module, type_name)
                # print(t.golang_name, file=sys.stderr)
                emit_type_name = t.golang_name

        # case 'case'
        if is_case(child):
            continue

        if is_choice(child) and is_enum_choice(child):
            emit_type_name = val_name_go

        # case leaflist
        if is_leaflist(child):
            type_obj = child.search_one('type')
            type_name = type_obj.arg
            val_name_go = val_name_go + 'List'
            tag_name += '-list'
            equal_type = EQUAL_TYPE_ARRAY

            # case leafref
            if is_leafref(type_obj):
                t = dig_leafref(type_obj)
                emit_type_name = '[]' + t.arg

            # case identityref
            elif is_identityref(type_obj):
                emit_type_name = '[]' + convert_to_golang(type_obj.search_one('base').arg.split(':')[-1])

            # case translation required
            elif is_translation_required(type_obj):
                print('  // original type is list of %s' % type_obj.arg, file=fd)
                emit_type_name = '[]'+translate_type(type_name)

            # case other primitives
            elif is_builtin_type(type_obj):
                emit_type_name = '[]' + type_name

            # default
            else:
                base_module = type_obj.i_orig_module.i_prefix
                t = lookup_typedef(ctx, base_module, type_name)
                emit_type_name = '[]' + t.golang_name

        # case container
        elif is_container(child) or (is_choice(child) and not is_enum_choice(child)):
            key = child_prefix + ':' + container_or_list_name
            t = ctx.golang_struct_names[key]
            val_name_go = t.golang_name
            if len(t.i_children) == 1 and is_list(t.i_children[0]):
                l = t.i_children[0]
                emit_type_name = '[]' + l.golang_name
                equal_type = EQUAL_TYPE_MAP
                equal_data = l.search_one('key').arg
                leaf = l.search_one('leaf').search_one('type')
                if leaf.arg == 'leafref' and leaf.search_one('path').arg.startswith('../config'):
                    equal_data = 'config.' + equal_data
            else:
                emit_type_name = t.golang_name
                equal_type = EQUAL_TYPE_CONTAINER

        # case list
        elif is_list(child):
            key = child_prefix + ':' + container_or_list_name
            t = ctx.golang_struct_names[key]
            val_name_go = val_name_go + 'List'
            tag_name += '-list'
            emit_type_name = '[]' + t.golang_name
            equal_type = EQUAL_TYPE_MAP
            equal_data = child.search_one('key').arg

        if is_container(child):
            name = emit_type_name
            if name.startswith(convert_to_golang(struct_name)) and name.endswith("Config"):
                tag_name = 'config'
                val_name_go = 'Config'
            elif name.startswith(convert_to_golang(struct_name)) and name.endswith("State"):
                tag_name = 'state'
                val_name_go = 'State'

        print('  {0}\t{1} `mapstructure:"{2}" json:"{2},omitempty"`'.format(val_name_go, emit_type_name, tag_name), file=fd)

        equal_elems.append((val_name_go, emit_type_name, equal_type, equal_data))

    print('}', file=fd)

    if not struct_name.endswith('state'):
        print('func (lhs *{0}) Equal(rhs *{0}) bool {{'.format(convert_to_golang(struct_name)), file=fd)
        print('if lhs == nil || rhs == nil {', file=fd)
        print('return false', file=fd)
        print('}', file=fd)

        for val_name, type_name, typ, elem in equal_elems:
            if val_name == 'State':
                continue
            if typ == EQUAL_TYPE_LEAF:
                if type_name == '[]byte':
                    print('if bytes.Compare(lhs.{0}, rhs.{0}) != 0 {{'.format(val_name), file=fd)
                else:
                    print('if lhs.{0} != rhs.{0} {{'.format(val_name), file=fd)
                print('return false', file=fd)
                print('}', file=fd)
            elif typ == EQUAL_TYPE_CONTAINER:
                print('if !lhs.{0}.Equal(&(rhs.{0})) {{'.format(val_name), file=fd)
                print('return false', file=fd)
                print('}', file=fd)
            elif typ == EQUAL_TYPE_ARRAY:
                print('if len(lhs.{0}) != len(rhs.{0}) {{'.format(val_name), file=fd)
                print('return false', file=fd)
                print('}', file=fd)
                print('for idx, l := range lhs.{0} {{'.format(val_name), file=fd)
                if type_name == '[][]byte':
                    print('if bytes.Compare(l, rhs.{0}[idx]) != 0 {{'.format(val_name), file=fd)
                else:
                    print('if l != rhs.{0}[idx] {{'.format(val_name), file=fd)
                print('return false', file=fd)
                print('}', file=fd)
                print('}', file=fd)
            elif typ == EQUAL_TYPE_MAP:
                print('if len(lhs.{0}) != len(rhs.{0}) {{'.format(val_name), file=fd)
                print('return false', file=fd)
                print('}', file=fd)
                print('{', file=fd)
                print('lmap := make(map[string]*{0})'.format(type_name[2:]), file=fd)
                print('for i, l := range lhs.{0} {{'.format(val_name), file=fd)
                print('lmap[mapkey(i, string({0}))] = &lhs.{1}[i]'.format(' + '.join('l.{0}'.format(convert_to_golang(v)) for v in elem.split(' ')), val_name), file=fd)
                print('}', file=fd)
                print('for i, r := range rhs.{0} {{'.format(val_name), file=fd)
                print('if l, y := lmap[mapkey(i, string({0}))]; !y {{'.format('+'.join('r.{0}'.format(convert_to_golang(v)) for v in elem.split(' '))), file=fd)
                print('return false', file=fd)
                print('} else if !r.Equal(l) {', file=fd)
                print('return false', file=fd)
                print('}', file=fd)
                print('}', file=fd)
                print('}', file=fd)
            else:
                sys.stderr.write("invalid equal type %s", typ)

        print('return true', file=fd)
        print('}', file=fd)


def get_orig_prefix(mod):
    orig = mod.i_orig_module
    if orig:
        get_orig_prefix(orig)
    else:
        return mod.i_prefix


def get_path(c):
    path = ''
    if c.parent is not None:
        p = ''
        if hasattr(c, 'i_module'):
            mod = c.i_module
            prefix = mod.search_one('prefix')
            if prefix:
                p = prefix.arg + ":"

        path = get_path(c.parent) + "/" + p + c.arg
    return path


# define container embedded enums
def define_enum(ctx, mod, c):
    prefix = mod.i_prefix
    c.path = get_path(c)
    c.golang_name = convert_to_golang(c.arg)
    if prefix in ctx.golang_typedef_map:
        ctx.golang_typedef_map[prefix][c.arg] = c
    else:
        ctx.golang_typedef_map[prefix] = {c.arg: c}


def visit_children(ctx, mod, children):
    for c in children:
        if is_case(c):
            prefix = get_orig_prefix(c.parent.i_orig_module)
            c.i_orig_module = c.parent.i_orig_module
        else:
            prefix = get_orig_prefix(c.i_orig_module)

        c.uniq_name = c.arg
        if c.arg == 'config':
            c.uniq_name = c.parent.uniq_name + '-config'

        elif c.arg == 'state':
            c.uniq_name = c.parent.uniq_name + '-state'

        elif c.arg == 'graceful-restart' and prefix == 'bgp-mp':
            c.uniq_name = 'mp-graceful-restart'

        if is_leaf(c) and is_enum(c.search_one('type')):
            define_enum(ctx, mod, c)

        elif is_list(c) or is_container(c) or is_choice(c):
            c.golang_name = convert_to_golang(c.uniq_name)

            if is_choice(c):
                picks = pickup_choice(c)
                c.i_children = picks
                if is_enum_choice(c):
                    define_enum(ctx, mod, c)
                    continue

            prefix_name = prefix + ':' + c.uniq_name
            if prefix_name in ctx.golang_struct_names:
                ext_c = ctx.golang_struct_names.get(prefix_name)
                ext_c_child_count = len(getattr(ext_c, "i_children"))
                current_c_child_count = len(getattr(c, "i_children"))
                if ext_c_child_count < current_c_child_count:
                    c.module_prefix = prefix
                    ctx.golang_struct_names[prefix_name] = c
                    idx = ctx.golang_struct_def.index(ext_c)
                    ctx.golang_struct_def[idx] = c
            else:
                c.module_prefix = prefix
                ctx.golang_struct_names[prefix_name] = c
                ctx.golang_struct_def.append(c)

        c.path = get_path(c)
        # print(c.path, file=sys.stderr)
        if hasattr(c, 'i_children'):
            visit_children(ctx, mod, c.i_children)


def pickup_choice(c):
    element = []
    for child in c.i_children:
        if is_case(child):
            element = element + child.i_children

    return element


def get_type_spec(stmt):
    for s in stmt.substmts:
        if hasattr(s, 'i_type_spec'):
            return s.i_type_spec.name

    return None


def visit_typedef(ctx, mod):
    prefix = mod.i_prefix
    child_map = {}
    for stmt in mod.substmts:
        if is_typedef(stmt):
            stmt.path = get_path(stmt)
            # print('stmt.path = "%s"' % stmt.path, file=sys.stderr)
            name = stmt.arg
            stmt.golang_name = convert_to_golang(name)
            # print('stmt.golang_name = "%s"' % stmt.golang_name, file=sys.stderr)
            child_map[name] = stmt

    ctx.golang_typedef_map[prefix] = child_map
    # print('ctx.golang_typedef_map["%s"] = %s' % (prefix, child_map), file=sys.stderr)
    prefix_rel = ctx.prefix_rel[prefix]
    ctx.golang_typedef_map[prefix_rel] = child_map
    # print('ctx.golang_typedef_map["%s"] = %s' % (prefix_rel, child_map)), file=sys.stderr)


def visit_identity(ctx, mod):
    prefix = mod.i_prefix
    child_map = {}
    for stmt in mod.substmts:
        if is_identity(stmt):
            name = stmt.arg
            stmt.golang_name = convert_to_golang(name)
            # print('stmt.golang_name = "%s"' % stmt.golang_name, file=sys.stderr)
            child_map[name] = stmt

            base = stmt.search_one('base')
            if base:
                base_name = base.arg
                if ':' in base_name:
                    base_prefix, base_name = base_name.split(':', 1)
                    if base_prefix in ctx.golang_identity_map:
                        ctx.golang_identity_map[base_prefix][base_name].substmts.append(stmt)
                else:
                    child_map[base_name].substmts.append(stmt)

    ctx.golang_identity_map[prefix] = child_map
    # print('ctx.golang_identity_map["%s"] = %s\n' % (prefix, child_map), file=sys.stderr)
    prefix_rel = ctx.prefix_rel[prefix]
    ctx.golang_identity_map[prefix_rel] = child_map
    # print('ctx.golang_identity_map["%s"] = %s\n' % (prefix_rel, child_map), file=sys.stderr)


def lookup_identity(ctx, default_prefix, identity_name):
    result = lookup(ctx.golang_identity_map, default_prefix, identity_name)
    return result


def lookup_typedef(ctx, default_prefix, type_name):
    result = lookup(ctx.golang_typedef_map, default_prefix, type_name)
    return result


def lookup(basemap, default_prefix, key):
    if ':' in key:
        pref, name = key.split(':')
    else:
        pref = default_prefix
        name = key

    if pref in basemap:
        return basemap[pref].get(name, None)
    else:
        return key


def emit_enum(prefix, name, stmt, substmts, fd):
        type_name_org = name
        type_name = stmt.golang_name
        print('// typedef for identity %s:%s' % (prefix, type_name_org), file=fd)
        print('type %s string' % type_name, file=fd)

        const_prefix = convert_const_prefix(type_name_org)
        print('const (', file=fd)
        m = {}

        if is_choice(stmt) and is_enum_choice(stmt):
            n = namedtuple('Statement', ['arg'])
            n.arg = 'none'
            substmts = [n] + substmts

        for sub in substmts:
            enum_name = '%s_%s' % (const_prefix, convert_const_prefix(sub.arg))
            m[sub.arg.lower()] = enum_name
            print(' %s %s = "%s"' % (enum_name, type_name, sub.arg.lower()), file=fd)
        print(')\n', file=fd)

        print('var %sToIntMap = map[%s]int {' % (type_name, type_name), file=fd)
        for i, sub in enumerate(substmts):
            enum_name = '%s_%s' % (const_prefix, convert_const_prefix(sub.arg))
            print(' %s: %d,' % (enum_name, i), file=fd)
        print('}\n', file=fd)

        print('func (v %s) ToInt() int {' % type_name, file=fd)
        print('i, ok := %sToIntMap[v]' % type_name, file=fd)
        print('if !ok {', file=fd)
        print('return -1', file=fd)
        print('}', file=fd)
        print('return i', file=fd)
        print('}', file=fd)

        print('var IntTo%sMap = map[int]%s {' % (type_name, type_name), file=fd)
        for i, sub in enumerate(substmts):
            enum_name = '%s_%s' % (const_prefix, convert_const_prefix(sub.arg))
            print(' %d: %s,' % (i, enum_name), file=fd)
        print('}\n', file=fd)

        print('func (v %s) Validate() error {' % type_name, file=fd)
        print('if _, ok := %sToIntMap[v]; !ok {' % type_name, file=fd)
        print('return fmt.Errorf("invalid %s: %%s", v)' % type_name, file=fd)
        print('}', file=fd)
        print('return nil', file=fd)
        print('}\n', file=fd)

        if stmt.search_one('default'):
            default = stmt.search_one('default')
            print('func (v %s) Default() %s {' % (type_name, type_name), file=fd)
            print('return %s' % m[default.arg.lower()], file=fd)
            print('}\n', file=fd)

            print('func (v %s) DefaultAsNeeded() %s {' % (type_name, type_name), file=fd)
            print(' if string(v) == "" {', file=fd)
            print(' return v.Default()', file=fd)
            print('}', file=fd)
            print(' return v', file=fd)
            print('}', file=fd)


def emit_typedef(ctx, mod, fd):
    prefix = mod.i_prefix
    t_map = ctx.golang_typedef_map[prefix]
    for name, stmt in t_map.items():
        if stmt.path in _typedef_exclude:
            continue

        # skip identityref type because currently skip identity
        if get_type_spec(stmt) == 'identityref':
            continue

        type_name_org = name
        type_name = stmt.golang_name
        if type_name in ctx.emitted_type_names:
            print("warning %s: %s has already been emitted from %s."
                  % (prefix + ":" + type_name_org, type_name_org, ctx.emitted_type_names[type_name]),
                  file=sys.stderr)
            continue

        ctx.emitted_type_names[type_name] = prefix + ":" + type_name_org

        t = stmt.search_one('type')
        if not t and is_choice(stmt):
            emit_enum(prefix, type_name_org, stmt, stmt.i_children, fd)
        elif is_enum(t):
            emit_enum(prefix, type_name_org, stmt, t.substmts, fd)
        elif is_union(t):
            print('// typedef for typedef %s:%s' % (prefix, type_name_org), file=fd)
            print('type %s string' % type_name, file=fd)
        else:
            if is_leafref(t):
                t = dig_leafref(t)

            print('// typedef for typedef %s:%s' % (prefix, type_name_org), file=fd)
            if is_builtin_type(t):
                print('type %s %s' % (type_name, t.arg), file=fd)
            elif is_translation_required(t):
                print('  //%s:%s\'s original type is %s' % (prefix, name, t.arg), file=fd)
                print('type %s %s' % (type_name, translate_type(t.arg)), file=fd)
            else:
                m = ctx.golang_typedef_map
                for k in t.arg.split(':'):
                    m = m[k]
                print('type %s %s' % (type_name, m.golang_name), file=fd)


def emit_identity(ctx, mod, fd):
    prefix = mod.i_prefix
    i_map = ctx.golang_identity_map[prefix]
    for name, stmt in i_map.items():
        enums = stmt.search('identity')
        if len(enums) > 0:
            emit_enum(prefix, name, stmt, enums, fd)


def is_reference(s):
    return s.arg in ['leafref', 'identityref']


def is_leafref(s):
    return s.arg in ['leafref']


def is_identityref(s):
    return s.arg in ['identityref']


def is_enum(s):
    return s.arg in ['enumeration']


def is_union(s):
    return s.arg in ['union']


def is_typedef(s):
    return s.keyword in ['typedef']


def is_identity(s):
    return s.keyword in ['identity']


def is_leaf(s):
    return s.keyword in ['leaf']


def is_leaflist(s):
    return s.keyword in ['leaf-list']


def is_list(s):
    return s.keyword in ['list']


def is_container(s):
    return s.keyword in ['container']


def is_case(s):
    return s.keyword in ['case']


def is_choice(s):
    return s.keyword in ['choice']


def is_enum_choice(s):
    return all(e.search_one('type').arg in _type_enum_case for e in s.i_children)

_type_enum_case = [
    'empty',
]


def is_builtin_type(t):
    return t.arg in _type_builtin


def is_translation_required(t):
    return t.arg in list(_type_translation_map.keys())


_type_translation_map = {
    'union': 'string',
    'decimal64': 'float64',
    'boolean': 'bool',
    'empty': 'bool',
    'inet:ip-address': 'string',
    'inet:ip-prefix': 'string',
    'inet:ipv4-address': 'string',
    'inet:as-number': 'uint32',
    'bgp-set-community-option-type': 'string',
    'inet:port-number': 'uint16',
    'yang:timeticks': 'int64',
    'ptypes:install-protocol-type': 'string',
    'binary': '[]byte',
    'bgp-capability': 'bgp.ParameterCapabilityInterface',
    'bgp-open-message': '*bgp.BGPMessage',
}


_type_builtin = [
    "union",
    "int8",
    "int16",
    "int32",
    "int64",
    "string",
    "uint8",
    "uint16",
    "uint32",
    "uint64",
]


_module_excluded = [
    "ietf-inet-types",
    "ietf-yang-types",
]

_path_exclude = [
    "/rpol:routing-policy/rpol:defined-sets/rpol:neighbor-sets/rpol:neighbor-set/rpol:neighbor",
    "/rpol:routing-policy/rpol:defined-sets/bgp-pol:bgp-defined-sets/bgp-pol:community-sets/bgp-pol:community-set/bgp-pol:community-member",
    "/rpol:routing-policy/rpol:defined-sets/bgp-pol:bgp-defined-sets/bgp-pol:ext-community-sets/bgp-pol:ext-community-set/bgp-pol:ext-community-member",
    "/rpol:routing-policy/rpol:defined-sets/bgp-pol:bgp-defined-sets/bgp-pol:as-path-sets/bgp-pol:as-path-set/bgp-pol:as-path-set-member",
]

_typedef_exclude = [
    "/gobgp:bgp-capability",
    "/gobgp:bgp-open-message",
]


def generate_header(ctx, fd):
    print(_COPYRIGHT_NOTICE, file=fd)
    print('package config', file=fd)
    print('', file=fd)
    print('import (', file=fd)
    print('"fmt"', file=fd)
    print('', file=fd)
    print('"github.com/osrg/gobgp/packet/bgp"', file=fd)
    print(')', file=fd)
    print('', file=fd)


def generate_common_functions(ctx, fd):
    print('func mapkey(index int, name string) string {', file=fd)
    print('if name != "" {', file=fd)
    print('return name', file=fd)
    print('}', file=fd)
    print('return fmt.Sprintf("%v", index)', file=fd)
    print('}', file=fd)


def translate_type(key):
    if key in _type_translation_map.keys():
        return _type_translation_map[key]
    else:
        return key


# 'hoge-hoge' -> 'HogeHoge'
def convert_to_golang(type_string):
    a = type_string.split('.')
    a = [x.capitalize() for x in a]  # XXX locale sensitive
    return '.'.join(''.join(t.capitalize() for t in x.split('-')) for x in a)


# 'hoge-hoge' -> 'HOGE_HOGE'
def convert_const_prefix(type_string):
    return type_string.replace('-', '_').upper()


def chop_suf(s, suf):
    if not s.endswith(suf):
        return s
    return s[:-len(suf)]