Skip to content

Commit 4d2a9e9

Browse files
committed
ns: Remove MAC settings profile reference on end device delete
1 parent 977968e commit 4d2a9e9

2 files changed

Lines changed: 144 additions & 9 deletions

File tree

pkg/networkserver/grpc_deviceregistry.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1613,10 +1613,25 @@ func (ns *NetworkServer) Delete(ctx context.Context, req *ttnpb.EndDeviceIdentif
16131613
return nil, err
16141614
}
16151615
var evt events.Event
1616-
_, _, err := ns.devices.SetByID(ctx, req.ApplicationIds, req.DeviceId, nil, func(ctx context.Context, dev *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error) {
1616+
_, _, err := ns.devices.SetByID(ctx, req.ApplicationIds, req.DeviceId, []string{"mac_settings_profile_ids"}, func(ctx context.Context, dev *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error) { // nolint: lll
16171617
if dev == nil {
16181618
return nil, nil, errDeviceNotFound.New()
16191619
}
1620+
if dev.MacSettingsProfileIds != nil {
1621+
_, err := ns.macSettingsProfiles.Set(
1622+
ctx,
1623+
dev.MacSettingsProfileIds,
1624+
[]string{"ids", "mac_settings", "end_devices_count"},
1625+
func(_ context.Context, existing *ttnpb.MACSettingsProfile) (*ttnpb.MACSettingsProfile, []string, error) {
1626+
if existing.EndDevicesCount > 0 {
1627+
existing.EndDevicesCount--
1628+
}
1629+
return existing, []string{"ids", "mac_settings", "end_devices_count"}, nil
1630+
})
1631+
if err != nil {
1632+
return nil, nil, err
1633+
}
1634+
}
16201635
evt = evtDeleteEndDevice.NewWithIdentifiersAndData(ctx, req, nil)
16211636
return nil, nil, nil
16221637
})

pkg/networkserver/grpc_deviceregistry_test.go

Lines changed: 128 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,10 +1290,12 @@ func TestDeviceRegistryDelete(t *testing.T) {
12901290
for _, tc := range []struct {
12911291
Name string
12921292
ContextFunc func(context.Context) context.Context
1293-
SetByIDFunc func(context.Context, *ttnpb.ApplicationIdentifiers, string, []string, func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error)) (*ttnpb.EndDevice, context.Context, error)
1293+
SetByIDFunc func(context.Context, *ttnpb.ApplicationIdentifiers, string, []string, func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error)) (*ttnpb.EndDevice, context.Context, error) // nolint: lll
1294+
SetFunc func(context.Context, *ttnpb.MACSettingsProfileIdentifiers, []string, func(context.Context, *ttnpb.MACSettingsProfile) (*ttnpb.MACSettingsProfile, []string, error)) (*ttnpb.MACSettingsProfile, error) // nolint: lll
12941295
Request *ttnpb.EndDeviceIdentifiers
12951296
ErrorAssertion func(*testing.T, error) bool
12961297
SetByIDCalls uint64
1298+
SetCalls uint64
12971299
}{
12981300
{
12991301
Name: "No device write rights",
@@ -1308,7 +1310,13 @@ func TestDeviceRegistryDelete(t *testing.T) {
13081310
}),
13091311
})
13101312
},
1311-
SetByIDFunc: func(ctx context.Context, appID *ttnpb.ApplicationIdentifiers, devID string, gets []string, f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error)) (*ttnpb.EndDevice, context.Context, error) {
1313+
SetByIDFunc: func(
1314+
ctx context.Context,
1315+
_ *ttnpb.ApplicationIdentifiers,
1316+
_ string,
1317+
_ []string,
1318+
_ func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error),
1319+
) (*ttnpb.EndDevice, context.Context, error) {
13121320
err := errors.New("SetByIDFunc must not be called")
13131321
test.MustTFromContext(ctx).Error(err)
13141322
return nil, ctx, err
@@ -1339,12 +1347,18 @@ func TestDeviceRegistryDelete(t *testing.T) {
13391347
}),
13401348
})
13411349
},
1342-
SetByIDFunc: func(ctx context.Context, appID *ttnpb.ApplicationIdentifiers, devID string, gets []string, f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error)) (*ttnpb.EndDevice, context.Context, error) {
1350+
SetByIDFunc: func(
1351+
ctx context.Context,
1352+
appID *ttnpb.ApplicationIdentifiers,
1353+
devID string,
1354+
gets []string,
1355+
f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error),
1356+
) (*ttnpb.EndDevice, context.Context, error) {
13431357
t := test.MustTFromContext(ctx)
13441358
a := assertions.New(t)
13451359
a.So(appID, should.Resemble, &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"})
13461360
a.So(devID, should.Equal, "test-dev-id")
1347-
a.So(gets, should.BeNil)
1361+
a.So(gets, should.Equal, []string{"mac_settings_profile_ids"})
13481362

13491363
dev, sets, err := f(ctx, nil)
13501364
if !a.So(errors.IsNotFound(err), should.BeTrue) {
@@ -1374,11 +1388,17 @@ func TestDeviceRegistryDelete(t *testing.T) {
13741388
}),
13751389
})
13761390
},
1377-
SetByIDFunc: func(ctx context.Context, appID *ttnpb.ApplicationIdentifiers, devID string, gets []string, f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error)) (*ttnpb.EndDevice, context.Context, error) {
1391+
SetByIDFunc: func(
1392+
ctx context.Context,
1393+
appID *ttnpb.ApplicationIdentifiers,
1394+
devID string,
1395+
gets []string,
1396+
f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error),
1397+
) (*ttnpb.EndDevice, context.Context, error) {
13781398
a := assertions.New(test.MustTFromContext(ctx))
13791399
a.So(appID, should.Resemble, &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"})
13801400
a.So(devID, should.Equal, "test-dev-id")
1381-
a.So(gets, should.BeNil)
1401+
a.So(gets, should.Equal, []string{"mac_settings_profile_ids"})
13821402

13831403
dev, sets, err := f(ctx, &ttnpb.EndDevice{
13841404
Ids: &ttnpb.EndDeviceIdentifiers{
@@ -1399,24 +1419,123 @@ func TestDeviceRegistryDelete(t *testing.T) {
13991419
},
14001420
SetByIDCalls: 1,
14011421
},
1422+
1423+
{
1424+
Name: "Existing device with MAC settings profile",
1425+
ContextFunc: func(ctx context.Context) context.Context {
1426+
return rights.NewContext(ctx, &rights.Rights{
1427+
ApplicationRights: *rights.NewMap(map[string]*ttnpb.Rights{
1428+
unique.ID(test.Context(), &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"}): {
1429+
Rights: []ttnpb.Right{
1430+
ttnpb.Right_RIGHT_APPLICATION_DEVICES_WRITE,
1431+
},
1432+
},
1433+
}),
1434+
})
1435+
},
1436+
SetByIDFunc: func(
1437+
ctx context.Context,
1438+
appID *ttnpb.ApplicationIdentifiers,
1439+
devID string,
1440+
gets []string,
1441+
f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error),
1442+
) (*ttnpb.EndDevice, context.Context, error) {
1443+
a := assertions.New(test.MustTFromContext(ctx))
1444+
a.So(appID, should.Resemble, &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"})
1445+
a.So(devID, should.Equal, "test-dev-id")
1446+
a.So(gets, should.Equal, []string{"mac_settings_profile_ids"})
1447+
1448+
dev, sets, err := f(ctx, &ttnpb.EndDevice{
1449+
Ids: &ttnpb.EndDeviceIdentifiers{
1450+
DeviceId: "test-dev-id",
1451+
ApplicationIds: &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"},
1452+
},
1453+
MacSettingsProfileIds: &ttnpb.MACSettingsProfileIdentifiers{
1454+
ProfileId: "test-mac-settings-profile-id",
1455+
ApplicationIds: &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"},
1456+
},
1457+
})
1458+
if !a.So(err, should.BeNil) {
1459+
return nil, ctx, err
1460+
}
1461+
a.So(sets, should.BeNil)
1462+
a.So(dev, should.BeNil)
1463+
return nil, ctx, nil
1464+
},
1465+
SetFunc: func(
1466+
ctx context.Context,
1467+
ids *ttnpb.MACSettingsProfileIdentifiers,
1468+
paths []string,
1469+
f func(context.Context, *ttnpb.MACSettingsProfile) (*ttnpb.MACSettingsProfile, []string, error),
1470+
) (*ttnpb.MACSettingsProfile, error) {
1471+
a := assertions.New(test.MustTFromContext(ctx))
1472+
a.So(ids, should.Resemble, &ttnpb.MACSettingsProfileIdentifiers{
1473+
ProfileId: "test-mac-settings-profile-id",
1474+
ApplicationIds: &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"},
1475+
})
1476+
a.So(paths, should.Equal, []string{"ids", "mac_settings", "end_devices_count"})
1477+
profile, sets, err := f(ctx, &ttnpb.MACSettingsProfile{
1478+
Ids: &ttnpb.MACSettingsProfileIdentifiers{
1479+
ProfileId: "test-mac-settings-profile-id",
1480+
ApplicationIds: &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"},
1481+
},
1482+
EndDevicesCount: 1,
1483+
})
1484+
if !a.So(err, should.BeNil) {
1485+
return nil, err
1486+
}
1487+
a.So(sets, should.Equal, []string{"ids", "mac_settings", "end_devices_count"})
1488+
a.So(profile, should.Resemble, &ttnpb.MACSettingsProfile{
1489+
Ids: &ttnpb.MACSettingsProfileIdentifiers{
1490+
ProfileId: "test-mac-settings-profile-id",
1491+
ApplicationIds: &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"},
1492+
},
1493+
EndDevicesCount: 0,
1494+
})
1495+
return profile, nil
1496+
},
1497+
Request: &ttnpb.EndDeviceIdentifiers{
1498+
DeviceId: "test-dev-id",
1499+
ApplicationIds: &ttnpb.ApplicationIdentifiers{ApplicationId: "test-app-id"},
1500+
},
1501+
SetByIDCalls: 1,
1502+
SetCalls: 1,
1503+
},
14021504
} {
14031505
tc := tc
14041506
test.RunSubtest(t, test.SubtestConfig{
14051507
Name: tc.Name,
14061508
Parallel: true,
14071509
Func: func(ctx context.Context, t *testing.T, a *assertions.Assertion) {
1408-
var setByIDCalls uint64
1510+
var setByIDCalls, setCalls uint64
14091511

14101512
ns, ctx, env, stop := StartTest(
14111513
ctx,
14121514
TestConfig{
14131515
NetworkServer: Config{
14141516
Devices: &MockDeviceRegistry{
1415-
SetByIDFunc: func(ctx context.Context, appID *ttnpb.ApplicationIdentifiers, devID string, gets []string, f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error)) (*ttnpb.EndDevice, context.Context, error) {
1517+
SetByIDFunc: func(
1518+
ctx context.Context,
1519+
appID *ttnpb.ApplicationIdentifiers,
1520+
devID string,
1521+
gets []string,
1522+
f func(context.Context, *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error),
1523+
) (*ttnpb.EndDevice, context.Context, error) {
14161524
atomic.AddUint64(&setByIDCalls, 1)
14171525
return tc.SetByIDFunc(ctx, appID, devID, gets, f)
14181526
},
14191527
},
1528+
MACSettingsProfileRegistry: &MockMACSettingsProfileRegistry{
1529+
SetFunc: func(
1530+
ctx context.Context,
1531+
ids *ttnpb.MACSettingsProfileIdentifiers,
1532+
paths []string,
1533+
f func(context.Context, *ttnpb.MACSettingsProfile) (*ttnpb.MACSettingsProfile, []string, error),
1534+
) (*ttnpb.MACSettingsProfile, error) {
1535+
atomic.AddUint64(&setCalls, 1)
1536+
return tc.SetFunc(ctx, ids, paths, f)
1537+
},
1538+
},
14201539
},
14211540
TaskStarter: StartTaskExclude(
14221541
DownlinkProcessTaskName,
@@ -1436,6 +1555,7 @@ func TestDeviceRegistryDelete(t *testing.T) {
14361555
req := ttnpb.Clone(tc.Request)
14371556
res, err := ttnpb.NewNsEndDeviceRegistryClient(ns.LoopbackConn()).Delete(ctx, req)
14381557
a.So(setByIDCalls, should.Equal, tc.SetByIDCalls)
1558+
a.So(setCalls, should.Equal, tc.SetCalls)
14391559
if tc.ErrorAssertion != nil && a.So(tc.ErrorAssertion(t, err), should.BeTrue) {
14401560
a.So(res, should.BeNil)
14411561
} else if a.So(err, should.BeNil) {

0 commit comments

Comments
 (0)