Skip to content
Snippets Groups Projects
subscribe.go 1.77 KiB
Newer Older
  • Learn to ignore specific revisions
  • package cli
    
    import (
    	"context"
    	"fmt"
    	"os"
    	"os/signal"
    	"syscall"
    	"time"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	tpb "code.fbi.h-da.de/cocsn/api/go/gosdn/transport"
    
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus/types"
    
    
    Andre Sterba's avatar
    Andre Sterba committed
    	"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus"
    	log "github.com/sirupsen/logrus"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Subscribe starts a gNMI subscriber requersting the specified paths on the target and
    // logs the response to stdout. Only 'stream' mode with 'sample' operation supported.
    
    func Subscribe(address, username, password, deviceName string, sample, heartbeat int64, args ...string) error {
    
    	sbi := &nucleus.OpenConfig{}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	tOpts := &tpb.TransportOption{
    		Address:  address,
    		Username: username,
    		Password: password,
    		Tls:      false,
    		TransportOption: &tpb.TransportOption_GnmiTransportOption{
    			GnmiTransportOption: &tpb.GnmiTransportOption{
    				Compression:     "",
    				GrpcDialOptions: nil,
    				Token:           "",
    				Encoding:        0,
    			},
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		},
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	device, err := nucleus.NewDevice(deviceName, tOpts, sbi)
    
    	if err != nil {
    		return err
    	}
    
    	opts := &gnmi.SubscribeOptions{
    		UpdatesOnly:       false,
    		Prefix:            "",
    		Mode:              "stream",
    		StreamMode:        "sample",
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		SampleInterval:    uint64(sample * time.Second.Nanoseconds()),
    
    		SuppressRedundant: false,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		HeartbeatInterval: uint64(heartbeat * time.Second.Nanoseconds()),
    		Paths:             gnmi.SplitPaths(args),
    
    		Origin:            "",
    
    	}
    	done := make(chan os.Signal, 1)
    	signal.Notify(done, syscall.SIGILL, syscall.SIGTERM)
    
    	ctx := context.WithValue(context.Background(), types.CtxKeyOpts, opts) //nolint
    
    	go func() {
    		if err := device.Transport.Subscribe(ctx); err != nil {
    			log.Fatal(err)
    		}
    	}()
    	fmt.Println("awaiting signal")
    	<-done
    	fmt.Println("exiting")
    
    	return nil
    }