diff --git a/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesIntegrationTest.java index a7de7eb571..9ac2d5fc11 100644 --- a/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesIntegrationTest.java @@ -19,7 +19,6 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.californium.core.server.resources.Resource; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.coapserver.DefaultCoapServerService; @@ -60,15 +59,11 @@ public class CoapAttributesUpdatesIntegrationTest extends AbstractCoapAttributes processAfterTest(); } - - - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { processJsonTestSubscribeToAttributesUpdates(false); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testSubscribeToAttributesUpdatesFromTheServerWithEmptyCurrentStateNotification() throws Exception { processJsonTestSubscribeToAttributesUpdates(true); diff --git a/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesJsonIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesJsonIntegrationTest.java index 4755ba134d..3fa625796c 100644 --- a/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesJsonIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesJsonIntegrationTest.java @@ -18,7 +18,6 @@ package org.thingsboard.server.transport.coap.attributes.updates; import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.server.common.data.CoapDeviceType; import org.thingsboard.server.common.data.TransportPayloadType; @@ -45,12 +44,11 @@ public class CoapAttributesUpdatesJsonIntegrationTest extends AbstractCoapAttrib processAfterTest(); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { processJsonTestSubscribeToAttributesUpdates(false); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 + @Test public void testSubscribeToAttributesUpdatesFromTheServerWithEmptyCurrentStateNotification() throws Exception { processJsonTestSubscribeToAttributesUpdates(true); diff --git a/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesProtoIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesProtoIntegrationTest.java index 1c9589bba9..4b07c8dcc2 100644 --- a/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesProtoIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/coap/attributes/updates/CoapAttributesUpdatesProtoIntegrationTest.java @@ -18,7 +18,6 @@ package org.thingsboard.server.transport.coap.attributes.updates; import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.server.common.data.CoapDeviceType; import org.thingsboard.server.common.data.TransportPayloadType; @@ -44,12 +43,10 @@ public class CoapAttributesUpdatesProtoIntegrationTest extends AbstractCoapAttri public void afterTest() throws Exception { processAfterTest(); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { processProtoTestSubscribeToAttributesUpdates(false); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testSubscribeToAttributesUpdatesFromTheServerWithEmptyCurrentStateNotification() throws Exception { processProtoTestSubscribeToAttributesUpdates(true); diff --git a/application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientIntegrationTest.java index e7a7485ef5..112d3e6aa5 100644 --- a/application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientIntegrationTest.java @@ -26,7 +26,6 @@ import org.eclipse.californium.core.coap.CoAP; import org.eclipse.californium.core.coap.MediaTypeRegistry; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.id.DeviceId; @@ -69,7 +68,6 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest { private static final List EXPECTED_KEYS = Arrays.asList("key1", "key2", "key3", "key4", "key5"); private static final String DEVICE_RESPONSE = "{\"value1\":\"A\",\"value2\":\"B\"}"; - @Before public void beforeTest() throws Exception { CoapTestConfigProperties configProperties = CoapTestConfigProperties.builder() @@ -83,7 +81,6 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest { processAfterTest(); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testConfirmableRequests() throws Exception { boolean confirmable = true; @@ -92,7 +89,6 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest { processTestRequestAttributesValuesFromTheServer(confirmable); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testNonConfirmableRequests() throws Exception { boolean confirmable = false; diff --git a/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcDefaultIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcDefaultIntegrationTest.java index 8f9d3379ef..df1159b128 100644 --- a/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcDefaultIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcDefaultIntegrationTest.java @@ -20,7 +20,6 @@ import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.server.dao.service.DaoSqlTest; import org.thingsboard.server.service.security.AccessValidator; @@ -83,13 +82,11 @@ public class CoapServerSideRpcDefaultIntegrationTest extends AbstractCoapServerS Assert.assertEquals(AccessValidator.DEVICE_WITH_REQUESTED_ID_NOT_FOUND, result); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testServerCoapOneWayRpc() throws Exception { processOneWayRpcTest(false); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testServerCoapTwoWayRpc() throws Exception { processTwoWayRpcTest("{\"value1\":\"A\",\"value2\":\"B\"}", false); diff --git a/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcJsonIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcJsonIntegrationTest.java index 34d678b5b5..5c94e91b72 100644 --- a/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcJsonIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcJsonIntegrationTest.java @@ -18,7 +18,6 @@ package org.thingsboard.server.transport.coap.rpc; import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.server.common.data.CoapDeviceType; import org.thingsboard.server.common.data.TransportPayloadType; @@ -44,13 +43,11 @@ public class CoapServerSideRpcJsonIntegrationTest extends AbstractCoapServerSide processAfterTest(); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testServerCoapOneWayRpc() throws Exception { processOneWayRpcTest(false); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testServerCoapTwoWayRpc() throws Exception { processTwoWayRpcTest("{\"value1\":\"A\",\"value2\":\"B\"}", false); diff --git a/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcProtoIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcProtoIntegrationTest.java index 138f87e964..2f7d46a390 100644 --- a/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcProtoIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/coap/rpc/CoapServerSideRpcProtoIntegrationTest.java @@ -18,7 +18,6 @@ package org.thingsboard.server.transport.coap.rpc; import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.server.common.data.CoapDeviceType; import org.thingsboard.server.common.data.TransportPayloadType; @@ -45,13 +44,11 @@ public class CoapServerSideRpcProtoIntegrationTest extends AbstractCoapServerSid processAfterTest(); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testServerCoapOneWayRpc() throws Exception { processOneWayRpcTest(true); } - @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215 @Test public void testServerCoapTwoWayRpc() throws Exception { processTwoWayRpcTest("{\"payload\":\"{\\\"value1\\\":\\\"A\\\",\\\"value2\\\":\\\"B\\\"}\"}", true); diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClient.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClient.java index 29df8f7f65..cb391b27e9 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClient.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClient.java @@ -229,7 +229,7 @@ public class LwM2mClient { this.resources.get(pathRezIdVer).updateLwM2mResource(resource, mode); return true; } else { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathRezIdVer)); + LwM2mPath pathIds = getLwM2mPathFromString(pathRezIdVer); ResourceModel resourceModel = modelProvider.getObjectModel(registration).getResourceModel(pathIds.getObjectId(), pathIds.getResourceId()); if (resourceModel != null) { this.resources.put(pathRezIdVer, new ResourceValue(resource, resourceModel)); @@ -257,7 +257,7 @@ public class LwM2mClient { } public String getRezIdByResourceNameAndObjectInstanceId(String resourceName, String pathObjectInstanceIdVer, LwM2mModelProvider modelProvider) { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathObjectInstanceIdVer)); + LwM2mPath pathIds = getLwM2mPathFromString(pathObjectInstanceIdVer); if (pathIds.isObjectInstance()) { Set rezIds = modelProvider.getObjectModel(registration) .getObjectModel(pathIds.getObjectId()).resources.entrySet() @@ -271,7 +271,7 @@ public class LwM2mClient { } public ResourceModel getResourceModel(String pathIdVer, LwM2mModelProvider modelProvider) { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathIdVer)); + LwM2mPath pathIds = getLwM2mPathFromString(pathIdVer); String verSupportedObject = String.valueOf(registration.getSupportedObject().get(pathIds.getObjectId())); String verRez = getVerFromPathIdVerOrId(pathIdVer); return verRez != null && verRez.equals(verSupportedObject) ? modelProvider.getObjectModel(registration) @@ -289,7 +289,7 @@ public class LwM2mClient { public ObjectModel getObjectModel(String pathIdVer, LwM2mModelProvider modelProvider) { try { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathIdVer)); + LwM2mPath pathIds = getLwM2mPathFromString(pathIdVer); String verSupportedObject = String.valueOf(registration.getSupportedObject().get(pathIds.getObjectId())); String verRez = getVerFromPathIdVerOrId(pathIdVer); return verRez != null && verRez.equals(verSupportedObject) ? modelProvider.getObjectModel(registration) @@ -309,7 +309,7 @@ public class LwM2mClient { public Collection getNewResourceForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider, LwM2mValueConverter converter) { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathRezIdVer)); + LwM2mPath pathIds = getLwM2mPathFromString(pathRezIdVer); Collection resources = ConcurrentHashMap.newKeySet(); Map resourceModels = modelProvider.getObjectModel(registration) .getObjectModel(pathIds.getObjectId()).resources; @@ -329,7 +329,7 @@ public class LwM2mClient { */ public Collection getNewResourcesForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider, LwM2mValueConverter converter) { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathRezIdVer)); + LwM2mPath pathIds = getLwM2mPathFromString(pathRezIdVer); Collection resources = ConcurrentHashMap.newKeySet(); Map resourceModels = modelProvider.getObjectModel(registration) .getObjectModel(pathIds.getObjectId()).resources; @@ -370,7 +370,7 @@ public class LwM2mClient { } public String isValidObjectVersion(String path) { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(path)); + LwM2mPath pathIds = getLwM2mPathFromString(path); LwM2m.Version verSupportedObject = registration.getSupportedObject().get(pathIds.getObjectId()); if (verSupportedObject == null) { return String.format("Specified object id %s absent in the list supported objects of the client or is security object!", pathIds.getObjectId()); @@ -390,7 +390,7 @@ public class LwM2mClient { public void deleteResources(String pathIdVer, LwM2mModelProvider modelProvider) { Set key = getKeysEqualsIdVer(pathIdVer); key.forEach(pathRez -> { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathRez)); + LwM2mPath pathIds = getLwM2mPathFromString(pathRez); ResourceModel resourceModel = modelProvider.getObjectModel(registration).getResourceModel(pathIds.getObjectId(), pathIds.getResourceId()); if (resourceModel != null) { this.resources.get(pathRez).setResourceModel(resourceModel); @@ -410,7 +410,7 @@ public class LwM2mClient { } private void saveResourceModel(String pathRez, LwM2mModelProvider modelProvider) { - LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathRez)); + LwM2mPath pathIds = getLwM2mPathFromString(pathRez); ResourceModel resourceModel = modelProvider.getObjectModel(registration).getResourceModel(pathIds.getObjectId(), pathIds.getResourceId()); this.resources.get(pathRez).setResourceModel(resourceModel); } @@ -456,5 +456,9 @@ public class LwM2mClient { return result; } + public LwM2mPath getLwM2mPathFromString(String path) { + return new LwM2mPath(fromVersionedIdToObjectId(path)); + } + } diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDes.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDes.java index 30800eb9ee..17fef8acb0 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDes.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDes.java @@ -15,8 +15,12 @@ */ package org.thingsboard.server.transport.lwm2m.server.store.util; -import com.fasterxml.jackson.annotation.JsonValue; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.protobuf.util.JsonFormat; import lombok.SneakyThrows; import org.eclipse.leshan.core.model.ResourceModel; @@ -25,23 +29,35 @@ import org.eclipse.leshan.core.node.LwM2mNodeException; import org.eclipse.leshan.core.node.LwM2mResource; import org.eclipse.leshan.core.node.LwM2mSingleResource; import org.eclipse.leshan.core.node.ObjectLink; +import org.eclipse.leshan.core.util.datatype.ULong; import org.eclipse.leshan.server.redis.serialization.RegistrationSerDes; +import org.thingsboard.server.common.data.device.data.PowerMode; +import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.gen.transport.TransportProtos; +import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientState; import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; +import org.thingsboard.server.transport.lwm2m.server.client.ResourceValue; import java.lang.reflect.Field; +import java.time.Instant; import java.util.Base64; import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicLong; + +import static org.thingsboard.common.util.JacksonUtil.toJsonNode; public class LwM2MClientSerDes { public static final String VALUE = "value"; + private static final RegistrationSerDes registrationSerDes = new RegistrationSerDes(); @SneakyThrows public static byte[] serialize(LwM2mClient client) { JsonObject o = new JsonObject(); - o.addProperty("nodeId", "client.getNodeId()"); + o.addProperty("nodeId", client.getNodeId()); o.addProperty("endpoint", client.getEndpoint()); JsonObject resources = new JsonObject(); @@ -92,8 +108,15 @@ public class LwM2MClientSerDes { o.addProperty("pagingTransmissionWindow", client.getPagingTransmissionWindow()); } if (client.getRegistration() != null) { - RegistrationSerDes regDez = new RegistrationSerDes(); - o.addProperty("registration", regDez.jSerialize(client.getRegistration()).toString()); + String registrationAddress = client.getRegistration().getAddress().toString(); + JsonNode registrationNode = registrationSerDes.jSerialize(client.getRegistration()); + if (!registrationAddress.equals(registrationNode.get("transportdata").get("address").asText())){ + ObjectNode actualRegAddress = (ObjectNode)registrationNode.get("transportdata"); + actualRegAddress.put("address", registrationAddress); + ObjectNode actualIdentity = (ObjectNode) actualRegAddress.get("identity"); + actualIdentity.put("address", registrationAddress); + } + o.addProperty("registration", registrationNode.toString()); } o.addProperty("asleep", client.isAsleep()); o.addProperty("lastUplinkTime", client.getLastUplinkTime()); @@ -138,33 +161,35 @@ public class LwM2MClientSerDes { ResourceModel.Type type = ResourceModel.Type.valueOf(o.get("type").getAsString()); if (multiInstances) { Map instances = new HashMap<>(); - o.get("instances").getAsJsonArray().forEach(entry -> { -// instances.put(Integer.valueOf(entry.getAsJsonObject().), parseValue(type, entry.getValue())); - }); + for (Entry entry : o.get("instances").getAsJsonObject().entrySet()) { + JsonObject instance = entry.getValue().getAsJsonObject(); + instances.put(Integer.valueOf(instance.get("id").getAsString()), parseValue(type, instance.get(VALUE))); + } return LwM2mMultipleResource.newResource(id, instances, type); } else { - return LwM2mSingleResource.newResource(id, parseValue(type, (JsonValue) o.get(VALUE))); + return LwM2mSingleResource.newResource(id, parseValue(type, o.get(VALUE))); } } - private static Object parseValue(ResourceModel.Type type, JsonValue value) { + + private static Object parseValue(ResourceModel.Type type, JsonElement value) { switch (type) { case INTEGER: - return value.value(); -// case FLOAT: -// return value.value(); -// case BOOLEAN: -// return value.asBoolean(); -// case OPAQUE: -// return Base64.getDecoder().decode(value.asString()); -// case STRING: -// return value.asString(); -// case TIME: -// return new Date(value.asLong()); -// case OBJLNK: -// return ObjectLink.decodeFromString(value.asString()); -// case UNSIGNED_INTEGER: -// return ULong.valueOf(value.asString()); + return value.getAsInt(); + case FLOAT: + return value.getAsDouble(); + case BOOLEAN: + return value.getAsBoolean(); + case OPAQUE: + return Base64.getDecoder().decode(value.getAsString()); + case STRING: + return value.getAsString(); + case TIME: + return Instant.ofEpochMilli(value.getAsLong()); + case OBJLNK: + return ObjectLink.decodeFromString(value.getAsString()); + case UNSIGNED_INTEGER: + return ULong.valueOf(value.getAsString()); default: throw new LwM2mNodeException(String.format("Type %s is not supported", type.name())); } @@ -212,7 +237,7 @@ public class LwM2MClientSerDes { o.addProperty(VALUE, Base64.getEncoder().encodeToString((byte[]) value)); break; case STRING: - o.addProperty(VALUE, (String) value); + o.addProperty(VALUE, String.valueOf(value)); break; case TIME: o.addProperty(VALUE, ((Date) value).getTime()); @@ -221,7 +246,7 @@ public class LwM2MClientSerDes { o.addProperty(VALUE, ((ObjectLink) value).encodeToString()); break; case UNSIGNED_INTEGER: - o.addProperty(VALUE, value.toString()); + o.addProperty(VALUE, Integer.toUnsignedString((int)value)); break; default: throw new LwM2mNodeException(String.format("Type %s is not supported", type.name())); @@ -230,111 +255,111 @@ public class LwM2MClientSerDes { @SneakyThrows public static LwM2mClient deserialize(byte[] data) { -// JsonObject o = new JsonObject(new String(data))); -// LwM2mClient lwM2mClient = new LwM2mClient(o.get("nodeId").getAsString(), o.get("endpoint").getAsString()); - LwM2mClient lwM2mClient = new LwM2mClient("nodeId", "endpoint"); -// o.get("resources").getAsJsonObject().forEach(entry -> { -// JsonObject resource = entry.getValue().asObject(); -// LwM2mResource lwM2mResource = parseLwM2mResource(resource.get("lwM2mResource").getAsJsonObject()); -// ResourceModel resourceModel = parseResourceModel(resource.get("resourceModel").asObject()); -// ResourceValue resourceValue = new ResourceValue(lwM2mResource, resourceModel); -// lwM2mClient.getResources().put(entry.getName(), resourceValue); -// }); -// -// for (JsonObject.Member entry : o.get("sharedAttributes").asObject()) { -// TransportProtos.TsKvProto.Builder builder = TransportProtos.TsKvProto.newBuilder(); -// JsonFormat.parser().merge(entry.getValue().getAsString(), builder); -// lwM2mClient.getSharedAttributes().put(entry.getName(), builder.build()); -// } -// -// o.get("keyTsLatestMap").asObject().forEach(entry -> { -// lwM2mClient.getKeyTsLatestMap().put(entry.getName(), new AtomicLong(entry.getValue().asLong())); -// }); -// -// lwM2mClient.setState(LwM2MClientState.valueOf(o.get("state").getAsString())); -// -// Class lwM2mClientClass = LwM2mClient.class; -// -// JsonValue session = o.get("session"); -// if (session != null) { -// TransportProtos.SessionInfoProto.Builder builder = TransportProtos.SessionInfoProto.newBuilder(); -// JsonFormat.parser().merge(session.asString(), builder); -// -// Field sessionField = lwM2mClientClass.getDeclaredField("session"); -// sessionField.setAccessible(true); -// sessionField.set(lwM2mClient, builder.build()); -// } -// -// JsonValue tenantId = o.get("tenantId"); -// if (tenantId != null) { -// Field tenantIdField = lwM2mClientClass.getDeclaredField("tenantId"); -// tenantIdField.setAccessible(true); -// tenantIdField.set(lwM2mClient, new TenantId(UUID.fromString(tenantId.asString()))); -// } -// -// JsonValue deviceId = o.get("deviceId"); -// if (tenantId != null) { -// Field deviceIdField = lwM2mClientClass.getDeclaredField("deviceId"); -// deviceIdField.setAccessible(true); -// deviceIdField.set(lwM2mClient, UUID.fromString(deviceId.asString())); -// } -// -// JsonValue profileId = o.get("profileId"); -// if (tenantId != null) { -// Field profileIdField = lwM2mClientClass.getDeclaredField("profileId"); -// profileIdField.setAccessible(true); -// profileIdField.set(lwM2mClient, UUID.fromString(profileId.asString())); -// } -// -// JsonValue powerMode = o.get("powerMode"); -// if (powerMode != null) { -// Field powerModeField = lwM2mClientClass.getDeclaredField("powerMode"); -// powerModeField.setAccessible(true); -// powerModeField.set(lwM2mClient, PowerMode.valueOf(powerMode.asString())); -// } -// -// JsonValue edrxCycle = o.get("edrxCycle"); -// if (edrxCycle != null) { -// Field edrxCycleField = lwM2mClientClass.getDeclaredField("edrxCycle"); -// edrxCycleField.setAccessible(true); -// edrxCycleField.set(lwM2mClient, edrxCycle.asLong()); -// } -// -// JsonValue psmActivityTimer = o.get("psmActivityTimer"); -// if (psmActivityTimer != null) { -// Field psmActivityTimerField = lwM2mClientClass.getDeclaredField("psmActivityTimer"); -// psmActivityTimerField.setAccessible(true); -// psmActivityTimerField.set(lwM2mClient, psmActivityTimer.asLong()); -// } -// -// JsonValue pagingTransmissionWindow = o.get("pagingTransmissionWindow"); -// if (pagingTransmissionWindow != null) { -// Field pagingTransmissionWindowField = lwM2mClientClass.getDeclaredField("pagingTransmissionWindow"); -// pagingTransmissionWindowField.setAccessible(true); -// pagingTransmissionWindowField.set(lwM2mClient, pagingTransmissionWindow.asLong()); -// } -// -// JsonValue registration = o.get("registration"); -// if (registration != null) { -// lwM2mClient.setRegistration(RegistrationSerDes.deserialize(registration.asObject())); -// } -// -// lwM2mClient.setAsleep(o.get("asleep").getAsBoolean()); -// -// Field lastUplinkTimeField = lwM2mClientClass.getDeclaredField("lastUplinkTime"); -// lastUplinkTimeField.setAccessible(true); -// lastUplinkTimeField.setLong(lwM2mClient, o.get("lastUplinkTime").asLong()); -// -// Field firstEdrxDownlinkField = lwM2mClientClass.getDeclaredField("firstEdrxDownlink"); -// firstEdrxDownlinkField.setAccessible(true); -// firstEdrxDownlinkField.setBoolean(lwM2mClient, o.get("firstEdrxDownlink").getAsBoolean()); -// -// lwM2mClient.getRetryAttempts().set(o.get("retryAttempts").asInt()); -// -// JsonValue lastSentRpcId = o.get("lastSentRpcId"); -// if (lastSentRpcId != null) { -// lwM2mClient.setLastSentRpcId(UUID.fromString(lastSentRpcId.asString())); -// } + JsonObject o = JsonParser.parseString(new String(data)).getAsJsonObject(); + LwM2mClient lwM2mClient = new LwM2mClient(o.get("nodeId").getAsString(), o.get("endpoint").getAsString()); + + o.get("resources").getAsJsonObject().entrySet().forEach(entry -> { + JsonObject resource = entry.getValue().getAsJsonObject(); + LwM2mResource lwM2mResource = parseLwM2mResource(resource.get("lwM2mResource").getAsJsonObject()); + ResourceModel resourceModel = parseResourceModel(resource.get("resourceModel").getAsJsonObject()); + ResourceValue resourceValue = new ResourceValue(lwM2mResource, resourceModel); + lwM2mClient.getResources().put(String.valueOf(lwM2mResource.getId()), resourceValue); + }); + + for (Entry entry : o.get("sharedAttributes").getAsJsonObject().entrySet()) { + TransportProtos.TsKvProto.Builder builder = TransportProtos.TsKvProto.newBuilder(); + JsonFormat.parser().merge(entry.getValue().getAsString(), builder); + lwM2mClient.getSharedAttributes().put(entry.getKey(), builder.build()); + } + + o.get("keyTsLatestMap").getAsJsonObject().entrySet().forEach(entry -> { + lwM2mClient.getKeyTsLatestMap().put(entry.getKey(), new AtomicLong(entry.getValue().getAsLong())); + }); + + lwM2mClient.setState(LwM2MClientState.valueOf(o.get("state").getAsString())); + + Class lwM2mClientClass = LwM2mClient.class; + + JsonElement session = o.get("session"); + if (session != null) { + TransportProtos.SessionInfoProto.Builder builder = TransportProtos.SessionInfoProto.newBuilder(); + JsonFormat.parser().merge(session.getAsString(), builder); + + Field sessionField = lwM2mClientClass.getDeclaredField("session"); + sessionField.setAccessible(true); + sessionField.set(lwM2mClient, builder.build()); + } + + JsonElement tenantId = o.get("tenantId"); + if (tenantId != null) { + Field tenantIdField = lwM2mClientClass.getDeclaredField("tenantId"); + tenantIdField.setAccessible(true); + tenantIdField.set(lwM2mClient, new TenantId(UUID.fromString(tenantId.getAsString()))); + } + + JsonElement deviceId = o.get("deviceId"); + if (tenantId != null) { + Field deviceIdField = lwM2mClientClass.getDeclaredField("deviceId"); + deviceIdField.setAccessible(true); + deviceIdField.set(lwM2mClient, UUID.fromString(deviceId.getAsString())); + } + + JsonElement profileId = o.get("profileId"); + if (tenantId != null) { + Field profileIdField = lwM2mClientClass.getDeclaredField("profileId"); + profileIdField.setAccessible(true); + profileIdField.set(lwM2mClient, UUID.fromString(profileId.getAsString())); + } + + JsonElement powerMode = o.get("powerMode"); + if (powerMode != null) { + Field powerModeField = lwM2mClientClass.getDeclaredField("powerMode"); + powerModeField.setAccessible(true); + powerModeField.set(lwM2mClient, PowerMode.valueOf(powerMode.getAsString())); + } + + JsonElement edrxCycle = o.get("edrxCycle"); + if (edrxCycle != null) { + Field edrxCycleField = lwM2mClientClass.getDeclaredField("edrxCycle"); + edrxCycleField.setAccessible(true); + edrxCycleField.set(lwM2mClient, edrxCycle.getAsLong()); + } + + JsonElement psmActivityTimer = o.get("psmActivityTimer"); + if (psmActivityTimer != null) { + Field psmActivityTimerField = lwM2mClientClass.getDeclaredField("psmActivityTimer"); + psmActivityTimerField.setAccessible(true); + psmActivityTimerField.set(lwM2mClient, psmActivityTimer.getAsLong()); + } + + JsonElement pagingTransmissionWindow = o.get("pagingTransmissionWindow"); + if (pagingTransmissionWindow != null) { + Field pagingTransmissionWindowField = lwM2mClientClass.getDeclaredField("pagingTransmissionWindow"); + pagingTransmissionWindowField.setAccessible(true); + pagingTransmissionWindowField.set(lwM2mClient, pagingTransmissionWindow.getAsLong()); + } + + JsonElement registration = o.get("registration"); + if (registration != null) { + lwM2mClient.setRegistration(registrationSerDes.deserialize(toJsonNode(registration.getAsString()))); + } + + lwM2mClient.setAsleep(o.get("asleep").getAsBoolean()); + + Field lastUplinkTimeField = lwM2mClientClass.getDeclaredField("lastUplinkTime"); + lastUplinkTimeField.setAccessible(true); + lastUplinkTimeField.setLong(lwM2mClient, o.get("lastUplinkTime").getAsLong()); + + Field firstEdrxDownlinkField = lwM2mClientClass.getDeclaredField("firstEdrxDownlink"); + firstEdrxDownlinkField.setAccessible(true); + firstEdrxDownlinkField.setBoolean(lwM2mClient, o.get("firstEdrxDownlink").getAsBoolean()); + + lwM2mClient.getRetryAttempts().set(o.get("retryAttempts").getAsInt()); + + JsonElement lastSentRpcId = o.get("lastSentRpcId"); + if (lastSentRpcId != null) { + lwM2mClient.setLastSentRpcId(UUID.fromString(lastSentRpcId.getAsString())); + } return lwM2mClient; } diff --git a/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/bootstrap/LwM2MTransportBootstrapServiceTest.java b/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/bootstrap/LwM2MTransportBootstrapServiceTest.java deleted file mode 100644 index 9f49f3b138..0000000000 --- a/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/bootstrap/LwM2MTransportBootstrapServiceTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Copyright © 2016-2024 The Thingsboard Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.thingsboard.server.transport.lwm2m.bootstrap; - -import org.eclipse.californium.core.network.CoapEndpoint; -import org.eclipse.californium.scandium.config.DtlsConnectorConfig; -import org.junit.Ignore; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.test.util.ReflectionTestUtils; -import org.thingsboard.server.common.transport.TransportService; -import org.thingsboard.server.transport.lwm2m.bootstrap.secure.TbLwM2MDtlsBootstrapCertificateVerifier; -import org.thingsboard.server.transport.lwm2m.bootstrap.store.LwM2MBootstrapSecurityStore; -import org.thingsboard.server.transport.lwm2m.bootstrap.store.LwM2MInMemoryBootstrapConfigStore; -import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportBootstrapConfig; -import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.BDDMockito.when; - -@ExtendWith(MockitoExtension.class) -public class LwM2MTransportBootstrapServiceTest { - - @Mock - private LwM2MTransportServerConfig serverConfig; - @Mock - private LwM2MTransportBootstrapConfig bootstrapConfig; - @Mock - private LwM2MBootstrapSecurityStore lwM2MBootstrapSecurityStore; - @Mock - private LwM2MInMemoryBootstrapConfigStore lwM2MInMemoryBootstrapConfigStore; - @Mock - private TransportService transportService; - @Mock - private TbLwM2MDtlsBootstrapCertificateVerifier certificateVerifier; - - @Disabled // fixme: nick - @Test - public void getLHServer_creates_ConnectionIdGenerator_when_connection_id_length_not_null(){ - final Integer CONNECTION_ID_LENGTH = 6; - when(serverConfig.getDtlsCidLength()).thenReturn(CONNECTION_ID_LENGTH); - var lwM2MBootstrapService = createLwM2MBootstrapService(); - - var server = lwM2MBootstrapService.getLhBootstrapServer(); - var securedEndpoint = (CoapEndpoint) ReflectionTestUtils.getField(server, "securedEndpoint"); - assertThat(securedEndpoint).isNotNull(); - - var config = (DtlsConnectorConfig) ReflectionTestUtils.getField(securedEndpoint.getConnector(), "config"); - assertThat(config).isNotNull(); - assertThat(config.getConnectionIdGenerator()).isNotNull(); - assertThat((Integer) ReflectionTestUtils.getField(config.getConnectionIdGenerator(), "connectionIdLength")) - .isEqualTo(CONNECTION_ID_LENGTH); - } - - @Disabled // fixme: nick - @Test - public void getLHServer_creates_no_ConnectionIdGenerator_when_connection_id_length_is_null(){ - when(serverConfig.getDtlsCidLength()).thenReturn(null); - var lwM2MBootstrapService = createLwM2MBootstrapService(); - - var server = lwM2MBootstrapService.getLhBootstrapServer(); - var securedEndpoint = (CoapEndpoint) ReflectionTestUtils.getField(server, "securedEndpoint"); - assertThat(securedEndpoint).isNotNull(); - - var config = (DtlsConnectorConfig) ReflectionTestUtils.getField(securedEndpoint.getConnector(), "config"); - assertThat(config).isNotNull(); - assertThat(config.getConnectionIdGenerator()).isNull(); - } - - private LwM2MTransportBootstrapService createLwM2MBootstrapService() { - setDefaultConfigVariables(); - return new LwM2MTransportBootstrapService(serverConfig, bootstrapConfig, lwM2MBootstrapSecurityStore, - lwM2MInMemoryBootstrapConfigStore, transportService, certificateVerifier); - } - - private void setDefaultConfigVariables(){ - when(bootstrapConfig.getPort()).thenReturn(5683); - when(bootstrapConfig.getSecurePort()).thenReturn(5684); - when(serverConfig.isRecommendedCiphers()).thenReturn(false); - when(serverConfig.getDtlsRetransmissionTimeout()).thenReturn(9000); - } - - -} \ No newline at end of file diff --git a/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientTest.java b/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientTest.java index 5bc334756d..8fe58a5276 100644 --- a/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientTest.java +++ b/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientTest.java @@ -15,9 +15,10 @@ */ package org.thingsboard.server.transport.lwm2m.server.client; +import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; +import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.server.registration.Registration; -import org.junit.Ignore; import org.junit.Test; import org.junit.jupiter.api.Assertions; @@ -25,14 +26,14 @@ import java.net.InetSocketAddress; public class LwM2mClientTest { - @Ignore @Test public void setRegistration() { LwM2mClient client = new LwM2mClient("nodeId", "testEndpoint"); - Registration registration = null; /*new Registration - .Builder("test", "testEndpoint", Identity.unsecure(new InetSocketAddress(1000))) // FIXME: nick + Registration registration = new Registration + .Builder("testId", "testEndpoint", new IpPeer(new InetSocketAddress(1000)), + EndpointUriUtil.createUri("coap://localhost:5685")) .objectLinks(new Link[0]) - .build();*/ + .build(); Assertions.assertDoesNotThrow(() -> client.setRegistration(registration)); } diff --git a/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDesTest.java b/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDesTest.java index 43187f2a36..b3473a67b2 100644 --- a/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDesTest.java +++ b/common/transport/lwm2m/src/test/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MClientSerDesTest.java @@ -15,13 +15,17 @@ */ package org.thingsboard.server.transport.lwm2m.server.store.util; +import org.eclipse.leshan.core.LwM2m.LwM2mVersion; +import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.node.LwM2mMultipleResource; +import org.eclipse.leshan.core.node.LwM2mPath; import org.eclipse.leshan.core.node.LwM2mResource; import org.eclipse.leshan.core.node.LwM2mSingleResource; +import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.request.WriteRequest; import org.eclipse.leshan.server.registration.Registration; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.server.common.data.TbResource; import org.thingsboard.server.common.data.device.data.PowerMode; @@ -41,9 +45,12 @@ import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; import org.thingsboard.server.transport.lwm2m.server.client.ResourceValue; +import java.net.Inet4Address; +import java.net.InetSocketAddress; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; +import java.util.Map.Entry; import java.util.Optional; import java.util.UUID; @@ -56,10 +63,9 @@ import static org.mockito.Mockito.when; public class LwM2MClientSerDesTest { - @Ignore @Test public void serializeDeserialize() throws Exception { - LwM2mClient client = new LwM2mClient("nodeId", "testEndpoint"); + LwM2mClient client = new LwM2mClient("nodeId", "endpoint"); TransportDeviceInfo tdi = new TransportDeviceInfo(); tdi.setPowerMode(PowerMode.PSM); @@ -78,13 +84,13 @@ public class LwM2MClientSerDesTest { client.init(credentialsResponse, UUID.randomUUID()); - Registration registration = null; // FIXME: nick -// new Registration.Builder("test", "testEndpoint", Identity -// .unsecure(new InetSocketAddress(1000))) -// .supportedContentFormats() -// .supportedObjects(Map.of(15, "1.0", 17, "1.0")) -// .objectLinks(new Link[]{new Link("/")}) -// .build(); + Registration registration = new Registration + .Builder("test", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 1000)), + EndpointUriUtil.createUri("coap://localhost:5685")) + .supportedContentFormats() + .supportedObjects(Map.of(15, LwM2mVersion.V1_0, 17, LwM2mVersion.V1_0)) + .objectLinks(new Link[] { new Link("/15"), new Link("/17") }) + .build(); client.setRegistration(registration); client.setState(LwM2MClientState.REGISTERED); @@ -130,7 +136,12 @@ public class LwM2MClientSerDesTest { assertEquals(client.getPsmActivityTimer(), desClient.getPsmActivityTimer()); assertEquals(client.getPagingTransmissionWindow(), desClient.getPagingTransmissionWindow()); assertEquals(client.getEdrxCycle(), desClient.getEdrxCycle()); - assertEquals(client.getRegistration(), desClient.getRegistration()); + if (((IpPeer)desClient.getRegistration().getClientTransportData()).getSocketAddress().isUnresolved()) { + String actualReg = desClient.getRegistration().toString().replaceAll("/", ""); + assertEquals(client.getRegistration().toString(), actualReg); + } else { + assertEquals(client.getRegistration(), desClient.getRegistration()); + } assertEquals(client.isAsleep(), desClient.isAsleep()); assertEquals(client.getLastUplinkTime(), desClient.getLastUplinkTime()); assertEquals(client.getSleepTask(), desClient.getSleepTask()); @@ -143,7 +154,11 @@ public class LwM2MClientSerDesTest { Map actualResources = desClient.getResources(); assertNotNull(actualResources); assertEquals(expectedResources.size(), actualResources.size()); - expectedResources.forEach((key, value) -> assertEquals(value.toString(), actualResources.get(key).toString())); + for (Entry entry : expectedResources.entrySet()) { + LwM2mPath expectedPathId = client.getLwM2mPathFromString(entry.getKey().toString()); + String actualOld = actualResources.get(String.valueOf(expectedPathId.getObjectId())).toString(); + String actual = actualOld.replaceAll("\"", ""); + assertEquals(entry.getValue().toString(), actual); + } } - } \ No newline at end of file diff --git a/pom.xml b/pom.xml index d2450e0a65..5f392f7267 100755 --- a/pom.xml +++ b/pom.xml @@ -72,7 +72,7 @@ 1.3.4 4.2.1 2.2.6 - 3.10.0 + 3.11.0 2.0.0-M14 2.9.0 2.3.30 @@ -830,7 +830,6 @@ src/main/scripts/control/** src/main/scripts/windows/** src/main/resources/public/static/rulenode/** - src/main/java/org/eclipse/leshan/server/observation//** **/*.proto.js docker/haproxy/** docker/tb-node/** diff --git a/transport/lwm2m/src/main/java/org/eclipse/leshan/server/observation/ObservationServiceImpl.java b/transport/lwm2m/src/main/java/org/eclipse/leshan/server/observation/ObservationServiceImpl.java deleted file mode 100644 index 89a2f07179..0000000000 --- a/transport/lwm2m/src/main/java/org/eclipse/leshan/server/observation/ObservationServiceImpl.java +++ /dev/null @@ -1,296 +0,0 @@ -/** - * Copyright © 2016-2024 The Thingsboard Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (c) 2016 Sierra Wireless and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v2.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v20.html - * and the Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.html. - * - * Contributors: - * Sierra Wireless - initial API and implementation - * Michał Wadowski (Orange) - Add Observe-Composite feature. - */ -package org.eclipse.leshan.server.observation; - -import lombok.extern.slf4j.Slf4j; -import org.eclipse.leshan.core.node.LwM2mPath; -import org.eclipse.leshan.core.observation.CompositeObservation; -import org.eclipse.leshan.core.observation.Observation; -import org.eclipse.leshan.core.observation.SingleObservation; -import org.eclipse.leshan.core.peer.LwM2mPeer; -import org.eclipse.leshan.core.response.ObserveCompositeResponse; -import org.eclipse.leshan.core.response.ObserveResponse; -import org.eclipse.leshan.server.endpoint.LwM2mServerEndpoint; -import org.eclipse.leshan.server.endpoint.LwM2mServerEndpointsProvider; -import org.eclipse.leshan.server.profile.ClientProfile; -import org.eclipse.leshan.server.registration.Registration; -import org.eclipse.leshan.server.registration.RegistrationStore; -import org.eclipse.leshan.server.registration.RegistrationUpdate; -import org.eclipse.leshan.server.registration.UpdatedRegistration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * Implementation of the {@link ObservationService} accessing the persisted observation via the provided - * {@link RegistrationStore}. - * - * When a new observation is added or changed or canceled, the registered listeners are notified. - */ -@Slf4j -public class ObservationServiceImpl implements ObservationService, LwM2mNotificationReceiver { - - private final Logger LOG = LoggerFactory.getLogger(ObservationServiceImpl.class); - - private final RegistrationStore registrationStore; - private final LwM2mServerEndpointsProvider endpointProvider; - private final boolean updateRegistrationOnNotification; - - private final List listeners = new CopyOnWriteArrayList<>();; - - /** - * Creates an instance of {@link ObservationServiceImpl} - */ - public ObservationServiceImpl(RegistrationStore store, LwM2mServerEndpointsProvider endpointProvider) { - this(store, endpointProvider, false); - } - - /** - * Creates an instance of {@link ObservationServiceImpl} - * - * @param updateRegistrationOnNotification will activate registration update on observe notification. - * - * @since 1.1 - */ - public ObservationServiceImpl(RegistrationStore store, LwM2mServerEndpointsProvider endpointProvider, - boolean updateRegistrationOnNotification) { - this.registrationStore = store; - this.updateRegistrationOnNotification = updateRegistrationOnNotification; - this.endpointProvider = endpointProvider; - } - - @Override - public int cancelObservations(Registration registration) { - // check registration id - String registrationId = registration.getId(); - if (registrationId == null) - return 0; - - Collection observations = registrationStore.removeObservations(registrationId); - if (observations == null) - return 0; - - for (Observation observation : observations) { - cancel(observation); - } - - return observations.size(); - } - - @Override - public int cancelObservations(Registration registration, String nodePath) { - if (registration == null || registration.getId() == null || nodePath == null || nodePath.isEmpty()) - return 0; - - Set observations = getObservationsForCancel(registration.getId(), nodePath); - for (Observation observation : observations) { - cancelObservation(observation); - } - return observations.size(); - } - - @Override - public int cancelCompositeObservations(Registration registration, String[] nodePaths) { - if (registration == null || registration.getId() == null || nodePaths == null || nodePaths.length == 0) - return 0; - - Set observations = getCompositeObservationsForCancel(registration.getId(), nodePaths); - for (Observation observation : observations) { - cancelObservation(observation); - } - return observations.size(); - } - - @Override - public void cancelObservation(Observation observation) { - if (observation == null) - return; - - registrationStore.removeObservation(observation.getRegistrationId(), observation.getId()); - cancel(observation); - } - - private void cancel(Observation observation) { - List endpoints = endpointProvider.getEndpoints(); - for (LwM2mServerEndpoint lwM2mEndpoint : endpoints) { - lwM2mEndpoint.cancelObservation(observation); - } - - for (ObservationListener listener : listeners) { - listener.cancelled(observation); - } - } - - @Override - public Set getObservations(Registration registration) { - return getObservations(registration.getId()); - } - - private Set getObservations(String registrationId) { - if (registrationId == null) - return Collections.emptySet(); - - return new HashSet<>(registrationStore.getObservations(registrationId)); - } - - private Set getCompositeObservationsForCancel(String registrationId, String[] nodePaths) { - if (registrationId == null || nodePaths == null) - return Collections.emptySet(); - - // array of String to array of LWM2M path - List lwPaths = new ArrayList<>(nodePaths.length); - for (int i = 0; i < nodePaths.length; i++) { - lwPaths.add(new LwM2mPath(nodePaths[i])); - } - - // search composite-observation - Set result = new HashSet<>(); - for (Observation obs : getObservations(registrationId)) { - if (obs instanceof CompositeObservation) { - if (lwPaths.equals(((CompositeObservation) obs).getPaths())) { - result.add(obs); - } - } - } - return result; - } - - private Set getObservationsForCancel(String registrationId, String nodePath) { - if (registrationId == null || nodePath == null) - return Collections.emptySet(); - - Set result = new HashSet<>(); - LwM2mPath lwPath = new LwM2mPath(nodePath); - for (Observation obs : getObservations(registrationId)) { - if (obs instanceof SingleObservation) { - LwM2mPath lwPathObs = ((SingleObservation) obs).getPath(); - if (lwPath.equals(lwPathObs) || lwPathObs.startWith(lwPath)) { // nodePath = "3", lwPathObs = "3/0/9": cancel for tne all lwPathObs - result.add(obs); - } else if (!lwPath.equals(lwPathObs) && lwPath.startWith(lwPathObs)) { // nodePath = "3/0/9", lwPathObs = "3": error... - String errorMsg = String.format( - "Unexpected error : There is registration with id [%s] existing observation [%s] includes input observation [%s]!", - registrationId, lwPathObs, lwPath); - throw new IllegalStateException(errorMsg); - } - } - } - - return result; - } - - @Override - public void addListener(ObservationListener listener) { - listeners.add(listener); - } - - @Override - public void removeListener(ObservationListener listener) { - listeners.remove(listener); - } - - private Registration updateRegistrationOnRegistration(Observation observation, LwM2mPeer sender, - ClientProfile profile) { - if (updateRegistrationOnNotification) { - RegistrationUpdate regUpdate = new RegistrationUpdate(observation.getRegistrationId(), sender, null, null, - null, null, null, null, null, null, null, null); - UpdatedRegistration updatedRegistration = registrationStore.updateRegistration(regUpdate); - if (updatedRegistration == null || updatedRegistration.getUpdatedRegistration() == null) { - String errorMsg = String.format( - "Unexpected error: There is no registration with id %s for this observation %s", - observation.getRegistrationId(), observation); - LOG.error(errorMsg); - throw new IllegalStateException(errorMsg); - } - return updatedRegistration.getUpdatedRegistration(); - } - return profile.getRegistration(); - } - - // ********** NotificationListener interface **********// - @Override - public void onNotification(SingleObservation observation, LwM2mPeer sender, ClientProfile profile, - ObserveResponse response) { - try { - Registration updatedRegistration = updateRegistrationOnRegistration(observation, sender, profile); - for (ObservationListener listener : listeners) { - listener.onResponse(observation, updatedRegistration, response); - } - } catch (Exception e) { - for (ObservationListener listener : listeners) { - listener.onError(observation, profile.getRegistration(), e); - } - } - } - - @Override - public void onNotification(CompositeObservation observation, LwM2mPeer sender, ClientProfile profile, - ObserveCompositeResponse response) { - try { - Registration updatedRegistration = updateRegistrationOnRegistration(observation, sender, profile); - for (ObservationListener listener : listeners) { - listener.onResponse(observation, updatedRegistration, response); - } - } catch (Exception e) { - for (ObservationListener listener : listeners) { - listener.onError(observation, profile.getRegistration(), e); - } - } - } - - @Override - public void onError(Observation observation, LwM2mPeer sender, ClientProfile profile, Exception error) { - for (ObservationListener listener : listeners) { - listener.onError(observation, profile.getRegistration(), error); - } - } - - @Override - public void newObservation(Observation observation, Registration registration) { - for (ObservationListener listener : listeners) { - listener.newObservation(observation, registration); - } - } - - @Override - public void cancelled(Observation observation) { - for (ObservationListener listener : listeners) { - listener.cancelled(observation); - } - - } -}