-
Notifications
You must be signed in to change notification settings - Fork 10
/
local.go
115 lines (99 loc) · 2.54 KB
/
local.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
package device
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/freeconf/yang/fc"
"github.com/freeconf/yang/meta"
"github.com/freeconf/yang/node"
"github.com/freeconf/yang/nodeutil"
"github.com/freeconf/yang/parser"
"github.com/freeconf/yang/source"
)
type Local struct {
browsers map[string]*node.Browser
schemaSource source.Opener
uiSource source.Opener
}
func New(schemaSource source.Opener) *Local {
return &Local{
schemaSource: schemaSource,
browsers: make(map[string]*node.Browser),
}
}
func NewWithUi(schemaSource source.Opener, uiSource source.Opener) *Local {
return &Local{
schemaSource: schemaSource,
uiSource: uiSource,
browsers: make(map[string]*node.Browser),
}
}
func (self *Local) SchemaSource() source.Opener {
return self.schemaSource
}
func (self *Local) UiSource() source.Opener {
return self.uiSource
}
func (self *Local) Modules() map[string]*meta.Module {
mods := make(map[string]*meta.Module)
for _, b := range self.browsers {
mods[b.Meta.Ident()] = b.Meta
}
return mods
}
func (self *Local) Browser(module string) (*node.Browser, error) {
return self.browsers[module], nil
}
func (self *Local) Close() {
}
func (self *Local) Add(module string, n node.Node) error {
m, err := parser.LoadModule(self.schemaSource, module)
if err != nil {
return err
}
self.browsers[module] = node.NewBrowser(m, n)
return nil
}
func (self *Local) AddSource(module string, src func() node.Node) error {
m, err := parser.LoadModule(self.schemaSource, module)
if err != nil {
return err
}
self.browsers[module] = node.NewBrowserSource(m, src)
return nil
}
func (self *Local) AddBrowser(b *node.Browser) {
self.browsers[b.Meta.Ident()] = b
}
func (self *Local) ApplyStartupConfig(config io.Reader) error {
var cfg map[string]interface{}
if err := json.NewDecoder(config).Decode(&cfg); err != nil {
return err
}
return self.ApplyStartupConfigData(cfg)
}
func (self *Local) ApplyStartupConfigData(config map[string]interface{}) error {
for module, data := range config {
b, err := self.Browser(module)
if err != nil {
return err
}
if b == nil {
return fmt.Errorf("%w. browser not found: %s", fc.NotFoundError, module)
}
moduleCfg := data.(map[string]interface{})
if err := b.Root().UpsertFromSetDefaults(nodeutil.ReflectChild(moduleCfg)); err != nil {
return err
}
}
return nil
}
func (self *Local) ApplyStartupConfigFile(fname string) error {
cfgRdr, err := os.Open(fname)
defer cfgRdr.Close()
if err != nil {
return err
}
return self.ApplyStartupConfig(cfgRdr)
}