Commit f6eb3b1342b5446f0d3de3a5f9dbb724f12d2e92

Authored by nickAS21
Committed by Andrew Shvayka
1 parent 9b4309c6

Lwm2m: back: refactoring-light start

... ... @@ -581,6 +581,7 @@ transport:
581 581 request_pool_size: "${LWM2M_REQUEST_POOL_SIZE:100}"
582 582 request_error_pool_size: "${LWM2M_REQUEST_ERROR_POOL_SIZE:10}"
583 583 registered_pool_size: "${LWM2M_REGISTERED_POOL_SIZE:10}"
  584 + client_update_value_after_connect: "${CLIENT_UPDATE_VALUE_AFTER_CONNECT:false}"
584 585 update_registered_pool_size: "${LWM2M_UPDATE_REGISTERED_POOL_SIZE:10}"
585 586 un_registered_pool_size: "${LWM2M_UN_REGISTERED_POOL_SIZE:10}"
586 587 secure:
... ...
... ... @@ -20,7 +20,6 @@ import com.google.gson.JsonArray;
20 20 import com.google.gson.JsonElement;
21 21 import com.google.gson.JsonObject;
22 22 import lombok.extern.slf4j.Slf4j;
23   -import org.eclipse.leshan.core.Link;
24 23 import org.eclipse.leshan.core.model.ResourceModel;
25 24 import org.eclipse.leshan.core.node.LwM2mMultipleResource;
26 25 import org.eclipse.leshan.core.node.LwM2mObject;
... ... @@ -155,6 +154,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
155 154 lwM2MClient.setSessionUuid(UUID.randomUUID());
156 155 this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client Registered", registration);
157 156 this.setLwM2mFromClientValue(lwServer, registration, lwM2MClient);
  157 + lwM2MClient.setClientUpdateValueAfterConnect(this.context.getCtxServer().isClientUpdateValueAfterConnect());
158 158 SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration);
159 159 if (sessionInfo != null) {
160 160 lwM2MClient.setDeviceUuid(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB()));
... ... @@ -165,7 +165,9 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
165 165 transportService.process(sessionInfo, DefaultTransportService.getSessionEventMsg(SessionEvent.OPEN), null);
166 166 transportService.process(sessionInfo, TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null);
167 167 this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client create after Registration", registration);
168   - this.putDelayedUpdateResourcesThingsboard(lwM2MClient);
  168 + if (this.context.getCtxServer().isClientUpdateValueAfterConnect()) {
  169 + this.putDelayedUpdateResourcesThingsboard(lwM2MClient);
  170 + }
169 171 } else {
170 172 log.error("Client: [{}] onRegistered [{}] name [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null);
171 173 }
... ... @@ -272,8 +274,11 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
272 274 /**
273 275 * #0 Add new ObjectModel to context
274 276 * Create new LwM2MClient for current session -> setModelClient...
275   - * #1 Add all ObjectLinks (instance) to control the process of executing requests to the client
  277 + * if need all value after registration:
  278 + * #1.1 Add all ObjectLinks (instance) to control the process of executing requests to the client
276 279 * to get the client model with current values
  280 + * if not need all value after registration (only observe)
  281 + * #1.2 Get observe
277 282 * #2 Get the client model with current values. Analyze the response in -> lwM2MTransportRequest.sendResponse
278 283 *
279 284 * @param lwServer - LeshanServer
... ... @@ -281,48 +286,35 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
281 286 * @param lwM2MClient - object with All parameters off client
282 287 */
283 288 private void setLwM2mFromClientValue(LeshanServer lwServer, Registration registration, LwM2MClient lwM2MClient) {
  289 + // #1.1
  290 + // get all instances in client
  291 + Set<String> clientInstances = ConcurrentHashMap.newKeySet();
284 292 Arrays.stream(registration.getObjectLinks()).forEach(url -> {
285 293 LwM2mPath pathIds = new LwM2mPath(url.getUrl());
286 294 if (pathIds.isObjectInstance() && !pathIds.isResource()) {
287   - // #1
288   - lwM2MClient.getPendingRequests().add(url.getUrl());
289   - // #2
290   - lwM2MTransportRequest.sendAllRequest(lwServer, registration, url.getUrl(), GET_TYPE_OPER_READ, ContentFormat.TLV.getName(),
291   - lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false);
  295 + clientInstances.add(url.getUrl());
292 296 }
293 297 });
294   -
295   - // #1
296   - for (Link url : registration.getObjectLinks()) {
297   - LwM2mPath pathIds = new LwM2mPath(url.getUrl());
298   - if (pathIds.isObjectInstance() && !pathIds.isResource()) {
299   - lwM2MClient.getPendingRequests().add(url.getUrl());
300   - }
301   - }
302   - // #2
303   - for (Link url : registration.getObjectLinks()) {
304   - LwM2mPath pathIds = new LwM2mPath(url.getUrl());
305   - if (pathIds.isObjectInstance() && !pathIds.isResource()) {
306   - lwM2MTransportRequest.sendAllRequest(lwServer, registration, url.getUrl(), GET_TYPE_OPER_READ, ContentFormat.TLV.getName(),
307   - lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false);
308   - }
  298 + if (this.context.getCtxServer().isClientUpdateValueAfterConnect()) {
  299 + lwM2MClient.getPendingRequests().addAll(clientInstances);
  300 + } else {
  301 + // #1.2
  302 + UUID profileUUid = lwM2mInMemorySecurityStore.getSessions().get(registration.getId()).getProfileUuid();
  303 + JsonArray observeValue = lwM2mInMemorySecurityStore.getProfiles().get(profileUUid).getPostObserveProfile();
  304 + observeValue.forEach(path -> {
  305 + String[] resPath = path.getAsString().split("/");
  306 + String instance = "/" + resPath[1] + "/" + resPath[2];
  307 + if (clientInstances.contains(instance)) {
  308 + lwM2MClient.getPendingRequests().add(path.getAsString());
  309 + }
  310 + }
  311 + );
309 312 }
310 313
311   - // #1
312   - Arrays.stream(registration.getObjectLinks()).forEach(url -> {
313   - LwM2mPath pathIds = new LwM2mPath(url.getUrl());
314   - if (pathIds.isObjectInstance() && !pathIds.isResource()) {
315   - lwM2MClient.getPendingRequests().add(url.getUrl());
316   - }
317   - });
318   -
319 314 // #2
320   - Arrays.stream(registration.getObjectLinks()).forEach(url -> {
321   - LwM2mPath pathIds = new LwM2mPath(url.getUrl());
322   - if (pathIds.isObjectInstance() && !pathIds.isResource()) {
323   - lwM2MTransportRequest.sendAllRequest(lwServer, registration, url.getUrl(), GET_TYPE_OPER_READ, ContentFormat.TLV.getName(),
324   - lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false);
325   - }
  315 + lwM2MClient.getPendingRequests().forEach(path -> {
  316 + lwM2MTransportRequest.sendAllRequest(lwServer, registration, path, GET_TYPE_OPER_READ, ContentFormat.TLV.getName(),
  317 + lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false);
326 318 });
327 319 }
328 320
... ... @@ -704,28 +696,21 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
704 696 * @param values - LwM2mSingleResource response.getContent()
705 697 * @param path - resource
706 698 */
707   - private void onObservationSetResourcesValue(Registration registration, Object value, Map<Integer, ?> values, String path) {
  699 + private void onObservationSetResourcesValue(Registration registration, Object value, Map<Integer, ?> values, String path) {
708 700 boolean isChange = false;
709 701 try {
710 702 writeLock.lock();
711 703 // #1
712 704 LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null);
713 705 LwM2mPath pathIds = new LwM2mPath(path);
714   -// log.warn("#0 nameDevice: [{}] resultIds: [{}] value: [{}], values: [{}] ", lwM2MClient.getDeviceName(), pathIds, value, values);
715 706 ResourceModel.Type resModelType = context.getCtxServer().getResourceModelType(registration, pathIds);
716 707 ResourceValue resValueOld = lwM2MClient.getResources().get(path);
717 708 // #2
718 709 if (resValueOld.isMultiInstances() && !values.toString().equals(resValueOld.getResourceValue().toString())) {
719 710 lwM2MClient.getResources().get(path).setValues(values);
720   -// ResourceValue resourceValue = new ResourceValue(values, null, true);
721   -// lwM2MClient.getResources().put(path, resourceValue);
722 711 isChange = true;
723 712 } else if (!LwM2MTransportHandler.equalsResourceValue(resValueOld.getValue(), value, resModelType, pathIds)) {
724 713 lwM2MClient.getResources().get(path).setValue(value);
725   -// ResourceValue resourceValueOld = lwM2MClient.getResources().get(path);
726   -// lwM2MClient.getResources().remove(resourceValueOld);
727   -// ResourceValue resourceValue = new ResourceValue(null, value, false);
728   -// lwM2MClient.getResources().put(path, resourceValue);
729 714 log.warn("upDateResize: [{}] [{}] [{}] [{}]", lwM2MClient.getEndPoint(), lwM2MClient.getResources().size(), value, path);
730 715 isChange = true;
731 716 }
... ...
... ... @@ -17,10 +17,11 @@ package org.thingsboard.server.transport.lwm2m.server.client;
17 17
18 18 import lombok.Data;
19 19 import lombok.extern.slf4j.Slf4j;
20   -import org.eclipse.leshan.core.model.ObjectModel;
21 20 import org.eclipse.leshan.core.node.LwM2mMultipleResource;
22 21 import org.eclipse.leshan.core.node.LwM2mObjectInstance;
23 22 import org.eclipse.leshan.core.node.LwM2mPath;
  23 +import org.eclipse.leshan.core.node.LwM2mResource;
  24 +import org.eclipse.leshan.core.node.LwM2mSingleResource;
24 25 import org.eclipse.leshan.core.response.LwM2mResponse;
25 26 import org.eclipse.leshan.core.response.ReadResponse;
26 27 import org.eclipse.leshan.server.californium.LeshanServer;
... ... @@ -35,7 +36,6 @@ import java.util.Map;
35 36 import java.util.Set;
36 37 import java.util.UUID;
37 38 import java.util.concurrent.ConcurrentHashMap;
38   -import java.util.stream.Collectors;
39 39
40 40 @Slf4j
41 41 @Data
... ... @@ -44,7 +44,7 @@ public class LwM2MClient implements Cloneable {
44 44 private String deviceProfileName;
45 45 private String endPoint;
46 46 private String identity;
47   - private SecurityInfo info;
  47 + private SecurityInfo securityInfo;
48 48 private UUID deviceUuid;
49 49 private UUID sessionUuid;
50 50 private UUID profileUuid;
... ... @@ -59,15 +59,16 @@ public class LwM2MClient implements Cloneable {
59 59 private Set<Integer> delayedRequestsId;
60 60 private Map<String, LwM2mResponse> responses;
61 61 private final LwM2mValueConverterImpl converter;
  62 + private boolean clientUpdateValueAfterConnect;
62 63
63 64 public Object clone() throws CloneNotSupportedException {
64 65 return super.clone();
65 66 }
66 67
67   - public LwM2MClient(String endPoint, String identity, SecurityInfo info, ValidateDeviceCredentialsResponseMsg credentialsResponse, UUID profileUuid) {
  68 + public LwM2MClient(String endPoint, String identity, SecurityInfo securityInfo, ValidateDeviceCredentialsResponseMsg credentialsResponse, UUID profileUuid) {
68 69 this.endPoint = endPoint;
69 70 this.identity = identity;
70   - this.info = info;
  71 + this.securityInfo = securityInfo;
71 72 this.credentialsResponse = credentialsResponse;
72 73 this.attributes = new ConcurrentHashMap<>();
73 74 this.pendingRequests = ConcurrentHashMap.newKeySet();
... ... @@ -98,28 +99,33 @@ public class LwM2MClient implements Cloneable {
98 99 }
99 100
100 101 private void initValue() {
101   - this.responses.forEach((key, resp) -> {
  102 + this.responses.forEach((key, lwM2mResponse) -> {
102 103 LwM2mPath pathIds = new LwM2mPath(key);
103   - if (pathIds.isObject() || pathIds.isObjectInstance() || pathIds.isResource()) {
104   - ObjectModel objectModel = this.lwServer.getModelProvider().getObjectModel(registration).getObjectModels().stream().filter(v -> v.id == pathIds.getObjectId()).collect(Collectors.toList()).get(0);
105   - if (objectModel != null) {
106   - ((LwM2mObjectInstance)((ReadResponse)resp).getContent()).getResources().forEach((k, v) -> {
107   - String rez = pathIds.toString() + "/" + k;
108   - if (((LwM2mObjectInstance) ((ReadResponse) resp).getContent()).getResource(k) instanceof LwM2mMultipleResource){
109   - this.resources.put(rez, new ResourceValue(v.getValues(), null, true));
110   - }
111   - else {
112   - this.resources.put(rez, new ResourceValue(null, v.getValue(), false));
113   - }
114   - });
115   - }
  104 + if (pathIds.isObjectInstance()) {
  105 + ((LwM2mObjectInstance) ((ReadResponse) lwM2mResponse).getContent()).getResources().forEach((k, v) -> {
  106 + String pathRez = pathIds.toString() + "/" + k;
  107 + this.updateResourceValue(pathRez, v);
  108 + });
  109 + }
  110 + else if (pathIds.isResource()) {
  111 + this.updateResourceValue(pathIds.toString(), ((LwM2mResource) ((ReadResponse) lwM2mResponse).getContent()));
116 112 }
117 113 });
118 114 if (this.responses.size() == 0) this.responses = new ConcurrentHashMap<>();
119 115 }
120 116
  117 + private void updateResourceValue(String pathRez, LwM2mResource rez) {
  118 + if (rez instanceof LwM2mMultipleResource){
  119 + this.resources.put(pathRez, new ResourceValue(rez.getValues(), null, true));
  120 + }
  121 + else if (rez instanceof LwM2mSingleResource) {
  122 + this.resources.put(pathRez, new ResourceValue(null, rez.getValue(), false));
  123 + }
  124 + }
  125 +
121 126 /**
122 127 * if path != null
  128 + *
123 129 * @param path
124 130 */
125 131 public void onSuccessOrErrorDelayedRequests(String path) {
... ...
... ... @@ -77,7 +77,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore {
77 77 try {
78 78 String registrationId = this.getByRegistrationId(endPoint, null);
79 79 return (registrationId != null && sessions.size() > 0 && sessions.get(registrationId) != null) ?
80   - sessions.get(registrationId).getInfo() : this.addLwM2MClientToSession(endPoint);
  80 + sessions.get(registrationId).getSecurityInfo() : this.addLwM2MClientToSession(endPoint);
81 81 } finally {
82 82 readLock.unlock();
83 83 }
... ... @@ -93,7 +93,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore {
93 93 readLock.lock();
94 94 try {
95 95 String integrationId = this.getByRegistrationId(null, identity);
96   - return (integrationId != null) ? sessions.get(integrationId).getInfo() : this.addLwM2MClientToSession(identity);
  96 + return (integrationId != null) ? sessions.get(integrationId).getSecurityInfo() : this.addLwM2MClientToSession(identity);
97 97 } finally {
98 98 readLock.unlock();
99 99 }
... ... @@ -103,7 +103,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore {
103 103 public Collection<SecurityInfo> getAll() {
104 104 readLock.lock();
105 105 try {
106   - return Collections.unmodifiableCollection(this.sessions.values().stream().map(LwM2MClient::getInfo).collect(Collectors.toList()));
  106 + return Collections.unmodifiableCollection(this.sessions.values().stream().map(LwM2MClient::getSecurityInfo).collect(Collectors.toList()));
107 107 } finally {
108 108 readLock.unlock();
109 109 }
... ... @@ -119,7 +119,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore {
119 119 LwM2MClient lwM2MClient = (sessions.get(registrationId) != null) ? sessions.get(registrationId) : null;
120 120 if (lwM2MClient != null) {
121 121 if (listener != null) {
122   - listener.securityInfoRemoved(INFOS_ARE_COMPROMISED, lwM2MClient.getInfo());
  122 + listener.securityInfoRemoved(INFOS_ARE_COMPROMISED, lwM2MClient.getSecurityInfo());
123 123 }
124 124 sessions.remove(registrationId);
125 125 }
... ...
... ... @@ -119,6 +119,10 @@ public class LwM2MTransportConfigServer {
119 119 private int registeredPoolSize;
120 120
121 121 @Getter
  122 + @Value("${transport.lwm2m.client_update_value_after_connect:}")
  123 + private boolean clientUpdateValueAfterConnect;
  124 +
  125 + @Getter
122 126 @Value("${transport.lwm2m.update_registered_pool_size:}")
123 127 private int updateRegisteredPoolSize;
124 128
... ...