Skip to content

Commit

Permalink
feat(dp): Remove grant channel relationship
Browse files Browse the repository at this point in the history
From now grant will store channel data himself,
this is because grant channel does not have to match
any of spectrum inquiry channels.
For example there can be [3550, 3560], [3560, 3570]
spectrum inquiry channels, but grant can be on [3555, 3565].

Signed-off-by: Kuba Marciniszyn <kuba@freedomfi.com>
  • Loading branch information
jkmar committed Mar 4, 2022
1 parent 6b28797 commit 45e715a
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 311 deletions.
6 changes: 3 additions & 3 deletions dp/cloud/go/services/dp/servicers/cbsd_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func cbsdFromDatabase(data *storage.DetailedCbsd, inactivityInterval time.Durati
const mega int64 = 1e6
var grant *protos.GrantDetails
if data.GrantState.Name.Valid {
bandwidth := (data.Channel.HighFrequency.Int64 - data.Channel.LowFrequency.Int64) / mega
frequency := (data.Channel.HighFrequency.Int64 + data.Channel.LowFrequency.Int64) / (mega * 2)
bandwidth := (data.Grant.HighFrequency.Int64 - data.Grant.LowFrequency.Int64) / mega
frequency := (data.Grant.HighFrequency.Int64 + data.Grant.LowFrequency.Int64) / (mega * 2)
grant = &protos.GrantDetails{
BandwidthMhz: bandwidth,
FrequencyMhz: frequency,
MaxEirp: data.Channel.LastUsedMaxEirp.Float64,
MaxEirp: data.Grant.MaxEirp.Float64,
State: data.GrantState.Name.String,
TransmitExpireTimestamp: data.Grant.TransmitExpireTime.Time.Unix(),
GrantExpireTimestamp: data.Grant.GrantExpireTime.Time.Unix(),
Expand Down
9 changes: 3 additions & 6 deletions dp/cloud/go/services/dp/servicers/cbsd_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func (s *CbsdManagerTestSuite) TestFetchCbsdWithoutGrant() {
s.store.details = &storage.DetailedCbsd{
Cbsd: getDBCbsd(),
CbsdState: &storage.DBCbsdState{},
Channel: &storage.DBChannel{},
Grant: &storage.DBGrant{},
GrantState: &storage.DBGrantState{},
}
Expand Down Expand Up @@ -297,14 +296,12 @@ func getDetailedCbsd() *storage.DetailedCbsd {
CbsdState: &storage.DBCbsdState{
Name: db.MakeString("registered"),
},
Channel: &storage.DBChannel{
LowFrequency: db.MakeInt(3600 * 1e6),
HighFrequency: db.MakeInt(3620 * 1e6),
LastUsedMaxEirp: db.MakeFloat(35),
},
Grant: &storage.DBGrant{
GrantExpireTime: db.MakeTime(time.Unix(2e9, 0).UTC()),
TransmitExpireTime: db.MakeTime(time.Unix(1e9, 0).UTC()),
LowFrequency: db.MakeInt(3600 * 1e6),
HighFrequency: db.MakeInt(3620 * 1e6),
MaxEirp: db.MakeFloat(35),
},
GrantState: &storage.DBGrantState{
Name: db.MakeString("authorized"),
Expand Down
21 changes: 9 additions & 12 deletions dp/cloud/go/services/dp/storage/cbsd_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type DetailedCbsdList struct {
type DetailedCbsd struct {
Cbsd *DBCbsd
CbsdState *DBCbsdState
Channel *DBChannel
Grant *DBGrant
GrantState *DBGrantState
}
Expand Down Expand Up @@ -233,9 +232,8 @@ func convertToDetails(models []db.Model) *DetailedCbsd {
return &DetailedCbsd{
Cbsd: models[0].(*DBCbsd),
CbsdState: models[1].(*DBCbsdState),
Channel: models[2].(*DBChannel),
Grant: models[3].(*DBGrant),
GrantState: models[4].(*DBGrantState),
Grant: models[2].(*DBGrant),
GrantState: models[3].(*DBGrantState),
}
}

Expand All @@ -248,15 +246,14 @@ func buildDetailedCbsdQuery(builder sq.StatementBuilderType) *db.Query {
From(&DBCbsdState{}).
Select(db.NewIncludeMask("name"))).
Join(db.NewQuery().
From(&DBChannel{}).
Select(db.NewIncludeMask("low_frequency", "high_frequency", "last_used_max_eirp")).
From(&DBGrant{}).
Select(db.NewIncludeMask(
"grant_expire_time", "transmit_expire_time",
"low_frequency", "high_frequency", "max_eirp")).
Join(db.NewQuery().
From(&DBGrant{}).
Select(db.NewIncludeMask("grant_expire_time", "transmit_expire_time")).
Join(db.NewQuery().
From(&DBGrantState{}).
Select(db.NewIncludeMask("name")).
Where(sq.NotEq{GrantStateTable + ".name": "idle"}))).
From(&DBGrantState{}).
Select(db.NewIncludeMask("name")).
Where(sq.NotEq{GrantStateTable + ".name": "idle"})).
Nullable())
}

Expand Down
91 changes: 11 additions & 80 deletions dp/cloud/go/services/dp/storage/cbsd_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (s *CbsdManagerTestSuite) TestFetchCbsdFromDifferentNetwork() {
s.Assert().ErrorIs(err, merrors.ErrNotFound)
}

func (s *CbsdManagerTestSuite) TestFetchCbsdWithoutGrantAndChannel() {
func (s *CbsdManagerTestSuite) TestFetchCbsdWithoutGrant() {
var cbsdId int64
err := s.resourceManager.InTransaction(func() {
state := s.enumMaps[storage.CbsdStateTable]["registered"]
Expand All @@ -269,43 +269,19 @@ func (s *CbsdManagerTestSuite) TestFetchCbsdWithoutGrantAndChannel() {
expected := &storage.DetailedCbsd{
Cbsd: getDetailedCbsd(cbsdId),
CbsdState: &storage.DBCbsdState{Name: db.MakeString("registered")},
Channel: &storage.DBChannel{},
Grant: &storage.DBGrant{},
GrantState: &storage.DBGrantState{},
}
s.Assert().Equal(expected, actual)
}

func (s *CbsdManagerTestSuite) TestFetchCbsdWithoutGrantButWithChannel() {
func (s *CbsdManagerTestSuite) TestFetchCbsdWithIdleGrant() {
var cbsdId int64
err := s.resourceManager.InTransaction(func() {
state := s.enumMaps[storage.CbsdStateTable]["registered"]
cbsdId = s.givenResourceInserted(getCbsd(someNetwork, state))
s.givenResourceInserted(getChannel(cbsdId))
})
s.Require().NoError(err)

actual, err := s.cbsdManager.FetchCbsd(someNetwork, cbsdId)
s.Require().NoError(err)

expected := &storage.DetailedCbsd{
Cbsd: getDetailedCbsd(cbsdId),
CbsdState: &storage.DBCbsdState{Name: db.MakeString("registered")},
Channel: &storage.DBChannel{},
Grant: &storage.DBGrant{},
GrantState: &storage.DBGrantState{},
}
s.Assert().Equal(expected, actual)
}

func (s *CbsdManagerTestSuite) TestFetchCbsdWithIdleGrantAndChannel() {
var cbsdId int64
err := s.resourceManager.InTransaction(func() {
state := s.enumMaps[storage.CbsdStateTable]["registered"]
cbsdId = s.givenResourceInserted(getCbsd(someNetwork, state))
channelId := s.givenResourceInserted(getChannel(cbsdId))
grantState := s.enumMaps[storage.GrantStateTable]["idle"]
s.givenResourceInserted(getGrant(grantState, channelId))
s.givenResourceInserted(getGrant(grantState, cbsdId))
})
s.Require().NoError(err)

Expand All @@ -315,21 +291,19 @@ func (s *CbsdManagerTestSuite) TestFetchCbsdWithIdleGrantAndChannel() {
expected := &storage.DetailedCbsd{
Cbsd: getDetailedCbsd(cbsdId),
CbsdState: &storage.DBCbsdState{Name: db.MakeString("registered")},
Channel: &storage.DBChannel{},
Grant: &storage.DBGrant{},
GrantState: &storage.DBGrantState{},
}
s.Assert().Equal(expected, actual)
}

func (s *CbsdManagerTestSuite) TestFetchCbsdWithGrantAndChannel() {
func (s *CbsdManagerTestSuite) TestFetchCbsdWithGrant() {
var cbsdId int64
err := s.resourceManager.InTransaction(func() {
state := s.enumMaps[storage.CbsdStateTable]["registered"]
cbsdId = s.givenResourceInserted(getCbsd(someNetwork, state))
channelId := s.givenResourceInserted(getChannel(cbsdId))
grantState := s.enumMaps[storage.GrantStateTable]["authorized"]
s.givenResourceInserted(getGrant(grantState, channelId))
s.givenResourceInserted(getGrant(grantState, cbsdId))
})
s.Require().NoError(err)

Expand All @@ -339,7 +313,6 @@ func (s *CbsdManagerTestSuite) TestFetchCbsdWithGrantAndChannel() {
expected := &storage.DetailedCbsd{
Cbsd: getDetailedCbsd(cbsdId),
CbsdState: &storage.DBCbsdState{Name: db.MakeString("registered")},
Channel: getBaseChannel(),
Grant: getBaseGrant(),
GrantState: &storage.DBGrantState{Name: db.MakeString("authorized")},
}
Expand All @@ -363,32 +336,6 @@ func (s *CbsdManagerTestSuite) TestListCbsdFromDifferentNetwork() {
s.Assert().Equal(expected, actual)
}

func (s *CbsdManagerTestSuite) TestListNotIncludeEmptyChannels() {
var cbsdId int64
err := s.resourceManager.InTransaction(func() {
state := s.enumMaps[storage.CbsdStateTable]["registered"]
cbsdId = s.givenResourceInserted(getCbsd(someNetwork, state))
s.givenResourceInserted(getChannel(cbsdId))
s.givenResourceInserted(getChannel(cbsdId))
})
s.Require().NoError(err)

actual, err := s.cbsdManager.ListCbsd(someNetwork, &storage.Pagination{})
s.Require().NoError(err)

expected := &storage.DetailedCbsdList{
Cbsds: []*storage.DetailedCbsd{{
Cbsd: getDetailedCbsd(cbsdId),
CbsdState: &storage.DBCbsdState{Name: db.MakeString("registered")},
Channel: &storage.DBChannel{},
Grant: &storage.DBGrant{},
GrantState: &storage.DBGrantState{},
}},
Count: 1,
}
s.Assert().Equal(expected, actual)
}

func (s *CbsdManagerTestSuite) TestListWithPagination() {
const count = 4
models := make([]db.Model, count)
Expand Down Expand Up @@ -419,7 +366,6 @@ func (s *CbsdManagerTestSuite) TestListWithPagination() {
expected.Cbsds[i] = &storage.DetailedCbsd{
Cbsd: &storage.DBCbsd{Id: db.MakeInt(int64(i + 1 + offset))},
CbsdState: &storage.DBCbsdState{Name: db.MakeString("unregistered")},
Channel: &storage.DBChannel{},
Grant: &storage.DBGrant{},
GrantState: &storage.DBGrantState{},
}
Expand All @@ -432,10 +378,9 @@ func (s *CbsdManagerTestSuite) TestListNotIncludeIdleGrants() {
err := s.resourceManager.InTransaction(func() {
state := s.enumMaps[storage.CbsdStateTable]["registered"]
cbsdId = s.givenResourceInserted(getCbsd(someNetwork, state))
channelId := s.givenResourceInserted(getChannel(cbsdId))
grantState := s.enumMaps[storage.GrantStateTable]["idle"]
s.givenResourceInserted(getGrant(grantState, channelId))
s.givenResourceInserted(getGrant(grantState, channelId))
s.givenResourceInserted(getGrant(grantState, cbsdId))
s.givenResourceInserted(getGrant(grantState, cbsdId))
})
s.Require().NoError(err)

Expand All @@ -446,7 +391,6 @@ func (s *CbsdManagerTestSuite) TestListNotIncludeIdleGrants() {
Cbsds: []*storage.DetailedCbsd{{
Cbsd: getDetailedCbsd(cbsdId),
CbsdState: &storage.DBCbsdState{Name: db.MakeString("registered")},
Channel: &storage.DBChannel{},
Grant: &storage.DBGrant{},
GrantState: &storage.DBGrantState{},
}},
Expand Down Expand Up @@ -516,29 +460,16 @@ func getBaseGrant() *storage.DBGrant {
base := &storage.DBGrant{}
base.GrantExpireTime = db.MakeTime(time.Unix(123, 0).UTC())
base.TransmitExpireTime = db.MakeTime(time.Unix(456, 0).UTC())
return base
}

func getGrant(stateId int64, channelId int64) *storage.DBGrant {
base := getBaseGrant()
base.StateId = db.MakeInt(stateId)
base.ChannelId = db.MakeInt(channelId)
return base
}

func getBaseChannel() *storage.DBChannel {
base := &storage.DBChannel{}
base.LowFrequency = db.MakeInt(3600 * 1e6)
base.HighFrequency = db.MakeInt(3620 * 1e6)
base.LastUsedMaxEirp = db.MakeFloat(35)
base.MaxEirp = db.MakeFloat(35)
return base
}

func getChannel(cbsdId int64) *storage.DBChannel {
base := getBaseChannel()
func getGrant(stateId int64, cbsdId int64) *storage.DBGrant {
base := getBaseGrant()
base.CbsdId = db.MakeInt(cbsdId)
base.ChannelType = db.MakeString("some_channel_type")
base.RuleApplied = db.MakeString("some_rule_applied")
base.StateId = db.MakeInt(stateId)
return base
}

Expand Down
18 changes: 12 additions & 6 deletions dp/cloud/go/services/dp/storage/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ type DBGrant struct {
Id sql.NullInt64
StateId sql.NullInt64
CbsdId sql.NullInt64
ChannelId sql.NullInt64
GrantId sql.NullInt64
GrantExpireTime sql.NullTime
TransmitExpireTime sql.NullTime
HeartbeatInterval sql.NullInt64
ChannelType sql.NullString
LowFrequency sql.NullInt64
HighFrequency sql.NullInt64
MaxEirp sql.NullFloat64
}

func (g *DBGrant) Fields() db.FieldMap {
Expand All @@ -225,10 +227,6 @@ func (g *DBGrant) Fields() db.FieldMap {
BaseType: db.IntType{X: &g.CbsdId},
Nullable: true,
},
"channel_id": &db.Field{
BaseType: db.IntType{X: &g.ChannelId},
Nullable: true,
},
"grant_id": &db.Field{
BaseType: db.IntType{X: &g.GrantId},
},
Expand All @@ -248,6 +246,15 @@ func (g *DBGrant) Fields() db.FieldMap {
BaseType: db.StringType{X: &g.ChannelType},
Nullable: true,
},
"low_frequency": &db.Field{
BaseType: db.IntType{X: &g.LowFrequency},
},
"high_frequency": &db.Field{
BaseType: db.IntType{X: &g.HighFrequency},
},
"max_eirp": &db.Field{
BaseType: db.FloatType{X: &g.MaxEirp},
},
}
}

Expand All @@ -257,7 +264,6 @@ func (g *DBGrant) GetMetadata() *db.ModelMetadata {
Relations: map[string]string{
GrantStateTable: "state_id",
CbsdTable: "cbsd_id",
ChannelTable: "channel_id",
},
CreateObject: func() db.Model {
return &DBGrant{}
Expand Down
4 changes: 2 additions & 2 deletions dp/cloud/go/services/dp/storage/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ func TestFields(t *testing.T) {
name: "check field names for DBGrant",
model: &storage.DBGrant{},
expected: []string{
"id", "state_id", "cbsd_id", "channel_id", "grant_id",
"id", "state_id", "cbsd_id", "grant_id",
"grant_expire_time", "transmit_expire_time",
"heartbeat_interval", "channel_type",
"low_frequency", "high_frequency", "max_eirp",
},
},
{
Expand Down Expand Up @@ -178,7 +179,6 @@ func TestGetMetadata(t *testing.T) {
Table: storage.GrantTable,
Relations: map[string]string{
storage.CbsdTable: "cbsd_id",
storage.ChannelTable: "channel_id",
storage.GrantStateTable: "state_id",
},
},
Expand Down

0 comments on commit 45e715a

Please sign in to comment.