Search in sources :

Example 1 with MockPlatformTransactionManager

use of org.opennms.core.test.MockPlatformTransactionManager in project opennms by OpenNMS.

the class NsclientCollectorTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    startServer("None&8&", "10");
    // Initialize Mocks
    m_transactionManager = new MockPlatformTransactionManager();
    m_ipInterfaceDao = EasyMock.createMock(IpInterfaceDao.class);
    m_eventProxy = EasyMock.createMock(EventProxy.class);
    NetworkBuilder builder = new NetworkBuilder();
    builder.addNode("winsrv");
    builder.addInterface(getServer().getInetAddress().getHostAddress()).addSnmpInterface(1).setCollectionEnabled(true);
    builder.getCurrentNode().setId(1);
    OnmsIpInterface iface = builder.getCurrentNode().getIpInterfaces().iterator().next();
    iface.setIsSnmpPrimary(PrimaryType.PRIMARY);
    iface.setId(1);
    EasyMock.expect(m_ipInterfaceDao.load(1)).andReturn(iface).anyTimes();
    EasyMock.replay(m_ipInterfaceDao, m_eventProxy);
    // Initialize NSClient Configuration
    String nsclient_config = "<nsclient-config port=\"" + getServer().getLocalPort() + "\" retry=\"1\" timeout=\"3000\" />";
    NSClientPeerFactory.setInstance(new NSClientPeerFactory(new ByteArrayInputStream(nsclient_config.getBytes())));
    NSClientDataCollectionConfigFactory.setInstance(new NSClientDataCollectionConfigFactory("src/test/resources/nsclient-datacollection-config.xml"));
    // Initialize Collection Agent
    m_collectionAgent = DefaultCollectionAgent.create(1, m_ipInterfaceDao, m_transactionManager);
}
Also used : NSClientPeerFactory(org.opennms.protocols.nsclient.config.NSClientPeerFactory) NetworkBuilder(org.opennms.netmgt.model.NetworkBuilder) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) NSClientDataCollectionConfigFactory(org.opennms.protocols.nsclient.config.NSClientDataCollectionConfigFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) EventProxy(org.opennms.netmgt.events.api.EventProxy) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) Before(org.junit.Before)

Example 2 with MockPlatformTransactionManager

use of org.opennms.core.test.MockPlatformTransactionManager in project opennms by OpenNMS.

the class DefaultCollectionAgentTest method verifyThatTheIpAndNodeIdAreCached.

/**
 * NMS-5105: When processing serviceDeleted and interfaceDeleted events
 * in Collectd we need to match both the Node ID and IP Address of
 * the service that is being collected with the information from the event.
 *
 * Since the entities have been deleted, we not longer be able to reach
 * in the database to fetch the required details. Instead, they
 * should be loaded when the agent is created, and cached for the lifetime
 * of the object.
 */
@Test
public void verifyThatTheIpAndNodeIdAreCached() {
    OnmsNode node = new OnmsNode();
    node.setId(11);
    OnmsIpInterface iface = new OnmsIpInterface();
    iface.setId(42);
    iface.setNode(node);
    iface.setIpAddress(InetAddressUtils.ONE_TWENTY_SEVEN);
    IpInterfaceDao ifaceDao = EasyMock.createMock(IpInterfaceDao.class);
    EasyMock.expect(ifaceDao.load(iface.getId())).andReturn(iface).times(5);
    EasyMock.replay(ifaceDao);
    PlatformTransactionManager transMgr = new MockPlatformTransactionManager();
    CollectionAgent agent = DefaultCollectionAgent.create(iface.getId(), ifaceDao, transMgr);
    EasyMock.verify(ifaceDao);
    assertEquals(iface.getIpAddress(), agent.getAddress());
    assertEquals(node.getId().intValue(), agent.getNodeId());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) DefaultCollectionAgent(org.opennms.netmgt.collection.core.DefaultCollectionAgent) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) Test(org.junit.Test)

Example 3 with MockPlatformTransactionManager

use of org.opennms.core.test.MockPlatformTransactionManager in project opennms by OpenNMS.

the class PersistRegexSelectorStrategyTest method setUp.

@Before
public void setUp() throws Exception {
    ipInterfaceDao = EasyMock.createMock(IpInterfaceDao.class);
    String localhost = InetAddress.getLocalHost().getHostAddress();
    NetworkBuilder builder = new NetworkBuilder();
    builder.addNode("myNode");
    builder.addInterface(localhost).setIsManaged("M").setIsSnmpPrimary("P");
    OnmsNode node = builder.getCurrentNode();
    node.setId(1);
    OnmsIpInterface ipInterface = node.getIpInterfaces().iterator().next();
    EasyMock.expect(ipInterfaceDao.load(1)).andReturn(ipInterface).anyTimes();
    EasyMock.replay(ipInterfaceDao);
    Package pkg = new Package();
    pkg.setName("junitTestPackage");
    Filter filter = new Filter();
    filter.setContent("IPADDR != '0.0.0.0'");
    pkg.setFilter(filter);
    Service service = new Service();
    service.setName("SNMP");
    pkg.addService(service);
    Map<String, Object> map = new TreeMap<String, Object>();
    List<org.opennms.netmgt.config.collectd.Parameter> params = pkg.getService("SNMP").getParameters();
    for (org.opennms.netmgt.config.collectd.Parameter p : params) {
        map.put(p.getKey(), p.getValue());
    }
    map.put("collection", "default");
    serviceParams = new ServiceParameters(map);
    LocationAwareSnmpClient locationAwareSnmpClient = new LocationAwareSnmpClientRpcImpl(new MockRpcClientFactory());
    PlatformTransactionManager ptm = new MockPlatformTransactionManager();
    SnmpCollectionAgent agent = DefaultSnmpCollectionAgent.create(1, ipInterfaceDao, ptm);
    OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, serviceParams, new MockDataCollectionConfigDao(), locationAwareSnmpClient);
    org.opennms.netmgt.config.datacollection.ResourceType rt = new org.opennms.netmgt.config.datacollection.ResourceType();
    rt.setName("myResourceType");
    StorageStrategy storageStrategy = new StorageStrategy();
    storageStrategy.setClazz("org.opennms.netmgt.collection.support.IndexStorageStrategy");
    rt.setStorageStrategy(storageStrategy);
    PersistenceSelectorStrategy persistenceSelectorStrategy = new PersistenceSelectorStrategy();
    persistenceSelectorStrategy.setClazz("org.opennms.netmgt.collectd.PersistRegexSelectorStrategy");
    Parameter param = new Parameter();
    param.setKey(PersistRegexSelectorStrategy.MATCH_EXPRESSION);
    param.setValue("#name matches '^agalue.*$'");
    persistenceSelectorStrategy.addParameter(param);
    rt.setPersistenceSelectorStrategy(persistenceSelectorStrategy);
    GenericIndexResourceType resourceType = new GenericIndexResourceType(agent, snmpCollection, rt);
    resourceA = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.1"));
    AttributeGroupType groupType = new AttributeGroupType("mib2-interfaces", AttributeGroupType.IF_TYPE_ALL);
    MibObject mibObject = new MibObject();
    mibObject.setOid(".1.2.3.4.5.6.7.8.9.2.1");
    mibObject.setInstance("1");
    mibObject.setAlias("name");
    mibObject.setType("string");
    StringAttributeType attributeType = new StringAttributeType(resourceType, snmpCollection.getName(), mibObject, groupType);
    SnmpValue snmpValue = new Snmp4JValueFactory().getOctetString("agalue rules!".getBytes());
    resourceA.setAttributeValue(attributeType, snmpValue);
    resourceB = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.2"));
}
Also used : StorageStrategy(org.opennms.netmgt.config.datacollection.StorageStrategy) Snmp4JValueFactory(org.opennms.netmgt.snmp.snmp4j.Snmp4JValueFactory) NetworkBuilder(org.opennms.netmgt.model.NetworkBuilder) LocationAwareSnmpClientRpcImpl(org.opennms.netmgt.snmp.proxy.common.LocationAwareSnmpClientRpcImpl) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) MockDataCollectionConfigDao(org.opennms.netmgt.dao.mock.MockDataCollectionConfigDao) OnmsNode(org.opennms.netmgt.model.OnmsNode) Service(org.opennms.netmgt.config.collectd.Service) TreeMap(java.util.TreeMap) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) MockRpcClientFactory(org.opennms.core.rpc.mock.MockRpcClientFactory) LocationAwareSnmpClient(org.opennms.netmgt.snmp.proxy.LocationAwareSnmpClient) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Filter(org.opennms.netmgt.config.collectd.Filter) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) Parameter(org.opennms.netmgt.config.datacollection.Parameter) MibObject(org.opennms.netmgt.config.datacollection.MibObject) Package(org.opennms.netmgt.config.collectd.Package) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) MibObject(org.opennms.netmgt.config.datacollection.MibObject) PersistenceSelectorStrategy(org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy) Before(org.junit.Before)

Example 4 with MockPlatformTransactionManager

use of org.opennms.core.test.MockPlatformTransactionManager in project opennms by OpenNMS.

the class SnmpAttributeTest method testPersisting.

@Ignore
private void testPersisting(String matchValue, SnmpValue snmpValue) throws Exception {
    OnmsNode node = new OnmsNode();
    node.setId(3);
    OnmsIpInterface ipInterface = new OnmsIpInterface();
    ipInterface.setId(1);
    ipInterface.setNode(node);
    ipInterface.setIpAddress(InetAddressUtils.addr("192.168.1.1"));
    // It used to be 3, but I think it is more correct to use getStoreDir from DefaultCollectionAgentService on DefaultCollectionAgent (NMS-7516)
    expect(m_ipInterfaceDao.load(1)).andReturn(ipInterface).times(5);
    expect(m_rrdStrategy.getDefaultFileExtension()).andReturn(".myLittleEasyMockedStrategyAndMe").anyTimes();
    expect(m_rrdStrategy.createDefinition(isA(String.class), isA(String.class), isA(String.class), anyInt(), isAList(RrdDataSource.class), isAList(String.class))).andReturn(new Object());
    m_rrdStrategy.createFile(isA(Object.class));
    expect(m_rrdStrategy.openFile(isA(String.class))).andReturn(new Object());
    m_rrdStrategy.updateFile(isA(Object.class), isA(String.class), matches(".*:" + matchValue));
    m_rrdStrategy.closeFile(isA(Object.class));
    m_mocks.replayAll();
    SnmpCollectionAgent agent = DefaultSnmpCollectionAgent.create(ipInterface.getId(), m_ipInterfaceDao, new MockPlatformTransactionManager());
    OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), new MockDataCollectionConfig(), m_locationAwareSnmpClient);
    NodeResourceType resourceType = new NodeResourceType(agent, snmpCollection);
    NodeInfo nodeInfo = resourceType.getNodeInfo();
    MibObject mibObject = new MibObject();
    mibObject.setOid(".1.3.6.1.4.1.12238.55.9997.4.1.2.9.116.101.109.112.95.117.108.107.111");
    mibObject.setInstance("1");
    mibObject.setAlias("temp_ulko");
    mibObject.setType("gauge");
    NumericAttributeType attributeType = new NumericAttributeType(resourceType, snmpCollection.getName(), mibObject, new AttributeGroupType("foo", AttributeGroupType.IF_TYPE_IGNORE));
    attributeType.storeResult(new SnmpCollectionSet(agent, snmpCollection, m_locationAwareSnmpClient), null, new SnmpResult(mibObject.getSnmpObjId(), new SnmpInstId(mibObject.getInstance()), snmpValue));
    RrdRepository repository = createRrdRepository();
    repository.setRraList(Collections.singletonList("RRA:AVERAGE:0.5:1:2016"));
    RrdPersisterFactory persisterFactory = new RrdPersisterFactory();
    persisterFactory.setRrdStrategy(m_rrdStrategy);
    persisterFactory.setResourceStorageDao(m_resourceStorageDao);
    CollectionSetVisitor persister = persisterFactory.createPersister(new ServiceParameters(Collections.emptyMap()), repository);
    final AtomicInteger count = new AtomicInteger(0);
    nodeInfo.visit(new CollectionSetVisitorWrapper(persister) {

        @Override
        public void visitAttribute(CollectionAttribute attribute) {
            super.visitAttribute(attribute);
            count.incrementAndGet();
        }
    });
    assertEquals(1, count.get());
}
Also used : HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) RrdPersisterFactory(org.opennms.netmgt.collection.persistence.rrd.RrdPersisterFactory) RrdDataSource(org.opennms.netmgt.rrd.RrdDataSource) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) CollectionSetVisitorWrapper(org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) OnmsNode(org.opennms.netmgt.model.OnmsNode) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) MibObject(org.opennms.netmgt.config.datacollection.MibObject) Ignore(org.junit.Ignore)

Example 5 with MockPlatformTransactionManager

use of org.opennms.core.test.MockPlatformTransactionManager in project opennms by OpenNMS.

the class SnmpCollectorITCase method createAgent.

protected void createAgent(int ifIndex, PrimaryType ifCollType) {
    m_node = new OnmsNode();
    m_node.setSysObjectId(".1.2.3.4.5.6.7");
    OnmsSnmpInterface snmpIface = new OnmsSnmpInterface(m_node, ifIndex);
    m_iface = new OnmsIpInterface();
    m_iface.setId(123);
    m_iface.setIpAddress(myLocalHost());
    m_iface.setIsSnmpPrimary(ifCollType);
    m_iface.setSnmpInterface(snmpIface);
    m_node.addIpInterface(m_iface);
    EasyMock.expect(m_ifaceDao.load(m_iface.getId())).andReturn(m_iface).anyTimes();
    m_easyMockUtils.replayAll();
    m_agent = DefaultSnmpCollectionAgent.create(m_iface.getId(), m_ifaceDao, new MockPlatformTransactionManager());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager)

Aggregations

MockPlatformTransactionManager (org.opennms.core.test.MockPlatformTransactionManager)5 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)5 OnmsNode (org.opennms.netmgt.model.OnmsNode)4 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)3 Before (org.junit.Before)2 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)2 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)2 MibObject (org.opennms.netmgt.config.datacollection.MibObject)2 NetworkBuilder (org.opennms.netmgt.model.NetworkBuilder)2 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)2 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 MockRpcClientFactory (org.opennms.core.rpc.mock.MockRpcClientFactory)1 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)1 CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)1