forked from Juniper/go-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
72 lines (55 loc) · 1.34 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
package netconf
import (
"encoding/xml"
)
type Session struct {
Transport Transport
SessionID int
ServerCapabilities []string
ErrOnWarning bool
}
func (s *Session) Close() error {
return s.Transport.Close()
}
func (s *Session) Exec(methods ...RPCMethod) (*RPCReply, error) {
rpc := NewRpcMessage(methods)
request, err := xml.Marshal(rpc)
if err != nil {
return nil, err
}
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
}
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: DEFAULT_CAPABILITIES})
return s
}