1919import java .time .format .DateTimeParseException ;
2020import java .util .ArrayList ;
2121import java .util .List ;
22+ import java .util .Optional ;
2223
2324import org .apache .http .HttpStatus ;
2425import org .apache .logging .log4j .LogManager ;
4950import eu .arrowhead .common .Defaults ;
5051import eu .arrowhead .common .Utilities ;
5152import eu .arrowhead .common .core .CoreSystem ;
53+ import eu .arrowhead .common .database .entity .ChoreographerPlan ;
5254import eu .arrowhead .common .database .entity .ChoreographerSession ;
5355import eu .arrowhead .common .database .entity .Logs ;
5456import eu .arrowhead .common .database .service .CommonDBService ;
5860import eu .arrowhead .common .dto .shared .ChoreographerPlanListResponseDTO ;
5961import eu .arrowhead .common .dto .shared .ChoreographerPlanRequestDTO ;
6062import eu .arrowhead .common .dto .shared .ChoreographerPlanResponseDTO ;
63+ import eu .arrowhead .common .dto .shared .ChoreographerRunPlanRequestByClientDTO ;
6164import eu .arrowhead .common .dto .shared .ChoreographerRunPlanRequestDTO ;
6265import eu .arrowhead .common .dto .shared .ChoreographerRunPlanResponseDTO ;
6366import eu .arrowhead .common .dto .shared .ChoreographerSessionStatus ;
@@ -111,7 +114,8 @@ public class ChoreographerPlanController {
111114 private static final String DELETE_PLAN_HTTP_400_MESSAGE = "Could not remove Plan." ;
112115
113116 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." ;
115119
116120 private static final String ABORT_SESSION_HTTP_200_MESSAGE = "Initiated session abortion with given id." ;
117121 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) {
367371
368372 return new ChoreographerCheckPlanResponseDTO (id , result .getErrorMessages (), result .getNeedInterCloud ());
369373 }
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+ }
370437
371438 //=================================================================================================
372439 // assistant methods
@@ -376,4 +443,15 @@ private String createNotifyUri(final ChoreographerRunPlanRequestDTO request) {
376443 return Utilities .isEmpty (request .getNotifyAddress ()) ? null
377444 : request .getNotifyProtocol () + "://" + request .getNotifyAddress () + ":" + request .getNotifyPort () + "/" + request .getNotifyPath ();
378445 }
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+ }
379457}
0 commit comments