forked from nemith/netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
226 lines (193 loc) · 5.86 KB
/
msg.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
116
117
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package netconf
import (
"encoding/xml"
"fmt"
"strings"
"time"
"golang.org/x/exp/slices"
)
// RawXML captures the raw xml for the given element. Used to process certain
// elements later.
type RawXML []byte
func (x *RawXML) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var inner struct {
Data []byte `xml:",innerxml"`
}
if err := d.DecodeElement(&inner, &start); err != nil {
return err
}
*x = inner.Data
return nil
}
// MarshalXML implements xml.Marshaller. Raw XML is passed verbatim, errors and
// all.
func (x *RawXML) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
inner := struct {
Data []byte `xml:",innerxml"`
}{
Data: []byte(*x),
}
return e.EncodeElement(&inner, start)
}
// helloMsg maps the xml value of the <hello> message in RFC6241
type helloMsg struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 hello"`
SessionID uint64 `xml:"session-id,omitempty"`
Capabilities []string `xml:"capabilities>capability"`
}
// request maps the xml value of <rpc> in RFC6241
type request struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc"`
MessageID uint64 `xml:"message-id,attr"`
Operation any `xml:",innerxml"`
}
func (msg *request) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if msg.Operation == nil {
return fmt.Errorf("operation cannot be nil")
}
// TODO: validate operation is named?
// alias the type to not cause recursion calling e.Encode
type rpcMsg request
inner := rpcMsg(*msg)
return e.Encode(&inner)
}
// Reply maps the xml value of <rpc-reply> in RFC6241
type Reply struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply"`
MessageID uint64 `xml:"message-id,attr"`
Errors RPCErrors `xml:"rpc-error,omitempty"`
Body []byte `xml:",innerxml"`
}
// Decode will decode the body of a reply into a value pointed to by v. This is
// a simple wrapper around xml.Unmarshal.
func (r Reply) Decode(v interface{}) error {
return xml.Unmarshal(r.Body, v)
}
// Err will return go error(s) from a Reply that are of the given severities. If
// no severity is given then it defaults to `ErrSevError`.
//
// If one error is present then the underlyign type is `RPCError`. If more than
// one error exists than the underlying type is `[]RPCError`
//
// Example
// get all errors with severity of error
//
// if err := reply.Err(ErrSevError); err != nil { /* ... */ }
//
// or
//
// if err := reply.Err(); err != nil { /* ... */ }
//
// get all errors with severity of only warning
//
// if err := reply.Err(ErrSevWarning); err != nil { /* ... */ }
//
// get all errors
//
// if err := reply.Err(ErrSevWarning, ErrSevError); err != nil { /* ... */ }
func (r Reply) Err(severity ...ErrSeverity) error {
// fast escape for no errors
if len(r.Errors) == 0 {
return nil
}
errs := r.Errors.Filter(severity...)
switch len(errs) {
case 0:
return nil
case 1:
return errs[0]
default:
return errs
}
}
type Notification struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:notification:1.0 notification"`
EventTime time.Time `xml:"eventTime"`
Body []byte `xml:",innerxml"`
}
// Decode will decode the body of a noticiation into a value pointed to by v.
// This is a simple wrapper around xml.Unmarshal.
func (r Notification) Decode(v interface{}) error {
return xml.Unmarshal(r.Body, v)
}
type ErrSeverity string
const (
SevError ErrSeverity = "error"
SevWarning ErrSeverity = "warning"
)
type ErrType string
const (
ErrTypeTransport ErrType = "transport"
ErrTypeRPC ErrType = "rpc"
ErrTypeProtocol ErrType = "protocol"
ErrTypeApp ErrType = "app"
)
type ErrTag string
const (
ErrInUse ErrTag = "in-use"
ErrInvalidValue ErrTag = "invalid-value"
ErrTooBig ErrTag = "too-big"
ErrMissingAttribute ErrTag = "missing-attribute"
ErrBadAttribute ErrTag = "bad-attribute"
ErrUnknownAttribute ErrTag = "unknown-attribute"
ErrMissingElement ErrTag = "missing-element"
ErrBadElement ErrTag = "bad-element"
ErrUnknownElement ErrTag = "unknown-element"
ErrUnknownNamespace ErrTag = "unknown-namespace"
ErrAccesDenied ErrTag = "access-denied"
ErrLockDenied ErrTag = "lock-denied"
ErrResourceDenied ErrTag = "resource-denied"
ErrRollbackFailed ErrTag = "rollback-failed"
ErrDataExists ErrTag = "data-exists"
ErrDataMissing ErrTag = "data-missing"
ErrOperationNotSupported ErrTag = "operation-not-supported"
ErrOperationFailed ErrTag = "operation-failed"
ErrPartialOperation ErrTag = "partial-operation"
ErrMalformedMessage ErrTag = "malformed-message"
)
type RPCError struct {
Type ErrType `xml:"error-type"`
Tag ErrTag `xml:"error-tag"`
Severity ErrSeverity `xml:"error-severity"`
AppTag string `xml:"error-app-tag,omitempty"`
Path string `xml:"error-path,omitempty"`
Message string `xml:"error-message,omitempty"`
Info RawXML `xml:"error-info,omitempty"`
}
func (e RPCError) Error() string {
return fmt.Sprintf("netconf error: %s %s: %s", e.Type, e.Tag, e.Message)
}
type RPCErrors []RPCError
func (errs RPCErrors) Filter(severity ...ErrSeverity) RPCErrors {
if len(errs) == 0 {
return nil
}
if len(severity) == 0 {
severity = []ErrSeverity{SevError}
}
filteredErrs := make(RPCErrors, 0, len(errs))
for _, err := range errs {
if !slices.Contains(severity, err.Severity) {
continue
}
filteredErrs = append(filteredErrs, err)
}
return filteredErrs
}
func (errs RPCErrors) Error() string {
var sb strings.Builder
for i, err := range errs {
if i > 0 {
sb.WriteRune('\n')
}
sb.WriteString(err.Error())
}
return sb.String()
}
func (errs RPCErrors) Unwrap() []error {
boxedErrs := make([]error, len(errs))
for i, err := range errs {
boxedErrs[i] = err
}
return boxedErrs
}