Search in sources :

Example 1 with SnmpProtocolVersion

use of org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion in project thingsboard by thingsboard.

the class PduService method setUpPdu.

private PDU setUpPdu(DeviceSessionContext sessionContext) {
    PDU pdu;
    SnmpDeviceTransportConfiguration deviceTransportConfiguration = sessionContext.getDeviceTransportConfiguration();
    SnmpProtocolVersion snmpVersion = deviceTransportConfiguration.getProtocolVersion();
    switch(snmpVersion) {
        case V1:
        case V2C:
            pdu = new PDU();
            break;
        case V3:
            ScopedPDU scopedPdu = new ScopedPDU();
            scopedPdu.setContextName(new OctetString(deviceTransportConfiguration.getContextName()));
            scopedPdu.setContextEngineID(new OctetString(deviceTransportConfiguration.getEngineId()));
            pdu = scopedPdu;
            break;
        default:
            throw new UnsupportedOperationException("SNMP version " + snmpVersion + " is not supported");
    }
    return pdu;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) ScopedPDU(org.snmp4j.ScopedPDU) SnmpProtocolVersion(org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion)

Example 2 with SnmpProtocolVersion

use of org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion 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

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