-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…1473) Co-authored-by: Simon Braitsch <simon.braitsch@novatec-gmbh.de>
- Loading branch information
1 parent
94fdf8d
commit 945412d
Showing
10 changed files
with
397 additions
and
3 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...va/rocks/inspectit/ocelot/agentcommunication/handlers/impl/EnvironmentCommandHandler.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,84 @@ | ||
package rocks.inspectit.ocelot.agentcommunication.handlers.impl; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.async.DeferredResult; | ||
import rocks.inspectit.ocelot.agentcommunication.handlers.CommandHandler; | ||
import rocks.inspectit.ocelot.commons.models.command.Command; | ||
import rocks.inspectit.ocelot.commons.models.command.CommandResponse; | ||
import rocks.inspectit.ocelot.commons.models.command.impl.EnvironmentCommand; | ||
import rocks.inspectit.ocelot.config.model.InspectitServerSettings; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Handler for the Agent Environment command. | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class EnvironmentCommandHandler implements CommandHandler { | ||
|
||
@Autowired | ||
private InspectitServerSettings configuration; | ||
|
||
/** | ||
* Checks if the given {@link Command} is an instance of {@link EnvironmentCommand}. | ||
* | ||
* @param command The command which should be checked. | ||
* @return True if the given command is an instance of {@link EnvironmentCommand}. | ||
*/ | ||
@Override | ||
public boolean canHandle(Command command) { | ||
return command instanceof EnvironmentCommand; | ||
} | ||
|
||
/** | ||
* Checks if the given {@link CommandResponse} is an instance of {@link EnvironmentCommand.Response}. | ||
* | ||
* @param response The response which should be checked. | ||
* @return True if the given response is an instance of {@link EnvironmentCommand.Response}. | ||
*/ | ||
@Override | ||
public boolean canHandle(CommandResponse response) { | ||
return response instanceof EnvironmentCommand.Response; | ||
} | ||
|
||
/** | ||
* Prepares an instance of {@link DeferredResult} by setting the Timeout as well as the onTimeout function. | ||
* This onTimeout function sets the {@link ResponseEntity} to the status REQUEST_TIMEOUT. | ||
* | ||
* @param agentId The id of the agent the command is meant for. | ||
* @param command The command to be Executed. | ||
* @return An instance of {@link DeferredResult} with a set timeout value and a set timeout function. | ||
*/ | ||
@Override | ||
public DeferredResult<ResponseEntity<?>> prepareResponse(String agentId, Command command) { | ||
if (!canHandle(command)) { | ||
throw new IllegalArgumentException("EnvironmentCommandHandler can only handle commands of type EnvironmentCommand."); | ||
} | ||
|
||
Duration responseTimeout = configuration.getAgentCommand().getResponseTimeout(); | ||
DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(responseTimeout.toMillis()); | ||
deferredResult.onTimeout(() -> ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).build()); | ||
|
||
return deferredResult; | ||
} | ||
|
||
/** | ||
* Takes an instance of {@link CommandResponse} as well as an instance of {@link DeferredResult}. | ||
* Sets the {@link ResponseEntity} of the {@link DeferredResult} according to the | ||
* Environment Information received from the respective Agent." | ||
* | ||
* @param response The {@link CommandResponse} to be handled. | ||
* @param result The {@link DeferredResult} the response should be written in. | ||
*/ | ||
@Override | ||
public void handleResponse(CommandResponse response, DeferredResult<ResponseEntity<?>> result) { | ||
EnvironmentCommand.Response environmentResponse = (EnvironmentCommand.Response) response; | ||
result.setResult(ResponseEntity.ok().body(environmentResponse.getEnvironment())); | ||
} | ||
|
||
} |
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
75 changes: 75 additions & 0 deletions
75
...ocks/inspectit/ocelot/agentcommunication/handlers/impl/EnvironmentCommandHandlerTest.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,75 @@ | ||
package rocks.inspectit.ocelot.agentcommunication.handlers.impl; | ||
|
||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Answers; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.context.request.async.DeferredResult; | ||
import rocks.inspectit.ocelot.commons.models.command.Command; | ||
import rocks.inspectit.ocelot.commons.models.command.impl.EnvironmentCommand; | ||
import rocks.inspectit.ocelot.config.model.InspectitServerSettings; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class EnvironmentCommandHandlerTest { | ||
|
||
@InjectMocks | ||
EnvironmentCommandHandler environmentCommandHandler; | ||
|
||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
private InspectitServerSettings configuration; | ||
|
||
@Nested | ||
class PrepareResponse { | ||
|
||
@Test | ||
public void cantHandleCommand() { | ||
Command command = null; | ||
String agentId = "test-id"; | ||
|
||
try { | ||
environmentCommandHandler.prepareResponse(agentId, command); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage()).isEqualTo("EnvironmentCommandHandler can only handle commands of type EnvironmentCommand."); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void preparesResponse() { | ||
when(configuration.getAgentCommand().getResponseTimeout()).thenReturn(Duration.ofMinutes(1)); | ||
EnvironmentCommand command = new EnvironmentCommand(); | ||
String agentId = "test-id"; | ||
|
||
DeferredResult<ResponseEntity<?>> result = environmentCommandHandler.prepareResponse(agentId, command); | ||
|
||
assertThat(result).isNotNull(); | ||
|
||
} | ||
|
||
} | ||
|
||
@Nested | ||
class HandleResponse { | ||
|
||
@Test | ||
public void handlesResponse() { | ||
EnvironmentCommand.Response mockResponse = mock(EnvironmentCommand.Response.class); | ||
DeferredResult<ResponseEntity<?>> result = new DeferredResult<>(); | ||
|
||
environmentCommandHandler.handleResponse(mockResponse, result); | ||
|
||
assertThat(result.getResult()).isEqualTo(ResponseEntity.ok().build()); | ||
} | ||
} | ||
|
||
} |
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
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
51 changes: 51 additions & 0 deletions
51
.../src/main/java/rocks/inspectit/ocelot/commons/models/command/impl/EnvironmentCommand.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,51 @@ | ||
package rocks.inspectit.ocelot.commons.models.command.impl; | ||
|
||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
import rocks.inspectit.ocelot.commons.models.command.Command; | ||
import rocks.inspectit.ocelot.commons.models.command.CommandResponse; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Represents an Environment-Command. Environment commands are used to receive the details of a certain agent. | ||
* These details include environment variables, system properties and JVM arguments. | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
public class EnvironmentCommand extends Command { | ||
|
||
/** | ||
* Type identifier for JSON serialization. | ||
*/ | ||
public static final String TYPE_IDENTIFIER = "environment"; | ||
|
||
/** | ||
* Represents a response to the {@link EnvironmentCommand}. | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
public static class Response extends CommandResponse { | ||
private EnvironmentDetail environment; | ||
} | ||
|
||
/** | ||
* Represents the structure of the returned environment details | ||
*/ | ||
@Data | ||
public static class EnvironmentDetail { | ||
@JsonPropertyOrder(alphabetic = true) | ||
private Map<String, String> environmentVariables; | ||
@JsonPropertyOrder(alphabetic = true) | ||
private Properties systemProperties; | ||
private List<String> jvmArguments; | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...ain/java/rocks/inspectit/ocelot/core/command/handler/impl/EnvironmentCommandExecutor.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,55 @@ | ||
package rocks.inspectit.ocelot.core.command.handler.impl; | ||
|
||
import org.springframework.stereotype.Component; | ||
import rocks.inspectit.ocelot.commons.models.command.Command; | ||
import rocks.inspectit.ocelot.commons.models.command.CommandResponse; | ||
import rocks.inspectit.ocelot.commons.models.command.impl.EnvironmentCommand; | ||
import rocks.inspectit.ocelot.core.command.handler.CommandExecutor; | ||
|
||
import java.lang.management.ManagementFactory; | ||
|
||
/** | ||
* Executor for executing {@link EnvironmentCommand}s. | ||
*/ | ||
@Component | ||
public class EnvironmentCommandExecutor implements CommandExecutor { | ||
|
||
/** | ||
* Checks if the given {@link Command} is an instance of {@link EnvironmentCommand}. | ||
* | ||
* @param command The {@link Command} to be checked. | ||
* @return True if the given {@link Command} is an instance of {@link EnvironmentCommand}. | ||
*/ | ||
@Override | ||
public boolean canExecute(Command command) { | ||
return command instanceof EnvironmentCommand; | ||
} | ||
|
||
/** | ||
* Executes the given {@link Command}. Throws an {@link IllegalArgumentException} if the given command is either null | ||
* or not handled by this implementation. | ||
* Populates an instance of {@link EnvironmentCommand.EnvironmentDetail} with the respective values from the agent. | ||
* | ||
* @param command The command to be executed. | ||
* @return An instance of {@link EnvironmentCommand} with alive set to true and the id of the given command. | ||
*/ | ||
@Override | ||
public CommandResponse execute(Command command) { | ||
if (!canExecute(command)) { | ||
String exceptionMessage = "Invalid command type. Executor does not support commands of type " + command.getClass(); | ||
throw new IllegalArgumentException(exceptionMessage); | ||
} | ||
|
||
EnvironmentCommand.Response response = new EnvironmentCommand.Response(); | ||
response.setCommandId(command.getCommandId()); | ||
|
||
EnvironmentCommand.EnvironmentDetail body = new EnvironmentCommand.EnvironmentDetail(); | ||
body.setEnvironmentVariables(System.getenv()); | ||
body.setSystemProperties(System.getProperties()); | ||
body.setJvmArguments(ManagementFactory.getRuntimeMXBean().getInputArguments()); | ||
|
||
response.setEnvironment(body); | ||
|
||
return response; | ||
} | ||
} |
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
Oops, something went wrong.