/
main.go
160 lines (129 loc) · 4.61 KB
/
main.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
package main
import (
"fmt"
"github.com/metajar/netconf/netconf"
"github.com/metajar/netconf/netconf/message"
"golang.org/x/crypto/ssh"
"log"
"time"
)
/*
Adapted from https://github.com/openshift-telco/go-netconf-client/blob/main/examples/test-rpc.go
*/
func main() {
// java -jar lighty-notifications-device-15.0.1-SNAPSHOT.jar 12345
//testNotification()
// java -jar lighty-toaster-multiple-devices-15.0.1-SNAPSHOT.jar --starting-port 20000 --device-count 200 --thread-pool-size 200
testRPC()
time.Sleep(10 * time.Second)
}
func testNotification() {
notificationSession := createSession(12346)
callback := func(event netconf.Event) {
reply := event.Notification()
println(reply.RawReply)
}
notificationSession.CreateNotificationStream(1, "", "", "", callback)
triggerNotification := " <triggerDataNotification xmlns=\"yang:lighty:test:notifications\">\n <ClientId>0</ClientId>\n <Count>5</Count>\n <Delay>1</Delay>\n <Payload>just simple notification</Payload>\n </triggerDataNotification>"
rpc := message.NewRPC(triggerNotification)
notificationSession.SyncRPC(rpc, 1)
err := notificationSession.CreateNotificationStream(1, "", "", "", callback)
if err == nil {
panic("must fail")
}
d := message.NewCloseSession()
notificationSession.AsyncRPC(d, defaultLogRpcReplyCallback(d.MessageID))
notificationSession.Listener.Remove(message.NetconfNotificationStreamHandler)
notificationSession.Listener.WaitForMessages()
notificationSession.Close()
}
func testRPC() {
for i := 0; i < 2; i++ {
i := i
go func() {
number := 20000 + i
session := createSession(number)
defer session.Close()
execRPC(session)
}()
}
}
// Execute all types of RPC against the device
// Add a 100ms delay after each RPC to leave enough time for the device to reply
// Else, too many request and things get bad.
func execRPC(session *netconf.Session) {
// Get Config
g := message.NewGetConfig(message.DatastoreRunning, message.FilterTypeSubtree, "")
session.AsyncRPC(g, defaultLogRpcReplyCallback(g.MessageID))
time.Sleep(100 * time.Millisecond)
// Get
gt := message.NewGet("", "")
session.AsyncRPC(gt, defaultLogRpcReplyCallback(gt.MessageID))
time.Sleep(100 * time.Millisecond)
// Lock
l := message.NewLock(message.DatastoreCandidate)
session.AsyncRPC(l, defaultLogRpcReplyCallback(l.MessageID))
time.Sleep(100 * time.Millisecond)
// EditConfig
data := "<toaster xmlns=\"http://netconfcentral.org/ns/toaster\">\n <darknessFactor>750</darknessFactor>\n</toaster>"
e := message.NewEditConfig(message.DatastoreCandidate, message.DefaultOperationTypeMerge, data)
session.AsyncRPC(e, defaultLogRpcReplyCallback(e.MessageID))
time.Sleep(100 * time.Millisecond)
// Commit
c := message.NewCommit()
session.AsyncRPC(c, defaultLogRpcReplyCallback(c.MessageID))
time.Sleep(100 * time.Millisecond)
// Unlock
u := message.NewUnlock(message.DatastoreCandidate)
session.AsyncRPC(u, defaultLogRpcReplyCallback(u.MessageID))
time.Sleep(100 * time.Millisecond)
// RPC
d := " <make-toast xmlns=\"http://netconfcentral.org/ns/toaster\">\n <toasterDoneness>9</toasterDoneness>\n <toasterToastType>frozen-waffle</toasterToastType>\n </make-toast>"
rpc := message.NewRPC(d)
session.AsyncRPC(rpc, defaultLogRpcReplyCallback(rpc.MessageID))
time.Sleep(100 * time.Millisecond)
// RPCs
rpc0 := message.NewGetConfig(message.DatastoreRunning, "", "")
reply, _ := session.SyncRPC(rpc0, 1)
println(fmt.Printf("blabla %s\n", reply.RawReply))
rpc2 := message.NewRPC(d)
syncRPC, _ := session.SyncRPC(rpc2, 1)
println(fmt.Printf("blabla %s\n", syncRPC.RawReply))
rpc3 := message.NewRPC(d)
session.SyncRPC(rpc3, 1)
rpc4 := message.NewRPC(d)
session.SyncRPC(rpc4, 1)
// Close Session
d2 := message.NewCloseSession()
session.AsyncRPC(d2, defaultLogRpcReplyCallback(d2.MessageID))
session.Listener.WaitForMessages()
}
func createSession(port int) *netconf.Session {
sshConfig := &ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{ssh.Password("admin")},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
s, err := netconf.DialSSH(fmt.Sprintf("127.0.0.1:%d", port), sshConfig)
if err != nil {
log.Fatal(err)
}
capabilities := netconf.DefaultCapabilities
err = s.SendHello(&message.Hello{Capabilities: capabilities})
if err != nil {
log.Fatal(err)
}
return s
}
func defaultLogRpcReplyCallback(eventId string) netconf.Callback {
return func(event netconf.Event) {
reply := event.RPCReply()
if reply == nil {
println("Failed to execute RPC")
}
if event.EventID() == eventId {
println("Successfully executed RPC")
println(reply.RawReply)
}
}
}