From 9bd522501debe00e484a0de04f458cc06bf9aa51 Mon Sep 17 00:00:00 2001 From: Jeff Gehlbach Date: Tue, 26 Aug 2014 09:36:12 -0400 Subject: [PATCH 1/3] Squashed commit of the following: commit 3c768737e46abf37fa5ab82edcea085623de231a Merge: 05ff5c2 4b2e13e Author: Jeff Gehlbach Date: Tue Aug 26 09:35:20 2014 -0400 Merge branch '1.12' of github.com:OpenNMS/opennms into features/collectable-service-count commit 05ff5c2c88d077ffe0bbde457d11ae4f65775479 Author: Jeff Gehlbach Date: Fri Aug 22 13:27:12 2014 -0400 Expose via JMX Collectd's collectable service count --- .../filtered/etc/jmx-datacollection-config.xml | 1 + .../src/main/filtered/etc/snmp-graph.properties | 14 +++++++++++++- .../java/org/opennms/netmgt/collectd/Collectd.java | 4 ++++ .../org/opennms/netmgt/collectd/jmx/Collectd.java | 6 ++++++ .../opennms/netmgt/collectd/jmx/CollectdMBean.java | 5 +++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/opennms-base-assembly/src/main/filtered/etc/jmx-datacollection-config.xml b/opennms-base-assembly/src/main/filtered/etc/jmx-datacollection-config.xml index d6bd855dd920..69afde4f16dc 100644 --- a/opennms-base-assembly/src/main/filtered/etc/jmx-datacollection-config.xml +++ b/opennms-base-assembly/src/main/filtered/etc/jmx-datacollection-config.xml @@ -101,6 +101,7 @@ + diff --git a/opennms-base-assembly/src/main/filtered/etc/snmp-graph.properties b/opennms-base-assembly/src/main/filtered/etc/snmp-graph.properties index b9a9865b309d..76cc5fcbc6d0 100644 --- a/opennms-base-assembly/src/main/filtered/etc/snmp-graph.properties +++ b/opennms-base-assembly/src/main/filtered/etc/snmp-graph.properties @@ -67,7 +67,7 @@ include.directory=snmp-graph.properties.d reports=onms.queued.updates, onms.queued.pending, \ onms.pollerd.activeThreads, onms.pollerd.completedRatio, \ onms.collectd.activeThreads, onms.collectd.threadpool, \ -onms.collectd.completedRatio, \ +onms.collectd.completedRatio, onms.collectd.collectableServiceCount, \ OpenNMS.Capsd.ActiveSuspecThreads.AttributeReport, \ OpenNMS.Capsd.ActiveRescanThreads.AttributeReport, \ OpenNMS.Capsd.SuspectCompletTasks.AttributeReport, \ @@ -218,6 +218,18 @@ report.onms.collectd.completedRatio.command=--title="OpenNMS Collectd Task Compl GPRINT:percent:MIN:"Min \\: %8.2lf %s" \ GPRINT:percent:MAX:"Max \\: %8.2lf %s\\n" +report.onms.collectd.collectableServiceCount.name=OpenNMS Collectd Collectable Services +report.onms.collectd.collectableServiceCount.columns=ONMSCollectSvcCount +report.onms.collectd.collectableServiceCount.type=interfaceSnmp +report.onms.collectd.collectableServiceCount.command=--title="OpenNMS Collectd Collectable Services" \ + --vertical-label="Services" \ + DEF:total={rrd1}:ONMSCollectSvcCount:AVERAGE \ + AREA:total#F5CD9A \ + LINE2:total#F19A2A:"Collectable" \ + GPRINT:total:AVERAGE:" Avg \\: %8.2lf %s" \ + GPRINT:total:MIN:"Min \\: %8.2lf %s" \ + GPRINT:total:MAX:"Max \\: %8.2lf %s\\n" + ########################################### ## OpenNMS.Capsd.ActiveSuspecThreads.AttributeReport ########################################### diff --git a/opennms-services/src/main/java/org/opennms/netmgt/collectd/Collectd.java b/opennms-services/src/main/java/org/opennms/netmgt/collectd/Collectd.java index 8a1ae37bbe9c..118a4168e18a 100644 --- a/opennms-services/src/main/java/org/opennms/netmgt/collectd/Collectd.java +++ b/opennms-services/src/main/java/org/opennms/netmgt/collectd/Collectd.java @@ -1596,4 +1596,8 @@ private void instantiateCollectors() { } } + public long getCollectableServiceCount() { + return m_collectableServices.size(); + } + } diff --git a/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/Collectd.java b/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/Collectd.java index 636ca57738ef..5222eb62e7e5 100644 --- a/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/Collectd.java +++ b/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/Collectd.java @@ -120,6 +120,12 @@ public long getPeakPoolThreads() { return 0L; } } + + @Override + public long getCollectableServiceCount() { + return getDaemon().getCollectableServiceCount(); + } + private ThreadPoolExecutor getExecutor() { return (ThreadPoolExecutor) ((LegacyScheduler) getDaemon().getScheduler()).getRunner(); } diff --git a/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/CollectdMBean.java b/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/CollectdMBean.java index 289c3e9f2016..795e0d58cf5e 100644 --- a/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/CollectdMBean.java +++ b/opennms-services/src/main/java/org/opennms/netmgt/collectd/jmx/CollectdMBean.java @@ -71,4 +71,9 @@ public interface CollectdMBean extends BaseOnmsMBean { * @return The ratio of completed to scheduled collection tasks since collector startup */ public double getTaskCompletionRatio(); + + /** + * @return The number of collectable services currently seen by Collectd + */ + public long getCollectableServiceCount(); } From 405662493a02fd7b42fd5c031e25bf3ee4c2ac80 Mon Sep 17 00:00:00 2001 From: Benjamin Reed Date: Mon, 25 Aug 2014 12:28:53 -0400 Subject: [PATCH 2/3] fix a couple of eclipse warnings --- .../src/main/java/org/opennms/gwt/web/ui/CoreWeb.gwt.xml | 1 - .../opennms/web/svclayer/applicationContext-svclayer.xml | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/opennms-webapp/src/main/java/org/opennms/gwt/web/ui/CoreWeb.gwt.xml b/opennms-webapp/src/main/java/org/opennms/gwt/web/ui/CoreWeb.gwt.xml index 4ed9491c9c05..391db3328213 100644 --- a/opennms-webapp/src/main/java/org/opennms/gwt/web/ui/CoreWeb.gwt.xml +++ b/opennms-webapp/src/main/java/org/opennms/gwt/web/ui/CoreWeb.gwt.xml @@ -1,5 +1,4 @@ - diff --git a/opennms-webapp/src/main/resources/org/opennms/web/svclayer/applicationContext-svclayer.xml b/opennms-webapp/src/main/resources/org/opennms/web/svclayer/applicationContext-svclayer.xml index a83733d47fc8..6edca197b931 100644 --- a/opennms-webapp/src/main/resources/org/opennms/web/svclayer/applicationContext-svclayer.xml +++ b/opennms-webapp/src/main/resources/org/opennms/web/svclayer/applicationContext-svclayer.xml @@ -4,9 +4,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:onmsgi="http://xmlns.opennms.org/xsd/spring/onms-osgi" -xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd +xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://xmlns.opennms.org/xsd/spring/onms-osgi http://xmlns.opennms.org/xsd/spring/onms-osgi.xsd "> From b50c8cd3accd72513367f2dd58646ec90ec56350 Mon Sep 17 00:00:00 2001 From: Benjamin Reed Date: Tue, 26 Aug 2014 14:22:41 -0400 Subject: [PATCH 3/3] clean SnmpPeerFactory; add ReST range support * get rid of a bunch of pointless constructors in SnmpPeerFactory * add FileReloadContainer support to SnmpPeerFactory (auto-reloads snmp-config.xml when it changes) * add range support to the SnmpConfigRestService updates -- PUTs now support giving an IP range rather than just an IP address --- .../snmp/ProxySnmpAgentConfigFactory.java | 5 +- .../netmgt/config/SnmpPeerFactory.java | 176 ++++++-------- .../netmgt/config/SnmpEventInfoTest.java | 84 ++++--- .../operations/UpdateOperationTest.java | 3 +- .../collectd/CollectorConfigDaoImplTest.java | 2 +- .../web/rest/SnmpConfigRestService.java | 24 +- .../web/rest/SnmpConfigRestServiceTest.java | 219 +++++++++++++----- 7 files changed, 316 insertions(+), 197 deletions(-) diff --git a/core/test-api/snmp/src/main/java/org/opennms/core/test/snmp/ProxySnmpAgentConfigFactory.java b/core/test-api/snmp/src/main/java/org/opennms/core/test/snmp/ProxySnmpAgentConfigFactory.java index d206d988be2b..1baea7aacfc0 100644 --- a/core/test-api/snmp/src/main/java/org/opennms/core/test/snmp/ProxySnmpAgentConfigFactory.java +++ b/core/test-api/snmp/src/main/java/org/opennms/core/test/snmp/ProxySnmpAgentConfigFactory.java @@ -42,11 +42,12 @@ import org.opennms.netmgt.config.SnmpPeerFactory; import org.opennms.netmgt.snmp.SnmpAgentAddress; import org.opennms.netmgt.snmp.SnmpAgentConfig; +import org.springframework.core.io.InputStreamResource; public class ProxySnmpAgentConfigFactory extends SnmpPeerFactory { - public ProxySnmpAgentConfigFactory(InputStream config) throws MarshalException, ValidationException, FileNotFoundException, IOException { - super(config); + public ProxySnmpAgentConfigFactory(final InputStream config) throws MarshalException, ValidationException, FileNotFoundException, IOException { + super(new InputStreamResource(config)); } public SnmpAgentConfig getAgentConfig(final InetAddress address) { diff --git a/opennms-config/src/main/java/org/opennms/netmgt/config/SnmpPeerFactory.java b/opennms-config/src/main/java/org/opennms/netmgt/config/SnmpPeerFactory.java index 8eb211fea334..f7e1abbf0ba0 100644 --- a/opennms-config/src/main/java/org/opennms/netmgt/config/SnmpPeerFactory.java +++ b/opennms-config/src/main/java/org/opennms/netmgt/config/SnmpPeerFactory.java @@ -32,9 +32,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStreamWriter; -import java.io.Reader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; @@ -47,6 +45,8 @@ import org.apache.commons.lang.StringUtils; import org.opennms.core.utils.ByteArrayComparator; import org.opennms.core.utils.ConfigFileConstants; +import org.opennms.core.utils.FileReloadCallback; +import org.opennms.core.utils.FileReloadContainer; import org.opennms.core.utils.IPLike; import org.opennms.core.utils.InetAddressUtils; import org.opennms.core.utils.LogUtils; @@ -57,7 +57,6 @@ import org.opennms.netmgt.snmp.SnmpAgentConfig; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; -import org.xml.sax.InputSource; /** * This class is the main repository for SNMP configuration information used by @@ -93,6 +92,9 @@ public class SnmpPeerFactory implements SnmpAgentConfigFactory { private static File m_configFile; + private static FileReloadContainer m_container; + private static FileReloadCallback m_callback; + /** * This member is set to true if the configuration file has been loaded. */ @@ -100,20 +102,10 @@ public class SnmpPeerFactory implements SnmpAgentConfigFactory { private static final int VERSION_UNSPECIFIED = -1; - /** - * Private constructor - * - * @exception java.io.IOException - * Thrown if the specified config file cannot be read - * @exception org.exolab.castor.xml.MarshalException - * Thrown if the file does not conform to the schema. - * @exception org.exolab.castor.xml.ValidationException - * Thrown if the contents do not match the required schema. - */ - private SnmpPeerFactory(final File configFile) throws IOException { - this(new FileSystemResource(configFile)); + protected SnmpConfig loadConfig(final Resource resource) { + return JaxbUtils.unmarshal(SnmpConfig.class, resource); } - + /** *

Constructor for SnmpPeerFactory.

* @@ -122,56 +114,33 @@ private SnmpPeerFactory(final File configFile) throws IOException { public SnmpPeerFactory(final Resource resource) { SnmpPeerFactory.getWriteLock().lock(); try { - m_config = JaxbUtils.unmarshal(SnmpConfig.class, resource); - } finally { - SnmpPeerFactory.getWriteLock().unlock(); - } - } - - /** - *

Constructor for SnmpPeerFactory.

- * - * @param rdr a {@link java.io.Reader} object. - * @throws java.io.IOException if any. - * @throws org.exolab.castor.xml.MarshalException if any. - * @throws org.exolab.castor.xml.ValidationException if any. - * @deprecated Use code for InputStream instead to avoid character set issues - */ - public SnmpPeerFactory(final Reader rdr) throws IOException { - SnmpPeerFactory.getWriteLock().lock(); - try { - m_config = JaxbUtils.unmarshal(SnmpConfig.class, rdr); - } finally { - SnmpPeerFactory.getWriteLock().unlock(); - } - } - - /** - * A constructor that takes a config string for use mostly in tests - */ - public SnmpPeerFactory(final String configString) throws IOException { - SnmpPeerFactory.getWriteLock().lock(); - try { - m_config = JaxbUtils.unmarshal(SnmpConfig.class, configString); + final SnmpConfig config = loadConfig(resource); + + try { + final File file = resource.getFile(); + if (file != null) { + m_callback = new FileReloadCallback() { + @Override + public SnmpConfig reload(final SnmpConfig object, final Resource resource) throws IOException { + return loadConfig(resource); + } + + }; + m_container = new FileReloadContainer(config, resource, m_callback); + return; + } + } catch (final IOException e) { + LogUtils.debugf(this, "No file associated with resource %s, skipping reload container initialization.", resource); + } + + // if we fall through to here, then the file was null, or something else went wrong + // store the config directly + m_config = config; } finally { SnmpPeerFactory.getWriteLock().unlock(); } } - /** - *

Constructor for SnmpPeerFactory.

- * - * @param stream a {@link java.io.InputStream} object. - */ - public SnmpPeerFactory(final InputStream stream) { - SnmpPeerFactory.getWriteLock().lock(); - try { - m_config = JaxbUtils.unmarshal(SnmpConfig.class, new InputSource(stream), null); - } finally { - SnmpPeerFactory.getWriteLock().unlock(); - } - } - public static Lock getReadLock() { return m_readLock; } @@ -186,13 +155,6 @@ public static Lock getWriteLock() { * * @exception java.io.IOException * Thrown if the specified config file cannot be read - * @exception org.exolab.castor.xml.MarshalException - * Thrown if the file does not conform to the schema. - * @exception org.exolab.castor.xml.ValidationException - * Thrown if the contents do not match the required schema. - * @throws java.io.IOException if any. - * @throws org.exolab.castor.xml.MarshalException if any. - * @throws org.exolab.castor.xml.ValidationException if any. */ public static void init() throws IOException { SnmpPeerFactory.getWriteLock().lock(); @@ -205,7 +167,9 @@ public static void init() throws IOException { final File cfgFile = getFile(); LogUtils.debugf(SnmpPeerFactory.class, "init: config file path: %s", cfgFile.getPath()); - m_singleton = new SnmpPeerFactory(cfgFile); + final FileSystemResource resource = new FileSystemResource(cfgFile); + + m_singleton = new SnmpPeerFactory(resource); m_loaded = true; } finally { SnmpPeerFactory.getWriteLock().unlock(); @@ -227,8 +191,7 @@ public static void saveToFile(final File file) throws UnsupportedEncodingException, FileNotFoundException, IOException { // Marshal to a string first, then write the string to the file. This - // way the original config - // isn't lost if the XML from the marshal is hosed. + // way the original config isn't lost if the XML from the marshal is hosed. final String marshalledConfig = marshallConfig(); SnmpPeerFactory.getWriteLock().lock(); @@ -241,6 +204,9 @@ public static void saveToFile(final File file) fileWriter.write(marshalledConfig); fileWriter.flush(); fileWriter.close(); + if (m_container != null) { + m_container.reload(); + } } } finally { IOUtils.closeQuietly(fileWriter); @@ -332,7 +298,7 @@ public SnmpAgentConfig getAgentConfig(final InetAddress agentAddress) { private SnmpAgentConfig getAgentConfig(final InetAddress agentInetAddress, final int requestedSnmpVersion) { SnmpPeerFactory.getReadLock().lock(); try { - if (m_config == null) { + if (getSnmpConfig() == null) { final SnmpAgentConfig agentConfig = new SnmpAgentConfig(agentInetAddress); if (requestedSnmpVersion == VERSION_UNSPECIFIED) { agentConfig.setVersion(SnmpAgentConfig.DEFAULT_VERSION); @@ -345,11 +311,11 @@ private SnmpAgentConfig getAgentConfig(final InetAddress agentInetAddress, final final SnmpAgentConfig agentConfig = new SnmpAgentConfig(agentInetAddress); - // Now set the defaults from the m_config + // Now set the defaults from the getSnmpConfig() setSnmpAgentConfig(agentConfig, new Definition(), requestedSnmpVersion); // Attempt to locate the node - DEFLOOP: for (final Definition def : m_config.getDefinitionCollection()) { + DEFLOOP: for (final Definition def : getSnmpConfig().getDefinitionCollection()) { // check the specifics first for (final String saddr : def.getSpecificCollection()) { try { @@ -443,13 +409,13 @@ private void setCommonAttributes(final SnmpAgentConfig agentConfig, final Defini private int determineMaxRepetitions(final Definition def) { return (!def.hasMaxRepetitions() ? - (!m_config.hasMaxRepetitions() ? - SnmpAgentConfig.DEFAULT_MAX_REPETITIONS : m_config.getMaxRepetitions()) : def.getMaxRepetitions()); + (!getSnmpConfig().hasMaxRepetitions() ? + SnmpAgentConfig.DEFAULT_MAX_REPETITIONS : getSnmpConfig().getMaxRepetitions()) : def.getMaxRepetitions()); } private InetAddress determineProxyHost(final Definition def) { InetAddress inetAddr = null; - final String address = def.getProxyHost() == null ? (m_config.getProxyHost() == null ? null : m_config.getProxyHost()) : def.getProxyHost(); + final String address = def.getProxyHost() == null ? (getSnmpConfig().getProxyHost() == null ? null : getSnmpConfig().getProxyHost()) : def.getProxyHost(); if (address != null) { try { inetAddr = InetAddressUtils.addr(address); @@ -462,8 +428,8 @@ private InetAddress determineProxyHost(final Definition def) { private int determineMaxVarsPerPdu(final Definition def) { return (!def.hasMaxVarsPerPdu() ? - (!m_config.hasMaxVarsPerPdu() ? - SnmpAgentConfig.DEFAULT_MAX_VARS_PER_PDU : m_config.getMaxVarsPerPdu()) : def.getMaxVarsPerPdu()); + (!getSnmpConfig().hasMaxVarsPerPdu() ? + SnmpAgentConfig.DEFAULT_MAX_VARS_PER_PDU : getSnmpConfig().getMaxVarsPerPdu()) : def.getMaxVarsPerPdu()); } /** * Helper method to search the snmp-config for the appropriate read @@ -472,7 +438,7 @@ private int determineMaxVarsPerPdu(final Definition def) { * @return */ private String determineReadCommunity(final Definition def) { - return (def.getReadCommunity() == null ? (m_config.getReadCommunity() == null ? SnmpAgentConfig.DEFAULT_READ_COMMUNITY :m_config.getReadCommunity()) : def.getReadCommunity()); + return (def.getReadCommunity() == null ? (getSnmpConfig().getReadCommunity() == null ? SnmpAgentConfig.DEFAULT_READ_COMMUNITY :getSnmpConfig().getReadCommunity()) : def.getReadCommunity()); } /** @@ -482,7 +448,7 @@ private String determineReadCommunity(final Definition def) { * @return */ private String determineWriteCommunity(final Definition def) { - return (def.getWriteCommunity() == null ? (m_config.getWriteCommunity() == null ? SnmpAgentConfig.DEFAULT_WRITE_COMMUNITY :m_config.getWriteCommunity()) : def.getWriteCommunity()); + return (def.getWriteCommunity() == null ? (getSnmpConfig().getWriteCommunity() == null ? SnmpAgentConfig.DEFAULT_WRITE_COMMUNITY :getSnmpConfig().getWriteCommunity()) : def.getWriteCommunity()); } /** @@ -492,7 +458,7 @@ private String determineWriteCommunity(final Definition def) { * @return */ private int determineMaxRequestSize(final Definition def) { - return (!def.hasMaxRequestSize() ? (!m_config.hasMaxRequestSize() ? SnmpAgentConfig.DEFAULT_MAX_REQUEST_SIZE : m_config.getMaxRequestSize()) : def.getMaxRequestSize()); + return (!def.hasMaxRequestSize() ? (!getSnmpConfig().hasMaxRequestSize() ? SnmpAgentConfig.DEFAULT_MAX_REQUEST_SIZE : getSnmpConfig().getMaxRequestSize()) : def.getMaxRequestSize()); } /** @@ -503,7 +469,7 @@ private int determineMaxRequestSize(final Definition def) { * @return */ private String determineSecurityName(final Definition def) { - final String securityName = (def.getSecurityName() == null ? m_config.getSecurityName() : def.getSecurityName() ); + final String securityName = (def.getSecurityName() == null ? getSnmpConfig().getSecurityName() : def.getSecurityName() ); if (securityName == null) { return SnmpAgentConfig.DEFAULT_SECURITY_NAME; } @@ -518,7 +484,7 @@ private String determineSecurityName(final Definition def) { * @return */ private String determineAuthProtocol(final Definition def) { - final String authProtocol = (def.getAuthProtocol() == null ? m_config.getAuthProtocol() : def.getAuthProtocol()); + final String authProtocol = (def.getAuthProtocol() == null ? getSnmpConfig().getAuthProtocol() : def.getAuthProtocol()); if (authProtocol == null && determineAuthPassPhrase(def) != null) { return SnmpAgentConfig.DEFAULT_AUTH_PROTOCOL; } @@ -533,7 +499,7 @@ private String determineAuthProtocol(final Definition def) { * @return */ private String determineAuthPassPhrase(final Definition def) { - final String authPassPhrase = (def.getAuthPassphrase() == null ? m_config.getAuthPassphrase() : def.getAuthPassphrase()); + final String authPassPhrase = (def.getAuthPassphrase() == null ? getSnmpConfig().getAuthPassphrase() : def.getAuthPassphrase()); // Forcing a default is wrong, because if it is not explicitly defined, probably it means that it should not be used, and SNMP4J expect null for optional parameters. /* if (authPassPhrase == null) { @@ -551,7 +517,7 @@ private String determineAuthPassPhrase(final Definition def) { * @return */ private String determinePrivPassPhrase(final Definition def) { - final String privPassPhrase = (def.getPrivacyPassphrase() == null ? m_config.getPrivacyPassphrase() : def.getPrivacyPassphrase()); + final String privPassPhrase = (def.getPrivacyPassphrase() == null ? getSnmpConfig().getPrivacyPassphrase() : def.getPrivacyPassphrase()); // Forcing a default is wrong, because if it is not explicitly defined, probably it means that it should not be used, and SNMP4J expect null for optional parameters. /* if (privPassPhrase == null) { @@ -569,7 +535,7 @@ private String determinePrivPassPhrase(final Definition def) { * @return */ private String determinePrivProtocol(final Definition def) { - final String authPrivProtocol = (def.getPrivacyProtocol() == null ? m_config.getPrivacyProtocol() : def.getPrivacyProtocol()); + final String authPrivProtocol = (def.getPrivacyProtocol() == null ? getSnmpConfig().getPrivacyProtocol() : def.getPrivacyProtocol()); if (authPrivProtocol == null && determinePrivPassPhrase(def) != null) { return SnmpAgentConfig.DEFAULT_PRIV_PROTOCOL; } @@ -594,15 +560,15 @@ private int determineSecurityLevel(final Definition def) { } // use a configured default security level next - if (m_config.hasSecurityLevel()) { - return m_config.getSecurityLevel(); + if (getSnmpConfig().hasSecurityLevel()) { + return getSnmpConfig().getSecurityLevel(); } // if no security level configuration exists use int securityLevel = SnmpAgentConfig.NOAUTH_NOPRIV; - final String authPassPhrase = (StringUtils.isBlank(def.getAuthPassphrase()) ? m_config.getAuthPassphrase() : def.getAuthPassphrase()); - final String privPassPhrase = (StringUtils.isBlank(def.getPrivacyPassphrase()) ? m_config.getPrivacyPassphrase() : def.getPrivacyPassphrase()); + final String authPassPhrase = (StringUtils.isBlank(def.getAuthPassphrase()) ? getSnmpConfig().getAuthPassphrase() : def.getAuthPassphrase()); + final String privPassPhrase = (StringUtils.isBlank(def.getPrivacyPassphrase()) ? getSnmpConfig().getPrivacyPassphrase() : def.getPrivacyPassphrase()); if (authPassPhrase == null) { securityLevel = SnmpAgentConfig.NOAUTH_NOPRIV; @@ -623,7 +589,7 @@ private int determineSecurityLevel(final Definition def) { * @return */ private int determinePort(final Definition def) { - return (def.getPort() == 0 ? (m_config.getPort() == 0 ? DEFAULT_SNMP_PORT : m_config.getPort()) : def.getPort()); + return (def.getPort() == 0 ? (getSnmpConfig().getPort() == 0 ? DEFAULT_SNMP_PORT : getSnmpConfig().getPort()) : def.getPort()); } /** @@ -633,7 +599,7 @@ private int determinePort(final Definition def) { */ private String determineEngineId(Definition def) { if (def.getEngineId() != null) return def.getEngineId(); - if (m_config.getEngineId() != null) return m_config.getEngineId(); + if (getSnmpConfig().getEngineId() != null) return getSnmpConfig().getEngineId(); return null; } @@ -644,7 +610,7 @@ private String determineEngineId(Definition def) { */ private String determineContextEngineId(Definition def) { if (def.getContextEngineId() != null) return def.getContextEngineId(); - if (m_config.getContextEngineId() != null) return m_config.getContextEngineId(); + if (getSnmpConfig().getContextEngineId() != null) return getSnmpConfig().getContextEngineId(); return null; } @@ -655,7 +621,7 @@ private String determineContextEngineId(Definition def) { */ private String determineContextName(Definition def) { if (def.getContextName() != null) return def.getContextName(); - if (m_config.getContextName() != null) return m_config.getContextName(); + if (getSnmpConfig().getContextName() != null) return getSnmpConfig().getContextName(); return null; } @@ -666,7 +632,7 @@ private String determineContextName(Definition def) { */ private String determineEnterpriseId(Definition def) { if (def.getEnterpriseId() != null) return def.getEnterpriseId(); - if (m_config.getEnterpriseId() != null) return m_config.getEnterpriseId(); + if (getSnmpConfig().getEnterpriseId() != null) return getSnmpConfig().getEnterpriseId(); return null; } @@ -677,12 +643,12 @@ private String determineEnterpriseId(Definition def) { */ private long determineTimeout(final Definition def) { final long timeout = SnmpAgentConfig.DEFAULT_TIMEOUT; - return (def.getTimeout() == 0 ? (m_config.getTimeout() == 0 ? timeout : m_config.getTimeout()) : def.getTimeout()); + return (def.getTimeout() == 0 ? (getSnmpConfig().getTimeout() == 0 ? timeout : getSnmpConfig().getTimeout()) : def.getTimeout()); } private int determineRetries(final Definition def) { final int retries = SnmpAgentConfig.DEFAULT_RETRIES; - return (def.getRetry() == 0 ? (m_config.getRetry() == 0 ? retries : m_config.getRetry()) : def.getRetry()); + return (def.getRetry() == 0 ? (getSnmpConfig().getRetry() == 0 ? retries : getSnmpConfig().getRetry()) : def.getRetry()); } /** @@ -704,10 +670,10 @@ private int determineVersion(final Definition def, final int requestedSnmpVersio String cfgVersion = "v1"; if (requestedSnmpVersion == VERSION_UNSPECIFIED) { if (def.getVersion() == null) { - if (m_config.getVersion() == null) { + if (getSnmpConfig().getVersion() == null) { return version; } else { - cfgVersion = m_config.getVersion(); + cfgVersion = getSnmpConfig().getVersion(); } } else { cfgVersion = def.getVersion(); @@ -735,7 +701,11 @@ private int determineVersion(final Definition def, final int requestedSnmpVersio public static SnmpConfig getSnmpConfig() { SnmpPeerFactory.getReadLock().lock(); try { - return m_config; + if (m_container == null) { + return m_config; + } else { + return m_container.getObject(); + } } finally { SnmpPeerFactory.getReadLock().unlock(); } @@ -755,7 +725,7 @@ public static SnmpConfig getSnmpConfig() { public void define(final SnmpEventInfo info) { getWriteLock().lock(); try { - final SnmpConfigManager mgr = new SnmpConfigManager(m_config); + final SnmpConfigManager mgr = new SnmpConfigManager(getSnmpConfig()); mgr.mergeIntoConfig(info.createDef()); } finally { getWriteLock().unlock(); @@ -776,7 +746,7 @@ public static String marshallConfig() { StringWriter writer = null; try { writer = new StringWriter(); - JaxbUtils.marshal(m_config, writer); + JaxbUtils.marshal(getSnmpConfig(), writer); marshalledConfig = writer.toString(); } finally { IOUtils.closeQuietly(writer); diff --git a/opennms-config/src/test/java/org/opennms/netmgt/config/SnmpEventInfoTest.java b/opennms-config/src/test/java/org/opennms/netmgt/config/SnmpEventInfoTest.java index 3c5e6ea35f74..39815e11b47d 100644 --- a/opennms-config/src/test/java/org/opennms/netmgt/config/SnmpEventInfoTest.java +++ b/opennms-config/src/test/java/org/opennms/netmgt/config/SnmpEventInfoTest.java @@ -31,17 +31,21 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.opennms.core.test.xml.XmlTest.assertXmlEquals; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; +import org.apache.commons.io.IOUtils; import org.junit.Test; import org.opennms.netmgt.EventConstants; import org.opennms.netmgt.config.snmp.Definition; @@ -50,6 +54,7 @@ import org.opennms.netmgt.model.discovery.IPAddressRangeSet; import org.opennms.netmgt.xml.event.Event; import org.opennms.netmgt.xml.event.Parm; +import org.springframework.core.io.AbstractResource; /** * JUnit tests for the configureSNMP event handling and optimization of @@ -392,7 +397,7 @@ public final void testModifySpecificInDef() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -431,7 +436,7 @@ public final void testModifySpecificInDefIPv6() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -446,6 +451,33 @@ public final void testModifySpecificInDefIPv6() throws Exception { } + private static class StringResource extends AbstractResource { + private final File m_tempFile; + + public StringResource(final String text) throws IOException { + m_tempFile = File.createTempFile("StringResource", ".txt"); + FileWriter fw = null; + try { + fw = new FileWriter(m_tempFile); + fw.write(text); + fw.flush(); + fw.close(); + } finally { + IOUtils.closeQuietly(fw); + } + } + + @Override + public String getDescription() { + return "string [" + m_tempFile.getPath() + "]"; + } + + @Override + public InputStream getInputStream() throws IOException { + return new FileInputStream(this.m_tempFile); + } + } + /** * This tests the merging of a new specific into a definition that already contains a specific * that is adjacent. The two specifics should be converted to a single range in the definition. @@ -472,7 +504,7 @@ public final void testAddAdjacentSpecificToDef() throws Exception { ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -515,7 +547,7 @@ public final void testAddAdjacentSpecificToDefIPv6() throws Exception { ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -553,7 +585,7 @@ public final void testAddSpecificBetweenAdjacentsSpecifics() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -592,7 +624,7 @@ public final void testAddSpecificBetweenAdjacentsSpecificsMostlyZeros() throws E "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -631,7 +663,7 @@ public final void testAddSpecificBetweenAdjacentsSpecificsIPv6() throws Exceptio "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -665,7 +697,7 @@ public final void testAddSpecificBetweenAdjacentSpecificAndRange() throws Except "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -708,7 +740,7 @@ public void testSplitRange() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -755,7 +787,7 @@ public void testRemoveSpecificFromRange() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -796,7 +828,7 @@ public void testRemoveSpecificNearEndOfRange() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -836,7 +868,7 @@ public void testRemoveSpecificAtEndOfRange() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -883,7 +915,7 @@ public void testRecombineSpecificIntoRange() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -930,7 +962,7 @@ public void testRemoveSpecificInSeparateDefWithNewRange() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -972,7 +1004,7 @@ public void testRemoveTrivialEntry() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -1002,7 +1034,7 @@ public void testInvalidRange() throws IOException { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); SnmpEventInfo info = new SnmpEventInfo(); info.setVersion("v1"); @@ -1045,7 +1077,7 @@ public void testNewSpecifcSameAsBeginInOldDef() throws Exception { ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -1093,7 +1125,7 @@ public void testOverlapsTwoRanges() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -1129,7 +1161,7 @@ public void testOverlapsTwoRangesAndCombinesThem(String firstIp, String lastIp) "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -1184,7 +1216,7 @@ public void testRecombineRanges() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -1229,7 +1261,7 @@ public void testRecombineRangesNonAdjacentRange() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -1272,7 +1304,7 @@ public final void testAddNewSpecificToConfig() throws Exception { "\n" + ""; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); @@ -1303,7 +1335,7 @@ public void testEmptySnmpConfigAddDefinitionWhichMatchesDefaults() throws IOExce "\n" + "\n"; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); SnmpEventInfo info = new SnmpEventInfo(); @@ -1338,7 +1370,7 @@ public void testMaxRepetitionsAndMaxVarsPerPdu() throws IOException { "\n"; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); SnmpEventInfo info = new SnmpEventInfo(); @@ -1371,7 +1403,7 @@ public void testProxyHost() throws Exception { "\n"; - SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml))); assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig()); SnmpEventInfo info = new SnmpEventInfo(); diff --git a/opennms-import/src/test/java/org/opennms/netmgt/importer/operations/UpdateOperationTest.java b/opennms-import/src/test/java/org/opennms/netmgt/importer/operations/UpdateOperationTest.java index e3747d89fb40..42151a9ba894 100644 --- a/opennms-import/src/test/java/org/opennms/netmgt/importer/operations/UpdateOperationTest.java +++ b/opennms-import/src/test/java/org/opennms/netmgt/importer/operations/UpdateOperationTest.java @@ -35,6 +35,7 @@ import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.ValidationException; +import org.springframework.core.io.InputStreamResource; import org.opennms.netmgt.config.SnmpPeerFactory; import org.opennms.netmgt.importer.config.types.InterfaceSnmpPrimaryType; @@ -60,7 +61,7 @@ public final void testScanForSnmpData() throws MarshalException, ValidationExcep "\n" + "").getBytes()); - SnmpPeerFactory.setInstance(new SnmpPeerFactory(in)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new InputStreamResource(in))); AbstractSaveOrUpdateOperation op = new UpdateOperation(new Integer(1), "imported:", "1", "node1", "theoffice", "pittsboro"); diff --git a/opennms-services/src/test/java/org/opennms/netmgt/collectd/CollectorConfigDaoImplTest.java b/opennms-services/src/test/java/org/opennms/netmgt/collectd/CollectorConfigDaoImplTest.java index 5d6e2663e2e6..f9d5093d1464 100644 --- a/opennms-services/src/test/java/org/opennms/netmgt/collectd/CollectorConfigDaoImplTest.java +++ b/opennms-services/src/test/java/org/opennms/netmgt/collectd/CollectorConfigDaoImplTest.java @@ -100,7 +100,7 @@ private CollectorConfigDao initialize() throws IOException, MarshalException, Va stream.close(); stream = getInputStreamForFile("/org/opennms/netmgt/config/snmp-config.xml"); - SnmpPeerFactory.setInstance(new SnmpPeerFactory(stream)); + SnmpPeerFactory.setInstance(new SnmpPeerFactory(new InputStreamResource(stream))); stream.close(); stream = getInputStreamForFile("/org/opennms/netmgt/config/datacollection-config.xml"); diff --git a/opennms-webapp/src/main/java/org/opennms/web/rest/SnmpConfigRestService.java b/opennms-webapp/src/main/java/org/opennms/web/rest/SnmpConfigRestService.java index 8ed4bb52ac60..419ba0e76584 100644 --- a/opennms-webapp/src/main/java/org/opennms/web/rest/SnmpConfigRestService.java +++ b/opennms-webapp/src/main/java/org/opennms/web/rest/SnmpConfigRestService.java @@ -148,7 +148,14 @@ public SnmpInfo getSnmpInfo(@PathParam("ipAddr") String ipAddr) { public Response setSnmpInfo(@PathParam("ipAddr") final String ipAddress, final SnmpInfo snmpInfo) { writeLock(); try { - final SnmpEventInfo eventInfo = snmpInfo.createEventInfo(ipAddress); + final SnmpEventInfo eventInfo; + if (ipAddress.contains("-")) { + final String[] addrs = SnmpConfigRestService.getAddresses(ipAddress); + eventInfo = snmpInfo.createEventInfo(addrs[0], addrs[1]); + } else { + eventInfo = snmpInfo.createEventInfo(ipAddress); + } + m_snmpPeerFactory.define(eventInfo); SnmpPeerFactory.saveCurrent(); //TODO: this shouldn't be a static call return Response.seeOther(getRedirectUri(m_uriInfo)).build(); @@ -175,7 +182,13 @@ public Response updateInterface(@PathParam("ipAddr") final String ipAddress, fin try { final SnmpInfo info = new SnmpInfo(); setProperties(params, info); - final SnmpEventInfo eventInfo = info.createEventInfo(ipAddress); + final SnmpEventInfo eventInfo; + if (ipAddress.contains("-")) { + final String[] addrs = SnmpConfigRestService.getAddresses(ipAddress); + eventInfo = info.createEventInfo(addrs[0], addrs[1]); + } else { + eventInfo = info.createEventInfo(ipAddress); + } m_snmpPeerFactory.define(eventInfo); SnmpPeerFactory.saveCurrent(); return Response.seeOther(getRedirectUri(m_uriInfo)).build(); @@ -186,4 +199,11 @@ public Response updateInterface(@PathParam("ipAddr") final String ipAddress, fin } } + protected static String[] getAddresses(final String input) { + if (input == null || input.trim().isEmpty()) { + return new String[] { null, null }; + } else { + return input.trim().split("-", 2); + } + } } diff --git a/opennms-webapp/src/test/java/org/opennms/web/rest/SnmpConfigRestServiceTest.java b/opennms-webapp/src/test/java/org/opennms/web/rest/SnmpConfigRestServiceTest.java index a4d33ce0d634..e85266ffa407 100644 --- a/opennms-webapp/src/test/java/org/opennms/web/rest/SnmpConfigRestServiceTest.java +++ b/opennms-webapp/src/test/java/org/opennms/web/rest/SnmpConfigRestServiceTest.java @@ -29,6 +29,7 @@ package org.opennms.web.rest; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import java.io.File; @@ -200,72 +201,166 @@ public void testSetNewValueForSnmpV2c() throws Exception { dumpConfig(); } - @Test - public void testSetNewValueForSnmpV3() throws Exception { - String url = "/snmpConfig/1.1.1.1"; + @Test + public void testSetNewValueForSnmpV3() throws Exception { + String url = "/snmpConfig/1.1.1.1"; - // Testing GET Collection - SnmpInfo changedConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class); - SnmpInfo expectedConfig = createSnmpInfoWithDefaultsForSnmpV3("1.1.1.1"); - assertConfiguration(expectedConfig, changedConfig); // check if expected defaults matches actual defaults - - // change values - changedConfig.setAuthPassPhrase("authPassPhrase"); - changedConfig.setAuthProtocol("authProtocol"); - changedConfig.setReadCommunity("readCommunity"); - changedConfig.setWriteCommunity("writeCommunity"); - changedConfig.setContextEngineId("contextEngineId"); - changedConfig.setContextName("contextName"); - changedConfig.setEngineId("engineId"); - changedConfig.setEnterpriseId("enterpriseId"); - changedConfig.setMaxRepetitions(1000); - changedConfig.setMaxVarsPerPdu(2000); - changedConfig.setPort(3000); - changedConfig.setProxyHost("127.0.0.1"); - changedConfig.setPrivPassPhrase("privPassPhrase"); - changedConfig.setPrivProtocol("privProtocol"); - changedConfig.setRetries(4000); - changedConfig.setSecurityLevel(5000); - changedConfig.setSecurityName("securityName"); - changedConfig.setTimeout(6000); - changedConfig.setVersion("v3"); - changedConfig.setMaxRequestSize(7000); + // Testing GET Collection + SnmpInfo changedConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class); + SnmpInfo expectedConfig = createSnmpInfoWithDefaultsForSnmpV3("1.1.1.1"); + assertConfiguration(expectedConfig, changedConfig); // check if expected defaults matches actual defaults + + // change values + changedConfig.setAuthPassPhrase("authPassPhrase"); + changedConfig.setAuthProtocol("authProtocol"); + changedConfig.setReadCommunity("readCommunity"); + changedConfig.setWriteCommunity("writeCommunity"); + changedConfig.setContextEngineId("contextEngineId"); + changedConfig.setContextName("contextName"); + changedConfig.setEngineId("engineId"); + changedConfig.setEnterpriseId("enterpriseId"); + changedConfig.setMaxRepetitions(1000); + changedConfig.setMaxVarsPerPdu(2000); + changedConfig.setPort(3000); + changedConfig.setProxyHost("127.0.0.1"); + changedConfig.setPrivPassPhrase("privPassPhrase"); + changedConfig.setPrivProtocol("privProtocol"); + changedConfig.setRetries(4000); + changedConfig.setSecurityLevel(5000); + changedConfig.setSecurityName("securityName"); + changedConfig.setTimeout(6000); + changedConfig.setVersion("v3"); + changedConfig.setMaxRequestSize(7000); - // store them via REST - putXmlObject(m_jaxbContext, url, 303, changedConfig, "/snmpConfig/1.1.1.1"); - - // prepare expected Result - expectedConfig = new SnmpInfo(); - expectedConfig.setAuthPassPhrase("authPassPhrase"); - expectedConfig.setAuthProtocol("authProtocol"); - expectedConfig.setContextEngineId("contextEngineId"); - expectedConfig.setContextName("contextName"); - expectedConfig.setEngineId("engineId"); - expectedConfig.setEnterpriseId("enterpriseId"); - expectedConfig.setMaxRepetitions(1000); - expectedConfig.setMaxVarsPerPdu(2000); - expectedConfig.setPort(3000); - expectedConfig.setProxyHost("127.0.0.1"); - expectedConfig.setPrivPassPhrase("privPassPhrase"); - expectedConfig.setPrivProtocol("privProtocol"); - expectedConfig.setRetries(4000); - expectedConfig.setSecurityLevel(5000); - expectedConfig.setSecurityName("securityName"); - expectedConfig.setTimeout(6000); - expectedConfig.setVersion("v3"); - expectedConfig.setMaxRequestSize(7000); - expectedConfig.setReadCommunity(null); - expectedConfig.setWriteCommunity(null); - - // read via REST - SnmpInfo newConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class); - - // check ... - assertConfiguration(expectedConfig, newConfig); // ... if changes were made + // store them via REST + putXmlObject(m_jaxbContext, url, 303, changedConfig, "/snmpConfig/1.1.1.1"); + + // prepare expected Result + expectedConfig = new SnmpInfo(); + expectedConfig.setAuthPassPhrase("authPassPhrase"); + expectedConfig.setAuthProtocol("authProtocol"); + expectedConfig.setContextEngineId("contextEngineId"); + expectedConfig.setContextName("contextName"); + expectedConfig.setEngineId("engineId"); + expectedConfig.setEnterpriseId("enterpriseId"); + expectedConfig.setMaxRepetitions(1000); + expectedConfig.setMaxVarsPerPdu(2000); + expectedConfig.setPort(3000); + expectedConfig.setProxyHost("127.0.0.1"); + expectedConfig.setPrivPassPhrase("privPassPhrase"); + expectedConfig.setPrivProtocol("privProtocol"); + expectedConfig.setRetries(4000); + expectedConfig.setSecurityLevel(5000); + expectedConfig.setSecurityName("securityName"); + expectedConfig.setTimeout(6000); + expectedConfig.setVersion("v3"); + expectedConfig.setMaxRequestSize(7000); + expectedConfig.setReadCommunity(null); + expectedConfig.setWriteCommunity(null); + + // read via REST + SnmpInfo newConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class); + + // check ... + assertConfiguration(expectedConfig, newConfig); // ... if changes were made - dumpConfig(); + dumpConfig(); + } + + @Test + public void testGetAddresses() throws Exception { + String[] addrs = SnmpConfigRestService.getAddresses(null); + assertEquals(2, addrs.length); + assertEquals(null, addrs[0]); + + addrs = SnmpConfigRestService.getAddresses(" "); + assertEquals(2, addrs.length); + assertEquals(null, addrs[0]); + + addrs = SnmpConfigRestService.getAddresses("192.168.0.1"); + assertEquals(1, addrs.length); + assertEquals("192.168.0.1", addrs[0]); + + addrs = SnmpConfigRestService.getAddresses("192.168.0.1-192.168.0.255"); + assertEquals(2, addrs.length); + assertEquals("192.168.0.1", addrs[0]); + assertEquals("192.168.0.255", addrs[1]); } - + + @Test + public void testSetRanges() throws Exception { + String url = "/snmpConfig/1.1.1.1"; + String urlRange = "/snmpConfig/1.1.1.1-2.2.2.2"; + + // Testing GET Collection + SnmpInfo changedConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class); + SnmpInfo expectedConfig = createSnmpInfoWithDefaultsForSnmpV3("1.1.1.1"); + assertConfiguration(expectedConfig, changedConfig); // check if expected defaults matches actual defaults + + // change values + changedConfig.setAuthPassPhrase("authPassPhrase"); + changedConfig.setAuthProtocol("authProtocol"); + changedConfig.setReadCommunity("readCommunity"); + changedConfig.setWriteCommunity("writeCommunity"); + changedConfig.setContextEngineId("contextEngineId"); + changedConfig.setContextName("contextName"); + changedConfig.setEngineId("engineId"); + changedConfig.setEnterpriseId("enterpriseId"); + changedConfig.setMaxRepetitions(1000); + changedConfig.setMaxVarsPerPdu(2000); + changedConfig.setPort(3000); + changedConfig.setProxyHost("127.0.0.1"); + changedConfig.setPrivPassPhrase("privPassPhrase"); + changedConfig.setPrivProtocol("privProtocol"); + changedConfig.setRetries(4000); + changedConfig.setSecurityLevel(5000); + changedConfig.setSecurityName("securityName"); + changedConfig.setTimeout(6000); + changedConfig.setVersion("v3"); + changedConfig.setMaxRequestSize(7000); + + // store them via REST + putXmlObject(m_jaxbContext, urlRange, 303, changedConfig, urlRange); + + // prepare expected Result + expectedConfig = new SnmpInfo(); + expectedConfig.setAuthPassPhrase("authPassPhrase"); + expectedConfig.setAuthProtocol("authProtocol"); + expectedConfig.setContextEngineId("contextEngineId"); + expectedConfig.setContextName("contextName"); + expectedConfig.setEngineId("engineId"); + expectedConfig.setEnterpriseId("enterpriseId"); + expectedConfig.setMaxRepetitions(1000); + expectedConfig.setMaxVarsPerPdu(2000); + expectedConfig.setPort(3000); + expectedConfig.setProxyHost("127.0.0.1"); + expectedConfig.setPrivPassPhrase("privPassPhrase"); + expectedConfig.setPrivProtocol("privProtocol"); + expectedConfig.setRetries(4000); + expectedConfig.setSecurityLevel(5000); + expectedConfig.setSecurityName("securityName"); + expectedConfig.setTimeout(6000); + expectedConfig.setVersion("v3"); + expectedConfig.setMaxRequestSize(7000); + expectedConfig.setReadCommunity(null); + expectedConfig.setWriteCommunity(null); + + // read via REST + SnmpInfo newConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class); + + // check ... + assertConfiguration(expectedConfig, newConfig); // ... if changes were made + + SnmpInfo otherConfig = createSnmpInfoWithDefaultsForSnmpV3("3.3.3.3"); + assertFalse(newConfig.equals(otherConfig)); + + expectedConfig = createSnmpInfoWithDefaultsForSnmpV3("1.2.3.4"); + newConfig = getXmlObject(m_jaxbContext, "/snmpConfig/1.2.3.4", 200, SnmpInfo.class); + assertEquals(expectedConfig, newConfig); + + dumpConfig(); + } + private void dumpConfig() throws Exception { IOUtils.copy(new FileInputStream(m_snmpConfigFile), System.out); }