Skip to content
Snippets Groups Projects
telemetry.go 1.67 KiB
Newer Older
  • Learn to ignore specific revisions
  • Manuel Kieweg's avatar
    Manuel Kieweg committed
    package main
    
    import (
    	"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus"
    	"context"
    	"fmt"
    	"github.com/google/uuid"
    	gpb "github.com/openconfig/gnmi/proto/gnmi"
    	log "github.com/sirupsen/logrus"
    	"os"
    	"os/signal"
    	"syscall"
    	"time"
    )
    
    func main() {
    	log.SetLevel(log.DebugLevel)
    	sbi := &nucleus.AristaOC{}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	device := nucleus.Device{
    
    		GoStruct: sbi.Schema().Root,
    		SBI:      sbi,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		Config: nucleus.DeviceConfig{
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			Uuid: uuid.New(),
    
    	pnd := nucleus.NewPND("openconfig", "a simple openconfig PND", sbi)
    	if err := pnd.AddDevice(&device); err != nil {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		log.Fatal(err)
    	}
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	transport := &nucleus.Gnmi{
    		SetNode:  sbi.SetNode(),
    		RespChan: make(chan *gpb.SubscribeResponse),
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	cfg := &gnmi.Config{
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		Addr:     "portainer.danet.fbi.h-da.de:6030",
    		Username: "admin",
    		Password: "arista",
    		Encoding: gpb.Encoding_JSON_IETF,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	transport.SetConfig(cfg)
    
    	device.Transport = transport
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    	paths := []string{"/interfaces/interface/name"}
    
    	opts := &gnmi.SubscribeOptions{
    		UpdatesOnly:       false,
    		Prefix:            "",
    		Mode:              "stream",
    		StreamMode:        "sample",
    		SampleInterval:    uint64(10 * time.Second.Nanoseconds()),
    		SuppressRedundant: false,
    		HeartbeatInterval: uint64(time.Second.Nanoseconds()),
    		Paths:             gnmi.SplitPaths(paths),
    		Origin:            "",
    		Target:            device.Config.Address,
    	}
    	done := make(chan os.Signal, 1)
    	signal.Notify(done, syscall.SIGILL, syscall.SIGTERM)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	ctx := context.WithValue(context.Background(), "opts", opts)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	go func() {
    		if err := transport.Subscribe(ctx); err != nil {
    			log.Fatal(err)
    		}
    	}()
    	fmt.Println("awaiting signal")
    	<-done
    	fmt.Println("exiting")