19
19
import java .time .format .DateTimeParseException ;
20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
+ import java .util .Optional ;
22
23
23
24
import org .apache .http .HttpStatus ;
24
25
import org .apache .logging .log4j .LogManager ;
49
50
import eu .arrowhead .common .Defaults ;
50
51
import eu .arrowhead .common .Utilities ;
51
52
import eu .arrowhead .common .core .CoreSystem ;
53
+ import eu .arrowhead .common .database .entity .ChoreographerPlan ;
52
54
import eu .arrowhead .common .database .entity .ChoreographerSession ;
53
55
import eu .arrowhead .common .database .entity .Logs ;
54
56
import eu .arrowhead .common .database .service .CommonDBService ;
58
60
import eu .arrowhead .common .dto .shared .ChoreographerPlanListResponseDTO ;
59
61
import eu .arrowhead .common .dto .shared .ChoreographerPlanRequestDTO ;
60
62
import eu .arrowhead .common .dto .shared .ChoreographerPlanResponseDTO ;
63
+ import eu .arrowhead .common .dto .shared .ChoreographerRunPlanRequestByClientDTO ;
61
64
import eu .arrowhead .common .dto .shared .ChoreographerRunPlanRequestDTO ;
62
65
import eu .arrowhead .common .dto .shared .ChoreographerRunPlanResponseDTO ;
63
66
import eu .arrowhead .common .dto .shared .ChoreographerSessionStatus ;
@@ -111,7 +114,8 @@ public class ChoreographerPlanController {
111
114
private static final String DELETE_PLAN_HTTP_400_MESSAGE = "Could not remove Plan." ;
112
115
113
116
private static final String START_SESSION_HTTP_200_MESSAGE = "Initiated plan execution with given id(s)." ;
114
- private static final String START_PLAN_HTTP_400_MESSAGE = "Could not start plan with given id(s)." ;
117
+ private static final String START_SESSION_BY_CLIENT_HTTP_200_MESSAGE = "Initiated plan execution with given id(s) or name(s)." ;
118
+ private static final String START_PLAN_HTTP_400_MESSAGE = "Could not start plan." ;
115
119
116
120
private static final String ABORT_SESSION_HTTP_200_MESSAGE = "Initiated session abortion with given id." ;
117
121
private static final String ABORT_SESSION_HTTP_400_MESSAGE = "Could not abort session with given id." ;
@@ -367,6 +371,69 @@ public void abortSession(@PathVariable final Long id) {
367
371
368
372
return new ChoreographerCheckPlanResponseDTO (id , result .getErrorMessages (), result .getNeedInterCloud ());
369
373
}
374
+
375
+ //-------------------------------------------------------------------------------------------------
376
+ @ ApiOperation (value = "Initiate the start of one or more plans." , tags = { CoreCommonConstants .SWAGGER_TAG_CLIENT })
377
+ @ ApiResponses (value = {
378
+ @ ApiResponse (code = HttpStatus .SC_OK , message = START_SESSION_BY_CLIENT_HTTP_200_MESSAGE , responseContainer = "List" , response = ChoreographerRunPlanResponseDTO .class ),
379
+ @ ApiResponse (code = HttpStatus .SC_BAD_REQUEST , message = START_PLAN_HTTP_400_MESSAGE , response = ErrorMessageDTO .class ),
380
+ @ ApiResponse (code = HttpStatus .SC_UNAUTHORIZED , message = CoreCommonConstants .SWAGGER_HTTP_401_MESSAGE , response = ErrorMessageDTO .class ),
381
+ @ ApiResponse (code = HttpStatus .SC_INTERNAL_SERVER_ERROR , message = CoreCommonConstants .SWAGGER_HTTP_500_MESSAGE , response = ErrorMessageDTO .class )
382
+ })
383
+ @ PostMapping (path = CommonConstants .OP_CHOREOGRAPHER_CLIENT_SERVICE_SESSION_START_URI , consumes = MediaType .APPLICATION_JSON_VALUE , produces = MediaType .APPLICATION_JSON_VALUE )
384
+ @ ResponseBody public List <ChoreographerRunPlanResponseDTO > startPlansByClient (@ RequestBody final List <ChoreographerRunPlanRequestByClientDTO > requests ) {
385
+ logger .debug ("startPlans started..." );
386
+
387
+ if (requests == null || requests .isEmpty ()) {
388
+ throw new BadPayloadException ("No plan specified to start." , HttpStatus .SC_BAD_REQUEST , CommonConstants .CHOREOGRAPHER_URI + CommonConstants .OP_CHOREOGRAPHER_CLIENT_SERVICE_SESSION_START_URI );
389
+ }
390
+
391
+ final List <ChoreographerRunPlanResponseDTO > results = new ArrayList <>(requests .size ());
392
+ for (final ChoreographerRunPlanRequestByClientDTO request : requests ) {
393
+ request .setAllowInterCloud (gatekeeperIsPresent && request .isAllowInterCloud ()); // change inter-cloud flag based on gatekeeper presence in the cloud
394
+ if (request .getPlanId () == null ) {
395
+ request .setPlanId (findPlan (request .getName ())); // try to find plan id by name
396
+ }
397
+
398
+ final ChoreographerRunPlanResponseDTO response = planChecker .checkPlanForExecution (request );
399
+
400
+ if (!Utilities .isEmpty (response .getErrorMessages ())) {
401
+ results .add (response );
402
+ } else {
403
+ final ChoreographerSession session = sessionDBService .initiateSession (request .getPlanId (), request .getQuantity (), createNotifyUri (request ));
404
+ results .add (new ChoreographerRunPlanResponseDTO (request .getPlanId (), session .getId (), session .getQuantityGoal (), response .getNeedInterCloud ()));
405
+
406
+ logger .debug ("Sending a message to {}." , ChoreographerService .START_SESSION_DESTINATION );
407
+ jms .convertAndSend (ChoreographerService .START_SESSION_DESTINATION , new ChoreographerStartSessionDTO (session .getId (), request .getPlanId (), request .isAllowInterCloud (), request .getChooseOptimalExecutor ()));
408
+ }
409
+ }
410
+
411
+ return results ;
412
+ }
413
+
414
+ //-------------------------------------------------------------------------------------------------
415
+ @ ApiOperation (value = "Initiate the abortion of the specified session." , response = Void .class , tags = { CoreCommonConstants .SWAGGER_TAG_CLIENT })
416
+ @ ApiResponses (value = {
417
+ @ ApiResponse (code = HttpStatus .SC_OK , message = ABORT_SESSION_HTTP_200_MESSAGE ),
418
+ @ ApiResponse (code = HttpStatus .SC_BAD_REQUEST , message = ABORT_SESSION_HTTP_400_MESSAGE ),
419
+ @ ApiResponse (code = HttpStatus .SC_UNAUTHORIZED , message = CoreCommonConstants .SWAGGER_HTTP_401_MESSAGE ),
420
+ @ ApiResponse (code = HttpStatus .SC_INTERNAL_SERVER_ERROR , message = CoreCommonConstants .SWAGGER_HTTP_500_MESSAGE )
421
+ })
422
+ @ DeleteMapping (path = CommonConstants .OP_CHOREOGRAPHER_CLIENT_SERVICE_SESSION_ABORT_URI )
423
+ public void abortSessionByClient (@ PathVariable final Long id ) {
424
+ logger .debug ("New abort session request received with id: {}." , id );
425
+
426
+ if (id < 1 ) {
427
+ throw new BadPayloadException (ID_NOT_VALID_ERROR_MESSAGE , HttpStatus .SC_BAD_REQUEST , CommonConstants .CHOREOGRAPHER_URI + CommonConstants .OP_CHOREOGRAPHER_CLIENT_SERVICE_SESSION_ABORT_URI );
428
+ }
429
+
430
+ final ChoreographerSession session = sessionDBService .getSessionById (id );
431
+ if (session .getStatus () == ChoreographerSessionStatus .DONE ) {
432
+ throw new BadPayloadException ("Session with id " + id + " couldn't be aborted due to its DONE status" );
433
+ }
434
+
435
+ choreographerService .abortSession (id , null , MANUAL_ABORT_MESSAGE );
436
+ }
370
437
371
438
//=================================================================================================
372
439
// assistant methods
@@ -376,4 +443,15 @@ private String createNotifyUri(final ChoreographerRunPlanRequestDTO request) {
376
443
return Utilities .isEmpty (request .getNotifyAddress ()) ? null
377
444
: request .getNotifyProtocol () + "://" + request .getNotifyAddress () + ":" + request .getNotifyPort () + "/" + request .getNotifyPath ();
378
445
}
446
+
447
+ //-------------------------------------------------------------------------------------------------
448
+ private Long findPlan (final String name ) {
449
+ if (Utilities .isEmpty (name )) {
450
+ return null ;
451
+ }
452
+
453
+ final Optional <ChoreographerPlan > plan = planDBService .getPlanByName (name );
454
+
455
+ return plan .isPresent () ? plan .get ().getId () : null ;
456
+ }
379
457
}
0 commit comments