use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.
the class AuthorizationPolicyParserTest method readConfig.
private void readConfig(String filename) throws IOException {
realConfig = new Configuration(new Context());
try (InputStream inputStream = getClass().getResourceAsStream(filename)) {
assertNotNull(inputStream);
realConfig.mergeMap(0, new YAMLMapper().readValue(inputStream, Map.class));
}
when(kernel.getConfig()).thenReturn(realConfig);
}
use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.
the class KernelConfigResolver method resolveConfigurationToApply.
/**
* Resolve configurations to apply for a component. It resolves based on current running config, default config, and
* config update operation.
*
* @param configurationUpdateOperation nullable component configuration update operation.
* @param componentRecipe component recipe containing default configuration.
* @param document deployment document
* @return resolved configuration for this component. non null.
*/
@SuppressWarnings("PMD.ConfusingTernary")
private Map<String, Object> resolveConfigurationToApply(@Nullable ConfigurationUpdateOperation configurationUpdateOperation, ComponentRecipe componentRecipe, DeploymentDocument document) {
// try read the running service config
try (Context context = new Context()) {
Configuration currentRunningConfig = new Configuration(context);
// Copy from running config (if any)
Topics serviceTopics = kernel.findServiceTopic(componentRecipe.getComponentName());
if (serviceTopics != null) {
Topics configuration = serviceTopics.findTopics(CONFIGURATION_CONFIG_KEY);
if (configuration != null) {
currentRunningConfig.copyFrom(configuration);
}
} else if (ComponentType.NUCLEUS.equals(componentRecipe.getComponentType())) {
// Copy from existing Nucleus config (if any)
Topics nucleusTopics = kernel.findServiceTopic(deviceConfiguration.getNucleusComponentName());
if (nucleusTopics != null) {
Topics nucleusConfig = nucleusTopics.findTopics(CONFIGURATION_CONFIG_KEY);
if (nucleusConfig != null) {
currentRunningConfig.copyFrom(nucleusConfig);
}
}
}
// Remove keys which want to be reset to their default value
if (configurationUpdateOperation != null) {
removeKeysFromConfigWhichAreReset(currentRunningConfig, configurationUpdateOperation.getPathsToReset());
}
// Merge in the defaults with timestamp 1 so that they don't overwrite any pre-existing values
JsonNode defaultConfig = Optional.ofNullable(componentRecipe.getComponentConfiguration()).map(ComponentConfiguration::getDefaultConfiguration).orElse(// init null to be empty default config
MAPPER.createObjectNode());
// Merge in the defaults from the recipe using timestamp 1 to denote a default
currentRunningConfig.mergeMap(1, MAPPER.convertValue(defaultConfig, Map.class));
currentRunningConfig.context.waitForPublishQueueToClear();
// Merge in the requested config updates
if (configurationUpdateOperation != null && configurationUpdateOperation.getValueToMerge() != null) {
currentRunningConfig.mergeMap(document.getTimestamp(), configurationUpdateOperation.getValueToMerge());
}
return currentRunningConfig.toPOJO();
} catch (IOException ignored) {
}
return new HashMap<>();
}
use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.
the class AuthorizationHandlerTest method beforeEach.
@BeforeEach
void beforeEach() {
when(mockKernel.getConfig()).thenReturn(new Configuration(new Context()));
authModule = new AuthorizationModule();
}
use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.
the class KernelTest method GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_non_truncated_tlog.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_non_truncated_tlog() throws Exception {
Configuration config = kernel.getConfig();
Context context = kernel.getContext();
Path configPath = tempRootDir.resolve("config");
Files.createDirectories(configPath);
kernel.writeEffectiveConfigAsTransactionLog(configPath.resolve("full.tlog"));
try (ConfigurationWriter configurationWriter = ConfigurationWriter.logTransactionsTo(config, configPath.resolve("full.tlog")).flushImmediately(true)) {
kernel.parseArgs().launch();
Topic testTopic = config.lookup("testTopic").withValue("initial");
KernelLifecycle kernelLifecycle = context.get(KernelLifecycle.class);
context.runOnPublishQueueAndWait(() -> {
// make truncate run by setting a small limit
kernelLifecycle.getTlog().withMaxEntries(1);
testTopic.withValue("triggering truncate");
// immediately queue a task to increase max size to prevent repeated truncation
context.runOnPublishQueue(() -> kernelLifecycle.getTlog().withMaxEntries(10000));
});
// wait for things to complete
CountDownLatch startupCdl = new CountDownLatch(1);
context.addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.FINISHED)) {
startupCdl.countDown();
}
});
startupCdl.await(30, TimeUnit.SECONDS);
// shutdown to stop config/tlog changes
kernel.shutdown();
Configuration fullConfig = ConfigurationReader.createFromTLog(context, configPath.resolve("full.tlog"));
Configuration compressedConfig = ConfigurationReader.createFromTLog(context, configPath.resolve("config.tlog"));
assertEquals("triggering truncate", compressedConfig.find("testTopic").getOnce());
assertThat(fullConfig.toPOJO(), is(compressedConfig.toPOJO()));
}
}
use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.
the class KernelTest method GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_config.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_config() throws Exception {
kernel.parseArgs().launch();
Configuration config = kernel.getConfig();
Context context = kernel.getContext();
Path configPath = tempRootDir.resolve("config");
Topic testTopic = config.lookup("testTopic").withValue("initial");
KernelLifecycle kernelLifecycle = context.get(KernelLifecycle.class);
context.runOnPublishQueueAndWait(() -> {
// make truncate run by setting a small limit
kernelLifecycle.getTlog().withMaxEntries(1);
testTopic.withValue("triggering truncate");
// immediately queue a task to increase max size to prevent repeated truncation
context.runOnPublishQueue(() -> kernelLifecycle.getTlog().withMaxEntries(10000));
});
// wait for things to complete
CountDownLatch startupCdl = new CountDownLatch(1);
context.addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.FINISHED)) {
startupCdl.countDown();
}
});
startupCdl.await(30, TimeUnit.SECONDS);
// shutdown to stop config/tlog changes
kernel.shutdown();
Configuration tlogConfig = ConfigurationReader.createFromTLog(context, configPath.resolve("config.tlog"));
assertEquals("triggering truncate", tlogConfig.find("testTopic").getOnce());
// data type may be different when recreating tlog
// using this to coerce to string for comparison
assertEqualsDeepMap(config.toPOJO(), tlogConfig.toPOJO());
}
Aggregations