/** * Helper to obtain the list of known resources * @return Set of resources including the platform */ private Set<Resource> getResources() { Set<Resource> resources = new HashSet<Resource>(); Stack<Resource> stack = new Stack<Resource>(); stack.push(platform); while (!stack.isEmpty()) { Resource r = stack.pop(); resources.add(r); stack.addAll(r.getChildResources()); } return resources; }
private int countResourceHierarchy(Resource r) { int count = 1; for (Resource child : r.getChildResources()) { count += countResourceHierarchy(child); } return count; } }
/** * Returns the set of child Resources defined by the given parent Resource, which are accepted by the specified * filter. If the filter is null, all of the parent Resource's children will be returned. * * @param parentResource the parent Resource * @param filter the filter; may be null * * @return the set of MeasurementDefinition defined by the given Resource type, which are accepted by the specified * filter, or, if the filter is null, all of the type's MeasurementDefinitions */ @NotNull public static Set<Resource> getChildResources(Resource parentResource, ResourceFilter filter) { if (filter == null) { return parentResource.getChildResources(); } Set<Resource> acceptedChildResources = new LinkedHashSet<Resource>(); for (Resource childResource : parentResource.getChildResources()) { if (filter.accept(childResource)) { acceptedChildResources.add(childResource); } } return acceptedChildResources; }
private Set<ResourceType> getResourceTypes(Set<Resource> resources) { Set<ResourceType> types = new HashSet<ResourceType>(); for (Resource resource : resources) { types.add(resource.getResourceType()); types.addAll(getResourceTypes(resource.getChildResources())); } return types; }
private void practiceSafeSets(final Resource resource) { Set<Resource> children = resource.getChildResources(); if (null == children) { resource.setChildResources(new CopyOnWriteArraySet<Resource>()); } else if (!(children instanceof CopyOnWriteArraySet)) { if (log.isDebugEnabled()) { log.debug("Converting persisted childResources to CopyOnWriteArraySet from [" + children.getClass().getSimpleName() + "] for [" + resource.getName() + "]"); } resource.setChildResources(new CopyOnWriteArraySet<Resource>(children)); } }
/** * Recursively set the agent on the resource tree. * * @param resource pojo, the parent * @param agent pojo, the agent */ private void presetAgent(Resource resource, Agent agent) { resource.setAgent(agent); for (Resource child : resource.getChildResources()) { presetAgent(child, agent); } }
/** * Request an ASAP avail check for the provided resource. If recursive, do the same for the children. * * @param resource * @param recursive */ public void requestAvailabilityCheck(Resource resource, boolean recursive) { if (null == resource) { return; } ResourceContainer resourceContainer = getResourceContainer(resource); if (null != resourceContainer) { // set the avail schedule time to 1 to ensure the resource will have an avail check performed on // the next availability scan. (note, do not set to 0, that has special semantics that will actually // push the avail check out one cycle. resourceContainer.setAvailabilityScheduleTime(1L); } if (recursive && null != resource.getChildResources()) { for (Resource child : resource.getChildResources()) { requestAvailabilityCheck(child, true); } } }
private List<Resource> treeToBreadthFirstList(Resource resource) { // if we are to ignore this resource's type, don't bother doing anything since all is to be ignored if (resource.getResourceType().isIgnored()) { return new ArrayList<Resource>(0); } List<Resource> result = new ArrayList<Resource>(MERGE_BATCH_SIZE); LinkedList<Resource> queue = new LinkedList<Resource>(); queue.add(resource); while (!queue.isEmpty()) { Resource node = queue.remove(); // if this node is to be ignored, don't traverse it and don't add it to the returned results if (node.getResourceType().isIgnored()) { continue; } result.add(node); for (Resource child : node.getChildResources()) { queue.add(child); } } return result; }
/** * Print the direct child resources of a given resource * @param tokens tokenized command line tokens[0] is the command itself */ private void children(String[] tokens) { int id = getResourceIdFromTokens(tokens,resourceId); ResourceContainer resourceContainer = inventoryManager.getResourceContainer(id); if (resourceContainer != null) { Resource r = resourceContainer.getResource(); Set<Resource> resources = r.getChildResources(); for (Resource res : resources) { System.out.println(res); } } else { System.err.println("There is no resource with id " + id); } }
public static Resource getChildResource(Resource parent, ResourceType type, String key) { for (Resource resource : parent.getChildResources()) { if (resource.getResourceType().equals(type) && resource.getResourceKey().equals(key)) { return resource; } } return null; }
private Resource findMatchingChildResource(Resource resource, Resource parent) { if (resource != null) { if (parent == null) { // Resource must be a platform - see if it matches our local platform if (this.platform != null && matches(resource, this.platform)) { return this.platform; } } else { // don't use container children here, the caller is providing the desired resources for (Resource child : parent.getChildResources()) { if (child != null && matches(resource, child)) { return child; } } } } return null; }
public static PlatformSyncInfo buildPlatformSyncInfo(Resource platform) { Set<Integer> toplevelServerIds = new HashSet<Integer>(); for (Resource r : platform.getChildResources()) { if (r.getResourceType().getCategory().equals(ResourceCategory.SERVER)) { toplevelServerIds.add(r.getId()); } } ResourceSyncInfo resSyncInfo = ResourceSyncInfo.buildResourceSyncInfo(platform); PlatformSyncInfo syncInfo = new PlatformSyncInfo(resSyncInfo, new HashSet<ResourceSyncInfo>(1), toplevelServerIds); return syncInfo; } }
private void updateAncestry(Resource resource) { resource.updateAncestryForResource(); for (Resource child : resource.getChildResources()) { child.setParentResource(resource); updateAncestry(child); } }
private void removeStaleResources(Resource parent, ResourceType childResourceType, Map<String, Resource> mergedResources) { Set<Resource> existingChildResources = parent.getChildResources(); for (Resource existingChildResource : existingChildResources) { // NOTE: If inside Agent, only remove Resources w/ id == 0. Other Resources may still exist in the // the Server's inventory. if (existingChildResource.getResourceType().equals(childResourceType) && !mergedResources.containsKey(existingChildResource.getUuid()) && (existingChildResource.getId() == 0 || !this.pluginContainerConfiguration.isInsideAgent())) { log.info("Removing stale resource [" + existingChildResource + "]"); this.inventoryManager.removeResourceAndIndicateIfScanIsNeeded(existingChildResource, true); } } } }
private Set<Resource> findDuplicitSiblingResources(Resource resource, ResourceUpgradeReport upgradeReport) { Resource parent = resource.getParentResource(); if (parent == null) { //there is only a single platform resource on an agent return Collections.emptySet(); } Set<Resource> ret = new HashSet<Resource>(); for (Resource sibling : parent.getChildResources()) { //we'd have a resource key conflict if there was a resource //of the same type under the same parent that would have the same //resource key. if (upgradeReport.getNewResourceKey() != null && sibling.getResourceType().equals(resource.getResourceType()) && !sibling.getUuid().equals(resource.getUuid())) { if (sibling.getResourceKey().equals(upgradeReport.getNewResourceKey())) { ret.add(sibling); } } } return ret; }
@Override public PlatformSyncInfo getPlatformSyncInfo(Agent knownAgent) { Resource platform = resourceManager.getPlatform(knownAgent); if (null == platform) { return null; } Set<Resource> toplevelServices = new HashSet<Resource>(); Set<Integer> topLevelServerIds = new HashSet<Integer>(); for (Resource platformChild : platform.getChildResources()) { switch (platformChild.getResourceType().getCategory()) { case SERVER: topLevelServerIds.add(platformChild.getId()); break; case SERVICE: toplevelServices.add(platformChild); break; default: break; } } ResourceSyncInfo platformSyncInfo = ResourceSyncInfo.buildResourceSyncInfo(platform); Set<ResourceSyncInfo> topLevelServiceSyncInfo = getToplevelServiceSyncInfo(toplevelServices); PlatformSyncInfo result = new PlatformSyncInfo(platformSyncInfo, topLevelServiceSyncInfo, topLevelServerIds); return result; }
private Resource getDiscoveredResource() { ResourceContainer parentResourceContainer = inventoryManager.getResourceContainer(parentResourceId); Resource parentResource = parentResourceContainer.getResource(); for (Resource childResource : parentResource.getChildResources()) { if (childResource.getResourceType().equals(report.getResourceType()) && childResource.getResourceKey().equals(report.getResourceKey())) { return childResource; } } return null; }
private void prefetchResource(Resource resource, boolean recursive) { if (resource == null) { return; // Nothing to do on invalid input } resource.getId(); resource.getPluginConfiguration().getNotes(); // Initialize the lazy plugin config // Init the lazy parent... // Don't fetch the parent's children, otherwise we'll end up in infinite recursion. prefetchResource(resource.getParentResource(), false); if (recursive) { // Recurse... for (Resource child : resource.getChildResources()) { prefetchResource(child, true); } } }
/** * @param resource This can be a detached object * @throws InvalidInventoryReportException */ private void validateResource(Resource resource) throws InvalidInventoryReportException { if (resource.getResourceType() == null) { throw new InvalidInventoryReportException("Reported resource [" + resource + "] has a null type."); } if (resource.getResourceKey() == null) { throw new InvalidInventoryReportException("Reported resource [" + resource + "] has a null key."); } if (resource.getInventoryStatus() == InventoryStatus.DELETED) { throw new InvalidInventoryReportException( "Reported resource [" + resource + "] has an illegal inventory status of 'DELETED' - agents are not allowed to delete platforms from inventory."); } // Recursively validate all the resource's descendants. for (Resource childResource : resource.getChildResources()) { validateResource(childResource); } }
private static Resource convertToPojoResource(Resource resource, boolean includeDescendants) { Resource pojoResource = new Resource(resource.getId()); pojoResource.setUuid(resource.getUuid()); pojoResource.setResourceKey(resource.getResourceKey()); pojoResource.setResourceType(resource.getResourceType()); pojoResource.setMtime(resource.getMtime()); pojoResource.setInventoryStatus(resource.getInventoryStatus()); Configuration pcCopy = resource.getPluginConfiguration(); if (pcCopy != null) { pcCopy = pcCopy.deepCopy(); } pojoResource.setPluginConfiguration(pcCopy); pojoResource.setName(resource.getName()); pojoResource.setDescription(resource.getDescription()); pojoResource.setLocation(resource.getLocation()); pojoResource.setVersion(resource.getVersion()); if (resource.getParentResource() != null) { pojoResource.setParentResource(convertToPojoResource(resource.getParentResource(), false)); } if (includeDescendants) { for (Resource childResource : resource.getChildResources()) { if (isVisibleInInventory(childResource)) { pojoResource.addChildResource(convertToPojoResource(childResource, true)); } } } return pojoResource; }