-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mock stage requests for monitoring tests
- Loading branch information
Showing
13 changed files
with
347 additions
and
9,154 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/org/italiangrid/storm/webdav/tape/WlcgMockTapeRestApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.italiangrid.storm.webdav.tape; | ||
|
||
import java.util.Map; | ||
|
||
import javax.validation.Valid; | ||
|
||
import org.italiangrid.storm.webdav.tape.model.StageRequest; | ||
import org.italiangrid.storm.webdav.tape.model.StageRequestResponse; | ||
import org.italiangrid.storm.webdav.tape.model.StageStatusRequest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.google.common.collect.Maps; | ||
|
||
import io.milton.http.exceptions.NotFoundException; | ||
|
||
|
||
@RestController | ||
public class WlcgMockTapeRestApiController { | ||
|
||
private static Map<String, StageStatusRequest> cache = Maps.newLinkedHashMap(); | ||
|
||
|
||
@PostMapping({"mock/stage"}) | ||
public StageRequestResponse createRequest(@RequestBody @Valid StageRequest request) { | ||
|
||
StageStatusRequest req = new StageStatusRequest(request.getFiles()); | ||
cache.put(req.getId(), req); | ||
return new StageRequestResponse(req.getId()); | ||
} | ||
|
||
@GetMapping({"mock/stage/{requestId}"}) | ||
public StageStatusRequest getRequestStatus( | ||
@PathVariable String requestId) throws NotFoundException { | ||
if (cache.keySet().contains(requestId)) { | ||
StageStatusRequest r = cache.get(requestId); | ||
r.evolve(); | ||
cache.put(r.getId(), r); | ||
return r; | ||
} else { | ||
throw new NotFoundException(requestId + " not found!"); | ||
} | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ExceptionHandler(NotFoundException.class) | ||
public String handleNotFoundException(NotFoundException e) { | ||
return e.getMessage(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/org/italiangrid/storm/webdav/tape/model/FileStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.italiangrid.storm.webdav.tape.model; | ||
|
||
public enum FileStatus { | ||
|
||
SUBMITTED, STARTED, CANCELLED, FAILED, COMPLETED; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/italiangrid/storm/webdav/tape/model/StageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.italiangrid.storm.webdav.tape.model; | ||
|
||
import java.util.List; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
|
||
public class StageRequest { | ||
|
||
@NotEmpty(message = "Input file list cannot be empty.") | ||
List<StageRequestFile> files; | ||
|
||
public List<StageRequestFile> getFiles() { | ||
return files; | ||
} | ||
|
||
public void setFiles(List<StageRequestFile> files) { | ||
this.files = files; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/italiangrid/storm/webdav/tape/model/StageRequestFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.italiangrid.storm.webdav.tape.model; | ||
|
||
public class StageRequestFile { | ||
|
||
String path; | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
src/main/java/org/italiangrid/storm/webdav/tape/model/StageRequestFileStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package org.italiangrid.storm.webdav.tape.model; | ||
|
||
import java.sql.Timestamp; | ||
import java.util.Random; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
|
||
@JsonInclude(Include.NON_NULL) | ||
public class StageRequestFileStatus { | ||
|
||
private static Random rd = new Random(); | ||
|
||
String path; | ||
boolean onDisk; | ||
FileStatus status; | ||
Timestamp startedAt; | ||
Timestamp finishedAt; | ||
String error; | ||
|
||
public StageRequestFileStatus(String path) { | ||
this.path = path; | ||
this.onDisk = false; | ||
this.status = FileStatus.SUBMITTED; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
public boolean isOnDisk() { | ||
return onDisk; | ||
} | ||
|
||
public void setOnDisk(boolean onDisk) { | ||
this.onDisk = onDisk; | ||
} | ||
|
||
public FileStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(FileStatus status) { | ||
this.status = status; | ||
} | ||
|
||
public Timestamp getStartedAt() { | ||
return startedAt; | ||
} | ||
|
||
public void setStartedAt(Timestamp startedAt) { | ||
this.startedAt = startedAt; | ||
} | ||
|
||
public Timestamp getFinishedAt() { | ||
return finishedAt; | ||
} | ||
|
||
public void setFinishedAt(Timestamp finishedAt) { | ||
this.finishedAt = finishedAt; | ||
} | ||
|
||
public String getError() { | ||
return error; | ||
} | ||
|
||
public void setError(String error) { | ||
this.error = error; | ||
} | ||
|
||
public boolean getRandomBoolean(double p) { | ||
return rd.nextFloat() < p; | ||
} | ||
|
||
public void evolve() { | ||
|
||
if (FileStatus.COMPLETED.equals(status) | ||
|| FileStatus.CANCELLED.equals(status) | ||
|| FileStatus.FAILED.equals(status)) { | ||
return; | ||
} | ||
if (FileStatus.SUBMITTED.equals(status)) { | ||
status = FileStatus.STARTED; | ||
startedAt = new Timestamp(System.currentTimeMillis()); | ||
return; | ||
} | ||
if (FileStatus.STARTED.equals(status)) { | ||
boolean isRunning = getRandomBoolean(0.3); | ||
if (isRunning) { | ||
return; | ||
} | ||
boolean isCompleted = getRandomBoolean(0.5); | ||
if (isCompleted) { | ||
status = FileStatus.COMPLETED; | ||
finishedAt = new Timestamp(System.currentTimeMillis()); | ||
onDisk = true; | ||
return; | ||
} | ||
boolean isCanceled = getRandomBoolean(0.1); | ||
if (isCanceled) { | ||
status = FileStatus.CANCELLED; | ||
finishedAt = new Timestamp(System.currentTimeMillis()); | ||
return; | ||
} | ||
boolean isFailed = getRandomBoolean(0.2); | ||
if (isFailed) { | ||
status = FileStatus.FAILED; | ||
finishedAt = new Timestamp(System.currentTimeMillis()); | ||
error = "Recall failed!"; | ||
return; | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/italiangrid/storm/webdav/tape/model/StageRequestResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.italiangrid.storm.webdav.tape.model; | ||
|
||
public class StageRequestResponse { | ||
|
||
private String requestId; | ||
|
||
public StageRequestResponse(String id) { | ||
requestId = id; | ||
} | ||
|
||
public String getRequestId() { | ||
return requestId; | ||
} | ||
|
||
public void setRequestId(String requestId) { | ||
this.requestId = requestId; | ||
} | ||
|
||
} |
Oops, something went wrong.