Skip to content

Commit

Permalink
[scd] oir upsert: factor out building and validating of the OIR (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shastick authored Sep 6, 2024
1 parent 7d74523 commit 83bc148
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 48 deletions.
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

0 comments on commit 83bc148

Please sign in to comment.