-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
208 lines (174 loc) · 5 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
package netconf
import (
"encoding/xml"
"fmt"
"strings"
"time"
"golang.org/x/exp/slices"
)
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.
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)
}
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, _ xml.StartElement) error {
if msg.Operation == nil {
return fmt.Errorf("operation cannot be nil")
}
type rpcMsg request
inner := rpcMsg(*msg)
return e.Encode(&inner)
}
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"`
rpc []byte `xml:"-"`
}
// Decode decodes the Reply into the v
func (r Reply) Decode(v interface{}) error {
return xml.Unmarshal(r.rpc, v)
}
// String returns the string representation of the Reply inside <data> or some other element
func (r Reply) String() string {
return string(r.rpc)
}
// Raw returns full rpc Reply
func (r Reply) Raw() []byte {
return r.rpc
}
func (r Reply) Err(severity ...ErrSeverity) error {
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"`
rpc []byte `xml:"-"`
}
// Decode decodes the Notification into the v
func (r Notification) Decode(v interface{}) error {
return xml.Unmarshal(r.rpc, v)
}
// String returns the string representation of the Notification inside <notification> element
func (r Notification) String() string {
return string(r.rpc)
}
// Raw returns full rpc Notification
func (r Notification) Raw() []byte {
return r.rpc
}
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" json:"error-type"`
Tag ErrTag `xml:"error-tag" json:"error-tag"`
Severity ErrSeverity `xml:"error-severity" json:"error-severity"`
AppTag string `xml:"error-app-tag,omitempty" json:"error-app-tag,omitempty"`
Path string `xml:"error-path,omitempty" json:"error-path,omitempty"`
Message string `xml:"error-message,omitempty" json:"error-message,omitempty"`
Info RawXML `xml:"error-info,omitempty" json:"error-info,omitempty"`
}
func (e RPCError) Error() string {
if e.Message != "" {
return e.Message
} else {
return string(e.Info)
}
}
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) {
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
}