From 8d6768009d0435a960d42ad8e6108ef726bf97e3 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 2 Oct 2024 13:12:53 +0300 Subject: [PATCH] lwm2m: update version: leshan = M15, californium = 3.12.1 --- .../lwm2m/attributes/LwM2mAttributesTest.java | 4 +- .../lwm2m/client/TbLwm2mObjectEnabler.java | 85 +++++++++---------- ...ntegrationDiscoverWriteAttributesTest.java | 44 +++++----- ...LwM2mCredentialsSecurityInfoValidator.java | 2 +- .../device/DeviceCredentialsServiceImpl.java | 2 +- .../validator/DeviceProfileDataValidator.java | 2 +- pom.xml | 4 +- 7 files changed, 72 insertions(+), 71 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/attributes/LwM2mAttributesTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/attributes/LwM2mAttributesTest.java index 73f8346566..ad23ed9f0c 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/attributes/LwM2mAttributesTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/attributes/LwM2mAttributesTest.java @@ -49,13 +49,13 @@ public class LwM2mAttributesTest { @ParameterizedTest(name = "Tests {index} : {0}") @MethodSource("doesntSupportAttributesWithoutValue") public void check_attribute_can_not_be_created_without_value(LwM2mAttributeModel model) { - assertThrows(UnsupportedOperationException.class, () -> LwM2mAttributes.create(model)); + assertThrows(IllegalArgumentException.class, () -> LwM2mAttributes.create(model)); } @ParameterizedTest(name = "Tests {index} : {0}") @MethodSource("doesntSupportAttributesWithValueNull") public void check_attribute_can_not_be_created_with_null(LwM2mAttributeModel model) { - assertThrows(NullPointerException.class, () -> LwM2mAttributes.create(model, null)); + assertThrows(IllegalArgumentException.class, () -> LwM2mAttributes.create(model, null)); } private static Stream supportNullAttributes() throws InvalidAttributeException { diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/TbLwm2mObjectEnabler.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/TbLwm2mObjectEnabler.java index f548cc30e2..a7bdae3321 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/TbLwm2mObjectEnabler.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/TbLwm2mObjectEnabler.java @@ -69,7 +69,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -88,6 +87,7 @@ public class TbLwm2mObjectEnabler extends BaseObjectEnabler implements Destroyab private LinkFormatHelper tbLinkFormatHelper; protected Map lwM2mAttributes; + public TbLwm2mObjectEnabler(int id, ObjectModel objectModel, Map instances, LwM2mInstanceEnablerFactory instanceFactory, ContentFormat defaultContentFormat) { super(id, objectModel); @@ -586,10 +586,10 @@ public class TbLwm2mObjectEnabler extends BaseObjectEnabler implements Destroyab * - Less Than lt (def = -- ) Float Resource Numerical&Readable Resource * - Step st (def = -- ) Float Resource Numerical&Readable Resource */ - public WriteAttributesResponse doWriteAttributes(LwM2mServer server, WriteAttributesRequest request) { + public WriteAttributesResponse doWriteAttributes(LwM2mServer server, WriteAttributesRequest request) { LwM2mPath lwM2mPath = request.getPath(); LwM2mAttributeSet attributeSet = lwM2mAttributes.get(lwM2mPath); - Map > attributes = new HashMap<>(); + Map> attributes = new HashMap<>(); for (LwM2mAttribute attr : request.getAttributes().getLwM2mAttributes()) { if (attr.getName().equals("pmax") || attr.getName().equals("pmin")) { @@ -606,12 +606,12 @@ public class TbLwm2mObjectEnabler extends BaseObjectEnabler implements Destroyab } } } - if (attributes.size()>0){ + if (attributes.size() > 0) { if (attributeSet == null) { attributeSet = new LwM2mAttributeSet(attributes.values()); } else { Iterable> lwM2mAttributeIterable = attributeSet.getLwM2mAttributes(); - Map > attributesOld = new HashMap<>(); + Map> attributesOld = new HashMap<>(); for (LwM2mAttribute attr : lwM2mAttributeIterable) { attributesOld.put(attr.getName(), attr); } @@ -643,7 +643,7 @@ public class TbLwm2mObjectEnabler extends BaseObjectEnabler implements Destroyab LwM2mPath path = request.getPath(); if (path.isObject()) { - LwM2mLink[] ObjectLinks = linkUpdateAttributes(this.tbLinkFormatHelper.getObjectDescription(this, null), server); + LwM2mLink[] ObjectLinks = linkAddUpdateAttributes(this.tbLinkFormatHelper.getObjectDescription(server, this, null), server); return DiscoverResponse.success(ObjectLinks); } else if (path.isObjectInstance()) { @@ -651,7 +651,7 @@ public class TbLwm2mObjectEnabler extends BaseObjectEnabler implements Destroyab if (!getAvailableInstanceIds().contains(path.getObjectInstanceId())) return DiscoverResponse.notFound(); - LwM2mLink[] instanceLink = linkUpdateAttributes(this.tbLinkFormatHelper.getInstanceDescription(this, path.getObjectInstanceId(), null), server); + LwM2mLink[] instanceLink = linkAddUpdateAttributes(this.tbLinkFormatHelper.getInstanceDescription(server, this, path.getObjectInstanceId(), null), server); return DiscoverResponse.success(instanceLink); } else if (path.isResource()) { @@ -666,46 +666,43 @@ public class TbLwm2mObjectEnabler extends BaseObjectEnabler implements Destroyab if (!getAvailableResourceIds(path.getObjectInstanceId()).contains(path.getResourceId())) return DiscoverResponse.notFound(); - LwM2mLink resourceLink = linkAddAttribute( - this.tbLinkFormatHelper.getResourceDescription(this, path.getObjectInstanceId(), path.getResourceId(), null), - server); - return DiscoverResponse.success(new LwM2mLink[] { resourceLink }); + LwM2mLink[] resourceLink = linkAddUpdateAttributes(this.tbLinkFormatHelper.getResourceDescription(server, + this, path.getObjectInstanceId(), path.getResourceId(), null), server); + return DiscoverResponse.success(resourceLink); } return DiscoverResponse.badRequest(null); } - private LwM2mLink[] linkUpdateAttributes(LwM2mLink[] links, LwM2mServer server) { - return Arrays.stream(links) - .map(link -> linkAddAttribute(link, server)) - .toArray(LwM2mLink[]::new); + private LwM2mLink[] linkAddUpdateAttributes(LwM2mLink[] links, LwM2mServer server) { + ArrayList resourceLinkList = new ArrayList<>(); + for (LwM2mLink link : links) { + + LwM2mAttributeSet lwM2mAttributeSetDop = null; + if (this.lwM2mAttributes.get(link.getPath()) != null) { + lwM2mAttributeSetDop = this.lwM2mAttributes.get(link.getPath()); + } + LwM2mAttribute resourceAttributeDim = getResourceAttributes(server, link.getPath()); + + Map> attributes = new HashMap<>(); + if (link.getAttributes() != null) { + for (LwM2mAttribute attr : link.getAttributes().getLwM2mAttributes()) { + attributes.put(attr.getName(), attr); + } + } + if (lwM2mAttributeSetDop != null) { + for (LwM2mAttribute attr : lwM2mAttributeSetDop.getLwM2mAttributes()) { + attributes.put(attr.getName(), attr); + } + } + if (resourceAttributeDim != null) { + attributes.put(resourceAttributeDim.getName(), resourceAttributeDim); + } + resourceLinkList.add(new LwM2mLink(link.getRootPath(), link.getPath(), attributes.values())); + } + return resourceLinkList.toArray(LwM2mLink[]::new); } - private LwM2mLink linkAddAttribute(LwM2mLink link, LwM2mServer server) { - - LwM2mAttributeSet lwM2mAttributeSetDop = null; - if (this.lwM2mAttributes.get(link.getPath())!= null){ - lwM2mAttributeSetDop = this.lwM2mAttributes.get(link.getPath()); - } - LwM2mAttribute resourceAttributeDim = getResourceAttributes (server, link.getPath()); - - Map > attributes = new HashMap<>(); - if (link.getAttributes() != null) { - for (LwM2mAttribute attr : link.getAttributes().getLwM2mAttributes()) { - attributes.put(attr.getName(), attr); - } - } - if (lwM2mAttributeSetDop != null) { - for (LwM2mAttribute attr : lwM2mAttributeSetDop.getLwM2mAttributes()) { - attributes.put(attr.getName(), attr); - } - } - if (resourceAttributeDim != null) { - attributes.put(resourceAttributeDim.getName(), resourceAttributeDim); - } - return new LwM2mLink(link.getRootPath(), link.getPath(), attributes.values()); - } - - protected LwM2mAttribute getResourceAttributes (LwM2mServer server, LwM2mPath path) { + protected LwM2mAttribute getResourceAttributes(LwM2mServer server, LwM2mPath path) { ResourceModel resourceModel = getObjectModel().resources.get(path.getResourceId()); if (path.isResource() && resourceModel.multiple) { return getResourceAttributeDim(path, server); @@ -717,13 +714,13 @@ public class TbLwm2mObjectEnabler extends BaseObjectEnabler implements Destroyab LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId()); try { ReadResponse readResponse = instance.read(server, path.getResourceId()); - if (readResponse.getCode().getCode()==205 && readResponse.getContent() instanceof LwM2mMultipleResource) { - long valueDim = ((LwM2mMultipleResource)readResponse.getContent()).getInstances().size(); + if (readResponse.getCode().getCode() == 205 && readResponse.getContent() instanceof LwM2mMultipleResource) { + long valueDim = ((LwM2mMultipleResource) readResponse.getContent()).getInstances().size(); return LwM2mAttributes.create(LwM2mAttributes.DIMENSION, valueDim); } else { return null; } - } catch (Exception e ){ + } catch (Exception e) { return null; } } diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationDiscoverWriteAttributesTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationDiscoverWriteAttributesTest.java index d7807ee24a..c44048849e 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationDiscoverWriteAttributesTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationDiscoverWriteAttributesTest.java @@ -49,6 +49,7 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL String actual = rpcActualResult.get("error").asText(); assertTrue(actual.equals(expected)); } + /** * WriteAttributes {"id":"/3_1.2/0/6","attributes":{"pmax":100, "pmin":10}} * if not implemented: @@ -82,7 +83,7 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL @Test public void testWriteAttributesResourceServerUriWithParametersById_Result_BAD_REQUEST() throws Exception { - String expectedPath = objectInstanceIdVer_1; + String expectedPath = objectInstanceIdVer_1; String actualResult = sendRPCReadById(expectedPath); String expectedValue = "{\"uri\":\"coaps://localhost:5690\"}"; actualResult = sendRPCExecuteWithValueById(expectedPath, expectedValue); @@ -108,13 +109,13 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL * "ver" only for objectId */ @Test - public void testReadDIM_3_0_6_Only_R () throws Exception { + public void testReadDIM_3_0_6_Only_R() throws Exception { String path = objectInstanceIdVer_3 + "/" + RESOURCE_ID_6; String actualResult = sendDiscover(path); ObjectNode rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CONTENT.getName(), rpcActualResult.get("result").asText()); String expected = ";dim=3"; - assertTrue(rpcActualResult.get("value").asText().equals(expected)); + assertTrue(rpcActualResult.get("value").asText().contains(expected)); } @@ -126,7 +127,7 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL * "ver" only for objectId */ @Test - public void testReadVer () throws Exception { + public void testReadVer() throws Exception { String path = objectIdVer_3; String actualResult = sendDiscover(path); ObjectNode rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); @@ -149,7 +150,7 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL String actualResult = sendRPCExecuteWithValueById(expectedPath, expectedValue); ObjectNode rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CHANGED.getName(), rpcActualResult.get("result").asText()); - // result changed + // result changed actualResult = sendDiscover(expectedPath); rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CONTENT.getName(), rpcActualResult.get("result").asText()); @@ -175,7 +176,7 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL * <3/0/6>;dim=8,<3/0/7>;gt=50;lt=42.2;st=0.5,<3/0/8>;... */ @Test - public void testWriteAttributesPeriodLtGt () throws Exception { + public void testWriteAttributesPeriodLtGt() throws Exception { String expectedPath = objectInstanceIdVer_3; String expectedValue = "{\"pmax\":60}"; String actualResult = sendRPCExecuteWithValueById(expectedPath, expectedValue); @@ -187,30 +188,33 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CHANGED.getName(), rpcActualResult.get("result").asText()); expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_7; - expectedValue ="{\"gt\":50, \"lt\":42.2, \"st\":0.5}"; + expectedValue = "{\"gt\":50.0, \"lt\":42.2, \"st\":0.5}"; actualResult = sendRPCExecuteWithValueById(expectedPath, expectedValue); rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CHANGED.getName(), rpcActualResult.get("result").asText()); - // ObjectId + // ObjectId expectedPath = objectIdVer_3; actualResult = sendDiscover(expectedPath); rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CONTENT.getName(), rpcActualResult.get("result").asText()); - // String expected = ";ver=1.2,;pmax=60,,,,,;dim=3,;st=0.5;lt=42.2;gt=50.0,,,,;dim=1,,,,,,,,,"; + String actualValue = rpcActualResult.get("value").asText(); String expected = ";ver=1.2,;pmax=65"; - assertTrue(rpcActualResult.get("value").asText().contains(expected)); - expected = ";dim=3,;st=0.5;lt=42.2;gt=50.0"; - assertTrue(rpcActualResult.get("value").asText().contains(expected)); - // ObjectInstanceId + assertTrue(actualValue.contains(expected)); + expected = ";dim=3"; + assertTrue(actualValue.contains(expected)); + expected = ";st=0.5;lt=42.2;gt=50"; + assertTrue(actualValue.contains(expected)); + // ObjectInstanceId expectedPath = objectInstanceIdVer_3; actualResult = sendDiscover(expectedPath); rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CONTENT.getName(), rpcActualResult.get("result").asText()); + actualValue = rpcActualResult.get("value").asText(); expected = ";pmax=65"; - assertTrue(rpcActualResult.get("value").asText().contains(expected)); - expected = ";dim=3,;st=0.5;lt=42.2;gt=50.0"; - assertTrue(rpcActualResult.get("value").asText().contains(expected)); - // ResourceId + assertTrue(actualValue.contains(expected)); + expected = ";dim=3,;st=0.5;lt=42.2;gt=50"; + assertTrue(actualValue.contains(expected)); + // ResourceId expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_6; actualResult = sendDiscover(expectedPath); rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); @@ -221,10 +225,10 @@ public class RpcLwm2mIntegrationDiscoverWriteAttributesTest extends AbstractRpcL actualResult = sendDiscover(expectedPath); rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.CONTENT.getName(), rpcActualResult.get("result").asText()); - expected = ";st=0.5;lt=42.2;gt=50.0"; + expected = ";st=0.5;lt=42.2;gt=50"; assertTrue(rpcActualResult.get("value").asText().contains(expected)); - // ResourceInstanceId - expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_6+ "/1"; + // ResourceInstanceId + expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_6 + "/1"; actualResult = sendDiscover(expectedPath); rpcActualResult = JacksonUtil.fromString(actualResult, ObjectNode.class); assertEquals(ResponseCode.INTERNAL_SERVER_ERROR.getName(), rpcActualResult.get("result").asText()); diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/secure/LwM2mCredentialsSecurityInfoValidator.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/secure/LwM2mCredentialsSecurityInfoValidator.java index a44c6adb6e..66b072fdaa 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/secure/LwM2mCredentialsSecurityInfoValidator.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/secure/LwM2mCredentialsSecurityInfoValidator.java @@ -18,7 +18,7 @@ package org.thingsboard.server.transport.lwm2m.secure; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.DecoderException; -import org.eclipse.leshan.core.util.SecurityUtil; +import org.eclipse.leshan.core.security.util.SecurityUtil; import org.eclipse.leshan.server.security.SecurityInfo; import org.springframework.stereotype.Component; import org.thingsboard.common.util.JacksonUtil; diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java index e958313d67..cce114f361 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java @@ -19,7 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.eclipse.leshan.core.SecurityMode; -import org.eclipse.leshan.core.util.SecurityUtil; +import org.eclipse.leshan.core.security.util.SecurityUtil; import org.hibernate.exception.ConstraintViolationException; import org.springframework.stereotype.Service; import org.springframework.transaction.event.TransactionalEventListener; diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java index 2d2c7bf21c..bfff00f6b1 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java @@ -18,7 +18,7 @@ package org.thingsboard.server.dao.service.validator; import com.google.protobuf.Descriptors; import com.google.protobuf.DynamicMessage; import lombok.extern.slf4j.Slf4j; -import org.eclipse.leshan.core.util.SecurityUtil; +import org.eclipse.leshan.core.security.util.SecurityUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; diff --git a/pom.xml b/pom.xml index 5e4cc50c7c..ea50c0fddb 100755 --- a/pom.xml +++ b/pom.xml @@ -74,8 +74,8 @@ 1.7.0 4.4.0 2.2.14 - 3.11.0 - 2.0.0-M14 + 3.12.1 + 2.0.0-M15 2.10.1 2.3.32 2.0.1