Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dp): Add enodebd update checker #12803

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions dp/cloud/go/services/dp/storage/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package storage

import (
"math"
)

const (
radiusM = 6371e3
distanceThresholdM = 10
)

func ShouldENodeBDUpdate(prev *DBCbsd, next *DBCbsd) bool {
return canUpdate(prev) &&
(paramsChanges(next, prev) || coordinatesChanged(next, prev))
}

func canUpdate(prev *DBCbsd) bool {
return !prev.CpiDigitalSignature.Valid
}

func paramsChanges(prev *DBCbsd, next *DBCbsd) bool {
return prev.HeightM != next.HeightM ||
prev.HeightType != next.HeightType ||
prev.IndoorDeployment != next.IndoorDeployment ||
prev.AntennaGain != next.AntennaGain
}

func coordinatesChanged(prev *DBCbsd, next *DBCbsd) bool {
return coordinatesAreEmpty(prev, next) ||
distanceM(
prev.LatitudeDeg.Float64, prev.LongitudeDeg.Float64,
next.LatitudeDeg.Float64, next.LongitudeDeg.Float64,
) > distanceThresholdM
}

func coordinatesAreEmpty(prev *DBCbsd, next *DBCbsd) bool {
return !prev.LatitudeDeg.Valid ||
!prev.LongitudeDeg.Valid ||
!next.LatitudeDeg.Valid ||
!next.LongitudeDeg.Valid
}

func distanceM(lat1 float64, lon1 float64, lat2 float64, lon2 float64) float64 {
lat1 = lat1 * math.Pi / 180
lat2 = lat2 * math.Pi / 180
lon1 = lon1 * math.Pi / 180
lon2 = lon2 * math.Pi / 180
dLon := lon2 - lon1
dLat := lat2 - lat1
sinLat := math.Sin(dLat / 2)
sinLon := math.Sin(dLon / 2)
res := sinLat*sinLat + math.Cos(lat1)*math.Cos(lat2)*sinLon*sinLon
return 2 * math.Asin(math.Sqrt(res)) * radiusM
}
165 changes: 165 additions & 0 deletions dp/cloud/go/services/dp/storage/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package storage_test

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"

"magma/dp/cloud/go/services/dp/storage"
"magma/dp/cloud/go/services/dp/storage/db"
)

func TestShouldENodeBDUpdate(t *testing.T) {
// TODO switch to builders when available
testData := []struct {
name string
prev *storage.DBCbsd
next *storage.DBCbsd
expected bool
}{{
name: "should update if installation parameters have changes",
prev: &storage.DBCbsd{
AntennaGain: db.MakeFloat(10),
CbsdCategory: sql.NullString{},
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(5),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(false),
CpiDigitalSignature: sql.NullString{},
},
next: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
expected: true,
}, {
name: "should not update all parameters are the same",
prev: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
next: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
expected: false,
}, {
name: "should not update if cbsd had cpi signature",
prev: &storage.DBCbsd{
AntennaGain: db.MakeFloat(10),
CbsdCategory: sql.NullString{},
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(5),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(false),
CpiDigitalSignature: db.MakeString("some signature"),
},
next: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
expected: false,
}, {
name: "should update if any of coordinates are empty",
prev: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: sql.NullFloat64{},
LongitudeDeg: sql.NullFloat64{},
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
next: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: sql.NullFloat64{},
LongitudeDeg: sql.NullFloat64{},
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
expected: true,
}, {
name: "should not update if coordinates changed less than 10m",
prev: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
next: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50.00006),
LongitudeDeg: db.MakeFloat(100.0001),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
expected: false,
}, {
name: "should update if coordinates changed more than 10m",
prev: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50),
LongitudeDeg: db.MakeFloat(100),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
next: &storage.DBCbsd{
AntennaGain: db.MakeFloat(20),
CbsdCategory: db.MakeString("A"),
LatitudeDeg: db.MakeFloat(50.00007),
LongitudeDeg: db.MakeFloat(100.0001),
HeightM: db.MakeFloat(8),
HeightType: db.MakeString("AGL"),
IndoorDeployment: db.MakeBool(true),
CpiDigitalSignature: sql.NullString{},
},
expected: true,
}}
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
actual := storage.ShouldENodeBDUpdate(tt.prev, tt.next)
assert.Equal(t, tt.expected, actual)
})
}
}