NIFI-2521 This closes #815. removed test classes with questionable origin and licensing.

This commit is contained in:
joewitt 2016-08-09 00:33:45 -04:00
parent 7a1f749f69
commit b2401522ea
8 changed files with 0 additions and 1776 deletions

View File

@ -1,391 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.snmp4j.AbstractTarget;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.UserTarget;
import org.snmp4j.agent.mo.DefaultMOFactory;
import org.snmp4j.agent.mo.MOAccessImpl;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
/**
* Class to test SNMP Get processor
*/
public class GetSNMPTest {
/** agent for version v1 */
private static TestSnmpAgentV1 agentv1 = null;
/** agent for version v2c */
private static TestSnmpAgentV2c agentv2c = null;
/** OID for system description */
private static final OID sysDescr = new OID("1.3.6.1.2.1.1.1.0");
/** value we set for system description at set-up */
private static final String value = "MySystemDescr";
/** OID for write only */
private static final OID writeOnlyOID = new OID("1.3.6.1.2.1.1.3.0");
/** value we set for write only at set-up */
private static final int writeOnlyValue = 1;
/**
* Method to set up different SNMP agents
* @throws Exception Exception
*/
@BeforeClass
public static void setUp() throws Exception {
agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start();
agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
agentv2c.registerManagedObject(
DefaultMOFactory.getInstance().createScalar(sysDescr,
MOAccessImpl.ACCESS_READ_WRITE,
new OctetString(value)));
agentv2c.registerManagedObject(
DefaultMOFactory.getInstance().createScalar(writeOnlyOID,
MOAccessImpl.ACCESS_WRITE_ONLY,
new Integer32(writeOnlyValue)));
agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start();
agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
agentv1.registerManagedObject(
DefaultMOFactory.getInstance().createScalar(sysDescr,
MOAccessImpl.ACCESS_READ_WRITE,
new OctetString(value)));
}
/**
* Method to close SNMP Agent once the tests are completed
* @throws Exception Exception
*/
@AfterClass
public static void tearDown() throws Exception {
agentv1.stop();
agentv2c.stop();
}
/**
* Test to check FlowFile handling when performing a SNMP Get.
* First we set a new value for the OID we want to request, then we
* request this OID and we check that the returned value if the one
* we set just before.
* @throws Exception Exception
*/
@Test
public void validateSuccessfullSnmpSetGetv2c() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(sysDescr), new OctetString("test")));
pdu.setType(PDU.SET);
ResponseEvent response = setter.set(pdu);
if(response.getResponse().getErrorStatus() != PDU.noError ) {
fail();
}
Thread.sleep(200);
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
final MockFlowFile successFF = runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).get(0);
assertNotNull(successFF);
assertEquals("test", successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + sysDescr.toString() + SNMPUtils.SNMP_PROP_DELIMITER + "4"));
pubProc.close();
}
}
/**
* Test to check FlowFile handling when performing a SNMP Get.
* First we set a new value for the OID we want to request, then we
* request this OID and we check that the returned value if the one
* we set just before.
* @throws Exception Exception
*/
@Test
public void validateSuccessfullSnmpSetGetv1() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1);
try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(sysDescr), new OctetString("test")));
pdu.setType(PDU.SET);
ResponseEvent response = setter.set(pdu);
if(response.getResponse().getErrorStatus() != PDU.noError ) {
fail();
}
Thread.sleep(200);
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(agentv1.getPort()));
runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1");
runner.run();
Thread.sleep(200);
final MockFlowFile successFF = runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).get(0);
assertNotNull(successFF);
assertEquals("test", successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + sysDescr.toString() + SNMPUtils.SNMP_PROP_DELIMITER + "4"));
pubProc.close();
}
}
/**
* Test the unauthorized case during a SNMP get request
* @throws Exception Exception
*/
@Test
public void errorUnauthorizedSnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, writeOnlyOID.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).isEmpty());
final MockFlowFile failFF = runner.getFlowFilesForRelationship(GetSNMP.REL_FAILURE).get(0);
assertNotNull(failFF);
assertEquals("Null", failFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + writeOnlyOID.toString() + SNMPUtils.SNMP_PROP_DELIMITER + "5"));
assertEquals("No access", failFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "errorStatusText"));
}
/**
* Test the timeout case during a SNMP get request
* @throws Exception Exception
*/
@Test
public void errorTimeoutSnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(SNMPTestUtil.availablePort()));
runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).isEmpty());
assertTrue(runner.getFlowFilesForRelationship(GetSNMP.REL_FAILURE).isEmpty());
}
/**
* Test the case noSuchObject during a SNMP get request
* @throws Exception Exception
*/
@Test
public void errorNotExistingOIDSnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0");
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
final MockFlowFile successFF = runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).get(0);
assertNotNull(successFF);
assertEquals("noSuchObject", successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "1.3.6.1.2.1.1.2.0" + SNMPUtils.SNMP_PROP_DELIMITER + "128"));
}
/**
* Test the case with not existing community during a SNMP get request
* @throws Exception Exception
*/
@Test
public void errorCommunitySnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("errorCommunity", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0");
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(GetSNMP.SNMP_COMMUNITY, "errorCommunity");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).isEmpty());
assertTrue(runner.getFlowFilesForRelationship(GetSNMP.REL_FAILURE).isEmpty());
}
/**
* Method to test SNMP v3 cases
* @throws Exception Exception
*/
@Test
public void validateSnmpVersion3() throws Exception {
int port = SNMPTestUtil.availablePort();
Thread thread= new Thread(new Runnable() {
@Override
public void run() {
TestSnmpAgentV3.main(new String[]{"0.0.0.0/" + port});
}
});
thread.start();
DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transportMapping);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transportMapping.listen();
this.executeCase(snmp, port, "SHADES", "authPriv", "SHA", "SHADESPassword", "DES", "SHADESPassword");
this.executeCase(snmp, port, "MD5DES", "authPriv", "MD5", "MD5DESAuthPassword", "DES", "MD5DESPrivPassword");
this.executeCase(snmp, port, "SHAAES128", "authPriv", "SHA", "SHAAES128AuthPassword", "AES128", "SHAAES128PrivPassword");
}
/**
* Method to test a specific configuration for SNMP V3
* @param snmp SNMP
* @param port Port
* @param securityName Security name
* @param securityLevel security level
* @param authProt authentication protocol
* @param authPwd authentication password
* @param privProt private protocol
* @param privPwd private password
* @throws InterruptedException exception
*/
private void executeCase(Snmp snmp, int port, String securityName, String securityLevel, String authProt, String authPwd, String privProt, String privPwd) throws InterruptedException {
UserTarget target = SNMPUtilsTest.prepareUser(snmp, "127.0.0.1/" + port, SNMPUtils.getSecLevel(securityLevel),
securityName, SNMPUtils.getAuth(authProt), SNMPUtils.getPriv(privProt), authPwd, privPwd);
this.testTarget(snmp, port, target, securityName, securityLevel, authProt, authPwd, privProt, privPwd);
}
/**
* Method to test a specific configuration for SNMP V3
* @param snmp SNMP
* @param port Port
* @param target target
* @param securityName Security name
* @param securityLevel security level
* @param authProt authentication protocol
* @param authPwd authentication password
* @param privProt private protocol
* @param privPwd private password
* @throws InterruptedException exception
*/
private void testTarget(Snmp snmp, int port, UserTarget target, String securityName, String securityLevel,
String authProt, String authPwd, String privProt, String privPwd) throws InterruptedException {
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(port));
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv3");
runner.setProperty(GetSNMP.SNMP_SECURITY_NAME, securityName);
runner.setProperty(GetSNMP.SNMP_SECURITY_LEVEL, securityLevel);
runner.setProperty(GetSNMP.SNMP_AUTH_PROTOCOL, authProt);
if(authPwd != null) {
runner.setProperty(GetSNMP.SNMP_AUTH_PASSWORD, authPwd);
}
runner.setProperty(GetSNMP.SNMP_PRIV_PROTOCOL, privProt);
if(privPwd != null) {
runner.setProperty(GetSNMP.SNMP_PRIV_PASSWORD, privPwd);
}
runner.run();
Thread.sleep(500);
// Assert that a file has been transferred before attempting to access
runner.assertTransferCount(GetSNMP.REL_SUCCESS, 1);
final MockFlowFile successFF = runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).get(0);
assertNotNull(successFF);
assertTrue(successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + sysDescr.toString() + SNMPUtils.SNMP_PROP_DELIMITER + "4").startsWith("SNMP4J-Agent"));
}
/**
* Local extension of SNMP Getter
*/
private class LocalGetSnmp extends GetSNMP {
/** SNMP */
private Snmp snmp;
/** Target to request */
private AbstractTarget target;
/**
* Constructor
* @param snmp SNMP
* @param target Target
*/
public LocalGetSnmp(Snmp snmp, AbstractTarget target) {
this.snmp = snmp;
this.target = target;
}
}
}

View File

@ -1,123 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.agent.mo.DefaultMOFactory;
import org.snmp4j.agent.mo.MOAccessImpl;
import org.snmp4j.agent.mo.MOFactory;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
/**
* Test class used to check SNMP Get processor
*/
public class SNMPGetTest {
/** agent for test with SNMP v2c */
private static TestSnmpAgentV2c agentv2c = null;
/** agent for test with SNMP v1 */
private static TestSnmpAgentV1 agentv1 = null;
/** OID to request */
private static final OID sysDescr = new OID(".1.3.6.1.2.1.1.1.0");
/** value we are supposed to retrieve */
private static final String value = "MySystemDescr";
/**
* Method to set up different SNMP agents
* @throws Exception Exception
*/
@BeforeClass
public static void setUp() throws Exception {
MOFactory factory = DefaultMOFactory.getInstance();
agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start();
agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
agentv1.registerManagedObject(factory.createScalar(sysDescr,
MOAccessImpl.ACCESS_READ_ONLY,
new OctetString(value)));
agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start();
agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
agentv2c.registerManagedObject(factory.createScalar(sysDescr,
MOAccessImpl.ACCESS_READ_ONLY,
new OctetString(value)));
}
/**
* Method to close SNMP Agent once the tests are completed
* @throws Exception Exception
*/
@AfterClass
public static void tearDown() throws Exception {
agentv1.stop();
agentv2c.stop();
}
/**
* Method to test successful SNMP Get in case of v2c
* @throws IOException IO Exception
* @throws TimeoutException Timeout exception
*/
@Test
public void validateSuccessfulSnmpGetVersion2c() throws IOException, TimeoutException {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
try (SNMPGetter getter = new SNMPGetter(snmp, target, sysDescr)) {
ResponseEvent response = getter.get();
if(response.getResponse() == null) {
fail();
}
assertEquals(value, response.getResponse().get(0).getVariable().toString());
}
}
/**
* Method to test successful SNMP Get in case of v1
* @throws IOException IO Exception
* @throws TimeoutException Timeout exception
*/
@Test
public void validateSuccessfulSnmpGetVersion1() throws IOException, TimeoutException {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1);
try (SNMPGetter getter = new SNMPGetter(snmp, target, sysDescr)) {
ResponseEvent response = getter.get();
if(response.getResponse() == null) {
fail();
}
assertEquals(value, response.getResponse().get(0).getVariable().toString());
}
}
}

View File

@ -1,121 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.TreeEvent;
/**
* Test class used to check SNMP Walk part of the SNMP Get processor
*/
public class SNMPWalkTest {
/** agent for test with SNMP v2c */
private static TestSnmpAgentV2c agentv2c = null;
/** agent for test with SNMP v1 */
private static TestSnmpAgentV1 agentv1 = null;
/** Root OID to perform a WALK */
private static final OID root = new OID("1.3.6.1.2.1.1");
/**
* Method to set up different SNMP agents
* @throws Exception Exception
*/
@BeforeClass
public static void setUp() throws Exception {
agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start();
agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start();
}
/**
* Method to close SNMP Agent once the tests are completed
* @throws Exception Exception
*/
@AfterClass
public static void tearDown() throws Exception {
agentv1.stop();
agentv2c.stop();
}
/**
* Method to test successful SNMP Walk in case of v2c
* @throws IOException IO Exception
* @throws TimeoutException Timeout exception
*/
@Test
public void validateSuccessfulSnmpWalkVersion2c() throws IOException, TimeoutException {
DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping();
transportMapping.listen();
Snmp snmp = new Snmp(transportMapping);
CommunityTarget target = new CommunityTarget();
target.setVersion(SnmpConstants.version2c);
target.setCommunity(new OctetString("public"));
target.setAddress(new UdpAddress("127.0.0.1/" + agentv2c.getPort()));
target.setRetries(0);
target.setTimeout(500);
try (SNMPGetter getter = new SNMPGetter(snmp, target, root)) {
List<TreeEvent> response = getter.walk();
assertEquals(response.size(), 1);
}
}
/**
* Method to test successful SNMP Walk in case of v1
* @throws IOException IO Exception
* @throws TimeoutException Timeout exception
*/
@Test
public void validateSuccessfullSnmpWalkVersion1() throws IOException, TimeoutException {
DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping();
transportMapping.listen();
Snmp snmp = new Snmp(transportMapping);
CommunityTarget target = new CommunityTarget();
target.setVersion(SnmpConstants.version1);
target.setCommunity(new OctetString("public"));
target.setAddress(new UdpAddress("127.0.0.1/" + agentv1.getPort()));
target.setRetries(0);
target.setTimeout(500);
try (SNMPGetter getter = new SNMPGetter(snmp, target, root)) {
List<TreeEvent> response = getter.walk();
assertEquals(response.size(), 9);
}
}
}

View File

@ -1,332 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.snmp4j.AbstractTarget;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.agent.mo.DefaultMOFactory;
import org.snmp4j.agent.mo.MOAccessImpl;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
/**
* Class to test SNMP Get processor
*/
public class SetSNMPTest {
/** agent for version v1 */
private static TestSnmpAgentV1 agentv1 = null;
/** agent for version v2c */
private static TestSnmpAgentV2c agentv2c = null;
/** OID for system description */
private static final OID sysDescr = new OID("1.3.6.1.2.1.1.1.0");
/** value we set for system description at set-up */
private static final String value = "MySystemDescr";
/** OID for read only access */
private static final OID readOnlyOID = new OID("1.3.6.1.2.1.1.3.0");
/** value we set for read only at set-up */
private static final int readOnlyValue = 1;
/** OID for write only access */
private static final OID writeOnlyOID = new OID("1.3.6.1.2.1.1.3.0");
/** value we set for write only at set-up */
private static final int writeOnlyValue = 1;
/**
* Method to set up different SNMP agents
* @throws Exception Exception
*/
@BeforeClass
public static void setUp() throws Exception {
agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start();
agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
agentv2c.registerManagedObject(
DefaultMOFactory.getInstance().createScalar(sysDescr,
MOAccessImpl.ACCESS_READ_WRITE,
new OctetString(value)));
agentv2c.registerManagedObject(
DefaultMOFactory.getInstance().createScalar(readOnlyOID,
MOAccessImpl.ACCESS_READ_ONLY,
new Integer32(readOnlyValue)));
agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start();
agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
agentv1.registerManagedObject(
DefaultMOFactory.getInstance().createScalar(sysDescr,
MOAccessImpl.ACCESS_READ_WRITE,
new OctetString(value)));
agentv1.registerManagedObject(
DefaultMOFactory.getInstance().createScalar(writeOnlyOID,
MOAccessImpl.ACCESS_WRITE_ONLY,
new Integer32(writeOnlyValue)));
}
/**
* Method to close SNMP Agent once the tests are completed
* @throws Exception Exception
*/
@AfterClass
public static void tearDown() throws Exception {
agentv1.stop();
agentv2c.stop();
}
/**
* Test the type success case during a SNMP set request
* @throws Exception Exception
*/
@Test
public void successTypeSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1);
SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
int syntax = new Integer32(writeOnlyValue).getSyntax();
Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
attributes.put("snmp$" + writeOnlyOID.toString() + "$" + syntax, String.valueOf(writeOnlyValue));
runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1");
runner.setProperty(SetSNMP.PORT, String.valueOf(agentv1.getPort()));
runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
runner.run();
Thread.sleep(200);
final MockFlowFile successFF = runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).get(0);
assertNotNull(successFF);
pubProc.close();
}
/**
* Test the type error case during a SNMP set request
* @throws Exception Exception
*/
@Test
public void errorTypeSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1);
SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
attributes.put("snmp$" + writeOnlyOID.toString(), String.valueOf(writeOnlyValue));
runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1");
runner.setProperty(SetSNMP.PORT, String.valueOf(agentv1.getPort()));
runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).isEmpty());
final MockFlowFile failFF = runner.getFlowFilesForRelationship(SetSNMP.REL_FAILURE).get(0);
assertNotNull(failFF);
assertEquals(String.valueOf(writeOnlyValue), failFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + writeOnlyOID.toString()));
assertEquals("Bad value", failFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "error"));
}
/**
* Test the unauthorized case during a SNMP set request
* @throws Exception Exception
*/
@Test
public void errorUnauthorizedSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
attributes.put("snmp$" + readOnlyOID.toString(), String.valueOf(readOnlyValue));
runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1");
runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).isEmpty());
final MockFlowFile failFF = runner.getFlowFilesForRelationship(SetSNMP.REL_FAILURE).get(0);
assertNotNull(failFF);
assertEquals(String.valueOf(readOnlyValue), failFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + readOnlyOID.toString()));
assertEquals("Not writable", failFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "error"));
}
/**
* Test the case with no OID in incoming FlowFile during a SNMP set request
* @throws Exception Exception
*/
@Test
public void errorNoOIDSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1");
runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).isEmpty());
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_FAILURE).size() == 1);
}
/**
* Test the timeout case during a SNMP set request
* @throws Exception Exception
*/
@Test
public void errorTimeoutSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
attributes.put("snmp$" + "1.3.6.1.2.1.1.2.0", value);
runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1");
runner.setProperty(SetSNMP.PORT, String.valueOf(SNMPTestUtil.availablePort()));
runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).isEmpty());
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_FAILURE).size() == 1);
}
/**
* Test the case noSuchObject during a SNMP set request
* @throws Exception Exception
*/
@Test
public void errorNotExistingOIDSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
attributes.put("snmp$" + "1.3.6.1.2.1.1.2.0", value);
runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1");
runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).isEmpty());
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_FAILURE).size() == 1);
final MockFlowFile failFF = runner.getFlowFilesForRelationship(SetSNMP.REL_FAILURE).get(0);
assertNotNull(failFF);
assertEquals("Not writable", failFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "error"));
}
/**
* Test the case with not existing community during a SNMP set request
* @throws Exception Exception
*/
@Test
public void errorCommunitySnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("errorCommunity", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c);
SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
attributes.put("snmp$" + sysDescr, value);
runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1");
runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort()));
runner.setProperty(SetSNMP.SNMP_COMMUNITY, "errorCommunity");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
runner.run();
Thread.sleep(200);
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).isEmpty());
assertTrue(runner.getFlowFilesForRelationship(SetSNMP.REL_FAILURE).size() == 1);
}
/**
* Local extension of SNMP Getter
*/
private class LocalSetSnmp extends SetSNMP {
/** SNMP */
private Snmp snmp;
/** Target to request */
private AbstractTarget target;
/**
* Constructor
* @param snmp SNMP
* @param target Target
*/
public LocalSetSnmp(Snmp snmp, AbstractTarget target) {
this.snmp = snmp;
this.target = target;
}
}
}

View File

@ -1,218 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import java.io.File;
import java.io.IOException;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor;
import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.agent.MOGroup;
import org.snmp4j.agent.ManagedObject;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.mo.snmp.StorageType;
import org.snmp4j.agent.mo.snmp.VacmMIB;
import org.snmp4j.agent.security.MutableVACM;
import org.snmp4j.log.Log4jLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModel;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.transport.TransportMappings;
/**
* This Agent contains minimal functionality for running a version 1 SNMP agent.
*/
public class TestSnmpAgentV1 extends BaseAgent {
// not needed but very useful of course
static {
LogFactory.setLogFactory(new Log4jLogFactory());
}
/** address */
private String address;
/** port */
private int port;
/** constructor
* @param address address
* @throws IOException IO Exception
*/
public TestSnmpAgentV1(String address) throws IOException {
// These files have to be specified
// Read snmp4j doc for more info
super(new File("target/conf1.agent"), new File("target/bootCounter1.agent"), new CommandProcessor(new OctetString(MPv3.createLocalEngineID())));
this.port = SNMPTestUtil.availablePort();
this.address = address + "/" + port;
}
/**
* We let clients of this agent register the MO they
* need so this method does nothing
*/
@Override
protected void registerManagedObjects() {
}
/**
* Clients can register the MO they need
* @param mo managed object
*/
public void registerManagedObject(ManagedObject mo) {
try {
this.server.register(mo, null);
} catch (DuplicateRegistrationException ex) {
throw new RuntimeException(ex);
}
}
/** Method used to unregister objects
* @param moGroup group to unregister
*/
public void unregisterManagedObject(MOGroup moGroup) {
moGroup.unregisterMOs(this.server, this.getContext(moGroup));
}
/**
* @see org.snmp4j.agent.BaseAgent#addNotificationTargets(org.snmp4j.agent.mo.snmp.SnmpTargetMIB, org.snmp4j.agent.mo.snmp.SnmpNotificationMIB)
*/
@Override
protected void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB) {
/** nothing to do */
}
/**
* Minimal View based Access Control
* http://www.faqs.org/rfcs/rfc2575.html
*/
@Override
protected void addViews(VacmMIB vacm) {
vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv1,
new OctetString("cpublic"),
new OctetString("v1v2group"),
StorageType.nonVolatile);
vacm.addAccess(new OctetString("v1v2group"),
new OctetString("public"),
SecurityModel.SECURITY_MODEL_ANY,
SecurityLevel.NOAUTH_NOPRIV,
MutableVACM.VACM_MATCH_EXACT,
new OctetString("fullReadView"),
new OctetString("fullWriteView"),
new OctetString("fullNotifyView"),
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullReadView"),
new OID("1.3"),
new OctetString(),
VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullWriteView"),
new OID("1.3"),
new OctetString(),
VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullNotifyView"),
new OID("1.3"),
new OctetString(),
VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
}
/**
* User based Security Model, only applicable to SNMP v.3
*/
@Override
protected void addUsmUser(USM usm) {
/** nothing to do */
}
/**
* @see org.snmp4j.agent.BaseAgent#initTransportMappings()
*/
@Override
protected void initTransportMappings() throws IOException {
this.transportMappings = new TransportMapping[1];
Address addr = GenericAddress.parse(this.address);
TransportMapping tm = TransportMappings.getInstance().createTransportMapping(addr);
this.transportMappings[0] = tm;
}
/**
* Start method invokes some initialization methods needed to
* start the agent
* @throws IOException IO Exception
*/
public void start() throws IOException {
this.init();
this.addShutdownHook();
this.getServer().addContext(new OctetString("public"));
this.finishInit();
this.run();
this.sendColdStartNotification();
}
/**
* @see org.snmp4j.agent.BaseAgent#unregisterManagedObjects()
*/
@Override
protected void unregisterManagedObjects() {
/** nothing to do */
}
/**
* The table of community strings configured in the SNMP
* engine's Local Configuration Datastore (LCD).
*
* We only configure one, "public".
*/
@Override
protected void addCommunities(SnmpCommunityMIB communityMIB) {
Variable[] com2sec = new Variable[] {
new OctetString("public"), // community name
new OctetString("cpublic"), // security name
this.getAgent().getContextEngineID(), // local engine ID
new OctetString("public"), // default context name
new OctetString(), // transport tag
new Integer32(StorageType.nonVolatile), // storage type
new Integer32(RowStatus.active) // row status
};
MOTableRow row = communityMIB.getSnmpCommunityEntry().createRow(new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}
public int getPort() {
return port;
}
}

View File

@ -1,219 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import java.io.File;
import java.io.IOException;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor;
import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.agent.MOGroup;
import org.snmp4j.agent.ManagedObject;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.mo.snmp.StorageType;
import org.snmp4j.agent.mo.snmp.VacmMIB;
import org.snmp4j.agent.security.MutableVACM;
import org.snmp4j.log.Log4jLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModel;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.transport.TransportMappings;
/**
* This Agent contains minimal functionality for running a version 2c SNMP agent.
*/
public class TestSnmpAgentV2c extends BaseAgent {
// not needed but very useful of course
static {
LogFactory.setLogFactory(new Log4jLogFactory());
}
/** address */
private String address;
/** port */
private int port;
/** constructor
* @param address address
* @throws IOException IO Exception
*/
public TestSnmpAgentV2c(String address) throws IOException {
// These files have to be specified
// Read snmp4j doc for more info
super(new File("target/conf2.agent"), new File("target/bootCounter2.agent"), new CommandProcessor(new OctetString(MPv3.createLocalEngineID())));
this.port = SNMPTestUtil.availablePort();
this.address = address + "/" + port;
}
/**
* We let clients of this agent register the MO they
* need so this method does nothing
*/
@Override
protected void registerManagedObjects() {
}
/**
* Clients can register the MO they need
* @param mo managed object
*/
public void registerManagedObject(ManagedObject mo) {
try {
this.server.register(mo, null);
} catch (DuplicateRegistrationException ex) {
throw new RuntimeException(ex);
}
}
/** Method used to unregister objects
* @param moGroup group to unregister
*/
public void unregisterManagedObject(MOGroup moGroup) {
moGroup.unregisterMOs(this.server, this.getContext(moGroup));
}
/**
* @see org.snmp4j.agent.BaseAgent#addNotificationTargets(org.snmp4j.agent.mo.snmp.SnmpTargetMIB, org.snmp4j.agent.mo.snmp.SnmpNotificationMIB)
*/
@Override
protected void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB) {
/** nothing to do */
}
/**
* Minimal View based Access Control
* http://www.faqs.org/rfcs/rfc2575.html
*/
@Override
protected void addViews(VacmMIB vacm) {
vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c,
new OctetString("cpublic"),
new OctetString("v1v2group"),
StorageType.nonVolatile);
vacm.addAccess(new OctetString("v1v2group"),
new OctetString("public"),
SecurityModel.SECURITY_MODEL_ANY,
SecurityLevel.NOAUTH_NOPRIV,
MutableVACM.VACM_MATCH_EXACT,
new OctetString("fullReadView"),
new OctetString("fullWriteView"),
new OctetString("fullNotifyView"),
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullReadView"),
new OID("1.3"),
new OctetString(),
VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullWriteView"),
new OID("1.3"),
new OctetString(),
VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullNotifyView"),
new OID("1.3"),
new OctetString(),
VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
}
/**
* User based Security Model, only applicable to SNMP v.3
*/
@Override
protected void addUsmUser(USM usm) {
/** nothing to do */
}
/**
* @see org.snmp4j.agent.BaseAgent#initTransportMappings()
*/
@Override
protected void initTransportMappings() throws IOException {
this.transportMappings = new TransportMapping[1];
Address addr = GenericAddress.parse(this.address);
TransportMapping tm = TransportMappings.getInstance().createTransportMapping(addr);
this.transportMappings[0] = tm;
}
/**
* Start method invokes some initialization methods needed to
* start the agent
* @throws IOException IO Exception
*/
public void start() throws IOException {
this.init();
this.addShutdownHook();
this.getServer().addContext(new OctetString("public"));
this.finishInit();
this.run();
this.sendColdStartNotification();
}
/**
* @see org.snmp4j.agent.BaseAgent#unregisterManagedObjects()
*/
@Override
protected void unregisterManagedObjects() {
/** nothing to do */
}
/**
* The table of community strings configured in the SNMP
* engine's Local Configuration Datastore (LCD).
*
* We only configure one, "public".
*/
@Override
protected void addCommunities(SnmpCommunityMIB communityMIB) {
Variable[] com2sec = new Variable[] {
new OctetString("public"), // community name
new OctetString("cpublic"), // security name
this.getAgent().getContextEngineID(), // local engine ID
new OctetString("public"), // default context name
new OctetString(), // transport tag
new Integer32(StorageType.nonVolatile), // storage type
new Integer32(RowStatus.active) // row status
};
MOTableRow row = communityMIB.getSnmpCommunityEntry().createRow(new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}
public int getPort() {
return port;
}
}

View File

@ -1,255 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.BasicConfigurator;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor;
import org.snmp4j.agent.io.ImportModes;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.mo.snmp.StorageType;
import org.snmp4j.agent.mo.snmp.VacmMIB;
import org.snmp4j.agent.security.MutableVACM;
import org.snmp4j.log.Log4jLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModel;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.transport.TransportMappings;
import org.snmp4j.util.ThreadPool;
/**
* The <code>TestAgent</code> is a sample SNMP agent implementation of all
* features (MIB implementations) provided by the SNMP4J-Agent framework.
* The <code>TestAgent</code> extends the <code>BaseAgent</code> which provides
* a framework for custom agent implementations through hook methods. Those
* abstract hook methods need to be implemented by extending the
* <code>BaseAgent</code>.
* <p>
* This IF-MIB implementation part of this test agent, is instrumentation as
* a simulation MIB. Thus, by changing the agentppSimMode
* (1.3.6.1.4.1.4976.2.1.1.0) from 'oper(1)' to 'config(2)' any object of the
* IF-MIB is writable and even creatable (columnar objects) via SNMP. Check it
* out!
*
* @author Frank Fock
* @version 1.0
*/
public class TestSnmpAgentV3 extends BaseAgent {
// initialize Log4J logging
static {
LogFactory.setLogFactory(new Log4jLogFactory());
}
/** address */
protected String address;
/**
* Creates the test agent with a file to read and store the boot counter and
* a file to read and store its configuration.
*
* @param bootCounterFile
* a file containing the boot counter in serialized form (as expected by
* BaseAgent).
* @param configFile
* a configuration file with serialized management information.
* @throws IOException
* if the boot counter or config file cannot be read properly.
*/
public TestSnmpAgentV3(File bootCounterFile, File configFile) throws IOException {
super(bootCounterFile, configFile, new CommandProcessor(OctetString.fromHexString("00:00:00:00:00:00:02", ':')));
this.agent.setWorkerPool(ThreadPool.create("RequestPool", 4));
}
/**
* @see org.snmp4j.agent.BaseAgent#registerManagedObjects()
*/
@Override
protected void registerManagedObjects() {
/** nothing to do */
}
/**
* @see org.snmp4j.agent.BaseAgent#addNotificationTargets(org.snmp4j.agent.mo.snmp.SnmpTargetMIB, org.snmp4j.agent.mo.snmp.SnmpNotificationMIB)
*/
@Override
protected void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB) {
/** nothing to do */
}
/**
* @see org.snmp4j.agent.BaseAgent#addViews(org.snmp4j.agent.mo.snmp.VacmMIB)
*/
@Override
protected void addViews(VacmMIB vacm) {
vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
new OctetString("SHADES"),
new OctetString("v3group"),
StorageType.nonVolatile);
vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
new OctetString("MD5DES"),
new OctetString("v3group"),
StorageType.nonVolatile);
vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
new OctetString("SHAAES128"),
new OctetString("v3group"),
StorageType.nonVolatile);
vacm.addAccess(new OctetString("v3group"), new OctetString(),
SecurityModel.SECURITY_MODEL_USM,
SecurityLevel.AUTH_PRIV,
MutableVACM.VACM_MATCH_EXACT,
new OctetString("fullReadView"),
new OctetString("fullWriteView"),
new OctetString("fullNotifyView"),
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"),
new OctetString(), VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullWriteView"), new OID("1.3"),
new OctetString(), VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullNotifyView"), new OID("1.3"),
new OctetString(), VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
}
/**
* @see org.snmp4j.agent.BaseAgent#addUsmUser(org.snmp4j.security.USM)
*/
@Override
protected void addUsmUser(USM usm) {
UsmUser user = new UsmUser(new OctetString("SHA"),
AuthSHA.ID,
new OctetString("SHAAuthPassword"),
null,
null);
usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
user = new UsmUser(new OctetString("SHADES"),
AuthSHA.ID,
new OctetString("SHADESAuthPassword"),
PrivDES.ID,
new OctetString("SHADESPrivPassword"));
usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
user = new UsmUser(new OctetString("MD5DES"),
AuthMD5.ID,
new OctetString("MD5DESAuthPassword"),
PrivDES.ID,
new OctetString("MD5DESPrivPassword"));
usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
user = new UsmUser(new OctetString("SHAAES128"),
AuthSHA.ID,
new OctetString("SHAAES128AuthPassword"),
PrivAES128.ID,
new OctetString("SHAAES128PrivPassword"));
usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
}
/**
* @see org.snmp4j.agent.BaseAgent#initTransportMappings()
*/
@Override
protected void initTransportMappings() throws IOException {
this.transportMappings = new TransportMapping[1];
Address addr = GenericAddress.parse(this.address);
TransportMapping tm = TransportMappings.getInstance().createTransportMapping(addr);
this.transportMappings[0] = tm;
}
/**
* Method to run agent
* @param args arguments
*/
public static void main(String[] args) {
String address = args[0] + "/" + SNMPTestUtil.availablePort();;
BasicConfigurator.configure();
try {
TestSnmpAgentV3 testAgent1 = new TestSnmpAgentV3(new File("target/SNMP4JTestAgentBC.cfg"),
new File("target/SNMP4JTestAgentConfig.cfg"));
testAgent1.address = address;
testAgent1.init();
testAgent1.loadConfig(ImportModes.REPLACE_CREATE);
testAgent1.addShutdownHook();
testAgent1.getServer().addContext(new OctetString("public"));
testAgent1.finishInit();
testAgent1.run();
testAgent1.sendColdStartNotification();
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* @see org.snmp4j.agent.BaseAgent#unregisterManagedObjects()
*/
@Override
protected void unregisterManagedObjects() {
/** nothing to do */
}
/**
* @see org.snmp4j.agent.BaseAgent#addCommunities(org.snmp4j.agent.mo.snmp.SnmpCommunityMIB)
*/
@Override
protected void addCommunities(SnmpCommunityMIB communityMIB) {
Variable[] com2sec = new Variable[] {
new OctetString("public"), // community name
new OctetString("cpublic"), // security name
this.getAgent().getContextEngineID(), // local engine ID
new OctetString("public"), // default context name
new OctetString(), // transport tag
new Integer32(StorageType.nonVolatile), // storage type
new Integer32(RowStatus.active) // row status
};
MOTableRow row = communityMIB.getSnmpCommunityEntry().createRow(new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}
/**
* @see org.snmp4j.agent.BaseAgent#registerSnmpMIBs()
*/
@Override
protected void registerSnmpMIBs() {
super.registerSnmpMIBs();
}
}

View File

@ -1,117 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.snmp.processors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.snmp4j.AbstractTarget;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
/**
* Class to test SNMP Get processor
*/
public class WalkSNMPTest {
/** agent for test with SNMP v1 */
private static TestSnmpAgentV1 agentv1 = null;
/** Root OID to perform a WALK */
private static final OID root = new OID("1.3.6.1.2.1.1");
/**
* Method to set up different SNMP agents
* @throws Exception Exception
*/
@BeforeClass
public static void setUp() throws Exception {
agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start();
}
/**
* Method to close SNMP Agent once the tests are completed
* @throws Exception Exception
*/
@AfterClass
public static void tearDown() throws Exception {
agentv1.stop();
}
/**
* Method to test successful SNMP Walk in case of v1
* @throws Exception Exception
*/
@Test
public void validateSuccessfullSnmpWalkVersion1() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp();
CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1);
GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, root.toString());
runner.setProperty(GetSNMP.SNMP_STRATEGY, "WALK");
runner.setProperty(GetSNMP.HOST, "127.0.0.1");
runner.setProperty(GetSNMP.PORT, String.valueOf(agentv1.getPort()));
runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1");
runner.run();
Thread.sleep(200);
final MockFlowFile successFF = runner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).get(0);
assertNotNull(successFF);
int i = 0;
for (String attributeKey : successFF.getAttributes().keySet()) {
if(attributeKey.startsWith(SNMPUtils.SNMP_PROP_PREFIX)) {
i++;
}
}
assertEquals(8,i);
}
/**
* Local extension of SNMP Getter
*/
private class LocalGetSnmp extends GetSNMP {
/** SNMP */
private Snmp snmp;
/** Target to request */
private AbstractTarget target;
/**
* Constructor
* @param snmp SNMP
* @param target Target
*/
public LocalGetSnmp(Snmp snmp, AbstractTarget target) {
this.snmp = snmp;
this.target = target;
}
}
}