use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.
the class InterfaceSnmpResourceType method getNodeResources.
private List<OnmsResource> getNodeResources(ResourcePath parent, Set<String> intfNames, OnmsNode node) {
ArrayList<OnmsResource> resources = new ArrayList<>();
Set<OnmsSnmpInterface> snmpInterfaces = node.getSnmpInterfaces();
Map<String, OnmsSnmpInterface> intfMap = new HashMap<String, OnmsSnmpInterface>();
for (OnmsSnmpInterface snmpInterface : snmpInterfaces) {
for (String key : getKeysFor(snmpInterface)) {
if (!intfMap.containsKey(key)) {
intfMap.put(key, snmpInterface);
}
}
}
for (String intfName : intfNames) {
String key = getKeyFor(intfName);
OnmsSnmpInterface snmpInterface = intfMap.get(key);
String label;
Long ifSpeed = null;
String ifSpeedFriendly = null;
if (snmpInterface == null) {
label = intfName + " (*)";
} else {
final StringBuilder descr = new StringBuilder();
final StringBuilder parenString = new StringBuilder();
if (snmpInterface.getIfAlias() != null) {
parenString.append(snmpInterface.getIfAlias());
}
// Append all of the IP addresses on this ifindex
for (OnmsIpInterface ipif : snmpInterface.getIpInterfaces()) {
String ipaddr = InetAddressUtils.str(ipif.getIpAddress());
if (!"0.0.0.0".equals(ipaddr)) {
if (parenString.length() > 0) {
parenString.append(", ");
}
parenString.append(ipaddr);
}
}
if ((snmpInterface.getIfSpeed() != null) && (snmpInterface.getIfSpeed() != 0)) {
ifSpeed = snmpInterface.getIfSpeed();
ifSpeedFriendly = SIUtils.getHumanReadableIfSpeed(ifSpeed);
if (parenString.length() > 0) {
parenString.append(", ");
}
parenString.append(ifSpeedFriendly);
}
if (snmpInterface.getIfName() != null) {
descr.append(snmpInterface.getIfName());
} else if (snmpInterface.getIfDescr() != null) {
descr.append(snmpInterface.getIfDescr());
} else {
/*
* Should never reach this point, since ifLabel is based on
* the values of ifName and ifDescr but better safe than sorry.
*/
descr.append(intfName);
}
/* Add the extended information in parenthesis after the ifLabel,
* if such information was found.
*/
if (parenString.length() > 0) {
descr.append(" (");
descr.append(parenString);
descr.append(")");
}
label = descr.toString();
}
OnmsResource resource = getResourceByParentPathAndInterface(parent, intfName, label, ifSpeed, ifSpeedFriendly);
if (snmpInterface != null) {
Set<OnmsIpInterface> ipInterfaces = snmpInterface.getIpInterfaces();
if (ipInterfaces.size() > 0) {
int id = ipInterfaces.iterator().next().getId();
resource.setLink("element/interface.jsp?ipinterfaceid=" + id);
} else {
int ifIndex = snmpInterface.getIfIndex();
if (ifIndex > -1) {
resource.setLink("element/snmpinterface.jsp?node=" + node.getNodeId() + "&ifindex=" + ifIndex);
}
}
resource.setEntity(snmpInterface);
} else {
LOG.debug("populateResourceList: snmpInterface is null");
}
LOG.debug("populateResourceList: adding resource toString {}", resource.toString());
resources.add(resource);
}
return resources;
}
use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.
the class InterfaceSnmpResourceType method getResourceByParentPathAndInterface.
private OnmsResource getResourceByParentPathAndInterface(ResourcePath parent, String intf) {
final ResourcePath path = ResourcePath.get(parent, intf);
final LazyResourceAttributeLoader loader = new LazyResourceAttributeLoader(m_resourceStorageDao, path);
final Set<OnmsAttribute> set = new LazySet<OnmsAttribute>(loader);
return new OnmsResource(intf, intf, this, set, path);
}
use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.
the class InterfaceSnmpResourceType method getChildByName.
/**
* {@inheritDoc}
*/
@Override
public OnmsResource getChildByName(final OnmsResource parent, final String name) {
if (DomainResourceType.isDomain(parent)) {
// This is not efficient, but resources of this type should be sparse.
for (final OnmsResource resource : getResourcesForParent(parent)) {
if (resource.getName().equals(name)) {
return resource;
}
}
throw new ObjectRetrievalFailureException(OnmsResource.class, "No child with name '" + name + "' found on '" + parent + "'");
}
// Grab the node entity
final OnmsNode node = ResourceTypeUtils.getNodeFromResource(parent);
// Verify that the requested resource exists
final ResourcePath resourcePath = new ResourcePath(parent.getPath(), name);
if (!m_resourceStorageDao.exists(resourcePath, 0)) {
throw new ObjectRetrievalFailureException(OnmsResource.class, "No resource with name '" + name + "' found.");
}
// Leverage the existing function for retrieving the resource list
final List<OnmsResource> resources = getNodeResources(parent.getPath(), Sets.newHashSet(name), node);
if (resources.size() != 1) {
throw new ObjectRetrievalFailureException(OnmsResource.class, "No resource with name '" + name + "' found.");
}
final OnmsResource resource = resources.get(0);
resource.setParent(parent);
return resource;
}
use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.
the class RrdGraphHelper method checkLabelForQuotes.
/**
* Checks a resource label for quotes.
*
* @param childResource the child resource to check
* @return the resource
*/
private OnmsResource checkLabelForQuotes(OnmsResource childResource) {
String lbl = Util.convertToJsSafeString(childResource.getLabel());
OnmsResource resource = new OnmsResource(childResource.getName(), lbl, childResource.getResourceType(), childResource.getAttributes(), childResource.getPath());
resource.setParent(childResource.getParent());
resource.setEntity(childResource.getEntity());
resource.setLink(childResource.getLink());
return resource;
}
use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.
the class DefaultResourceDaoTest method testFindNodeResourcesWithResponseTime.
@Test
public void testFindNodeResourcesWithResponseTime() throws Exception {
List<OnmsNode> nodes = new LinkedList<>();
OnmsNode node = createNode();
OnmsIpInterface ip = createIpInterface();
node.addIpInterface(ip);
nodes.add(node);
expect(m_nodeDao.findAll()).andReturn(nodes);
File response = m_fileAnticipator.tempDir("response");
File ipDir = m_fileAnticipator.tempDir(response, "192.168.1.1");
m_fileAnticipator.tempFile(ipDir, "icmp" + m_rrdFileExtension);
expect(m_resourceTypesDao.getLastUpdate()).andReturn(m_lastUpdateTime);
m_easyMockUtils.replayAll();
List<OnmsResource> resources = m_resourceDao.findTopLevelResources();
m_easyMockUtils.verifyAll();
assertNotNull("resource list should not be null", resources);
assertEquals("resource list size", 1, resources.size());
}
Aggregations