Skip to content
Snippets Groups Projects
Commit 8a11ab6e authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

path traversal library

parent 7f4d2e92
No related branches found
No related tags found
3 merge requests!95path traversal library,!91"Overhaul Architecture",!90Develop
Pipeline #60776 passed with warnings
package main package main
import ( import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
schema "code.fbi.h-da.de/cocsn/yang-models/generated/arista" schema "code.fbi.h-da.de/cocsn/yang-models/generated/arista"
"fmt"
"github.com/openconfig/goyang/pkg/yang"
) )
type PathElement struct {
Children []*PathElement
Name string
}
func (p *PathElement)Print() {
printPE(0, p)
}
func printPE(indent int, pe *PathElement) {
for i := 0; i < indent; i++ {
fmt.Print(" ")
}
fmt.Println(pe.Name)
if len(pe.Children) > 0 {
for _,p := range pe.Children {
printPE(indent+1, p)
}
}
}
func main() { func main() {
tree := schema.SchemaTree tree := schema.SchemaTree
paths := make(map[string]*PathElement) paths := util.NewPaths()
for k,v := range tree { for _,v := range tree {
if v.Parent != nil { if v.Parent != nil {
if v.Parent.Name == "device" { if v.Parent.Name == "device" {
pathElement := processEntry(v) pathElement := paths.ProcessEntry(v)
pathElement.Print() pathElement.Print()
paths[k] = pathElement
} }
} }
} }
} }
func processEntry(e *yang.Entry) *PathElement {
if e.Dir != nil {
elem := &PathElement{
Children: make([]*PathElement, len(e.Dir)),
Name: e.Name,
}
i := 0
for _,v := range e.Dir {
elem.Children[i] = processEntry(v)
i++
}
return elem
}
leaf := &PathElement{
Name: e.Name,
}
return leaf
}
\ No newline at end of file
package util
import (
"fmt"
"github.com/openconfig/goyang/pkg/yang"
)
type PathElement struct {
Children []*PathElement
Name string
}
func (p *PathElement)Print() {
printPE(0, p)
}
func printPE(indent int, pe *PathElement) {
for i := 0; i < indent; i++ {
fmt.Print(" ")
}
fmt.Println(pe.Name)
if len(pe.Children) > 0 {
for _,p := range pe.Children {
printPE(indent+1, p)
}
}
}
func NewPaths() Paths {
return make(map[string]*PathElement)
}
type Paths map[string]*PathElement
func (p Paths)ProcessEntry(e *yang.Entry) *PathElement {
if e.Dir != nil {
elem := &PathElement{
Children: make([]*PathElement, len(e.Dir)),
Name: e.Name,
}
i := 0
for _,v := range e.Dir {
elem.Children[i] = p.ProcessEntry(v)
i++
}
p[e.Name] = elem
return elem
}
leaf := &PathElement{
Name: e.Name,
}
return leaf
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment