forked from openconfig/goyang
/
proto.go
688 lines (648 loc) · 17.1 KB
/
proto.go
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
// Copyright 2015 Google Inc.
//
// 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.
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/openconfig/goyang/pkg/indent"
"github.com/openconfig/goyang/pkg/yang"
"github.com/pborman/getopt"
)
const (
protoVersion = "1"
tagPrefix = "// goyang-tag "
versionPrefix = "// goyang-version "
)
var (
proto2 bool
protoDir string
protoNoComments bool
protoFlat bool
protoPreserve string
protoWithSource bool
)
func init() {
flags := getopt.New()
register(&formatter{
name: "proto",
f: doProto,
help: "display tree in a proto like format",
flags: flags,
})
flags.BoolVarLong(&proto2, "proto2", 0, "produce proto2 protobufs")
flags.StringVarLong(&protoDir, "proto_dir", 0, "write a .proto file for each module in DIR", "DIR")
flags.BoolVarLong(&protoNoComments, "proto_no_comments", 0, "do not include comments in output")
flags.BoolVarLong(&protoFlat, "proto_flat", 0, "do not produce nested protobufs")
flags.StringVarLong(&protoPreserve, "proto_save", 0, "preserve existing .proto files as filename.SUFFIX", "SUFFIX")
flags.BoolVarLong(&protoWithSource, "proto_with_source", 0, "add source location comments")
}
// A protofile collects the produced proto along with meta information.
type protofile struct {
buf bytes.Buffer
fixedNames map[string]string // maps a fixed name back to its origial name.
errs []error
messages map[string]*messageInfo
hasDecimal64 bool
}
// A messageInfo contains tag information about fields in a message.
type messageInfo struct {
last int
fields map[string]int
}
func doProto(w io.Writer, entries []*yang.Entry) {
failed := false
if protoPreserve != "" && protoPreserve[0] != '.' {
protoPreserve = "." + protoPreserve
}
for _, e := range entries {
if len(e.Dir) == 0 {
// skip modules that have nothing in them
continue
}
pf := &protofile{
fixedNames: map[string]string{},
messages: map[string]*messageInfo{},
}
var out string
// optionally read in tags from old proto
if protoDir != "" {
out = filepath.Join(protoDir, e.Name+".proto")
if fd, err := os.Open(out); err == nil {
err = pf.importTags(fd)
fd.Close()
if err != nil {
failed = true
fmt.Fprintln(os.Stderr, err)
continue
}
}
}
pf.printHeader(&pf.buf, e)
for _, e := range flatten(e) {
pf.printService(&pf.buf, e)
}
for _, child := range children(e) {
if protoFlat {
for _, e := range flatten(child) {
fmt.Fprintln(&pf.buf)
pf.printNode(&pf.buf, e, false)
}
} else {
fmt.Fprintln(&pf.buf)
pf.printNode(&pf.buf, child, true)
}
}
if pf.hasDecimal64 {
prefix := " "
if proto2 {
prefix = " optional"
}
fmt.Fprintf(&pf.buf, `
// A Decimal64 is the YANG decimal64 type.
message Decimal64 {
%s int64 value = 1; // integeral value
%s uint32 fraction_digits = 2; // decimal point position [1..18]
}
`, prefix, prefix)
}
pf.dumpMessageInfo()
if len(pf.errs) != 0 {
for _, err := range pf.errs {
failed = true
fmt.Fprintf(os.Stderr, "%s: %v\n", e.Name, err)
}
continue
}
var closeme io.Closer
if out == "" {
out = "stdout"
} else {
var old string
if protoPreserve != "" {
old = out + protoPreserve
if _, err := os.Stat(out); err == nil {
if err := os.Rename(out, old); err != nil {
failed = true
fmt.Fprintf(os.Stderr, "%s: %v\n", e.Name, err)
continue
}
}
}
fd, err := os.Create(out)
if err != nil {
failed = true
fmt.Fprintln(os.Stderr, err)
}
w = fd
closeme = fd
}
if _, err := io.Copy(w, &pf.buf); err != nil {
failed = true
fmt.Fprintf(os.Stderr, "%s: %v\n", out, err)
}
if closeme != nil {
if err := closeme.Close(); err != nil {
failed = true
fmt.Fprintf(os.Stderr, "%s: %v\n", out, err)
}
}
}
if failed {
os.Exit(1)
}
}
// Children returns all the children nodes of e that are not RPC nodes.
func children(e *yang.Entry) []*yang.Entry {
var names []string
for k, se := range e.Dir {
if se.RPC == nil {
names = append(names, k)
}
}
if len(names) == 0 {
return nil
}
sort.Strings(names)
children := make([]*yang.Entry, len(names))
for x, n := range names {
children[x] = e.Dir[n]
}
return children
}
func (pf *protofile) dumpMessageInfo() {
w := &pf.buf
fmt.Fprint(w, `
// Do not delete the lines below, they preserve tag information for goyang.
`)
names := make([]string, len(pf.messages))
x := 0
for name := range pf.messages {
names[x] = name
x++
}
sort.Strings(names)
for _, name := range names {
mi := pf.messages[name]
mi.dump(name, &pf.buf)
}
}
func (mi *messageInfo) dump(mname string, w io.Writer) {
names := make([]string, len(mi.fields))
x := 0
for name := range mi.fields {
names[x] = name
x++
}
sort.Strings(names)
for _, name := range names {
tag := mi.fields[name]
fmt.Fprintf(w, "%s%s %s %d\n", tagPrefix, mname, name, tag)
}
}
func (pf *protofile) importTags(r io.Reader) error {
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
if strings.HasPrefix(line, versionPrefix) {
version := strings.TrimSpace(line[len(versionPrefix):])
if version != protoVersion {
return fmt.Errorf("unsupported goyang proto version: %s", version)
}
continue
}
if !strings.HasPrefix(line, tagPrefix) {
continue
}
line = line[len(tagPrefix):]
fields := strings.Fields(line)
if len(fields) != 3 {
return fmt.Errorf("invalid goyang-tag: %s", line)
}
tag, err := strconv.Atoi(fields[2])
if err != nil {
return fmt.Errorf("invalid goyang-tag: %s", line)
}
mi := pf.messages[fields[0]]
if mi == nil {
mi = &messageInfo{
fields: map[string]int{},
}
pf.messages[fields[0]] = mi
}
mi.fields[fields[1]] = tag
if mi.last < tag {
mi.last = tag
}
}
return s.Err()
}
func (m *messageInfo) tag(name, kind string, isList bool) int {
key := name + "/" + kind
if isList {
key = key + "[]"
}
if i := m.fields[key]; i != 0 {
return i
}
m.last++
m.fields[key] = m.last
return m.last
}
// kind2proto maps base yang types to protocol buffer types.
// TODO(borman): do TODO types.
var kind2proto = map[yang.TypeKind]string{
yang.Yint8: "int32", // int in range [-128, 127]
yang.Yint16: "int32", // int in range [-32768, 32767]
yang.Yint32: "int32", // int in range [-2147483648, 2147483647]
yang.Yint64: "int64", // int in range [-9223372036854775808, 9223372036854775807]
yang.Yuint8: "uint32", // int in range [0, 255]
yang.Yuint16: "uint32", // int in range [0, 65535]
yang.Yuint32: "uint32", // int in range [0, 4294967295]
yang.Yuint64: "uint64", // int in range [0, 18446744073709551615]
yang.Ybinary: "bytes", // arbitrary data
yang.Ybits: "INLINE-bits", // set of bits or flags
yang.Ybool: "bool", // true or false
yang.Ydecimal64: "INLINE-d64", // signed decimal number
yang.Yempty: "bool", // value is its presense
yang.Yenum: "INLINE-enum", // enumerated strings
yang.Yidentityref: "string", // reference to abstract identity
yang.YinstanceIdentifier: "string", // reference of a data tree node
yang.Yleafref: "string", // reference to a leaf instance
yang.Ystring: "string", // human readable string
yang.Yunion: "INLINE-union", // handled inline
}
func isStream(e *yang.Entry) bool {
for _, ext := range e.Exts {
if ext.Kind() == "grpc:stream" {
return true
}
}
return false
}
func (pf *protofile) printHeader(w io.Writer, e *yang.Entry) {
syntax := "proto3"
if proto2 {
syntax = "proto2"
}
fmt.Fprintf(w, `syntax = %q;
// Automatically generated by goyang https://github.com/openconfig/goyang
// compiled %s`, syntax, time.Now().UTC().Format(time.RFC3339))
fmt.Fprintf(w, `
// do not delete the next line
%s%s`, versionPrefix, protoVersion)
fmt.Fprintf(w, `
// module %q
`, e.Name)
if v := e.Extra["revision"]; len(v) > 0 {
for _, rev := range v[0].([]*yang.Revision) {
fmt.Fprintf(w, "// revision %q\n", rev.Name)
}
}
if v := e.Extra["namespace"]; len(v) > 0 {
fmt.Fprintf(w, "// namespace %q\n", v[0].(*yang.Value).Name)
}
fmt.Fprintln(w)
if !protoNoComments && e.Description != "" {
fmt.Fprintln(indent.NewWriter(w, "// "), e.Description)
}
fmt.Fprintf(w, "package %s;\n", pf.fieldName(e.Name))
}
// printService writes e, formatted almost like a protobuf message, to w.
func (pf *protofile) printService(w io.Writer, e *yang.Entry) {
var names []string
for k, se := range e.Dir {
if se.RPC != nil {
names = append(names, k)
}
}
needEmpty := false
if len(names) == 0 {
return
}
sort.Strings(names)
fmt.Fprintf(w, "service %s {\n", pf.fixName(e.Name))
for _, k := range names {
rpc := e.Dir[k].RPC
k = pf.fixName(k)
iName := "Empty"
oName := "Empty"
if rpc.Input != nil {
iName = k + "Request"
rpc.Input.Name = iName
if isStream(rpc.Input) {
iName = "stream " + iName
}
}
if rpc.Output != nil {
oName = k + "Response"
rpc.Output.Name = oName
if isStream(rpc.Output) {
oName = "stream " + oName
}
}
needEmpty = needEmpty || rpc.Input == nil || rpc.Output == nil
fmt.Fprintf(w, " rpc %s (%s) returns (%s);\n", k, iName, oName)
}
fmt.Fprintln(w, "}")
for _, k := range names {
rpc := e.Dir[k].RPC
if rpc.Input != nil {
pf.printNode(w, rpc.Input, false)
}
if rpc.Output != nil {
pf.printNode(w, rpc.Output, false)
}
}
if needEmpty {
fmt.Fprintln(w, "\nmessage Empty { }")
}
}
// printNode writes e, formatted almost like a protobuf message, to w.
func (pf *protofile) printNode(w io.Writer, e *yang.Entry, nest bool) {
nodes := children(e)
if !protoNoComments && e.Description != "" {
fmt.Fprintln(indent.NewWriter(w, "// "), e.Description)
}
messageName := pf.fullName(e)
mi := pf.messages[messageName]
if mi == nil {
mi = &messageInfo{
fields: map[string]int{},
}
pf.messages[messageName] = mi
}
fmt.Fprintf(w, "message %s {", pf.messageName(e)) // matching brace }
if protoWithSource {
fmt.Fprintf(w, " // %s", yang.Source(e.Node))
}
fmt.Fprintln(w)
for i, se := range nodes {
k := se.Name
if !protoNoComments && se.Description != "" {
fmt.Fprintln(indent.NewWriter(w, " // "), se.Description)
}
if nest && (len(se.Dir) > 0 || se.Type == nil) {
pf.printNode(indent.NewWriter(w, " "), se, true)
}
prefix := " "
if se.ListAttr != nil {
prefix = " repeated "
} else if proto2 {
prefix = " optional "
}
name := pf.fieldName(k)
printed := false
var kind string
if len(se.Dir) > 0 || se.Type == nil {
kind = pf.messageName(se)
} else if se.Type.Kind == yang.Ybits {
values := dedup(se.Type.Bit.Values())
asComment := false
switch {
case len(values) > 0 && values[len(values)-1] > 63:
if i != 0 {
fmt.Fprintln(w)
}
fmt.Fprintf(w, " // *WARNING* bitfield %s has more than 64 positions\n", name)
kind = "uint64"
asComment = true
case len(values) > 0 && values[len(values)-1] > 31:
if i != 0 {
fmt.Fprintln(w)
}
fmt.Fprintf(w, " // bitfield %s to large for enum\n", name)
kind = "uint64"
asComment = true
default:
kind = pf.fixName(se.Name)
}
if !asComment {
fmt.Fprintf(w, " enum %s {\n", kind)
fmt.Fprintf(w, " %s_FIELD_NOT_SET = 0;\n", kind)
} else {
fmt.Fprintf(w, " // Values:\n")
}
names := map[int64][]string{}
for n, v := range se.Type.Bit.NameMap() {
names[v] = append(names[v], n)
}
for _, v := range values {
ns := names[v]
sort.Strings(ns)
if asComment {
for _, n := range ns {
fmt.Fprintf(w, " // %s = 1 << %d\n", n, v)
}
} else {
n := strings.ToUpper(pf.fieldName(ns[0]))
fmt.Fprintf(w, " %s_%s = %d;\n", kind, n, 1<<uint(v))
for _, n := range ns[1:] {
n = strings.ToUpper(pf.fieldName(n))
fmt.Fprintf(w, " // %s = %d; (DUPLICATE VALUE)\n", n, 1<<uint(v))
}
}
}
if !asComment {
fmt.Fprintf(w, " };\n")
}
} else if se.Type.Kind == yang.Ydecimal64 {
kind = "Decimal64"
pf.hasDecimal64 = true
} else if se.Type.Kind == yang.Yenum {
kind = pf.fixName(se.Name)
fmt.Fprintf(w, " enum %s {", kind)
if protoWithSource {
fmt.Fprintf(w, " // %s", yang.Source(se.Node))
}
fmt.Fprintln(w)
for i, n := range se.Type.Enum.Names() {
fmt.Fprintf(w, " %s_%s = %d;\n", kind, strings.ToUpper(pf.fieldName(n)), i)
}
fmt.Fprintf(w, " };\n")
} else if se.Type.Kind == yang.Yunion {
types := pf.unionTypes(se.Type, map[string]bool{})
switch len(types) {
case 0:
fmt.Fprintf(w, " // *WARNING* union %s has no types\n", se.Name)
printed = true
case 1:
kind = types[0]
default:
iw := w
kind = pf.fixName(se.Name)
if se.ListAttr != nil {
fmt.Fprintf(w, " message %s {\n", kind)
iw = indent.NewWriter(w, " ")
}
fmt.Fprintf(iw, " oneof %s {", kind) // matching brace }
if protoWithSource {
fmt.Fprintf(w, " // %s", yang.Source(se.Node))
}
fmt.Fprintln(w)
for _, tkind := range types {
fmt.Fprintf(iw, " %s %s_%s = %d;\n", tkind, kind, tkind, mi.tag(name, tkind, false))
}
// { to match the brace below to keep brace matching working
fmt.Fprintf(iw, " }\n")
if se.ListAttr != nil {
fmt.Fprintf(w, " }\n")
} else {
printed = true
}
}
} else {
kind = kind2proto[se.Type.Kind]
}
if !printed {
fmt.Fprintf(w, "%s%s %s = %d;", prefix, kind, name, mi.tag(name, kind, se.ListAttr != nil))
if protoWithSource {
fmt.Fprintf(w, " // %s", yang.Source(se.Node))
}
fmt.Fprintln(w)
}
}
// { to match the brace below to keep brace matching working
fmt.Fprintln(w, "}")
}
// unionTypes returns a slice of all types in the union (and sub unions).
func (pf *protofile) unionTypes(ut *yang.YangType, seen map[string]bool) []string {
var types []string
for _, t := range ut.Type {
k := t.Kind
switch k {
case yang.Yenum:
// TODO(borman): this is not correct for enums that fit in 32 bits
k = yang.Yuint64
case yang.Ybits:
k = yang.Yuint64
case yang.Yunion:
for _, st := range t.Type {
types = append(types, pf.unionTypes(st, seen)...)
}
continue
}
kn := kind2proto[k]
if k == yang.Ydecimal64 {
kn = "Decimal64"
pf.hasDecimal64 = true
}
if seen[kn] {
continue
}
seen[kn] = true
types = append(types, kn)
}
sort.Strings(types)
return types
}
// messageName returns the name for the message defined by e.
func (pf *protofile) messageName(e *yang.Entry) string {
if protoFlat {
return pf.fullName(e)
}
return pf.fixName(e.Name)
}
// isPlural returns true if p is the plural of s.
func isPlural(s, p string) bool {
if s == "" || p == "" {
return false
}
n := len(p)
return s == p[:n-1] && p[n-1] == 's'
}
// fullName always returns the full pathname of entry e.
func (pf *protofile) fullName(e *yang.Entry) string {
parts := []string{pf.fixName(e.Name)}
for p := e.Parent; p != nil && p.Parent != nil; p = p.Parent {
parts = append(parts, pf.fixName(p.Name))
// Don't output Foos_Foo_, just output Foo_
if len(p.Parent.Dir) == 1 && isPlural(p.Name, p.Parent.Name) {
p = p.Parent
}
}
for i := 0; i < len(parts)/2; i++ {
parts[i], parts[len(parts)-i-1] = parts[len(parts)-i-1], parts[i]
}
return strings.Join(parts, "_")
}
// fieldName simply changes -'s to _'s.
func (pf *protofile) fieldName(s string) string {
if s == "" {
return ""
}
fn := strings.Replace(s, "-", "_", -1)
switch {
case fn[0] >= 'a' && fn[0] <= 'z':
case fn[0] >= 'A' && fn[0] <= 'Z':
default:
fn = "X_" + fn
}
if fn != s {
if o := pf.fixedNames[fn]; o != "" && o != s {
pf.errs = append(pf.errs, fmt.Errorf("collision on %s and %s\n", o, s))
}
pf.fixedNames[fn] = s
}
return fn
}
// fixName returns s in camel case
func (pf *protofile) fixName(s string) string {
cc := yang.CamelCase(s)
if cc != s {
if o := pf.fixedNames[cc]; o != "" && o != s {
pf.errs = append(pf.errs, fmt.Errorf("collision on %s and %s\n", o, s))
}
pf.fixedNames[cc] = s
}
return cc
}
// flatten returns a slice of all directory entries in e and e's decendents.
func flatten(e *yang.Entry) []*yang.Entry {
if e == nil || (len(e.Dir) == 0 && e.Type != nil) {
return nil
}
f := []*yang.Entry{e}
var names []string
for n := range e.Dir {
names = append(names, n)
}
sort.Strings(names)
for _, n := range names {
f = append(f, flatten(e.Dir[n])...)
}
return f
}
// dedump returns the sorted slice a with duplicate values removed.
func dedup(a []int64) []int64 {
if len(a) == 0 {
return a
}
b := []int64{a[0]}
last := a[0]
for _, v := range a[1:] {
if v != last {
b = append(b, v)
last = v
}
}
return b
}