Commit 447bf05b7b9bd3e3d37bc53d42780b613d3a4ea9
1 parent
65f4ca58
Lwm2m: back: add tenantId and repositoryTenant - test2
Showing
15 changed files
with
113 additions
and
19 deletions
... | ... | @@ -70,6 +70,18 @@ public class ResourceController extends BaseController { |
70 | 70 | } |
71 | 71 | |
72 | 72 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
73 | + @RequestMapping(value = "/resource/{resourceType}", method = RequestMethod.GET) | |
74 | + @ResponseBody | |
75 | + public List<Resource> getResources(@RequestParam(required = false) boolean system, | |
76 | + @PathVariable("resourceType") ResourceType resourceType) throws ThingsboardException { | |
77 | + try { | |
78 | + return checkNotNull(resourceService.findResourcesByTenantIdResourceType(system ? TenantId.SYS_TENANT_ID : getTenantId(), resourceType)); | |
79 | + } catch (Exception e) { | |
80 | + throw handleException(e); | |
81 | + } | |
82 | + } | |
83 | + | |
84 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | |
73 | 85 | @RequestMapping(value = "/resource/{resourceType}/{resourceId}", method = RequestMethod.DELETE) |
74 | 86 | @ResponseBody |
75 | 87 | public void deleteResource(@PathVariable("resourceType") ResourceType resourceType, | ... | ... |
... | ... | @@ -373,9 +373,14 @@ public class DefaultTransportApiService implements TransportApiService { |
373 | 373 | |
374 | 374 | List<TransportProtos.ResourceMsg> resources; |
375 | 375 | |
376 | - if (resourceType != null && resourceId != null) { | |
376 | + if (resourceType != null && resourceId != null && !resourceId.isEmpty()) { | |
377 | 377 | resources = Collections.singletonList(toProto( |
378 | 378 | resourceService.getResource(tenantId, ResourceType.valueOf(resourceType), resourceId))); |
379 | + } else if (resourceType != null && !resourceType.isEmpty()) { | |
380 | + resources = resourceService.findResourcesByTenantIdResourceType(tenantId, ResourceType.valueOf(resourceType)) | |
381 | + .stream() | |
382 | + .map(this::toProto) | |
383 | + .collect(Collectors.toList()); | |
379 | 384 | } else { |
380 | 385 | resources = resourceService.findResourcesByTenantId(tenantId) |
381 | 386 | .stream() | ... | ... |
... | ... | @@ -29,6 +29,8 @@ public interface ResourceService { |
29 | 29 | |
30 | 30 | List<Resource> findResourcesByTenantId(TenantId tenantId); |
31 | 31 | |
32 | + List<Resource> findResourcesByTenantIdResourceType(TenantId tenantId, ResourceType resourceType); | |
33 | + | |
32 | 34 | void deleteResource(TenantId tenantId, ResourceType resourceType, String resourceId); |
33 | 35 | |
34 | 36 | void deleteResourcesByTenantId(TenantId tenantId); | ... | ... |
... | ... | @@ -34,16 +34,25 @@ import com.google.gson.JsonElement; |
34 | 34 | import com.google.gson.JsonObject; |
35 | 35 | import lombok.Getter; |
36 | 36 | import lombok.extern.slf4j.Slf4j; |
37 | +import org.eclipse.leshan.core.model.ObjectModel; | |
37 | 38 | import org.springframework.stereotype.Component; |
38 | 39 | import org.thingsboard.server.common.transport.TransportContext; |
39 | 40 | import org.thingsboard.server.common.transport.TransportService; |
40 | 41 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
41 | 42 | import org.thingsboard.server.common.transport.adaptor.AdaptorException; |
42 | 43 | import org.thingsboard.server.common.transport.lwm2m.LwM2MTransportConfigServer; |
43 | -import org.thingsboard.server.gen.transport.TransportProtos; | |
44 | +import org.thingsboard.server.gen.transport.TransportProtos.GetResourcesRequestMsg; | |
45 | +import org.thingsboard.server.gen.transport.TransportProtos.GetResourcesResponseMsg; | |
46 | +import org.thingsboard.server.gen.transport.TransportProtos.PostAttributeMsg; | |
47 | +import org.thingsboard.server.gen.transport.TransportProtos.PostTelemetryMsg; | |
48 | +import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto; | |
49 | +import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg; | |
44 | 50 | import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
45 | 51 | import org.thingsboard.server.transport.lwm2m.server.adaptors.LwM2MJsonAdaptor; |
46 | 52 | |
53 | +import java.util.List; | |
54 | +import java.util.UUID; | |
55 | + | |
47 | 56 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandler.LOG_LW2M_TELEMETRY; |
48 | 57 | |
49 | 58 | @Slf4j |
... | ... | @@ -56,6 +65,8 @@ public class LwM2mTransportContextServer extends TransportContext { |
56 | 65 | |
57 | 66 | private final TransportService transportService; |
58 | 67 | |
68 | + private List<ObjectModel> modelsValueServer; | |
69 | + | |
59 | 70 | @Getter |
60 | 71 | private final LwM2MJsonAdaptor adaptor; |
61 | 72 | |
... | ... | @@ -89,14 +100,14 @@ public class LwM2mTransportContextServer extends TransportContext { |
89 | 100 | }; |
90 | 101 | } |
91 | 102 | |
92 | - public void sentParametersOnThingsboard(JsonElement msg, String topicName, TransportProtos.SessionInfoProto sessionInfo) { | |
103 | + public void sentParametersOnThingsboard(JsonElement msg, String topicName, SessionInfoProto sessionInfo) { | |
93 | 104 | try { |
94 | 105 | if (topicName.equals(LwM2mTransportHandler.DEVICE_ATTRIBUTES_TOPIC)) { |
95 | - TransportProtos.PostAttributeMsg postAttributeMsg = adaptor.convertToPostAttributes(msg); | |
106 | + PostAttributeMsg postAttributeMsg = adaptor.convertToPostAttributes(msg); | |
96 | 107 | TransportServiceCallback call = this.getPubAckCallbackSentAttrTelemetry(postAttributeMsg); |
97 | 108 | transportService.process(sessionInfo, postAttributeMsg, this.getPubAckCallbackSentAttrTelemetry(call)); |
98 | 109 | } else if (topicName.equals(LwM2mTransportHandler.DEVICE_TELEMETRY_TOPIC)) { |
99 | - TransportProtos.PostTelemetryMsg postTelemetryMsg = adaptor.convertToPostTelemetry(msg); | |
110 | + PostTelemetryMsg postTelemetryMsg = adaptor.convertToPostTelemetry(msg); | |
100 | 111 | TransportServiceCallback call = this.getPubAckCallbackSentAttrTelemetry(postTelemetryMsg); |
101 | 112 | transportService.process(sessionInfo, postTelemetryMsg, this.getPubAckCallbackSentAttrTelemetry(call)); |
102 | 113 | } |
... | ... | @@ -115,8 +126,8 @@ public class LwM2mTransportContextServer extends TransportContext { |
115 | 126 | /** |
116 | 127 | * @return - sessionInfo after access connect client |
117 | 128 | */ |
118 | - public TransportProtos.SessionInfoProto getValidateSessionInfo(TransportProtos.ValidateDeviceCredentialsResponseMsg msg, long mostSignificantBits, long leastSignificantBits) { | |
119 | - return TransportProtos.SessionInfoProto.newBuilder() | |
129 | + public SessionInfoProto getValidateSessionInfo(ValidateDeviceCredentialsResponseMsg msg, long mostSignificantBits, long leastSignificantBits) { | |
130 | + return SessionInfoProto.newBuilder() | |
120 | 131 | .setNodeId(this.getNodeId()) |
121 | 132 | .setSessionIdMSB(mostSignificantBits) |
122 | 133 | .setSessionIdLSB(leastSignificantBits) |
... | ... | @@ -131,4 +142,25 @@ public class LwM2mTransportContextServer extends TransportContext { |
131 | 142 | .build(); |
132 | 143 | } |
133 | 144 | |
145 | + | |
146 | + | |
147 | + | |
148 | + /** | |
149 | + * ResourcesRequestMsg | |
150 | + * | |
151 | + * @param resourceType | |
152 | + * @return | |
153 | + */ | |
154 | + public GetResourcesResponseMsg getResourceTenant (UUID tenantId, String resourceType) { | |
155 | + | |
156 | + GetResourcesResponseMsg responseMsg = | |
157 | + this.getTransportService() | |
158 | + .getResources(GetResourcesRequestMsg.newBuilder() | |
159 | + .setResourceType(resourceType) | |
160 | + .setTenantIdLSB(tenantId.getLeastSignificantBits()) | |
161 | + .setTenantIdMSB(tenantId.getMostSignificantBits()) | |
162 | + .build()); | |
163 | + return responseMsg; | |
164 | + } | |
165 | + | |
134 | 166 | } | ... | ... |
... | ... | @@ -45,6 +45,7 @@ import java.util.Arrays; |
45 | 45 | import java.util.Date; |
46 | 46 | import java.util.LinkedList; |
47 | 47 | import java.util.Optional; |
48 | +import java.util.UUID; | |
48 | 49 | |
49 | 50 | @Slf4j |
50 | 51 | //@Component("LwM2MTransportHandler") |
... | ... | @@ -188,7 +189,7 @@ public class LwM2mTransportHandler { |
188 | 189 | return null; |
189 | 190 | } |
190 | 191 | |
191 | - public static LwM2mClientProfile getNewProfileParameters(JsonObject profilesConfigData, String tenantId) { | |
192 | + public static LwM2mClientProfile getNewProfileParameters(JsonObject profilesConfigData, UUID tenantId) { | |
192 | 193 | LwM2mClientProfile lwM2MClientProfile = new LwM2mClientProfile(); |
193 | 194 | lwM2MClientProfile.setTenantId(tenantId); |
194 | 195 | lwM2MClientProfile.setPostClientLwM2mSettings(profilesConfigData.get(CLIENT_LWM2M_SETTINGS).getAsJsonObject()); |
... | ... | @@ -222,7 +223,7 @@ public class LwM2mTransportHandler { |
222 | 223 | ObjectMapper mapper = new ObjectMapper(); |
223 | 224 | String profileStr = mapper.writeValueAsString(profile); |
224 | 225 | JsonObject profileJson = (profileStr != null) ? validateJson(profileStr) : null; |
225 | - return (getValidateCredentialsBodyFromThingsboard(profileJson)) ? LwM2mTransportHandler.getNewProfileParameters(profileJson, deviceProfile.getTenantId().getId().toString()) : null; | |
226 | + return (getValidateCredentialsBodyFromThingsboard(profileJson)) ? LwM2mTransportHandler.getNewProfileParameters(profileJson, deviceProfile.getTenantId().getId()) : null; | |
226 | 227 | } catch (IOException e) { |
227 | 228 | log.error("", e); |
228 | 229 | } | ... | ... |
... | ... | @@ -47,7 +47,6 @@ import org.eclipse.leshan.core.util.Hex; |
47 | 47 | import org.eclipse.leshan.core.util.NamedThreadFactory; |
48 | 48 | import org.eclipse.leshan.server.californium.LeshanServer; |
49 | 49 | import org.eclipse.leshan.server.registration.Registration; |
50 | -import org.springframework.beans.factory.annotation.Autowired; | |
51 | 50 | import org.springframework.stereotype.Service; |
52 | 51 | import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
53 | 52 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; |
... | ... | @@ -88,13 +87,16 @@ public class LwM2mTransportRequest { |
88 | 87 | |
89 | 88 | private final LeshanServer leshanServer; |
90 | 89 | |
91 | - @Autowired | |
92 | - private LwM2mTransportServiceImpl serviceImpl; | |
90 | +// @Autowired | |
91 | +// private LwM2mTransportServiceImpl serviceImpl; | |
93 | 92 | |
94 | - public LwM2mTransportRequest(LwM2mTransportContextServer context, LwM2mClientContext lwM2mClientContext, LeshanServer leshanServer) { | |
93 | + private final LwM2mTransportServiceImpl serviceImpl; | |
94 | + | |
95 | + public LwM2mTransportRequest(LwM2mTransportContextServer context, LwM2mClientContext lwM2mClientContext, LeshanServer leshanServer, LwM2mTransportServiceImpl serviceImpl) { | |
95 | 96 | this.context = context; |
96 | 97 | this.lwM2mClientContext = lwM2mClientContext; |
97 | 98 | this.leshanServer = leshanServer; |
99 | + this.serviceImpl = serviceImpl; | |
98 | 100 | } |
99 | 101 | |
100 | 102 | @PostConstruct | ... | ... |
... | ... | @@ -30,6 +30,9 @@ import org.eclipse.leshan.server.security.SecurityChecker; |
30 | 30 | import org.springframework.beans.factory.annotation.Autowired; |
31 | 31 | import org.springframework.context.annotation.Bean; |
32 | 32 | import org.springframework.stereotype.Component; |
33 | +import org.thingsboard.server.common.data.id.TenantId; | |
34 | +import org.thingsboard.server.common.data.transport.resource.ResourceType; | |
35 | +import org.thingsboard.server.gen.transport.TransportProtos; | |
33 | 36 | import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
34 | 37 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; |
35 | 38 | import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl; |
... | ... | @@ -98,6 +101,7 @@ public class LwM2mTransportServerConfiguration { |
98 | 101 | builder.setCoapConfig(getCoapConfig(serverPortNoSec, serverSecurePort)); |
99 | 102 | |
100 | 103 | /** Define model provider (Create Models )*/ |
104 | + TransportProtos.GetResourcesResponseMsg responseMsg= this.context.getResourceTenant(TenantId.SYS_TENANT_ID.getId(), ResourceType.LWM2M_MODEL.name()); | |
101 | 105 | // LwM2mModelProvider modelProvider = new VersionedModelProvider(this.context.getLwM2MTransportConfigServer().getModelsValueCommon()); |
102 | 106 | LwM2mModelProvider modelProvider = new LwM2mVersionedModelProvider(this.context.getLwM2MTransportConfigServer().getModelsValueServer(), this.lwM2mClientContext); |
103 | 107 | builder.setObjectModelProvider(modelProvider); | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
1 | 16 | package org.thingsboard.server.transport.lwm2m.server; |
2 | 17 | |
3 | 18 | import org.eclipse.leshan.core.model.LwM2mModel; |
... | ... | @@ -11,6 +26,7 @@ import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; |
11 | 26 | import java.util.ArrayList; |
12 | 27 | import java.util.Collection; |
13 | 28 | import java.util.Map; |
29 | +import java.util.UUID; | |
14 | 30 | import java.util.concurrent.ConcurrentHashMap; |
15 | 31 | |
16 | 32 | public class LwM2mVersionedModelProvider implements LwM2mModelProvider { |
... | ... | @@ -57,9 +73,9 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider { |
57 | 73 | private class DynamicModel implements LwM2mModel { |
58 | 74 | |
59 | 75 | private final Registration registration; |
60 | - private final String tenantId; | |
76 | + private final UUID tenantId; | |
61 | 77 | |
62 | - public DynamicModel(Registration registration, String tenantId) { | |
78 | + public DynamicModel(Registration registration, UUID tenantId) { | |
63 | 79 | this.registration = registration; |
64 | 80 | this.tenantId = tenantId; |
65 | 81 | } | ... | ... |
... | ... | @@ -15,7 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.transport.lwm2m.server.client; |
17 | 17 | |
18 | -import lombok.extern.slf4j.Slf4j; | |
19 | 18 | import org.eclipse.leshan.server.registration.Registration; |
20 | 19 | import org.eclipse.leshan.server.security.EditableSecurityStore; |
21 | 20 | import org.springframework.stereotype.Service; |
... | ... | @@ -105,6 +104,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { |
105 | 104 | |
106 | 105 | /** |
107 | 106 | * Add new LwM2MClient to session |
107 | + * | |
108 | 108 | * @param identity- |
109 | 109 | * @return SecurityInfo. If error - SecurityInfoError |
110 | 110 | * and log: |
... | ... | @@ -168,4 +168,5 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { |
168 | 168 | } |
169 | 169 | return false; |
170 | 170 | } |
171 | + | |
171 | 172 | } | ... | ... |
... | ... | @@ -20,10 +20,12 @@ import com.google.gson.JsonArray; |
20 | 20 | import com.google.gson.JsonObject; |
21 | 21 | import lombok.Data; |
22 | 22 | |
23 | +import java.util.UUID; | |
24 | + | |
23 | 25 | @Data |
24 | 26 | public class LwM2mClientProfile { |
25 | 27 | |
26 | - private String tenantId; | |
28 | + private UUID tenantId; | |
27 | 29 | /** |
28 | 30 | * {"clientLwM2mSettings": { |
29 | 31 | * clientUpdateValueAfterConnect: false; | ... | ... |
... | ... | @@ -47,7 +47,6 @@ import java.util.stream.Collectors; |
47 | 47 | @ConditionalOnExpression("('${service.type:null}'=='tb-transport' && '${transport.lwm2m.enabled:false}'=='true') || '${service.type:null}'=='monolith' || '${service.type:null}'=='tb-core'") |
48 | 48 | public class LwM2MTransportConfigServer { |
49 | 49 | |
50 | - | |
51 | 50 | @Getter |
52 | 51 | private String MODEL_PATH_DEFAULT = "models"; |
53 | 52 | |
... | ... | @@ -190,6 +189,8 @@ public class LwM2MTransportConfigServer { |
190 | 189 | @Value("${transport.lwm2m.server.secure.alias:}") |
191 | 190 | private String serverAlias; |
192 | 191 | |
192 | + | |
193 | + | |
193 | 194 | @PostConstruct |
194 | 195 | public void init() { |
195 | 196 | modelsValueServer = ObjectLoader.loadDefault(); |
... | ... | @@ -276,4 +277,5 @@ public class LwM2MTransportConfigServer { |
276 | 277 | ResourceModel resource = this.getResourceModel(registration, pathIds); |
277 | 278 | return (resource == null) ? ResourceModel.Operations.NONE : resource.operations; |
278 | 279 | } |
280 | + | |
279 | 281 | } | ... | ... |
... | ... | @@ -66,6 +66,13 @@ public class BaseResourceService implements ResourceService { |
66 | 66 | } |
67 | 67 | |
68 | 68 | @Override |
69 | + public List<Resource> findResourcesByTenantIdResourceType(TenantId tenantId, ResourceType resourceType) { | |
70 | + log.trace("Executing findByTenantId [{}]", tenantId); | |
71 | + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
72 | + return resourceDao.findAllByTenantIdResourceType(tenantId, resourceType); | |
73 | + } | |
74 | + | |
75 | + @Override | |
69 | 76 | public void deleteResourcesByTenantId(TenantId tenantId) { |
70 | 77 | log.trace("Executing deleteDevicesByTenantId, tenantId [{}]", tenantId); |
71 | 78 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | ... | ... |
... | ... | @@ -71,6 +71,11 @@ public class ResourceDaoImpl implements ResourceDao { |
71 | 71 | } |
72 | 72 | |
73 | 73 | @Override |
74 | + public List<Resource> findAllByTenantIdResourceType(TenantId tenantId, ResourceType resourceType) { | |
75 | + return DaoUtil.convertDataList(resourceRepository.findAllByTenantIdAndResourceType(tenantId.getId(), resourceType.name())); | |
76 | + } | |
77 | + | |
78 | + @Override | |
74 | 79 | public void removeAllByTenantId(TenantId tenantId) { |
75 | 80 | resourceRepository.removeAllByTenantId(tenantId.getId()); |
76 | 81 | } | ... | ... |
... | ... | @@ -16,7 +16,6 @@ |
16 | 16 | package org.thingsboard.server.dao.sql.resource; |
17 | 17 | |
18 | 18 | import org.springframework.data.repository.CrudRepository; |
19 | -import org.thingsboard.server.common.data.id.TenantId; | |
20 | 19 | import org.thingsboard.server.dao.model.sql.ResourceCompositeKey; |
21 | 20 | import org.thingsboard.server.dao.model.sql.ResourceEntity; |
22 | 21 | |
... | ... | @@ -27,5 +26,7 @@ public interface ResourceRepository extends CrudRepository<ResourceEntity, Resou |
27 | 26 | |
28 | 27 | List<ResourceEntity> findAllByTenantId(UUID tenantId); |
29 | 28 | |
29 | + List<ResourceEntity> findAllByTenantIdAndResourceType(UUID tenantId, String resourceType); | |
30 | + | |
30 | 31 | void removeAllByTenantId(UUID tenantId); |
31 | 32 | } | ... | ... |