Search in sources :

Example 1 with SnmpMapping

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

the class SnmpTransportService method configureResponseDataMappers.

private void configureResponseDataMappers() {
    responseDataMappers.put(SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST, (pdu, requestInfo) -> {
        JsonObject responseData = new JsonObject();
        pduService.processPdu(pdu).forEach((oid, value) -> {
            requestInfo.getResponseMappings().stream().filter(snmpMapping -> snmpMapping.getOid().equals(oid.toDottedString())).findFirst().ifPresent(snmpMapping -> {
                pduService.processValue(snmpMapping.getKey(), snmpMapping.getDataType(), value, responseData);
            });
        });
        return responseData;
    });
    ResponseDataMapper defaultResponseDataMapper = (pdu, requestInfo) -> {
        return pduService.processPdu(pdu, requestInfo.getResponseMappings());
    };
    Arrays.stream(SnmpCommunicationSpec.values()).forEach(communicationSpec -> {
        responseDataMappers.putIfAbsent(communicationSpec, defaultResponseDataMapper);
    });
}
Also used : JsonObject(com.google.gson.JsonObject) SnmpCommunicationSpec(org.thingsboard.server.common.data.transport.snmp.SnmpCommunicationSpec) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) RequiredArgsConstructor(lombok.RequiredArgsConstructor) SnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig) PDU(org.snmp4j.PDU) PreDestroy(javax.annotation.PreDestroy) Map(java.util.Map) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) SecurityModels(org.snmp4j.security.SecurityModels) EnumMap(java.util.EnumMap) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) TransportService(org.thingsboard.server.common.transport.TransportService) Collectors(java.util.stream.Collectors) TransportMapping(org.snmp4j.TransportMapping) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ResponseEvent(org.snmp4j.event.ResponseEvent) SecurityProtocols(org.snmp4j.security.SecurityProtocols) MPv3(org.snmp4j.mp.MPv3) TbTransportService(org.thingsboard.server.common.data.TbTransportService) PostConstruct(javax.annotation.PostConstruct) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) TbSnmpTransportComponent(org.thingsboard.server.queue.util.TbSnmpTransportComponent) Optional(java.util.Optional) DeviceSessionContext(org.thingsboard.server.transport.snmp.session.DeviceSessionContext) RepeatingQueryingSnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.RepeatingQueryingSnmpCommunicationConfig) Getter(lombok.Getter) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) JsonElement(com.google.gson.JsonElement) Value(org.springframework.beans.factory.annotation.Value) DataType(org.thingsboard.server.common.data.kv.DataType) Service(org.springframework.stereotype.Service) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Snmp(org.snmp4j.Snmp) ThingsBoardExecutors(org.thingsboard.common.util.ThingsBoardExecutors) DataConstants(org.thingsboard.server.common.data.DataConstants) USM(org.snmp4j.security.USM) SnmpMapping(org.thingsboard.server.common.data.transport.snmp.SnmpMapping) IOException(java.io.IOException) OctetString(org.snmp4j.smi.OctetString) SnmpMethod(org.thingsboard.server.common.data.transport.snmp.SnmpMethod) TimeUnit(java.util.concurrent.TimeUnit) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) Data(lombok.Data) Collections(java.util.Collections) JsonObject(com.google.gson.JsonObject)

Example 2 with SnmpMapping

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

the class PduService method processPdu.

public JsonObject processPdu(PDU pdu, List<SnmpMapping> responseMappings) {
    Map<OID, String> values = processPdu(pdu);
    Map<OID, SnmpMapping> mappings = new HashMap<>();
    if (responseMappings != null) {
        for (SnmpMapping mapping : responseMappings) {
            OID oid = new OID(mapping.getOid());
            mappings.put(oid, mapping);
        }
    }
    JsonObject data = new JsonObject();
    values.forEach((oid, value) -> {
        log.trace("Processing variable binding: {} - {}", oid, value);
        SnmpMapping mapping = mappings.get(oid);
        if (mapping == null) {
            log.debug("No SNMP mapping for oid {}", oid);
            return;
        }
        processValue(mapping.getKey(), mapping.getDataType(), value, data);
    });
    return data;
}
Also used : SnmpMapping(org.thingsboard.server.common.data.transport.snmp.SnmpMapping) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString)

Example 3 with SnmpMapping

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

the class SnmpTransportService method onToDeviceRpcRequest.

public void onToDeviceRpcRequest(DeviceSessionContext sessionContext, TransportProtos.ToDeviceRpcRequestMsg toDeviceRpcRequestMsg) {
    SnmpMethod snmpMethod = SnmpMethod.valueOf(toDeviceRpcRequestMsg.getMethodName());
    JsonObject params = JsonConverter.parse(toDeviceRpcRequestMsg.getParams()).getAsJsonObject();
    String key = Optional.ofNullable(params.get("key")).map(JsonElement::getAsString).orElse(null);
    String value = Optional.ofNullable(params.get("value")).map(JsonElement::getAsString).orElse(null);
    if (value == null && snmpMethod == SnmpMethod.SET) {
        throw new IllegalArgumentException("Value must be specified for SNMP method 'SET'");
    }
    SnmpCommunicationConfig communicationConfig = sessionContext.getProfileTransportConfiguration().getCommunicationConfigs().stream().filter(config -> config.getSpec() == SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST).findFirst().orElseThrow(() -> new IllegalArgumentException("No communication config found with RPC spec"));
    SnmpMapping snmpMapping = communicationConfig.getAllMappings().stream().filter(mapping -> mapping.getKey().equals(key)).findFirst().orElseThrow(() -> new IllegalArgumentException("No SNMP mapping found in the config for specified key"));
    String oid = snmpMapping.getOid();
    DataType dataType = snmpMapping.getDataType();
    PDU request = pduService.createSingleVariablePdu(sessionContext, snmpMethod, oid, value, dataType);
    RequestInfo requestInfo = new RequestInfo(toDeviceRpcRequestMsg.getRequestId(), communicationConfig.getSpec(), communicationConfig.getAllMappings());
    sendRequest(sessionContext, request, requestInfo);
}
Also used : SnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig) RepeatingQueryingSnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.RepeatingQueryingSnmpCommunicationConfig) SnmpMapping(org.thingsboard.server.common.data.transport.snmp.SnmpMapping) PDU(org.snmp4j.PDU) SnmpMethod(org.thingsboard.server.common.data.transport.snmp.SnmpMethod) JsonObject(com.google.gson.JsonObject) DataType(org.thingsboard.server.common.data.kv.DataType) OctetString(org.snmp4j.smi.OctetString)

Aggregations

JsonObject (com.google.gson.JsonObject)3 OctetString (org.snmp4j.smi.OctetString)3 SnmpMapping (org.thingsboard.server.common.data.transport.snmp.SnmpMapping)3 PDU (org.snmp4j.PDU)2 JsonElement (com.google.gson.JsonElement)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1