Commit fda871392bfe828ad62369ccbb6803df8cb3b532

Authored by nickAS21
Committed by GitHub
1 parent 367be124

lwm2m: test model2 (#4298)

* lwm2m: test model2

* lwm2m: test model2 (fix test)
@@ -716,7 +716,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { @@ -716,7 +716,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
716 * @return - value of Resource or null 716 * @return - value of Resource or null
717 */ 717 */
718 private String getResourceValueToString(LwM2mClient lwM2MClient, String path) { 718 private String getResourceValueToString(LwM2mClient lwM2MClient, String path) {
719 - LwM2mPath pathIds =new LwM2mPath(convertToObjectIdFromIdVer(path)); 719 + LwM2mPath pathIds = new LwM2mPath(convertToObjectIdFromIdVer(path));
720 ResourceValue resourceValue = this.returnResourceValueFromLwM2MClient(lwM2MClient, path); 720 ResourceValue resourceValue = this.returnResourceValueFromLwM2MClient(lwM2MClient, path);
721 return resourceValue == null ? null : 721 return resourceValue == null ? null :
722 this.converter.convertValue(resourceValue.getResourceValue(), this.lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModelType(lwM2MClient.getRegistration(), pathIds), ResourceModel.Type.STRING, pathIds).toString(); 722 this.converter.convertValue(resourceValue.getResourceValue(), this.lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModelType(lwM2MClient.getRegistration(), pathIds), ResourceModel.Type.STRING, pathIds).toString();
@@ -830,8 +830,10 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { @@ -830,8 +830,10 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
830 830
831 // #5.1 831 // #5.1
832 if (!observeOld.equals(observeNew)) { 832 if (!observeOld.equals(observeNew)) {
833 - Set<String> observeSetOld = new Gson().fromJson(observeOld, new TypeToken<Set<String>>() {}.getType());  
834 - Set<String> observeSetNew = new Gson().fromJson(observeNew, new TypeToken<Set<String>>() {}.getType()); 833 + Set<String> observeSetOld = new Gson().fromJson(observeOld, new TypeToken<Set<String>>() {
  834 + }.getType());
  835 + Set<String> observeSetNew = new Gson().fromJson(observeNew, new TypeToken<Set<String>>() {
  836 + }.getType());
835 //#5.2 add 837 //#5.2 add
836 // path Attr/Telemetry includes newObserve 838 // path Attr/Telemetry includes newObserve
837 attributeSetOld.addAll(telemetrySetOld); 839 attributeSetOld.addAll(telemetrySetOld);
@@ -967,7 +969,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { @@ -967,7 +969,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
967 LwM2mClientProfile profile = lwM2mClientContext.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB())); 969 LwM2mClientProfile profile = lwM2mClientContext.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
968 Registration registration = lwM2mClientContext.getLwM2MClient(sessionInfo).getRegistration(); 970 Registration registration = lwM2mClientContext.getLwM2MClient(sessionInfo).getRegistration();
969 return profile.getPostKeyNameProfile().getAsJsonObject().entrySet().stream() 971 return profile.getPostKeyNameProfile().getAsJsonObject().entrySet().stream()
970 - .filter(e -> e.getValue().getAsString().equals(name) && validateResourceInModelByVer(registration, e.getKey())).findFirst().map(Map.Entry::getKey) 972 + .filter(e -> e.getValue().getAsString().equals(name) && validateResourceInModel(registration, e.getKey(), false)).findFirst().map(Map.Entry::getKey)
971 .orElse(""); 973 .orElse("");
972 } 974 }
973 975
@@ -987,7 +989,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { @@ -987,7 +989,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
987 try { 989 try {
988 LwM2mClient lwM2MClient = lwM2mClientContext.getLwM2MClient(sessionInfo); 990 LwM2mClient lwM2MClient = lwM2mClientContext.getLwM2MClient(sessionInfo);
989 attributesResponse.getSharedAttributeListList().forEach(attr -> { 991 attributesResponse.getSharedAttributeListList().forEach(attr -> {
990 - String path =this.getPathAttributeUpdate(sessionInfo, attr.getKv().getKey()); 992 + String path = this.getPathAttributeUpdate(sessionInfo, attr.getKv().getKey());
991 if (path != null) { 993 if (path != null) {
992 // #1.1 994 // #1.1
993 if (lwM2MClient.getDelayedRequests().containsKey(path) && attr.getTs() > lwM2MClient.getDelayedRequests().get(path).getTs()) { 995 if (lwM2MClient.getDelayedRequests().containsKey(path) && attr.getTs() > lwM2MClient.getDelayedRequests().get(path).getTs()) {
@@ -1116,7 +1118,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { @@ -1116,7 +1118,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
1116 1118
1117 ConcurrentMap<String, String> keyNamesIsWritable = keyNamesMap.entrySet() 1119 ConcurrentMap<String, String> keyNamesIsWritable = keyNamesMap.entrySet()
1118 .stream() 1120 .stream()
1119 - .filter(e -> (attrSet.contains(e.getKey()) && resourceIsWritable(lwM2MClient.getRegistration(), e.getKey()))) 1121 + .filter(e -> (attrSet.contains(e.getKey()) && validateResourceInModel(lwM2MClient.getRegistration(), e.getKey(), true)))
1120 .collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue)); 1122 .collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue));
1121 1123
1122 Set<String> namesIsWritable = ConcurrentHashMap.newKeySet(); 1124 Set<String> namesIsWritable = ConcurrentHashMap.newKeySet();
@@ -1124,17 +1126,13 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { @@ -1124,17 +1126,13 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
1124 return new ArrayList<>(namesIsWritable); 1126 return new ArrayList<>(namesIsWritable);
1125 } 1127 }
1126 1128
1127 - private boolean resourceIsWritable(Registration registration, String pathKey) {  
1128 - return validateResourceInModelByVer(registration, pathKey) &&  
1129 - lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModel(registration,  
1130 - new LwM2mPath(convertToObjectIdFromIdVer(pathKey))).operations.isWritable();  
1131 - }  
1132 -  
1133 - private boolean validateResourceInModelByVer(Registration registration, String pathKey) { 1129 + private boolean validateResourceInModel(Registration registration, String pathKey, boolean isWritable) {
  1130 + ResourceModel resourceModel = lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModel(registration,
  1131 + new LwM2mPath(convertToObjectIdFromIdVer(pathKey)));
1134 Integer objectId = validateObjectIdFromKey(pathKey); 1132 Integer objectId = validateObjectIdFromKey(pathKey);
1135 String objectVer = validateObjectVerFromKey(pathKey); 1133 String objectVer = validateObjectVerFromKey(pathKey);
1136 - LwM2mPath lwM2mPath = new LwM2mPath(convertToObjectIdFromIdVer(pathKey));  
1137 - return objectId != null && objectVer != null && objectVer.equals(registration.getSupportedVersion(objectId)) &&  
1138 - lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModel(registration, lwM2mPath) != null; 1134 + return resourceModel != null && (isWritable ?
  1135 + objectId != null && objectVer != null && objectVer.equals(registration.getSupportedVersion(objectId)) && resourceModel.operations.isWritable() :
  1136 + objectId != null && objectVer != null && objectVer.equals(registration.getSupportedVersion(objectId)));
1139 } 1137 }
1140 } 1138 }
@@ -22,6 +22,7 @@ import org.eclipse.leshan.core.model.ObjectModel; @@ -22,6 +22,7 @@ import org.eclipse.leshan.core.model.ObjectModel;
22 import org.eclipse.leshan.core.model.ResourceModel; 22 import org.eclipse.leshan.core.model.ResourceModel;
23 import org.eclipse.leshan.server.model.LwM2mModelProvider; 23 import org.eclipse.leshan.server.model.LwM2mModelProvider;
24 import org.eclipse.leshan.server.registration.Registration; 24 import org.eclipse.leshan.server.registration.Registration;
  25 +import org.thingsboard.server.common.data.TbResource;
25 import org.thingsboard.server.common.data.id.TenantId; 26 import org.thingsboard.server.common.data.id.TenantId;
26 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; 27 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext;
27 28
@@ -30,6 +31,7 @@ import java.util.Base64; @@ -30,6 +31,7 @@ import java.util.Base64;
30 import java.util.Collection; 31 import java.util.Collection;
31 import java.util.Iterator; 32 import java.util.Iterator;
32 import java.util.Map; 33 import java.util.Map;
  34 +import java.util.Optional;
33 35
34 import static org.thingsboard.server.common.data.ResourceType.LWM2M_MODEL; 36 import static org.thingsboard.server.common.data.ResourceType.LWM2M_MODEL;
35 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY; 37 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY;
@@ -83,6 +85,7 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider { @@ -83,6 +85,7 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider {
83 if (objectModel != null) 85 if (objectModel != null)
84 return objectModel.resources.get(resourceId); 86 return objectModel.resources.get(resourceId);
85 else 87 else
  88 + log.warn("TbResources (Object model) with id [{}/{}] not found on the server", objectId, resourceId);
86 return null; 89 return null;
87 } catch (Exception e) { 90 } catch (Exception e) {
88 log.error("", e); 91 log.error("", e);
@@ -116,17 +119,15 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider { @@ -116,17 +119,15 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider {
116 119
117 private ObjectModel getObjectModelDynamic(Integer objectId, String version) { 120 private ObjectModel getObjectModelDynamic(Integer objectId, String version) {
118 String key = getKeyIdVer(objectId, version); 121 String key = getKeyIdVer(objectId, version);
119 - String xmlB64 = lwM2mTransportContextServer.getTransportResourceCache().get(  
120 - this.tenantId,  
121 - LWM2M_MODEL,  
122 - key).  
123 - getData();  
124 - return xmlB64 != null && !xmlB64.isEmpty() ?  
125 - lwM2mTransportContextServer.parseFromXmlToObjectModel(  
126 - Base64.getDecoder().decode(xmlB64),  
127 - key + ".xml",  
128 - new DefaultDDFFileValidator()) :  
129 - null; 122 +
  123 + Optional<TbResource> tbResource = lwM2mTransportContextServer
  124 + .getTransportResourceCache()
  125 + .get(this.tenantId, LWM2M_MODEL, key);
  126 +
  127 + return tbResource.map(resource -> lwM2mTransportContextServer.parseFromXmlToObjectModel(
  128 + Base64.getDecoder().decode(resource.getData()),
  129 + key + ".xml",
  130 + new DefaultDDFFileValidator())).orElse(null);
130 } 131 }
131 } 132 }
132 } 133 }
@@ -15,13 +15,15 @@ @@ -15,13 +15,15 @@
15 */ 15 */
16 package org.thingsboard.server.common.transport; 16 package org.thingsboard.server.common.transport;
17 17
18 -import org.thingsboard.server.common.data.TbResource;  
19 import org.thingsboard.server.common.data.ResourceType; 18 import org.thingsboard.server.common.data.ResourceType;
  19 +import org.thingsboard.server.common.data.TbResource;
20 import org.thingsboard.server.common.data.id.TenantId; 20 import org.thingsboard.server.common.data.id.TenantId;
21 21
  22 +import java.util.Optional;
  23 +
22 public interface TransportResourceCache { 24 public interface TransportResourceCache {
23 25
24 - TbResource get(TenantId tenantId, ResourceType resourceType, String resourceId); 26 + Optional<TbResource> get(TenantId tenantId, ResourceType resourceType, String resourceId);
25 27
26 void update(TenantId tenantId, ResourceType resourceType, String resourceI); 28 void update(TenantId tenantId, ResourceType resourceType, String resourceI);
27 29
@@ -19,8 +19,8 @@ import lombok.Data; @@ -19,8 +19,8 @@ import lombok.Data;
19 import lombok.extern.slf4j.Slf4j; 19 import lombok.extern.slf4j.Slf4j;
20 import org.springframework.context.annotation.Lazy; 20 import org.springframework.context.annotation.Lazy;
21 import org.springframework.stereotype.Component; 21 import org.springframework.stereotype.Component;
22 -import org.thingsboard.server.common.data.TbResource;  
23 import org.thingsboard.server.common.data.ResourceType; 22 import org.thingsboard.server.common.data.ResourceType;
  23 +import org.thingsboard.server.common.data.TbResource;
24 import org.thingsboard.server.common.data.id.TenantId; 24 import org.thingsboard.server.common.data.id.TenantId;
25 import org.thingsboard.server.common.transport.TransportResourceCache; 25 import org.thingsboard.server.common.transport.TransportResourceCache;
26 import org.thingsboard.server.common.transport.TransportService; 26 import org.thingsboard.server.common.transport.TransportService;
@@ -53,7 +53,7 @@ public class DefaultTransportResourceCache implements TransportResourceCache { @@ -53,7 +53,7 @@ public class DefaultTransportResourceCache implements TransportResourceCache {
53 } 53 }
54 54
55 @Override 55 @Override
56 - public TbResource get(TenantId tenantId, ResourceType resourceType, String resourceKey) { 56 + public Optional<TbResource> get(TenantId tenantId, ResourceType resourceType, String resourceKey) {
57 ResourceCompositeKey compositeKey = new ResourceCompositeKey(tenantId, resourceType, resourceKey); 57 ResourceCompositeKey compositeKey = new ResourceCompositeKey(tenantId, resourceType, resourceKey);
58 TbResource resource; 58 TbResource resource;
59 59
@@ -79,7 +79,7 @@ public class DefaultTransportResourceCache implements TransportResourceCache { @@ -79,7 +79,7 @@ public class DefaultTransportResourceCache implements TransportResourceCache {
79 } 79 }
80 } 80 }
81 81
82 - return resource; 82 + return Optional.ofNullable(resource);
83 } 83 }
84 84
85 private TbResource fetchResource(ResourceCompositeKey compositeKey) { 85 private TbResource fetchResource(ResourceCompositeKey compositeKey) {
@@ -87,7 +87,7 @@ public class BaseTbResourceService implements TbResourceService { @@ -87,7 +87,7 @@ public class BaseTbResourceService implements TbResourceService {
87 String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.getVersion(); 87 String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.getVersion();
88 String name = objectModel.name; 88 String name = objectModel.name;
89 resource.setResourceKey(resourceKey); 89 resource.setResourceKey(resourceKey);
90 - resource.setTitle(name); 90 + resource.setTitle(name + " id=" +objectModel.id + " v" + objectModel.getVersion());
91 resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name); 91 resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);
92 } else { 92 } else {
93 throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText())); 93 throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
@@ -123,7 +123,7 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest { @@ -123,7 +123,7 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
123 Assert.assertNotNull(savedResource.getId()); 123 Assert.assertNotNull(savedResource.getId());
124 Assert.assertTrue(savedResource.getCreatedTime() > 0); 124 Assert.assertTrue(savedResource.getCreatedTime() > 0);
125 Assert.assertEquals(resource.getTenantId(), savedResource.getTenantId()); 125 Assert.assertEquals(resource.getTenantId(), savedResource.getTenantId());
126 - Assert.assertEquals("My first resource", savedResource.getTitle()); 126 + Assert.assertEquals("My first resource id=0 v1.0", savedResource.getTitle());
127 Assert.assertEquals("0_1.0", savedResource.getResourceKey()); 127 Assert.assertEquals("0_1.0", savedResource.getResourceKey());
128 Assert.assertEquals(resource.getData(), savedResource.getData()); 128 Assert.assertEquals(resource.getData(), savedResource.getData());
129 129