86 changed files with 1778 additions and 581 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,48 @@ |
|||
/** |
|||
* 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.ota.firmware; |
|||
|
|||
public enum FirmwareDeliveryMethod { |
|||
PULL(0, "Pull only"), |
|||
PUSH(1, "Push only"), |
|||
BOTH(2, "Push or Push"); |
|||
|
|||
public int code; |
|||
public String type; |
|||
|
|||
FirmwareDeliveryMethod(int code, String type) { |
|||
this.code = code; |
|||
this.type = type; |
|||
} |
|||
|
|||
public static FirmwareDeliveryMethod fromStateFwByType(String type) { |
|||
for (FirmwareDeliveryMethod to : FirmwareDeliveryMethod.values()) { |
|||
if (to.type.equals(type)) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported FW delivery type : %s", type)); |
|||
} |
|||
|
|||
public static FirmwareDeliveryMethod fromStateFwByCode(int code) { |
|||
for (FirmwareDeliveryMethod to : FirmwareDeliveryMethod.values()) { |
|||
if (to.code == code) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported FW delivery code : %s", code)); |
|||
} |
|||
} |
|||
@ -0,0 +1,84 @@ |
|||
/** |
|||
* 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.ota.software; |
|||
|
|||
/** |
|||
* SW Update Result |
|||
* Contains the result of downloading or installing/uninstalling the software |
|||
* 0: Initial value. |
|||
* - Prior to download any new package in the Device, Update Result MUST be reset to this initial value. |
|||
* - One side effect of executing the Uninstall resource is to reset Update Result to this initial value "0". |
|||
* 1: Downloading. |
|||
* - The package downloading process is on-going. |
|||
* 2: Software successfully installed. |
|||
* 3: Successfully Downloaded and package integrity verified |
|||
* (( 4-49, for expansion, of other scenarios)) |
|||
* ** Failed |
|||
* 50: Not enough storage for the new software package. |
|||
* 51: Out of memory during downloading process. |
|||
* 52: Connection lost during downloading process. |
|||
* 53: Package integrity check failure. |
|||
* 54: Unsupported package type. |
|||
* 56: Invalid URI |
|||
* 57: Device defined update error |
|||
* 58: Software installation failure |
|||
* 59: Uninstallation Failure during forUpdate(arg=0) |
|||
* 60-200 : (for expansion, selection to be in blocks depending on new introduction of features) |
|||
* This Resource MAY be reported by sending Observe operation. |
|||
*/ |
|||
public enum SoftwareUpdateResult { |
|||
INITIAL(0, "Initial value", false), |
|||
DOWNLOADING(1, "Downloading", false), |
|||
SUCCESSFULLY_INSTALLED(2, "Software successfully installed", false), |
|||
SUCCESSFULLY_DOWNLOADED_VERIFIED(3, "Successfully Downloaded and package integrity verified", false), |
|||
NOT_ENOUGH_STORAGE(50, "Not enough storage for the new software package", true), |
|||
OUT_OFF_MEMORY(51, "Out of memory during downloading process", true), |
|||
CONNECTION_LOST(52, "Connection lost during downloading process", false), |
|||
PACKAGE_CHECK_FAILURE(53, "Package integrity check failure.", false), |
|||
UNSUPPORTED_PACKAGE_TYPE(54, "Unsupported package type", false), |
|||
INVALID_URI(56, "Invalid URI", true), |
|||
UPDATE_ERROR(57, "Device defined update error", true), |
|||
INSTALL_FAILURE(58, "Software installation failure", true), |
|||
UN_INSTALL_FAILURE(59, "Uninstallation Failure during forUpdate(arg=0)", true); |
|||
|
|||
public int code; |
|||
public String type; |
|||
public boolean isAgain; |
|||
|
|||
SoftwareUpdateResult(int code, String type, boolean isAgain) { |
|||
this.code = code; |
|||
this.type = type; |
|||
this.isAgain = isAgain; |
|||
} |
|||
|
|||
public static SoftwareUpdateResult fromUpdateResultSwByType(String type) { |
|||
for (SoftwareUpdateResult to : SoftwareUpdateResult.values()) { |
|||
if (to.type.equals(type)) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported SW Update Result type : %s", type)); |
|||
} |
|||
|
|||
public static SoftwareUpdateResult fromUpdateResultSwByCode(int code) { |
|||
for (SoftwareUpdateResult to : SoftwareUpdateResult.values()) { |
|||
if (to.code == code) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported SW Update Result code : %s", code)); |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
/** |
|||
* 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.ota.software; |
|||
|
|||
/** |
|||
* SW Update State R |
|||
* 0: INITIAL Before downloading. (see 5.1.2.1) |
|||
* 1: DOWNLOAD STARTED The downloading process has started and is on-going. (see 5.1.2.2) |
|||
* 2: DOWNLOADED The package has been completely downloaded (see 5.1.2.3) |
|||
* 3: DELIVERED In that state, the package has been correctly downloaded and is ready to be installed. (see 5.1.2.4) |
|||
* If executing the Install Resource failed, the state remains at DELIVERED. |
|||
* If executing the Install Resource was successful, the state changes from DELIVERED to INSTALLED. |
|||
* After executing the UnInstall Resource, the state changes to INITIAL. |
|||
* 4: INSTALLED |
|||
*/ |
|||
public enum SoftwareUpdateState { |
|||
INITIAL(0, "Initial"), |
|||
DOWNLOAD_STARTED(1, "DownloadStarted"), |
|||
DOWNLOADED(2, "Downloaded"), |
|||
DELIVERED(3, "Delivered"), |
|||
INSTALLED(4, "Installed"); |
|||
|
|||
public int code; |
|||
public String type; |
|||
|
|||
SoftwareUpdateState(int code, String type) { |
|||
this.code = code; |
|||
this.type = type; |
|||
} |
|||
|
|||
public static SoftwareUpdateState fromUpdateStateSwByType(String type) { |
|||
for (SoftwareUpdateState to : SoftwareUpdateState.values()) { |
|||
if (to.type.equals(type)) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported SW State type : %s", type)); |
|||
} |
|||
|
|||
public static SoftwareUpdateState fromUpdateStateSwByCode(int code) { |
|||
for (SoftwareUpdateState to : SoftwareUpdateState.values()) { |
|||
if (to.code == code) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported SW State type : %s", code)); |
|||
} |
|||
} |
|||
|
|||
@ -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,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.transport.lwm2m.server.uplink; |
|||
|
|||
public enum LwM2mTypeServer { |
|||
BOOTSTRAP(0, "bootstrap"), |
|||
CLIENT(1, "client"); |
|||
|
|||
public int code; |
|||
public String type; |
|||
|
|||
LwM2mTypeServer(int code, String type) { |
|||
this.code = code; |
|||
this.type = type; |
|||
} |
|||
|
|||
public static LwM2mTypeServer fromLwM2mTypeServer(String type) { |
|||
for (LwM2mTypeServer sm : LwM2mTypeServer.values()) { |
|||
if (sm.type.equals(type)) { |
|||
return sm; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported typeServer type : %d", type)); |
|||
} |
|||
} |
|||
|
|||
@ -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