Search in sources :

Example 1 with SnmpDeviceTransportConfiguration

use of org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration in project thingsboard by thingsboard.

the class SnmpTransportContext method establishDeviceSession.

private void establishDeviceSession(Device device) {
    if (device == null)
        return;
    log.info("Establishing SNMP session for device {}", device.getId());
    DeviceProfileId deviceProfileId = device.getDeviceProfileId();
    DeviceProfile deviceProfile = deviceProfileCache.get(deviceProfileId);
    DeviceCredentials credentials = protoEntityService.getDeviceCredentialsByDeviceId(device.getId());
    if (credentials.getCredentialsType() != DeviceCredentialsType.ACCESS_TOKEN) {
        log.warn("[{}] Expected credentials type is {} but found {}", device.getId(), DeviceCredentialsType.ACCESS_TOKEN, credentials.getCredentialsType());
        return;
    }
    SnmpDeviceProfileTransportConfiguration profileTransportConfiguration = (SnmpDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
    SnmpDeviceTransportConfiguration deviceTransportConfiguration = (SnmpDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
    DeviceSessionContext deviceSessionContext;
    try {
        deviceSessionContext = new DeviceSessionContext(device, deviceProfile, credentials.getCredentialsId(), profileTransportConfiguration, deviceTransportConfiguration, this);
        registerSessionMsgListener(deviceSessionContext);
    } catch (Exception e) {
        log.error("Failed to establish session for SNMP device {}: {}", device.getId(), e.toString());
        return;
    }
    sessions.put(device.getId(), deviceSessionContext);
    snmpTransportService.createQueryingTasks(deviceSessionContext);
    log.info("Established SNMP device session for device {}", device.getId());
}
Also used : SnmpDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) DeviceSessionContext(org.thingsboard.server.transport.snmp.session.DeviceSessionContext) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 2 with SnmpDeviceTransportConfiguration

use of org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration 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 3 with SnmpDeviceTransportConfiguration

use of org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration in project thingsboard by thingsboard.

the class SnmpAuthService method cleanUpSnmpAuthInfo.

public void cleanUpSnmpAuthInfo(DeviceSessionContext sessionContext) {
    SnmpDeviceTransportConfiguration deviceTransportConfiguration = sessionContext.getDeviceTransportConfiguration();
    if (deviceTransportConfiguration.getProtocolVersion() == SnmpProtocolVersion.V3) {
        OctetString username = new OctetString(deviceTransportConfiguration.getUsername());
        OctetString engineId = new OctetString(deviceTransportConfiguration.getEngineId());
        snmpTransportService.getSnmp().getUSM().removeAllUsers(username, engineId);
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration)

Example 4 with SnmpDeviceTransportConfiguration

use of org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration in project thingsboard by thingsboard.

the class SnmpTransportContext method updateDeviceSession.

private void updateDeviceSession(DeviceSessionContext sessionContext, Device device, DeviceProfile deviceProfile) {
    log.info("Updating SNMP session for device {}", device.getId());
    DeviceCredentials credentials = protoEntityService.getDeviceCredentialsByDeviceId(device.getId());
    if (credentials.getCredentialsType() != DeviceCredentialsType.ACCESS_TOKEN) {
        log.warn("[{}] Expected credentials type is {} but found {}", device.getId(), DeviceCredentialsType.ACCESS_TOKEN, credentials.getCredentialsType());
        destroyDeviceSession(sessionContext);
        return;
    }
    SnmpDeviceProfileTransportConfiguration newProfileTransportConfiguration = (SnmpDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
    SnmpDeviceTransportConfiguration newDeviceTransportConfiguration = (SnmpDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
    try {
        if (!newProfileTransportConfiguration.equals(sessionContext.getProfileTransportConfiguration())) {
            sessionContext.setProfileTransportConfiguration(newProfileTransportConfiguration);
            sessionContext.initializeTarget(newProfileTransportConfiguration, newDeviceTransportConfiguration);
            snmpTransportService.cancelQueryingTasks(sessionContext);
            snmpTransportService.createQueryingTasks(sessionContext);
        } else if (!newDeviceTransportConfiguration.equals(sessionContext.getDeviceTransportConfiguration())) {
            sessionContext.setDeviceTransportConfiguration(newDeviceTransportConfiguration);
            sessionContext.initializeTarget(newProfileTransportConfiguration, newDeviceTransportConfiguration);
        } else {
            log.trace("Configuration of the device {} was not updated", device);
        }
    } catch (Exception e) {
        log.error("Failed to update session for SNMP device {}: {}", sessionContext.getDeviceId(), e.getMessage());
        destroyDeviceSession(sessionContext);
    }
}
Also used : SnmpDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Aggregations

SnmpDeviceTransportConfiguration (org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration)4 OctetString (org.snmp4j.smi.OctetString)2 SnmpDeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration)2 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)2 PDU (org.snmp4j.PDU)1 ScopedPDU (org.snmp4j.ScopedPDU)1 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)1 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)1 SnmpProtocolVersion (org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion)1 DeviceSessionContext (org.thingsboard.server.transport.snmp.session.DeviceSessionContext)1