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);
});
}
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());
}
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());
}
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();
}
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));
}
Aggregations