capabilities.go 980 Bytes
Newer Older
Manuel Kieweg's avatar
Manuel Kieweg committed
1
2
3
4
5
6
package cli

import (
	"context"
	"fmt"
	"strings"
Manuel Kieweg's avatar
Manuel Kieweg committed
7

Manuel Kieweg's avatar
Manuel Kieweg committed
8
	"code.fbi.h-da.de/danet/forks/goarista/gnmi"
Manuel Kieweg's avatar
Manuel Kieweg committed
9
10
	"code.fbi.h-da.de/danet/gosdn/nucleus"
	gpb "github.com/openconfig/gnmi/proto/gnmi"
Manuel Kieweg's avatar
Manuel Kieweg committed
11
12
)

Manuel Kieweg's avatar
Manuel Kieweg committed
13
14
// Capabilities sends a gNMI Capabilities request to the specified target
// and prints the supported models to stdout
Malte Bauch's avatar
Malte Bauch committed
15
func Capabilities(a, u, p string) error {
Manuel Kieweg's avatar
Manuel Kieweg committed
16
	cfg := gnmi.Config{
Manuel Kieweg's avatar
Manuel Kieweg committed
17
18
19
		Addr:     a,
		Username: u,
		Password: p,
Manuel Kieweg's avatar
Manuel Kieweg committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
		Encoding: gpb.Encoding_JSON_IETF,
	}
	opts := &nucleus.GnmiTransportOptions{Config: cfg}
	transport, err := nucleus.NewGnmiTransport(opts)
	if err != nil {
		return err
	}
	resp, err := transport.Capabilities(context.Background())
	if err != nil {
		return err
	}
	modelData := resp.(*gpb.CapabilityResponse).SupportedModels
	b := strings.Builder{}
	for _, elem := range modelData {
		_, err := b.WriteString(elem.Name)
		if err != nil {
			return err
		}
		_, err = b.WriteString("\n")
		if err != nil {
			return err
		}
	}
	fmt.Println(b.String())
	return nil
}