use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class IPCAuthorizationTest method GIVEN_authorizationClient_WHEN_valid_ondemand_lambda_token_provided_THEN_succeeds.
@Test
void GIVEN_authorizationClient_WHEN_valid_ondemand_lambda_token_provided_THEN_succeeds() {
GreengrassService mockService = mock(GreengrassService.class);
when(mockService.getServiceName()).thenReturn("ABCService");
// Pretend to be instance #1 of a lambda
lenient().when(mockService.getName()).thenReturn("ABCService#1");
when(mockService.getServiceConfig()).thenReturn(kernel.findServiceTopic("ServiceName"));
when(mockService.getPrivateConfig()).thenReturn(kernel.findServiceTopic("ServiceName").lookupTopics(PRIVATE_STORE_NAMESPACE_TOPIC));
registerAuthenticationToken(mockService);
Topics authTokensArray = kernel.findServiceTopic(AUTHENTICATION_TOKEN_LOOKUP_KEY);
boolean found = false;
for (Node node : authTokensArray) {
if ("ABCService".equals(Coerce.toString(node))) {
if (found) {
fail("Duplicate entry!");
}
found = true;
}
}
assertTrue(found);
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentService method getAllGroupNames.
/**
* Gets the list of all the groups that the thing is a part of. This method is used by log manager.
*
* @return All the group configs.
*/
public Set<String> getAllGroupNames() {
Topics componentsToGroupsTopics = config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS);
Set<String> allGroupNames = new HashSet<>();
if (componentsToGroupsTopics != null) {
componentsToGroupsTopics.iterator().forEachRemaining(node -> {
Topics groupsTopics = (Topics) node;
groupsTopics.children.values().stream().map(n -> (Topic) n).forEach(topic -> {
String groupName = Coerce.toString(topic);
if (!Utils.isEmpty(groupName)) {
allGroupNames.add(groupName);
}
});
});
}
return allGroupNames;
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentService method setComponentsToGroupsMapping.
void setComponentsToGroupsMapping(Topics groupsToRootComponents) {
Set<String> pendingComponents = new HashSet<>();
Map<String, Object> componentsToGroupsMappingCache = new ConcurrentHashMap<>();
Topics componentsToGroupsTopics = getConfig().lookupTopics(COMPONENTS_TO_GROUPS_TOPICS);
/*
* Structure of COMPONENTS_TO_GROUPS_TOPICS is:
* COMPONENTS_TO_GROUPS_TOPICS :
* |_ <componentName> :
* |_ <deploymentID> : <GroupName>
* This stores all the components with the list of deployment IDs associated to it along with the thing group
* (if available) to be associated to the deployment.
*/
// Get all the groups associated to the root components.
groupsToRootComponents.forEach(groupNode -> ((Topics) groupNode).forEach(componentNode -> {
Topics componentTopics = (Topics) componentNode;
Topic groupConfigTopic = componentTopics.lookup(GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN);
String groupConfig = Coerce.toString(groupConfigTopic);
Topic groupNameTopic = componentTopics.lookup(GROUP_TO_ROOT_COMPONENTS_GROUP_NAME);
String groupName = Coerce.toString(groupNameTopic);
Map<String, Object> groupDeploymentIdSet = (Map<String, Object>) componentsToGroupsMappingCache.getOrDefault(componentTopics.getName(), new HashMap<>());
groupDeploymentIdSet.putIfAbsent(groupConfig, groupName);
componentsToGroupsMappingCache.put(componentTopics.getName(), groupDeploymentIdSet);
pendingComponents.add(componentTopics.getName());
}));
// Associate the groups to the dependant services based on the services it is depending on.
while (!pendingComponents.isEmpty()) {
String componentName = pendingComponents.iterator().next();
try {
GreengrassService greengrassService = kernel.locate(componentName);
Map<String, Object> groupNamesForComponent = (Map<String, Object>) componentsToGroupsMappingCache.getOrDefault(greengrassService.getName(), new HashMap<>());
greengrassService.getDependencies().forEach((greengrassService1, dependencyType) -> {
pendingComponents.add(greengrassService1.getName());
Map<String, Object> groupNamesForDependentComponent = (Map<String, Object>) componentsToGroupsMappingCache.getOrDefault(greengrassService1.getName(), new HashMap<>());
groupNamesForDependentComponent.putAll(groupNamesForComponent);
componentsToGroupsMappingCache.put(greengrassService1.getName(), groupNamesForDependentComponent);
});
} catch (ServiceLoadException ex) {
logger.atError().cause(ex).log("Unable to get status for {}.", componentName);
}
pendingComponents.remove(componentName);
}
if (componentsToGroupsTopics != null) {
componentsToGroupsTopics.replaceAndWait(componentsToGroupsMappingCache);
}
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentStatusKeeper method persistAndPublishDeploymentStatus.
/**
* Persist deployment status in kernel config.
*
* @param deploymentId id for the deployment.
* @param deploymentType type of deployment.
* @param status status of deployment.
* @param statusDetails other details of deployment status.
* @throws IllegalArgumentException for invalid deployment type
*/
public void persistAndPublishDeploymentStatus(String deploymentId, DeploymentType deploymentType, String status, Map<String, String> statusDetails) {
// method which consumes the data in config from the same topics. These two thread needs to be synchronized
synchronized (deploymentType) {
logger.atDebug().kv(DEPLOYMENT_ID_KEY_NAME, deploymentId).kv(DEPLOYMENT_STATUS_KEY_NAME, status).log("Storing deployment status");
Map<String, Object> deploymentDetails = new HashMap<>();
deploymentDetails.put(DEPLOYMENT_ID_KEY_NAME, deploymentId);
deploymentDetails.put(DEPLOYMENT_TYPE_KEY_NAME, deploymentType.toString());
deploymentDetails.put(DEPLOYMENT_STATUS_KEY_NAME, status);
deploymentDetails.put(DEPLOYMENT_STATUS_DETAILS_KEY_NAME, statusDetails);
// Each status update is uniquely stored
Topics processedDeployments = getProcessedDeployments();
Topics thisJob = processedDeployments.createInteriorChild(String.valueOf(System.currentTimeMillis()));
thisJob.replaceAndWait(deploymentDetails);
logger.atInfo().kv(DEPLOYMENT_ID_KEY_NAME, deploymentId).kv(DEPLOYMENT_STATUS_KEY_NAME, status).log("Stored deployment status");
}
publishPersistedStatusUpdates(deploymentType);
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentStatusKeeper method publishPersistedStatusUpdates.
/**
* Invokes the call-backs with persisted deployment status updates for deployments with specified type.
* This is called by IotJobsHelper/MqttJobsHelper when connection is re-established to update cloud of all
* all deployments the device performed when offline
*
* @param type deployment type
*/
public void publishPersistedStatusUpdates(DeploymentType type) {
synchronized (type) {
Topics processedDeployments = getProcessedDeployments();
ArrayList<Topics> deployments = new ArrayList<>();
processedDeployments.forEach(node -> {
Topics deploymentDetails = (Topics) node;
DeploymentType deploymentType = Coerce.toEnum(DeploymentType.class, deploymentDetails.find(DEPLOYMENT_TYPE_KEY_NAME));
if (Objects.equals(deploymentType, type)) {
deployments.add(deploymentDetails);
}
});
// Topics are stored as ConcurrentHashMaps which do not guarantee ordering of elements
// We want the statuses to be updated in the cloud in the order in which they were processed on the device.
// This will be accurate representation of what happened on the device, especially when deployment service
// processes multiple deployments in the order in which they come. Additionally, a customer workflow can
// depend on this order. If Group2 gets successfully updated before Group1 then customer workflow may
// error out.
List<Topics> sortedByTimestamp = deployments.stream().sorted((o1, o2) -> {
if (o1.getModtime() > o2.getModtime()) {
return 1;
}
return -1;
}).collect(Collectors.toList());
List<Function<Map<String, Object>, Boolean>> consumers = getConsumersForDeploymentType(type);
logger.atDebug().kv("deploymentType", type).kv("numberOfSubscribers", consumers.size()).log("Updating status of persisted deployments to subscribers");
for (Topics topics : sortedByTimestamp) {
boolean allConsumersUpdated = consumers.stream().allMatch(consumer -> consumer.apply(topics.toPOJO()));
if (!allConsumersUpdated) {
// If one deployment update fails, exit the loop to ensure the update order.
logger.atDebug().log("Unable to update status of persisted deployments. Retry later");
break;
}
processedDeployments.remove(topics);
}
}
}
Aggregations