64 changed files with 1285 additions and 112 deletions
@ -0,0 +1,77 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.rpc; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.RpcId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.rpc.Rpc; |
|||
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.dao.rpc.RpcService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.queue.TbClusterService; |
|||
|
|||
@TbCoreComponent |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
@Slf4j |
|||
public class TbRpcService { |
|||
private final RpcService rpcService; |
|||
private final TbClusterService tbClusterService; |
|||
|
|||
public Rpc save(TenantId tenantId, Rpc rpc) { |
|||
Rpc saved = rpcService.save(rpc); |
|||
pushRpcMsgToRuleEngine(tenantId, saved); |
|||
return saved; |
|||
} |
|||
|
|||
public void save(TenantId tenantId, RpcId rpcId, RpcStatus newStatus, JsonNode response) { |
|||
Rpc foundRpc = rpcService.findById(tenantId, rpcId); |
|||
if (foundRpc != null) { |
|||
foundRpc.setStatus(newStatus); |
|||
if (response != null) { |
|||
foundRpc.setResponse(response); |
|||
} |
|||
Rpc saved = rpcService.save(foundRpc); |
|||
pushRpcMsgToRuleEngine(tenantId, saved); |
|||
} else { |
|||
log.warn("[{}] Failed to update RPC status because RPC was already deleted", rpcId); |
|||
} |
|||
} |
|||
|
|||
private void pushRpcMsgToRuleEngine(TenantId tenantId, Rpc rpc) { |
|||
TbMsg msg = TbMsg.newMsg("RPC_" + rpc.getStatus().name(), rpc.getDeviceId(), TbMsgMetaData.EMPTY, JacksonUtil.toString(rpc)); |
|||
tbClusterService.pushMsgToRuleEngine(tenantId, rpc.getId(), msg, null); |
|||
} |
|||
|
|||
public Rpc findRpcById(TenantId tenantId, RpcId rpcId) { |
|||
return rpcService.findById(tenantId, rpcId); |
|||
} |
|||
|
|||
public PageData<Rpc> findAllByDeviceIdAndStatus(TenantId tenantId, DeviceId deviceId, RpcStatus rpcStatus, PageLink pageLink) { |
|||
return rpcService.findAllByDeviceIdAndStatus(tenantId, deviceId, rpcStatus, pageLink); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,83 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.ttl.rpc; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.scheduling.annotation.Scheduled; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
|||
import org.thingsboard.server.common.msg.queue.ServiceType; |
|||
import org.thingsboard.server.dao.rpc.RpcDao; |
|||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
|||
import org.thingsboard.server.dao.tenant.TenantDao; |
|||
import org.thingsboard.server.queue.discovery.PartitionService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
import java.util.Date; |
|||
import java.util.Optional; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@TbCoreComponent |
|||
@Service |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
public class RpcCleanUpService { |
|||
@Value("${sql.ttl.rpc.enabled}") |
|||
private boolean ttlTaskExecutionEnabled; |
|||
|
|||
private final TenantDao tenantDao; |
|||
private final PartitionService partitionService; |
|||
private final TbTenantProfileCache tenantProfileCache; |
|||
private final RpcDao rpcDao; |
|||
|
|||
@Scheduled(initialDelayString = "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.rpc.checking_interval})}", fixedDelayString = "${sql.ttl.rpc.checking_interval}") |
|||
public void cleanUp() { |
|||
if (ttlTaskExecutionEnabled) { |
|||
PageLink tenantsBatchRequest = new PageLink(10_000, 0); |
|||
PageData<TenantId> tenantsIds; |
|||
do { |
|||
tenantsIds = tenantDao.findTenantsIds(tenantsBatchRequest); |
|||
for (TenantId tenantId : tenantsIds.getData()) { |
|||
if (!partitionService.resolve(ServiceType.TB_CORE, tenantId, tenantId).isMyPartition()) { |
|||
continue; |
|||
} |
|||
|
|||
Optional<DefaultTenantProfileConfiguration> tenantProfileConfiguration = tenantProfileCache.get(tenantId).getProfileConfiguration(); |
|||
if (tenantProfileConfiguration.isEmpty() || tenantProfileConfiguration.get().getRpcTtlDays() == 0) { |
|||
continue; |
|||
} |
|||
|
|||
long ttl = TimeUnit.DAYS.toMillis(tenantProfileConfiguration.get().getRpcTtlDays()); |
|||
long expirationTime = System.currentTimeMillis() - ttl; |
|||
|
|||
long totalRemoved = rpcDao.deleteOutdatedRpcByTenantId(tenantId, expirationTime); |
|||
|
|||
if (totalRemoved > 0) { |
|||
log.info("Removed {} outdated rpc(s) for tenant {} older than {}", totalRemoved, tenantId, new Date(expirationTime)); |
|||
} |
|||
} |
|||
|
|||
tenantsBatchRequest = tenantsBatchRequest.nextPageLink(); |
|||
} while (tenantsIds.hasNext()); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.rpc; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.RpcId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.rpc.Rpc; |
|||
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|||
|
|||
public interface RpcService { |
|||
Rpc save(Rpc rpc); |
|||
|
|||
void deleteRpc(TenantId tenantId, RpcId id); |
|||
|
|||
void deleteAllRpcByTenantId(TenantId tenantId); |
|||
|
|||
Rpc findById(TenantId tenantId, RpcId id); |
|||
|
|||
ListenableFuture<Rpc> findRpcByIdAsync(TenantId tenantId, RpcId id); |
|||
|
|||
PageData<Rpc> findAllByDeviceIdAndStatus(TenantId tenantId, DeviceId deviceId, RpcStatus rpcStatus, PageLink pageLink); |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.id; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonCreator; |
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import com.fasterxml.jackson.annotation.JsonProperty; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public final class RpcId extends UUIDBased implements EntityId { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
@JsonCreator |
|||
public RpcId(@JsonProperty("id") UUID id) { |
|||
super(id); |
|||
} |
|||
|
|||
@JsonIgnore |
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.RPC; |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.rpc; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.thingsboard.server.common.data.BaseData; |
|||
import org.thingsboard.server.common.data.HasTenantId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.RpcId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
public class Rpc extends BaseData<RpcId> implements HasTenantId { |
|||
private TenantId tenantId; |
|||
private DeviceId deviceId; |
|||
private long expirationTime; |
|||
private JsonNode request; |
|||
private JsonNode response; |
|||
private RpcStatus status; |
|||
|
|||
public Rpc() { |
|||
super(); |
|||
} |
|||
|
|||
public Rpc(RpcId id) { |
|||
super(id); |
|||
} |
|||
|
|||
public Rpc(Rpc rpc) { |
|||
super(rpc); |
|||
this.tenantId = rpc.getTenantId(); |
|||
this.deviceId = rpc.getDeviceId(); |
|||
this.expirationTime = rpc.getExpirationTime(); |
|||
this.request = rpc.getRequest(); |
|||
this.response = rpc.getResponse(); |
|||
this.status = rpc.getStatus(); |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.rpc; |
|||
|
|||
public enum RpcStatus { |
|||
QUEUED, DELIVERED, SUCCESSFUL, TIMEOUT, FAILED |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.server.store; |
|||
|
|||
import org.eclipse.leshan.server.security.NonUniqueSecurityInfoException; |
|||
import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo; |
|||
|
|||
public interface TbMainSecurityStore extends TbSecurityStore { |
|||
|
|||
void putX509(TbLwM2MSecurityInfo tbSecurityInfo) throws NonUniqueSecurityInfoException; |
|||
|
|||
void registerX509(String endpoint, String registrationId); |
|||
|
|||
void remove(String endpoint, String registrationId); |
|||
|
|||
} |
|||
@ -0,0 +1,102 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.model.sql; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.hibernate.annotations.Type; |
|||
import org.hibernate.annotations.TypeDef; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.RpcId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.rpc.Rpc; |
|||
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|||
import org.thingsboard.server.dao.model.BaseEntity; |
|||
import org.thingsboard.server.dao.model.BaseSqlEntity; |
|||
import org.thingsboard.server.dao.util.mapping.JsonStringType; |
|||
|
|||
import javax.persistence.Column; |
|||
import javax.persistence.Entity; |
|||
import javax.persistence.EnumType; |
|||
import javax.persistence.Enumerated; |
|||
import javax.persistence.Table; |
|||
import java.util.UUID; |
|||
|
|||
import static org.thingsboard.server.dao.model.ModelConstants.RPC_DEVICE_ID; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.RPC_EXPIRATION_TIME; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.RPC_REQUEST; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.RPC_RESPONSE; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.RPC_STATUS; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.RPC_TABLE_NAME; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.RPC_TENANT_ID_COLUMN; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
@Entity |
|||
@TypeDef(name = "json", typeClass = JsonStringType.class) |
|||
@Table(name = RPC_TABLE_NAME) |
|||
public class RpcEntity extends BaseSqlEntity<Rpc> implements BaseEntity<Rpc> { |
|||
|
|||
@Column(name = RPC_TENANT_ID_COLUMN) |
|||
private UUID tenantId; |
|||
|
|||
@Column(name = RPC_DEVICE_ID) |
|||
private UUID deviceId; |
|||
|
|||
@Column(name = RPC_EXPIRATION_TIME) |
|||
private long expirationTime; |
|||
|
|||
@Type(type = "json") |
|||
@Column(name = RPC_REQUEST) |
|||
private JsonNode request; |
|||
|
|||
@Type(type = "json") |
|||
@Column(name = RPC_RESPONSE) |
|||
private JsonNode response; |
|||
|
|||
@Enumerated(EnumType.STRING) |
|||
@Column(name = RPC_STATUS) |
|||
private RpcStatus status; |
|||
|
|||
public RpcEntity() { |
|||
super(); |
|||
} |
|||
|
|||
public RpcEntity(Rpc rpc) { |
|||
this.setUuid(rpc.getUuidId()); |
|||
this.createdTime = rpc.getCreatedTime(); |
|||
this.tenantId = rpc.getTenantId().getId(); |
|||
this.deviceId = rpc.getDeviceId().getId(); |
|||
this.expirationTime = rpc.getExpirationTime(); |
|||
this.request = rpc.getRequest(); |
|||
this.response = rpc.getResponse(); |
|||
this.status = rpc.getStatus(); |
|||
} |
|||
|
|||
@Override |
|||
public Rpc toData() { |
|||
Rpc rpc = new Rpc(new RpcId(id)); |
|||
rpc.setCreatedTime(createdTime); |
|||
rpc.setTenantId(new TenantId(tenantId)); |
|||
rpc.setDeviceId(new DeviceId(deviceId)); |
|||
rpc.setExpirationTime(expirationTime); |
|||
rpc.setRequest(request); |
|||
rpc.setResponse(response); |
|||
rpc.setStatus(status); |
|||
return rpc; |
|||
} |
|||
} |
|||
@ -0,0 +1,100 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.rpc; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.RpcId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.rpc.Rpc; |
|||
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|||
import org.thingsboard.server.dao.service.PaginatedRemover; |
|||
|
|||
import static org.thingsboard.server.dao.service.Validator.validateId; |
|||
import static org.thingsboard.server.dao.service.Validator.validatePageLink; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
public class BaseRpcService implements RpcService { |
|||
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
|||
public static final String INCORRECT_RPC_ID = "Incorrect rpcId "; |
|||
|
|||
private final RpcDao rpcDao; |
|||
|
|||
@Override |
|||
public Rpc save(Rpc rpc) { |
|||
log.trace("Executing save, [{}]", rpc); |
|||
return rpcDao.save(rpc.getTenantId(), rpc); |
|||
} |
|||
|
|||
@Override |
|||
public void deleteRpc(TenantId tenantId, RpcId rpcId) { |
|||
log.trace("Executing deleteRpc, tenantId [{}], rpcId [{}]", tenantId, rpcId); |
|||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
|||
validateId(rpcId, INCORRECT_RPC_ID + rpcId); |
|||
rpcDao.removeById(tenantId, rpcId.getId()); |
|||
} |
|||
|
|||
@Override |
|||
public void deleteAllRpcByTenantId(TenantId tenantId) { |
|||
log.trace("Executing deleteAllRpcByTenantId, tenantId [{}]", tenantId); |
|||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
|||
tenantRpcRemover.removeEntities(tenantId, tenantId); |
|||
} |
|||
|
|||
@Override |
|||
public Rpc findById(TenantId tenantId, RpcId rpcId) { |
|||
log.trace("Executing findById, tenantId [{}], rpcId [{}]", tenantId, rpcId); |
|||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
|||
validateId(rpcId, INCORRECT_RPC_ID + rpcId); |
|||
return rpcDao.findById(tenantId, rpcId.getId()); |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<Rpc> findRpcByIdAsync(TenantId tenantId, RpcId rpcId) { |
|||
log.trace("Executing findRpcByIdAsync, tenantId [{}], rpcId: [{}]", tenantId, rpcId); |
|||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
|||
validateId(rpcId, INCORRECT_RPC_ID + rpcId); |
|||
return rpcDao.findByIdAsync(tenantId, rpcId.getId()); |
|||
} |
|||
|
|||
@Override |
|||
public PageData<Rpc> findAllByDeviceIdAndStatus(TenantId tenantId, DeviceId deviceId, RpcStatus rpcStatus, PageLink pageLink) { |
|||
log.trace("Executing findAllByDeviceIdAndStatus, tenantId [{}], deviceId [{}], rpcStatus [{}], pageLink [{}]", tenantId, deviceId, rpcStatus, pageLink); |
|||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
|||
validatePageLink(pageLink); |
|||
return rpcDao.findAllByDeviceId(tenantId, deviceId, rpcStatus, pageLink); |
|||
} |
|||
|
|||
private PaginatedRemover<TenantId, Rpc> tenantRpcRemover = |
|||
new PaginatedRemover<>() { |
|||
@Override |
|||
protected PageData<Rpc> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) { |
|||
return rpcDao.findAllRpcByTenantId(id, pageLink); |
|||
} |
|||
|
|||
@Override |
|||
protected void removeEntity(TenantId tenantId, Rpc entity) { |
|||
deleteRpc(tenantId, entity.getId()); |
|||
} |
|||
}; |
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.rpc; |
|||
|
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.rpc.Rpc; |
|||
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|||
import org.thingsboard.server.dao.Dao; |
|||
|
|||
public interface RpcDao extends Dao<Rpc> { |
|||
PageData<Rpc> findAllByDeviceId(TenantId tenantId, DeviceId deviceId, RpcStatus rpcStatus, PageLink pageLink); |
|||
|
|||
PageData<Rpc> findAllRpcByTenantId(TenantId tenantId, PageLink pageLink); |
|||
|
|||
Long deleteOutdatedRpcByTenantId(TenantId tenantId, Long expirationTime); |
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.sql.rpc; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.rpc.Rpc; |
|||
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|||
import org.thingsboard.server.dao.DaoUtil; |
|||
import org.thingsboard.server.dao.model.sql.RpcEntity; |
|||
import org.thingsboard.server.dao.rpc.RpcDao; |
|||
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@AllArgsConstructor |
|||
public class JpaRpcDao extends JpaAbstractDao<RpcEntity, Rpc> implements RpcDao { |
|||
|
|||
private final RpcRepository rpcRepository; |
|||
|
|||
@Override |
|||
protected Class<RpcEntity> getEntityClass() { |
|||
return RpcEntity.class; |
|||
} |
|||
|
|||
@Override |
|||
protected CrudRepository<RpcEntity, UUID> getCrudRepository() { |
|||
return rpcRepository; |
|||
} |
|||
|
|||
@Override |
|||
public PageData<Rpc> findAllByDeviceId(TenantId tenantId, DeviceId deviceId, RpcStatus rpcStatus, PageLink pageLink) { |
|||
return DaoUtil.toPageData(rpcRepository.findAllByTenantIdAndDeviceIdAndStatus(tenantId.getId(), deviceId.getId(), rpcStatus, DaoUtil.toPageable(pageLink))); |
|||
} |
|||
|
|||
@Override |
|||
public PageData<Rpc> findAllRpcByTenantId(TenantId tenantId, PageLink pageLink) { |
|||
return DaoUtil.toPageData(rpcRepository.findAllByTenantId(tenantId.getId(), DaoUtil.toPageable(pageLink))); |
|||
} |
|||
|
|||
@Override |
|||
public Long deleteOutdatedRpcByTenantId(TenantId tenantId, Long expirationTime) { |
|||
return rpcRepository.deleteOutdatedRpcByTenantId(tenantId.getId(), expirationTime); |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.sql.rpc; |
|||
|
|||
import org.springframework.data.domain.Page; |
|||
import org.springframework.data.domain.Pageable; |
|||
import org.springframework.data.jpa.repository.Query; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.springframework.data.repository.query.Param; |
|||
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|||
import org.thingsboard.server.dao.model.sql.RpcEntity; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public interface RpcRepository extends CrudRepository<RpcEntity, UUID> { |
|||
Page<RpcEntity> findAllByTenantIdAndDeviceIdAndStatus(UUID tenantId, UUID deviceId, RpcStatus status, Pageable pageable); |
|||
|
|||
Page<RpcEntity> findAllByTenantId(UUID tenantId, Pageable pageable); |
|||
|
|||
@Query(value = "WITH deleted AS (DELETE FROM rpc WHERE (tenant_id = :tenantId AND created_time < :expirationTime) IS TRUE RETURNING *) SELECT count(*) FROM deleted", |
|||
nativeQuery = true) |
|||
Long deleteOutdatedRpcByTenantId(@Param("tenantId") UUID tenantId, @Param("expirationTime") Long expirationTime); |
|||
} |
|||
Loading…
Reference in new issue