Skip to content

Commit

Permalink
feat(dp): Enodebd triggered update (magma#12804)
Browse files Browse the repository at this point in the history
* feat(dp): CBSD update modification

Signed-off-by: Wojciech Sadowy <wojciech.sadowy@freedomfi.com>

* chore(dp): Add lock to db framework

Signed-off-by: Kuba Marciniszyn <kuba@freedomfi.com>

* chore(orc8r): Add sql locker to sqorc lib

Not all sql dialects support locking feature (SELECT FOR UPDATE),
so in order to use locking for different dialects,
a locker interface was introduced.

Signed-off-by: Kuba Marciniszyn <kuba@freedomfi.com>

* fixed tests

Signed-off-by: Wojciech Sadowy <wojciech.sadowy@freedomfi.com>

* moved pointer converters

Signed-off-by: Wojciech Sadowy <wojciech.sadowy@freedomfi.com>

* fixes

Signed-off-by: Wojciech Sadowy <wojciech.sadowy@freedomfi.com>

Co-authored-by: Wojciech Sadowy <wojciech.sadowy@freedomfi.com>
Co-authored-by: Kuba Marciniszyn <kuba@freedomfi.com>
  • Loading branch information
3 people authored and emakeev committed Aug 5, 2022
1 parent 8f67cff commit c7a1b3d
Show file tree
Hide file tree
Showing 28 changed files with 2,641 additions and 1,078 deletions.
826 changes: 548 additions & 278 deletions dp/cloud/go/protos/cbsd.pb.go

Large diffs are not rendered by default.

615 changes: 615 additions & 0 deletions dp/cloud/go/services/dp/builders/builders.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dp/cloud/go/services/dp/dp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
if err != nil {
glog.Fatalf("Error opening db connection: %s", err)
}
cbsdStore := dp_storage.NewCbsdManager(db, sqorc.GetSqlBuilder(), sqorc.GetErrorChecker())
cbsdStore := dp_storage.NewCbsdManager(db, sqorc.GetSqlBuilder(), sqorc.GetErrorChecker(), sqorc.GetSqlLocker())

interval := time.Second * time.Duration(serviceConfig.CbsdInactivityIntervalSec)
protos.RegisterCbsdManagementServer(srv.GrpcServer, servicers.NewCbsdManager(cbsdStore, interval))
Expand Down
2 changes: 1 addition & 1 deletion dp/cloud/go/services/dp/obsidian/cbsd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func updateCbsd(c echo.Context) error {
data := models.CbsdToBackend(payload)
req := protos.UpdateCbsdRequest{NetworkId: networkId, Id: int64(id), Data: data}
ctx := c.Request().Context()
_, ierr := client.UpdateCbsd(ctx, &req)
_, ierr := client.UserUpdateCbsd(ctx, &req)
if ierr != nil {
return getHttpError(ierr)
}
Expand Down
210 changes: 63 additions & 147 deletions dp/cloud/go/services/dp/obsidian/cbsd/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"magma/dp/cloud/go/dp"
"magma/dp/cloud/go/protos"
dp_service "magma/dp/cloud/go/services/dp"
b "magma/dp/cloud/go/services/dp/builders"
"magma/dp/cloud/go/services/dp/obsidian/cbsd"
"magma/dp/cloud/go/services/dp/obsidian/models"
"magma/dp/cloud/go/services/dp/obsidian/to_pointer"
"magma/dp/cloud/go/services/dp/storage/db"
"magma/orc8r/cloud/go/obsidian"
"magma/orc8r/cloud/go/obsidian/tests"
Expand Down Expand Up @@ -91,7 +91,7 @@ func (s *HandlersTestSuite) TestListCbsds() {
paramNames: []string{"network_id"},
ParamValues: []string{"n1"},
expectedStatus: http.StatusOK,
expectedResult: getPaginatedCbsds(),
expectedResult: b.GetPaginatedCbsds(b.NewCbsdModelPayloadBuilder().WithGrant()),
expectedError: "",
queryParamsString: "",
expectedListRequest: &protos.ListCbsdRequest{
Expand All @@ -104,7 +104,7 @@ func (s *HandlersTestSuite) TestListCbsds() {
paramNames: []string{"network_id"},
ParamValues: []string{"n1"},
expectedStatus: http.StatusOK,
expectedResult: getPaginatedCbsds(),
expectedResult: b.GetPaginatedCbsds(b.NewCbsdModelPayloadBuilder().WithGrant()),
expectedError: "",
queryParamsString: "?limit=4&offset=3",
expectedListRequest: &protos.ListCbsdRequest{
Expand All @@ -120,7 +120,7 @@ func (s *HandlersTestSuite) TestListCbsds() {
paramNames: []string{"network_id"},
ParamValues: []string{"n1"},
expectedStatus: http.StatusOK,
expectedResult: getPaginatedCbsds(),
expectedResult: b.GetPaginatedCbsds(b.NewCbsdModelPayloadBuilder().WithGrant()),
expectedError: "",
queryParamsString: "?limit=4&offset=3&serial_number=foo123",
expectedListRequest: &protos.ListCbsdRequest{
Expand Down Expand Up @@ -154,7 +154,12 @@ func (s *HandlersTestSuite) TestListCbsds() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
s.cbsdServer.listResponse = &protos.ListCbsdResponse{
Details: []*protos.CbsdDetails{getCbsdDetails()},
Details: []*protos.CbsdDetails{
b.NewDetailedProtoCbsdBuilder(
b.NewCbsdProtoPayloadBuilder()).
WithGrant().
WithCbsdId("some_cbsd_id").
Details},
TotalCount: 1,
}
listCbsds := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdsPath, obsidian.GET).HandlerFunc
Expand All @@ -180,13 +185,17 @@ func (s *HandlersTestSuite) TestListCbsds() {
func (s *HandlersTestSuite) TestFetchCbsd() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
s.cbsdServer.fetchResponse = &protos.FetchCbsdResponse{Details: getCbsdDetails()}
s.cbsdServer.fetchResponse = &protos.FetchCbsdResponse{Details: b.NewDetailedProtoCbsdBuilder(
b.NewCbsdProtoPayloadBuilder()).
WithGrant().
WithCbsdId("some_cbsd_id").
Details}
s.cbsdServer.expectedFetchRequest = &protos.FetchCbsdRequest{
NetworkId: "n1",
Id: 1,
}
fetchCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdPath, obsidian.GET).HandlerFunc
expectedResult := getCbsd()
expectedResult := b.NewCbsdModelPayloadBuilder().WithGrant().Payload
tc := tests.Test{
Method: http.MethodGet,
URL: "/magma/v1/dp/n1/cbsds/1",
Expand Down Expand Up @@ -225,32 +234,51 @@ func (s *HandlersTestSuite) TestFetchNonexistentCbsd() {
}

func (s *HandlersTestSuite) TestCreateCbsd() {
e := echo.New()

obsidianHandlers := cbsd.GetHandlers()
payload := createOrUpdateCbsdPayload()
s.cbsdServer.createResponse = &protos.CreateCbsdResponse{}
s.cbsdServer.expectedCreateRequest = &protos.CreateCbsdRequest{
NetworkId: "n1",
Data: getCbsdData(),
}
createCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdsPath, obsidian.POST).HandlerFunc
tc := tests.Test{
Method: http.MethodPost,
URL: "/magma/v1/dp/n1/cbsds",
Payload: payload,
ParamNames: []string{"network_id"},
ParamValues: []string{"n1"},
Handler: createCbsd,
ExpectedStatus: http.StatusCreated,
testCases := []struct {
name string
inputPayload *models.MutableCbsd
expectedPayload *protos.CbsdData
}{{
name: "test create without installation param",
inputPayload: b.NewMutableCbsdModelPayloadBuilder().Payload,
expectedPayload: b.NewCbsdProtoPayloadBuilder().Payload,
}, {
name: "test create with antenna gain",
inputPayload: b.NewMutableCbsdModelPayloadBuilder().
WithAntennaGain(10.5).Payload,
expectedPayload: b.NewCbsdProtoPayloadBuilder().
WithEmptyInstallationParam().
WithAntennaGain(10.5).Payload,
}}
for _, tc := range testCases {
s.Run(tc.name, func() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
payload := tc.inputPayload
s.cbsdServer.createResponse = &protos.CreateCbsdResponse{}
s.cbsdServer.expectedCreateRequest = &protos.CreateCbsdRequest{
NetworkId: "n1",
Data: tc.expectedPayload,
}
createCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdsPath, obsidian.POST).HandlerFunc
tc := tests.Test{
Method: http.MethodPost,
URL: "/magma/v1/dp/n1/cbsds",
Payload: payload,
ParamNames: []string{"network_id"},
ParamValues: []string{"n1"},
Handler: createCbsd,
ExpectedStatus: http.StatusCreated,
}
tests.RunUnitTest(s.T(), e, tc)
})
}
tests.RunUnitTest(s.T(), e, tc)
}

func (s *HandlersTestSuite) TestCreateWithDuplicateUniqueFieldsReturnsConflict() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
payload := createOrUpdateCbsdPayload()
payload := b.NewMutableCbsdModelPayloadBuilder().Payload
const errMsg = "some error"
s.cbsdServer.err = status.Error(codes.AlreadyExists, errMsg)
s.cbsdServer.expectedCreateRequest = &protos.CreateCbsdRequest{
Expand All @@ -274,13 +302,7 @@ func (s *HandlersTestSuite) TestCreateWithDuplicateUniqueFieldsReturnsConflict()
func (s *HandlersTestSuite) TestCreateCbsdWithoutAllRequiredParams() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
payload := &models.MutableCbsd{
Capabilities: models.Capabilities{
AntennaGain: to_pointer.Float(1),
NumberOfAntennas: 1,
},
SerialNumber: "someSerialNumber",
}
payload := b.NewMutableCbsdModelPayloadBuilder().Empty().WithSerialNumber("someSerialNumber").Payload
createCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdsPath, obsidian.POST).HandlerFunc
tc := tests.Test{
Method: http.MethodPost,
Expand Down Expand Up @@ -344,10 +366,10 @@ func (s *HandlersTestSuite) TestUpdateCbsd() {
obsidianHandlers := cbsd.GetHandlers()
s.cbsdServer.updateResponse = &protos.UpdateCbsdResponse{}
updateCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdPath, obsidian.PUT).HandlerFunc
payload := createOrUpdateCbsdPayload()
payload := b.NewMutableCbsdModelPayloadBuilder().Payload
s.cbsdServer.expectedUpdateRequest = &protos.UpdateCbsdRequest{
NetworkId: "n1",
Data: getCbsdData(),
Data: b.NewCbsdProtoPayloadBuilder().Payload,
}
tc := tests.Test{
Method: http.MethodPut,
Expand All @@ -365,13 +387,7 @@ func (s *HandlersTestSuite) TestUpdateCbsd() {
func (s *HandlersTestSuite) TestUpdateCbsdWithoutAllRequiredParams() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
payload := &models.MutableCbsd{
Capabilities: models.Capabilities{
AntennaGain: to_pointer.Float(1),
NumberOfAntennas: 1,
},
SerialNumber: "someSerialNumber",
}
payload := b.NewMutableCbsdModelPayloadBuilder().Empty().WithSerialNumber("someSerialNumber").Payload
updateCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdPath, obsidian.PUT).HandlerFunc
tc := tests.Test{
Method: http.MethodPut,
Expand All @@ -391,10 +407,10 @@ func (s *HandlersTestSuite) TestUpdateNonexistentCbsd() {
obsidianHandlers := cbsd.GetHandlers()
const errorMsg = "some msg"
s.cbsdServer.err = status.Error(codes.NotFound, errorMsg)
payload := createOrUpdateCbsdPayload()
payload := b.NewMutableCbsdModelPayloadBuilder().Payload
s.cbsdServer.expectedUpdateRequest = &protos.UpdateCbsdRequest{
NetworkId: "n1",
Data: getCbsdData(),
Data: b.NewCbsdProtoPayloadBuilder().Payload,
}
updateCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdPath, obsidian.PUT).HandlerFunc
tc := tests.Test{
Expand All @@ -413,7 +429,7 @@ func (s *HandlersTestSuite) TestUpdateNonexistentCbsd() {
func (s *HandlersTestSuite) TestUpdateCbsdWithDuplicateUniqueFieldsReturnsConflict() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
payload := createOrUpdateCbsdPayload()
payload := b.NewMutableCbsdModelPayloadBuilder().Payload
const errMsg = "some error"
s.cbsdServer.err = status.Error(codes.AlreadyExists, errMsg)
s.cbsdServer.expectedUpdateRequest = &protos.UpdateCbsdRequest{
Expand Down Expand Up @@ -599,7 +615,7 @@ func (s *stubCbsdServer) CreateCbsd(_ context.Context, request *protos.CreateCbs
return s.createResponse, s.err
}

func (s *stubCbsdServer) UpdateCbsd(_ context.Context, request *protos.UpdateCbsdRequest) (*protos.UpdateCbsdResponse, error) {
func (s *stubCbsdServer) UserUpdateCbsd(_ context.Context, request *protos.UpdateCbsdRequest) (*protos.UpdateCbsdResponse, error) {
assert.Equal(s.t, s.expectedUpdateRequest.NetworkId, request.NetworkId)
assert.Equal(s.t, s.expectedUpdateRequest.Id, request.Id)
assert.Equal(s.t, s.expectedUpdateRequest.Data, request.Data)
Expand Down Expand Up @@ -630,103 +646,3 @@ func (s *stubCbsdServer) DeregisterCbsd(_ context.Context, request *protos.Dereg
assert.Equal(s.t, s.expectedDeregisterRequest.Id, request.Id)
return s.deregisterResponse, s.err
}

func getPaginatedCbsds() *models.PaginatedCbsds {
return &models.PaginatedCbsds{
Cbsds: []*models.Cbsd{getCbsd()},
TotalCount: 1,
}
}

func getCbsd() *models.Cbsd {
return &models.Cbsd{
Capabilities: models.Capabilities{
AntennaGain: to_pointer.Float(1),
MaxPower: to_pointer.Float(24),
MinPower: to_pointer.Float(0),
NumberOfAntennas: 1,
},
CbsdID: "someCbsdId",
DesiredState: "registered",
FccID: "someFCCId",
FrequencyPreferences: models.FrequencyPreferences{
BandwidthMhz: 10,
FrequenciesMhz: []int64{3600},
},
Grant: &models.Grant{
BandwidthMhz: 0,
FrequencyMhz: 0,
GrantExpireTime: to_pointer.TimeToDateTime(0),
MaxEirp: 0,
State: "someState",
TransmitExpireTime: to_pointer.TimeToDateTime(0),
},
ID: 0,
IsActive: false,
SerialNumber: "someSerialNumber",
State: "unregistered",
UserID: "someUserId",
CbsdCategory: "b",
SingleStepEnabled: false,
}
}

func createOrUpdateCbsdPayload() *models.MutableCbsd {
return &models.MutableCbsd{
Capabilities: models.Capabilities{
AntennaGain: to_pointer.Float(1),
MaxPower: to_pointer.Float(24),
MinPower: to_pointer.Float(0),
NumberOfAntennas: 1,
},
DesiredState: "registered",
SingleStepEnabled: to_pointer.Bool(false),
CbsdCategory: "b",
FrequencyPreferences: models.FrequencyPreferences{
BandwidthMhz: 10,
FrequenciesMhz: []int64{3600},
},
FccID: "someFCCId",
SerialNumber: "someSerialNumber",
UserID: "someUserId",
}
}

func getCbsdData() *protos.CbsdData {
return &protos.CbsdData{
UserId: "someUserId",
FccId: "someFCCId",
SerialNumber: "someSerialNumber",
Capabilities: &protos.Capabilities{
MinPower: 0,
MaxPower: 24,
NumberOfAntennas: 1,
AntennaGain: 1,
},
Preferences: &protos.FrequencyPreferences{
BandwidthMhz: 10,
FrequenciesMhz: []int64{3600},
},
DesiredState: "registered",
CbsdCategory: "b",
SingleStepEnabled: false,
}
}

func getCbsdDetails() *protos.CbsdDetails {
return &protos.CbsdDetails{
Id: 0,
Data: getCbsdData(),
CbsdId: "someCbsdId",
State: "unregistered",
IsActive: false,
Grant: &protos.GrantDetails{
BandwidthMhz: 0,
FrequencyMhz: 0,
MaxEirp: 0,
State: "someState",
TransmitExpireTimestamp: 0,
GrantExpireTimestamp: 0,
},
}
}
18 changes: 0 additions & 18 deletions dp/cloud/go/services/dp/obsidian/models/capabilities_swaggergen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7a1b3d

Please sign in to comment.