From f6eb3b1342b5446f0d3de3a5f9dbb724f12d2e92 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Sun, 31 Jan 2021 13:38:20 +0200 Subject: [PATCH] Lwm2m: back: refactoring-light start --- .../src/main/resources/thingsboard.yml | 1 + .../server/LwM2MTransportServiceImpl.java | 75 ++++++++----------- .../lwm2m/server/client/LwM2MClient.java | 44 ++++++----- .../secure/LwM2mInMemorySecurityStore.java | 8 +- .../lwm2m/LwM2MTransportConfigServer.java | 4 + 5 files changed, 64 insertions(+), 68 deletions(-) diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 461d2f04c8..9f661a462d 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -581,6 +581,7 @@ transport: request_pool_size: "${LWM2M_REQUEST_POOL_SIZE:100}" request_error_pool_size: "${LWM2M_REQUEST_ERROR_POOL_SIZE:10}" registered_pool_size: "${LWM2M_REGISTERED_POOL_SIZE:10}" + client_update_value_after_connect: "${CLIENT_UPDATE_VALUE_AFTER_CONNECT:false}" update_registered_pool_size: "${LWM2M_UPDATE_REGISTERED_POOL_SIZE:10}" un_registered_pool_size: "${LWM2M_UN_REGISTERED_POOL_SIZE:10}" secure: diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2MTransportServiceImpl.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2MTransportServiceImpl.java index e412e113e9..a02aa1b29d 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2MTransportServiceImpl.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2MTransportServiceImpl.java @@ -20,7 +20,6 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import lombok.extern.slf4j.Slf4j; -import org.eclipse.leshan.core.Link; import org.eclipse.leshan.core.model.ResourceModel; import org.eclipse.leshan.core.node.LwM2mMultipleResource; import org.eclipse.leshan.core.node.LwM2mObject; @@ -155,6 +154,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService { lwM2MClient.setSessionUuid(UUID.randomUUID()); this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client Registered", registration); this.setLwM2mFromClientValue(lwServer, registration, lwM2MClient); + lwM2MClient.setClientUpdateValueAfterConnect(this.context.getCtxServer().isClientUpdateValueAfterConnect()); SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration); if (sessionInfo != null) { lwM2MClient.setDeviceUuid(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB())); @@ -165,7 +165,9 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService { transportService.process(sessionInfo, DefaultTransportService.getSessionEventMsg(SessionEvent.OPEN), null); transportService.process(sessionInfo, TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null); this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client create after Registration", registration); - this.putDelayedUpdateResourcesThingsboard(lwM2MClient); + if (this.context.getCtxServer().isClientUpdateValueAfterConnect()) { + this.putDelayedUpdateResourcesThingsboard(lwM2MClient); + } } else { log.error("Client: [{}] onRegistered [{}] name [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null); } @@ -272,8 +274,11 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService { /** * #0 Add new ObjectModel to context * Create new LwM2MClient for current session -> setModelClient... - * #1 Add all ObjectLinks (instance) to control the process of executing requests to the client + * if need all value after registration: + * #1.1 Add all ObjectLinks (instance) to control the process of executing requests to the client * to get the client model with current values + * if not need all value after registration (only observe) + * #1.2 Get observe * #2 Get the client model with current values. Analyze the response in -> lwM2MTransportRequest.sendResponse * * @param lwServer - LeshanServer @@ -281,48 +286,35 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService { * @param lwM2MClient - object with All parameters off client */ private void setLwM2mFromClientValue(LeshanServer lwServer, Registration registration, LwM2MClient lwM2MClient) { + // #1.1 + // get all instances in client + Set clientInstances = ConcurrentHashMap.newKeySet(); Arrays.stream(registration.getObjectLinks()).forEach(url -> { LwM2mPath pathIds = new LwM2mPath(url.getUrl()); if (pathIds.isObjectInstance() && !pathIds.isResource()) { - // #1 - lwM2MClient.getPendingRequests().add(url.getUrl()); - // #2 - lwM2MTransportRequest.sendAllRequest(lwServer, registration, url.getUrl(), GET_TYPE_OPER_READ, ContentFormat.TLV.getName(), - lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false); + clientInstances.add(url.getUrl()); } }); - - // #1 - for (Link url : registration.getObjectLinks()) { - LwM2mPath pathIds = new LwM2mPath(url.getUrl()); - if (pathIds.isObjectInstance() && !pathIds.isResource()) { - lwM2MClient.getPendingRequests().add(url.getUrl()); - } - } - // #2 - for (Link url : registration.getObjectLinks()) { - LwM2mPath pathIds = new LwM2mPath(url.getUrl()); - if (pathIds.isObjectInstance() && !pathIds.isResource()) { - lwM2MTransportRequest.sendAllRequest(lwServer, registration, url.getUrl(), GET_TYPE_OPER_READ, ContentFormat.TLV.getName(), - lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false); - } + if (this.context.getCtxServer().isClientUpdateValueAfterConnect()) { + lwM2MClient.getPendingRequests().addAll(clientInstances); + } else { + // #1.2 + UUID profileUUid = lwM2mInMemorySecurityStore.getSessions().get(registration.getId()).getProfileUuid(); + JsonArray observeValue = lwM2mInMemorySecurityStore.getProfiles().get(profileUUid).getPostObserveProfile(); + observeValue.forEach(path -> { + String[] resPath = path.getAsString().split("/"); + String instance = "/" + resPath[1] + "/" + resPath[2]; + if (clientInstances.contains(instance)) { + lwM2MClient.getPendingRequests().add(path.getAsString()); + } + } + ); } - // #1 - Arrays.stream(registration.getObjectLinks()).forEach(url -> { - LwM2mPath pathIds = new LwM2mPath(url.getUrl()); - if (pathIds.isObjectInstance() && !pathIds.isResource()) { - lwM2MClient.getPendingRequests().add(url.getUrl()); - } - }); - // #2 - Arrays.stream(registration.getObjectLinks()).forEach(url -> { - LwM2mPath pathIds = new LwM2mPath(url.getUrl()); - if (pathIds.isObjectInstance() && !pathIds.isResource()) { - lwM2MTransportRequest.sendAllRequest(lwServer, registration, url.getUrl(), GET_TYPE_OPER_READ, ContentFormat.TLV.getName(), - lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false); - } + lwM2MClient.getPendingRequests().forEach(path -> { + lwM2MTransportRequest.sendAllRequest(lwServer, registration, path, GET_TYPE_OPER_READ, ContentFormat.TLV.getName(), + lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false); }); } @@ -704,28 +696,21 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService { * @param values - LwM2mSingleResource response.getContent() * @param path - resource */ - private void onObservationSetResourcesValue(Registration registration, Object value, Map values, String path) { + private void onObservationSetResourcesValue(Registration registration, Object value, Map values, String path) { boolean isChange = false; try { writeLock.lock(); // #1 LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null); LwM2mPath pathIds = new LwM2mPath(path); -// log.warn("#0 nameDevice: [{}] resultIds: [{}] value: [{}], values: [{}] ", lwM2MClient.getDeviceName(), pathIds, value, values); ResourceModel.Type resModelType = context.getCtxServer().getResourceModelType(registration, pathIds); ResourceValue resValueOld = lwM2MClient.getResources().get(path); // #2 if (resValueOld.isMultiInstances() && !values.toString().equals(resValueOld.getResourceValue().toString())) { lwM2MClient.getResources().get(path).setValues(values); -// ResourceValue resourceValue = new ResourceValue(values, null, true); -// lwM2MClient.getResources().put(path, resourceValue); isChange = true; } else if (!LwM2MTransportHandler.equalsResourceValue(resValueOld.getValue(), value, resModelType, pathIds)) { lwM2MClient.getResources().get(path).setValue(value); -// ResourceValue resourceValueOld = lwM2MClient.getResources().get(path); -// lwM2MClient.getResources().remove(resourceValueOld); -// ResourceValue resourceValue = new ResourceValue(null, value, false); -// lwM2MClient.getResources().put(path, resourceValue); log.warn("upDateResize: [{}] [{}] [{}] [{}]", lwM2MClient.getEndPoint(), lwM2MClient.getResources().size(), value, path); isChange = 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 5ccfbff18a..7d32fe5f8a 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 @@ -17,10 +17,11 @@ package org.thingsboard.server.transport.lwm2m.server.client; import lombok.Data; import lombok.extern.slf4j.Slf4j; -import org.eclipse.leshan.core.model.ObjectModel; import org.eclipse.leshan.core.node.LwM2mMultipleResource; import org.eclipse.leshan.core.node.LwM2mObjectInstance; 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.response.LwM2mResponse; import org.eclipse.leshan.core.response.ReadResponse; import org.eclipse.leshan.server.californium.LeshanServer; @@ -35,7 +36,6 @@ import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; @Slf4j @Data @@ -44,7 +44,7 @@ public class LwM2MClient implements Cloneable { private String deviceProfileName; private String endPoint; private String identity; - private SecurityInfo info; + private SecurityInfo securityInfo; private UUID deviceUuid; private UUID sessionUuid; private UUID profileUuid; @@ -59,15 +59,16 @@ public class LwM2MClient implements Cloneable { private Set delayedRequestsId; private Map responses; private final LwM2mValueConverterImpl converter; + private boolean clientUpdateValueAfterConnect; public Object clone() throws CloneNotSupportedException { return super.clone(); } - public LwM2MClient(String endPoint, String identity, SecurityInfo info, ValidateDeviceCredentialsResponseMsg credentialsResponse, UUID profileUuid) { + public LwM2MClient(String endPoint, String identity, SecurityInfo securityInfo, ValidateDeviceCredentialsResponseMsg credentialsResponse, UUID profileUuid) { this.endPoint = endPoint; this.identity = identity; - this.info = info; + this.securityInfo = securityInfo; this.credentialsResponse = credentialsResponse; this.attributes = new ConcurrentHashMap<>(); this.pendingRequests = ConcurrentHashMap.newKeySet(); @@ -98,28 +99,33 @@ public class LwM2MClient implements Cloneable { } private void initValue() { - this.responses.forEach((key, resp) -> { + this.responses.forEach((key, lwM2mResponse) -> { LwM2mPath pathIds = new LwM2mPath(key); - if (pathIds.isObject() || pathIds.isObjectInstance() || pathIds.isResource()) { - ObjectModel objectModel = this.lwServer.getModelProvider().getObjectModel(registration).getObjectModels().stream().filter(v -> v.id == pathIds.getObjectId()).collect(Collectors.toList()).get(0); - if (objectModel != null) { - ((LwM2mObjectInstance)((ReadResponse)resp).getContent()).getResources().forEach((k, v) -> { - String rez = pathIds.toString() + "/" + k; - if (((LwM2mObjectInstance) ((ReadResponse) resp).getContent()).getResource(k) instanceof LwM2mMultipleResource){ - this.resources.put(rez, new ResourceValue(v.getValues(), null, true)); - } - else { - this.resources.put(rez, new ResourceValue(null, v.getValue(), false)); - } - }); - } + if (pathIds.isObjectInstance()) { + ((LwM2mObjectInstance) ((ReadResponse) lwM2mResponse).getContent()).getResources().forEach((k, v) -> { + String pathRez = pathIds.toString() + "/" + k; + this.updateResourceValue(pathRez, v); + }); + } + else if (pathIds.isResource()) { + this.updateResourceValue(pathIds.toString(), ((LwM2mResource) ((ReadResponse) lwM2mResponse).getContent())); } }); if (this.responses.size() == 0) this.responses = new ConcurrentHashMap<>(); } + private void updateResourceValue(String pathRez, LwM2mResource rez) { + if (rez instanceof LwM2mMultipleResource){ + this.resources.put(pathRez, new ResourceValue(rez.getValues(), null, true)); + } + else if (rez instanceof LwM2mSingleResource) { + this.resources.put(pathRez, new ResourceValue(null, rez.getValue(), false)); + } + } + /** * if path != null + * * @param path */ public void onSuccessOrErrorDelayedRequests(String path) { diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/secure/LwM2mInMemorySecurityStore.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/secure/LwM2mInMemorySecurityStore.java index 5f0e82126b..fa35ea70e4 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/secure/LwM2mInMemorySecurityStore.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/secure/LwM2mInMemorySecurityStore.java @@ -77,7 +77,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore { try { String registrationId = this.getByRegistrationId(endPoint, null); return (registrationId != null && sessions.size() > 0 && sessions.get(registrationId) != null) ? - sessions.get(registrationId).getInfo() : this.addLwM2MClientToSession(endPoint); + sessions.get(registrationId).getSecurityInfo() : this.addLwM2MClientToSession(endPoint); } finally { readLock.unlock(); } @@ -93,7 +93,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore { readLock.lock(); try { String integrationId = this.getByRegistrationId(null, identity); - return (integrationId != null) ? sessions.get(integrationId).getInfo() : this.addLwM2MClientToSession(identity); + return (integrationId != null) ? sessions.get(integrationId).getSecurityInfo() : this.addLwM2MClientToSession(identity); } finally { readLock.unlock(); } @@ -103,7 +103,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore { public Collection getAll() { readLock.lock(); try { - return Collections.unmodifiableCollection(this.sessions.values().stream().map(LwM2MClient::getInfo).collect(Collectors.toList())); + return Collections.unmodifiableCollection(this.sessions.values().stream().map(LwM2MClient::getSecurityInfo).collect(Collectors.toList())); } finally { readLock.unlock(); } @@ -119,7 +119,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore { LwM2MClient lwM2MClient = (sessions.get(registrationId) != null) ? sessions.get(registrationId) : null; if (lwM2MClient != null) { if (listener != null) { - listener.securityInfoRemoved(INFOS_ARE_COMPROMISED, lwM2MClient.getInfo()); + listener.securityInfoRemoved(INFOS_ARE_COMPROMISED, lwM2MClient.getSecurityInfo()); } sessions.remove(registrationId); } diff --git a/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/lwm2m/LwM2MTransportConfigServer.java b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/lwm2m/LwM2MTransportConfigServer.java index dfe2b75fef..5fe68f7dab 100644 --- a/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/lwm2m/LwM2MTransportConfigServer.java +++ b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/lwm2m/LwM2MTransportConfigServer.java @@ -118,6 +118,10 @@ public class LwM2MTransportConfigServer { @Value("${transport.lwm2m.registered_pool_size:}") private int registeredPoolSize; + @Getter + @Value("${transport.lwm2m.client_update_value_after_connect:}") + private boolean clientUpdateValueAfterConnect; + @Getter @Value("${transport.lwm2m.update_registered_pool_size:}") private int updateRegisteredPoolSize;