This commit is contained in:
2021-12-16 11:42:27 +01:00
parent 27ccc54458
commit 6c17b20c55
60 changed files with 947 additions and 216 deletions

View File

@@ -4,17 +4,19 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import ch.unisg.roster.roster.adapter.out.persistence.mongodb.RosterRepository;
import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort;
import ch.unisg.roster.roster.domain.Roster;
import ch.unisg.roster.roster.domain.RosterItem;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import ch.unisg.roster.roster.adapter.common.clients.TapasMqttClient;
import ch.unisg.roster.roster.adapter.in.messaging.mqtt.ExecutorEventMqttListener;
import ch.unisg.roster.roster.adapter.in.messaging.mqtt.ExecutorEventsMqttDispatcher;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@@ -26,14 +28,14 @@ public class RosterApplication {
private static ConfigurableEnvironment ENVIRONMENT;
private static LoadRosterItemPort loadRosterItemPort;
@Autowired
private LoadRosterItemPort loadRosterItemPort;
public static void main(String[] args) {
SpringApplication rosterApp = new SpringApplication(RosterApplication.class);
ENVIRONMENT = rosterApp.run(args).getEnvironment();
bootstrapMarketplaceWithMqtt();
// initialiseRoster();
}
/**
@@ -52,7 +54,8 @@ public class RosterApplication {
}
}
private static void initialiseRoster(){
@PostConstruct
private void initialiseRoster(){
List<RosterItem> rosterItemList = loadRosterItemPort.loadAllRosterItems();
Roster.getInstance().initialiseRoster(rosterItemList);
}

View File

@@ -18,8 +18,6 @@ public class ExecutorAddedEventListenerMqttAdapter extends ExecutorEventMqttList
@Override
public boolean handleEvent(MqttMessage message) {
System.out.println("New Executor added!");
String payload = new String(message.getPayload());
try {
@@ -30,6 +28,8 @@ public class ExecutorAddedEventListenerMqttAdapter extends ExecutorEventMqttList
String taskType = data.get("executorTaskType").asText();
String executorId = data.get("executorUri").asText();
LOGGER.info("Roster | New executor with type " + taskType + " added.");
ExecutorAddedEvent executorAddedEvent = new ExecutorAddedEvent(
new ExecutorURI(executorId),
new ExecutorType(taskType)

View File

@@ -1,11 +1,8 @@
package ch.unisg.roster.roster.adapter.in.web;
import ch.unisg.roster.roster.domain.Roster;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.json.JSONObject;
import org.springframework.http.HttpHeaders;
import java.util.logging.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@@ -16,8 +13,6 @@ import ch.unisg.roster.roster.domain.ExecutorInfo;
import ch.unisg.roster.roster.domain.Task;
import org.springframework.web.server.ResponseStatusException;
import javax.validation.ConstraintViolationException;
@RestController
public class ApplyForTaskWebController {
private final ApplyForTaskUseCase applyForTaskUseCase;
@@ -26,14 +21,15 @@ public class ApplyForTaskWebController {
this.applyForTaskUseCase = applyForTaskUseCase;
}
// TODO fix return type
Logger logger = Logger.getLogger(ApplyForTaskWebController.class.getName());
/**
* Checks if task is available for the requesting executor.
* @return a task or null if no task found
* @return a task or 404 if no task available
**/
@PostMapping(path = "/task/apply", consumes = {"application/json"})
public ResponseEntity<String> applyForTask (@RequestBody ExecutorInfo executorInfo) {
public Task applyForTask (@RequestBody ExecutorInfo executorInfo) {
logger.info("Roster | Execuor applying for task");
ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(),
executorInfo.getExecutorURI());
@@ -43,23 +39,6 @@ public class ApplyForTaskWebController {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
try {
String executorType = command.getTaskType().getValue().toString();
String executorURI = command.getExecutorURI().getValue().toString();
String jsonPayLoad = new JSONObject()
.put("executorType", executorType)
.put("executorURI", executorURI)
.toString();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "application/json");
return new ResponseEntity<>(jsonPayLoad, responseHeaders, HttpStatus.CREATED);
} catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
return task;
}
}

View File

@@ -1,15 +1,15 @@
package ch.unisg.roster.roster.adapter.in.web;
import java.util.logging.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import ch.unisg.roster.roster.application.port.in.DeleteTaskCommand;
import ch.unisg.roster.roster.application.port.in.DeleteTaskUseCase;
import ch.unisg.roster.roster.domain.Task;
@RestController
public class DeleteTaskController {
@@ -19,13 +19,15 @@ public class DeleteTaskController {
this.deleteTaskUseCase = deleteTaskUseCase;
}
Logger logger = Logger.getLogger(DeleteTaskController.class.getName());
/**
* Controller to delete a task
* @return 200 OK, 409 Conflict
**/
@DeleteMapping(path = "/task/{taskId}")
public ResponseEntity<Void> deleteTask(@PathVariable("taskId") String taskId) {
logger.info("Roster | Delete task request.");
DeleteTaskCommand command = new DeleteTaskCommand(taskId);
if (deleteTaskUseCase.deleteTask(command)) {

View File

@@ -2,11 +2,14 @@ package ch.unisg.roster.roster.adapter.in.web;
import java.util.logging.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.function.ServerRequest.Headers;
import ch.unisg.roster.roster.application.port.in.NewTaskCommand;
import ch.unisg.roster.roster.application.port.in.NewTaskUseCase;
@@ -27,16 +30,33 @@ public class NewTaskController {
* @return 201 Create or 409 Conflict
**/
@PostMapping(path = "/task", consumes = {"application/task+json"})
public ResponseEntity<Void> newTaskController(@RequestBody Task task) {
public ResponseEntity<Void> newTaskController(@RequestBody Task task, @RequestHeader HttpHeaders header) {
logger.info("New task with id:" + task.getTaskID());
logger.info("Roster | New task with id:" + task.getTaskID());
NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskUri(), task.getTaskType(),
// Check if task URI is passed in body. (This happends when we handeling an external task) Else get task URI from header.
String taskUri = task.getTaskUri();
if (taskUri == null) {
if (header.containsKey("Link")) {
for (String location : header.get("Link")) {
if (location.contains("rel=\"task\"")) {
taskUri = location.split(">")[0].substring(1);
}
}
}
}
if (taskUri == null) {
logger.warning("Roster | TaskUri still null, something went wrong!");
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
NewTaskCommand command = new NewTaskCommand(task.getTaskID(), taskUri, task.getTaskType(),
task.getInputData());
boolean success = newTaskUseCase.addNewTaskToQueue(command);
logger.info("Could create task: " + success);
logger.info("Roster | Could create task: " + success);
if (success) {
return new ResponseEntity<>(HttpStatus.CREATED);

View File

@@ -6,6 +6,8 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -24,15 +26,15 @@ public class LaunchAuctionEventAdapter implements LaunchAuctionEventPort {
@Value("${auction.house.uri}")
String server;
Logger logger = Logger.getLogger(LaunchAuctionEventAdapter.class.getName());
@Override
public void launchAuctionEvent(LaunchAuctionEvent launchAuctionEvent) {
var values = new HashMap<String, String>() {{
var values = new HashMap<String, String>();
put("taskUri", launchAuctionEvent.taskUri);
put("taskType", launchAuctionEvent.taskType.getValue());
}};
values.put("taskUri", launchAuctionEvent.taskUri);
values.put("taskType", launchAuctionEvent.taskType.getValue());
var objectMapper = new ObjectMapper();
String requestBody = null;
@@ -44,17 +46,18 @@ public class LaunchAuctionEventAdapter implements LaunchAuctionEventPort {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server+"/auctions/"))
.header("Content-Type", "application/task+json")
.uri(URI.create(server + "/auctions/"))
.header("Content-Type", "application/auction+json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException e) {
e.printStackTrace();
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
} catch (InterruptedException e) {
e.printStackTrace();
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
Thread.currentThread().interrupt();
}
}

View File

@@ -1,7 +1,5 @@
package ch.unisg.roster.roster.application.port.in;
import ch.unisg.roster.roster.domain.RosterItem;
public interface DeleteRosterItem {
void deleteRosterItem(String taskId);

View File

@@ -2,7 +2,6 @@ package ch.unisg.roster.roster.application.port.in;
import javax.validation.constraints.NotNull;
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
import ch.unisg.common.validation.SelfValidating;
import lombok.EqualsAndHashCode;
import lombok.Value;

View File

@@ -19,10 +19,8 @@ public class NewTaskCommand extends SelfValidating<NewTaskCommand> {
@NotNull
private final ExecutorType taskType;
@NotNull
private final String inputData;
@NotNull
private final String taskUri;
public NewTaskCommand(String taskID, String taskUri, ExecutorType taskType, String inputData) {

View File

@@ -1,5 +1,7 @@
package ch.unisg.roster.roster.application.service;
import java.util.logging.Logger;
import javax.transaction.Transactional;
import org.springframework.stereotype.Component;
@@ -24,6 +26,8 @@ public class NewTaskService implements NewTaskUseCase {
private final LaunchAuctionEventPort launchAuctionEventPort;
Logger logger = Logger.getLogger(NewTaskService.class.getName());
/**
* Checks if we can execute the give task, if yes the task gets added to the task queue and return true.
* If the task can not be executed by an internal or auction house executor, the method return false.
@@ -35,12 +39,15 @@ public class NewTaskService implements NewTaskUseCase {
ExecutorRegistry executorRegistry = ExecutorRegistry.getInstance();
if (!executorRegistry.containsTaskType(command.getTaskType())) {
logger.info("Roster | Task with type " + command.getTaskType() + "can not be handled internaly. Send to auction house!");
LaunchAuctionEvent launchAuctionEvent = new LaunchAuctionEvent( command.getTaskUri(),
command.getTaskType());
launchAuctionEventPort.launchAuctionEvent(launchAuctionEvent);
return true;
}
logger.info("Roster | Task with type " + command.getTaskType() + " can be handled internaly.");
Task task = new Task(command.getTaskID(), command.getTaskType(), command.getInputData());
Roster.getInstance().addTaskToQueue(task);

View File

@@ -1,6 +1,7 @@
server.port=8082
executor.robot.uri=http://127.0.0.1:8084
executor.computation.uri=http://127.0.0.1:8085
executor.humidity.uri=http://127.0.0.1:8087
auction.house.uri=http://127.0.0.1:8086
task.list.uri=http://127.0.0.1:8081
# mqtt.broker.uri=tcp://localhost:1883