m_callback;
/**
* This member is set to true if the configuration file has been loaded.
*/
private static boolean m_loaded = false;
- /**
- * Private constructor
- *
- * @exception java.io.IOException
- * Thrown if the specified config file cannot be read
- */
- private SnmpPeerFactory(final File configFile) throws IOException {
- this(new FileSystemResource(configFile));
+ private static final int VERSION_UNSPECIFIED = -1;
+
+ protected SnmpConfig loadConfig(final Resource resource) {
+ return JaxbUtils.unmarshal(SnmpConfig.class, resource);
}
/**
@@ -116,54 +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();
- }
- }
+ final SnmpConfig config = loadConfig(resource);
- /**
- * Constructor for SnmpPeerFactory.
- *
- * @param rdr a {@link java.io.Reader} object.
- * @throws java.io.IOException 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);
- } 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);
+ 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) {
+ LOG.debug("No file associated with resource {}, 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();
}
}
-
+
protected static Lock getReadLock() {
return m_readLock;
}
@@ -192,7 +169,9 @@ public static void init() throws IOException {
final File cfgFile = getFile();
LOG.debug("init: config file path: {}", 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();
@@ -212,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();
FileOutputStream out = null;
@@ -226,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);
@@ -318,7 +299,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 == SnmpAgentConfig.VERSION_UNSPECIFIED) {
agentConfig.setVersion(SnmpAgentConfig.DEFAULT_VERSION);
@@ -331,11 +312,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);
final AddressSnmpConfigVisitor visitor = new AddressSnmpConfigVisitor(agentInetAddress);
- m_config.visit(visitor);
+ getSnmpConfig().visit(visitor);
final Definition matchingDef = visitor.getDefinition();
if (matchingDef != null) {
setSnmpAgentConfig(agentConfig, matchingDef, requestedSnmpVersion);
@@ -347,7 +328,7 @@ private SnmpAgentConfig getAgentConfig(final InetAddress agentInetAddress, final
}
private void setSnmpAgentConfig(final SnmpAgentConfig agentConfig, final Definition def, final int requestedSnmpVersion) {
- int version = getVersionCode(def, m_config, requestedSnmpVersion);
+ int version = getVersionCode(def, getSnmpConfig(), requestedSnmpVersion);
setCommonAttributes(agentConfig, def, version);
agentConfig.setSecurityLevel(def.getSecurityLevel());
@@ -411,7 +392,11 @@ public int getVersionCode(final Definition def, final SnmpConfig config, final i
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();
}
@@ -430,7 +415,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();
@@ -450,7 +435,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 8cc40741c77d..273b96954031 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.core.network.IPAddress;
import org.opennms.core.network.IPAddressRange;
@@ -50,6 +54,7 @@
import org.opennms.netmgt.config.snmp.Definition;
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 Except
"\n" +
"\n";
- SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml));
+ SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml)));
assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig());
SnmpEventInfo info = new SnmpEventInfo();
@@ -1337,7 +1369,7 @@ public void testMaxRepetitionsAndMaxVarsPerPdu() throws Exception {
"\n";
- SnmpPeerFactory.setInstance(new SnmpPeerFactory(snmpConfigXml));
+ SnmpPeerFactory.setInstance(new SnmpPeerFactory(new StringResource(snmpConfigXml)));
assertXmlEquals(snmpConfigXml, SnmpPeerFactory.marshallConfig());
SnmpEventInfo info = new SnmpEventInfo();
@@ -1370,7 +1402,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-services/src/main/java/org/opennms/netmgt/collectd/Collectd.java b/opennms-services/src/main/java/org/opennms/netmgt/collectd/Collectd.java
index 6fa5b0c93756..d90341c44a82 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
@@ -1501,4 +1501,9 @@ private void instantiateCollectors() {
public static String getLoggingCategory() {
return LOG4J_CATEGORY;
}
+
+ 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 9b2ac017a63f..40db27480012 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();
}
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 4fa74c8a057b..04bb6f8de37d 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
@@ -99,7 +99,7 @@ private void initialize() throws IOException, MarshalException, ValidationExcept
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/gwt/web/ui/CoreWeb.gwt.xml b/opennms-webapp/src/main/java/org/opennms/gwt/web/ui/CoreWeb.gwt.xml
index 9b0ea7fe1f10..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/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 d63a8cbd8136..30b9c7ed198e 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;
@@ -228,72 +229,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);
}