Search in sources :

Example 1 with SnmpAgentConfigFactory

use of org.opennms.netmgt.config.api.SnmpAgentConfigFactory in project opennms by OpenNMS.

the class JUnitSnmpAgentExecutionListener method handleSnmpAgent.

private void handleSnmpAgent(final TestContext testContext, final JUnitSnmpAgent config, boolean useMockSnmpStrategy, MockSnmpDataProvider provider) throws IOException, UnknownHostException, InterruptedException {
    if (config == null)
        return;
    String factoryClassName = "unknown";
    try {
        final SnmpAgentConfigFactory factory = testContext.getApplicationContext().getBean("snmpPeerFactory", SnmpAgentConfigFactory.class);
        factoryClassName = factory.getClass().getName();
    } catch (final Exception e) {
    // ignore
    }
    if (!factoryClassName.contains("ProxySnmpAgentConfigFactory")) {
        LOG.warn("SNMP Peer Factory ({}) is not the ProxySnmpAgentConfigFactory -- did you forget to include applicationContext-proxy-snmp.xml?", factoryClassName);
    }
    LOG.debug("handleSnmpAgent(testContext, {}, {})", config, useMockSnmpStrategy);
    String host = config.host();
    if (host == null || "".equals(host)) {
        /*
             * NOTE: This call produces different results on different platforms so make
             * sure your client code is aware of this. If you use the {@link ProxySnmpAgentConfigFactory}
             * by including the <code>classpath:/META-INF/opennms/applicationContext-proxy-snmp.xml</code>
             * Spring context, you probably won't need to deal with this. It will override the
             * SnmpPeerFactory with the correct values.
             * 
             * Linux: 127.0.0.1
             * Mac OS: primary external interface
             */
        host = InetAddressUtils.getLocalHostAddressAsString();
    //host = "127.0.0.1";
    }
    final ResourceLoader loader = new DefaultResourceLoader();
    final Resource resource = loader.getResource(config.resource());
    // NOTE: The default value for config.port is specified inside {@link JUnitSnmpAgent}
    final InetAddress hostAddress = addr(host);
    final int port = config.port();
    final SnmpAgentAddress agentAddress = new SnmpAgentAddress(hostAddress, port);
    final SnmpAgentConfigProxyMapper mapper = SnmpAgentConfigProxyMapper.getInstance();
    if (useMockSnmpStrategy) {
        // since it's all virtual, the "mapped" port just points to the real agent address
        mapper.addProxy(hostAddress, agentAddress);
    } else {
        MockSnmpAgent agent = null;
        try {
            agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddress.getLocalHost()) + "/0");
        } catch (Throwable e) {
            agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddressUtils.ONE_TWENTY_SEVEN) + "/0");
        }
        SnmpAgentAddress listenAddress = new SnmpAgentAddress(agent.getInetAddress(), agent.getPort());
        mapper.addProxy(hostAddress, listenAddress);
        testContext.setAttribute(IPADDRESS_KEY, listenAddress.getAddress());
        testContext.setAttribute(PORT_KEY, listenAddress.getPort());
        LOG.debug("using MockSnmpAgent on {} for 'real' address {}", listenAddress, agentAddress);
        provider.addAgent(agentAddress, agent);
    }
    provider.setDataForAddress(agentAddress, resource);
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress) ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) MockSnmpAgent(org.opennms.mock.snmp.MockSnmpAgent) SnmpAgentConfigFactory(org.opennms.netmgt.config.api.SnmpAgentConfigFactory) Resource(org.springframework.core.io.Resource) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Aggregations

IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 MockSnmpAgent (org.opennms.mock.snmp.MockSnmpAgent)1 SnmpAgentConfigFactory (org.opennms.netmgt.config.api.SnmpAgentConfigFactory)1 SnmpAgentAddress (org.opennms.netmgt.snmp.SnmpAgentAddress)1 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)1 Resource (org.springframework.core.io.Resource)1 ResourceLoader (org.springframework.core.io.ResourceLoader)1