-
Notifications
You must be signed in to change notification settings - Fork 0
/
netconf_lib.go
745 lines (687 loc) · 22.5 KB
/
netconf_lib.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package main
// package lib
import (
"bytes"
"context"
"encoding/xml"
"fmt"
// "netconf-go/internal/completions"
"netconf-go/internal/types"
"netconf-go/internal/xmlstore"
"os"
"sort"
"strings"
"time"
netconf "github.com/nemith/netconf"
"github.com/openconfig/goyang/pkg/yang"
log "github.com/sirupsen/logrus"
)
type cfgDatastore int
const (
running cfgDatastore = iota
candidate = 1
runningInheritance = 2
)
const (
newTokens = 0
replaceLast = 1
)
type yangReply struct {
XMLName xml.Name `xml:"data"`
Rest string `xml:",chardata"`
}
type netconfPathElement struct {
name string
value *string
delete bool
}
type netconfRequest struct {
ncEntry *yang.Entry
NetConfPath []netconfPathElement
Value string
reqType types.RequestType
store *xmlstore.XMLStore
}
type schemaReply struct {
XMLName xml.Name `xml:"data"`
Text string `xml:",chardata"`
NetconfState struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Schemas struct {
Text string `xml:",chardata"`
Schema []struct {
Text string `xml:",chardata"`
Identifier string `xml:"identifier"`
Version string `xml:"version"`
Format string `xml:"format"`
Namespace string `xml:"namespace"`
Location string `xml:"location"`
} `xml:"schema"`
} `xml:"schemas"`
} `xml:"netconf-state"`
}
var ms *yang.Modules = yang.NewModules()
var mods = map[string]*yang.Module{}
var modNames2 []string
var globalSession *netconf.Session
func listYang(path string) ([]string, int) {
var didAugment bool = false
var currentPrefix string
var returnType = newTokens
log.Debugf("listYang called on path: %s", path)
// yang.ParseOptions.IgnoreSubmoduleCircularDependencies = true
names := make([]string, 0)
tokens := strings.Fields(path)
log.Debugf("tokens: %d, %v", len(tokens), tokens)
if len(tokens) >= 2 {
// We have a module name; check for partial or incorrect
if i := sort.SearchStrings(modNames2, tokens[1]); i == len(modNames2) || modNames2[i] != tokens[1] {
log.Debugf("didn't find %s in %v, returning all, 1", tokens[1], len(modNames2))
return modNames2, returnType
}
if mods[tokens[1]] == nil {
mods[tokens[1]] = getYangModule(globalSession, tokens[1])
}
mod := mods[tokens[1]]
if mod == nil {
log.Debugf("didn't find %s in %v, returning all, 2", tokens[1], len(mods))
return modNames2, returnType
}
entry := yang.ToEntry(mod)
currentPrefix = mod.Namespace.Name
// println("currentPrefix", currentPrefix)
var deletedLastToken bool = false
for _, e := range tokens[2:] {
if entry != nil && e != "" {
log.Debugf("Foo: e %v kind %v %v\n", e, entry.Kind, entry)
if entry.Kind == yang.DirectoryEntry {
prevEntry := entry
// Check if a module prefix has been specified and if so strip it.
if strings.Contains(e, "@") {
e = strings.Split(e, "@")[1]
}
entry = entry.Dir[e]
if entry == nil && prevEntry.RPC != nil {
entry = prevEntry.RPC.Input.Dir[e]
}
if entry == nil {
log.Debugf("Couldn't find %v in %v", e, prevEntry.Dir)
entry = prevEntry
if entry.IsList() {
// Assume this is a key value.
// @@@ Check whether list key has been specified or not
i := strings.Index(e, "=")
// fmt.Printf("Compare %v to %v, %d, %d\n", e, entry.Key, i, len(e))
// @@@ This is horrible, needs fixing, and messes up the web code.
// Doesn't have an equals - assume this is a partial string, ignore it an d return the whole dir
// OR
// = is last character of token OR
// We're on the last token and we don't have a space - not ready to advance yet - what if we remove this case?
if i == -1 ||
(i == len(e)-1 && !strings.HasSuffix(path, " ") && e == tokens[len(tokens)-1]) ||
(e == tokens[len(tokens)-1] && !strings.HasSuffix(path, " ")) {
tokens = tokens[:len(tokens)-1]
deletedLastToken = true
}
} else {
// Check for augment type path
// - get prefix - bit before colon
// - look up prefix in imports - gives you the module being augmented
if strings.Contains(e, ":") {
aug_tokens := strings.FieldsFunc(e,
func(r rune) bool {
return r == ':' || r == '/'
},
)
// For now, assume the augment is for the same
// module; check this explicitly to at least
// make this assumption obvious.
for i := 2; i < len(aug_tokens); i += 2 {
if !strings.HasPrefix(aug_tokens[0], aug_tokens[i]) {
// TODO do something better than panic here
panic("Augments with different prefixes not currently support: " + e)
}
}
// println("Possible augment prefix:", aug_tokens[0])
var augMod *yang.Import
for _, augMod = range mod.Import {
if augMod.Prefix.Name == aug_tokens[0] {
// println("Found aug module:", augMod.Name)
break
}
}
if augMod.Prefix.Name == aug_tokens[0] {
// println(augMod.Name, aug_tokens[1], aug_tokens[3])
m2 := yang.ToEntry(augMod.Module)
// Find entry in the augmented module, and
// set the tokens to point there.
entry = m2
tokens = []string{tokens[0], augMod.Name}
for i := 1; i < len(aug_tokens); i += 2 {
entry = entry.Dir[aug_tokens[i]]
tokens = append(tokens, aug_tokens[i])
}
didAugment = true
}
} else {
tokens = tokens[:len(tokens)-1]
}
}
}
}
if entry.IsList() {
log.Debugln("Found list: ", entry.Name, entry.Key)
}
}
}
if entry != nil {
log.Debugf("Entry: %v kind %v dir %v Errors: %v Augments: %v Augmented-by: %v Uses: %v", entry.Name, entry.Kind, entry.Dir, entry.Errors, entry.Augmented, entry.Augments, entry.Uses)
if entry.Prefix != nil {
// Need to store the prefix somewhere and add it when constructing the request.
var prefix_ns string
switch entry.Prefix.Parent.(type) {
case *yang.Module:
log.Debugln("Found prefix: ", entry.Prefix.Parent.(*yang.Module).Namespace.Name)
// println("Found prefix: ", entry.Prefix.Parent.(*yang.Module).Namespace.Name)
prefix_ns = entry.Prefix.Parent.(*yang.Module).Namespace.Name
case *yang.BelongsTo:
log.Debugln("Found prefix2: ", entry.Prefix.Parent.(*yang.BelongsTo).Name)
// println("Found prefix2: ", entry.Prefix.Parent.(*yang.BelongsTo).Name)
prefix_ns = entry.Prefix.Parent.(*yang.BelongsTo).Name
}
if currentPrefix != prefix_ns && !didAugment {
currentPrefix = prefix_ns
println("Changed prefix:", currentPrefix)
// Add prefix to current i.e. last token, if it doesn't
// already have one.
// @@@ Should check prefix matches?
if !strings.Contains(tokens[len(tokens)-1], "@") {
tokens[len(tokens)-1] = currentPrefix + "@" + tokens[len(tokens)-1]
didAugment = true
}
}
}
}
var prefix string
if !didAugment {
prefix = ""
} else {
prefix = strings.Join(tokens[1:], " ") + " "
}
if entry.IsList() {
// TODO Need to support multiple keys properly here
var keys []string
if entry.Key != "" {
keys = strings.Split(entry.Key, " ")
fmt.Printf("Enter list key (%s, %s, %v)\n", keys[0], entry.Dir[keys[0]].Description, entry.Dir[keys[0]].Type.Name)
}
// fmt.Printf("list key tokens: %v\n", tokens)
e := tokens[len(tokens)-1]
i := strings.Index(e, "=")
// fmt.Printf("Compare %v to %v, %d, %d\n", e, entry.Key, i, len(e))
// if i == -1 || i == len(e)-1 {
if (i == -1 || (!deletedLastToken && !strings.HasSuffix(path, " "))) &&
entry.Key != "" {
if entry.Dir[keys[0]].Type.Name == "Interface-name" ||
(entry.Name == "interface" && keys[0] == "name") {
intfs := GetInterfaces(globalSession)
println(intfs)
for _, intf := range intfs {
names = append(names, prefix+keys[0]+"="+intf)
}
} else if entry.Dir[keys[0]].Type.Name == "Node-id" {
nodes := GetNodes(globalSession)
println(nodes)
for _, node := range nodes {
names = append(names, prefix+keys[0]+"="+node)
}
} else {
names = append(names, prefix+keys[0]+"=")
}
} else {
for s := range entry.Dir {
names = append(names, s)
}
}
} else if entry != nil && entry.RPC != nil {
for s := range entry.RPC.Input.Dir {
names = append(names, strings.Join(tokens[1:], " ")+" "+s+"=")
}
} else if entry != nil && entry.Kind == yang.DirectoryEntry {
for s := range entry.Dir {
names = append(names, prefix+s)
}
} else if entry != nil && entry.Parent.Kind == yang.InputEntry {
// This is a leaf specifying the input for the RPC, prompt the user
// for input
fmt.Printf("Enter RPC input: %s\n", entry.Description)
names = append(names, entry.Name+"=")
returnType = replaceLast
}
for _, s := range mod.Augment {
log.Debugln("Mod augment: ", s.Name)
if !didAugment {
// This isn't quite right.
// names = append(names, strings.Join(tokens[1:], " ")+" "+s.Name)
names = append(names, s.Name)
}
}
// if len(tokens) > 3 {
// if len(tokens) > 4 {
// entry := yang.ToEntry(mod).Dir[tokens[2]]
// log.Debugf("Foo: %v\n", entry)
// } else {
// for s := range entry.Dir {
// log.Debugf("Foo: %v\n", s)
// names = append(names, tokens[1]+" "+tokens[2]+" "+s)
// }
// }
// } else {
// log.Debugf("Yang mod: type %T", mod)
// entry := yang.ToEntry(mod)
// log.Debugf("Yang mod: %v", entry.Kind)
// if entry.Kind == yang.DirectoryEntry {
// for s := range entry.Dir {
// names = append(names, tokens[1]+" "+s)
// }
// }
// }
log.Debugf("names: %v\n", names)
} else {
log.Debug("Returning all modules")
names = modNames2
}
return names, returnType
}
func newNetconfRequest(netconfEntry *yang.Entry, Path []string, value string, requestType types.RequestType, delete bool) *netconfRequest {
ncArray := make([]netconfPathElement, len(Path))
for i, p := range Path {
if strings.Contains(p, "=") {
values := strings.Split(p, "=")
ncArray[i].name = values[0]
ncArray[i].value = &values[1]
} else {
ncArray[i].name = p
ncArray[i].value = nil
}
if i == len(Path)-1 {
ncArray[i].delete = delete
}
}
return &netconfRequest{
NetConfPath: ncArray,
ncEntry: netconfEntry,
Value: value,
reqType: requestType,
}
}
func emitNestedXML(enc *xml.Encoder, paths []netconfPathElement, value string, reqType types.RequestType) {
var start3 xml.StartElement
if paths[0].delete {
start3 = xml.StartElement{
Name: xml.Name{Local: paths[0].name},
// Possible operations are merge, replace, create, delete, remove
// Use remove rather than delete so as not to error if config doesn't exist.
Attr: []xml.Attr{{Name: xml.Name{Local: "operation", Space: "urn:ietf:params:xml:ns:netconf:base:1.0"}, Value: "remove"}}}
} else {
if strings.Contains(paths[0].name, "@") {
elems := strings.Split(paths[0].name, "@")
start3 = xml.StartElement{Name: xml.Name{Space: elems[0], Local: elems[1]}}
} else {
start3 = xml.StartElement{Name: xml.Name{Local: paths[0].name}}
}
}
err := enc.EncodeToken(start3)
if err != nil {
fmt.Println(err)
}
if paths[0].value != nil {
enc.EncodeToken(xml.CharData(*paths[0].value))
enc.EncodeToken(start3.End())
}
if len(paths) > 1 {
emitNestedXML(enc, paths[1:], value, reqType)
} else if value != "" {
enc.EncodeToken(xml.CharData(value))
}
if paths[0].value == nil {
err = enc.EncodeToken(start3.End())
if err != nil {
fmt.Println(err)
}
}
}
func (nc *netconfRequest) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
switch nc.reqType {
case types.Commit:
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "commit"}})
case types.Validate:
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "validate"}})
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "source"}})
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "candidate"}})
case types.EditConf:
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "edit-config"}})
emitNestedXML(enc, []netconfPathElement{
{name: "target", value: nil},
{name: "candidate", value: nil}},
"",
nc.reqType)
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "config", Space: "urn:ietf:params:xml:ns:netconf:base:1.0"}})
case types.GetConf:
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "get-config"}})
emitNestedXML(enc, []netconfPathElement{
{name: "source", value: nil},
// TODO - add an option to choose between views.
{name: "running", value: nil}},
// {name: "running-inheritance", value: nil}},
"", nc.reqType)
if nc.ncEntry != nil {
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "filter"}})
}
case types.GetOper:
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "get"}})
enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "filter"}, Attr: []xml.Attr{{Name: xml.Name{Local: "type"}, Value: "subtree"}}})
case types.RpcOp:
// enc.EncodeToken(xml.StartElement{Name: xml.Name{Local: "rpc"}})
}
var err error
if nc.ncEntry != nil {
// start2 := xml.StartElement{Name: xml.Name{Local: nc.NetConfPath[0].name, Space: nc.ncEntry.Namespace().Name}}
//fmt.Println(start2)
// err = enc.EncodeToken(start2)
if err != nil {
fmt.Println(err)
}
// @@@ To reinstate old, also uncomment 'start2 above and below
// emitNestedXML(enc, nc.NetConfPath[1:], nc.Value, nc.reqType)
enc.Encode(nc.store.Root)
// err = enc.EncodeToken(start2.End())
if err != nil {
fmt.Println(err)
}
}
switch nc.reqType {
case types.Commit:
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "commit"}})
case types.Validate:
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "validate"}})
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "source"}})
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "candidate"}})
case types.EditConf:
err = enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "config", Space: "urn:ietf:params:xml:ns:netconf:base:1.0"}})
if err != nil {
fmt.Println(err)
}
err = enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "edit-config"}})
if err != nil {
fmt.Println(err)
}
case types.GetConf:
if nc.ncEntry != nil {
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "filter"}})
}
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "get-config"}})
case types.GetOper:
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "filter"}})
enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "get"}})
case types.RpcOp:
// enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "rpc"}})
}
enc.Flush()
return nil
}
func getYangModule(s *netconf.Session, yangMod string) *yang.Module {
/*
* Get the yang module from XR and read it into the map
*/
log.Debug("Getting: ", yangMod)
reply, error := s.Do(context.Background(),
`<get-schema
xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring">
<identifier>`+
yangMod+
`</identifier>
</get-schema>
`)
if error != nil {
fmt.Printf("Request reply error1: %v\n", error)
return nil
}
if reply.Errors != nil && len(reply.Errors) > 0 {
fmt.Printf("Request reply error1: %v\n", reply.Errors[0])
return nil
}
// log.Debugf("Request reply: %v, error: %v\n", reply.Data, error)
// re, _ := regexp.Compile("\n#[0-9]+\n")
// strs := re.FindAllStringSubmatch(reply.Data, 10)
// fmt.Printf("%v\n", strs)
// reply.Data = re.ReplaceAllLiteralString(reply.Data, "")
yangReply := yangReply{}
_ = xml.Unmarshal([]byte(reply.Body), &yangReply)
//fmt.Printf("Request reply: %v, error: %v\n", yangReply, err)
err := ms.Parse(yangReply.Rest, yangMod)
if err != nil {
fmt.Fprintln(os.Stderr, err)
panic("Error")
}
// TODO move this into getYangModule
var mod *yang.Module = nil
if ms.Modules[yangMod] != nil {
mod = ms.Modules[yangMod]
} else if ms.SubModules[yangMod] != nil {
mod = ms.SubModules[yangMod]
}
if mod != nil {
for _, i := range mod.Include {
log.Debugf("Mod include: %v %v", mod.Name, i)
// Add check here whether we already have the submodule; if not get it, and note we need to reprocess this module further down.
if ms.Modules[i.Name] == nil && ms.SubModules[i.Name] == nil {
submod := getYangModule(globalSession, i.Name)
if submod == nil {
log.Infof("Failed to get %v", i.Name)
} else {
yang.ToEntry(submod)
}
} else {
log.Debug("Already processed: ", i.Name)
}
}
}
if mod != nil {
for _, i := range mod.Import {
log.Debugf("Mod import: %v %v", mod.Name, i)
// Add check here whether we already have the submodule; if not get it, and note we need to reprocess this module further down.
if ms.Modules[i.Name] == nil && ms.SubModules[i.Name] == nil {
submod := getYangModule(globalSession, i.Name)
if submod == nil {
log.Infof("Failed to get %v", i.Name)
} else {
yang.ToEntry(submod)
}
} else {
log.Debug("Already processed: ", i.Name)
}
}
}
if ms.Modules[yangMod] != nil {
ms.Modules[ms.Modules[yangMod].FullName()] = nil
} else if ms.SubModules[yangMod] != nil {
ms.SubModules[ms.SubModules[yangMod].FullName()] = nil
}
//mods[yangMod] = getYangModule(globalSession, yangMod)
err = ms.Parse(yangReply.Rest, yangMod)
if err != nil {
fmt.Fprintln(os.Stderr, err)
panic("Error")
}
if ms.Modules[yangMod] != nil {
mod = ms.Modules[yangMod]
} else if ms.SubModules[yangMod] != nil {
mod = ms.SubModules[yangMod]
}
mods[yangMod] = mod
//if ms.Modules[yangMod] != nil {
log.Debugf("About to process %s", mod.Name)
//yang.ToEntry(mod)
ms.Process()
log.Debugf("Stored and re-processed %s", mod.Name)
//}
return mod
}
func sendNetconfRequest(s *netconf.Session, requestLine string, requestType types.RequestType) (string, string) {
var store xmlstore.XMLStore
defer timeTrack(time.Now(), "Request")
slice := strings.Split(requestLine, " ")
var yang_module *yang.Entry
if len(slice) > 1 {
/*
* If we don't know the module, read it from the router now.
*/
if mods[slice[1]] == nil {
mods[slice[1]] = getYangModule(s, slice[1])
if mods[slice[1]] == nil {
return "Couldn't get yang module", ""
}
}
yang_module = yang.ToEntry(mods[slice[1]])
}
// Create a request structure with module, path array, and string value.
var ncRequest *netconfRequest
switch requestType {
case types.Commit:
fallthrough
case types.Validate:
ncRequest = newNetconfRequest(nil, nil, "", requestType, false)
case types.EditConf:
if slice[0] == "delete" {
ncRequest = newNetconfRequest(yang.ToEntry(mods[slice[1]]), slice[2:], "", requestType, true)
} else {
ncRequest = newNetconfRequest(yang.ToEntry(mods[slice[1]]), slice[2:len(slice)-1], slice[len(slice)-1], requestType, false)
}
case types.GetOper, types.GetConf, types.RpcOp:
if len(slice) >= 3 {
ncRequest = newNetconfRequest(yang.ToEntry(mods[slice[1]]), slice[2:], "", requestType, false)
} else if requestType == types.GetConf {
// getConf supports getting the whole config.
ncRequest = newNetconfRequest(nil, []string{}, "", requestType, false)
}
default:
panic("Bad request type")
}
// fmt.Printf("ncRequest: %v\n", ncRequest)
// Add to xmlstore
if len(slice) > 1 {
store.Insert(yang_module, requestLine, requestType)
}
ncRequest.store = &store
// var reply *netconf.RPCReply
var error error
// rpc := netconf.NewRPCMessage([]netconf.RPCMethod{ncRequest})
// xml2, err := xml.MarshalIndent(rpc, "", " ")
// if err != nil {
// fmt.Fprintln(os.Stderr, err)
// }
// log.Debug(string(xml2))
// println(string(xml2))
// xml3 := ncRequest.MarshalMethod()
// myxml := store.Marshal()
// fmt.Print(diff.LineDiff(string(xml3), string(myxml)))
// log.Debugf("Request reply: %s, error: %v\n", reply, error)
var theString string
var reply *netconf.Reply = nil
if requestType == types.Commit {
error = s.Commit(context.Background())
log.Debugf("Request reply: %v, error: %v\n", reply, error)
} else if requestType == types.Validate {
error = s.Validate(context.Background(), netconf.Candidate)
log.Debugf("Request reply: %v, error: %v\n", reply, error)
// } else if requestType == getConf || requestType == getOper {
} else {
rpc := ncRequest
reply, error = s.Do(context.Background(), &rpc)
if error != nil {
fmt.Printf("Request reply: %v, error: %v\n", reply, error)
return "", ""
}
log.Debugf("Request reply: %v, error: %v, data: %v\n", reply, error, reply.Body)
// fmt.Printf("Request data: %v\n", reply.Data)
}
if reply != nil {
dec := xml.NewDecoder(bytes.NewReader(reply.Body))
var tok xml.Token
var lastString string
var seenFirstEnd bool
seenFirstEnd = false
for {
tok, error = dec.Token()
// fmt.Printf("Token: %T %v\n", tok, error)
switch v := tok.(type) {
case xml.CharData:
// fmt.Printf("Token: %v %v\n", string(v), error)
lastString = string(v)
// theString = lastString
case xml.EndElement:
if !seenFirstEnd {
seenFirstEnd = true
theString = lastString
}
default:
// fmt.Printf("Token: %v %v\n", v, error)
}
if tok == nil {
break
}
}
// TODO Handle bool/presence type items
fmt.Println("Data: ", theString)
}
if reply != nil {
return string(reply.Body), theString
} else if error != nil {
fmt.Printf("Error: %v\n", error.Error())
return error.Error(), ""
} else {
return "", ""
}
}
func getSchemaList(s *netconf.Session) []string {
/*
* Get a list of schemas
*/
reply, error := s.Do(context.Background(), `<get>
<filter type="subtree">
<netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring">
<schemas/>
</netconf-state>
</filter>
</get>`)
if error != nil {
fmt.Printf("Request reply error2: %v\n", error)
// panic(error)
return nil
}
// fmt.Printf("Request reply: %v, error: %v\n", reply.Data[0:1000], error)
schemaReply := schemaReply{}
error = xml.Unmarshal([]byte(reply.Body), &schemaReply)
//fmt.Printf("Request reply: %v, error: %v\n", schemaReply.Rest.Rest.Schemas[0], err)
//fmt.Printf("Request reply: %v, error: %v\n", schemaReply.Rest.Rest.Schemas[99].Identifier, err)
if error != nil {
fmt.Printf("Request reply error3: %v\n", error)
}
var schStrings []string
// for _, sch := range schemaReply.Rest.Rest.Schemas {
for _, sch := range schemaReply.NetconfState.Schemas.Schema {
schStrings = append(schStrings, sch.Identifier)
}
sort.Strings(schStrings)
modNames2 = schStrings
return schStrings
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
func ListYang(path string) ([]string, int) {
return listYang(path)
}