use of org.snmp4j.AbstractTarget in project nifi by apache.
the class AbstractSNMPProcessor method createSnmpTarget.
/**
* Creates {@link AbstractTarget} to request SNMP agent.
* @param context Process context
* @return the SNMP target
*/
private AbstractTarget createSnmpTarget(ProcessContext context) {
AbstractTarget result = null;
String snmpVersion = context.getProperty(SNMP_VERSION).getValue();
int version = 0;
switch(snmpVersion) {
case "SNMPv2c":
version = SnmpConstants.version2c;
break;
case "SNMPv3":
version = SnmpConstants.version3;
break;
case "SNMPv1":
default:
version = SnmpConstants.version1;
break;
}
if (version == SnmpConstants.version3) {
final String username = context.getProperty(SNMP_SECURITY_NAME).getValue();
final String authPassword = context.getProperty(SNMP_AUTH_PASSWORD).getValue();
final String privPassword = context.getProperty(SNMP_PRIV_PASSWORD).getValue();
final String authProtocol = context.getProperty(SNMP_AUTH_PROTOCOL).getValue();
final String privProtocol = context.getProperty(SNMP_PRIV_PROTOCOL).getValue();
OctetString aPwd = authPassword != null ? new OctetString(authPassword) : null;
OctetString pPwd = privPassword != null ? new OctetString(privPassword) : null;
// add user information
this.snmp.getUSM().addUser(new OctetString(username), new UsmUser(new OctetString(username), SNMPUtils.getAuth(authProtocol), aPwd, SNMPUtils.getPriv(privProtocol), pPwd));
result = new UserTarget();
((UserTarget) result).setSecurityLevel(SNMPUtils.getSecLevel(context.getProperty(SNMP_SECURITY_LEVEL).getValue()));
final String securityName = context.getProperty(SNMP_SECURITY_NAME).getValue();
if (securityName != null) {
((UserTarget) result).setSecurityName(new OctetString(securityName));
}
} else {
result = new CommunityTarget();
String community = context.getProperty(SNMP_COMMUNITY).getValue();
if (community != null) {
((CommunityTarget) result).setCommunity(new OctetString(community));
}
}
result.setVersion(version);
result.setAddress(new UdpAddress(context.getProperty(HOST).getValue() + "/" + context.getProperty(PORT).getValue()));
result.setRetries(context.getProperty(SNMP_RETRIES).asInteger());
result.setTimeout(context.getProperty(SNMP_TIMEOUT).asInteger());
return result;
}
use of org.snmp4j.AbstractTarget in project thingsboard by thingsboard.
the class SnmpAuthService method setUpSnmpTarget.
public Target setUpSnmpTarget(SnmpDeviceProfileTransportConfiguration profileTransportConfig, SnmpDeviceTransportConfiguration deviceTransportConfig) {
AbstractTarget target;
SnmpProtocolVersion protocolVersion = deviceTransportConfig.getProtocolVersion();
switch(protocolVersion) {
case V1:
CommunityTarget communityTargetV1 = new CommunityTarget();
communityTargetV1.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv1);
communityTargetV1.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
communityTargetV1.setCommunity(new OctetString(deviceTransportConfig.getCommunity()));
target = communityTargetV1;
break;
case V2C:
CommunityTarget communityTargetV2 = new CommunityTarget();
communityTargetV2.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv2c);
communityTargetV2.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
communityTargetV2.setCommunity(new OctetString(deviceTransportConfig.getCommunity()));
target = communityTargetV2;
break;
case V3:
OctetString username = new OctetString(deviceTransportConfig.getUsername());
OctetString securityName = new OctetString(deviceTransportConfig.getSecurityName());
OctetString engineId = new OctetString(deviceTransportConfig.getEngineId());
OID authenticationProtocol = new OID(deviceTransportConfig.getAuthenticationProtocol().getOid());
OID privacyProtocol = new OID(deviceTransportConfig.getPrivacyProtocol().getOid());
OctetString authenticationPassphrase = new OctetString(deviceTransportConfig.getAuthenticationPassphrase());
authenticationPassphrase = new OctetString(SecurityProtocols.getInstance().passwordToKey(authenticationProtocol, authenticationPassphrase, engineId.getValue()));
OctetString privacyPassphrase = new OctetString(deviceTransportConfig.getPrivacyPassphrase());
privacyPassphrase = new OctetString(SecurityProtocols.getInstance().passwordToKey(privacyProtocol, authenticationProtocol, privacyPassphrase, engineId.getValue()));
USM usm = snmpTransportService.getSnmp().getUSM();
if (usm.hasUser(engineId, securityName)) {
usm.removeAllUsers(username, engineId);
}
usm.addLocalizedUser(engineId.getValue(), username, authenticationProtocol, authenticationPassphrase.getValue(), privacyProtocol, privacyPassphrase.getValue());
UserTarget userTarget = new UserTarget();
userTarget.setSecurityName(securityName);
userTarget.setAuthoritativeEngineID(engineId.getValue());
userTarget.setSecurityModel(SecurityModel.SECURITY_MODEL_USM);
userTarget.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target = userTarget;
break;
default:
throw new UnsupportedOperationException("SNMP protocol version " + protocolVersion + " is not supported");
}
Address address = GenericAddress.parse(snmpUnderlyingProtocol + ":" + deviceTransportConfig.getHost() + "/" + deviceTransportConfig.getPort());
target.setAddress(Optional.ofNullable(address).orElseThrow(() -> new IllegalArgumentException("Address of the SNMP device is invalid")));
target.setTimeout(profileTransportConfig.getTimeoutMs());
target.setRetries(profileTransportConfig.getRetries());
target.setVersion(protocolVersion.getCode());
return target;
}
Aggregations