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

lib path traversal v0.0.1

parent 3a906b3b
Branches
Tags
3 merge requests!95path traversal library,!91"Overhaul Architecture",!90Develop
Pipeline #60819 passed with warnings
This commit is part of merge request !91. Comments created here will be created in the context of that merge request.
......@@ -3,16 +3,19 @@ package main
import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
"code.fbi.h-da.de/cocsn/yang-models/generated/arista"
log "github.com/sirupsen/logrus"
)
func main() {
schema,_ := arista.Schema()
log.SetLevel(log.DebugLevel)
schema, _ := arista.Schema()
paths := util.NewPaths()
paths.ParseSchema(schema, "device")
for _,v := range paths {
for _, v := range paths {
v.Print()
}
}
p := paths.StringBuilder()
log.Debug(p)
}
......@@ -4,14 +4,18 @@ import (
"fmt"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ytypes"
log "github.com/sirupsen/logrus"
"strings"
)
const DELIM = "/"
type PathElement struct {
Children []*PathElement
Name string
}
func (p *PathElement)Print() {
func (p *PathElement) Print() {
printPE(0, p)
}
......@@ -21,7 +25,7 @@ func printPE(indent int, pe *PathElement) {
}
fmt.Println(pe.Name)
if len(pe.Children) > 0 {
for _,p := range pe.Children {
for _, p := range pe.Children {
printPE(indent+1, p)
}
}
......@@ -33,34 +37,84 @@ func NewPaths() Paths {
type Paths map[string]*PathElement
func (p Paths) processEntry(e *yang.Entry) *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] = p.processEntry(v)
for _, v := range e.Dir {
elem.Children[i] = processEntry(v)
i++
}
p[e.Name] = elem
return elem
}
leaf := &PathElement{
Name: e.Name,
Name: e.Name,
}
return leaf
}
func (p Paths) ParseSchema(schema *ytypes.Schema, root string) error {
tree := schema.SchemaTree
for _,v := range tree {
for k, v := range tree {
if v.Parent != nil {
if v.Parent.Name == root {
p.processEntry(v)
path := processEntry(v)
p[k] = path
}
}
}
return nil
}
\ No newline at end of file
}
func (p Paths) StringBuilder() []string {
paths := make([]string, 0)
ch := make(chan string)
stop := make(chan bool)
val := make(chan []string)
go appendix(ch, stop, val)
for _, v := range p {
var b strings.Builder
stringBuilder(ch, &b, v)
}
stop <- true
paths = <-val
return paths
}
func appendix(c chan string, stop chan bool,p chan []string) {
paths := make([]string, 0)
var sig bool
for {
select {
case path := <-c:
paths = append(paths, path)
log.Debug(path)
case sig = <-stop:
log.Debug("Signal received: %v", sig)
}
if sig {break}
}
p <- paths
}
func stringBuilder(ch chan string, b *strings.Builder, v *PathElement) {
if b.Len() == 0 {
b.WriteString("/")
}
b.WriteString(v.Name)
if v.Children != nil {
b.WriteString(DELIM)
for _, c := range v.Children {
var b_cpy strings.Builder
b_cpy.WriteString(b.String())
stringBuilder(ch, &b_cpy, c)
}
} else {
log.Debug("leaf")
ch <- b.String()
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment