use of org.opennms.netmgt.collection.api.CollectionResource in project opennms by OpenNMS.
the class CollectCommand method printCollectionSet.
private static void printCollectionSet(CollectionSet collectionSet) {
AtomicBoolean didPrintAttribute = new AtomicBoolean(false);
collectionSet.visit(new AbstractCollectionSetVisitor() {
@Override
public void visitResource(CollectionResource resource) {
System.out.printf("%s\n", resource);
}
@Override
public void visitGroup(AttributeGroup group) {
System.out.printf("\tGroup: %s\n", group.getName());
}
@Override
public void visitAttribute(CollectionAttribute attribute) {
System.out.printf("\t\t%s\n", attribute);
didPrintAttribute.set(true);
}
});
if (!didPrintAttribute.get()) {
System.out.println("(Empty collection set)");
}
}
use of org.opennms.netmgt.collection.api.CollectionResource in project opennms by OpenNMS.
the class ObjectNameStorageStrategyTest method testGetResourceNameFromIndex2.
@Test
public void testGetResourceNameFromIndex2() {
ResourcePath parentResource = ResourcePath.get("1");
CollectionResource resource = new MockCollectionResource(parentResource, "java.lang:type=MemoryPool,name=Survivor Space", "");
List<org.opennms.netmgt.collection.api.Parameter> params = new ArrayList<>();
Parameter p = new Parameter("index-format", "${domain}");
params.add(p);
ObjectNameStorageStrategy instance = new ObjectNameStorageStrategy();
instance.setParameters(params);
String expResult = "java.lang";
String result = instance.getResourceNameFromIndex(resource);
assertEquals(expResult, result);
params.clear();
p.setValue("${type}");
params.add(p);
instance.setParameters(params);
expResult = "MemoryPool";
result = instance.getResourceNameFromIndex(resource);
assertEquals(expResult, result);
params.clear();
p.setValue("${name}");
params.add(p);
instance.setParameters(params);
expResult = "Survivor Space";
result = instance.getResourceNameFromIndex(resource);
assertEquals(expResult, result);
params.clear();
p.setValue("${domain}:type=${type},name=${name}");
params.add(p);
instance.setParameters(params);
expResult = "java.lang:type=MemoryPool,name=Survivor Space";
result = instance.getResourceNameFromIndex(resource);
assertEquals(expResult, result);
}
use of org.opennms.netmgt.collection.api.CollectionResource in project opennms by OpenNMS.
the class SnmpCollectionSet method visit.
/**
* {@inheritDoc}
*/
@Override
public void visit(CollectionSetVisitor visitor) {
visitor.visitCollectionSet(this);
for (CollectionResource resource : getResources()) {
resource.visit(visitor);
}
visitor.completeCollectionSet(this);
}
use of org.opennms.netmgt.collection.api.CollectionResource in project opennms by OpenNMS.
the class AbstractXmlCollectionHandler method fillCollectionSet.
/**
* Fill collection set.
*
* @param agent the agent
* @param collectionSet the collection set
* @param source the source
* @param doc the doc
* @throws XPathExpressionException the x path expression exception
* @throws ParseException the parse exception
*/
protected void fillCollectionSet(CollectionAgent agent, CollectionSetBuilder builder, XmlSource source, Document doc) throws XPathExpressionException, ParseException {
NamespaceContext nc = new DocumentNamespaceResolver(doc);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(nc);
for (XmlGroup group : source.getXmlGroups()) {
LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath());
Date timestamp = getTimeStamp(doc, xpath, group);
NodeList resourceList = (NodeList) xpath.evaluate(group.getResourceXpath(), doc, XPathConstants.NODESET);
for (int j = 0; j < resourceList.getLength(); j++) {
Node resource = resourceList.item(j);
String resourceName = getResourceName(xpath, group, resource);
final Resource collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp);
LOG.debug("fillCollectionSet: processing resource {}", collectionResource);
for (XmlObject object : group.getXmlObjects()) {
String value = (String) xpath.evaluate(object.getXpath(), resource, XPathConstants.STRING);
builder.withAttribute(collectionResource, group.getName(), object.getName(), value, object.getDataType());
}
processXmlResource(builder, collectionResource, resourceName, group.getName());
}
}
LOG.debug("fillCollectionSet: finishing collection set with {} resources and {} attributes on {}", builder.getNumResources(), builder.getNumAttributes(), agent);
}
use of org.opennms.netmgt.collection.api.CollectionResource in project opennms by OpenNMS.
the class CollectionSetDTO method buildCollectionResources.
private Set<CollectionResource> buildCollectionResources() {
final Set<CollectionResource> collectionResources = new LinkedHashSet<>();
for (CollectionResourceDTO entry : this.collectionResources) {
final Resource resource = entry.getResource();
final AbstractCollectionResource collectionResource = CollectionSetBuilder.toCollectionResource(resource, agent);
for (Attribute<?> attribute : entry.getAttributes()) {
final AttributeGroupType groupType = new AttributeGroupType(attribute.getGroup(), AttributeGroupType.IF_TYPE_ALL);
final AbstractCollectionAttributeType attributeType = new AbstractCollectionAttributeType(groupType) {
@Override
public AttributeType getType() {
return attribute.getType();
}
@Override
public String getName() {
return attribute.getName();
}
@Override
public void storeAttribute(CollectionAttribute collectionAttribute, Persister persister) {
if (AttributeType.STRING.equals(attribute.getType())) {
persister.persistStringAttribute(collectionAttribute);
} else {
persister.persistNumericAttribute(collectionAttribute);
}
}
@Override
public String toString() {
return attribute.toString();
}
};
collectionResource.addAttribute(new AbstractCollectionAttribute(attributeType, collectionResource) {
@Override
public String getMetricIdentifier() {
return attribute.getName();
}
@Override
public Number getNumericValue() {
return attribute.getNumericValue();
}
@Override
public String getStringValue() {
return attribute.getStringValue();
}
@Override
public boolean shouldPersist(ServiceParameters params) {
return !(Boolean.FALSE.equals(disableCounterPersistence) && AttributeType.COUNTER.equals(attribute.getType()));
}
@Override
public String toString() {
return String.format("Attribute[%s:%s]", getMetricIdentifier(), attribute.getValue());
}
});
}
collectionResources.add(collectionResource);
}
return collectionResources;
}
Aggregations