Search in sources :

Example 11 with SnmpValueFactory

use of org.opennms.netmgt.snmp.SnmpValueFactory in project opennms by OpenNMS.

the class SnmpValueTest method testSnmpObjId.

@Test
public void testSnmpObjId() {
    for (final SnmpValueFactory factory : m_factories) {
        final String oid = ".1.3.6.1.4.1.2925.4.5.2.1.1";
        final SnmpObjId id = SnmpObjId.get(oid);
        final SnmpValue value = factory.getObjectId(id);
        final String className = factory.getClass().getName();
        assertTrue(className + ": getInetAddress isDisplayable should be true", value.isDisplayable());
        assertEquals(className + ": getObjectId to SnmpObjId should return " + oid, id, value.toSnmpObjId());
        assertEquals(className + ": getObjectId to String should return " + oid, oid, value.toString());
        assertEquals(className + ": getObjectId to DisplayString should return " + oid, oid, value.toDisplayString());
        try {
            value.toInt();
            fail(className + ": getObjectId to int should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toLong();
            fail(className + ": getObjectId to long should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toBigInteger();
            fail(className + ": getObjectId to BigInteger should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toHexString();
            fail(className + ": getObjectId to HexString should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toInetAddress();
            fail(className + ": getObjectId to InetAddress should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
    }
}
Also used : SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) JoeSnmpValueFactory(org.opennms.netmgt.snmp.joesnmp.JoeSnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Test(org.junit.Test)

Example 12 with SnmpValueFactory

use of org.opennms.netmgt.snmp.SnmpValueFactory in project opennms by OpenNMS.

the class SyntaxToEventTest method testProcessSyntaxWithTerminatingNull.

/**
 * We are allowing NULL in the last position of the string in case there
 * are any SNMP agents sending OctetString values in this format.
 *
 * @see SnmpUtils#allBytesDisplayable()
 */
@Test
public void testProcessSyntaxWithTerminatingNull() {
    SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
    assertNotNull(valueFactory);
    byte[] macAddr = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x00 };
    SnmpValue octetString = valueFactory.getOctetString(macAddr);
    Parm parm = SyntaxToEvent.processSyntax("Test", octetString);
    assertEquals("Test", parm.getParmName());
    assertEquals(EventConstants.XML_ENCODING_TEXT, parm.getValue().getEncoding());
    // I'm not sure what is converting the NULL char to a "."...
    assertEquals("UUUUU.", parm.getValue().getContent());
}
Also used : SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Parm(org.opennms.netmgt.xml.event.Parm) Test(org.junit.Test)

Example 13 with SnmpValueFactory

use of org.opennms.netmgt.snmp.SnmpValueFactory in project opennms by OpenNMS.

the class TcaCollectorIT method testCollector.

/**
 * Test collector.
 *
 * @throws Exception the exception
 */
@Test
public void testCollector() throws Exception {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "default");
    // Create Collection Set
    TcaCollector collector = new TcaCollector();
    collector.setConfigDao(m_configDao);
    collector.setResourceStorageDao(m_resourceStorageDao);
    collector.setResourceTypesDao(m_resourceTypesDao);
    collector.setLocationAwareSnmpClient(m_client);
    CollectionSetVisitor persister = m_persisterFactory.createOneToOnePersister(new ServiceParameters(parameters), collector.getRrdRepository("default"), false, false);
    // Setup SNMP Value Handling
    SnmpValueFactory valFac = SnmpUtils.getValueFactory();
    SnmpObjId peer1 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.37.60");
    SnmpObjId peer2 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.38.70");
    // Collect and Persist Data - Step 1
    CollectionSet collectionSet = collector.collect(m_collectionAgent, parameters);
    validateCollectionSet(collectionSet);
    collectionSet.visit(persister);
    // Generate new SNMP Data
    final StringBuilder sb = new StringBuilder("|25|");
    long ts = 1327451787l;
    for (int i = 0; i < 25; i++) {
        sb.append(ts++);
        sb.append(",12,-1,12,-2,1|");
    }
    // Get Current Values
    SnmpValue v1a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
    SnmpValue v2a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
    // Set New Values
    SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer1, valFac.getOctetString(sb.toString().getBytes()));
    SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer2, valFac.getOctetString(sb.toString().getBytes()));
    // Validate New Values
    SnmpValue v1b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
    SnmpValue v2b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
    Assert.assertFalse(v1a.toDisplayString().equals(v1b.toDisplayString()));
    Assert.assertFalse(v2a.toDisplayString().equals(v2b.toDisplayString()));
    // Collect and Persist Data - Step 2
    collectionSet = collector.collect(m_collectionAgent, parameters);
    validateCollectionSet(collectionSet);
    collectionSet.visit(persister);
    // Validate Persisted Data
    Path pathToJrbFile = getSnmpRoot().toPath().resolve(Paths.get("1", TcaCollectionHandler.RESOURCE_TYPE_NAME, "171.19.37.60", TcaCollectionHandler.INBOUND_DELAY + m_rrdStrategy.getDefaultFileExtension()));
    RrdDb jrb = new RrdDb(pathToJrbFile.toString());
    // According with the Fixed Step
    Assert.assertEquals(1, jrb.getArchive(0).getArcStep());
    // According with the Sample Data
    Assert.assertEquals(ts - 1, jrb.getArchive(0).getEndTime());
    Robin inboundDelay = jrb.getArchive(0).getRobin(0);
    for (int i = inboundDelay.getSize() - 49; i < inboundDelay.getSize() - 25; i++) {
        Assert.assertEquals(new Double(11), Double.valueOf(inboundDelay.getValue(i)));
    }
    for (int i = inboundDelay.getSize() - 24; i < inboundDelay.getSize(); i++) {
        Assert.assertEquals(new Double(12), Double.valueOf(inboundDelay.getValue(i)));
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) Robin(org.jrobin.core.Robin) Test(org.junit.Test)

Example 14 with SnmpValueFactory

use of org.opennms.netmgt.snmp.SnmpValueFactory in project opennms by OpenNMS.

the class TrapHandlerITCase method testV1EnterpriseIdAndGenericAndSpecificAndMatchWithVarbindsAndTC.

// These exist to provide testing for the new Textual Convention feature
// See EventConfDataTest for the other part of this testing
@Test
@DirtiesContext
public void testV1EnterpriseIdAndGenericAndSpecificAndMatchWithVarbindsAndTC() throws Exception {
    SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
    LinkedHashMap<String, SnmpValue> varbinds = new LinkedHashMap<String, SnmpValue>();
    varbinds.put(".1.3.6.1.4.1.14179.2.6.2.20.0", valueFactory.getOctetString(new byte[] { (byte) 0x00, (byte) 0x14, (byte) 0xf1, (byte) 0xad, (byte) 0xa7, (byte) 0x50 }));
    Collection<Event> events = anticipateAndSend(false, true, "uei.opennms.org/vendor/cisco/bsnAPNoiseProfileUpdatedToPass", "v1", ".1.3.6.1.4.1.14179.2.6.3", 6, 38, varbinds);
    boolean foundMacAddress = false;
    // Assert that the MAC address varbind has been formatted into a colon-separated octet string
    for (Event event : events) {
        for (Parm parm : event.getParmCollection()) {
            if (".1.3.6.1.4.1.14179.2.6.2.20.0".equals(parm.getParmName())) {
                assertEquals("MAC address does not match", "00:14:F1:AD:A7:50", parm.getValue().getContent());
                foundMacAddress = true;
            }
        }
    }
    assertTrue("Did not find expected MAC address parm", foundMacAddress);
}
Also used : SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Event(org.opennms.netmgt.xml.event.Event) Parm(org.opennms.netmgt.xml.event.Parm) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 15 with SnmpValueFactory

use of org.opennms.netmgt.snmp.SnmpValueFactory in project opennms by OpenNMS.

the class SnmpValueTest method testCounter32.

@Test
public void testCounter32() {
    for (final SnmpValueFactory factory : m_factories) {
        final String methodName = "Counter32";
        final String stringResult = "42";
        final Long numberResult = 42L;
        final SnmpValue value = factory.getCounter32(numberResult);
        final String className = factory.getClass().getName();
        doNumericCheck(className, methodName, value, stringResult, numberResult);
    }
}
Also used : SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) JoeSnmpValueFactory(org.opennms.netmgt.snmp.joesnmp.JoeSnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Test(org.junit.Test)

Aggregations

SnmpValueFactory (org.opennms.netmgt.snmp.SnmpValueFactory)25 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)24 Test (org.junit.Test)23 JoeSnmpValueFactory (org.opennms.netmgt.snmp.joesnmp.JoeSnmpValueFactory)12 Parm (org.opennms.netmgt.xml.event.Parm)7 LinkedHashMap (java.util.LinkedHashMap)5 DirtiesContext (org.springframework.test.annotation.DirtiesContext)5 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)2 Event (org.opennms.netmgt.xml.event.Event)2 InetAddress (java.net.InetAddress)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Robin (org.jrobin.core.Robin)1 RrdDb (org.jrobin.core.RrdDb)1 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)1 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)1 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)1 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)1 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)1 MockSnmpValueFactory (org.opennms.netmgt.snmp.mock.MockSnmpValueFactory)1