-
Notifications
You must be signed in to change notification settings - Fork 109
/
session.go
79 lines (61 loc) · 1.65 KB
/
session.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
import (
"encoding/xml"
)
// Session defines the necessary components for a NETCONF session
type Session struct {
Transport Transport
SessionID int
ServerCapabilities []string
ErrOnWarning bool
}
// Close is used to close and end a transport session
func (s *Session) Close() error {
return s.Transport.Close()
}
// Exec is used to execute an RPC method or methods
func (s *Session) Exec(methods ...RPCMethod) (*RPCReply, error) {
rpc := NewRPCMessage(methods)
request, err := xml.Marshal(rpc)
if err != nil {
return nil, err
}
header := []byte(xml.Header)
request = append(header, request...)
log.Debugf("REQUEST: %s\n", request)
err = s.Transport.Send(request)
if err != nil {
return nil, err
}
rawXML, err := s.Transport.Receive()
if err != nil {
return nil, err
}
log.Debugf("REPLY: %s\n", rawXML)
reply := &RPCReply{}
reply.RawReply = string(rawXML)
if err := xml.Unmarshal(rawXML, reply); err != nil {
return nil, err
}
if reply.Errors != nil {
// We have errors, lets see if it's a warning or an error.
for _, rpcErr := range reply.Errors {
if rpcErr.Severity == "error" || s.ErrOnWarning {
return reply, &rpcErr
}
}
}
return reply, nil
}
// NewSession creates a new NETCONF session using the provided transport layer.
func NewSession(t Transport) *Session {
s := new(Session)
s.Transport = t
// Receive Servers Hello message
serverHello, _ := t.ReceiveHello()
s.SessionID = serverHello.SessionID
s.ServerCapabilities = serverHello.Capabilities
// Send our hello using default capabilities.
t.SendHello(&HelloMessage{Capabilities: DefaultCapabilities})
return s
}