-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice.go
65 lines (57 loc) · 1.88 KB
/
device.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
package lang
import (
"context"
"io"
"github.com/freeconf/lang/pb"
"github.com/freeconf/restconf/client"
"github.com/freeconf/restconf/device"
"github.com/freeconf/yang/node"
)
type DeviceService struct {
pb.UnimplementedDeviceServer
d *Driver
}
func (s *DeviceService) NewDevice(ctx context.Context, req *pb.NewDeviceRequest) (*pb.NewDeviceResponse, error) {
ypath := resolveOpener(s.d.handles, req.YangPathSourceHnd)
d := device.New(ypath)
resp := pb.NewDeviceResponse{
DeviceHnd: s.d.handles.Put(d),
}
return &resp, nil
}
func (s *DeviceService) DeviceAddBrowser(ctx context.Context, req *pb.DeviceAddBrowserRequest) (*pb.DeviceAddBrowserResponse, error) {
d := s.d.handles.Require(req.DeviceHnd).(*device.Local)
b := s.d.handles.Require(req.BrowserHnd).(*node.Browser)
d.AddBrowser(b)
return &pb.DeviceAddBrowserResponse{}, nil
}
func (s *DeviceService) DeviceGetBrowser(ctx context.Context, req *pb.DeviceGetBrowserRequest) (*pb.DeviceGetBrowserResponse, error) {
d := s.d.handles.Require(req.DeviceHnd).(device.Device)
b, err := d.Browser(req.ModuleIdent)
if err != nil {
return nil, err
}
resp := pb.DeviceGetBrowserResponse{
BrowserHnd: s.d.handles.Hnd(b),
}
return &resp, nil
}
func (s *DeviceService) ApplyStartupConfig(ctx context.Context, req *pb.ApplyStartupConfigRequest) (*pb.ApplyStartupConfigResponse, error) {
d := s.d.handles.Require(req.DeviceHnd).(*device.Local)
rdr := s.d.handles.Require(req.StreamHnd).(io.Reader)
err := d.ApplyStartupConfig(rdr)
resp := pb.ApplyStartupConfigResponse{}
return &resp, err
}
func (s *DeviceService) Client(ctx context.Context, req *pb.ClientRequest) (*pb.ClientResponse, error) {
ypath := resolveOpener(s.d.handles, req.YpathHnd)
proto := client.ProtocolHandler(ypath)
dev, err := proto(req.Address)
if err != nil {
return nil, err
}
resp := pb.ClientResponse{
DeviceHnd: s.d.handles.Hnd(dev),
}
return &resp, nil
}