Commit 3a7101a8a356cb67756d8ca31ce45d0a1658821a

Authored by nickAS21
1 parent e91cb653

Lwm2m: backEnd: model Client from provide

... ... @@ -65,15 +65,10 @@
65 65 <groupId>ch.qos.logback</groupId>
66 66 <artifactId>logback-classic</artifactId>
67 67 </dependency>
68   - <!-- lechan start -->
69 68 <dependency>
70 69 <groupId>org.eclipse.leshan</groupId>
71 70 <artifactId>leshan-server-cf</artifactId>
72 71 </dependency>
73   -<!-- <dependency>-->
74   -<!-- <groupId>org.eclipse.leshan</groupId>-->
75   -<!-- <artifactId>leshan-server-cf</artifactId>-->
76   -<!-- </dependency> -->
77 72 <dependency>
78 73 <groupId>org.eclipse.leshan</groupId>
79 74 <artifactId>leshan-client-cf</artifactId>
... ... @@ -83,12 +78,6 @@
83 78 <groupId>org.eclipse.leshan</groupId>
84 79 <artifactId>leshan-server-redis</artifactId>
85 80 </dependency>
86   -<!-- <dependency>-->
87   -<!-- <groupId>org.eclipse.californium</groupId>-->
88   -<!-- <artifactId>californium-core</artifactId>-->
89   -<!-- </dependency>-->
90   - <!-- leshan finish -->
91   -
92 81 <dependency>
93 82 <groupId>org.springframework.boot</groupId>
94 83 <artifactId>spring-boot-starter-test</artifactId>
... ... @@ -107,14 +96,12 @@
107 96 <dependency>
108 97 <groupId>org.eclipse.californium</groupId>
109 98 <artifactId>californium-core</artifactId>
110   - <version>${californium.version}</version>
111   - <type>test-jar</type>
  99 + <type>test-jar</type>
112 100 <scope>test</scope>
113 101 </dependency>
114 102 <dependency>
115 103 <groupId>org.eclipse.californium</groupId>
116 104 <artifactId>element-connector</artifactId>
117   - <version>${californium.version}</version>
118 105 <type>test-jar</type>
119 106 <scope>test</scope>
120 107 </dependency>
... ...
... ... @@ -124,7 +124,7 @@ public class LwM2MTransportRequest {
124 124 if (registration != null && resultIds.getObjectId() >= 0) {
125 125 DownlinkRequest request = null;
126 126 ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
127   - ResourceModel resource = service.context.getCtxServer().getResourceModel(resultIds);
  127 + ResourceModel resource = service.context.getCtxServer().getResourceModel(registration, resultIds);
128 128 timeoutInMs = timeoutInMs > 0 ? timeoutInMs : DEFAULT_TIMEOUT;
129 129 switch (typeOper) {
130 130 case GET_TYPE_OPER_READ:
... ... @@ -222,7 +222,7 @@ public class LwM2MTransportRequest {
222 222 this.sendRequest(lwServer, registration, request, lwM2MClient, timeoutInMs, isDelayedUpdate);
223 223 } else if (request == null && isDelayedUpdate) {
224 224 String msg = String.format(LOG_LW2M_ERROR + ": sendRequest: Resource path - %s msg No SendRequest to Client", target);
225   - service.sentLogsToThingsboard(msg, registration.getId());
  225 + service.sentLogsToThingsboard(msg, registration);
226 226 log.error("[{}] - [{}] No SendRequest", target);
227 227 this.handleResponseError(registration, target, lwM2MClient, isDelayedUpdate);
228 228
... ... @@ -251,14 +251,14 @@ public class LwM2MTransportRequest {
251 251 String msg = String.format(LOG_LW2M_INFO + ": sendRequest Replace%s: CoapCde - %s Lwm2m code - %d name - %s Resource path - %s value - %s SendRequest to Client",
252 252 delayedUpdateStr, ((Response) response.getCoapResponse()).getCode(), response.getCode().getCode(), response.getCode().getName(), request.getPath().toString(),
253 253 ((LwM2mSingleResource) ((WriteRequest) request).getNode()).getValue().toString());
254   - service.sentLogsToThingsboard(msg, registration.getId());
  254 + service.sentLogsToThingsboard(msg, registration);
255 255 log.info("[{}] - [{}] [{}] [{}] Update SendRequest[{}]", ((Response) response.getCoapResponse()).getCode(), response.getCode(), request.getPath().toString(),
256 256 ((LwM2mSingleResource) ((WriteRequest) request).getNode()).getValue(), delayedUpdateStr);
257 257 }
258 258 } else {
259 259 String msg = String.format(LOG_LW2M_ERROR + ": sendRequest: CoapCode - %s Lwm2m code - %d name - %s Resource path - %s SendRequest to Client",
260 260 ((Response) response.getCoapResponse()).getCode(), response.getCode().getCode(), response.getCode().getName(), request.getPath().toString());
261   - service.sentLogsToThingsboard(msg, registration.getId());
  261 + service.sentLogsToThingsboard(msg, registration);
262 262 log.error("[{}] - [{}] [{}] error SendRequest", ((Response) response.getCoapResponse()).getCode(), response.getCode(), request.getPath().toString());
263 263 if (request instanceof WriteRequest && ((WriteRequest) request).isReplaceRequest() && isDelayedUpdate) {
264 264 this.handleResponseError(registration, request.getPath().toString(), lwM2MClient, isDelayedUpdate);
... ... @@ -268,7 +268,7 @@ public class LwM2MTransportRequest {
268 268 }, e -> {
269 269 String msg = String.format(LOG_LW2M_ERROR + ": sendRequest: Resource path - %s msg error - %s SendRequest to Client",
270 270 request.getPath().toString(), e.toString());
271   - service.sentLogsToThingsboard(msg, registration.getId());
  271 + service.sentLogsToThingsboard(msg, registration);
272 272 log.error("[{}] - [{}] error SendRequest", request.getPath().toString(), e.toString());
273 273 if (request instanceof WriteRequest && ((WriteRequest) request).isReplaceRequest() && isDelayedUpdate) {
274 274 this.handleResponseError(registration, request.getPath().toString(), lwM2MClient, isDelayedUpdate);
... ... @@ -300,7 +300,7 @@ public class LwM2MTransportRequest {
300 300 String patn = "/" + objectId + "/" + instanceId + "/" + resourceId;
301 301 String msg = String.format(LOG_LW2M_ERROR + ": NumberFormatException: Resource path - %s type - %s value - %s msg error - %s SendRequest to Client",
302 302 patn, type, value, e.toString());
303   - service.sentLogsToThingsboard(msg, registration.getId());
  303 + service.sentLogsToThingsboard(msg, registration);
304 304 log.error("Path: [{}] type: [{}] value: [{}] errorMsg: [{}]]", patn, type, value, e.toString());
305 305 return null;
306 306 }
... ...
... ... @@ -20,13 +20,11 @@ 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.model.ObjectModel;
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;
27 26 import org.eclipse.leshan.core.node.LwM2mObjectInstance;
28 27 import org.eclipse.leshan.core.node.LwM2mPath;
29   -import org.eclipse.leshan.core.node.LwM2mResource;
30 28 import org.eclipse.leshan.core.node.LwM2mSingleResource;
31 29 import org.eclipse.leshan.core.observation.Observation;
32 30 import org.eclipse.leshan.core.request.ContentFormat;
... ... @@ -51,7 +49,6 @@ import org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCre
51 49 import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg;
52 50 import org.thingsboard.server.transport.lwm2m.server.client.AttrTelemetryObserveValue;
53 51 import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClient;
54   -import org.thingsboard.server.transport.lwm2m.server.client.ModelObject;
55 52 import org.thingsboard.server.transport.lwm2m.server.client.ResourceValue;
56 53 import org.thingsboard.server.transport.lwm2m.server.client.ResultsAnalyzerParameters;
57 54 import org.thingsboard.server.transport.lwm2m.server.secure.LwM2mInMemorySecurityStore;
... ... @@ -62,9 +59,9 @@ import java.util.ArrayList;
62 59 import java.util.Arrays;
63 60 import java.util.Collection;
64 61 import java.util.HashSet;
  62 +import java.util.LinkedHashSet;
65 63 import java.util.List;
66 64 import java.util.Map;
67   -import java.util.NoSuchElementException;
68 65 import java.util.Optional;
69 66 import java.util.Random;
70 67 import java.util.Set;
... ... @@ -76,7 +73,6 @@ import java.util.concurrent.ExecutorService;
76 73 import java.util.concurrent.Executors;
77 74 import java.util.concurrent.TimeUnit;
78 75 import java.util.concurrent.atomic.AtomicBoolean;
79   -import java.util.function.Predicate;
80 76 import java.util.stream.Collectors;
81 77
82 78 import static org.thingsboard.server.common.transport.util.JsonUtils.getJsonObject;
... ... @@ -148,13 +144,14 @@ public class LwM2MTransportService {
148 144 public void onRegistered(LeshanServer lwServer, Registration registration, Collection<Observation> previousObsersations) {
149 145 executorRegistered.submit(() -> {
150 146 try {
151   - log.info("[{}] Client: onRegistered name ", registration.getEndpoint());
  147 + log.info("[{}] [{{}] Client: create after Registration", registration.getEndpoint(), registration.getId());
152 148 LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.updateInSessionsLwM2MClient(lwServer, registration);
153 149 if (lwM2MClient != null) {
154 150 lwM2MClient.setLwM2MTransportService(this);
155 151 lwM2MClient.setSessionUuid(UUID.randomUUID());
  152 + this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client Registered", registration);
156 153 this.setLwM2MClient(lwServer, registration, lwM2MClient);
157   - SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration.getId());
  154 + SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration);
158 155 if (sessionInfo != null) {
159 156 lwM2MClient.setDeviceUuid(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB()));
160 157 lwM2MClient.setProfileUuid(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
... ... @@ -163,7 +160,7 @@ public class LwM2MTransportService {
163 160 transportService.registerAsyncSession(sessionInfo, new LwM2MSessionMsgListener(this, sessionInfo));
164 161 transportService.process(sessionInfo, DefaultTransportService.getSessionEventMsg(SessionEvent.OPEN), null);
165 162 transportService.process(sessionInfo, TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null);
166   - this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client registration", registration.getId());
  163 + this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client create after Registration", registration);
167 164 } else {
168 165 log.error("Client: [{}] onRegistered [{}] name [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null);
169 166 }
... ... @@ -184,7 +181,7 @@ public class LwM2MTransportService {
184 181 public void updatedReg(LeshanServer lwServer, Registration registration) {
185 182 executorUpdateRegistered.submit(() -> {
186 183 try {
187   - SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration.getId());
  184 + SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration);
188 185 if (sessionInfo != null) {
189 186 this.checkInactivity(sessionInfo);
190 187 log.info("Client: [{}] updatedReg [{}] name [{}] profile ", registration.getId(), registration.getEndpoint(), sessionInfo.getDeviceType());
... ... @@ -206,7 +203,7 @@ public class LwM2MTransportService {
206 203 public void unReg(Registration registration, Collection<Observation> observations) {
207 204 executorUnRegistered.submit(() -> {
208 205 try {
209   - this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client unRegistration", registration.getId());
  206 + this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client unRegistration", registration);
210 207 this.closeClientSession(registration);
211 208 } catch (Throwable t) {
212 209 log.error("[{}] endpoint [{}] error Unable un registration.", registration.getEndpoint(), t);
... ... @@ -215,7 +212,7 @@ public class LwM2MTransportService {
215 212 }
216 213
217 214 private void closeClientSession(Registration registration) {
218   - SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration.getId());
  215 + SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration);
219 216 if (sessionInfo != null) {
220 217 transportService.deregisterSession(sessionInfo);
221 218 this.doCloseSession(sessionInfo);
... ... @@ -278,9 +275,6 @@ public class LwM2MTransportService {
278 275 * @param lwM2MClient - object with All parameters off client
279 276 */
280 277 private void setLwM2MClient(LeshanServer lwServer, Registration registration, LwM2MClient lwM2MClient) {
281   - // #0
282   - this.setNewObjectModels(lwServer, registration);
283   - // #1
284 278 Arrays.stream(registration.getObjectLinks()).forEach(url -> {
285 279 LwM2mPath pathIds = new LwM2mPath(url.getUrl());
286 280 if (pathIds.isObjectInstance() && !pathIds.isResource()) {
... ... @@ -297,30 +291,35 @@ public class LwM2MTransportService {
297 291 });
298 292 }
299 293
300   - private void setNewObjectModels(LeshanServer lwServer, Registration registration) {
301   - Arrays.stream(registration.getObjectLinks()).forEach(url -> {
302   - LwM2mPath pathIds = new LwM2mPath(url.getUrl());
303   - if (pathIds.isObjectInstance() && !pathIds.isResource() && !context.getCtxServer().getObjectModels().containsKey(pathIds.getObjectId())) {
304   - ObjectModel model = lwServer.getModelProvider().getObjectModel(registration).getObjectModels().stream().filter(v -> v.id == pathIds.getObjectId()).collect(Collectors.toList()).get(0);
305   - context.getCtxServer().getObjectModels().put(pathIds.getObjectId(), model);
306   - }
307   - });
  294 + /**
  295 + * @param registration - Registration LwM2M Client
  296 + * @return - sessionInfo after access connect client
  297 + */
  298 + private SessionInfoProto getValidateSessionInfo(Registration registration) {
  299 + LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null);
  300 + return getNewSessionInfoProto(lwM2MClient);
  301 +
308 302 }
309 303
310 304 /**
311   - * @param registrationId - Id of Registration LwM2M Client
312   - * @return - sessionInfo after access connect client
  305 + *
  306 + * @param registrationId -
  307 + * @return -
313 308 */
314 309 private SessionInfoProto getValidateSessionInfo(String registrationId) {
315   - SessionInfoProto sessionInfo = null;
316   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClient(registrationId);
  310 + LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(null, registrationId);
  311 + return getNewSessionInfoProto(lwM2MClient);
  312 + }
  313 +
  314 + private SessionInfoProto getNewSessionInfoProto(LwM2MClient lwM2MClient) {
317 315 if (lwM2MClient != null) {
318 316 ValidateDeviceCredentialsResponseMsg msg = lwM2MClient.getCredentialsResponse();
319 317 if (msg == null || msg.getDeviceInfo() == null) {
320 318 log.error("[{}] [{}]", lwM2MClient.getEndPoint(), CLIENT_NOT_AUTHORIZED);
321 319 this.closeClientSession(lwM2MClient.getRegistration());
  320 + return null;
322 321 } else {
323   - sessionInfo = SessionInfoProto.newBuilder()
  322 + return SessionInfoProto.newBuilder()
324 323 .setNodeId(this.context.getNodeId())
325 324 .setSessionIdMSB(lwM2MClient.getSessionUuid().getMostSignificantBits())
326 325 .setSessionIdLSB(lwM2MClient.getSessionUuid().getLeastSignificantBits())
... ... @@ -335,7 +334,7 @@ public class LwM2MTransportService {
335 334 .build();
336 335 }
337 336 }
338   - return sessionInfo;
  337 + return null;
339 338 }
340 339
341 340 /**
... ... @@ -365,7 +364,7 @@ public class LwM2MTransportService {
365 364 * @param lwM2MClient - LwM2M Client
366 365 */
367 366 public void putDelayedUpdateResourcesThingsboard(LwM2MClient lwM2MClient) {
368   - SessionInfoProto sessionInfo = this.getValidateSessionInfo(lwM2MClient.getRegistration().getId());
  367 + SessionInfoProto sessionInfo = this.getValidateSessionInfo(lwM2MClient.getRegistration());
369 368 if (sessionInfo != null) {
370 369 //#1.1 + #1.2
371 370 List<String> attrSharedNames = this.getNamesAttrFromProfileIsWritable(lwM2MClient);
... ... @@ -404,7 +403,7 @@ public class LwM2MTransportService {
404 403 attributesResponse.getSharedAttributeListList().forEach(attr -> {
405 404 String path = this.getPathAttributeUpdate(sessionInfo, attr.getKv().getKey());
406 405 // #1.1
407   - if (lwM2MClient.getDelayedRequests().keySet().contains(path) && attr.getTs() > lwM2MClient.getDelayedRequests().get(path).getTs()) {
  406 + if (lwM2MClient.getDelayedRequests().containsKey(path) && attr.getTs() > lwM2MClient.getDelayedRequests().get(path).getTs()) {
408 407 lwM2MClient.getDelayedRequests().put(path, attr);
409 408 } else {
410 409 lwM2MClient.getDelayedRequests().put(path, attr);
... ... @@ -412,7 +411,7 @@ public class LwM2MTransportService {
412 411 });
413 412 // #2.1
414 413 lwM2MClient.getDelayedRequests().forEach((k, v) -> {
415   - List listV = new ArrayList<TransportProtos.KeyValueProto>();
  414 + ArrayList<TransportProtos.KeyValueProto> listV = new ArrayList<>();
416 415 listV.add(v.getKv());
417 416 this.putDelayedUpdateResourcesClient(lwM2MClient, this.getResourceValueToString(lwM2MClient, k), getJsonObject(listV).get(v.getKv().getKey()), k);
418 417 });
... ... @@ -432,24 +431,24 @@ public class LwM2MTransportService {
432 431 }
433 432
434 433 /**
435   - * Get names and keyNames from profile attr resources IsWritable
436   - *
  434 + * Get names and keyNames from profile shared!!!! attr resources IsWritable
437 435 * @param lwM2MClient -
438   - * @return ArrayList names and keyNames from profile attr resources IsWritable
  436 + * @return ArrayList keyNames from profile attr resources shared!!!! && IsWritable
439 437 */
440 438 private List<String> getNamesAttrFromProfileIsWritable(LwM2MClient lwM2MClient) {
441   - Set<String> namesIsIsWritable = ConcurrentHashMap.newKeySet();
442 439 AttrTelemetryObserveValue profile = lwM2mInMemorySecurityStore.getProfile(lwM2MClient.getProfileUuid());
443 440 Set attrSet = new Gson().fromJson(profile.getPostAttributeProfile(), Set.class);
444 441 ConcurrentMap<String, String> keyNamesMap = new Gson().fromJson(profile.getPostKeyNameProfile().toString(), ConcurrentHashMap.class);
  442 +
445 443 ConcurrentMap<String, String> keyNamesIsWritable = keyNamesMap.entrySet()
446 444 .stream()
447   - .filter(e -> (attrSet.contains(e.getKey()) && context.getCtxServer().getResourceModel(new LwM2mPath(e.getKey())) != null &&
448   - context.getCtxServer().getResourceModel(new LwM2mPath(e.getKey())).operations.isWritable()))
  445 + .filter(e -> (attrSet.contains(e.getKey()) && context.getCtxServer().getResourceModel(lwM2MClient.getRegistration(), new LwM2mPath(e.getKey())) != null &&
  446 + context.getCtxServer().getResourceModel(lwM2MClient.getRegistration(), new LwM2mPath(e.getKey())).operations.isWritable()))
449 447 .collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue));
450   - namesIsIsWritable.addAll(new HashSet<>(keyNamesIsWritable.values()));
451   - keyNamesIsWritable.keySet().forEach(p -> namesIsIsWritable.add(this.getResourceName(p)));
452   - return new ArrayList<>(namesIsIsWritable);
  448 +
  449 + Set<String> namesIsWritable = ConcurrentHashMap.newKeySet();
  450 + namesIsWritable.addAll(new HashSet<>(keyNamesIsWritable.values()));
  451 + return new ArrayList<>(namesIsWritable);
453 452 }
454 453
455 454
... ... @@ -484,9 +483,9 @@ public class LwM2MTransportService {
484 483 log.error("[{}] updateAttrTelemetry", e.toString());
485 484 }
486 485 if (attributes.getAsJsonObject().entrySet().size() > 0)
487   - this.updateParametersOnThingsboard(attributes, DEVICE_ATTRIBUTES_TOPIC, registration.getId());
  486 + this.updateParametersOnThingsboard(attributes, DEVICE_ATTRIBUTES_TOPIC, registration);
488 487 if (telemetries.getAsJsonObject().entrySet().size() > 0)
489   - this.updateParametersOnThingsboard(telemetries, DEVICE_TELEMETRY_TOPIC, registration.getId());
  488 + this.updateParametersOnThingsboard(telemetries, DEVICE_TELEMETRY_TOPIC, registration);
490 489 }
491 490
492 491 /**
... ... @@ -541,9 +540,8 @@ public class LwM2MTransportService {
541 540 JsonObject names = lwM2mInMemorySecurityStore.getProfiles().get(lwM2MClient.getProfileUuid()).getPostKeyNameProfile();
542 541 String resName = String.valueOf(names.get(path));
543 542 if (resName != null && !resName.isEmpty()) {
544   - String resValue = null;
545 543 try {
546   - resValue = this.getResourceValueToString(lwM2MClient, path);
  544 + String resValue = this.getResourceValueToString(lwM2MClient, path);
547 545 if (resValue != null) {
548 546 parameters.addProperty(resName, resValue);
549 547 }
... ... @@ -558,14 +556,14 @@ public class LwM2MTransportService {
558 556 *
559 557 * @param msg - JsonArray: [{name: value}]
560 558 * @param topicName - Api Attribute or Telemetry
561   - * @param registrationId - Id of Registration LwM2M Client
  559 + * @param registration - Id of Registration LwM2M Client
562 560 */
563   - public void updateParametersOnThingsboard(JsonElement msg, String topicName, String registrationId) {
564   - SessionInfoProto sessionInfo = this.getValidateSessionInfo(registrationId);
  561 + public void updateParametersOnThingsboard(JsonElement msg, String topicName, Registration registration) {
  562 + SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration);
565 563 if (sessionInfo != null) {
566 564 context.sentParametersOnThingsboard(msg, topicName, sessionInfo);
567 565 } else {
568   - log.error("Client: [{}] updateParametersOnThingsboard [{}] sessionInfo ", registrationId, null);
  566 + log.error("Client: [{}] updateParametersOnThingsboard [{}] sessionInfo ", registration, null);
569 567 }
570 568 }
571 569
... ... @@ -655,10 +653,9 @@ public class LwM2MTransportService {
655 653 public void onObservationResponse(Registration registration, String path, ReadResponse response) {
656 654 if (response.getContent() != null) {
657 655 if (response.getContent() instanceof LwM2mObject) {
658   - LwM2mObject content = (LwM2mObject) response.getContent();
659   - String target = "/" + content.getId();
  656 +// LwM2mObject content = (LwM2mObject) response.getContent();
660 657 } else if (response.getContent() instanceof LwM2mObjectInstance) {
661   - LwM2mObjectInstance content = (LwM2mObjectInstance) response.getContent();
  658 +// LwM2mObjectInstance content = (LwM2mObjectInstance) response.getContent();
662 659 } else if (response.getContent() instanceof LwM2mSingleResource) {
663 660 LwM2mSingleResource content = (LwM2mSingleResource) response.getContent();
664 661 this.onObservationSetResourcesValue(registration, content.getValue(), null, path);
... ... @@ -684,10 +681,10 @@ public class LwM2MTransportService {
684 681 boolean isChange = false;
685 682 try {
686 683 // #1
687   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClient(registration.getId());
  684 + LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null);
688 685 LwM2mPath pathIds = new LwM2mPath(path);
689 686 log.warn("#0 nameDevice: [{}] resultIds: [{}] value: [{}], values: [{}] ", lwM2MClient.getDeviceName(), pathIds, value, values);
690   - ResourceModel.Type resModelType = context.getCtxServer().getResourceModelType(pathIds);
  687 + ResourceModel.Type resModelType = context.getCtxServer().getResourceModelType(registration, pathIds);
691 688 ResourceValue resValueOld = lwM2MClient.getResources().get(path);
692 689 // #2
693 690 if (resValueOld.isMultiInstances() && !values.toString().equals(resValueOld.getResourceValue().toString())) {
... ... @@ -739,8 +736,8 @@ public class LwM2MTransportService {
739 736 String value = de.getValue().getAsString();
740 737 LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getSession(new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB())).entrySet().iterator().next().getValue();
741 738 AttrTelemetryObserveValue profile = lwM2mInMemorySecurityStore.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
742   - ResourceModel resourceModel = context.getCtxServer().getResourceModel(new LwM2mPath(path));
743   - if (path != null && (this.validatePathInAttrProfile(profile, path) || this.validatePathInTelemetryProfile(profile, path))) {
  739 + ResourceModel resourceModel = context.getCtxServer().getResourceModel(lwM2MClient.getRegistration(), new LwM2mPath(path));
  740 + if (!path.isEmpty() && (this.validatePathInAttrProfile(profile, path) || this.validatePathInTelemetryProfile(profile, path))) {
744 741 if (resourceModel != null && resourceModel.operations.isWritable()) {
745 742 lwM2MTransportRequest.sendAllRequest(lwM2MClient.getLwServer(), lwM2MClient.getRegistration(), path, POST_TYPE_OPER_WRITE_REPLACE,
746 743 ContentFormat.TLV.getName(), lwM2MClient, null, value, this.context.getCtxServer().getTimeout(),
... ... @@ -748,12 +745,12 @@ public class LwM2MTransportService {
748 745 } else {
749 746 log.error("Resource path - [{}] value - [{}] is not Writable and cannot be updated", path, value);
750 747 String logMsg = String.format(LOG_LW2M_ERROR + ": attributeUpdate: Resource path - %s value - %s is not Writable and cannot be updated", path, value);
751   - this.sentLogsToThingsboard(logMsg, lwM2MClient.getRegistration().getId());
  748 + this.sentLogsToThingsboard(logMsg, lwM2MClient.getRegistration());
752 749 }
753 750 } else {
754 751 log.error("Attribute name - [{}] value - [{}] is not present as attribute in profile and cannot be updated", de.getKey(), value);
755 752 String logMsg = String.format(LOG_LW2M_ERROR + ": attributeUpdate: attribute name - %s value - %s is not present as attribute in profile and cannot be updated", de.getKey(), value);
756   - this.sentLogsToThingsboard(logMsg, lwM2MClient.getRegistration().getId());
  753 + this.sentLogsToThingsboard(logMsg, lwM2MClient.getRegistration());
757 754 }
758 755 });
759 756 } else if (msg.getSharedDeletedCount() > 0) {
... ... @@ -767,11 +764,12 @@ public class LwM2MTransportService {
767 764 *
768 765 * @param sessionInfo -
769 766 * @param name -
770   - * @return true if path isPresent in postProfile
  767 + * @return path if path isPresent in postProfile
771 768 */
772 769 private String getPathAttributeUpdate(TransportProtos.SessionInfoProto sessionInfo, String name) {
773 770 String profilePath = this.getPathAttributeUpdateProfile(sessionInfo, name);
774   - return !profilePath.isEmpty() ? profilePath : this.getPathAttributeUpdateModelObject(sessionInfo, name);
  771 +// return !profilePath.isEmpty() ? profilePath : this.getPathAttributeUpdateModelObject(name);
  772 + return !profilePath.isEmpty() ? profilePath : null;
775 773 }
776 774
777 775 /**
... ... @@ -800,7 +798,7 @@ public class LwM2MTransportService {
800 798 *
801 799 * @param sessionInfo -
802 800 * @param name -
803   - * @return
  801 + * @return -
804 802 */
805 803 private String getPathAttributeUpdateProfile(TransportProtos.SessionInfoProto sessionInfo, String name) {
806 804 AttrTelemetryObserveValue profile = lwM2mInMemorySecurityStore.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
... ... @@ -810,31 +808,6 @@ public class LwM2MTransportService {
810 808 }
811 809
812 810 /**
813   - * Get path to resource from ModelObject equal name
814   - *
815   - * @param name -
816   - * @return true if name isPresent as Resource name (usual format) in ResourceModel
817   - */
818   - private String getPathAttributeUpdateModelObject(TransportProtos.SessionInfoProto sessionInfo, String name) {
819   - try {
820   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getSession(new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB())).entrySet().iterator().next().getValue();
821   - Predicate<Map.Entry<Integer, ResourceModel>> predicateRes = res -> name.equals(res.getValue().name);
822   - Predicate<Map.Entry<Integer, ModelObject>> predicateObj = (obj -> {
823   - return obj.getValue().getObjectModel().resources.entrySet().stream().filter(predicateRes).findFirst().isPresent();
824   - });
825   -// Map.Entry<Integer, ModelObject> object = lwM2MClient.getModelObjects().entrySet().stream().filter(predicateObj).findFirst().get();
826   - Map.Entry<Integer, ModelObject> object = null;
827   - ModelObject modelObject = object.getValue();
828   - LwM2mObjectInstance instance = modelObject.getInstances().entrySet().stream().findFirst().get().getValue();
829   - ResourceModel resource = modelObject.getObjectModel().resources.entrySet().stream().filter(predicateRes).findFirst().get().getValue();
830   - return new LwM2mPath(object.getKey(), instance.getId(), resource.id).toString();
831   - } catch (NoSuchElementException e) {
832   - log.error("[{}] keyName [{}]", name, e.toString());
833   - return null;
834   - }
835   - }
836   -
837   - /**
838 811 * Update resource (attribute) value on thingsboard after update value in client
839 812 *
840 813 * @param registration -
... ... @@ -842,17 +815,14 @@ public class LwM2MTransportService {
842 815 * @param request -
843 816 */
844 817 public void onAttributeUpdateOk(Registration registration, String path, WriteRequest request, boolean isDelayedUpdate) {
845   -// ResultIds resultIds = new ResultIds(path);
846   - LwM2mPath resultIds = new LwM2mPath(path);
847   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClient(registration.getId());
848   -// LwM2mResource resource = lwM2MClient.getModelObjects().get(resultIds.getObjectId()).getInstances().get(resultIds.getObjectInstanceId()).getResource(resultIds.getResourceId());
849   - LwM2mResource resource = null;
850   - if (resource.isMultiInstances()) {
  818 + ResourceModel resource = context.getCtxServer().getResourceModel(registration, new LwM2mPath(path));
  819 + if (resource.multiple) {
851 820 this.onObservationSetResourcesValue(registration, null, ((LwM2mSingleResource) request.getNode()).getValues(), path);
852 821 } else {
853 822 this.onObservationSetResourcesValue(registration, ((LwM2mSingleResource) request.getNode()).getValue(), null, path);
854 823 }
855   - if (isDelayedUpdate) lwM2MClient.onSuccessOrErrorDelayedRequests(request.getPath().toString());
  824 + if (isDelayedUpdate) lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null)
  825 + .onSuccessOrErrorDelayedRequests(request.getPath().toString());
856 826 }
857 827
858 828 /**
... ... @@ -863,7 +833,7 @@ public class LwM2MTransportService {
863 833 Set<String> registrationIds = lwM2mInMemorySecurityStore.getSessions().entrySet()
864 834 .stream()
865 835 .filter(e -> e.getValue().getProfileUuid().equals(deviceProfile.getUuidId()))
866   - .map(Map.Entry::getKey).sorted().collect(Collectors.toSet());
  836 + .map(Map.Entry::getKey).sorted().collect(Collectors.toCollection(LinkedHashSet::new));
867 837 if (registrationIds.size() > 0) {
868 838 this.onDeviceUpdateChangeProfile(registrationIds, deviceProfile);
869 839 }
... ... @@ -968,11 +938,11 @@ public class LwM2MTransportService {
968 938 if (sentAttrToThingsboard.getPathPostParametersAdd().size() > 0) {
969 939 // update value in Resources
970 940 registrationIds.forEach(registrationId -> {
971   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClient(registrationId);
  941 + LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(null, registrationId);
972 942 LeshanServer lwServer = lwM2MClient.getLwServer();
973 943 Registration registration = lwM2mInMemorySecurityStore.getByRegistration(registrationId);
974 944 log.warn("[{}] # 4.1", registration.getEndpoint());
975   - this.updateResourceValueObserve(lwServer, registration, lwM2MClient, sentAttrToThingsboard.getPathPostParametersAdd(), GET_TYPE_OPER_READ);
  945 + this.updateResourceValueObserve(lwServer, registration, sentAttrToThingsboard.getPathPostParametersAdd(), GET_TYPE_OPER_READ);
976 946 // sent attr/telemetry to tingsboard for new path
977 947 this.updateAttrTelemetry(registration, false, sentAttrToThingsboard.getPathPostParametersAdd());
978 948 });
... ... @@ -997,11 +967,11 @@ public class LwM2MTransportService {
997 967 ResultsAnalyzerParameters postObserveAnalyzer = this.getAnalyzerParameters(sentObserveToClientOld.getPathPostParametersAdd(), sentObserveToClientNew.getPathPostParametersAdd());
998 968 // sent Request observe to Client
999 969 registrationIds.forEach(registrationId -> {
1000   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClient(registrationId);
  970 + LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClient(null, registrationId);
1001 971 LeshanServer lwServer = lwM2MClient.getLwServer();
1002 972 Registration registration = lwM2mInMemorySecurityStore.getByRegistration(registrationId);
1003 973 log.warn("[{}] # 5.1", registration.getEndpoint());
1004   - this.updateResourceValueObserve(lwServer, registration, lwM2MClient, postObserveAnalyzer.getPathPostParametersAdd(), GET_TYPE_OPER_OBSERVE);
  974 + this.updateResourceValueObserve(lwServer, registration, postObserveAnalyzer.getPathPostParametersAdd(), GET_TYPE_OPER_OBSERVE);
1005 975 // 5.3 del
1006 976 // sent Request cancel observe to Client
1007 977 this.cancelObserveIsValue(lwServer, registration, postObserveAnalyzer.getPathPostParametersDel());
... ... @@ -1052,10 +1022,9 @@ public class LwM2MTransportService {
1052 1022 *
1053 1023 * @param lwServer - LeshanServer
1054 1024 * @param registration - Registration LwM2M Client
1055   - * @param lwM2MClient - object with All parameters off client
1056 1025 * @param targets - path Resources == [ "/2/0/0", "/2/0/1"]
1057 1026 */
1058   - private void updateResourceValueObserve(LeshanServer lwServer, Registration registration, LwM2MClient lwM2MClient, Set<String> targets, String typeOper) {
  1027 + private void updateResourceValueObserve(LeshanServer lwServer, Registration registration, Set<String> targets, String typeOper) {
1059 1028 targets.forEach(target -> {
1060 1029 LwM2mPath pathIds = new LwM2mPath(target);
1061 1030 if (pathIds.isResource()) {
... ... @@ -1073,7 +1042,7 @@ public class LwM2MTransportService {
1073 1042 }
1074 1043
1075 1044 private void cancelObserveIsValue(LeshanServer lwServer, Registration registration, Set<String> paramAnallyzer) {
1076   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClient(registration.getId());
  1045 + LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null);
1077 1046 paramAnallyzer.forEach(p -> {
1078 1047 if (this.getResourceValue(lwM2MClient, new LwM2mPath(p)) != null) {
1079 1048 this.setCancelObservationRecourse(lwServer, registration, p);
... ... @@ -1130,7 +1099,7 @@ public class LwM2MTransportService {
1130 1099
1131 1100 /**
1132 1101 * if sessionInfo removed from sessions, then new registerAsyncSession
1133   - * @param sessionInfo
  1102 + * @param sessionInfo -
1134 1103 */
1135 1104 private void checkInactivity(SessionInfoProto sessionInfo) {
1136 1105 if (transportService.reportActivity(sessionInfo) == null) {
... ... @@ -1138,20 +1107,14 @@ public class LwM2MTransportService {
1138 1107 }
1139 1108 }
1140 1109
1141   - public void sentLogsToThingsboard(String msg, String registrationId) {
  1110 + public void sentLogsToThingsboard(String msg, Registration registration) {
1142 1111 if (msg != null) {
1143 1112 JsonObject telemetries = new JsonObject();
1144 1113 telemetries.addProperty(LOG_LW2M_TELEMETRY, msg);
1145   - this.updateParametersOnThingsboard(telemetries, LwM2MTransportHandler.DEVICE_TELEMETRY_TOPIC, registrationId);
  1114 + this.updateParametersOnThingsboard(telemetries, LwM2MTransportHandler.DEVICE_TELEMETRY_TOPIC, registration);
1146 1115 }
1147 1116 }
1148 1117
1149   - private String getResourceName(String path) {
1150   - LwM2mPath resultIds = new LwM2mPath(path);
1151   - return (context.getCtxServer().getObjectModels().get(resultIds.getObjectId()) != null) ?
1152   - context.getCtxServer().getObjectModels().get(resultIds.getObjectId()).resources.get(resultIds.getResourceId()).name : "";
1153   - }
1154   -
1155 1118 /**
1156 1119 * @param path - path resource
1157 1120 * @return - value of Resource or null
... ... @@ -1160,6 +1123,6 @@ public class LwM2MTransportService {
1160 1123 LwM2mPath pathIds = new LwM2mPath(path);
1161 1124 ResourceValue resourceValue = this.getResourceValue(lwM2MClient, pathIds);
1162 1125 return (resourceValue == null) ? null :
1163   - (String) this.converter.convertValue(resourceValue.getResourceValue(), this.context.getCtxServer().getResourceModelType(pathIds), ResourceModel.Type.STRING, pathIds);
  1126 + (String) this.converter.convertValue(resourceValue.getResourceValue(), this.context.getCtxServer().getResourceModelType(lwM2MClient.getRegistration(), pathIds), ResourceModel.Type.STRING, pathIds);
1164 1127 }
1165 1128 }
... ...
... ... @@ -129,8 +129,12 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore {
129 129 return (modelClients != null) ? modelClients.getValue() : null;
130 130 }
131 131
132   - public LwM2MClient getLwM2MClient(String registrationId) {
133   - return this.sessions.get(registrationId);
  132 + public LwM2MClient getLwM2MClientWithReg(Registration registration, String registrationId) {
  133 + return registrationId != null ?
  134 + this.sessions.get(registrationId) :
  135 + this.sessions.containsKey(registration.getId()) ?
  136 + this.sessions.get(registration.getId()) :
  137 + this.sessions.get(registration.getEndpoint());
134 138 }
135 139
136 140 public LwM2MClient getLwM2MClient(TransportProtos.SessionInfoProto sessionInfo) {
... ...
... ... @@ -114,9 +114,12 @@
114 114 <dependency>
115 115 <groupId>org.eclipse.leshan</groupId>
116 116 <artifactId>leshan-core</artifactId>
117   - <version>1.0.1</version>
118 117 <scope>compile</scope>
119 118 </dependency>
  119 + <dependency>
  120 + <groupId>org.eclipse.leshan</groupId>
  121 + <artifactId>leshan-server-cf</artifactId>
  122 + </dependency>
120 123 </dependencies>
121 124
122 125 <build>
... ...
... ... @@ -22,21 +22,25 @@ import org.eclipse.leshan.core.model.ObjectLoader;
22 22 import org.eclipse.leshan.core.model.ObjectModel;
23 23 import org.eclipse.leshan.core.model.ResourceModel;
24 24 import org.eclipse.leshan.core.node.LwM2mPath;
  25 +import org.eclipse.leshan.server.registration.Registration;
25 26 import org.springframework.beans.factory.annotation.Value;
26 27 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
27 28 import org.springframework.stereotype.Component;
28 29
29 30 import javax.annotation.PostConstruct;
30   -import java.io.*;
  31 +import java.io.File;
  32 +import java.io.FileInputStream;
  33 +import java.io.IOException;
  34 +import java.io.InputStream;
31 35 import java.nio.file.Path;
32 36 import java.nio.file.Paths;
33 37 import java.security.KeyStore;
34 38 import java.security.KeyStoreException;
35 39 import java.security.NoSuchAlgorithmException;
36 40 import java.security.cert.CertificateException;
  41 +import java.util.Arrays;
37 42 import java.util.List;
38   -import java.util.Map;
39   -import java.util.concurrent.ConcurrentHashMap;
  43 +import java.util.stream.Collectors;
40 44
41 45 @Slf4j
42 46 @Component
... ... @@ -177,10 +181,6 @@ public class LwM2MTransportConfigServer {
177 181 @Value("${transport.lwm2m.secure.redis_url:}")
178 182 private String redisUrl;
179 183
180   - @Getter
181   - @Setter
182   - private Map<Integer, ObjectModel> objectModels;
183   -
184 184 @PostConstruct
185 185 public void init() {
186 186 modelsValue = ObjectLoader.loadDefault();
... ... @@ -196,7 +196,6 @@ public class LwM2MTransportConfigServer {
196 196 log.error(" [{}] Read Models", path.getAbsoluteFile());
197 197 }
198 198 getInKeyStore();
199   - this.objectModels = new ConcurrentHashMap<Integer, ObjectModel>();
200 199 }
201 200
202 201 private File getPathModels() {
... ... @@ -248,21 +247,25 @@ public class LwM2MTransportConfigServer {
248 247 return FULL_FILE_PATH.toUri().getPath();
249 248 }
250 249
251   - public ResourceModel getResourceModel(LwM2mPath pathIds) {
252   - return (this.objectModels.size()>0 &&
253   - this.objectModels.containsKey(pathIds.getObjectId()) &&
254   - this.objectModels.get(pathIds.getObjectId()).resources.containsKey(pathIds.getResourceId())) ?
255   - this.objectModels.get(pathIds.getObjectId()).resources.get(pathIds.getResourceId()) : null;
  250 + public ResourceModel getResourceModel(Registration registration, LwM2mPath pathIds) {
  251 + String pathLink = "/" + pathIds.getObjectId() + "/" + pathIds.getObjectInstanceId();
  252 + return (Arrays.stream(registration.getObjectLinks()).filter(p-> p.getUrl().equals(pathLink)).findFirst().isPresent() &&
  253 + this.modelsValue.stream().filter(v -> v.id == pathIds.getObjectId()).collect(Collectors.toList()).size() > 0) &&
  254 + this.modelsValue.stream().filter(v -> v.id == pathIds.getObjectId()).collect(Collectors.toList()).get(0).resources.containsKey(pathIds.getResourceId()) ?
  255 + this.modelsValue.stream().filter(v -> v.id == pathIds.getObjectId()).collect(Collectors.toList()).get(0).resources.get(pathIds.getResourceId()) :
  256 + null;
256 257 }
257 258
258   - public ResourceModel.Type getResourceModelType(LwM2mPath pathIds) {
259   - ResourceModel resource = this.getResourceModel(pathIds);
  259 + public ResourceModel.Type getResourceModelType(Registration registration, LwM2mPath pathIds) {
  260 + ResourceModel resource = this.getResourceModel(registration, pathIds);
260 261 return (resource == null) ? null : resource.type;
261 262 }
262 263
263   - public ResourceModel.Operations getOperation(LwM2mPath pathIds) {
264   - ResourceModel resource = this.getResourceModel(pathIds);
  264 + public ResourceModel.Operations getOperation(Registration registration, LwM2mPath pathIds) {
  265 + ResourceModel resource = this.getResourceModel(registration, pathIds);
265 266 return (resource == null) ? ResourceModel.Operations.NONE : resource.operations;
266 267 }
  268 +
  269 +
267 270
268 271 }
... ...
... ... @@ -64,6 +64,7 @@
64 64 <json-schema-validator.version>2.2.6</json-schema-validator.version>
65 65 <californium.version>2.2.3</californium.version>
66 66 <leshan-server.version>1.0.1</leshan-server.version>
  67 + <leshan-core.version>1.0.1</leshan-core.version>
67 68 <leshan-client.version>1.0.0</leshan-client.version>
68 69 <gson.version>2.6.2</gson.version>
69 70 <freemarker.version>2.3.30</freemarker.version>
... ... @@ -1161,11 +1162,35 @@
1161 1162 <version>${leshan-server.version}</version>
1162 1163 </dependency>
1163 1164 <dependency>
  1165 + <groupId>org.eclipse.leshan</groupId>
  1166 + <artifactId>leshan-core</artifactId>
  1167 + <version>${leshan-core.version}</version>
  1168 + </dependency>
  1169 + <dependency>
1164 1170 <groupId>org.eclipse.californium</groupId>
1165 1171 <artifactId>californium-core</artifactId>
1166 1172 <version>${californium.version}</version>
1167 1173 </dependency>
1168 1174 <dependency>
  1175 + <groupId>org.eclipse.californium</groupId>
  1176 + <artifactId>californium-core</artifactId>
  1177 + <version>${californium.version}</version>
  1178 + <type>test-jar</type>
  1179 + <scope>test</scope>
  1180 + </dependency>
  1181 + <dependency>
  1182 + <groupId>org.eclipse.californium</groupId>
  1183 + <artifactId>californium-core</artifactId>
  1184 + <version>${californium.version}</version>
  1185 + </dependency>
  1186 + <dependency>
  1187 + <groupId>org.eclipse.californium</groupId>
  1188 + <artifactId>element-connector</artifactId>
  1189 + <version>${californium.version}</version>
  1190 + <type>test-jar</type>
  1191 + <scope>test</scope>
  1192 + </dependency>
  1193 + <dependency>
1169 1194 <groupId>com.google.code.gson</groupId>
1170 1195 <artifactId>gson</artifactId>
1171 1196 <version>${gson.version}</version>
... ...
... ... @@ -78,14 +78,10 @@
78 78 <dependency>
79 79 <groupId>org.eclipse.californium</groupId>
80 80 <artifactId>californium-core</artifactId>
81   - <version>${californium.version}</version>
82   - <type>test-jar</type>
83   - <scope>test</scope>
84 81 </dependency>
85 82 <dependency>
86 83 <groupId>org.eclipse.californium</groupId>
87 84 <artifactId>element-connector</artifactId>
88   - <version>${californium.version}</version>
89 85 <type>test-jar</type>
90 86 <scope>test</scope>
91 87 </dependency>
... ...