forked from Juniper/go-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
71 lines (57 loc) · 1.48 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
package netconf
import "encoding/xml"
type Session struct {
ID int
ServerCapabilities []Capability
Transport Transport
ErrOnWarnings bool
}
func (s *Session) Close() error {
return s.Transport.Close()
}
func NewSession(t Transport) (*Session, error) {
// recieve hello message from remote
remoteHello, err := t.ReceiveHello()
if err != nil {
return nil, err
}
// send hello message with default capabilities
capabilities := []Capability{CapabilityNetconfBase}
if err = t.SendHello(&TransportHelloMessage{Capabilities: capabilities}); err != nil {
return nil, err
}
return &Session{
ID: remoteHello.SessionID,
ServerCapabilities: remoteHello.Capabilities,
Transport: t,
}, nil
}
func (s *Session) Exec(methods ...RPCMethod) (*RPCReply, error) {
// prepare RPC message
req, err := xml.Marshal(NewRPCMessage(methods))
if err != nil {
return nil, err
}
if err = s.Transport.Send(req); err != nil {
return nil, err
}
res, err := s.Transport.Recieve()
if err != nil {
return nil, err
}
// unmarshal the reply
reply := &RPCReply{RawReply: string(res)}
if err := xml.Unmarshal(res, reply); err != nil {
return nil, err
}
// handle RPC errors
if reply.Errors != nil {
// only act upon non-warning errors unless we have been instructed to fail on warnings
for _, rpcErr := range reply.Errors {
if rpcErr.Severity == "error" || s.ErrOnWarnings {
return reply, &rpcErr
}
}
}
return reply, nil
}