forked from nemith/netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capability.go
79 lines (67 loc) · 2.16 KB
/
capability.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
package netconf
const (
baseCap = "urn:ietf:params:netconf:base"
stdCapPrefix = "urn:ietf:params:netconf:capability"
)
// DefaultCapabilities are the capabilities sent by the client during the hello
// exchange by the server.
var DefaultCapabilities = []string{
"urn:ietf:params:netconf:base:1.0",
"urn:ietf:params:netconf:base:1.1",
// XXX: these seems like server capabilities and i don't see why
// a client would need to send them
// "urn:ietf:params:netconf:capability:writable-running:1.0",
// "urn:ietf:params:netconf:capability:candidate:1.0",
// "urn:ietf:params:netconf:capability:confirmed-commit:1.0",
// "urn:ietf:params:netconf:capability:rollback-on-error:1.0",
// "urn:ietf:params:netconf:capability:startup:1.0",
// "urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp",
// "urn:ietf:params:netconf:capability:validate:1.0",
// "urn:ietf:params:netconf:capability:xpath:1.0",
// "urn:ietf:params:netconf:capability:notification:1.0",
// "urn:ietf:params:netconf:capability:interleave:1.0",
// "urn:ietf:params:netconf:capability:with-defaults:1.0",
}
// ExpandCapability will automatically add the standard capability prefix of
// `urn:ietf:params:netconf:capability` if not already present.
func ExpandCapability(s string) string {
if s == "" {
return ""
}
if s[0] != ':' {
return s
}
return stdCapPrefix + s
}
// XXX: may want to expose this type publicly in the future when the api has
// stabilized?
type capabilitySet struct {
caps map[string]struct{}
}
func newCapabilitySet(capabilities ...string) capabilitySet {
cs := capabilitySet{
caps: make(map[string]struct{}),
}
cs.Add(capabilities...)
return cs
}
func (cs *capabilitySet) Add(capabilities ...string) {
for _, cap := range capabilities {
cap = ExpandCapability(cap)
cs.caps[cap] = struct{}{}
}
}
func (cs capabilitySet) Has(s string) bool {
// XXX: need to figure out how to handle versions (i.e always map to 1.0 or
// map to latest/any?)
s = ExpandCapability(s)
_, ok := cs.caps[s]
return ok
}
func (cs capabilitySet) All() []string {
out := make([]string, 0, len(cs.caps))
for cap := range cs.caps {
out = append(out, cap)
}
return out
}