Search in sources :

Example 56 with OnmsResource

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface)

Example 57 with OnmsResource

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);
}
Also used : LazySet(org.opennms.core.collections.LazySet) ResourcePath(org.opennms.netmgt.model.ResourcePath) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute)

Example 58 with OnmsResource

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;
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsNode(org.opennms.netmgt.model.OnmsNode) ResourcePath(org.opennms.netmgt.model.ResourcePath) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException)

Example 59 with OnmsResource

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;
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource)

Example 60 with OnmsResource

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());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) File(java.io.File) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

OnmsResource (org.opennms.netmgt.model.OnmsResource)143 Test (org.junit.Test)54 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)40 MockResourceType (org.opennms.netmgt.mock.MockResourceType)31 OnmsNode (org.opennms.netmgt.model.OnmsNode)28 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)23 File (java.io.File)22 ResourcePath (org.opennms.netmgt.model.ResourcePath)19 ArrayList (java.util.ArrayList)14 HashSet (java.util.HashSet)14 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)12 HashMap (java.util.HashMap)11 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)11 ResourceId (org.opennms.netmgt.model.ResourceId)11 Date (java.util.Date)10 Source (org.opennms.netmgt.measurements.model.Source)8 LinkedList (java.util.LinkedList)7 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)7 ExternalValueAttribute (org.opennms.netmgt.model.ExternalValueAttribute)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7