Commit 3b74a806bc6c23d7319e561d7409fca1325a85c6

Authored by Viacheslav Klimov
Committed by GitHub
1 parent b34198f3

RPC functionality refactoring for SNMP (#4492)

* Refactor RPC functionality for SNMP

* Refactor

* SNMP response PDU mapping refactoring
... ... @@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.transport.snmp.SnmpMethod;
26 26 import org.thingsboard.server.common.data.transport.snmp.config.impl.ClientAttributesQueryingSnmpCommunicationConfig;
27 27 import org.thingsboard.server.common.data.transport.snmp.config.impl.SharedAttributesSettingSnmpCommunicationConfig;
28 28 import org.thingsboard.server.common.data.transport.snmp.config.impl.TelemetryQueryingSnmpCommunicationConfig;
  29 +import org.thingsboard.server.common.data.transport.snmp.config.impl.ToDeviceRpcRequestSnmpCommunicationConfig;
29 30
30 31 import java.util.List;
31 32
... ... @@ -34,7 +35,8 @@ import java.util.List;
34 35 @JsonSubTypes({
35 36 @Type(value = TelemetryQueryingSnmpCommunicationConfig.class, name = "TELEMETRY_QUERYING"),
36 37 @Type(value = ClientAttributesQueryingSnmpCommunicationConfig.class, name = "CLIENT_ATTRIBUTES_QUERYING"),
37   - @Type(value = SharedAttributesSettingSnmpCommunicationConfig.class, name = "SHARED_ATTRIBUTES_SETTING")
  38 + @Type(value = SharedAttributesSettingSnmpCommunicationConfig.class, name = "SHARED_ATTRIBUTES_SETTING"),
  39 + @Type(value = ToDeviceRpcRequestSnmpCommunicationConfig.class, name = "TO_DEVICE_RPC_REQUEST")
38 40 })
39 41 public interface SnmpCommunicationConfig {
40 42
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.common.data.transport.snmp.config.impl;
  17 +
  18 +import org.thingsboard.server.common.data.transport.snmp.SnmpCommunicationSpec;
  19 +import org.thingsboard.server.common.data.transport.snmp.config.MultipleMappingsSnmpCommunicationConfig;
  20 +
  21 +public class ToDeviceRpcRequestSnmpCommunicationConfig extends MultipleMappingsSnmpCommunicationConfig {
  22 + @Override
  23 + public SnmpCommunicationSpec getSpec() {
  24 + return SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST;
  25 + }
  26 +}
... ...
... ... @@ -151,7 +151,7 @@ public class PduService {
151 151 .collect(Collectors.toMap(VariableBinding::getOid, VariableBinding::toValueString));
152 152 }
153 153
154   - private void processValue(String key, DataType dataType, String value, JsonObject result) {
  154 + public void processValue(String key, DataType dataType, String value, JsonObject result) {
155 155 switch (dataType) {
156 156 case LONG:
157 157 result.addProperty(key, Long.parseLong(value));
... ...
... ... @@ -183,19 +183,28 @@ public class SnmpTransportService implements TbTransportService {
183 183 SnmpMethod snmpMethod = SnmpMethod.valueOf(toDeviceRpcRequestMsg.getMethodName());
184 184 JsonObject params = JsonConverter.parse(toDeviceRpcRequestMsg.getParams()).getAsJsonObject();
185 185
186   - String oid = Optional.ofNullable(params.get("oid")).map(JsonElement::getAsString).orElse(null);
  186 + String key = Optional.ofNullable(params.get("key")).map(JsonElement::getAsString).orElse(null);
187 187 String value = Optional.ofNullable(params.get("value")).map(JsonElement::getAsString).orElse(null);
188   - DataType dataType = Optional.ofNullable(params.get("dataType")).map(e -> DataType.valueOf(e.getAsString())).orElse(DataType.STRING);
189 188
190   - if (oid == null || oid.isEmpty()) {
191   - throw new IllegalArgumentException("OID in to-device RPC request is not specified");
192   - }
193 189 if (value == null && snmpMethod == SnmpMethod.SET) {
194 190 throw new IllegalArgumentException("Value must be specified for SNMP method 'SET'");
195 191 }
196 192
  193 + SnmpCommunicationConfig communicationConfig = sessionContext.getProfileTransportConfiguration().getCommunicationConfigs().stream()
  194 + .filter(config -> config.getSpec() == SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST)
  195 + .findFirst()
  196 + .orElseThrow(() -> new IllegalArgumentException("No communication config found with RPC spec"));
  197 + SnmpMapping snmpMapping = communicationConfig.getAllMappings().stream()
  198 + .filter(mapping -> mapping.getKey().equals(key))
  199 + .findFirst()
  200 + .orElseThrow(() -> new IllegalArgumentException("No SNMP mapping found in the config for specified key"));
  201 +
  202 + String oid = snmpMapping.getOid();
  203 + DataType dataType = snmpMapping.getDataType();
  204 +
197 205 PDU request = pduService.createSingleVariablePdu(sessionContext, snmpMethod, oid, value, dataType);
198   - sendRequest(sessionContext, request, new RequestInfo(toDeviceRpcRequestMsg.getRequestId(), SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST));
  206 + RequestInfo requestInfo = new RequestInfo(toDeviceRpcRequestMsg.getRequestId(), communicationConfig.getSpec(), communicationConfig.getAllMappings());
  207 + sendRequest(sessionContext, request, requestInfo);
199 208 }
200 209
201 210
... ... @@ -238,7 +247,12 @@ public class SnmpTransportService implements TbTransportService {
238 247 responseDataMappers.put(SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST, (pdu, requestInfo) -> {
239 248 JsonObject responseData = new JsonObject();
240 249 pduService.processPdu(pdu).forEach((oid, value) -> {
241   - responseData.addProperty(oid.toDottedString(), value);
  250 + requestInfo.getResponseMappings().stream()
  251 + .filter(snmpMapping -> snmpMapping.getOid().equals(oid.toDottedString()))
  252 + .findFirst()
  253 + .ifPresent(snmpMapping -> {
  254 + pduService.processValue(snmpMapping.getKey(), snmpMapping.getDataType(), value, responseData);
  255 + });
242 256 });
243 257 return responseData;
244 258 });
... ... @@ -314,13 +328,10 @@ public class SnmpTransportService implements TbTransportService {
314 328 private SnmpCommunicationSpec communicationSpec;
315 329 private List<SnmpMapping> responseMappings;
316 330
317   - public RequestInfo(Integer requestId, SnmpCommunicationSpec communicationSpec) {
  331 + public RequestInfo(Integer requestId, SnmpCommunicationSpec communicationSpec, List<SnmpMapping> responseMappings) {
318 332 this.requestId = requestId;
319 333 this.communicationSpec = communicationSpec;
320   - }
321   -
322   - public RequestInfo(SnmpCommunicationSpec communicationSpec) {
323   - this.communicationSpec = communicationSpec;
  334 + this.responseMappings = responseMappings;
324 335 }
325 336
326 337 public RequestInfo(SnmpCommunicationSpec communicationSpec, List<SnmpMapping> responseMappings) {
... ...