Implemented the following points:

- Roster/Auctionhouse send initial getExecutors request
- Remove executor (made DELETE endpoint in executor pool)
This commit is contained in:
reynisson 2021-12-16 20:39:14 +01:00
parent 6c17b20c55
commit cab63d6b76
19 changed files with 239 additions and 31 deletions

View File

@ -40,7 +40,7 @@ public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server+"/executor-pool/AddExecutor"))
.uri(URI.create(server+"/executor-pool/executors"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();

View File

@ -27,7 +27,7 @@ public class AddNewExecutorToExecutorPoolWebController {
this.addNewExecutorToExecutorPoolUseCase = addNewExecutorToExecutorPoolUseCase;
}
@PostMapping(path = "/executor-pool/AddExecutor", consumes = {ExecutorJsonRepresentation.EXECUTOR_MEDIA_TYPE})
@PostMapping(path = "/executor-pool/executors", consumes = {ExecutorJsonRepresentation.EXECUTOR_MEDIA_TYPE})
public ResponseEntity<String> addNewExecutorToExecutorPool(@RequestBody ExecutorJsonRepresentation payload){
try {
AddNewExecutorToExecutorPoolCommand command = new AddNewExecutorToExecutorPoolCommand(

View File

@ -19,7 +19,7 @@ public class GetAllExecutorsInExecutorPoolWebController {
this.getAllExecutorsInExecutorPoolUseCase = getAllExecutorsInExecutorPoolUseCase;
}
@GetMapping(path = "executor-pool/GetAllExecutorsinExecutorPool")
@GetMapping(path = "executor-pool/GetAllExecutorsInExecutorPool")
public ResponseEntity<String> getAllExecutorsInExecutorPool(){
List<ExecutorClass> executorClassList = getAllExecutorsInExecutorPoolUseCase.getAllExecutorsInExecutorPool();

View File

@ -7,9 +7,7 @@ import ch.unisg.executorpool.domain.ExecutorClass;
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.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.net.URI;
@ -23,10 +21,10 @@ public class RemoveExecutorFromExecutorPoolWebController {
this.removeExecutorFromExecutorPoolUseCase = removeExecutorFromExecutorPoolUseCase;
}
@PostMapping(path = "/executor-pool/RemoveExecutor", consumes = {ExecutorJsonRepresentation.EXECUTOR_MEDIA_TYPE})
public ResponseEntity<String> removeExecutorFromExecutorPool(@RequestBody ExecutorJsonRepresentation executorJsonRepresentation){
@DeleteMapping(path = "/executor-pool/executors/{executorUri}")
public ResponseEntity<String> removeExecutorFromExecutorPool(@PathVariable("executorUri") String executorUri){
RemoveExecutorFromExecutorPoolCommand command = new RemoveExecutorFromExecutorPoolCommand(
new ExecutorClass.ExecutorUri(URI.create(executorJsonRepresentation.getExecutorUri()))
new ExecutorClass.ExecutorUri(URI.create(executorUri))
);
Optional<ExecutorClass> removedExecutor = removeExecutorFromExecutorPoolUseCase.removeExecutorFromExecutorPool(command);

View File

@ -8,6 +8,7 @@ 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.application.service.GetExecutorsService;
import ch.unisg.roster.roster.domain.Roster;
import ch.unisg.roster.roster.domain.RosterItem;
import org.eclipse.paho.client.mqttv3.MqttException;
@ -36,6 +37,8 @@ public class RosterApplication {
SpringApplication rosterApp = new SpringApplication(RosterApplication.class);
ENVIRONMENT = rosterApp.run(args).getEnvironment();
bootstrapMarketplaceWithMqtt();
var getExecutorsService = new GetExecutorsService();
getExecutorsService.getExecutorsFromExecutorPool();
}
/**

View File

@ -0,0 +1,61 @@
package ch.unisg.roster.roster.adapter.out.web;
import ch.unisg.roster.roster.domain.ExecutorInfo;
import org.apache.logging.log4j.Level;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.LinkedList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class GetExecutorsInExecutorPoolWebAdapter {
private static final Logger LOGGER = LogManager.getLogger(GetExecutorsInExecutorPoolWebAdapter.class);
// TODO get from config
String server = "http://localhost:8083";
public List<ExecutorInfo> getExecutorsInExecutorPool(){
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create(server + "/executor-pool/GetAllExecutorsInExecutorPool"))
.GET()
.build();
try {
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != HttpStatus.OK.value()){
LOGGER.log(Level.INFO, "Could not get executors from Executor Pool");
return null;
}
LOGGER.log(Level.INFO, "Executors received from ExecutorPool: " + response.body());
var jsonExecutorArray = new JSONArray(response.body());
var executorList = new LinkedList<ExecutorInfo>();
for(int i = 0; i < jsonExecutorArray.length(); i++){
var jsonExecutorObject = jsonExecutorArray.getJSONObject(i);
var executorURI = jsonExecutorObject.getString("executorUri");
var executorType = jsonExecutorObject.getString("executorTaskType");
executorList.add(new ExecutorInfo(executorURI, executorType));
}
return executorList;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,18 @@
package ch.unisg.roster.roster.application.service;
import ch.unisg.roster.roster.adapter.out.web.GetExecutorsInExecutorPoolWebAdapter;
import ch.unisg.roster.roster.domain.ExecutorRegistry;
// TODO should this implement a port in the Hexagonal architecture?
public class GetExecutorsService {
public boolean getExecutorsFromExecutorPool(){
var getExecutorsAdapter = new GetExecutorsInExecutorPoolWebAdapter();
var executors = getExecutorsAdapter.getExecutorsInExecutorPool();
if(executors == null){
return false;
}
var executorRegistry = ExecutorRegistry.getInstance();
return executorRegistry.addExecutors(executors);
}
}

View File

@ -5,6 +5,8 @@ import ch.unisg.common.valueobject.ExecutorURI;
import lombok.Getter;
import lombok.Setter;
import java.net.URI;
public class ExecutorInfo {
@Getter
@Setter
@ -13,4 +15,9 @@ public class ExecutorInfo {
@Getter
@Setter
private ExecutorType executorType;
public ExecutorInfo(String executorURI, String executorType){
this.executorURI = new ExecutorURI(executorURI);
this.executorType = new ExecutorType(executorType);
}
}

View File

@ -30,8 +30,8 @@ public class ExecutorRegistry {
/**
* Adds an executor to the registry for a given task type.
*
* @param taskType the type of the task
* @param executorIdentifier the identifier of the executor (can be any string)
* @param executorType the type of the task
* @param executorURI the identifier of the executor (can be any string)
* @return true unless a runtime exception occurs
*/
public boolean addExecutor(ExecutorType executorType, ExecutorURI executorURI) {
@ -44,6 +44,15 @@ public class ExecutorRegistry {
return true;
}
public boolean addExecutors(List<ExecutorInfo> executors){
for (var executor : executors) {
if(!addExecutor(executor.getExecutorType(), executor.getExecutorURI())){
return false;
}
}
return true;
}
/**
* Removes an executor from the registry. The executor is disassociated from all known task types.
*

View File

@ -69,6 +69,12 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>ch.unisg</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -3,6 +3,7 @@ package ch.unisg.tapas;
import ch.unisg.tapas.auctionhouse.adapter.common.clients.TapasMqttClient;
import ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt.AuctionEventsMqttDispatcher;
import ch.unisg.tapas.auctionhouse.adapter.common.clients.WebSubSubscriber;
import ch.unisg.tapas.auctionhouse.application.service.GetExecutorsService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.paho.client.mqttv3.MqttException;
@ -31,6 +32,8 @@ public class TapasAuctionHouseApplication {
// We will use these bootstrap methods in Week 6:
// bootstrapMarketplaceWithWebSub();
bootstrapMarketplaceWithMqtt();
var getExecutorsService = new GetExecutorsService();
getExecutorsService.getExecutorsFromExecutorPool();
}
/**
* Discovers auction houses and subscribes to WebSub notifications

View File

@ -4,6 +4,7 @@ import ch.unisg.tapas.auctionhouse.application.handler.ExecutorAddedHandler;
import ch.unisg.tapas.auctionhouse.application.port.in.ExecutorAddedEvent;
import ch.unisg.tapas.auctionhouse.domain.Auction;
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry;
import ch.unisg.common.valueobject.ExecutorURI;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -34,7 +35,7 @@ public class ExecutorAddedEventListenerMqttAdapter extends AuctionEventMqttListe
String executorTaskType = data.get("executorTaskType").asText();
ExecutorAddedEvent executorAddedEvent = new ExecutorAddedEvent(
new ExecutorRegistry.ExecutorUri(URI.create(executorUri)),
new ExecutorURI(executorUri),
new Auction.AuctionedTaskType(executorTaskType)
);

View File

@ -2,6 +2,7 @@ package ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt;
import ch.unisg.tapas.auctionhouse.application.handler.ExecutorRemovedHandler;
import ch.unisg.tapas.auctionhouse.application.port.in.ExecutorRemovedEvent;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.tapas.auctionhouse.domain.Auction;
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -33,7 +34,7 @@ public class ExecutorRemovedEventListenerMqttAdapter extends AuctionEventMqttLis
String executorUri = data.get("executorUri").asText();
ExecutorRemovedEvent executorRemovedEvent = new ExecutorRemovedEvent(
new ExecutorRegistry.ExecutorUri(URI.create(executorUri))
new ExecutorURI(executorUri)
);
ExecutorRemovedHandler newExecutorHandler = new ExecutorRemovedHandler();

View File

@ -0,0 +1,60 @@
package ch.unisg.tapas.auctionhouse.adapter.out.web;
import ch.unisg.tapas.auctionhouse.domain.ExecutorInfo;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.springframework.http.HttpStatus;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.LinkedList;
import java.util.List;
public class GetExecutorsFromExecutorPoolWebAdapter {
private static final Logger LOGGER = LogManager.getLogger(GetExecutorsFromExecutorPoolWebAdapter.class);
// TODO get from config
String server = "http://localhost:8083";
public List<ExecutorInfo> getExecutorsInExecutorPool(){
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create(server + "/executor-pool/GetAllExecutorsInExecutorPool"))
.GET()
.build();
try {
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != HttpStatus.OK.value()){
LOGGER.log(Level.INFO, "Could not get executors from Executor Pool");
return null;
}
LOGGER.log(Level.INFO, "Executors received from ExecutorPool: " + response.body());
var jsonExecutorArray = new JSONArray(response.body());
var executorList = new LinkedList<ExecutorInfo>();
for(int i = 0; i < jsonExecutorArray.length(); i++){
var jsonExecutorObject = jsonExecutorArray.getJSONObject(i);
var executorURI = jsonExecutorObject.getString("executorUri");
var executorType = jsonExecutorObject.getString("executorTaskType");
executorList.add(new ExecutorInfo(executorURI, executorType));
}
return executorList;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -2,7 +2,7 @@ package ch.unisg.tapas.auctionhouse.application.port.in;
import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionedTaskType;
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry;
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry.ExecutorUri;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.tapas.common.SelfValidating;
import lombok.Value;
@ -14,7 +14,7 @@ import javax.validation.constraints.NotNull;
@Value
public class ExecutorAddedEvent extends SelfValidating<ExecutorAddedEvent> {
@NotNull
private final ExecutorRegistry.ExecutorUri executorUri;
private final ExecutorURI executorUri;
@NotNull
private final AuctionedTaskType taskType;
@ -24,7 +24,7 @@ public class ExecutorAddedEvent extends SelfValidating<ExecutorAddedEvent> {
*
* @param executorUri the identifier of the executor that was added to this TAPAS application
*/
public ExecutorAddedEvent(ExecutorUri executorUri, AuctionedTaskType taskType) {
public ExecutorAddedEvent(ExecutorURI executorUri, AuctionedTaskType taskType) {
this.executorUri = executorUri;
this.taskType = taskType;

View File

@ -1,7 +1,7 @@
package ch.unisg.tapas.auctionhouse.application.port.in;
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry;
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry.ExecutorUri;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.tapas.common.SelfValidating;
import lombok.Value;
@ -13,14 +13,14 @@ import javax.validation.constraints.NotNull;
@Value
public class ExecutorRemovedEvent extends SelfValidating<ExecutorRemovedEvent> {
@NotNull
private final ExecutorUri executorUri;
private final ExecutorURI executorUri;
/**
* Constructs an executor removed event.
*
* @param executorUri
*/
public ExecutorRemovedEvent(ExecutorUri executorUri) {
public ExecutorRemovedEvent(ExecutorURI executorUri) {
this.executorUri = executorUri;
this.validateSelf();
}

View File

@ -0,0 +1,17 @@
package ch.unisg.tapas.auctionhouse.application.service;
import ch.unisg.tapas.auctionhouse.adapter.out.web.GetExecutorsFromExecutorPoolWebAdapter;
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry;
public class GetExecutorsService {
public boolean getExecutorsFromExecutorPool(){
var getExecutorsAdapter = new GetExecutorsFromExecutorPoolWebAdapter();
var executors = getExecutorsAdapter.getExecutorsInExecutorPool();
if(executors == null){
return false;
}
var executorRegistry = ExecutorRegistry.getInstance();
return executorRegistry.addExecutors(executors);
}
}

View File

@ -0,0 +1,20 @@
package ch.unisg.tapas.auctionhouse.domain;
import ch.unisg.common.valueobject.ExecutorURI;
import lombok.Getter;
import lombok.Setter;
public class ExecutorInfo {
@Getter
@Setter
private ExecutorURI executorURI;
@Getter
@Setter
private Auction.AuctionedTaskType executorType;
public ExecutorInfo(String executorURI, String executorType){
this.executorURI = new ExecutorURI(executorURI);
this.executorType = new Auction.AuctionedTaskType(executorType);
}
}

View File

@ -1,6 +1,7 @@
package ch.unisg.tapas.auctionhouse.domain;
import lombok.Value;
import ch.unisg.common.valueobject.ExecutorURI;
import java.net.URI;
import java.util.*;
@ -14,7 +15,7 @@ import java.util.*;
public class ExecutorRegistry {
private static ExecutorRegistry registry;
private final Map<Auction.AuctionedTaskType, Set<ExecutorUri>> executors;
private final Map<Auction.AuctionedTaskType, Set<ExecutorURI>> executors;
private ExecutorRegistry() {
this.executors = new Hashtable<>();
@ -35,8 +36,8 @@ public class ExecutorRegistry {
* @param executorUri the executor's URI
* @return true unless a runtime exception occurs
*/
public boolean addExecutor(Auction.AuctionedTaskType taskType, ExecutorUri executorUri) {
Set<ExecutorUri> taskTypeExecs = executors.getOrDefault(taskType,
public boolean addExecutor(Auction.AuctionedTaskType taskType, ExecutorURI executorUri) {
Set<ExecutorURI> taskTypeExecs = executors.getOrDefault(taskType,
Collections.synchronizedSet(new HashSet<>()));
taskTypeExecs.add(executorUri);
@ -45,18 +46,27 @@ public class ExecutorRegistry {
return true;
}
public boolean addExecutors(List<ExecutorInfo> executors){
for (var executor : executors) {
if(!addExecutor(executor.getExecutorType(), executor.getExecutorURI())){
return false;
}
}
return true;
}
/**
* Removes an executor from the registry. The executor is disassociated from all known task types.
*
* @param executorUri the executor's URI
* @return true unless a runtime exception occurs
*/
public boolean removeExecutor(ExecutorUri executorUri) {
public boolean removeExecutor(ExecutorURI executorUri) {
Iterator<Auction.AuctionedTaskType> iterator = executors.keySet().iterator();
while (iterator.hasNext()) {
Auction.AuctionedTaskType taskType = iterator.next();
Set<ExecutorUri> set = executors.get(taskType);
Set<ExecutorURI> set = executors.get(taskType);
set.remove(executorUri);
@ -78,10 +88,4 @@ public class ExecutorRegistry {
public boolean containsTaskType(Auction.AuctionedTaskType taskType) {
return executors.containsKey(taskType);
}
// Value Object for the executor identifier
@Value
public static class ExecutorUri {
URI value;
}
}