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 716 * @return - value of Resource or null
717 717 */
718 718 private String getResourceValueToString(LwM2mClient lwM2MClient, String path) {
719   - LwM2mPath pathIds =new LwM2mPath(convertToObjectIdFromIdVer(path));
  719 + LwM2mPath pathIds = new LwM2mPath(convertToObjectIdFromIdVer(path));
720 720 ResourceValue resourceValue = this.returnResourceValueFromLwM2MClient(lwM2MClient, path);
721 721 return resourceValue == null ? null :
722 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 830
831 831 // #5.1
832 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 837 //#5.2 add
836 838 // path Attr/Telemetry includes newObserve
837 839 attributeSetOld.addAll(telemetrySetOld);
... ... @@ -967,7 +969,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
967 969 LwM2mClientProfile profile = lwM2mClientContext.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
968 970 Registration registration = lwM2mClientContext.getLwM2MClient(sessionInfo).getRegistration();
969 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 973 .orElse("");
972 974 }
973 975
... ... @@ -987,7 +989,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
987 989 try {
988 990 LwM2mClient lwM2MClient = lwM2mClientContext.getLwM2MClient(sessionInfo);
989 991 attributesResponse.getSharedAttributeListList().forEach(attr -> {
990   - String path =this.getPathAttributeUpdate(sessionInfo, attr.getKv().getKey());
  992 + String path = this.getPathAttributeUpdate(sessionInfo, attr.getKv().getKey());
991 993 if (path != null) {
992 994 // #1.1
993 995 if (lwM2MClient.getDelayedRequests().containsKey(path) && attr.getTs() > lwM2MClient.getDelayedRequests().get(path).getTs()) {
... ... @@ -1116,7 +1118,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
1116 1118
1117 1119 ConcurrentMap<String, String> keyNamesIsWritable = keyNamesMap.entrySet()
1118 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 1122 .collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue));
1121 1123
1122 1124 Set<String> namesIsWritable = ConcurrentHashMap.newKeySet();
... ... @@ -1124,17 +1126,13 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
1124 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 1132 Integer objectId = validateObjectIdFromKey(pathKey);
1135 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 22 import org.eclipse.leshan.core.model.ResourceModel;
23 23 import org.eclipse.leshan.server.model.LwM2mModelProvider;
24 24 import org.eclipse.leshan.server.registration.Registration;
  25 +import org.thingsboard.server.common.data.TbResource;
25 26 import org.thingsboard.server.common.data.id.TenantId;
26 27 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext;
27 28
... ... @@ -30,6 +31,7 @@ import java.util.Base64;
30 31 import java.util.Collection;
31 32 import java.util.Iterator;
32 33 import java.util.Map;
  34 +import java.util.Optional;
33 35
34 36 import static org.thingsboard.server.common.data.ResourceType.LWM2M_MODEL;
35 37 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY;
... ... @@ -83,6 +85,7 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider {
83 85 if (objectModel != null)
84 86 return objectModel.resources.get(resourceId);
85 87 else
  88 + log.warn("TbResources (Object model) with id [{}/{}] not found on the server", objectId, resourceId);
86 89 return null;
87 90 } catch (Exception e) {
88 91 log.error("", e);
... ... @@ -116,17 +119,15 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider {
116 119
117 120 private ObjectModel getObjectModelDynamic(Integer objectId, String version) {
118 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 15 */
16 16 package org.thingsboard.server.common.transport;
17 17
18   -import org.thingsboard.server.common.data.TbResource;
19 18 import org.thingsboard.server.common.data.ResourceType;
  19 +import org.thingsboard.server.common.data.TbResource;
20 20 import org.thingsboard.server.common.data.id.TenantId;
21 21
  22 +import java.util.Optional;
  23 +
22 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 28 void update(TenantId tenantId, ResourceType resourceType, String resourceI);
27 29
... ...
... ... @@ -19,8 +19,8 @@ import lombok.Data;
19 19 import lombok.extern.slf4j.Slf4j;
20 20 import org.springframework.context.annotation.Lazy;
21 21 import org.springframework.stereotype.Component;
22   -import org.thingsboard.server.common.data.TbResource;
23 22 import org.thingsboard.server.common.data.ResourceType;
  23 +import org.thingsboard.server.common.data.TbResource;
24 24 import org.thingsboard.server.common.data.id.TenantId;
25 25 import org.thingsboard.server.common.transport.TransportResourceCache;
26 26 import org.thingsboard.server.common.transport.TransportService;
... ... @@ -53,7 +53,7 @@ public class DefaultTransportResourceCache implements TransportResourceCache {
53 53 }
54 54
55 55 @Override
56   - public TbResource get(TenantId tenantId, ResourceType resourceType, String resourceKey) {
  56 + public Optional<TbResource> get(TenantId tenantId, ResourceType resourceType, String resourceKey) {
57 57 ResourceCompositeKey compositeKey = new ResourceCompositeKey(tenantId, resourceType, resourceKey);
58 58 TbResource resource;
59 59
... ... @@ -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 85 private TbResource fetchResource(ResourceCompositeKey compositeKey) {
... ...
... ... @@ -87,7 +87,7 @@ public class BaseTbResourceService implements TbResourceService {
87 87 String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.getVersion();
88 88 String name = objectModel.name;
89 89 resource.setResourceKey(resourceKey);
90   - resource.setTitle(name);
  90 + resource.setTitle(name + " id=" +objectModel.id + " v" + objectModel.getVersion());
91 91 resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);
92 92 } else {
93 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 123 Assert.assertNotNull(savedResource.getId());
124 124 Assert.assertTrue(savedResource.getCreatedTime() > 0);
125 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 127 Assert.assertEquals("0_1.0", savedResource.getResourceKey());
128 128 Assert.assertEquals(resource.getData(), savedResource.getData());
129 129
... ...