Search in sources :

Example 6 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class GreengrassServiceTest method beforeEach.

@BeforeEach
void beforeEach() throws IOException, URISyntaxException, ServiceLoadException {
    Path configPath = Paths.get(this.getClass().getResource("services.yaml").toURI());
    context = spy(new Context());
    context.put(Kernel.class, kernel);
    configuration = new Configuration(context);
    configuration.read(configPath);
    Topics root = configuration.getRoot();
    bService = new GreengrassService(root.findTopics(SERVICES_NAMESPACE_TOPIC, "B"));
    cService = new GreengrassService(root.findTopics(SERVICES_NAMESPACE_TOPIC, "C"));
    dService = new GreengrassService(root.findTopics(SERVICES_NAMESPACE_TOPIC, "D"));
    eService = new GreengrassService(root.findTopics(SERVICES_NAMESPACE_TOPIC, "E"));
    when(kernel.locateIgnoreError("B")).thenReturn(bService);
    when(kernel.locateIgnoreError("C")).thenReturn(cService);
    when(kernel.locateIgnoreError("D")).thenReturn(dService);
    lenient().when(kernel.locateIgnoreError("E")).thenReturn(eService);
    aService = spy(new GreengrassService(root.findTopics(SERVICES_NAMESPACE_TOPIC, "A")));
}
Also used : Path(java.nio.file.Path) Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) Configuration(com.aws.greengrass.config.Configuration) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class ConfigStoreIPCEventStreamAgentTest method setup.

@BeforeEach
public void setup() {
    configuration = new Configuration(new Context());
    Topics root = configuration.getRoot();
    root.lookup(SERVICES_NAMESPACE_TOPIC, TEST_COMPONENT_A, CONFIGURATION_CONFIG_KEY, TEST_CONFIG_KEY_1).withNewerValue(100, TEST_CONFIG_KEY_1_INITIAL_VALUE);
    root.lookup(SERVICES_NAMESPACE_TOPIC, TEST_COMPONENT_A, CONFIGURATION_CONFIG_KEY, TEST_CONFIG_KEY_2).withNewerValue(100, TEST_CONFIG_KEY_2_INITIAL_VALUE);
    root.lookupTopics(SERVICES_NAMESPACE_TOPIC, TEST_COMPONENT_B);
    root.lookup(SERVICES_NAMESPACE_TOPIC, TEST_COMPONENT_B, CONFIGURATION_CONFIG_KEY, TEST_CONFIG_KEY_3).withNewerValue(100, TEST_CONFIG_KEY_3_INITIAL_VALUE);
    configuration.context.waitForPublishQueueToClear();
    lenient().when(kernel.getConfig()).thenReturn(configuration);
    when(mockContext.getContinuation()).thenReturn(mockServerConnectionContinuation);
    when(mockContext.getAuthenticationData()).thenReturn(mockAuthenticationData);
    lenient().when(mockContext2.getContinuation()).thenReturn(mock(ServerConnectionContinuation.class));
    lenient().when(mockContext2.getAuthenticationData()).thenReturn(mockAuthenticationData2);
    agent = new ConfigStoreIPCEventStreamAgent();
    agent.setKernel(kernel);
}
Also used : Context(com.aws.greengrass.dependency.Context) OperationContinuationHandlerContext(software.amazon.awssdk.eventstreamrpc.OperationContinuationHandlerContext) Topics(com.aws.greengrass.config.Topics) Configuration(com.aws.greengrass.config.Configuration) ServerConnectionContinuation(software.amazon.awssdk.crt.eventstream.ServerConnectionContinuation) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class Lifecycle method startStateTransition.

private void startStateTransition() throws InterruptedException {
    AtomicReference<Predicate<Object>> asyncFinishAction = new AtomicReference<>((stateEvent) -> true);
    State prevState = getState();
    while (!(isClosed.get() && getState().isClosable())) {
        Optional<State> desiredState;
        State current = getState();
        logger.atDebug("service-state-transition-start").log();
        Configuration kernelConfig = greengrassService.getContext().get(Configuration.class);
        // postpone start/install when configuration is under update.
        if (current == State.NEW || current == State.INSTALLED) {
            kernelConfig.waitConfigUpdateComplete();
        }
        // if already in desired state, remove the head of desired state list.
        desiredState = peekOrRemoveFirstDesiredState(current);
        while (desiredState.isPresent() && desiredState.get().equals(current)) {
            desiredState = peekOrRemoveFirstDesiredState(current);
        }
        switch(current) {
            case BROKEN:
                handleCurrentStateBroken(desiredState, prevState);
                break;
            case NEW:
                handleCurrentStateNew(desiredState);
                break;
            case INSTALLED:
                handleCurrentStateInstalledAsync(desiredState, asyncFinishAction);
                break;
            case STARTING:
                handleCurrentStateStartingAsync(desiredState, asyncFinishAction);
                break;
            case RUNNING:
                handleCurrentStateRunning(desiredState);
                break;
            case STOPPING:
                handleCurrentStateStopping();
                break;
            case FINISHED:
                handleCurrentStateFinished(desiredState);
                break;
            case ERRORED:
                handleCurrentStateErrored(desiredState, prevState);
                break;
            default:
                logger.atError(INVALID_STATE_ERROR_EVENT).log("Unrecognized current state");
                break;
        }
        boolean canFinish = false;
        while (!canFinish) {
            // A state event can either be a report state transition event or a desired state updated event.
            Object stateEvent = stateEventQueue.poll();
            // drain them until a "State" event is encountered.
            while (!(stateEvent instanceof State) && !stateEventQueue.isEmpty()) {
                stateEvent = stateEventQueue.poll();
            }
            // if there are no events in the queue, block until one is available.
            if (stateEvent == null) {
                stateEvent = stateEventQueue.take();
            }
            if (stateEvent instanceof State) {
                State newState = (State) stateEvent;
                if (newState == current) {
                    continue;
                }
                canFinish = true;
                setState(current, newState);
                prevState = current;
            }
            if (asyncFinishAction.get().test(stateEvent)) {
                canFinish = true;
            }
        }
        asyncFinishAction.set((stateEvent) -> true);
    }
}
Also used : Configuration(com.aws.greengrass.config.Configuration) State(com.aws.greengrass.dependency.State) AtomicReference(java.util.concurrent.atomic.AtomicReference) Predicate(java.util.function.Predicate)

Example 9 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class KernelTest method GIVEN_kernel_WHEN_locate_finds_class_definition_in_config_THEN_create_service.

@SuppressWarnings("PMD.AvoidCatchingGenericException")
@Test
void GIVEN_kernel_WHEN_locate_finds_class_definition_in_config_THEN_create_service(ExtensionContext context) throws Exception {
    // We need to launch the kernel here as this triggers EZPlugins to search the classpath for @ImplementsService
    // it complains that there's no main, but we don't care for this test
    ignoreExceptionUltimateCauseWithMessage(context, "No matching definition in system model for: main");
    try {
        kernel.parseArgs().launch();
    } catch (RuntimeException ignored) {
    }
    Configuration config = kernel.getConfig();
    config.lookup(GreengrassService.SERVICES_NAMESPACE_TOPIC, "1", "class").withValue(TestClass.class.getName());
    GreengrassService main = kernel.locate("1");
    assertEquals("tester", main.getName());
    GreengrassService service2 = kernel.locate("testImpl");
    assertEquals("testImpl", service2.getName());
}
Also used : Configuration(com.aws.greengrass.config.Configuration) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Test(org.junit.jupiter.api.Test)

Example 10 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleTest method setupContext.

@BeforeEach
void setupContext() throws IOException {
    context = new Context();
    ScheduledThreadPoolExecutor ses = new ScheduledThreadPoolExecutor(4);
    ExecutorService executorService = Executors.newCachedThreadPool();
    context.put(ScheduledThreadPoolExecutor.class, ses);
    context.put(ScheduledExecutorService.class, ses);
    context.put(Executor.class, executorService);
    context.put(ExecutorService.class, executorService);
    context.put(ThreadPoolExecutor.class, ses);
    context.put(Clock.class, Clock.systemUTC());
    context.put(Kernel.class, mock(Kernel.class));
    Topics rootConfig = new Configuration(context).getRoot();
    config = rootConfig.createInteriorChild(GreengrassService.SERVICES_NAMESPACE_TOPIC).createInteriorChild("MockService");
    try (InputStream inputStream = new ByteArrayInputStream(BLANK_CONFIG_YAML_WITH_TIMEOUT.getBytes())) {
        config.updateFromMap(new YAMLMapper().readValue(inputStream, Map.class), new UpdateBehaviorTree(UpdateBehaviorTree.UpdateBehavior.MERGE, 0));
    }
    lenient().when(greengrassService.getConfig()).thenReturn(config);
    lenient().when(greengrassService.getRuntimeConfig()).thenReturn(config.lookupTopics(RUNTIME_STORE_NAMESPACE_TOPIC));
    lenient().when(greengrassService.getPrivateConfig()).thenReturn(config.lookupTopics(PRIVATE_STORE_NAMESPACE_TOPIC));
    lenient().when(greengrassService.getContext()).thenReturn(context);
    lenient().when(greengrassService.dependencyReady()).thenReturn(true);
    lenient().when(greengrassService.getState()).thenAnswer((a) -> State.values()[Coerce.toInt(greengrassService.getPrivateConfig().findLeafChild(STATE_TOPIC_NAME))]);
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) Configuration(com.aws.greengrass.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) UpdateBehaviorTree(com.aws.greengrass.config.UpdateBehaviorTree) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Map(java.util.Map) BeforeEach(org.junit.jupiter.api.BeforeEach)

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