use of org.apache.nifi.web.api.entity.BulletinEntity in project nifi by apache.
the class BulletinMerger method mergeBulletins.
/**
* Merges the bulletins.
*
* @param bulletins bulletins
*/
public static List<BulletinEntity> mergeBulletins(final Map<NodeIdentifier, List<BulletinEntity>> bulletins, final int totalNodes) {
final List<BulletinEntity> bulletinEntities = new ArrayList<>();
for (final Map.Entry<NodeIdentifier, List<BulletinEntity>> entry : bulletins.entrySet()) {
final NodeIdentifier nodeId = entry.getKey();
final List<BulletinEntity> nodeBulletins = entry.getValue();
final String nodeAddress = nodeId.getApiAddress() + ":" + nodeId.getApiPort();
for (final BulletinEntity bulletinEntity : nodeBulletins) {
if (bulletinEntity.getNodeAddress() == null) {
bulletinEntity.setNodeAddress(nodeAddress);
}
if (bulletinEntity.getCanRead() && bulletinEntity.getBulletin() != null && bulletinEntity.getBulletin().getNodeAddress() == null) {
bulletinEntity.getBulletin().setNodeAddress(nodeAddress);
}
bulletinEntities.add(bulletinEntity);
}
}
final List<BulletinEntity> entities = Lists.newArrayList();
// group by message when permissions allow
final Map<String, List<BulletinEntity>> groupingEntities = bulletinEntities.stream().filter(bulletinEntity -> bulletinEntity.getCanRead()).collect(Collectors.groupingBy(b -> b.getBulletin().getMessage()));
// add one from each grouped bulletin when all nodes report the same message
groupingEntities.forEach((message, groupedBulletinEntities) -> {
if (groupedBulletinEntities.size() == totalNodes) {
// get the most current bulletin
final BulletinEntity selectedBulletinEntity = groupedBulletinEntities.stream().max(Comparator.comparingLong(bulletinEntity -> {
if (bulletinEntity.getTimestamp() == null) {
return 0;
} else {
return bulletinEntity.getTimestamp().getTime();
}
})).orElse(null);
// should never be null, but just in case
if (selectedBulletinEntity != null) {
selectedBulletinEntity.setNodeAddress(ALL_NODES_MESSAGE);
selectedBulletinEntity.getBulletin().setNodeAddress(ALL_NODES_MESSAGE);
entities.add(selectedBulletinEntity);
}
} else {
// since all nodes didn't report the exact same bulletin, keep them all
entities.addAll(groupedBulletinEntities);
}
});
// ensure we also get the remainder of the bulletin entities
bulletinEntities.stream().filter(bulletinEntity -> !bulletinEntity.getCanRead()).forEach(entities::add);
// ensure the bulletins are sorted by time
Collections.sort(entities, (BulletinEntity o1, BulletinEntity o2) -> {
final int timeComparison = o1.getTimestamp().compareTo(o2.getTimestamp());
if (timeComparison != 0) {
return timeComparison;
}
return o1.getNodeAddress().compareTo(o2.getNodeAddress());
});
return entities;
}
use of org.apache.nifi.web.api.entity.BulletinEntity in project nifi by apache.
the class ComponentEntityMerger method merge.
/**
* Merges the ComponentEntity responses according to their {@link org.apache.nifi.web.api.dto.PermissionsDTO}s. Responsible for invoking
* {@link ComponentEntityMerger#mergeComponents(EntityType, Map)}.
*
* @param clientEntity the entity being returned to the client
* @param entityMap all node responses
*/
@SuppressWarnings("unchecked")
default void merge(final EntityType clientEntity, final Map<NodeIdentifier, EntityType> entityMap) {
for (final Map.Entry<NodeIdentifier, EntityType> entry : entityMap.entrySet()) {
final EntityType entity = entry.getValue();
PermissionsDtoMerger.mergePermissions(clientEntity.getPermissions(), entity.getPermissions());
}
if (clientEntity.getPermissions().getCanRead()) {
final Map<NodeIdentifier, List<BulletinEntity>> bulletinEntities = new HashMap<>();
for (final Map.Entry<NodeIdentifier, ? extends ComponentEntity> entry : entityMap.entrySet()) {
final NodeIdentifier nodeIdentifier = entry.getKey();
final ComponentEntity entity = entry.getValue();
// consider the bulletins if present and authorized
if (entity.getBulletins() != null) {
entity.getBulletins().forEach(bulletin -> {
bulletinEntities.computeIfAbsent(nodeIdentifier, nodeId -> new ArrayList<>()).add(bulletin);
});
}
}
clientEntity.setBulletins(BulletinMerger.mergeBulletins(bulletinEntities, entityMap.size()));
// sort the results
Collections.sort(clientEntity.getBulletins(), BULLETIN_COMPARATOR);
// prune the response to only include the max number of bulletins
if (clientEntity.getBulletins().size() > MAX_BULLETINS_PER_COMPONENT) {
clientEntity.setBulletins(clientEntity.getBulletins().subList(0, MAX_BULLETINS_PER_COMPONENT));
}
mergeComponents(clientEntity, entityMap);
} else {
clientEntity.setBulletins(null);
// unchecked warning suppressed
clientEntity.setComponent(null);
}
}
use of org.apache.nifi.web.api.entity.BulletinEntity in project nifi by apache.
the class BulletinMergerTest method createBulletin.
private BulletinEntity createBulletin(final String message) {
final BulletinDTO bulletin = new BulletinDTO();
bulletin.setId(bulletinId++);
bulletin.setMessage(message);
bulletin.setTimestamp(new Date());
final BulletinEntity entity = new BulletinEntity();
entity.setId(bulletin.getId());
entity.setTimestamp(bulletin.getTimestamp());
entity.setCanRead(true);
entity.setBulletin(bulletin);
return entity;
}
use of org.apache.nifi.web.api.entity.BulletinEntity in project nifi by apache.
the class BulletinBoardEndpointMerger method mergeResponses.
@Override
protected void mergeResponses(BulletinBoardDTO clientDto, Map<NodeIdentifier, BulletinBoardDTO> dtoMap, Set<NodeResponse> successfulResponses, Set<NodeResponse> problematicResponses) {
final Map<NodeIdentifier, List<BulletinEntity>> bulletinEntities = new HashMap<>();
for (final Map.Entry<NodeIdentifier, BulletinBoardDTO> entry : dtoMap.entrySet()) {
final NodeIdentifier nodeIdentifier = entry.getKey();
final BulletinBoardDTO boardDto = entry.getValue();
boardDto.getBulletins().forEach(bulletin -> {
bulletinEntities.computeIfAbsent(nodeIdentifier, nodeId -> new ArrayList<>()).add(bulletin);
});
}
clientDto.setBulletins(BulletinMerger.mergeBulletins(bulletinEntities, dtoMap.size()));
}
use of org.apache.nifi.web.api.entity.BulletinEntity in project nifi by apache.
the class StandardNiFiServiceFacade method updateProcessGroup.
@Override
public ProcessGroupEntity updateProcessGroup(final Revision revision, final ProcessGroupDTO processGroupDTO) {
final ProcessGroup processGroupNode = processGroupDAO.getProcessGroup(processGroupDTO.getId());
final RevisionUpdate<ProcessGroupDTO> snapshot = updateComponent(revision, processGroupNode, () -> processGroupDAO.updateProcessGroup(processGroupDTO), processGroup -> dtoFactory.createProcessGroupDto(processGroup));
final PermissionsDTO permissions = dtoFactory.createPermissionsDto(processGroupNode);
final RevisionDTO updatedRevision = dtoFactory.createRevisionDTO(snapshot.getLastModification());
final ProcessGroupStatusDTO status = dtoFactory.createConciseProcessGroupStatusDto(controllerFacade.getProcessGroupStatus(processGroupNode.getIdentifier()));
final List<BulletinDTO> bulletins = dtoFactory.createBulletinDtos(bulletinRepository.findBulletinsForSource(processGroupNode.getIdentifier()));
final List<BulletinEntity> bulletinEntities = bulletins.stream().map(bulletin -> entityFactory.createBulletinEntity(bulletin, permissions.getCanRead())).collect(Collectors.toList());
return entityFactory.createProcessGroupEntity(snapshot.getComponent(), updatedRevision, permissions, status, bulletinEntities);
}
Aggregations