diff --git a/cmd/path-traversal/path_traversal.go b/cmd/path-traversal/path_traversal.go index 9952b95896c93721134b7ea829fcf60693140fba..353c2b8e1c9fcb2538818651e40e6759af2e22a3 100644 --- a/cmd/path-traversal/path_traversal.go +++ b/cmd/path-traversal/path_traversal.go @@ -1,62 +1,21 @@ 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) +} diff --git a/nucleus/controller.go b/nucleus/controller.go index 9de78e91939e778db1c00f1c58b40d7d9ba9c72d..47c87bf0a6edf82066ee84ed43b7cfa7ff4c60c4 100644 --- a/nucleus/controller.go +++ b/nucleus/controller.go @@ -52,4 +52,4 @@ func (c *Core) Shutdown() { } log.Info("Shutdown complete") os.Exit(0) -} \ No newline at end of file +} diff --git a/nucleus/device.go b/nucleus/device.go index bd3dcb722746e8f0b3c1379cc6daae99fbc40e4e..78bb3fae47715b982bb3fd663ccf545981be17b1 100644 --- a/nucleus/device.go +++ b/nucleus/device.go @@ -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 } diff --git a/nucleus/device_test.go b/nucleus/device_test.go index 3f89d7d0fd7844082c76c530eed2d2acf4118007..a452c791232c5bf17f96eebb46f935261857003e 100644 --- a/nucleus/device_test.go +++ b/nucleus/device_test.go @@ -34,4 +34,4 @@ func TestDevice_Add(t *testing.T) { log.Debug(d.SBI.SbiIdentifier()) }) } -} \ No newline at end of file +} diff --git a/nucleus/southbound.go b/nucleus/southbound.go index a3aa5af682a41581b53ce03e69526a5e34454349..e908f41d17b114b92f3cb814ad92f0dbe41efb92 100644 --- a/nucleus/southbound.go +++ b/nucleus/southbound.go @@ -1,9 +1,18 @@ 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 +} diff --git a/nucleus/southbound_test.go b/nucleus/southbound_test.go index 447696fa7cb6350f089bed728ea31b5ea01af477..72536cbbb63612ae3e74312393ee4a94c462d91a 100644 --- a/nucleus/southbound_test.go +++ b/nucleus/southbound_test.go @@ -47,4 +47,4 @@ func TestOpenConfig_OpenconfigInterfaces(t *testing.T) { oc.OpenconfigInterfaces(tt.args.device) }) } -} \ No newline at end of file +} diff --git a/nucleus/util/path_traversal.go b/nucleus/util/path_traversal.go new file mode 100644 index 0000000000000000000000000000000000000000..65dd9eeb351898ee2369a550d0e95ce6b47d799c --- /dev/null +++ b/nucleus/util/path_traversal.go @@ -0,0 +1,120 @@ +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() + } +}