use of com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC in project aws-greengrass-nucleus by aws-greengrass.
the class MultiGroupDeploymentTest method GIVEN_device_belongs_to_two_groups_WHEN_device_is_removed_from_one_group_THEN_next_deployment_removes_corresponding_component.
@Test
void GIVEN_device_belongs_to_two_groups_WHEN_device_is_removed_from_one_group_THEN_next_deployment_removes_corresponding_component() throws Exception {
CountDownLatch firstGroupCDL = new CountDownLatch(1);
CountDownLatch secondGroupCDL = new CountDownLatch(1);
DeploymentStatusKeeper deploymentStatusKeeper = kernel.getContext().get(DeploymentStatusKeeper.class);
deploymentStatusKeeper.registerDeploymentStatusConsumer(Deployment.DeploymentType.IOT_JOBS, (status) -> {
if (status.get(DEPLOYMENT_ID_KEY_NAME).equals("firstGroup") && status.get(DEPLOYMENT_STATUS_KEY_NAME).equals("SUCCEEDED")) {
firstGroupCDL.countDown();
}
if (status.get(DEPLOYMENT_ID_KEY_NAME).equals("secondGroup") && status.get(DEPLOYMENT_STATUS_KEY_NAME).equals("SUCCEEDED")) {
secondGroupCDL.countDown();
}
return true;
}, "dummyValue");
when(thingGroupHelper.listThingGroupsForDevice(anyInt())).thenReturn(Optional.of(new HashSet<>(Arrays.asList("firstGroup", "secondGroup"))));
submitSampleJobDocument(DeploymentServiceIntegrationTest.class.getResource("FleetConfigWithRedSignalService.json").toURI(), "firstGroup", Deployment.DeploymentType.IOT_JOBS);
assertTrue(firstGroupCDL.await(10, TimeUnit.SECONDS));
when(thingGroupHelper.listThingGroupsForDevice(anyInt())).thenReturn(Optional.of(new HashSet<>(Arrays.asList("secondGroup"))));
submitSampleJobDocument(DeploymentServiceIntegrationTest.class.getResource("FleetConfigWithSomeService.json").toURI(), "secondGroup", Deployment.DeploymentType.IOT_JOBS);
assertTrue(secondGroupCDL.await(10, TimeUnit.SECONDS));
Topics groupToRootTopic = kernel.getConfig().lookupTopics(SERVICES_NAMESPACE_TOPIC, DEPLOYMENT_SERVICE_TOPICS, GROUP_TO_ROOT_COMPONENTS_TOPICS);
List<String> groupNames = new ArrayList<>();
groupToRootTopic.forEach(node -> groupNames.add(node.getName()));
assertTrue(groupNames.containsAll(Arrays.asList("secondGroup")), "Device should belong to firstGroup and secondGroup");
Map<GreengrassService, DependencyType> dependenciesAfter = kernel.getMain().getDependencies();
List<String> serviceNames = dependenciesAfter.keySet().stream().map(service -> service.getName()).collect(Collectors.toList());
assertTrue(serviceNames.containsAll(Arrays.asList("SomeService")));
Topics componentsToGroupTopic = kernel.getConfig().lookupTopics(SERVICES_NAMESPACE_TOPIC, DEPLOYMENT_SERVICE_TOPICS, COMPONENTS_TO_GROUPS_TOPICS);
assertNotNull(componentsToGroupTopic.find("SomeService", "secondGroup"));
}
use of com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC in project aws-greengrass-nucleus by aws-greengrass.
the class DeviceConfiguration method initNucleusComponentName.
/**
* Get the Nucleus component name to lookup the configuration in the right place. If no component of type Nucleus
* exists, create service config for the default Nucleus component.
*/
private String initNucleusComponentName() {
Optional<CaseInsensitiveString> nucleusComponent = kernel.getConfig().lookupTopics(SERVICES_NAMESPACE_TOPIC).children.keySet().stream().filter(s -> ComponentType.NUCLEUS.name().equals(getComponentType(s.toString()))).findAny();
String nucleusComponentName = nucleusComponent.isPresent() ? nucleusComponent.get().toString() : DEFAULT_NUCLEUS_COMPONENT_NAME;
// Initialize default/inferred required config if it doesn't exist
initializeNucleusComponentConfig(nucleusComponentName);
return nucleusComponentName;
}
use of com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC in project aws-greengrass-nucleus by aws-greengrass.
the class DeviceConfiguration method persistInitialLaunchParams.
/**
* Persist initial launch parameters of JVM options.
*
* @param kernelAlts KernelAlternatives instance
*/
void persistInitialLaunchParams(KernelAlternatives kernelAlts) {
if (Files.exists(kernelAlts.getLaunchParamsPath())) {
logger.atDebug().log("Nucleus launch parameters has already been set up");
return;
}
// Persist initial Nucleus launch parameters
try {
String jvmOptions = ManagementFactory.getRuntimeMXBean().getInputArguments().stream().sorted().filter(s -> !s.startsWith(JVM_OPTION_ROOT_PATH)).collect(Collectors.joining(" "));
kernel.getConfig().lookup(SERVICES_NAMESPACE_TOPIC, getNucleusComponentName(), CONFIGURATION_CONFIG_KEY, DEVICE_PARAM_JVM_OPTIONS).withNewerValue(DEFAULT_VALUE_TIMESTAMP + 1, jvmOptions);
kernelAlts.writeLaunchParamsToFile(jvmOptions);
logger.atInfo().log("Successfully setup Nucleus launch parameters");
} catch (IOException e) {
logger.atError().log("Unable to setup Nucleus launch parameters", e);
}
}
use of com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC in project aws-greengrass-nucleus by aws-greengrass.
the class Kernel method locateExternalPlugin.
@SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.CloseResource" })
private Class<?> locateExternalPlugin(String name, Topics serviceRootTopics) throws ServiceLoadException {
ComponentIdentifier componentId = ComponentIdentifier.fromServiceTopics(serviceRootTopics);
Path pluginJar;
try {
pluginJar = nucleusPaths.artifactPath(componentId).resolve(componentId.getName() + JAR_FILE_EXTENSION);
} catch (IOException e) {
throw new ServiceLoadException(e);
}
if (!pluginJar.toFile().exists() || !pluginJar.toFile().isFile()) {
throw new ServiceLoadException(String.format("Unable to find %s because %s does not exist", name, pluginJar));
}
Topic storedDigest = config.find(SERVICES_NAMESPACE_TOPIC, MAIN_SERVICE_NAME, GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC, SERVICE_DIGEST_TOPIC_KEY, componentId.toString());
if (storedDigest == null || storedDigest.getOnce() == null) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local external plugin is not supported by this greengrass version");
throw new ServiceLoadException("Custom plugins is not supported by this greengrass version");
}
ComponentStore componentStore = context.get(ComponentStore.class);
if (!componentStore.validateComponentRecipeDigest(componentId, Coerce.toString(storedDigest))) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local plugin does not match the version in cloud!!");
throw new ServiceLoadException("Plugin has been modified after it was downloaded");
}
Class<?> clazz;
try {
AtomicReference<Class<?>> classReference = new AtomicReference<>();
EZPlugins ezPlugins = context.get(EZPlugins.class);
ezPlugins.loadPlugin(pluginJar, (sc) -> sc.matchClassesWithAnnotation(ImplementsService.class, (c) -> {
// Only use the class whose name matches what we want
ImplementsService serviceImplementation = c.getAnnotation(ImplementsService.class);
if (serviceImplementation.name().equals(name)) {
if (classReference.get() != null) {
logger.atWarn().log("Multiple classes implementing service found in {} " + "for component {}. Using the first one found: {}", pluginJar, name, classReference.get());
return;
}
classReference.set(c);
}
}));
clazz = classReference.get();
} catch (Throwable e) {
throw new ServiceLoadException(String.format("Unable to load %s as a plugin", name), e);
}
if (clazz == null) {
throw new ServiceLoadException(String.format("Unable to find %s. Could not find any ImplementsService annotation with the same name.", name));
}
return clazz;
}
use of com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC in project aws-greengrass-nucleus by aws-greengrass.
the class AuthorizationPolicyParser method parseAllAuthorizationPolicies.
/**
* Given a kernel object, construct and return a map of AuthorizationPolicy objects that may exist,
* grouped into lists of the same destination component.
* This is used only upon kernel startup, to initialize all policies.
* Never returns null.
*
* @param kernel Kernel
* @return {@Map} of {@String} keys and {@List} of {@AuthorizationPolicy}'s as values"
*/
public Map<String, List<AuthorizationPolicy>> parseAllAuthorizationPolicies(Kernel kernel) {
Map<String, List<AuthorizationPolicy>> primaryAuthorizationPolicyMap = new HashMap<>();
Topics allServices = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC);
if (allServices == null) {
logger.atWarn("load-authorization-all-services-component-config-retrieval-error").log("Unable to retrieve services config");
return primaryAuthorizationPolicyMap;
}
// For each component
for (Node service : allServices) {
if (service == null) {
continue;
}
if (!(service instanceof Topics)) {
continue;
}
Topics serviceConfig = (Topics) service;
String componentName = Kernel.findServiceForNode(serviceConfig);
Node accessControlMapTopic = serviceConfig.findNode(CONFIGURATION_CONFIG_KEY, ACCESS_CONTROL_NAMESPACE_TOPIC);
if (accessControlMapTopic == null) {
continue;
}
// Retrieve all policies, mapped to each policy type
Map<String, List<AuthorizationPolicy>> componentAuthorizationPolicyMap = parseAllPoliciesForComponent(accessControlMapTopic, componentName);
// For each policy type (e.g. aws.greengrass.ipc.pubsub)
for (Map.Entry<String, List<AuthorizationPolicy>> policyTypeList : componentAuthorizationPolicyMap.entrySet()) {
String policyType = policyTypeList.getKey();
List<AuthorizationPolicy> policyList = policyTypeList.getValue();
// If multiple components have policies for the same policy type
primaryAuthorizationPolicyMap.computeIfAbsent(policyType, k -> new ArrayList<>()).addAll(policyList);
}
}
return primaryAuthorizationPolicyMap;
}
Aggregations