This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
97 lines (88 loc) · 2.41 KB
/
api.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
package gateway
import (
"strings"
"github.com/freeconf/restconf"
"github.com/freeconf/yang/val"
"github.com/freeconf/yang/node"
"github.com/freeconf/yang/nodeutil"
)
func CallHomeServer(registrar *LocalRegistrar) node.Node {
return &nodeutil.Basic{
OnAction: func(r node.ActionRequest) (node.Node, error) {
switch r.Meta.Ident() {
case "register":
var reg Registration
if err := r.Input.InsertInto(regNode(®)).LastErr; err != nil {
return nil, err
}
ctx := r.Selection.Context
if regAddr, hasRegAddr := ctx.Value(restconf.RemoteIpAddressKey).(string); hasRegAddr {
reg.Address = strings.Replace(reg.Address, "{REQUEST_ADDRESS}", regAddr, 1)
}
registrar.RegisterDevice(reg.DeviceId, reg.Address)
return nil, nil
case "unregister":
// TODO : lookup registered address and remove that entry
return nil, nil
}
return nil, nil
},
}
}
func RegistrarNode(registrar *LocalRegistrar) node.Node {
return &nodeutil.Basic{
OnChild: func(r node.ChildRequest) (node.Node, error) {
switch r.Meta.Ident() {
case "registration":
return registrationsNode(registrar), nil
}
return nil, nil
},
OnNotify: func(r node.NotifyRequest) (node.NotifyCloser, error) {
switch r.Meta.Ident() {
case "update":
sub := registrar.OnRegister(func(reg Registration) {
r.Send(regNode(®))
})
return sub.Close, nil
}
return nil, nil
},
}
}
func registrationsNode(registrar *LocalRegistrar) node.Node {
// assume local registrar, need better way to iterate
index := node.NewIndex(registrar.regs)
return &nodeutil.Basic{
OnNextItem: func(r node.ListRequest) nodeutil.BasicNextItem {
var reg Registration
var found bool
return nodeutil.BasicNextItem{
GetByKey: func() error {
reg, found = registrar.LookupRegistration(r.Key[0].String())
return nil
},
GetByRow: func() ([]val.Value, error) {
if r.Row < registrar.RegistrationCount() {
if v := index.NextKey(r.Row); v != node.NO_VALUE {
id := v.String()
reg, found = registrar.LookupRegistration(id)
key := []val.Value{val.String(reg.DeviceId)}
return key, nil
}
}
return nil, nil
},
Node: func() (node.Node, error) {
if found {
return nodeutil.ReflectChild(®), nil
}
return nil, nil
},
}
},
}
}
func regNode(reg *Registration) node.Node {
return nodeutil.ReflectChild(reg)
}