Skip to content
Snippets Groups Projects
telemetry.go 1.8 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"
    	schema "code.fbi.h-da.de/cocsn/yang-models/generated/arista"
    	"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{}
    	transport := &nucleus.Gnmi{
    		SetNode: sbi.SetNode(),
    		RespChan: make(chan *gpb.SubscribeResponse),
    	}
    	device := nucleus.Device{
    		Device: &schema.Device{},
    		SBI:    sbi,
    		Config: nucleus.DeviceConfig{
    			Uuid:     uuid.New(),
    			Address:  "[fdfd::ce05]:6030",
    			Username: "admin",
    			Password: "arista",
    		},
    		Transport: transport,
    	}
    	pnd := nucleus.NewPND("openconfig", sbi)
    	if err := pnd.AddDevice(device); err != nil {
    		log.Fatal(err)
    	}
    
    	cfg := &gnmi.Config{
    		Addr:     device.Config.Address,
    		Password: device.Config.Password,
    		Username: device.Config.Username,
    	}
    	ctx := gnmi.NewContext(context.Background(), cfg)
    	ctx = context.WithValue(ctx, "config", cfg)
    
    	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)
    	ctx = context.WithValue(ctx, "opts", opts)
    	go func() {
    		if err := transport.Subscribe(ctx); err != nil {
    			log.Fatal(err)
    		}
    	}()
    	fmt.Println("awaiting signal")
    	<-done
    	fmt.Println("exiting")
    }