-
Notifications
You must be signed in to change notification settings - Fork 111
/
rpc.go
95 lines (80 loc) · 2.59 KB
/
rpc.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
package netconf
import (
"bytes"
"encoding/xml"
"fmt"
)
// RPCMessage represents an RPC Message to be sent.
type RPCMessage struct {
MessageID string
Methods []RPCMethod
}
// NewRPCMessage generates a new RPC Message structure with the provided methods
func NewRPCMessage(methods []RPCMethod) *RPCMessage {
return &RPCMessage{
MessageID: uuid(),
Methods: methods,
}
}
// MarshalXML marshals the NETCONF XML data
func (m *RPCMessage) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
var buf bytes.Buffer
for _, method := range m.Methods {
buf.WriteString(method.MarshalMethod())
}
data := struct {
MessageID string `xml:"message-id,attr"`
Xmlns string `xml:"xmlns,attr"`
Methods []byte `xml:",innerxml"`
}{
m.MessageID,
"urn:ietf:params:xml:ns:netconf:base:1.0",
buf.Bytes(),
}
// Wrap the raw XML (data) into <rpc>...</rpc> tags
start.Name.Local = "rpc"
return e.EncodeElement(data, start)
}
// RPCReply defines a reply to a RPC request
type RPCReply struct {
XMLName xml.Name `xml:"rpc-reply"`
Errors []RPCError `xml:"rpc-error,omitempty"`
Data string `xml:",innerxml"`
Ok bool `xml:",omitempty"`
RawReply string `xml:"-"`
}
// RPCError defines an error reply to a RPC request
type RPCError struct {
Type string `xml:"error-type"`
Tag string `xml:"error-tag"`
Severity string `xml:"error-severity"`
Path string `xml:"error-path"`
Message string `xml:"error-message"`
Info string `xml:",innerxml"`
}
// Error generates a string representation of the provided RPC error
func (re *RPCError) Error() string {
return fmt.Sprintf("netconf rpc [%s] '%s'", re.Severity, re.Message)
}
// RPCMethod defines the interface for creating an RPC method.
type RPCMethod interface {
MarshalMethod() string
}
// RawMethod defines how a raw text request will be responded to
type RawMethod string
// MarshalMethod converts the method's output into a string
func (r RawMethod) MarshalMethod() string {
return string(r)
}
// MethodLock files a NETCONF lock target request with the remote host
func MethodLock(target string) RawMethod {
return RawMethod(fmt.Sprintf("<lock><target><%s/></target></lock>", target))
}
// MethodUnlock files a NETCONF unlock target request with the remote host
func MethodUnlock(target string) RawMethod {
return RawMethod(fmt.Sprintf("<unlock><target><%s/></target></unlock>", target))
}
// MethodGetConfig files a NETCONF get-config source request with the remote host
func MethodGetConfig(source string) RawMethod {
return RawMethod(fmt.Sprintf("<get-config><source><%s/></source></get-config>", source))
}