forked from ivajloip/goyang
/
entry.go
1440 lines (1321 loc) · 39.3 KB
/
entry.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
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 yang
// The file contains the code to convert an AST (Node) tree into an Entry tree
// via the ToEntry function. The entry tree, once fully resolved, is the
// product of this package. The tree should have all types and references
// resolved.
//
// TODO(borman): handle types, leafrefs, and extensions
import (
"errors"
"fmt"
"io"
"reflect"
"sort"
"strconv"
"strings"
"github.com/openconfig/goyang/pkg/indent"
)
// A TriState may be true, false, or unset
type TriState int
// The possible states of a TriState.
const (
TSUnset = TriState(iota)
TSTrue
TSFalse
)
// Value returns the value of t as a boolean. Unset is returned as false.
func (t TriState) Value() bool {
return t == TSTrue
}
// String displays t as a string.
func (t TriState) String() string {
switch t {
case TSUnset:
return "unset"
case TSTrue:
return "true"
case TSFalse:
return "false"
default:
return fmt.Sprintf("ts-%d", t)
}
}
// An Entry represents a single node (directory or leaf) created from the
// AST. Directory entries have a non-nil Dir entry. Leaf nodes have a nil
// Dir entry. If Errors is not nil then the only other valid field is Node.
type Entry struct {
Parent *Entry `json:"-"`
Node Node `json:"-"` // the base node this Entry was derived from.
Name string // our name, same as the key in our parent Dirs
Description string `json:",omitempty"` // description from node, if any
Default string `json:",omitempty"` // default from node, if any
Units string `json:",omitempty"` // units associated with the type, if any
Errors []error `json:"-"` // list of errors encountered on this node
Kind EntryKind // kind of Entry
Config TriState // config state of this entry, if known
Prefix *Value `json:",omitempty"` // prefix to use from this point down
Mandatory TriState `json:",omitempty"` // whether this entry is mandatory in the tree
// Fields associated with directory nodes
Dir map[string]*Entry `json:",omitempty"`
Key string `json:",omitempty"` // Optional key name for lists (i.e., maps)
// Fields associated with leaf nodes
Type *YangType `json:",omitempty"`
Exts []*Statement `json:",omitempty"` // extensions found
// Fields associated with list nodes (both lists and leaf-lists)
ListAttr *ListAttr `json:",omitempty"`
RPC *RPCEntry `json:",omitempty"` // set if we are an RPC
// Identities that are defined in this context, this is set if the Entry
// is a module only.
Identities []*Identity `json:",omitempty"`
Augments []*Entry `json:",omitempty"` // Augments defined in this entry.
Augmented []*Entry `json:",omitempty"` // Augments merged into this entry.
Deviations []*DeviatedEntry `json:"-"` // Deviations associated with this entry.
Deviate map[deviationType][]*Entry `json:"-"`
Uses []*UsesStmt `json:",omitempty"` // Uses merged into this entry.
// Extra maps all the unsupported fields to their values
Extra map[string][]interface{} `json:"-"`
// Annotation stores annotated values, and is not populated by this
// library but rather can be used by calling code where additional
// information should be stored alongside the Entry.
Annotation map[string]interface{} `json:",omitempty"`
// namespace stores the namespace of the Entry if it overrides the
// root namespace within the schema tree. This is the case where an
// entry is augmented into the tree, and it retains the namespace of
// the augmenting entity per RFC6020 Section 7.15.2. The namespace
// of the Entry should be accessed using the Namespace function.
namespace *Value
}
// An RPCEntry contains information related to an RPC Node.
type RPCEntry struct {
Input *Entry
Output *Entry
}
// A ListAttr is associated with an Entry that represents a List node
type ListAttr struct {
MinElements *Value // leaf-list or list MUST have at least min-elements
MaxElements *Value // leaf-list or list has at most max-elements
OrderedBy *Value // order of entries determined by "system" or "user"
}
// A UsesStmt associates a *Uses with its referenced grouping *Entry
type UsesStmt struct {
Uses *Uses
Grouping *Entry
}
// Modules returns the Modules structure that e is part of. This is needed
// when looking for rooted nodes not part of this Entry tree.
func (e *Entry) Modules() *Modules {
for e.Parent != nil {
e = e.Parent
}
return e.Node.(*Module).modules
}
// IsDir returns true if e is a directory.
func (e *Entry) IsDir() bool {
return e.Dir != nil
}
// IsLeaf returns true if e is a leaf i.e. is not a container, list, leaf-list,
// choice or case statement.
func (e *Entry) IsLeaf() bool {
return !e.IsDir() && e.Kind == LeafEntry && e.ListAttr == nil
}
// IsLeafList returns true if e is a leaf-list.
func (e *Entry) IsLeafList() bool {
return !e.IsDir() && e.Kind == LeafEntry && e.ListAttr != nil
}
// IsList returns true if e is a list.
func (e *Entry) IsList() bool {
return e.IsDir() && e.ListAttr != nil
}
// IsContainer returns true if e is a container.
func (e *Entry) IsContainer() bool {
return e.Kind == DirectoryEntry && e.ListAttr == nil
}
// IsChoice returns true if the entry is a choice node within the schema.
func (e *Entry) IsChoice() bool {
return e.Kind == ChoiceEntry
}
// IsCase returns true if the entry is a case node within the schema.
func (e *Entry) IsCase() bool {
return e.Kind == CaseEntry
}
// Print prints e to w in human readable form.
func (e *Entry) Print(w io.Writer) {
if e.Description != "" {
fmt.Fprintln(w)
fmt.Fprintln(indent.NewWriter(w, "// "), e.Description)
}
if e.ReadOnly() {
fmt.Fprintf(w, "RO: ")
} else {
fmt.Fprintf(w, "rw: ")
}
if e.Type != nil {
fmt.Fprintf(w, "%s ", e.Type.Name)
}
switch {
case e.Dir == nil && e.ListAttr != nil:
fmt.Fprintf(w, "[]%s\n", e.Name)
return
case e.Dir == nil:
fmt.Fprintf(w, "%s\n", e.Name)
return
case e.ListAttr != nil:
fmt.Fprintf(w, "[%s]%s {\n", e.Key, e.Name) //}
default:
fmt.Fprintf(w, "%s {\n", e.Name) //}
}
var names []string
for k := range e.Dir {
names = append(names, k)
}
sort.Strings(names)
for _, k := range names {
e.Dir[k].Print(indent.NewWriter(w, " "))
}
// { to match the brace below to keep brace matching working
fmt.Fprintln(w, "}")
}
// An EntryKind is the kind of node an Entry is. All leaf nodes are of kind
// LeafEntry. A LeafList is also considered a leaf node. All other kinds are
// directory nodes.
type EntryKind int
// Enumeration of the types of entries.
const (
LeafEntry = EntryKind(iota)
DirectoryEntry
AnyDataEntry
AnyXMLEntry
CaseEntry
ChoiceEntry
InputEntry
NotificationEntry
OutputEntry
DeviateEntry
)
// EntryKindToName maps EntryKind to their names
var EntryKindToName = map[EntryKind]string{
LeafEntry: "Leaf",
DirectoryEntry: "Directory",
AnyDataEntry: "AnyData",
AnyXMLEntry: "AnyXML",
CaseEntry: "Case",
ChoiceEntry: "Choice",
InputEntry: "Input",
NotificationEntry: "Notification",
OutputEntry: "Output",
DeviateEntry: "Deviate",
}
func (k EntryKind) String() string {
if s := EntryKindToName[k]; s != "" {
return s
}
return fmt.Sprintf("unknown-entry-%d", k)
}
// newDirectory returns an empty directory Entry.
func newDirectory(n Node) *Entry {
return &Entry{
Kind: DirectoryEntry,
Dir: make(map[string]*Entry),
Node: n,
Name: n.NName(),
Extra: map[string][]interface{}{},
}
}
// newLeaf returns an empty leaf Entry.
func newLeaf(n Node) *Entry {
return &Entry{
Kind: LeafEntry,
Node: n,
Name: n.NName(),
Extra: map[string][]interface{}{},
}
}
// newError returns an error node using format and v to create the error
// contained in the node. The location of the error is prepended.
func newError(n Node, format string, v ...interface{}) *Entry {
e := &Entry{Node: n}
e.errorf("%s: "+format, append([]interface{}{Source(n)}, v...)...)
return e
}
// errorf appends the entry constructed from string and v to the list of errors
// on e.
func (e *Entry) errorf(format string, v ...interface{}) {
e.Errors = append(e.Errors, fmt.Errorf(format, v...))
}
// addError appends err to the list of errors on e if err is not nil.
func (e *Entry) addError(err error) {
if err != nil {
e.Errors = append(e.Errors, err)
}
}
// importErrors imports all the errors from c and its children into e.
func (e *Entry) importErrors(c *Entry) {
if c == nil {
return
}
for _, err := range c.Errors {
e.addError(err)
}
// TODO(borman): need to determine if the extensions have errors
// for _, ce := range e.Exts {
// e.importErrors(ce)
// }
for _, ce := range c.Dir {
e.importErrors(ce)
}
}
// checkErrors calls f on every error found in the tree e and its children.
func (e *Entry) checkErrors(f func(error)) {
if e == nil {
return
}
for _, e := range e.Dir {
e.checkErrors(f)
}
for _, err := range e.Errors {
f(err)
}
// TODO(borman): need to determine if the extensions have errors
// for _, e := range e.Exts {
// e.checkErrors(f)
// }
}
// GetErrors returns a sorted list of errors found in e.
func (e *Entry) GetErrors() []error {
// the seen map is used to eliminate duplicate errors.
// Some entries will be processed more than once
// (groupings in particular) and as such may cause
// duplication of errors.
seen := map[error]bool{}
var errs []error
e.checkErrors(func(err error) {
if !seen[err] {
errs = append(errs, err)
seen[err] = true
}
})
return errorSort(errs)
}
// asKind sets the kind of e to k and returns e.
func (e *Entry) asKind(k EntryKind) *Entry {
e.Kind = k
return e
}
// add adds the directory entry key assigned to the provided value.
func (e *Entry) add(key string, value *Entry) *Entry {
value.Parent = e
if e.Dir[key] != nil {
e.errorf("%s: duplicate key from %s: %s", Source(e.Node), Source(value.Node), key)
return e
}
e.Dir[key] = value
return e
}
// delete removes the directory entry key from the entry.
func (e *Entry) delete(key string) {
if _, ok := e.Dir[key]; !ok {
e.errorf("%s: unknown child key %s", Source(e.Node), key)
}
delete(e.Dir, key)
}
// GetWhenXPath returns the when XPath statement of e if able.
func (e *Entry) GetWhenXPath() (string, bool) {
switch n := e.Node.(type) {
case *Container:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *Leaf:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *LeafList:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *List:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *Choice:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *Case:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *AnyXML:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *AnyData:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
case *Augment:
if n.When != nil && n.When.Statement() != nil {
return n.When.Statement().Arg()
}
}
return "", false
}
// entryCache is used to prevent unnecessary recursion into previously
// converted nodes.
var entryCache = map[Node]*Entry{}
// mergedSubmodule is used to prevent re-parsing a submodule that has already
// been merged into a particular entity when circular dependencies are being
// ignored. The keys of the map are a string that is formed by concatenating
// the name of the including (sub)module and the included submodule.
var mergedSubmodule = map[string]bool{}
var depth = 0
// deviationType specifies an enumerated value covering the different substmts
// to the deviate statement.
type deviationType int64
const (
// DeviationUnset specifies that the argument was unset, which is invalid.
DeviationUnset deviationType = iota
// DeviationNotSupported corresponds to the not-supported deviate argument.
DeviationNotSupported
// DeviationAdd corresponds to the add deviate argument to the deviate stmt.
DeviationAdd
// DeviationReplace corresponds to the replace argument to the deviate stmt.
DeviationReplace
// DeviationDelete corresponds to the delete argument to the deviate stmt.
DeviationDelete
)
var (
// fromDeviation maps from an enumerated deviation type to the YANG keyword.
fromDeviation = map[deviationType]string{
DeviationNotSupported: "not-supported",
DeviationAdd: "add",
DeviationReplace: "replace",
DeviationDelete: "delete",
DeviationUnset: "unknown",
}
// toDeviation maps from the YANG keyword to an enumerated deviation typee.
toDeviation = map[string]deviationType{
"not-supported": DeviationNotSupported,
"add": DeviationAdd,
"replace": DeviationReplace,
"delete": DeviationDelete,
}
)
func (d deviationType) String() string {
return fromDeviation[d]
}
// DeviatedEntry stores a wrapped Entry that corresponds to a deviation.
type DeviatedEntry struct {
Type deviationType // Type specifies the deviation type.
DeviatedPath string // DeviatedPath corresponds to the path that is being deviated.
// Entry is the embedded Entry storing the deviations that are made. Fields
// are set to the value in the schema after the deviation has been applied.
*Entry
}
// ToEntry expands node n into a directory Entry. Expansion is based on the
// YANG tags in the structure behind n. ToEntry must only be used
// with nodes that are directories, such as top level modules and sub-modules.
// ToEntry never returns nil. Any errors encountered are found in the Errors
// fields of the returned Entry and its children. Use GetErrors to determine
// if there were any errors.
func ToEntry(n Node) (e *Entry) {
if n == nil {
err := errors.New("ToEntry called with nil")
return &Entry{
Node: &ErrorNode{Error: err},
Errors: []error{err},
}
}
ms := getModules(n)
if e := entryCache[n]; e != nil {
return e
}
defer func() {
entryCache[n] = e
}()
// Copy in the extensions from our Node, if any.
defer func(n Node) {
if e != nil {
for _, ext := range n.Exts() {
e.Exts = append(e.Exts, ext)
}
}
}(n)
// tristateValue returns TSTrue if i contains the value of true, TSFalse
// if it contains the value of false, and TSUnset if i does not have
// a set value (for instance, i is nil). An error is returned if i
// contains a value other than true or false.
tristateValue := func(i interface{}) (TriState, error) {
if v, ok := i.(*Value); ok && v != nil {
switch v.Name {
case "true":
return TSTrue, nil
case "false":
return TSFalse, nil
default:
return TSUnset, fmt.Errorf("%s: invalid config value: %s", Source(n), v.Name)
}
}
return TSUnset, nil
}
var err error
// Handle non-directory nodes (leaf, leafref, and oddly enough, uses).
switch s := n.(type) {
case *Leaf:
e := newLeaf(n)
if errs := s.Type.resolve(ms.typeDict); errs != nil {
e.Errors = errs
}
if s.Description != nil {
e.Description = s.Description.Name
}
if s.Default != nil {
e.Default = s.Default.Name
}
e.Type = s.Type.YangType
entryCache[n] = e
e.Config, err = tristateValue(s.Config)
e.addError(err)
e.Prefix = getRootPrefix(e)
return e
case *LeafList:
// Create the equivalent leaf element that we are a list of.
// We can then just annotate it as a list rather than a leaf.
leaf := &Leaf{
Name: s.Name,
Source: s.Source,
Parent: s.Parent,
Extensions: s.Extensions,
Config: s.Config,
Description: s.Description,
IfFeature: s.IfFeature,
Must: s.Must,
Reference: s.Reference,
Status: s.Status,
Type: s.Type,
Units: s.Units,
When: s.When,
}
e := ToEntry(leaf)
e.ListAttr = &ListAttr{
MinElements: s.MinElements,
MaxElements: s.MaxElements,
OrderedBy: s.OrderedBy,
}
e.Prefix = getRootPrefix(e)
return e
case *Uses:
g := FindGrouping(s, s.Name, map[string]bool{})
if g == nil {
return newError(n, "unknown group: %s", s.Name)
}
// We need to return a duplicate so we resolve properly
// when the group is used in multiple locations and the
// grouping has a leafref that references outside the group.
return ToEntry(g).dup()
}
e = newDirectory(n)
// Special handling for individual Node types. Lists are like any other
// node except a List has a ListAttr.
//
// Nodes of identified special kinds have their Kind set here.
switch s := n.(type) {
case *List:
e.ListAttr = &ListAttr{
MinElements: s.MinElements,
MaxElements: s.MaxElements,
OrderedBy: s.OrderedBy,
}
case *Choice:
e.Kind = ChoiceEntry
if s.Default != nil {
e.Default = s.Default.Name
}
case *Case:
e.Kind = CaseEntry
case *AnyData:
e.Kind = AnyDataEntry
case *AnyXML:
e.Kind = AnyXMLEntry
case *Input:
e.Kind = InputEntry
case *Output:
e.Kind = OutputEntry
case *Notification:
e.Kind = NotificationEntry
case *Deviate:
e.Kind = DeviateEntry
}
// Use Elem to get the Value of structure that n is pointing to, not
// the Value of the pointer.
v := reflect.ValueOf(n).Elem()
t := v.Type()
found := false
for i := t.NumField() - 1; i > 0; i-- {
f := t.Field(i)
yang := f.Tag.Get("yang")
if yang == "" {
continue
}
fv := v.Field(i)
name := strings.Split(yang, ",")[0]
switch name {
case "":
e.addError(fmt.Errorf("%s: nil statement", Source(n)))
case "config":
e.Config, err = tristateValue(fv.Interface())
e.addError(err)
case "description":
if v := fv.Interface().(*Value); v != nil {
e.Description = v.Name
}
case "prefix":
if v := fv.Interface().(*Value); v != nil {
e.Prefix = v
}
case "action":
for _, r := range fv.Interface().([]*Action) {
e.add(r.Name, ToEntry(r))
}
case "augment":
for _, a := range fv.Interface().([]*Augment) {
ne := ToEntry(a)
ne.Parent = e
e.Augments = append(e.Augments, ne)
}
case "anydata":
for _, a := range fv.Interface().([]*AnyData) {
e.add(a.Name, ToEntry(a))
}
case "anyxml":
for _, a := range fv.Interface().([]*AnyXML) {
e.add(a.Name, ToEntry(a))
}
case "case":
for _, a := range fv.Interface().([]*Case) {
e.add(a.Name, ToEntry(a))
}
case "choice":
for _, a := range fv.Interface().([]*Choice) {
e.add(a.Name, ToEntry(a))
}
case "container":
for _, a := range fv.Interface().([]*Container) {
e.add(a.Name, ToEntry(a))
}
case "grouping":
for _, a := range fv.Interface().([]*Grouping) {
// We just want to parse the grouping to
// collect errors.
e.importErrors(ToEntry(a))
}
case "import":
// Apparently import only makes types and such
// available. There is nothing else for us to do.
case "include":
for _, a := range fv.Interface().([]*Include) {
// Handle circular dependencies between submodules. This can occur in
// two ways:
// - Where submodule A imports submodule B, and vice versa then the
// whilst processing A we will also try and process A (learnt via
// B). The default case of the switch handles this case.
// - Where submodule A imports submodule B that imports C, which also
// imports A, then we need to check whether we already have merged
// the specified module during this parse attempt. We check this
// against a map of merged submodules.
// The key of the map used is a synthesised value which is formed by
// concatenating the name of this node and the included submodule,
// separated by a ":".
srcToIncluded := a.Module.Name + ":" + n.NName()
includedToSrc := n.NName() + ":" + a.Module.Name
switch {
case mergedSubmodule[srcToIncluded]:
// We have already merged this module, so don't try and do it
// again.
continue
case !mergedSubmodule[includedToSrc] && a.Module.NName() != n.NName():
// We have not merged A->B, and B != B hence go ahead and merge.
includedToParent := a.Module.Name + ":" + a.Module.BelongsTo.Name
if mergedSubmodule[includedToParent] {
// Don't try and re-import submodules that have already been imported
// into the top-level module. Note that this ensures that we get to the
// top the tree (whichever the actual module for the chain of
// submodules is). The tracking of the immediate parent is achieved
// through 'key', which ensures that we do not end up in loops
// walking through a sub-cycle of the include graph.
continue
}
mergedSubmodule[srcToIncluded] = true
mergedSubmodule[includedToParent] = true
e.merge(a.Module.Prefix, nil, ToEntry(a.Module))
case ParseOptions.IgnoreSubmoduleCircularDependencies:
continue
default:
e.addError(fmt.Errorf("%s: has a circular dependency, importing %s", n.NName(), a.Module.NName()))
}
}
case "leaf":
for _, a := range fv.Interface().([]*Leaf) {
e.add(a.Name, ToEntry(a))
}
case "leaf-list":
for _, a := range fv.Interface().([]*LeafList) {
e.add(a.Name, ToEntry(a))
}
case "list":
for _, a := range fv.Interface().([]*List) {
e.add(a.Name, ToEntry(a))
}
case "key":
if v := fv.Interface().(*Value); v != nil {
e.Key = v.Name
}
case "notification":
for _, a := range fv.Interface().([]*Notification) {
e.add(a.Name, ToEntry(a))
}
case "rpc":
// TODO(borman): what do we do with these?
// seems fine to ignore them for now, we are
// just interested in the tree structure.
for _, r := range fv.Interface().([]*RPC) {
switch rpc := ToEntry(r); {
case rpc.RPC == nil:
// When "rpc" has no "input" or "output" children
rpc.RPC = &RPCEntry{}
fallthrough
default:
e.add(r.Name, rpc)
}
}
case "input":
if i := fv.Interface().(*Input); i != nil {
if e.RPC == nil {
e.RPC = &RPCEntry{}
}
in := ToEntry(i)
in.Parent = e
e.RPC.Input = in
e.RPC.Input.Name = "input"
e.RPC.Input.Kind = InputEntry
}
case "output":
if o := fv.Interface().(*Output); o != nil {
if e.RPC == nil {
e.RPC = &RPCEntry{}
}
out := ToEntry(o)
out.Parent = e
e.RPC.Output = out
e.RPC.Output.Name = "output"
e.RPC.Output.Kind = OutputEntry
}
case "identity":
if i := fv.Interface().([]*Identity); i != nil {
e.Identities = i
}
case "uses":
for _, a := range fv.Interface().([]*Uses) {
grouping := ToEntry(a)
e.merge(nil, nil, grouping)
if ParseOptions.StoreUses {
e.Uses = append(e.Uses, &UsesStmt{a, grouping.shallowDup()})
}
}
case "type":
// The type keyword is specific to deviate to change a type. Other type handling
// (e.g., leaf type resolution) is done outside of this case.
n, ok := n.(*Deviate)
if !ok {
e.addError(fmt.Errorf("unexpected type found, only valid under Deviate, is %T", n))
continue
}
if n.Type != nil {
if errs := n.Type.resolve(ms.typeDict); errs != nil {
e.addError(fmt.Errorf("deviation has unresolvable type, %v", errs))
continue
}
e.Type = n.Type.YangType
}
continue
// Keywords that do not need to be handled as an Entry as they are added
// to other dictionaries.
case "default":
if e.Kind == LeafEntry {
// default is handled separately for a leaf, but in a deviate statement
// we must deal with it here.
continue
}
d, ok := fv.Interface().(*Value)
if !ok {
e.addError(fmt.Errorf("%s: unexpected default type in %s:%s", Source(n), n.Kind(), n.NName()))
}
e.Default = d.asString()
case "typedef":
continue
case "deviation":
if a := fv.Interface().([]*Deviation); a != nil {
for _, d := range a {
e.Deviations = append(e.Deviations, &DeviatedEntry{
Entry: ToEntry(d),
DeviatedPath: d.Statement().Argument,
})
for _, sd := range d.Deviate {
if sd.Type != nil {
sd.Type.resolve(ms.typeDict)
}
}
}
}
case "deviate":
if a := fv.Interface().([]*Deviate); a != nil {
for _, d := range a {
de := ToEntry(d)
dt, ok := toDeviation[d.Statement().Argument]
if !ok {
e.addError(fmt.Errorf("%s: unknown deviation type in %s:%s", Source(n), n.Kind(), n.NName()))
continue
}
if e.Deviate == nil {
e.Deviate = map[deviationType][]*Entry{}
}
e.Deviate[dt] = append(e.Deviate[dt], de)
}
}
case "mandatory":
v, ok := fv.Interface().(*Value)
if !ok {
e.addError(fmt.Errorf("%s: did not get expected value type", Source(n)))
}
e.Mandatory, err = tristateValue(v)
e.addError(err)
case "max-elements", "min-elements":
if e.Kind != DeviateEntry {
continue
}
// we can get max-elements or min-elements in a deviate statement, so create the
// corresponding logic.
v, ok := fv.Interface().(*Value)
if !ok {
e.addError(fmt.Errorf("%s: max or min elements had wrong type, %s:%s", Source(n), n.Kind(), n.NName()))
continue
}
if e.ListAttr == nil {
e.ListAttr = &ListAttr{}
}
if name == "max-elements" {
e.ListAttr.MaxElements = v
} else {
e.ListAttr.MinElements = v
}
case "units":
v, ok := fv.Interface().(*Value)
if !ok {
e.addError(fmt.Errorf("%s: units had wrong type, %s:%s", Source(n), n.Kind(), n.NName()))
}
if v != nil {
e.Units = v.asString()
}
// TODO(borman): unimplemented keywords
case "belongs-to",
"contact",
"extension",
"feature",
"if-feature",
"must",
"namespace",
"ordered-by",
"organization",
"presence",
"reference",
"revision",
"status",
"unique",
"when",
"yang-version":
e.Extra[name] = append(e.Extra[name], fv.Interface())
continue
case "Ext", "Name", "Parent", "Statement":
// These are meta-keywords used internally
continue
default:
e.addError(fmt.Errorf("%s: unexpected statement: %s", Source(n), name))
continue
}
// We found at least one field.
found = true
}
if !found {
return newError(n, "%T: cannot be converted to a *Entry", n)
}
// If prefix isn't set, provide it based on our root node (module)
if e.Prefix == nil {
e.Prefix = getRootPrefix(e)
}
return e
}
func getModules(n Node) *Modules {
for n.ParentNode() != nil {
n = n.ParentNode()
}
return n.(*Module).modules
}
// getRootPrefix returns the prefix of e's root node (module)
func getRootPrefix(e *Entry) *Value {
if m := RootNode(e.Node); m != nil {
return m.getPrefix()
}
return nil
}
// Augment processes augments in e, return the number of augments processed
// and the augments skipped. If addErrors is true then missing augments will
// generate errors.
func (e *Entry) Augment(addErrors bool) (processed, skipped int) {
// Now process the augments we found
// NOTE(borman): is it possible this will fail if the augment refers
// to some removed sibling that has not been processed? Perhaps this
// should be done after the entire tree is built. Is it correct to
// assume augment paths are data tree paths and not schema tree paths?
// Augments can depend upon augments. We need to figure out how to
// order the augments (or just keep trying until we can make no further
// progress)
var sa []*Entry
for _, a := range e.Augments {
ae := a.Find(a.Name)
if ae == nil {
if addErrors {
e.errorf("%s: augment %s not found", Source(a.Node), a.Name)
}
skipped++
sa = append(sa, a)
continue
}
// Augments do not have a prefix we merge in, just a node.
// We retain the namespace from the original context of the
// augment since the nodes have this namespace even though they
// are merged into another entry.
processed++
ae.merge(nil, a.Namespace(), a)
ae.Augmented = append(ae.Augmented, a.shallowDup())
}
e.Augments = sa
return processed, skipped
}
// ApplyDeviate walks the deviations within the supplied entry, and applies them to the
// schema.
func (e *Entry) ApplyDeviate() []error {
var errs []error