Search in sources :

Example 1 with Subscriber

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

the class GreengrassService method addOrUpdateDependency.

/**
 * Add a dependency.
 *
 * @param dependentGreengrassService the service to add as a dependency.
 * @param dependencyType            type of the dependency.
 * @param isDefault                 True if the dependency is added without explicit declaration in 'dependencies'
 *                                  Topic.
 * @throws InputValidationException if the provided arguments are invalid.
 */
public synchronized void addOrUpdateDependency(GreengrassService dependentGreengrassService, DependencyType dependencyType, boolean isDefault) throws InputValidationException {
    if (dependentGreengrassService == null || dependencyType == null) {
        throw new InputValidationException("One or more parameters was null");
    }
    dependencies.compute(dependentGreengrassService, (dependentService, dependencyInfo) -> {
        // new subscriber with updated input.
        if (dependencyInfo != null) {
            dependentGreengrassService.removeStateSubscriber(dependencyInfo.stateTopicSubscriber);
        }
        Subscriber subscriber = createDependencySubscriber(dependentGreengrassService, dependencyType);
        dependentGreengrassService.addStateSubscriber(subscriber);
        context.get(Kernel.class).clearODcache();
        return new DependencyInfo(dependencyType, isDefault, subscriber);
    });
}
Also used : Subscriber(com.aws.greengrass.config.Subscriber) InputValidationException(com.aws.greengrass.lifecyclemanager.exceptions.InputValidationException)

Example 2 with Subscriber

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

the class TokenExchangeServiceTest method GIVEN_token_exchange_service_WHEN_started_with_empty_role_alias_THEN_server_errors_out.

@ParameterizedTest
@ValueSource(strings = { "  " })
@NullAndEmptySource
void GIVEN_token_exchange_service_WHEN_started_with_empty_role_alias_THEN_server_errors_out(String roleAlias, ExtensionContext context) {
    ignoreExceptionUltimateCauseOfType(context, IllegalArgumentException.class);
    // Set mock for role topic
    Topic roleTopic = mock(Topic.class);
    when(roleTopic.subscribe(any())).thenAnswer((a) -> {
        ((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, roleTopic);
        return null;
    });
    when(roleTopic.getOnce()).thenReturn(roleAlias);
    lenient().when(roleTopic.withValue(anyString())).thenReturn(roleTopic);
    when(roleTopic.dflt(anyString())).thenReturn(roleTopic);
    // set mock for port topic
    Topic portTopic = mock(Topic.class);
    when(portTopic.dflt(anyInt())).thenReturn(portTopic);
    when(portTopic.subscribe(any())).thenAnswer((a) -> {
        ((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, portTopic);
        return null;
    });
    when(portTopic.getOnce()).thenReturn(8080);
    when(config.lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC)).thenReturn(portTopic);
    when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY, IOT_ROLE_ALIAS_TOPIC)).thenReturn(roleTopic);
    TokenExchangeService tes = spy(new TokenExchangeService(config, mockCredentialHandler, mockAuthZHandler, executorService, deviceConfigurationWithRoleAlias(roleAlias)));
    ArgumentCaptor<State> stateArgumentCaptor = ArgumentCaptor.forClass(State.class);
    doNothing().when(tes).reportState(stateArgumentCaptor.capture());
    tes.startup();
    assertEquals(State.ERRORED, stateArgumentCaptor.getValue());
}
Also used : Subscriber(com.aws.greengrass.config.Subscriber) State(com.aws.greengrass.dependency.State) Topic(com.aws.greengrass.config.Topic) NullAndEmptySource(org.junit.jupiter.params.provider.NullAndEmptySource) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Subscriber

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

the class TokenExchangeServiceTest method GIVEN_token_exchange_service_WHEN_auth_errors_THEN_server_errors_out.

@Test
void GIVEN_token_exchange_service_WHEN_auth_errors_THEN_server_errors_out(ExtensionContext context) throws Exception {
    ignoreExceptionUltimateCauseOfType(context, AuthorizationException.class);
    doThrow(AuthorizationException.class).when(mockAuthZHandler).registerComponent(any(), any());
    // Set mock for role topic
    Topic roleTopic = mock(Topic.class);
    when(roleTopic.subscribe(any())).thenAnswer((a) -> {
        ((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, roleTopic);
        return null;
    });
    when(roleTopic.getOnce()).thenReturn("TEST");
    when(roleTopic.withValue(anyString())).thenReturn(roleTopic);
    when(roleTopic.dflt(anyString())).thenReturn(roleTopic);
    // set mock for port topic
    Topic portTopic = mock(Topic.class);
    when(portTopic.dflt(anyInt())).thenReturn(portTopic);
    when(portTopic.subscribe(any())).thenAnswer((a) -> {
        ((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, portTopic);
        return null;
    });
    when(portTopic.getOnce()).thenReturn(8080);
    when(config.lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC)).thenReturn(portTopic);
    when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY, IOT_ROLE_ALIAS_TOPIC)).thenReturn(roleTopic);
    TokenExchangeService tes = spy(new TokenExchangeService(config, mockCredentialHandler, mockAuthZHandler, executorService, deviceConfigurationWithRoleAlias("TEST")));
    ArgumentCaptor<State> stateArgumentCaptor = ArgumentCaptor.forClass(State.class);
    doNothing().when(tes).reportState(stateArgumentCaptor.capture());
    tes.postInject();
    assertEquals(State.ERRORED, stateArgumentCaptor.getValue());
    // this time make loadAuthorizationPolicy throw
    doNothing().when(mockAuthZHandler).registerComponent(any(), any());
    // GG_NEEDS_REVIEW: TODO: this no longer throws an exception; we need to parse the log to check the behavior
    // doThrow(AuthorizationException.class).when(mockAuthZHandler).loadAuthorizationPolicies(any(), any(), false);
    tes.postInject();
    assertEquals(State.ERRORED, stateArgumentCaptor.getValue());
}
Also used : Subscriber(com.aws.greengrass.config.Subscriber) State(com.aws.greengrass.dependency.State) Topic(com.aws.greengrass.config.Topic) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Subscriber

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

the class GenericExternalServiceIntegTest method GIVEN_service_with_dynamically_loaded_config_WHEN_dynamic_config_changes_THEN_service_does_not_restart.

@Test
void GIVEN_service_with_dynamically_loaded_config_WHEN_dynamic_config_changes_THEN_service_does_not_restart() throws Exception {
    ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("service_with_dynamic_config.yaml"));
    CountDownLatch mainRunning = new CountDownLatch(1);
    kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
        if (service.getName().equals("main") && newState.equals(State.RUNNING)) {
            mainRunning.countDown();
        }
    });
    kernel.launch();
    assertTrue(mainRunning.await(5, TimeUnit.SECONDS));
    GenericExternalService service = spy((GenericExternalService) kernel.locate("service_with_dynamic_config"));
    assertEquals(State.RUNNING, service.getState());
    CompletableFuture<Void> topicUpdateProcessedFuture = new CompletableFuture<>();
    Subscriber customConfigWatcher = (WhatHappened what, Topic t) -> {
        topicUpdateProcessedFuture.complete(null);
    };
    Topic customConfigTopic = service.getServiceConfig().find(CONFIGURATION_CONFIG_KEY, "my_custom_key");
    customConfigTopic.subscribe(customConfigWatcher);
    customConfigTopic.withValue("my_custom_initial_value");
    topicUpdateProcessedFuture.get();
    assertEquals(State.RUNNING, service.getState());
    verify(service, times(0)).requestReinstall();
    verify(service, times(0)).requestRestart();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) WhatHappened(com.aws.greengrass.config.WhatHappened) Subscriber(com.aws.greengrass.config.Subscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Topic(com.aws.greengrass.config.Topic) GenericExternalService(com.aws.greengrass.lifecyclemanager.GenericExternalService) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Subscriber

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

the class GreengrassService method waitForDependersToExit.

private void waitForDependersToExit() throws InterruptedException {
    List<GreengrassService> dependers = getHardDependers();
    Subscriber dependerExitWatcher = (WhatHappened what, Topic t) -> {
        synchronized (dependersExitedLock) {
            if (dependersExited(dependers)) {
                dependersExitedLock.notifyAll();
            }
        }
    };
    // subscribing to depender state changes
    dependers.forEach(dependerGreengrassService -> dependerGreengrassService.addStateSubscriber(dependerExitWatcher));
    synchronized (dependersExitedLock) {
        while (!dependersExited(dependers)) {
            logger.atDebug("service-waiting-for-depender-to-finish").log();
            dependersExitedLock.wait();
        }
    }
    // removing state change watchers
    dependers.forEach(dependerGreengrassService -> dependerGreengrassService.removeStateSubscriber(dependerExitWatcher));
}
Also used : WhatHappened(com.aws.greengrass.config.WhatHappened) Subscriber(com.aws.greengrass.config.Subscriber) Topic(com.aws.greengrass.config.Topic)

Aggregations

Subscriber (com.aws.greengrass.config.Subscriber)7 Topic (com.aws.greengrass.config.Topic)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Test (org.junit.jupiter.api.Test)3 WhatHappened (com.aws.greengrass.config.WhatHappened)2 State (com.aws.greengrass.dependency.State)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Topics (com.aws.greengrass.config.Topics)1 GenericExternalService (com.aws.greengrass.lifecyclemanager.GenericExternalService)1 InputValidationException (com.aws.greengrass.lifecyclemanager.exceptions.InputValidationException)1 Headers (com.sun.net.httpserver.Headers)1 URI (java.net.URI)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 NullAndEmptySource (org.junit.jupiter.params.provider.NullAndEmptySource)1