diff --git a/nucleus/cli-handling.go b/nucleus/cli-handling.go index 9bfbf9d7b0f9751b157f6965211dafe946fb9ab6..e46979d9d9ac68a373f5b64f7c79b903228a1c4a 100644 --- a/nucleus/cli-handling.go +++ b/nucleus/cli-handling.go @@ -218,8 +218,8 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad } sbi := s.core.principalNetworkDomains[uuidPND].GetSBIs()["default"] - newDevice, err := NewDevice(sbi, "gnmi", in.Device.Address, in.Device.Username, - in.Device.Password, &Encoding{E: gpb.Encoding_JSON_IETF}) + newDevice, err := NewDevice("gnmi", sbi, &gnmiTransportOptions{in.Device.Address, in.Device.Username, + in.Device.Password, sbi.SetNode(), gpb.Encoding_JSON_IETF}) if err != nil { log.Info(err) return &pb.AddDeviceReply{Message: err.Error()}, err diff --git a/nucleus/device.go b/nucleus/device.go index bb1b4ee9cd35310b240422b8bae4b1a9f7f856c9..694a2d0004bd53e06f24b60197a54c1550b1cd57 100644 --- a/nucleus/device.go +++ b/nucleus/device.go @@ -20,19 +20,23 @@ type Device struct { } //NewDevice creates a Device -func NewDevice(sbi SouthboundInterface, transportType, addr, username, password string, options ...TransportOptions) (*Device, error) { - transport, err := NewTransport(transportType, sbi, addr, username, password, options...) - if err != nil { - return nil, err +func NewDevice(ttype string, sbi SouthboundInterface, opts TransportOptions) (*Device, error) { + var transport Transport + //TODO: change to switch if supported transports increase + if ttype == "gnmi" { + transport = NewGnmiTransport(opts.(*gnmiTransportOptions)) + } else if ttype == "restconf" { + //TODO: implement restconf -> NewRestConfTransport(opts) } return &Device{ GoStruct: sbi.Schema().Root, SBI: sbi, + //TODO: needed? maybe change to TransportOptions Config: DeviceConfig{ Uuid: uuid.New(), - Address: addr, - Username: username, - Password: password, + Address: opts.GetAddress(), + Username: opts.GetUsername(), + Password: opts.GetPassword(), }, Transport: transport, }, nil diff --git a/nucleus/gnmi_transport.go b/nucleus/gnmi_transport.go index 478629ae76b896c527dbf4d7f93b5fd91b7fb805..c55c8d0974cd0fa6bea390b9655b68e3f52316ff 100644 --- a/nucleus/gnmi_transport.go +++ b/nucleus/gnmi_transport.go @@ -18,6 +18,13 @@ type Gnmi struct { config *gnmi.Config } +func NewGnmiTransport(opts *gnmiTransportOptions) *Gnmi { + ngt := &Gnmi{SetNode: opts.setNode} + config := ngt.CreateConfig(opts) + ngt.SetConfig(config) + return ngt +} + //SetConfig sets the config of gnmi func (g *Gnmi) SetConfig(config *gnmi.Config) { g.config = config @@ -29,12 +36,12 @@ func (g *Gnmi) GetConfig() *gnmi.Config { } //CreateConfig creates a new gnmi config based on the given parameters -func (g *Gnmi) CreateConfig(addr, username, password string, encoding gpb.Encoding) *gnmi.Config { +func (g *Gnmi) CreateConfig(opts *gnmiTransportOptions) *gnmi.Config { return &gnmi.Config{ - Addr: addr, - Username: username, - Password: password, - Encoding: encoding, + Addr: opts.addr, + Username: opts.username, + Password: opts.password, + Encoding: opts.encoding, } } @@ -184,3 +191,25 @@ func (g *Gnmi) subscribe(ctx context.Context) error { func (g *Gnmi) Close() error { return nil } + +type gnmiTransportOptions struct { + // all needed gnmi transport parameters + addr string + username string + password string + setNode func(schema *yang.Entry, root interface{}, path *gpb.Path, + val interface{}, opts ...ytypes.SetNodeOpt) error + encoding gpb.Encoding +} + +func (gto *gnmiTransportOptions) GetAddress() string { + return gto.addr +} +func (gto *gnmiTransportOptions) GetUsername() string { + return gto.username +} +func (gto *gnmiTransportOptions) GetPassword() string { + return gto.password +} + +func (gto *gnmiTransportOptions) IsTransportOption() {} diff --git a/nucleus/transport.go b/nucleus/transport.go index 024c795a6f09309c76a5b51696dbbc9c5bfb432d..814e750f137657943e7fc063d347bbcaaf891c49 100644 --- a/nucleus/transport.go +++ b/nucleus/transport.go @@ -3,8 +3,6 @@ package nucleus import ( "bytes" "context" - "errors" - gpb "github.com/openconfig/gnmi/proto/gnmi" "github.com/openconfig/ygot/ytypes" "io" ) @@ -20,27 +18,6 @@ type Transport interface { ProcessResponse(resp interface{}, root interface{}, models *ytypes.Schema) error } -func NewTransport(transportType string, sbi SouthboundInterface, - addr, username, password string, options ...TransportOptions) (Transport, error) { - //change to a switch if the supported transports increase - if transportType == "gnmi" { - gnmi := &Gnmi{SetNode: sbi.SetNode()} - e, exists := hasEncoding(options) - if !exists { - return nil, errors.New("missing Encoding option") - } - config := gnmi.CreateConfig(addr, username, password, e.E) - gnmi.SetConfig(config) - return gnmi, nil - } - if transportType == "restconf" { - //TODO: has to be changed as soon as we - // start to implement restconf support - return &Restconf{}, nil - } - return nil, errors.New("Transport type is not supported.") -} - // YANGConsumer is a auxillary type to redirect the response // of an RESTCONF call to a ygot YANG unmarshaler type YANGConsumer struct { @@ -54,22 +31,8 @@ func (yc YANGConsumer) Consume(reader io.Reader, _ interface{}) error { } type TransportOptions interface { + GetAddress() string + GetUsername() string + GetPassword() string IsTransportOption() } - -//TODO: will there be other transports that user different encodings, -// or is this just a gnmi thing? if true -> move into gnmi_transport -type Encoding struct { - E gpb.Encoding -} - -func (o *Encoding) IsTransportOption() {} - -func hasEncoding(transportOptions []TransportOptions) (*Encoding, bool) { - for _, option := range transportOptions { - if e, exists := option.(*Encoding); exists { - return e, true - } - } - return nil, false -}