Search in sources :

Example 1 with AbstractTarget

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;
}
Also used : AbstractTarget(org.snmp4j.AbstractTarget) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) OctetString(org.snmp4j.smi.OctetString) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) CommunityTarget(org.snmp4j.CommunityTarget)

Example 2 with AbstractTarget

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;
}
Also used : AbstractTarget(org.snmp4j.AbstractTarget) OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) GenericAddress(org.snmp4j.smi.GenericAddress) OID(org.snmp4j.smi.OID) SnmpProtocolVersion(org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion) UserTarget(org.snmp4j.UserTarget) CommunityTarget(org.snmp4j.CommunityTarget) USM(org.snmp4j.security.USM)

Aggregations

AbstractTarget (org.snmp4j.AbstractTarget)2 CommunityTarget (org.snmp4j.CommunityTarget)2 UserTarget (org.snmp4j.UserTarget)2 OctetString (org.snmp4j.smi.OctetString)2 USM (org.snmp4j.security.USM)1 UsmUser (org.snmp4j.security.UsmUser)1 Address (org.snmp4j.smi.Address)1 GenericAddress (org.snmp4j.smi.GenericAddress)1 OID (org.snmp4j.smi.OID)1 UdpAddress (org.snmp4j.smi.UdpAddress)1 SnmpProtocolVersion (org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion)1