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

Merge branch '67-1-path-traversal-get-all' into '67-overhaul-architecture'

path traversal library

See merge request cocsn/gosdn!95
parents 7f4d2e92 6cfff6f6
Branches
Tags
3 merge requests!95path traversal library,!91"Overhaul Architecture",!90Develop
Pipeline #61131 passed
This commit is part of merge request !91. Comments created here will be created in the context of that merge request.
package main
import (
schema "code.fbi.h-da.de/cocsn/yang-models/generated/arista"
"fmt"
"github.com/openconfig/goyang/pkg/yang"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
"code.fbi.h-da.de/cocsn/yang-models/generated/arista"
log "github.com/sirupsen/logrus"
)
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() {
tree := schema.SchemaTree
paths := make(map[string]*PathElement)
log.SetLevel(log.DebugLevel)
schema, _ := arista.Schema()
paths := util.NewPaths()
paths.ParseSchema(schema, "device")
for k,v := range tree {
if v.Parent != nil {
if v.Parent.Name == "device" {
pathElement := processEntry(v)
pathElement.Print()
paths[k] = pathElement
}
}
for _, v := range paths {
v.Print()
}
}
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
p := paths.StringBuilder()
log.Debug(p)
}
......@@ -52,4 +52,4 @@ func (c *Core) Shutdown() {
}
log.Info("Shutdown complete")
os.Exit(0)
}
\ No newline at end of file
}
......@@ -19,7 +19,7 @@ type Device struct {
// use better naming in further develop
// Also all that Interface Call specific logic belongs to SBI!
func (d Device) Add(resp interface{}) error {
s, err := openconfig.Schema()
s, err := d.SBI.Schema()
if err != nil {
return err
}
......
......@@ -34,4 +34,4 @@ func TestDevice_Add(t *testing.T) {
log.Debug(d.SBI.SbiIdentifier())
})
}
}
\ No newline at end of file
}
package nucleus
import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
"code.fbi.h-da.de/cocsn/yang-models/generated/openconfig"
"github.com/openconfig/ygot/ytypes"
)
// SouthboundInterface provides an
// interface for SBI implementations
type SouthboundInterface interface {
// Deprecated
SbiIdentifier() string
Schema() (*ytypes.Schema, error)
}
type Tapi struct {
......@@ -11,12 +20,17 @@ type Tapi struct {
type OpenConfig struct {
transport Transport
schema *ytypes.Schema
}
func (oc *OpenConfig) SbiIdentifier() string {
return "openconfig"
}
func (oc *OpenConfig) Schema() (*ytypes.Schema, error) {
return openconfig.Schema()
}
func (oc *OpenConfig) OpenconfigInterfaces(device Device) {
resp, err := oc.transport.Get(nil, nil...)
if err != nil {
......@@ -24,3 +38,10 @@ func (oc *OpenConfig) OpenconfigInterfaces(device Device) {
}
device.Add(resp)
}
func (oc *OpenConfig) GetFullDeviceInfo(device Device) error {
paths := util.NewPaths()
paths.ParseSchema(oc.schema, "device")
return nil
}
......@@ -47,4 +47,4 @@ func TestOpenConfig_OpenconfigInterfaces(t *testing.T) {
oc.OpenconfigInterfaces(tt.args.device)
})
}
}
\ No newline at end of file
}
package util
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() {
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 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
}
func (p Paths) ParseSchema(schema *ytypes.Schema, root string) error {
tree := schema.SchemaTree
for k, v := range tree {
if v.Parent != nil {
if v.Parent.Name == root {
path := processEntry(v)
p[k] = path
}
}
}
return nil
}
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(DELIM)
}
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