Search in sources :

Example 61 with OnmsResource

use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.

the class DefaultResourceDaoTest method testFindNodeResourcesWithNodeSnmp.

@Test
public void testFindNodeResourcesWithNodeSnmp() 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 snmp = m_fileAnticipator.tempDir("snmp");
    File nodeDir = m_fileAnticipator.tempDir(snmp, "1");
    m_fileAnticipator.tempFile(nodeDir, "foo" + 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)

Example 62 with OnmsResource

use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.

the class FindTopLevelResourcesTest method execute_testFindTopLevelResources_provisionedNodes.

/*
     * On environments where all the nodes are part of a requisition (i.e. they have been provisioned)
     * the top level resources are always going to be built using NodeSourceResourceType only
     * if storeByForeignSource is enabled, otherwise they are all going to built using NodeResourceType.
     */
private void execute_testFindTopLevelResources_provisionedNodes(boolean storeByForeignSource) throws Exception {
    setStoreByForeignSource(storeByForeignSource);
    final List<OnmsNode> nodes = new ArrayList<>();
    final String foreignSource = "Junit";
    // Node on the DB with RRD Data with Response Time
    OnmsNode n1 = createNode(1, "node1", foreignSource, "node1", "10.0.0.1");
    nodes.add(n1);
    // Node on the DB with RRD Data without Response Time
    OnmsNode n2 = createNode(2, "node2", foreignSource, "node2", "10.0.0.2");
    nodes.add(n2);
    // Node on the DB with No RRD Data or Response Time
    OnmsNode n3 = createNode(3, "node3", foreignSource, "node3", "10.0.0.3");
    nodes.add(n3);
    expect(m_resourceTypesDao.getLastUpdate()).andReturn(new Date(System.currentTimeMillis())).atLeastOnce();
    expect(m_resourceTypesDao.getResourceTypes()).andReturn(new HashMap<String, ResourceType>());
    expect(m_nodeDao.findAll()).andReturn(nodes);
    expect(m_locationMonitorDao.findStatusChangesForNodeForUniqueMonitorAndInterface(n1.getId())).andReturn(new ArrayList<LocationMonitorIpInterface>(0));
    expect(m_locationMonitorDao.findStatusChangesForNodeForUniqueMonitorAndInterface(n2.getId())).andReturn(new ArrayList<LocationMonitorIpInterface>(0));
    expect(m_locationMonitorDao.findStatusChangesForNodeForUniqueMonitorAndInterface(n3.getId())).andReturn(new ArrayList<LocationMonitorIpInterface>(0));
    // Common directories
    File snmpDir = m_fileAnticipator.tempDir(ResourceTypeUtils.SNMP_DIRECTORY);
    File responseDir = m_fileAnticipator.tempDir(ResourceTypeUtils.RESPONSE_DIRECTORY);
    File fsDir = m_fileAnticipator.tempDir(snmpDir, ResourceTypeUtils.FOREIGN_SOURCE_DIRECTORY);
    File foreignSourceDir = m_fileAnticipator.tempDir(fsDir, foreignSource);
    // RRD Directory for n1
    File nodeDir = null;
    if (storeByForeignSource) {
        nodeDir = m_fileAnticipator.tempDir(foreignSourceDir, n1.getForeignId());
    } else {
        nodeDir = m_fileAnticipator.tempDir(snmpDir, n1.getId().toString());
    }
    m_fileAnticipator.tempFile(nodeDir, "data" + m_rrdFileExtension);
    // RRD Directory for n2
    if (storeByForeignSource) {
        nodeDir = m_fileAnticipator.tempDir(foreignSourceDir, n2.getForeignId());
    } else {
        nodeDir = m_fileAnticipator.tempDir(snmpDir, n2.getId().toString());
    }
    m_fileAnticipator.tempFile(nodeDir, "data" + m_rrdFileExtension);
    // RRD Directory for an orphan node
    if (storeByForeignSource) {
        nodeDir = m_fileAnticipator.tempDir(foreignSourceDir, "orphan_node");
    } else {
        nodeDir = m_fileAnticipator.tempDir(snmpDir, "100");
    }
    m_fileAnticipator.tempFile(nodeDir, "data" + m_rrdFileExtension);
    // Response Time RRD Directory for n1
    File ipDir = m_fileAnticipator.tempDir(responseDir, n1.getIpInterfaces().iterator().next().getIpAddress().getHostAddress());
    m_fileAnticipator.tempFile(ipDir, "icmp" + m_rrdFileExtension);
    walkin(m_fileAnticipator.getTempDir());
    m_easyMockUtils.replayAll();
    m_resourceDao.afterPropertiesSet();
    List<OnmsResource> resources = m_resourceDao.findTopLevelResources();
    Assert.assertNotNull(resources);
    Collections.sort(resources);
    Assert.assertEquals(2, resources.size());
    // Node 1
    List<OnmsResource> children = resources.get(0).getChildResources();
    Collections.sort(children);
    Assert.assertEquals(2, children.size());
    Assert.assertEquals("node[Junit:node1].responseTime[10.0.0.1]", children.get(0).getId().toString());
    Assert.assertEquals("node[Junit:node1].nodeSnmp[]", children.get(1).getId().toString());
    // Node 2
    children = resources.get(1).getChildResources();
    Collections.sort(children);
    Assert.assertEquals(1, children.size());
    Assert.assertEquals("node[Junit:node2].nodeSnmp[]", children.get(0).getId().toString());
    m_easyMockUtils.verifyAll();
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsResource(org.opennms.netmgt.model.OnmsResource) ArrayList(java.util.ArrayList) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) LocationMonitorIpInterface(org.opennms.netmgt.model.LocationMonitorIpInterface) File(java.io.File) Date(java.util.Date)

Example 63 with OnmsResource

use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.

the class GenericIndexResourceTypeTest method testGetResourceByNodeAndIndexGetLabelIndexWithSubStringAndHexConversion.

/**
 * Test for enhancement in bug #2467.
 */
@Test
public void testGetResourceByNodeAndIndexGetLabelIndexWithSubStringAndHexConversion() {
    GenericIndexResourceType rt = new GenericIndexResourceType(m_resourceStorageDao, "foo", "Foo Resource", "MAC Address ${hex(subIndex(0, 6))} on interface ${subIndex(6, 1)}", m_storageStrategy);
    touch("snmp", "1", "foo", "0.21.109.80.9.66.4", RRD_FILE_NAME);
    m_mocks.replayAll();
    OnmsResource resource = rt.getChildByName(getNodeResource(1), "0.21.109.80.9.66.4");
    m_mocks.verifyAll();
    assertNotNull("resource", resource);
    assertEquals("resource label", "MAC Address 00:15:6D:50:09:42 on interface 4", resource.getLabel());
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) Test(org.junit.Test)

Example 64 with OnmsResource

use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.

the class ResourceDaoIntegrityIT method walkResourceTree.

@Test
@Transactional
public void walkResourceTree() throws IOException {
    // Setup the file tree and the necessary objects in the DAOs
    createResourceTree();
    createNodes();
    Map<String, ResourceType> types = createResourceTypes();
    expect(m_resourceTypesDao.getLastUpdate()).andReturn(new Date(System.currentTimeMillis())).anyTimes();
    expect(m_resourceTypesDao.getResourceTypes()).andReturn(types).anyTimes();
    m_easyMockUtils.replayAll();
    m_resourceDao.afterPropertiesSet();
    // Walk the tree and collect the results
    ResourceCollector visitor = new ResourceCollector();
    ResourceTreeWalker walker = new ResourceTreeWalker();
    walker.setResourceDao(m_resourceDao);
    walker.setVisitor(visitor);
    walker.walk();
    // We must have at least one resource for every known type
    for (OnmsResourceType type : m_resourceDao.getResourceTypes()) {
        // Ignore this type for now #needstoomanydbojects
        if (DistributedStatusResourceType.TYPE_NAME.equals(type.getName())) {
            continue;
        }
        // and should never be returned when enumerating resources
        if (InterfaceSnmpByIfIndexResourceType.TYPE_NAME.equals(type.getName())) {
            continue;
        }
        assertTrue("No resources of type: " + type.getLabel(), visitor.resourceTypes.contains(type));
    }
    // We must be able to retrieve the same resource by id
    for (Entry<ResourceId, OnmsResource> entry : visitor.resourcesById.entrySet()) {
        OnmsResource resourceRetrievedById = m_resourceDao.getResourceById(entry.getKey());
        assertNotNull(String.format("Failed to retrieve resource with id '%s'.", entry.getKey()), resourceRetrievedById);
        assertEquals(String.format("Result mismatch for resource with id '%s'. Retrieved id is '%s'.", entry.getKey(), resourceRetrievedById.getId()), entry.getValue().getName(), resourceRetrievedById.getName());
    }
    // Build a line that represent the resource for every unique id
    // and compare it to the known results
    int k = 0;
    String[] expectedResults = loadExpectedResults();
    for (Entry<ResourceId, OnmsResource> entry : visitor.resourcesById.entrySet()) {
        // Convert the attributes to strings and order them lexicographically
        Set<String> attributeNames = new TreeSet<>();
        for (OnmsAttribute attribute : entry.getValue().getAttributes()) {
            attributeNames.add(attribute.toString());
        }
        // Compare
        String actualResult = entry.getKey() + ": " + attributeNames;
        assertEquals(String.format("Result mismatch at line %d.", k + 1), expectedResults[k], actualResult);
        k++;
    }
    // We should have as many unique resource ids as we have results
    assertEquals(expectedResults.length, visitor.resourcesById.size());
    m_easyMockUtils.verifyAll();
}
Also used : ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) OnmsResourceType(org.opennms.netmgt.model.OnmsResourceType) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) Date(java.util.Date) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsResourceType(org.opennms.netmgt.model.OnmsResourceType) ResourceId(org.opennms.netmgt.model.ResourceId) TreeSet(java.util.TreeSet) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 65 with OnmsResource

use of org.opennms.netmgt.model.OnmsResource in project opennms by OpenNMS.

the class ReportDefinitionTest method testFilteredResourceAttributeFilteringWithMatch.

public void testFilteredResourceAttributeFilteringWithMatch() throws Exception {
    OnmsAttribute rrdAttribute = new RrdGraphAttribute("IfInOctets", "something", "something else");
    ExternalValueAttribute externalValueAttribute = new ExternalValueAttribute("ifSpeed", "100000000");
    Set<OnmsAttribute> attributes = new HashSet<>();
    attributes.add(rrdAttribute);
    attributes.add(externalValueAttribute);
    final OnmsNode node = new OnmsNode();
    node.setId(1);
    node.setLabel("Node One");
    EasyMock.expect(m_nodeDao.load(1)).andReturn(node);
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interfaceSnmp");
    OnmsResource resource = new OnmsResource(node.getId().toString(), node.getLabel(), resourceType, attributes, ResourcePath.get("foo"));
    ReportDefinition def = createReportDefinition();
    def.getReport().getPackage().setFilter("");
    def.setResourceAttributeKey(externalValueAttribute.getName());
    def.setResourceAttributeValueMatch(externalValueAttribute.getValue());
    ReportInstance report = def.createReport(m_nodeDao, m_resourceDao, m_fetchStrategy, m_filterDao);
    SortedMap<Integer, String> sortedNodeMap = new TreeMap<Integer, String>();
    sortedNodeMap.put(node.getId(), node.getLabel());
    EasyMock.expect(m_filterDao.getNodeMap("")).andReturn(sortedNodeMap);
    EasyMock.expect(m_resourceDao.getResourceForNode(node)).andReturn(resource);
    Source source = new Source();
    source.setLabel("result");
    source.setResourceId(resource.getId().toString());
    source.setAttribute(rrdAttribute.getName());
    source.setAggregation("AVERAGE");
    FetchResults results = new FetchResults(new long[] { report.getStartTime() }, Collections.singletonMap("result", new double[] { 100.0 }), report.getEndTime() - report.getStartTime(), Collections.emptyMap());
    EasyMock.expect(m_fetchStrategy.fetch(report.getStartTime(), report.getEndTime(), 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
    m_mocks.replayAll();
    report.walk();
    assertEquals("results size", 1, report.getResults().size());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) TreeMap(java.util.TreeMap) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Source(org.opennms.netmgt.measurements.model.Source) OnmsResource(org.opennms.netmgt.model.OnmsResource) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute) HashSet(java.util.HashSet) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

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