-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dp): Add enodebd update checker (#12803)
It checks if cbsd should be updated with parameters from enodebd Signed-off-by: Kuba Marciniszyn <kuba@freedomfi.com>
- Loading branch information
Showing
2 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |