Merge pull request #12427 from YevhenBondarenko/fix/lwm2m
[Transport] lwm2m client serialization fixes
This commit is contained in:
		
						commit
						e9036f8aa8
					
				@ -17,7 +17,6 @@ package org.thingsboard.server.transport.lwm2m.server.store.util;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
					import com.fasterxml.jackson.databind.JsonNode;
 | 
				
			||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
					 | 
				
			||||||
import com.google.gson.JsonElement;
 | 
					import com.google.gson.JsonElement;
 | 
				
			||||||
import com.google.gson.JsonObject;
 | 
					import com.google.gson.JsonObject;
 | 
				
			||||||
import com.google.gson.JsonParser;
 | 
					import com.google.gson.JsonParser;
 | 
				
			||||||
@ -57,7 +56,7 @@ public class LwM2MClientSerDes {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @SneakyThrows
 | 
					    @SneakyThrows
 | 
				
			||||||
    public static byte[] serialize(LwM2mClient client) {
 | 
					    public static byte[] serialize(LwM2mClient client) {
 | 
				
			||||||
        JsonObject o =  new JsonObject();
 | 
					        JsonObject o = new JsonObject();
 | 
				
			||||||
        o.addProperty("nodeId", client.getNodeId());
 | 
					        o.addProperty("nodeId", client.getNodeId());
 | 
				
			||||||
        o.addProperty("endpoint", client.getEndpoint());
 | 
					        o.addProperty("endpoint", client.getEndpoint());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -111,14 +110,7 @@ public class LwM2MClientSerDes {
 | 
				
			|||||||
        o.addProperty("defaultObjectIDVer", client.getDefaultObjectIDVer().toString());
 | 
					        o.addProperty("defaultObjectIDVer", client.getDefaultObjectIDVer().toString());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (client.getRegistration() != null) {
 | 
					        if (client.getRegistration() != null) {
 | 
				
			||||||
            String registrationAddress = client.getRegistration().getAddress().toString();
 | 
					 | 
				
			||||||
            JsonNode registrationNode = registrationSerDes.jSerialize(client.getRegistration());
 | 
					            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("registration", registrationNode.toString());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        o.addProperty("asleep", client.isAsleep());
 | 
					        o.addProperty("asleep", client.isAsleep());
 | 
				
			||||||
@ -188,7 +180,7 @@ public class LwM2MClientSerDes {
 | 
				
			|||||||
            case STRING:
 | 
					            case STRING:
 | 
				
			||||||
                return value.getAsString();
 | 
					                return value.getAsString();
 | 
				
			||||||
            case TIME:
 | 
					            case TIME:
 | 
				
			||||||
                return Instant.ofEpochMilli(value.getAsLong());
 | 
					                return new Date(value.getAsLong());
 | 
				
			||||||
            case OBJLNK:
 | 
					            case OBJLNK:
 | 
				
			||||||
                return ObjectLink.decodeFromString(value.getAsString());
 | 
					                return ObjectLink.decodeFromString(value.getAsString());
 | 
				
			||||||
            case UNSIGNED_INTEGER:
 | 
					            case UNSIGNED_INTEGER:
 | 
				
			||||||
@ -249,7 +241,7 @@ public class LwM2MClientSerDes {
 | 
				
			|||||||
                o.addProperty(VALUE, ((ObjectLink) value).encodeToString());
 | 
					                o.addProperty(VALUE, ((ObjectLink) value).encodeToString());
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case UNSIGNED_INTEGER:
 | 
					            case UNSIGNED_INTEGER:
 | 
				
			||||||
                o.addProperty(VALUE, Integer.toUnsignedString((int)value));
 | 
					                o.addProperty(VALUE, Integer.toUnsignedString((int) value));
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            default:
 | 
					            default:
 | 
				
			||||||
                throw new LwM2mNodeException(String.format("Type %s is not supported", type.name()));
 | 
					                throw new LwM2mNodeException(String.format("Type %s is not supported", type.name()));
 | 
				
			||||||
 | 
				
			|||||||
@ -18,13 +18,17 @@ package org.thingsboard.server.transport.lwm2m.server.store.util;
 | 
				
			|||||||
import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
 | 
					import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
 | 
				
			||||||
import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
 | 
					import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
 | 
				
			||||||
import org.eclipse.leshan.core.link.Link;
 | 
					import org.eclipse.leshan.core.link.Link;
 | 
				
			||||||
 | 
					import org.eclipse.leshan.core.link.attributes.AttributeSet;
 | 
				
			||||||
 | 
					import org.eclipse.leshan.core.link.attributes.ResourceTypeAttribute;
 | 
				
			||||||
import org.eclipse.leshan.core.node.LwM2mMultipleResource;
 | 
					import org.eclipse.leshan.core.node.LwM2mMultipleResource;
 | 
				
			||||||
import org.eclipse.leshan.core.node.LwM2mPath;
 | 
					import org.eclipse.leshan.core.node.LwM2mPath;
 | 
				
			||||||
import org.eclipse.leshan.core.node.LwM2mResource;
 | 
					import org.eclipse.leshan.core.node.LwM2mResource;
 | 
				
			||||||
import org.eclipse.leshan.core.node.LwM2mSingleResource;
 | 
					import org.eclipse.leshan.core.node.LwM2mSingleResource;
 | 
				
			||||||
import org.eclipse.leshan.core.peer.IpPeer;
 | 
					import org.eclipse.leshan.core.peer.IpPeer;
 | 
				
			||||||
import org.eclipse.leshan.core.request.WriteRequest;
 | 
					import org.eclipse.leshan.core.request.WriteRequest;
 | 
				
			||||||
 | 
					import org.eclipse.leshan.server.registration.DefaultRegistrationDataExtractor;
 | 
				
			||||||
import org.eclipse.leshan.server.registration.Registration;
 | 
					import org.eclipse.leshan.server.registration.Registration;
 | 
				
			||||||
 | 
					import org.eclipse.leshan.server.registration.RegistrationDataExtractor;
 | 
				
			||||||
import org.junit.jupiter.api.Test;
 | 
					import org.junit.jupiter.api.Test;
 | 
				
			||||||
import org.thingsboard.server.common.data.TbResource;
 | 
					import org.thingsboard.server.common.data.TbResource;
 | 
				
			||||||
import org.thingsboard.server.common.data.device.data.PowerMode;
 | 
					import org.thingsboard.server.common.data.device.data.PowerMode;
 | 
				
			||||||
@ -83,12 +87,22 @@ public class LwM2MClientSerDesTest {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        client.init(credentialsResponse, UUID.randomUUID());
 | 
					        client.init(credentialsResponse, UUID.randomUUID());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        AttributeSet attrs = new AttributeSet( //
 | 
				
			||||||
 | 
					                new ResourceTypeAttribute("oma.lwm2m"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Link[] objs = new Link[]{new Link("/15", attrs), new Link("/17")};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        RegistrationDataExtractor.RegistrationData dataFromObjectLinks = new DefaultRegistrationDataExtractor().extractDataFromObjectLinks(objs,
 | 
				
			||||||
 | 
					                LwM2mVersion.V1_0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Registration registration = new Registration
 | 
					        Registration registration = new Registration
 | 
				
			||||||
                .Builder("test", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 1000)),
 | 
					                .Builder("test", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 1000)),
 | 
				
			||||||
                        EndpointUriUtil.createUri("coap://localhost:5685"))
 | 
					                EndpointUriUtil.createUri("coap://localhost:5685"))
 | 
				
			||||||
                .supportedContentFormats()
 | 
					                .objectLinks(objs)
 | 
				
			||||||
                .supportedObjects(Map.of(15, LwM2mVersion.V1_0, 17, LwM2mVersion.V1_0))
 | 
					                .rootPath(dataFromObjectLinks.getAlternatePath())
 | 
				
			||||||
                .objectLinks(new Link[] { new Link("/15"),  new Link("/17") })
 | 
					                .supportedContentFormats(dataFromObjectLinks.getSupportedContentFormats())
 | 
				
			||||||
 | 
					                .supportedObjects(dataFromObjectLinks.getSupportedObjects())
 | 
				
			||||||
 | 
					                .availableInstances(dataFromObjectLinks.getAvailableInstances())
 | 
				
			||||||
                .build();
 | 
					                .build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client.setRegistration(registration);
 | 
					        client.setRegistration(registration);
 | 
				
			||||||
@ -135,12 +149,7 @@ public class LwM2MClientSerDesTest {
 | 
				
			|||||||
        assertEquals(client.getPsmActivityTimer(), desClient.getPsmActivityTimer());
 | 
					        assertEquals(client.getPsmActivityTimer(), desClient.getPsmActivityTimer());
 | 
				
			||||||
        assertEquals(client.getPagingTransmissionWindow(), desClient.getPagingTransmissionWindow());
 | 
					        assertEquals(client.getPagingTransmissionWindow(), desClient.getPagingTransmissionWindow());
 | 
				
			||||||
        assertEquals(client.getEdrxCycle(), desClient.getEdrxCycle());
 | 
					        assertEquals(client.getEdrxCycle(), desClient.getEdrxCycle());
 | 
				
			||||||
        if (((IpPeer)desClient.getRegistration().getClientTransportData()).getSocketAddress().isUnresolved()) {
 | 
					        assertEquals(client.getRegistration(), desClient.getRegistration());
 | 
				
			||||||
            String actualReg = desClient.getRegistration().toString().replaceAll("/<unresolved>", "");
 | 
					 | 
				
			||||||
            assertEquals(client.getRegistration().toString(), actualReg);
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            assertEquals(client.getRegistration(), desClient.getRegistration());
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        assertEquals(client.isAsleep(), desClient.isAsleep());
 | 
					        assertEquals(client.isAsleep(), desClient.isAsleep());
 | 
				
			||||||
        assertEquals(client.getLastUplinkTime(), desClient.getLastUplinkTime());
 | 
					        assertEquals(client.getLastUplinkTime(), desClient.getLastUplinkTime());
 | 
				
			||||||
        assertEquals(client.getSleepTask(), desClient.getSleepTask());
 | 
					        assertEquals(client.getSleepTask(), desClient.getSleepTask());
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user