Skip to content

Commit

Permalink
feat(dp): Add enodebd update checker (magma#12803)
Browse files Browse the repository at this point in the history
It checks if cbsd should be updated with parameters from enodebd

Signed-off-by: Kuba Marciniszyn <kuba@freedomfi.com>
  • Loading branch information
jkmar authored and mpfirrmann committed May 31, 2022
1 parent 7e4e4cc commit b352be1
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
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)
})
}
}

0 comments on commit b352be1

Please sign in to comment.