From 4acc92e2b701282ad1641daa830dda6b811fa9a6 Mon Sep 17 00:00:00 2001 From: Martin Stiemerling <martin.stiemerling@h-da.de> Date: Mon, 20 Dec 2021 12:25:39 +0100 Subject: [PATCH] Added an uuidInput parameter to NewDevice and removed NewDeviceWithUUID. uuidInput must be set to uuid.Nil if no uuid is already present, otherwise use the uuid. --- nucleus/device.go | 45 ++++---------------------- nucleus/device_test.go | 2 +- nucleus/principalNetworkDomain.go | 6 ++-- nucleus/principalNetworkDomain_test.go | 4 +-- 4 files changed, 12 insertions(+), 45 deletions(-) diff --git a/nucleus/device.go b/nucleus/device.go index a5f356dcd..80034d540 100644 --- a/nucleus/device.go +++ b/nucleus/device.go @@ -15,48 +15,15 @@ import ( ) // NewDevice creates a Device -func NewDevice(name string, opt *tpb.TransportOption, sbi southbound.SouthboundInterface) (device.Device, error) { +func NewDevice(name string, uuidInput uuid.UUID, opt *tpb.TransportOption, sbi southbound.SouthboundInterface) (device.Device, error) { t, err := NewTransport(opt, sbi) if err != nil { return nil, err } - if name == "" { - name = namesgenerator.GetRandomName(0) - } - - root, err := ygot.DeepCopy(sbi.Schema().Root) - if err != nil { - return nil, err - } - if opt.Type == spb.Type_CONTAINERISED { - return &CsbiDevice{ - CommonDevice: CommonDevice{ - UUID: uuid.New(), - GoStruct: root, - sbi: sbi, - transport: t, - name: name, - transportOptions: opt, - }, - }, nil - } - - return &CommonDevice{ - UUID: uuid.New(), - GoStruct: root, - sbi: sbi, - transport: t, - name: name, - transportOptions: opt, - }, nil -} - -// NewDeviceWithUUID creates a Device with a provided UUID -func NewDeviceWithUUID(name string, uuid uuid.UUID, opt *tpb.TransportOption, sbi southbound.SouthboundInterface) (device.Device, error) { - t, err := NewTransport(opt, sbi) - if err != nil { - return nil, err + // TODO: this needs to check the case that the uuidInput is set, as the same uuid may be already stored. + if uuidInput == uuid.Nil { + uuidInput = uuid.New() } if name == "" { @@ -70,7 +37,7 @@ func NewDeviceWithUUID(name string, uuid uuid.UUID, opt *tpb.TransportOption, sb if opt.Type == spb.Type_CONTAINERISED { return &CsbiDevice{ CommonDevice: CommonDevice{ - UUID: uuid, + UUID: uuidInput, GoStruct: root, sbi: sbi, transport: t, @@ -81,7 +48,7 @@ func NewDeviceWithUUID(name string, uuid uuid.UUID, opt *tpb.TransportOption, sb } return &CommonDevice{ - UUID: uuid, + UUID: uuidInput, GoStruct: root, sbi: sbi, transport: t, diff --git a/nucleus/device_test.go b/nucleus/device_test.go index 30364ce40..7b6a5e843 100644 --- a/nucleus/device_test.go +++ b/nucleus/device_test.go @@ -99,7 +99,7 @@ func TestNewDevice(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - resp, err := NewDevice(tt.args.name, tt.args.opts, tt.args.sbi) + resp, err := NewDevice(tt.args.name, uuid.Nil, tt.args.opts, tt.args.sbi) if (err != nil) != tt.wantErr { t.Errorf("NewDevice() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go index 3c5dc0e48..b5dd33d7a 100644 --- a/nucleus/principalNetworkDomain.go +++ b/nucleus/principalNetworkDomain.go @@ -173,7 +173,7 @@ func (pnd *pndImplementation) AddDevice(name string, opt *tpb.TransportOption, s } } - d, err := NewDevice(name, opt, sbi) + d, err := NewDevice(name, uuid.Nil, opt, sbi) if err != nil { return err } @@ -191,7 +191,7 @@ func (pnd *pndImplementation) AddDeviceFromStore(name string, deviceUUID uuid.UU return err } - d, err := NewDeviceWithUUID(name, deviceUUID, opt, sbi) + d, err := NewDevice(name, deviceUUID, opt, sbi) if err != nil { return err } @@ -444,7 +444,7 @@ func (pnd *pndImplementation) createCsbiDevice(ctx context.Context, name string, TransportOption: opt.TransportOption, } log.WithField("transport option", csbiTransportOptions).Debug("gosdn gnmi transport options") - d, err := NewDevice(name, csbiTransportOptions, csbi) + d, err := NewDevice(name, uuid.Nil, csbiTransportOptions, csbi) if err != nil { panic(err) } diff --git a/nucleus/principalNetworkDomain_test.go b/nucleus/principalNetworkDomain_test.go index bf2cd336e..93c91f400 100644 --- a/nucleus/principalNetworkDomain_test.go +++ b/nucleus/principalNetworkDomain_test.go @@ -676,7 +676,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) { func Test_pndImplementation_GetDevice(t *testing.T) { pnd := newPnd() sbi := NewSBI(spb.Type_OPENCONFIG) - d, err := NewDevice("", newGnmiTransportOptions(), sbi) + d, err := NewDevice("", uuid.Nil, newGnmiTransportOptions(), sbi) if err != nil { t.Error(err) return @@ -726,7 +726,7 @@ func Test_pndImplementation_GetDevice(t *testing.T) { func Test_pndImplementation_GetDeviceByName(t *testing.T) { p := newPnd() sbi := NewSBI(spb.Type_OPENCONFIG) - d, err := NewDevice("my-device", newGnmiTransportOptions(), sbi) + d, err := NewDevice("my-device", uuid.Nil, newGnmiTransportOptions(), sbi) if err != nil { t.Error(err) return -- GitLab