Skip to content
Snippets Groups Projects
Commit 6ead82b9 authored by Malte Bauch's avatar Malte Bauch
Browse files

integrated config to transport options

parent b755ccd5
Branches
Tags
3 merge requests!116Resolve "Transport Tests",!102Resolve "Follow-up from "Resolve "PND handling via CLI and database""",!90Develop
Pipeline #66881 passed with warnings
...@@ -11,13 +11,14 @@ import ( ...@@ -11,13 +11,14 @@ import (
) )
func main() { func main() {
cfg := &gnmi.Config{ cfg := gnmi.Config{
Addr: "[2003:e6:1722:fed0:0:242:ac11:5]:6030", Addr: "portainer.danet.fbi.h-da.de:6030",
Username: "admin", Username: "admin",
Password: "arista", Password: "arista",
Encoding: gpb.Encoding_JSON_IETF, Encoding: gpb.Encoding_JSON_IETF,
} }
transport, err := nucleus.NewGnmiTransport(cfg) opts := &nucleus.GnmiTransportOptions{Config: cfg}
transport, err := nucleus.NewGnmiTransport(opts)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
......
...@@ -17,10 +17,11 @@ func main() { ...@@ -17,10 +17,11 @@ func main() {
} }
opts := &nucleus.GnmiTransportOptions{ opts := &nucleus.GnmiTransportOptions{
Config: gnmi.Config{
Addr: a, Addr: a,
Username: "admin", Username: "admin",
Password: "arista", Password: "arista",
},
} }
t, err := nucleus.NewGnmiTransport(opts) t, err := nucleus.NewGnmiTransport(opts)
if err != nil { if err != nil {
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus" "code.fbi.h-da.de/cocsn/gosdn/nucleus"
"context" "context"
"fmt" "fmt"
"github.com/google/uuid"
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os" "os"
...@@ -17,14 +18,16 @@ func main() { ...@@ -17,14 +18,16 @@ func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
sbi := &nucleus.OpenConfig{} sbi := &nucleus.OpenConfig{}
device, err := nucleus.NewDevice("gnmi", sbi, device, err := nucleus.NewDevice(sbi,
&nucleus.GnmiTransportOptions{ &nucleus.GnmiTransportOptions{
Addr: "portainer.danet.fbi.h-da.de:6030", Config: gnmi.Config{
Username: "admin", Addr: "portainer.danet.fbi.h-da.de:6030",
Password: "arista", Username: "admin",
Password: "arista",
Encoding: gpb.Encoding_JSON_IETF,
},
SetNode: sbi.SetNode(), SetNode: sbi.SetNode(),
RespChan: make(chan *gpb.SubscribeResponse), RespChan: make(chan *gpb.SubscribeResponse),
Encoding: gpb.Encoding_JSON_IETF,
}) })
if err != nil { if err != nil {
log.Debug(err) log.Debug(err)
...@@ -32,8 +35,6 @@ func main() { ...@@ -32,8 +35,6 @@ func main() {
pnd, err := nucleus.NewPND("openconfig", "a simple openconfig PND", uuid.New(), sbi) pnd, err := nucleus.NewPND("openconfig", "a simple openconfig PND", uuid.New(), sbi)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
}
log.Fatal(err)
} }
if err := pnd.AddDevice(device); err != nil { if err := pnd.AddDevice(device); err != nil {
log.Fatal(err) log.Fatal(err)
......
package main package main
import ( import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/nucleus" "code.fbi.h-da.de/cocsn/gosdn/nucleus"
"github.com/google/uuid"
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
...@@ -12,16 +14,18 @@ import ( ...@@ -12,16 +14,18 @@ import (
*/ */
func main() { func main() {
log.SetLevel(log.DebugLevel)
sbi := &nucleus.OpenConfig{} sbi := &nucleus.OpenConfig{}
device, err := nucleus.NewDevice("gnmi", sbi, opts := &nucleus.GnmiTransportOptions{
&nucleus.GnmiTransportOptions{ Config: gnmi.Config{
Addr: "portainer.danet.fbi.h-da.de:6030", Addr: "portainer.danet.fbi.h-da.de:6030",
Username: "admin", Username: "admin",
Password: "arista", Password: "arista",
SetNode: sbi.SetNode(),
Encoding: gpb.Encoding_JSON_IETF, Encoding: gpb.Encoding_JSON_IETF,
}) },
SetNode: sbi.SetNode(),
}
log.SetLevel(log.DebugLevel)
device, err := nucleus.NewDevice(sbi, opts)
if err != nil { if err != nil {
log.Debug(err) log.Debug(err)
} }
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"errors" "errors"
"flag"
"fmt" "fmt"
"math" "math"
"net" "net"
...@@ -82,59 +81,6 @@ type SubscribeOptions struct { ...@@ -82,59 +81,6 @@ type SubscribeOptions struct {
Target string Target string
} }
// ParseFlags reads arguments from stdin and returns a populated Config object and a list of
// paths to subscribe to
func ParseFlags() (*Config, []string) {
// flags
var (
addrsFlag = flag.String("addrs", "localhost:6030",
"Comma-separated list of addresses of OpenConfig gRPC servers. The address 'HOSTNAME' "+
"is replaced by the current hostname.")
caFileFlag = flag.String("cafile", "",
"Path to server TLS certificate file")
certFileFlag = flag.String("certfile", "",
"Path to ciena TLS certificate file")
keyFileFlag = flag.String("keyfile", "",
"Path to ciena TLS private key file")
passwordFlag = flag.String("password", "",
"Password to authenticate with")
usernameFlag = flag.String("username", "",
"Username to authenticate with")
tlsFlag = flag.Bool("tls", false,
"Enable TLS")
compressionFlag = flag.String("compression", "",
"Type of compression to use")
subscribeFlag = flag.String("subscribe", "",
"Comma-separated list of paths to subscribe to upon connecting to the server")
token = flag.String("token", "",
"Authentication token")
)
flag.Parse()
cfg := &Config{
Addr: *addrsFlag,
CAFile: *caFileFlag,
CertFile: *certFileFlag,
KeyFile: *keyFileFlag,
Password: *passwordFlag,
Username: *usernameFlag,
TLS: *tlsFlag,
Compression: *compressionFlag,
Token: *token,
}
subscriptions := strings.Split(*subscribeFlag, ",")
return cfg, subscriptions
}
// accessTokenCred implements credentials.PerRPCCredentials, the gRPC // accessTokenCred implements credentials.PerRPCCredentials, the gRPC
// interface for credentials that need to attach security information // interface for credentials that need to attach security information
// to every RPC. // to every RPC.
......
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
pb "code.fbi.h-da.de/cocsn/gosdn/api/proto" pb "code.fbi.h-da.de/cocsn/gosdn/api/proto"
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"google.golang.org/grpc" "google.golang.org/grpc"
...@@ -239,8 +240,16 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad ...@@ -239,8 +240,16 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad
return &pb.AddDeviceReply{Message: err.Error()}, err return &pb.AddDeviceReply{Message: err.Error()}, err
} }
newDevice, err := NewDevice(sbi, &GnmiTransportOptions{in.Device.Address, in.Device.Username, newDevice, err := NewDevice(sbi,
in.Device.Password, sbi.SetNode(), nil, gpb.Encoding_JSON_IETF}) &GnmiTransportOptions{
Config: gnmi.Config{
Addr: in.Device.Address,
Username: in.Device.Username,
Password: in.Device.Password,
Encoding: gpb.Encoding_JSON_IETF,
},
SetNode: sbi.SetNode(),
})
if err != nil { if err != nil {
log.Info(err) log.Info(err)
return &pb.AddDeviceReply{Message: err.Error()}, err return &pb.AddDeviceReply{Message: err.Error()}, err
......
...@@ -14,7 +14,7 @@ func TestDevice_Id(t *testing.T) { ...@@ -14,7 +14,7 @@ func TestDevice_Id(t *testing.T) {
GoStruct ygot.GoStruct GoStruct ygot.GoStruct
SBI SouthboundInterface SBI SouthboundInterface
Transport Transport Transport Transport
Uuid uuid.UUID Uuid uuid.UUID
} }
tests := []struct { tests := []struct {
name string name string
...@@ -24,7 +24,7 @@ func TestDevice_Id(t *testing.T) { ...@@ -24,7 +24,7 @@ func TestDevice_Id(t *testing.T) {
{ {
name: "default", name: "default",
fields: fields{ fields: fields{
Uuid: did, Uuid: did,
}, },
want: did, want: did,
}, },
...@@ -35,7 +35,7 @@ func TestDevice_Id(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestDevice_Id(t *testing.T) {
GoStruct: tt.fields.GoStruct, GoStruct: tt.fields.GoStruct,
SBI: tt.fields.SBI, SBI: tt.fields.SBI,
Transport: tt.fields.Transport, Transport: tt.fields.Transport,
Uuid: tt.fields.Uuid, Uuid: tt.fields.Uuid,
} }
if got := d.Id(); !reflect.DeepEqual(got, tt.want) { if got := d.Id(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Id() = %v, want %v", got, tt.want) t.Errorf("Id() = %v, want %v", got, tt.want)
...@@ -47,7 +47,7 @@ func TestDevice_Id(t *testing.T) { ...@@ -47,7 +47,7 @@ func TestDevice_Id(t *testing.T) {
func TestNewDevice(t *testing.T) { func TestNewDevice(t *testing.T) {
sbi := &OpenConfig{} sbi := &OpenConfig{}
type args struct { type args struct {
sbi SouthboundInterface sbi SouthboundInterface
opts TransportOptions opts TransportOptions
} }
tests := []struct { tests := []struct {
...@@ -59,32 +59,27 @@ func TestNewDevice(t *testing.T) { ...@@ -59,32 +59,27 @@ func TestNewDevice(t *testing.T) {
{ {
name: "default", name: "default",
args: args{ args: args{
sbi: sbi, sbi: sbi,
opts: &GnmiTransportOptions{ opts: &GnmiTransportOptions{
Addr: "test:///", Config: gnmi.Config{
Username: "test", Addr: "test:///",
Password: "test", Username: "test",
Password: "test",
},
}, },
}, },
want: &Device{ want: &Device{
GoStruct: &openconfig.Device{}, GoStruct: &openconfig.Device{},
SBI: sbi, SBI: sbi,
Uuid: uuid.New(), Uuid: uuid.New(),
Transport: &Gnmi{ Transport: &Gnmi{
SetNode: nil, Options: &GnmiTransportOptions{
RespChan: nil, Config: gnmi.Config{
Unmarshal: nil, Addr: "test:///",
Options: &GnmiTransportOptions{ Username: "test",
Addr: "test:///", Password: "test",
Username: "test", },
Password: "test",
},
config: &gnmi.Config{
Addr: "test:///",
Username: "test",
Password: "test",
}, },
client: nil,
}, },
}, },
}, },
......
...@@ -12,24 +12,23 @@ import ( ...@@ -12,24 +12,23 @@ import (
) )
type Gnmi struct { type Gnmi struct {
SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
RespChan chan *gpb.SubscribeResponse RespChan chan *gpb.SubscribeResponse
Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error
Options *GnmiTransportOptions Options *GnmiTransportOptions
config *gnmi.Config config *gnmi.Config
client gpb.GNMIClient client gpb.GNMIClient
} }
func NewGnmiTransport(opts *GnmiTransportOptions) (*Gnmi, error) { func NewGnmiTransport(opts *GnmiTransportOptions) (*Gnmi, error) {
config := createConfig(opts) c, err := gnmi.Dial(&opts.Config)
c, err := gnmi.Dial(config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Gnmi{ return &Gnmi{
SetNode: opts.SetNode, SetNode: opts.SetNode,
RespChan: opts.RespChan, RespChan: opts.RespChan,
config: config, config: &opts.Config,
Options: opts, Options: opts,
client: c, client: c,
}, nil }, nil
...@@ -92,7 +91,7 @@ func (g *Gnmi) Set(ctx context.Context, params ...interface{}) (interface{}, err ...@@ -92,7 +91,7 @@ func (g *Gnmi) Set(ctx context.Context, params ...interface{}) (interface{}, err
// Invalid params cause unhealable error // Invalid params cause unhealable error
ops := make([]*gnmi.Operation, 0) ops := make([]*gnmi.Operation, 0)
exts := make([]*gnmi_ext.Extension, 0) exts := make([]*gnmi_ext.Extension, 0)
for _,p := range params{ for _, p := range params {
switch p.(type) { switch p.(type) {
case *gnmi.Operation: case *gnmi.Operation:
ops = append(ops, p.(*gnmi.Operation)) ops = append(ops, p.(*gnmi.Operation))
...@@ -224,13 +223,12 @@ func (g *Gnmi) Close() error { ...@@ -224,13 +223,12 @@ func (g *Gnmi) Close() error {
type GnmiTransportOptions struct { type GnmiTransportOptions struct {
// all needed gnmi transport parameters // all needed gnmi transport parameters
Addr string gnmi.Config
Username string
Password string SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path,
SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path,
val interface{}, opts ...ytypes.SetNodeOpt) error val interface{}, opts ...ytypes.SetNodeOpt) error
RespChan chan *gpb.SubscribeResponse Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error
Encoding gpb.Encoding RespChan chan *gpb.SubscribeResponse
} }
func (gto *GnmiTransportOptions) GetAddress() string { func (gto *GnmiTransportOptions) GetAddress() string {
......
...@@ -86,12 +86,14 @@ func mockTransport() Gnmi { ...@@ -86,12 +86,14 @@ func mockTransport() Gnmi {
func newGnmiTransportOptions() *GnmiTransportOptions { func newGnmiTransportOptions() *GnmiTransportOptions {
return &GnmiTransportOptions{ return &GnmiTransportOptions{
Username: "test", Config: gnmi.Config{
Password: "test", Username: "test",
Addr: "localhost:13371", Password: "test",
Addr: "localhost:13371",
Encoding: gpb.Encoding_PROTO,
},
SetNode: nil, SetNode: nil,
RespChan: make(chan *gpb.SubscribeResponse), RespChan: make(chan *gpb.SubscribeResponse),
Encoding: gpb.Encoding_PROTO,
} }
} }
...@@ -575,8 +577,7 @@ func TestGnmi_getWithRequest(t *testing.T) { ...@@ -575,8 +577,7 @@ func TestGnmi_getWithRequest(t *testing.T) {
func TestNewGnmiTransport(t *testing.T) { func TestNewGnmiTransport(t *testing.T) {
type args struct { type args struct {
opts *GnmiTransportOptions opts *GnmiTransportOptions
config *gnmi.Config
} }
tests := []struct { tests := []struct {
name string name string
...@@ -587,10 +588,12 @@ func TestNewGnmiTransport(t *testing.T) { ...@@ -587,10 +588,12 @@ func TestNewGnmiTransport(t *testing.T) {
{ {
name: "default", name: "default",
args: args{opts: &GnmiTransportOptions{ args: args{opts: &GnmiTransportOptions{
Username: "test", Config: gnmi.Config{
Password: "test", Username: "test",
Addr: "localhost:13371", Password: "test",
Encoding: gpb.Encoding_PROTO, Addr: "localhost:13371",
Encoding: gpb.Encoding_PROTO,
},
}}, }},
want: &Gnmi{ want: &Gnmi{
config: &gnmi.Config{ config: &gnmi.Config{
...@@ -605,19 +608,19 @@ func TestNewGnmiTransport(t *testing.T) { ...@@ -605,19 +608,19 @@ func TestNewGnmiTransport(t *testing.T) {
}, },
{ {
name: "unsupported compression", name: "unsupported compression",
args: args{config: &gnmi.Config{Compression: "brotli"}}, args: args{opts: &GnmiTransportOptions{Config: gnmi.Config{Compression: "brotli"}}},
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
{ {
name: "certificate error no key file", name: "certificate error no key file",
args: args{config: &gnmi.Config{TLS: true, CertFile: "invalid", KeyFile: ""}}, args: args{opts: &GnmiTransportOptions{Config: gnmi.Config{TLS: true, CertFile: "invalid", KeyFile: ""}}},
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
{ {
name: "certificate error no ca file", name: "certificate error no ca file",
args: args{config: &gnmi.Config{TLS: true, CAFile: "invalid"}}, args: args{opts: &GnmiTransportOptions{Config: gnmi.Config{TLS: true, CAFile: "invalid"}}},
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
......
...@@ -20,10 +20,12 @@ func testSetupIntegration() { ...@@ -20,10 +20,12 @@ func testSetupIntegration() {
} }
opt = &GnmiTransportOptions{ opt = &GnmiTransportOptions{
Addr: address, Config: gnmi.Config{
Username: "admin", Addr: address,
Password: "arista", Username: "admin",
Encoding: gpb.Encoding_JSON_IETF, Password: "arista",
Encoding: gpb.Encoding_JSON_IETF,
},
} }
} }
...@@ -48,21 +50,23 @@ func TestGnmi_SetIntegration(t *testing.T) { ...@@ -48,21 +50,23 @@ func TestGnmi_SetIntegration(t *testing.T) {
{ {
name: "destination unreachable", name: "destination unreachable",
fields: fields{opt: &GnmiTransportOptions{ fields: fields{opt: &GnmiTransportOptions{
Addr: "203.0.113.10:6030", Config: gnmi.Config{
Addr: "203.0.113.10:6030",
},
}, },
}, },
args: args{ args: args{
ctx: context.Background(), ctx: context.Background(),
params: []interface{}{&gnmi.Operation{}}, params: []interface{}{&gnmi.Operation{}},
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
{ {
name: "valid update", name: "valid update",
fields: fields{opt: opt}, fields: fields{opt: opt},
args: args{ args: args{
ctx: context.Background(), ctx: context.Background(),
params: []interface{}{ params: []interface{}{
&gnmi.Operation{ &gnmi.Operation{
Type: "update", Type: "update",
...@@ -206,7 +210,9 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) { ...@@ -206,7 +210,9 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) {
{ {
name: "destination unreachable", name: "destination unreachable",
fields: fields{opt: &GnmiTransportOptions{ fields: fields{opt: &GnmiTransportOptions{
Addr: "203.0.113.10:6030", Config: gnmi.Config{
Addr: "203.0.113.10:6030",
},
}, },
}, },
args: args{ctx: context.Background()}, args: args{ctx: context.Background()},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment