-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathoperational_intents_handler.go
744 lines (654 loc) · 29 KB
/
operational_intents_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package scd
import (
"context"
"time"
"github.com/golang/geo/s2"
"github.com/google/uuid"
"github.com/interuss/dss/pkg/api"
restapi "github.com/interuss/dss/pkg/api/scdv1"
"github.com/interuss/dss/pkg/auth"
dsserr "github.com/interuss/dss/pkg/errors"
dssmodels "github.com/interuss/dss/pkg/models"
scdmodels "github.com/interuss/dss/pkg/scd/models"
"github.com/interuss/dss/pkg/scd/repos"
"github.com/interuss/stacktrace"
)
// DeleteOperationalIntentReference deletes a single operational intent ref for a given ID at
// the specified version.
func (a *Server) DeleteOperationalIntentReference(ctx context.Context, req *restapi.DeleteOperationalIntentReferenceRequest,
) restapi.DeleteOperationalIntentReferenceResponseSet {
if req.Auth.Error != nil {
resp := restapi.DeleteOperationalIntentReferenceResponseSet{}
setAuthError(ctx, stacktrace.Propagate(req.Auth.Error, "Auth failed"), &resp.Response401, &resp.Response403, &resp.Response500)
return resp
}
// Retrieve OperationalIntent ID
id, err := dssmodels.IDFromString(string(req.Entityid))
if err != nil {
return restapi.DeleteOperationalIntentReferenceResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format: `%s`", req.Entityid))}}
}
// Retrieve ID of client making call
if req.Auth.ClientID == nil {
return restapi.DeleteOperationalIntentReferenceResponseSet{Response403: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.PermissionDenied, "Missing manager"))}}
}
// Retrieve OVN
ovn := scdmodels.OVN(req.Ovn)
if ovn == "" {
return restapi.DeleteOperationalIntentReferenceResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Missing OVN for operational intent to modify"))}}
}
var response *restapi.ChangeOperationalIntentReferenceResponse
action := func(ctx context.Context, r repos.Repository) (err error) {
// Get OperationalIntent to delete
old, err := r.GetOperationalIntent(ctx, id)
if err != nil {
return stacktrace.Propagate(err, "Unable to get OperationIntent from repo")
}
if old == nil {
return stacktrace.NewErrorWithCode(dsserr.NotFound, "OperationalIntent %s not found", id)
}
// Validate deletion request
if old.Manager != dssmodels.Manager(*req.Auth.ClientID) {
return stacktrace.NewErrorWithCode(dsserr.PermissionDenied,
"OperationalIntent owned by %s, but %s attempted to delete", old.Manager, *req.Auth.ClientID)
}
if old.OVN != ovn {
return stacktrace.NewErrorWithCode(dsserr.VersionMismatch,
"Current version is %s but client specified version %s", old.OVN, ovn)
}
// Get the Subscription supporting the OperationalIntent, if one is defined
var sub *scdmodels.Subscription
removeImplicitSubscription := false
if old.SubscriptionID != nil {
sub, err = r.GetSubscription(ctx, *old.SubscriptionID)
if err != nil {
return stacktrace.Propagate(err, "Unable to get OperationalIntent's Subscription from repo")
}
if sub == nil {
return stacktrace.NewError("OperationalIntent's Subscription missing from repo")
}
if sub.ImplicitSubscription {
// Get the Subscription's dependent OperationalIntents
dependentOps, err := r.GetDependentOperationalIntents(ctx, sub.ID)
if err != nil {
return stacktrace.Propagate(err, "Could not find dependent OperationalIntents")
}
if len(dependentOps) == 0 {
return stacktrace.NewError("An implicit Subscription had no dependent OperationalIntents")
} else if len(dependentOps) == 1 {
removeImplicitSubscription = true
}
}
}
// Find Subscriptions that may overlap the OperationalIntent's Volume4D
allsubs, err := r.SearchSubscriptions(ctx, &dssmodels.Volume4D{
StartTime: old.StartTime,
EndTime: old.EndTime,
SpatialVolume: &dssmodels.Volume3D{
AltitudeHi: old.AltitudeUpper,
AltitudeLo: old.AltitudeLower,
Footprint: dssmodels.GeometryFunc(func() (s2.CellUnion, error) {
return old.Cells, nil
}),
}})
if err != nil {
return stacktrace.Propagate(err, "Unable to search Subscriptions in repo")
}
// Limit Subscription notifications to only those interested in OperationalIntents
subs := repos.Subscriptions{}
for _, s := range allsubs {
if s.NotifyForOperationalIntents {
subs = append(subs, s)
}
}
// Increment notification indices for Subscriptions to be notified
if err := subs.IncrementNotificationIndices(ctx, r); err != nil {
return stacktrace.Propagate(err, "Unable to increment notification indices")
}
// Delete OperationalIntent from repo
if err := r.DeleteOperationalIntent(ctx, id); err != nil {
return stacktrace.Propagate(err, "Unable to delete OperationalIntent from repo")
}
// removeImplicitSubscription is only true if the OIR had a subscription defined
if removeImplicitSubscription {
// Automatically remove a now-unused implicit Subscription
err = r.DeleteSubscription(ctx, sub.ID)
if err != nil {
return stacktrace.Propagate(err, "Unable to delete associated implicit Subscription")
}
}
// Return response to client
response = &restapi.ChangeOperationalIntentReferenceResponse{
OperationalIntentReference: *old.ToRest(),
Subscribers: makeSubscribersToNotify(subs),
}
return nil
}
err = a.Store.Transact(ctx, action)
if err != nil {
err = stacktrace.Propagate(err, "Could not delete operational intent")
errResp := &restapi.ErrorResponse{Message: dsserr.Handle(ctx, err)}
switch stacktrace.GetCode(err) {
case dsserr.PermissionDenied:
return restapi.DeleteOperationalIntentReferenceResponseSet{Response403: errResp}
case dsserr.NotFound:
return restapi.DeleteOperationalIntentReferenceResponseSet{Response404: errResp}
case dsserr.VersionMismatch:
return restapi.DeleteOperationalIntentReferenceResponseSet{Response409: errResp}
default:
return restapi.DeleteOperationalIntentReferenceResponseSet{Response500: &api.InternalServerErrorBody{
ErrorMessage: *dsserr.Handle(ctx, stacktrace.Propagate(err, "Got an unexpected error"))}}
}
}
return restapi.DeleteOperationalIntentReferenceResponseSet{Response200: response}
}
// GetOperationalIntentReference returns a single operation intent ref for the given ID.
func (a *Server) GetOperationalIntentReference(ctx context.Context, req *restapi.GetOperationalIntentReferenceRequest,
) restapi.GetOperationalIntentReferenceResponseSet {
if req.Auth.Error != nil {
resp := restapi.GetOperationalIntentReferenceResponseSet{}
setAuthError(ctx, stacktrace.Propagate(req.Auth.Error, "Auth failed"), &resp.Response401, &resp.Response403, &resp.Response500)
return resp
}
id, err := dssmodels.IDFromString(string(req.Entityid))
if err != nil {
return restapi.GetOperationalIntentReferenceResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format: `%s`", req.Entityid))}}
}
if req.Auth.ClientID == nil {
return restapi.GetOperationalIntentReferenceResponseSet{Response403: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.PermissionDenied, "Missing manager"))}}
}
var response *restapi.GetOperationalIntentReferenceResponse
action := func(ctx context.Context, r repos.Repository) (err error) {
op, err := r.GetOperationalIntent(ctx, id)
if err != nil {
return stacktrace.Propagate(err, "Unable to get OperationalIntent from repo")
}
if op == nil {
return stacktrace.NewErrorWithCode(dsserr.NotFound, "OperationalIntent %s not found", id)
}
if op.Manager != dssmodels.Manager(*req.Auth.ClientID) {
op.OVN = scdmodels.NoOvnPhrase
}
response = &restapi.GetOperationalIntentReferenceResponse{
OperationalIntentReference: *op.ToRest(),
}
return nil
}
err = a.Store.Transact(ctx, action)
if err != nil {
err = stacktrace.Propagate(err, "Could not get operational intent")
if stacktrace.GetCode(err) == dsserr.NotFound {
return restapi.GetOperationalIntentReferenceResponseSet{Response404: &restapi.ErrorResponse{Message: dsserr.Handle(ctx, err)}}
}
return restapi.GetOperationalIntentReferenceResponseSet{Response500: &api.InternalServerErrorBody{
ErrorMessage: *dsserr.Handle(ctx, stacktrace.Propagate(err, "Got an unexpected error"))}}
}
return restapi.GetOperationalIntentReferenceResponseSet{Response200: response}
}
// QueryOperationalIntentReferences queries existing operational intent refs in the given
// bounds.
func (a *Server) QueryOperationalIntentReferences(ctx context.Context, req *restapi.QueryOperationalIntentReferencesRequest,
) restapi.QueryOperationalIntentReferencesResponseSet {
if req.Auth.Error != nil {
resp := restapi.QueryOperationalIntentReferencesResponseSet{}
setAuthError(ctx, stacktrace.Propagate(req.Auth.Error, "Auth failed"), &resp.Response401, &resp.Response403, &resp.Response500)
return resp
}
if req.BodyParseError != nil {
return restapi.QueryOperationalIntentReferencesResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.PropagateWithCode(req.BodyParseError, dsserr.BadRequest, "Malformed params"))}}
}
// Retrieve the area of interest parameter
aoi := req.Body.AreaOfInterest
if aoi == nil {
return restapi.QueryOperationalIntentReferencesResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Missing area_of_interest"))}}
}
// Parse area of interest to common Volume4D
vol4, err := dssmodels.Volume4DFromSCDRest(aoi)
if err != nil {
return restapi.QueryOperationalIntentReferencesResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Error parsing geometry"))}}
}
// Retrieve ID of client making call
if req.Auth.ClientID == nil {
return restapi.QueryOperationalIntentReferencesResponseSet{Response403: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.PermissionDenied, "Missing manager"))}}
}
var response *restapi.QueryOperationalIntentReferenceResponse
action := func(ctx context.Context, r repos.Repository) (err error) {
// Perform search query on Store
ops, err := r.SearchOperationalIntents(ctx, vol4)
if err != nil {
return stacktrace.Propagate(err, "Unable to query for OperationalIntents in repo")
}
// Create response for client
response = &restapi.QueryOperationalIntentReferenceResponse{
OperationalIntentReferences: make([]restapi.OperationalIntentReference, 0, len(ops)),
}
for _, op := range ops {
p := op.ToRest()
if op.Manager != dssmodels.Manager(*req.Auth.ClientID) {
noOvnPhrase := restapi.EntityOVN(scdmodels.NoOvnPhrase)
p.Ovn = &noOvnPhrase
}
response.OperationalIntentReferences = append(response.OperationalIntentReferences, *p)
}
return nil
}
err = a.Store.Transact(ctx, action)
if err != nil {
err = stacktrace.Propagate(err, "Could not query operational intent")
if stacktrace.GetCode(err) == dsserr.BadRequest {
return restapi.QueryOperationalIntentReferencesResponseSet{Response400: &restapi.ErrorResponse{Message: dsserr.Handle(ctx, err)}}
}
return restapi.QueryOperationalIntentReferencesResponseSet{Response500: &api.InternalServerErrorBody{
ErrorMessage: *dsserr.Handle(ctx, stacktrace.Propagate(err, "Got an unexpected error"))}}
}
return restapi.QueryOperationalIntentReferencesResponseSet{Response200: response}
}
func (a *Server) CreateOperationalIntentReference(ctx context.Context, req *restapi.CreateOperationalIntentReferenceRequest,
) restapi.CreateOperationalIntentReferenceResponseSet {
if req.Auth.Error != nil {
resp := restapi.CreateOperationalIntentReferenceResponseSet{}
setAuthError(ctx, stacktrace.Propagate(req.Auth.Error, "Auth failed"), &resp.Response401, &resp.Response403, &resp.Response500)
return resp
}
if req.BodyParseError != nil {
return restapi.CreateOperationalIntentReferenceResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.PropagateWithCode(req.BodyParseError, dsserr.BadRequest, "Malformed params"))}}
}
respOK, respConflict, err := a.upsertOperationalIntentReference(ctx, &req.Auth, req.Entityid, "", req.Body)
if err != nil {
err = stacktrace.Propagate(err, "Could not put Operational Intent Reference")
errResp := &restapi.ErrorResponse{Message: dsserr.Handle(ctx, err)}
switch stacktrace.GetCode(err) {
case dsserr.PermissionDenied:
return restapi.CreateOperationalIntentReferenceResponseSet{Response403: errResp}
case dsserr.BadRequest, dsserr.NotFound:
return restapi.CreateOperationalIntentReferenceResponseSet{Response400: errResp}
case dsserr.VersionMismatch:
return restapi.CreateOperationalIntentReferenceResponseSet{Response409: &restapi.AirspaceConflictResponse{
Message: dsserr.Handle(ctx, err)}}
case dsserr.MissingOVNs:
return restapi.CreateOperationalIntentReferenceResponseSet{Response409: respConflict}
default:
return restapi.CreateOperationalIntentReferenceResponseSet{Response500: &api.InternalServerErrorBody{
ErrorMessage: *dsserr.Handle(ctx, stacktrace.Propagate(err, "Got an unexpected error"))}}
}
}
return restapi.CreateOperationalIntentReferenceResponseSet{Response201: respOK}
}
func (a *Server) UpdateOperationalIntentReference(ctx context.Context, req *restapi.UpdateOperationalIntentReferenceRequest,
) restapi.UpdateOperationalIntentReferenceResponseSet {
if req.Auth.Error != nil {
resp := restapi.UpdateOperationalIntentReferenceResponseSet{}
setAuthError(ctx, stacktrace.Propagate(req.Auth.Error, "Auth failed"), &resp.Response401, &resp.Response403, &resp.Response500)
return resp
}
if req.BodyParseError != nil {
return restapi.UpdateOperationalIntentReferenceResponseSet{Response400: &restapi.ErrorResponse{
Message: dsserr.Handle(ctx, stacktrace.PropagateWithCode(req.BodyParseError, dsserr.BadRequest, "Malformed params"))}}
}
respOK, respConflict, err := a.upsertOperationalIntentReference(ctx, &req.Auth, req.Entityid, req.Ovn, req.Body)
if err != nil {
err = stacktrace.Propagate(err, "Could not put subscription")
errResp := &restapi.ErrorResponse{Message: dsserr.Handle(ctx, err)}
switch stacktrace.GetCode(err) {
case dsserr.PermissionDenied:
return restapi.UpdateOperationalIntentReferenceResponseSet{Response403: errResp}
case dsserr.BadRequest, dsserr.NotFound:
return restapi.UpdateOperationalIntentReferenceResponseSet{Response400: errResp}
case dsserr.VersionMismatch:
return restapi.UpdateOperationalIntentReferenceResponseSet{Response409: &restapi.AirspaceConflictResponse{
Message: dsserr.Handle(ctx, err)}}
case dsserr.MissingOVNs:
return restapi.UpdateOperationalIntentReferenceResponseSet{Response409: respConflict}
default:
return restapi.UpdateOperationalIntentReferenceResponseSet{Response500: &api.InternalServerErrorBody{
ErrorMessage: *dsserr.Handle(ctx, stacktrace.Propagate(err, "Got an unexpected error"))}}
}
}
return restapi.UpdateOperationalIntentReferenceResponseSet{Response200: respOK}
}
// upsertOperationalIntentReference inserts or updates an Operational Intent.
// If the ovn argument is empty (""), it will attempt to create a new Operational Intent.
func (a *Server) upsertOperationalIntentReference(ctx context.Context, authorizedManager *api.AuthorizationResult, entityid restapi.EntityID, ovn restapi.EntityOVN, params *restapi.PutOperationalIntentReferenceParameters,
) (*restapi.ChangeOperationalIntentReferenceResponse, *restapi.AirspaceConflictResponse, error) {
if authorizedManager.ClientID == nil {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.PermissionDenied, "Missing manager")
}
manager := dssmodels.Manager(*authorizedManager.ClientID)
id, err := dssmodels.IDFromString(string(entityid))
if err != nil {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format: `%s`", entityid)
}
var (
extents = make([]*dssmodels.Volume4D, len(params.Extents))
)
if len(params.UssBaseUrl) == 0 {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Missing required UssBaseUrl")
}
if !a.AllowHTTPBaseUrls {
err = scdmodels.ValidateUSSBaseURL(string(params.UssBaseUrl))
if err != nil {
return nil, nil, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to validate base URL")
}
}
state := scdmodels.OperationalIntentState(params.State)
if !state.IsValidInDSS() {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid OperationalIntent state: %s", params.State)
}
hasCMSARole := auth.HasScope(authorizedManager.Scopes, restapi.UtmConformanceMonitoringSaScope)
if state.RequiresCMSA() && !hasCMSARole {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.PermissionDenied, "Missing `%s` Conformance Monitoring for Situational Awareness scope to transition to CMSA state: %s (see SCD0100)", restapi.UtmConformanceMonitoringSaScope, params.State)
}
for idx, extent := range params.Extents {
cExtent, err := dssmodels.Volume4DFromSCDRest(&extent)
if err != nil {
return nil, nil, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to parse extent %d", idx)
}
extents[idx] = cExtent
}
uExtent, err := dssmodels.UnionVolumes4D(extents...)
if err != nil {
return nil, nil, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to union extents")
}
if uExtent.StartTime == nil {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Missing time_start from extents")
}
if uExtent.EndTime == nil {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Missing time_end from extents")
}
if time.Now().After(*uExtent.EndTime) {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "OperationalIntents may not end in the past")
}
cells, err := uExtent.CalculateSpatialCovering()
if err != nil {
return nil, nil, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Invalid area")
}
if uExtent.EndTime.Before(*uExtent.StartTime) {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "End time is past the start time")
}
if ovn == "" && params.State != restapi.OperationalIntentState_Accepted {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid state for initial version: `%s`", params.State)
}
subscriptionID := dssmodels.ID("")
if params.SubscriptionId != nil {
subscriptionID, err = dssmodels.IDFromOptionalString(string(*params.SubscriptionId))
if err != nil {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format for Subscription ID: `%s`", *params.SubscriptionId)
}
}
// Check if a subscription is required for this request:
// OIRs in an accepted state do not need a subscription.
if state.RequiresSubscription() &&
subscriptionID.Empty() &&
(params.NewSubscription == nil ||
params.NewSubscription.UssBaseUrl == "") {
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Provided Operational Intent Reference state `%s` requires either a subscription ID or information to create an implicit subscription", state)
}
var responseOK *restapi.ChangeOperationalIntentReferenceResponse
var responseConflict *restapi.AirspaceConflictResponse
action := func(ctx context.Context, r repos.Repository) (err error) {
var version int32 // Version of the Operational Intent (0 means creation requested).
// Lock subscriptions based on the cell to reduce the number of retries under concurrent load.
// See issue #1002 for details.
err = r.LockSubscriptionsOnCells(ctx, cells)
if err != nil {
return stacktrace.Propagate(err, "Unable to acquire lock")
}
// Get existing OperationalIntent, if any, and validate request
old, err := r.GetOperationalIntent(ctx, id)
if err != nil {
return stacktrace.Propagate(err, "Could not get OperationalIntent from repo")
}
if old != nil {
if old.Manager != manager {
return stacktrace.NewErrorWithCode(dsserr.PermissionDenied,
"OperationalIntent owned by %s, but %s attempted to modify", old.Manager, manager)
}
if old.OVN != scdmodels.OVN(ovn) {
return stacktrace.NewErrorWithCode(dsserr.VersionMismatch,
"Current version is %s but client specified version %s", old.OVN, ovn)
}
version = int32(old.Version)
} else {
if ovn != "" {
return stacktrace.NewErrorWithCode(dsserr.NotFound, "OperationalIntent does not exist and therefore is not version %s", ovn)
}
version = 0
}
var sub *scdmodels.Subscription
if subscriptionID.Empty() {
// Create an implicit subscription if the implicit subscription params are set:
// for situations where these params are required but have not been set,
// an error will have been returned earlier.
// If they are not set at this point, continue without creating an implicit subscription.
if params.NewSubscription != nil && params.NewSubscription.UssBaseUrl != "" {
if !a.AllowHTTPBaseUrls {
err := scdmodels.ValidateUSSBaseURL(string(params.NewSubscription.UssBaseUrl))
if err != nil {
return stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to validate USS base URL")
}
}
subToUpsert := scdmodels.Subscription{
ID: dssmodels.ID(uuid.New().String()),
Manager: manager,
StartTime: uExtent.StartTime,
EndTime: uExtent.EndTime,
AltitudeLo: uExtent.SpatialVolume.AltitudeLo,
AltitudeHi: uExtent.SpatialVolume.AltitudeHi,
Cells: cells,
USSBaseURL: string(params.NewSubscription.UssBaseUrl),
NotifyForOperationalIntents: true,
ImplicitSubscription: true,
}
if params.NewSubscription.NotifyForConstraints != nil {
subToUpsert.NotifyForConstraints = *params.NewSubscription.NotifyForConstraints
}
sub, err = r.UpsertSubscription(ctx, &subToUpsert)
if err != nil {
return stacktrace.Propagate(err, "Failed to create implicit subscription")
}
}
} else {
// Use existing Subscription
sub, err = r.GetSubscription(ctx, subscriptionID)
if err != nil {
return stacktrace.Propagate(err, "Unable to get Subscription")
}
if sub == nil {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Specified Subscription %s does not exist", subscriptionID)
}
if sub.Manager != dssmodels.Manager(manager) {
return stacktrace.Propagate(
stacktrace.NewErrorWithCode(dsserr.PermissionDenied, "Specificed Subscription is owned by different client"),
"Subscription %s owned by %s, but %s attempted to use it for an OperationalIntent", subscriptionID, sub.Manager, manager)
}
updateSub := false
if sub.StartTime != nil && sub.StartTime.After(*uExtent.StartTime) {
if sub.ImplicitSubscription {
sub.StartTime = uExtent.StartTime
updateSub = true
} else {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Subscription does not begin until after the OperationalIntent starts")
}
}
if sub.EndTime != nil && sub.EndTime.Before(*uExtent.EndTime) {
if sub.ImplicitSubscription {
sub.EndTime = uExtent.EndTime
updateSub = true
} else {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Subscription ends before the OperationalIntent ends")
}
}
if !sub.Cells.Contains(cells) {
if sub.ImplicitSubscription {
sub.Cells = s2.CellUnionFromUnion(sub.Cells, cells)
updateSub = true
} else {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Subscription does not cover entire spatial area of the OperationalIntent")
}
}
if updateSub {
sub, err = r.UpsertSubscription(ctx, sub)
if err != nil {
return stacktrace.Propagate(err, "Failed to update existing Subscription")
}
}
}
if state.RequiresKey() {
// Construct a hash set of OVNs as the key
key := map[scdmodels.OVN]bool{}
if params.Key != nil {
for _, ovn := range *params.Key {
key[scdmodels.OVN(ovn)] = true
}
}
// Identify OperationalIntents missing from the key
var missingOps []*scdmodels.OperationalIntent
relevantOps, err := r.SearchOperationalIntents(ctx, uExtent)
if err != nil {
return stacktrace.Propagate(err, "Unable to SearchOperations")
}
for _, relevantOp := range relevantOps {
_, ok := key[relevantOp.OVN]
// Note: The OIR being mutated does not need to be specified in the key:
if !ok && relevantOp.RequiresKey() && relevantOp.ID != id {
if relevantOp.Manager != dssmodels.Manager(manager) {
relevantOp.OVN = scdmodels.NoOvnPhrase
}
missingOps = append(missingOps, relevantOp)
}
}
// Identify Constraints missing from the key
var missingConstraints []*scdmodels.Constraint
if sub != nil && sub.NotifyForConstraints {
constraints, err := r.SearchConstraints(ctx, uExtent)
if err != nil {
return stacktrace.Propagate(err, "Unable to SearchConstraints")
}
for _, relevantConstraint := range constraints {
if _, ok := key[relevantConstraint.OVN]; !ok {
if relevantConstraint.Manager != dssmodels.Manager(manager) {
relevantConstraint.OVN = scdmodels.NoOvnPhrase
}
missingConstraints = append(missingConstraints, relevantConstraint)
}
}
}
// If the client is missing some OVNs, provide the pointers to the
// information they need
if len(missingOps) > 0 || len(missingConstraints) > 0 {
msg := "Current OVNs not provided for one or more OperationalIntents or Constraints"
responseConflict = &restapi.AirspaceConflictResponse{Message: &msg}
if len(missingOps) > 0 {
responseConflict.MissingOperationalIntents = new([]restapi.OperationalIntentReference)
for _, missingOp := range missingOps {
p := missingOp.ToRest()
if missingOp.Manager != manager {
noOvnPhrase := restapi.EntityOVN(scdmodels.NoOvnPhrase)
p.Ovn = &noOvnPhrase
}
*responseConflict.MissingOperationalIntents = append(*responseConflict.MissingOperationalIntents, *p)
}
}
if len(missingConstraints) > 0 {
responseConflict.MissingConstraints = new([]restapi.ConstraintReference)
for _, missingConstraint := range missingConstraints {
c := missingConstraint.ToRest()
if missingConstraint.Manager != manager {
noOvnPhrase := restapi.EntityOVN(scdmodels.NoOvnPhrase)
c.Ovn = &noOvnPhrase
}
*responseConflict.MissingConstraints = append(*responseConflict.MissingConstraints, *c)
}
}
return stacktrace.NewErrorWithCode(dsserr.MissingOVNs, "Missing OVNs: %v", msg)
}
}
// 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: id,
Manager: dssmodels.Manager(manager),
Version: scdmodels.VersionNumber(version + 1),
StartTime: uExtent.StartTime,
EndTime: uExtent.EndTime,
AltitudeLower: uExtent.SpatialVolume.AltitudeLo,
AltitudeUpper: uExtent.SpatialVolume.AltitudeHi,
Cells: cells,
USSBaseURL: string(params.UssBaseUrl),
SubscriptionID: subID,
State: state,
}
err = op.ValidateTimeRange()
if err != nil {
return stacktrace.Propagate(err, "Error validating time range")
}
// Compute total affected Volume4D for notification purposes
var notifyVol4 *dssmodels.Volume4D
if old == nil {
notifyVol4 = uExtent
} else {
oldVol4 := &dssmodels.Volume4D{
StartTime: old.StartTime,
EndTime: old.EndTime,
SpatialVolume: &dssmodels.Volume3D{
AltitudeHi: old.AltitudeUpper,
AltitudeLo: old.AltitudeLower,
Footprint: dssmodels.GeometryFunc(func() (s2.CellUnion, error) {
return old.Cells, nil
}),
}}
notifyVol4, err = dssmodels.UnionVolumes4D(uExtent, oldVol4)
if err != nil {
return stacktrace.Propagate(err, "Error constructing 4D volumes union")
}
}
// Upsert the OperationalIntent
op, err = r.UpsertOperationalIntent(ctx, op)
if err != nil {
return stacktrace.Propagate(err, "Failed to upsert OperationalIntent in repo")
}
// Find Subscriptions that may need to be notified
allsubs, err := r.SearchSubscriptions(ctx, notifyVol4)
if err != nil {
return err
}
// Limit Subscription notifications to only those interested in OperationalIntents
subs := repos.Subscriptions{}
for _, sub := range allsubs {
if sub.NotifyForOperationalIntents {
subs = append(subs, sub)
}
}
// Increment notification indices for relevant Subscriptions
err = subs.IncrementNotificationIndices(ctx, r)
if err != nil {
return err
}
// Return response to client
responseOK = &restapi.ChangeOperationalIntentReferenceResponse{
OperationalIntentReference: *op.ToRest(),
Subscribers: makeSubscribersToNotify(subs),
}
return nil
}
err = a.Store.Transact(ctx, action)
if err != nil {
return nil, responseConflict, err // No need to Propagate this error as this is not a useful stacktrace line
}
return responseOK, responseConflict, nil
}