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

[scd] oir upsert: factor out building and validating of the OIR #1099

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions pkg/scd/models/operational_intents.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (

"github.com/golang/geo/s2"
restapi "github.com/interuss/dss/pkg/api/scdv1"
dsserr "github.com/interuss/dss/pkg/errors"
dssmodels "github.com/interuss/dss/pkg/models"
"github.com/interuss/stacktrace"
)

// Aggregates constants for operational intents.
Expand Down Expand Up @@ -128,25 +126,6 @@ func (o *OperationalIntent) ToRest() *restapi.OperationalIntentReference {
return result
}

// ValidateTimeRange validates the time range of o.
func (o *OperationalIntent) ValidateTimeRange() error {
if o.StartTime == nil {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Operation must have an time_start")
}

// EndTime cannot be omitted for new Operational Intents.
if o.EndTime == nil {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Operation must have an time_end")
}

// EndTime cannot be before StartTime.
if o.EndTime.Sub(*o.StartTime) < 0 {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Operation time_end must be after time_start")
}

return nil
}

// SetCells is a convenience function that accepts an int64 array and converts
// to s2.CellUnion.
// TODO: wrap s2.CellUnion in a custom type that embeds the struct such that
Expand Down
58 changes: 31 additions & 27 deletions pkg/scd/operational_intents_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,32 @@ type validOIRParams struct {
key map[scdmodels.OVN]bool
}

func (vp *validOIRParams) toOIR(manager dssmodels.Manager, attachedSub *scdmodels.Subscription, version int32) *scdmodels.OperationalIntent {
// For OIR's in the accepted state, we may not have a attachedSub available,
// in such cases the attachedSub ID on scdmodels.OperationalIntent will be nil
// and will be replaced with the 'NullV4UUID' when sent over to a client.
var subID *dssmodels.ID
if attachedSub != nil {
// Note: do _not_ use vp.subscriptionID here, as it may be empty
subID = &attachedSub.ID
}
return &scdmodels.OperationalIntent{
ID: vp.id,
Manager: manager,
Version: scdmodels.VersionNumber(version),

StartTime: vp.uExtent.StartTime,
EndTime: vp.uExtent.EndTime,
AltitudeLower: vp.uExtent.SpatialVolume.AltitudeLo,
AltitudeUpper: vp.uExtent.SpatialVolume.AltitudeHi,
Cells: vp.cells,

USSBaseURL: vp.ussBaseURL,
SubscriptionID: subID,
State: vp.state,
}
}

// validateAndReturnUpsertParams checks that the parameters for an Operational Intent Reference upsert are valid.
// Note that this does NOT check for anything related to access controls: any error returned should be labeled
// as a dsserr.BadRequest.
Expand Down Expand Up @@ -463,6 +489,10 @@ func validateAndReturnUpsertParams(
return nil, stacktrace.NewError("OperationalIntents may not end in the past")
}

if valid.uExtent.StartTime.After(*valid.uExtent.EndTime) {
return nil, stacktrace.NewError("Operation time_end must be after time_start")
}

valid.cells, err = valid.uExtent.CalculateSpatialCovering()
if err != nil {
return nil, stacktrace.Propagate(err, "Invalid area")
Expand Down Expand Up @@ -808,34 +838,8 @@ func (a *Server) upsertOperationalIntentReference(ctx context.Context, authorize
}
}

// For OIR's in the accepted state, we may not have a subscription available,
// in such cases the subscription ID on scdmodels.OperationalIntent will be nil
// and will be replaced with the 'NullV4UUID' when sent over to a client.
var subID *dssmodels.ID = nil
if sub != nil {
subID = &sub.ID
}

// Construct the new OperationalIntent
op := &scdmodels.OperationalIntent{
ID: validParams.id,
Manager: manager,
Version: scdmodels.VersionNumber(version + 1),

StartTime: validParams.uExtent.StartTime,
EndTime: validParams.uExtent.EndTime,
AltitudeLower: validParams.uExtent.SpatialVolume.AltitudeLo,
AltitudeUpper: validParams.uExtent.SpatialVolume.AltitudeHi,
Cells: validParams.cells,

USSBaseURL: validParams.ussBaseURL,
SubscriptionID: subID,
State: validParams.state,
}
err = op.ValidateTimeRange()
if err != nil {
return stacktrace.Propagate(err, "Error validating time range")
}
op := validParams.toOIR(manager, sub, version+1)

// Upsert the OperationalIntent
op, err = r.UpsertOperationalIntent(ctx, op)
Expand Down
Loading