Search in sources :

Example 16 with Configuration

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);
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Context(com.aws.greengrass.dependency.Context) Configuration(com.aws.greengrass.config.Configuration) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) InputStream(java.io.InputStream) Map(java.util.Map)

Example 17 with Configuration

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<>();
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) Configuration(com.aws.greengrass.config.Configuration) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) ComponentConfiguration(com.amazon.aws.iot.greengrass.component.common.ComponentConfiguration) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with Configuration

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();
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Context(com.aws.greengrass.dependency.Context) Configuration(com.aws.greengrass.config.Configuration) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 19 with Configuration

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()));
    }
}
Also used : Context(com.aws.greengrass.dependency.Context) Path(java.nio.file.Path) ConfigurationWriter(com.aws.greengrass.config.ConfigurationWriter) Configuration(com.aws.greengrass.config.Configuration) KernelLifecycle(com.aws.greengrass.lifecyclemanager.KernelLifecycle) Topic(com.aws.greengrass.config.Topic) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with Configuration

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());
}
Also used : Context(com.aws.greengrass.dependency.Context) Path(java.nio.file.Path) Configuration(com.aws.greengrass.config.Configuration) KernelLifecycle(com.aws.greengrass.lifecyclemanager.KernelLifecycle) Topic(com.aws.greengrass.config.Topic) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Configuration (com.aws.greengrass.config.Configuration)20 Context (com.aws.greengrass.dependency.Context)12 Topics (com.aws.greengrass.config.Topics)8 Test (org.junit.jupiter.api.Test)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 Topic (com.aws.greengrass.config.Topic)3 UpdateBehaviorTree (com.aws.greengrass.config.UpdateBehaviorTree)3 Path (java.nio.file.Path)3 Map (java.util.Map)3 ComponentConfiguration (com.amazon.aws.iot.greengrass.component.common.ComponentConfiguration)2 DeploymentPackageConfiguration (com.aws.greengrass.deployment.model.DeploymentPackageConfiguration)2 KernelLifecycle (com.aws.greengrass.lifecyclemanager.KernelLifecycle)2 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)2 InputStream (java.io.InputStream)2 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)2 ConfigurationWriter (com.aws.greengrass.config.ConfigurationWriter)1 State (com.aws.greengrass.dependency.State)1 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)1