Newer
Older
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
package csbi
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus"
)
// Repository provides access to yang model files.
type Repository interface {
// FindYANGFiles returns all YANG files matching the provided ModelData
FindYANGFiles(models []*gpb.ModelData) ([]string, []error)
// YANGPathsWithSuffix returns all YANG search paths with a '...' suffix
YANGPathsWithSuffix() ([]string, error)
}
// NewRepository returns a implementation of the Repository interface
func NewRepository(basePath string) Repository {
return &repo{
fs: &filesystem{
root: basePath,
},
}
}
type repo struct {
fs Filesystem
}
func (r *repo) YANGPathsWithSuffix() ([]string, error) {
pathSet := make(map[string]struct{})
paths, err := r.fs.Glob(".yang")
out := make([]string, 0)
if err != nil {
return nil, err
}
for _, p := range paths {
p = p[:strings.LastIndex(p, "/")]
_, exist := pathSet[p]
if !exist {
pathSet[p] = struct{}{}
out = append(out, filepath.Join(p, "..."))
}
}
return out, nil
}
func (r *repo) FindYANGFiles(models []*gpb.ModelData) ([]string, []error) {
filePaths := make([]string, 0)
errs := make([]error, 0)
for _, model := range models {
if !(strings.Contains(model.Name, "augments") || strings.Contains(model.Name, "deviations")) {
path, err := searchYANGFile(r.fs, model.Name, model.Organization, model.Version)
if err != nil {
log.Error(err)
errs = append(errs, err)
continue
}
filePaths = append(filePaths, path)
} else {
log.WithFields(log.Fields{
"model": model.Name,
}).Info("augments and deviations are ignored")
}
}
return filePaths, errs
}
// The Filesystem interface defines the implementation required for any YANG
// model repository data source.
type Filesystem interface {
fs.FS
fs.GlobFS
}
type filesystem struct {
root string
}
func (osfs *filesystem) Open(name string) (fs.File, error) {
if err := osfs.fsValid(name); err != nil {
return nil, err
}
return os.Open(name)
}
func (osfs *filesystem) ReadDir(name string) ([]fs.DirEntry, error) {
if err := osfs.fsValid(name); err != nil {
return nil, err
}
return os.ReadDir(name)
}
func (osfs *filesystem) ReadFile(name string) ([]byte, error) {
if err := osfs.fsValid(name); err != nil {
return nil, err
}
return os.ReadFile(name)
}
func (osfs *filesystem) Stat(name string) (fs.FileInfo, error) {
if err := osfs.fsValid(name); err != nil {
return nil, err
}
return os.Stat(name)
}
func (osfs *filesystem) Glob(pattern string) (paths []string, err error) {
defer func() {
if e := recover(); e != nil {
err = e.(error) //nolint:errcheck
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
paths = nil
}
}()
_, err = osfs.ReadDir(osfs.root)
if err != nil {
return nil, err
}
paths = make([]string, 0)
re := regexp.MustCompile(pattern)
if err := filepath.WalkDir(osfs.root, func(path string, d fs.DirEntry, err error) error {
if re.Match([]byte(d.Name())) {
p := filepath.Join(path)
paths = append(paths, p)
}
return nil
}); err != nil {
return nil, err
}
return paths, nil
}
func (osfs *filesystem) fsValid(name string) error {
fullName := filepath.Join(osfs.root, name)
if !fs.ValidPath(fullName) {
return &fs.PathError{Err: fs.ErrInvalid}
}
return nil
}
func findBestMatch(paths []string, org, version string) (string, error) {
out := make([]string, 0)
for _, path := range paths {
if strings.Contains(path, org) {
out = append(out, path)
}
}
if len(out) > 0 {
return out[0], nil
}
return paths[0], nil
}
func searchYANGFile(fsys Filesystem, name, org, version string) (string, error) {
paths, err := fsys.Glob(name + ".yang")
if err != nil {
return "", err
}
if len(paths) == 0 {
return "", fmt.Errorf("did not find file for %v in %v", name, fsys)
}
return findBestMatch(paths, org, version)
}