163 changed files with 5149 additions and 1197 deletions
@ -0,0 +1,49 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.cf; |
|||
|
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.id.CalculatedFieldId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.util.CollectionsUtil; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Data |
|||
@Builder |
|||
public final class PropagationCalculatedFieldResult implements CalculatedFieldResult { |
|||
|
|||
private final List<EntityId> propagationEntityIds; |
|||
private final TelemetryCalculatedFieldResult result; |
|||
|
|||
@Override |
|||
public TbMsg toTbMsg(EntityId entityId, List<CalculatedFieldId> cfIds) { |
|||
return result.toTbMsg(entityId, cfIds); |
|||
} |
|||
|
|||
@Override |
|||
public String stringValue() { |
|||
return result.stringValue(); |
|||
} |
|||
|
|||
@Override |
|||
public boolean isEmpty() { |
|||
return CollectionsUtil.isEmpty(propagationEntityIds) || result.isEmpty(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,72 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.cf.ctx.state.propagation; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.script.api.tbel.TbelCfArg; |
|||
import org.thingsboard.script.api.tbel.TbelCfPropagationArg; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.util.CollectionsUtil; |
|||
import org.thingsboard.server.service.cf.ctx.state.ArgumentEntry; |
|||
import org.thingsboard.server.service.cf.ctx.state.ArgumentEntryType; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Data |
|||
public class PropagationArgumentEntry implements ArgumentEntry { |
|||
|
|||
private List<EntityId> propagationEntityIds; |
|||
|
|||
private boolean forceResetPrevious; |
|||
|
|||
public PropagationArgumentEntry(List<EntityId> propagationEntityIds) { |
|||
this.propagationEntityIds = propagationEntityIds; |
|||
} |
|||
|
|||
@Override |
|||
public ArgumentEntryType getType() { |
|||
return ArgumentEntryType.PROPAGATION; |
|||
} |
|||
|
|||
@Override |
|||
public Object getValue() { |
|||
return propagationEntityIds; |
|||
} |
|||
|
|||
@Override |
|||
public boolean updateEntry(ArgumentEntry entry) { |
|||
if (!(entry instanceof PropagationArgumentEntry propagationArgumentEntry)) { |
|||
throw new IllegalArgumentException("Unsupported argument entry type for propagation argument entry: " + entry.getType()); |
|||
} |
|||
if (propagationArgumentEntry.isEmpty()) { |
|||
propagationEntityIds.clear(); |
|||
} else { |
|||
propagationEntityIds = propagationArgumentEntry.getPropagationEntityIds(); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public boolean isEmpty() { |
|||
return CollectionsUtil.isEmpty(propagationEntityIds); |
|||
} |
|||
|
|||
@Override |
|||
public TbelCfArg toTbelCfArg() { |
|||
return new TbelCfPropagationArg(propagationEntityIds); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,113 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.cf.ctx.state.propagation; |
|||
|
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.actors.TbActorRef; |
|||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|||
import org.thingsboard.server.common.data.cf.configuration.Output; |
|||
import org.thingsboard.server.common.data.cf.configuration.OutputType; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.service.cf.CalculatedFieldResult; |
|||
import org.thingsboard.server.service.cf.PropagationCalculatedFieldResult; |
|||
import org.thingsboard.server.service.cf.TelemetryCalculatedFieldResult; |
|||
import org.thingsboard.server.service.cf.ctx.state.ArgumentEntry; |
|||
import org.thingsboard.server.service.cf.ctx.state.CalculatedFieldCtx; |
|||
import org.thingsboard.server.service.cf.ctx.state.ScriptCalculatedFieldState; |
|||
import org.thingsboard.server.service.cf.ctx.state.SingleValueArgumentEntry; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static org.thingsboard.server.common.data.cf.configuration.PropagationCalculatedFieldConfiguration.PROPAGATION_CONFIG_ARGUMENT; |
|||
|
|||
public class PropagationCalculatedFieldState extends ScriptCalculatedFieldState { |
|||
|
|||
public PropagationCalculatedFieldState(EntityId entityId) { |
|||
super(entityId); |
|||
} |
|||
|
|||
@Override |
|||
public void setCtx(CalculatedFieldCtx ctx, TbActorRef actorCtx) { |
|||
this.ctx = ctx; |
|||
this.actorCtx = actorCtx; |
|||
this.requiredArguments = ctx.getArgNames(); |
|||
if (ctx.isApplyExpressionForResolvedArguments()) { |
|||
this.tbelExpression = ctx.getTbelExpressions().get(ctx.getExpression()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public boolean isReady() { |
|||
if (!super.isReady()) { |
|||
return false; |
|||
} |
|||
ArgumentEntry propagationArg = arguments.get(PROPAGATION_CONFIG_ARGUMENT); |
|||
return propagationArg != null && !propagationArg.isEmpty(); |
|||
} |
|||
|
|||
@Override |
|||
public CalculatedFieldType getType() { |
|||
return CalculatedFieldType.PROPAGATION; |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<CalculatedFieldResult> performCalculation(Map<String, ArgumentEntry> updatedArgs, CalculatedFieldCtx ctx) { |
|||
ArgumentEntry argumentEntry = arguments.get(PROPAGATION_CONFIG_ARGUMENT); |
|||
if (!(argumentEntry instanceof PropagationArgumentEntry propagationArgumentEntry) || propagationArgumentEntry.isEmpty()) { |
|||
return Futures.immediateFuture(PropagationCalculatedFieldResult.builder().build()); |
|||
} |
|||
if (ctx.isApplyExpressionForResolvedArguments()) { |
|||
return Futures.transform(super.performCalculation(updatedArgs, ctx), telemetryCfResult -> |
|||
PropagationCalculatedFieldResult.builder() |
|||
.propagationEntityIds(propagationArgumentEntry.getPropagationEntityIds()) |
|||
.result((TelemetryCalculatedFieldResult) telemetryCfResult) |
|||
.build(), |
|||
MoreExecutors.directExecutor()); |
|||
} |
|||
return Futures.immediateFuture(PropagationCalculatedFieldResult.builder() |
|||
.propagationEntityIds(propagationArgumentEntry.getPropagationEntityIds()) |
|||
.result(toTelemetryResult(ctx)) |
|||
.build()); |
|||
} |
|||
|
|||
private TelemetryCalculatedFieldResult toTelemetryResult(CalculatedFieldCtx ctx) { |
|||
Output output = ctx.getOutput(); |
|||
TelemetryCalculatedFieldResult.TelemetryCalculatedFieldResultBuilder telemetryCfBuilder = |
|||
TelemetryCalculatedFieldResult.builder() |
|||
.type(output.getType()) |
|||
.scope(output.getScope()); |
|||
ObjectNode valuesNode = JacksonUtil.newObjectNode(); |
|||
arguments.forEach((outputKey, argumentEntry) -> { |
|||
if (argumentEntry instanceof PropagationArgumentEntry) { |
|||
return; |
|||
} |
|||
if (argumentEntry instanceof SingleValueArgumentEntry singleArgumentEntry) { |
|||
JacksonUtil.addKvEntry(valuesNode, singleArgumentEntry.getKvEntryValue(), outputKey); |
|||
return; |
|||
} |
|||
throw new IllegalArgumentException("Unsupported argument type: " + argumentEntry.getType() + " detected for argument: " + outputKey + ". " + |
|||
"Only Latest telemetry or Attribute arguments supported for 'Arguments Only' propagation mode!"); |
|||
}); |
|||
ObjectNode result = toSimpleResult(output.getType() == OutputType.TIME_SERIES, valuesNode); |
|||
telemetryCfBuilder.result(result); |
|||
return telemetryCfBuilder.build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,130 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.edge.rpc.processor.ai; |
|||
|
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.data.util.Pair; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.ai.AiModel; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.AiModelId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.dao.exception.DataValidationException; |
|||
import org.thingsboard.server.gen.edge.v1.AiModelUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.EdgeVersion; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.edge.EdgeMsgConstructorUtils; |
|||
|
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@TbCoreComponent |
|||
public class AiModelEdgeProcessor extends BaseAiModelProcessor implements AiModelProcessor { |
|||
|
|||
@Override |
|||
public ListenableFuture<Void> processAiModelMsgFromEdge(TenantId tenantId, Edge edge, AiModelUpdateMsg aiModelUpdateMsg) { |
|||
AiModelId aiModelId = new AiModelId(new UUID(aiModelUpdateMsg.getIdMSB(), aiModelUpdateMsg.getIdLSB())); |
|||
try { |
|||
edgeSynchronizationManager.getEdgeId().set(edge.getId()); |
|||
|
|||
switch (aiModelUpdateMsg.getMsgType()) { |
|||
case ENTITY_CREATED_RPC_MESSAGE: |
|||
case ENTITY_UPDATED_RPC_MESSAGE: |
|||
processAiModel(tenantId, aiModelId, aiModelUpdateMsg, edge); |
|||
return Futures.immediateFuture(null); |
|||
case UNRECOGNIZED: |
|||
default: |
|||
return handleUnsupportedMsgType(aiModelUpdateMsg.getMsgType()); |
|||
} |
|||
} catch (DataValidationException e) { |
|||
return Futures.immediateFailedFuture(e); |
|||
} finally { |
|||
edgeSynchronizationManager.getEdgeId().remove(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public DownlinkMsg convertEdgeEventToDownlink(EdgeEvent edgeEvent, EdgeVersion edgeVersion) { |
|||
AiModelId aiModelId = new AiModelId(edgeEvent.getEntityId()); |
|||
switch (edgeEvent.getAction()) { |
|||
case ADDED, UPDATED -> { |
|||
Optional<AiModel> aiModel = edgeCtx.getAiModelService().findAiModelById(edgeEvent.getTenantId(), aiModelId); |
|||
if (aiModel.isPresent()) { |
|||
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction()); |
|||
AiModelUpdateMsg aiModelUpdateMsg = EdgeMsgConstructorUtils.constructAiModelUpdatedMsg(msgType, aiModel.get()); |
|||
return DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addAiModelUpdateMsg(aiModelUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
case DELETED -> { |
|||
AiModelUpdateMsg aiModelUpdateMsg = EdgeMsgConstructorUtils.constructAiModelDeleteMsg(aiModelId); |
|||
return DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addAiModelUpdateMsg(aiModelUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public EdgeEventType getEdgeEventType() { |
|||
return EdgeEventType.AI_MODEL; |
|||
} |
|||
|
|||
private void processAiModel(TenantId tenantId, AiModelId aiModelId, AiModelUpdateMsg aiModelUpdateMsg, Edge edge) { |
|||
Pair<Boolean, Boolean> resultPair = super.saveOrUpdateAiModel(tenantId, aiModelId, aiModelUpdateMsg); |
|||
Boolean wasCreated = resultPair.getFirst(); |
|||
if (wasCreated) { |
|||
pushAiModelCreatedEventToRuleEngine(tenantId, edge, aiModelId); |
|||
} |
|||
Boolean nameWasUpdated = resultPair.getSecond(); |
|||
if (nameWasUpdated) { |
|||
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.AI_MODEL, EdgeEventActionType.UPDATED, aiModelId, null); |
|||
} |
|||
} |
|||
|
|||
private void pushAiModelCreatedEventToRuleEngine(TenantId tenantId, Edge edge, AiModelId aiModelId) { |
|||
try { |
|||
Optional<AiModel> aiModel = edgeCtx.getAiModelService().findAiModelById(tenantId, aiModelId); |
|||
if (aiModel.isPresent()) { |
|||
String aiModelAsString = JacksonUtil.toString(aiModel.get()); |
|||
TbMsgMetaData msgMetaData = getEdgeActionTbMsgMetaData(edge, edge.getCustomerId()); |
|||
pushEntityEventToRuleEngine(tenantId, aiModelId, edge.getCustomerId(), TbMsgType.ENTITY_CREATED, aiModelAsString, msgMetaData); |
|||
} else { |
|||
log.warn("[{}][{}] Failed to find aiModel", tenantId, aiModelId); |
|||
} |
|||
} catch (Exception e) { |
|||
log.warn("[{}][{}] Failed to push aiModel action to rule engine: {}", tenantId, aiModelId, TbMsgType.ENTITY_CREATED.name(), e); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.edge.rpc.processor.ai; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.gen.edge.v1.AiModelUpdateMsg; |
|||
import org.thingsboard.server.service.edge.rpc.processor.EdgeProcessor; |
|||
|
|||
public interface AiModelProcessor extends EdgeProcessor { |
|||
|
|||
ListenableFuture<Void> processAiModelMsgFromEdge(TenantId tenantId, Edge edge, AiModelUpdateMsg aiModelUpdateMsg); |
|||
|
|||
} |
|||
@ -0,0 +1,81 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.edge.rpc.processor.ai; |
|||
|
|||
import com.datastax.oss.driver.api.core.uuid.Uuids; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.data.util.Pair; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.ai.AiModel; |
|||
import org.thingsboard.server.common.data.id.AiModelId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.service.DataValidator; |
|||
import org.thingsboard.server.gen.edge.v1.AiModelUpdateMsg; |
|||
import org.thingsboard.server.service.edge.rpc.processor.BaseEdgeProcessor; |
|||
|
|||
import java.util.Optional; |
|||
|
|||
@Slf4j |
|||
public abstract class BaseAiModelProcessor extends BaseEdgeProcessor { |
|||
|
|||
@Autowired |
|||
private DataValidator<AiModel> aiModelValidator; |
|||
|
|||
protected Pair<Boolean, Boolean> saveOrUpdateAiModel(TenantId tenantId, AiModelId aiModelId, AiModelUpdateMsg aiModelUpdateMsg) { |
|||
boolean isCreated = false; |
|||
boolean isNameUpdated = false; |
|||
try { |
|||
AiModel aiModel = JacksonUtil.fromString(aiModelUpdateMsg.getEntity(), AiModel.class, true); |
|||
if (aiModel == null) { |
|||
throw new RuntimeException("[{" + tenantId + "}] aiModelUpdateMsg {" + aiModelUpdateMsg + " } cannot be converted to aiModel"); |
|||
} |
|||
|
|||
Optional<AiModel> aiModelById = edgeCtx.getAiModelService().findAiModelById(tenantId, aiModelId); |
|||
if (aiModelById.isEmpty()) { |
|||
aiModel.setCreatedTime(Uuids.unixTimestamp(aiModelId.getId())); |
|||
isCreated = true; |
|||
aiModel.setId(null); |
|||
} else { |
|||
aiModel.setId(aiModelId); |
|||
} |
|||
|
|||
String aiModelName = aiModel.getName(); |
|||
Optional<AiModel> aiModelByName = edgeCtx.getAiModelService().findAiModelByTenantIdAndName(aiModel.getTenantId(), aiModelName); |
|||
if (aiModelByName.isPresent() && !aiModelByName.get().getId().equals(aiModelId)) { |
|||
aiModelName = aiModelName + "_" + StringUtils.randomAlphabetic(15); |
|||
log.warn("[{}] aiModel with name {} already exists. Renaming aiModel name to {}", |
|||
tenantId, aiModel.getName(), aiModelByName.get().getName()); |
|||
isNameUpdated = true; |
|||
} |
|||
aiModel.setName(aiModelName); |
|||
|
|||
aiModelValidator.validate(aiModel, AiModel::getTenantId); |
|||
|
|||
if (isCreated) { |
|||
aiModel.setId(aiModelId); |
|||
} |
|||
|
|||
edgeCtx.getAiModelService().save(aiModel, false); |
|||
} catch (Exception e) { |
|||
log.error("[{}] Failed to process aiModel update msg [{}]", tenantId, aiModelUpdateMsg, e); |
|||
throw e; |
|||
} |
|||
return Pair.of(isCreated, isNameUpdated); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,196 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.edge; |
|||
|
|||
import com.datastax.oss.driver.api.core.uuid.Uuids; |
|||
import com.google.protobuf.AbstractMessage; |
|||
import com.google.protobuf.InvalidProtocolBufferException; |
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.ai.AiModel; |
|||
import org.thingsboard.server.common.data.ai.model.chat.OpenAiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.provider.OpenAiProviderConfig; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.gen.edge.v1.AiModelUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.gen.edge.v1.UplinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UplinkResponseMsg; |
|||
|
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
|
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
@DaoSqlTest |
|||
public class AiModelEdgeTest extends AbstractEdgeTest { |
|||
|
|||
private static final String DEFAULT_AI_MODEL_NAME = "Edge Test AiModel"; |
|||
private static final String UPDATED_AI_MODEL_NAME = "Updated Edge Test AiModel"; |
|||
|
|||
@Test |
|||
public void testAiModel_create_update_delete() throws Exception { |
|||
// create AiModel
|
|||
AiModel aiModel = createSimpleAiModel(DEFAULT_AI_MODEL_NAME); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
AiModel savedAiModel = doPost("/api/ai/model", aiModel, AiModel.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof AiModelUpdateMsg); |
|||
AiModelUpdateMsg aiModelUpdateMsg = (AiModelUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, aiModelUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(savedAiModel.getUuidId().getMostSignificantBits(), aiModelUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(savedAiModel.getUuidId().getLeastSignificantBits(), aiModelUpdateMsg.getIdLSB()); |
|||
AiModel aiModelFromMsg = JacksonUtil.fromString(aiModelUpdateMsg.getEntity(), AiModel.class, true); |
|||
Assert.assertNotNull(aiModelFromMsg); |
|||
|
|||
Assert.assertEquals(DEFAULT_AI_MODEL_NAME, aiModelFromMsg.getName()); |
|||
Assert.assertEquals(savedAiModel.getTenantId(), aiModelFromMsg.getTenantId()); |
|||
|
|||
// update AiModel
|
|||
edgeImitator.expectMessageAmount(1); |
|||
savedAiModel.setName(UPDATED_AI_MODEL_NAME); |
|||
savedAiModel = doPost("/api/ai/model", savedAiModel, AiModel.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof AiModelUpdateMsg); |
|||
aiModelUpdateMsg = (AiModelUpdateMsg) latestMessage; |
|||
aiModelFromMsg = JacksonUtil.fromString(aiModelUpdateMsg.getEntity(), AiModel.class, true); |
|||
Assert.assertNotNull(aiModelFromMsg); |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, aiModelUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(UPDATED_AI_MODEL_NAME, aiModelFromMsg.getName()); |
|||
|
|||
// delete AiModel
|
|||
edgeImitator.expectMessageAmount(1); |
|||
doDelete("/api/ai/model/" + savedAiModel.getUuidId()) |
|||
.andExpect(status().isOk()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof AiModelUpdateMsg); |
|||
aiModelUpdateMsg = (AiModelUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, aiModelUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(savedAiModel.getUuidId().getMostSignificantBits(), aiModelUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(savedAiModel.getUuidId().getLeastSignificantBits(), aiModelUpdateMsg.getIdLSB()); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendAiModelToCloud() throws Exception { |
|||
AiModel aiModel = createSimpleAiModel(DEFAULT_AI_MODEL_NAME); |
|||
UUID uuid = Uuids.timeBased(); |
|||
UplinkMsg uplinkMsg = getUplinkMsg(uuid, aiModel, UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE); |
|||
|
|||
checkAiModelOnCloud(uplinkMsg, uuid, aiModel.getName()); |
|||
} |
|||
|
|||
@Test |
|||
public void testUpdateAiModelNameOnCloud() throws Exception { |
|||
AiModel aiModel = createSimpleAiModel(DEFAULT_AI_MODEL_NAME); |
|||
UUID uuid = Uuids.timeBased(); |
|||
UplinkMsg uplinkMsg = getUplinkMsg(uuid, aiModel, UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE); |
|||
|
|||
checkAiModelOnCloud(uplinkMsg, uuid, aiModel.getName()); |
|||
|
|||
aiModel.setName(UPDATED_AI_MODEL_NAME); |
|||
UplinkMsg updatedUplinkMsg = getUplinkMsg(uuid, aiModel, UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE); |
|||
|
|||
checkAiModelOnCloud(updatedUplinkMsg, uuid, aiModel.getName()); |
|||
} |
|||
|
|||
@Test |
|||
public void testAiModelToCloudWithNameThatAlreadyExistsOnCloud() throws Exception { |
|||
AiModel aiModel = createSimpleAiModel(DEFAULT_AI_MODEL_NAME); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
AiModel savedAiModel = doPost("/api/ai/model", aiModel, AiModel.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
UUID uuid = Uuids.timeBased(); |
|||
|
|||
UplinkMsg uplinkMsg = getUplinkMsg(uuid, aiModel, UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE); |
|||
|
|||
edgeImitator.expectResponsesAmount(1); |
|||
edgeImitator.expectMessageAmount(1); |
|||
edgeImitator.sendUplinkMsg(uplinkMsg); |
|||
|
|||
Assert.assertTrue(edgeImitator.waitForResponses()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
Optional<AiModelUpdateMsg> aiModelUpdateMsgOpt = edgeImitator.findMessageByType(AiModelUpdateMsg.class); |
|||
Assert.assertTrue(aiModelUpdateMsgOpt.isPresent()); |
|||
AiModelUpdateMsg latestAiModelUpdateMsg = aiModelUpdateMsgOpt.get(); |
|||
AiModel aiModelFromMsg = JacksonUtil.fromString(latestAiModelUpdateMsg.getEntity(), AiModel.class, true); |
|||
Assert.assertNotNull(aiModelFromMsg); |
|||
Assert.assertNotEquals(DEFAULT_AI_MODEL_NAME, aiModelFromMsg.getName()); |
|||
|
|||
Assert.assertNotEquals(savedAiModel.getUuidId(), uuid); |
|||
|
|||
AiModel aiModelFromCloud = doGet("/api/ai/model/" + uuid, AiModel.class); |
|||
Assert.assertNotNull(aiModelFromCloud); |
|||
Assert.assertNotEquals(DEFAULT_AI_MODEL_NAME, aiModelFromCloud.getName()); |
|||
} |
|||
|
|||
private AiModel createSimpleAiModel(String name) { |
|||
AiModel aiModel = new AiModel(); |
|||
aiModel.setTenantId(tenantId); |
|||
aiModel.setName(name); |
|||
aiModel.setConfiguration(OpenAiChatModelConfig.builder() |
|||
.providerConfig(new OpenAiProviderConfig(null, "test-api-key")) |
|||
.modelId("gpt-4o") |
|||
.temperature(0.5) |
|||
.topP(0.3) |
|||
.frequencyPenalty(0.1) |
|||
.presencePenalty(0.2) |
|||
.maxOutputTokens(1000) |
|||
.timeoutSeconds(60) |
|||
.maxRetries(2) |
|||
.build()); |
|||
return aiModel; |
|||
} |
|||
|
|||
private UplinkMsg getUplinkMsg(UUID uuid, AiModel aiModel, UpdateMsgType updateMsgType) throws InvalidProtocolBufferException { |
|||
UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder(); |
|||
AiModelUpdateMsg.Builder aiModelUpdateMsgBuilder = AiModelUpdateMsg.newBuilder(); |
|||
aiModelUpdateMsgBuilder.setIdMSB(uuid.getMostSignificantBits()); |
|||
aiModelUpdateMsgBuilder.setIdLSB(uuid.getLeastSignificantBits()); |
|||
aiModelUpdateMsgBuilder.setEntity(JacksonUtil.toString(aiModel)); |
|||
aiModelUpdateMsgBuilder.setMsgType(updateMsgType); |
|||
testAutoGeneratedCodeByProtobuf(aiModelUpdateMsgBuilder); |
|||
uplinkMsgBuilder.addAiModelUpdateMsg(aiModelUpdateMsgBuilder.build()); |
|||
|
|||
testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder); |
|||
|
|||
return uplinkMsgBuilder.build(); |
|||
} |
|||
|
|||
private void checkAiModelOnCloud(UplinkMsg uplinkMsg, UUID uuid, String resourceTitle) throws Exception { |
|||
edgeImitator.expectResponsesAmount(1); |
|||
edgeImitator.sendUplinkMsg(uplinkMsg); |
|||
|
|||
Assert.assertTrue(edgeImitator.waitForResponses()); |
|||
|
|||
UplinkResponseMsg latestResponseMsg = edgeImitator.getLatestResponseMsg(); |
|||
Assert.assertTrue(latestResponseMsg.getSuccess()); |
|||
|
|||
AiModel aiModel = doGet("/api/ai/model/" + uuid, AiModel.class); |
|||
Assert.assertNotNull(aiModel); |
|||
Assert.assertEquals(resourceTitle, aiModel.getName()); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,143 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.cf.ctx.state; |
|||
|
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.script.api.tbel.TbelCfArg; |
|||
import org.thingsboard.script.api.tbel.TbelCfPropagationArg; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.service.cf.ctx.state.propagation.PropagationArgumentEntry; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|||
|
|||
public class PropagationArgumentEntryTest { |
|||
|
|||
private final AssetId ENTITY_1_ID = new AssetId(UUID.fromString("b0a8637d-6d67-43d5-a483-c0e391afe805")); |
|||
private final AssetId ENTITY_2_ID = new AssetId(UUID.fromString("7bd85073-ded5-414f-a2ef-bd56ad3dbf6a")); |
|||
private final AssetId ENTITY_3_ID = new AssetId(UUID.fromString("d64f3e51-2ec2-472f-b475-b095ef8bdc70")); |
|||
|
|||
private PropagationArgumentEntry entry; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
List<EntityId> propagationEntityIds = new ArrayList<>(); |
|||
propagationEntityIds.add(ENTITY_1_ID); |
|||
propagationEntityIds.add(ENTITY_2_ID); |
|||
entry = new PropagationArgumentEntry(propagationEntityIds); |
|||
} |
|||
|
|||
@Test |
|||
void testArgumentEntryType() { |
|||
assertThat(entry.getType()).isEqualTo(ArgumentEntryType.PROPAGATION); |
|||
} |
|||
|
|||
@Test |
|||
void testIsEmpty() { |
|||
PropagationArgumentEntry emptyEntry = new PropagationArgumentEntry(List.of()); |
|||
assertThat(emptyEntry.isEmpty()).isTrue(); |
|||
} |
|||
|
|||
@Test |
|||
void testIsEmptyWhenNullList() { |
|||
PropagationArgumentEntry nullListEntry = new PropagationArgumentEntry(null); |
|||
assertThat(nullListEntry.isEmpty()).isTrue(); |
|||
} |
|||
|
|||
@Test |
|||
void testGetValueReturnsPropagationIds() { |
|||
assertThat(entry.getValue()).isInstanceOf(List.class); |
|||
@SuppressWarnings("unchecked") |
|||
List<AssetId> value = (List<AssetId>) entry.getValue(); |
|||
assertThat(value).containsExactly(ENTITY_1_ID, ENTITY_2_ID); |
|||
} |
|||
|
|||
@Test |
|||
void testUpdateEntryWhenSingleEntryPassed() { |
|||
assertThatThrownBy(() -> entry.updateEntry(new SingleValueArgumentEntry())) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Unsupported argument entry type for propagation argument entry: SINGLE_VALUE"); |
|||
} |
|||
|
|||
@Test |
|||
void testUpdateEntryWhenRollingEntryPassed() { |
|||
assertThatThrownBy(() -> entry.updateEntry(new TsRollingArgumentEntry(5, 30000L))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Unsupported argument entry type for propagation argument entry: TS_ROLLING"); |
|||
} |
|||
|
|||
@Test |
|||
void testUpdateEntryReplacesWithNewIds() { |
|||
var newIds = new ArrayList<EntityId>(List.of(ENTITY_3_ID, ENTITY_1_ID)); |
|||
var updated = new PropagationArgumentEntry(newIds); |
|||
|
|||
boolean changed = entry.updateEntry(updated); |
|||
|
|||
assertThat(changed).isTrue(); |
|||
assertThat(entry.getPropagationEntityIds()).containsExactlyElementsOf(newIds); |
|||
} |
|||
|
|||
@Test |
|||
void testUpdateEntryClearsWhenNewEntryIsEmpty() { |
|||
var updatedEmpty = new PropagationArgumentEntry(List.of()); |
|||
|
|||
boolean changed = entry.updateEntry(updatedEmpty); |
|||
|
|||
assertThat(changed).isTrue(); |
|||
assertThat(entry.getPropagationEntityIds()).isEmpty(); |
|||
} |
|||
|
|||
@Test |
|||
void testUpdateEntryClearsWhenNewEntryIsNullList() { |
|||
var updatedNull = new PropagationArgumentEntry(null); |
|||
|
|||
boolean changed = entry.updateEntry(updatedNull); |
|||
|
|||
assertThat(changed).isTrue(); |
|||
assertThat(entry.getPropagationEntityIds()).isEmpty(); |
|||
} |
|||
|
|||
@Test |
|||
@SuppressWarnings("unchecked") |
|||
void testToTbelCfArgWithValues() { |
|||
TbelCfArg arg = entry.toTbelCfArg(); |
|||
assertThat(arg).isInstanceOf(TbelCfPropagationArg.class); |
|||
|
|||
TbelCfPropagationArg tbelCfPropagationArg = (TbelCfPropagationArg) arg; |
|||
assertThat(tbelCfPropagationArg.getValue()).isInstanceOf(List.class); |
|||
assertThat((List<EntityId>) tbelCfPropagationArg.getValue()).containsExactly(ENTITY_1_ID, ENTITY_2_ID); |
|||
} |
|||
|
|||
|
|||
@Test |
|||
@SuppressWarnings("unchecked") |
|||
void testToTbelCfArgWithEmptyValues() { |
|||
var empty = new PropagationArgumentEntry(List.of()); |
|||
TbelCfArg emptyArg = empty.toTbelCfArg(); |
|||
assertThat(emptyArg).isInstanceOf(TbelCfPropagationArg.class); |
|||
|
|||
TbelCfPropagationArg tbelCfPropagationArg = (TbelCfPropagationArg) emptyArg; |
|||
assertThat(tbelCfPropagationArg.getValue()).isInstanceOf(List.class); |
|||
assertThat((List<EntityId>) tbelCfPropagationArg.getValue()).isEmpty(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,247 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.cf.ctx.state; |
|||
|
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.test.context.SpringBootTest; |
|||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.script.api.tbel.DefaultTbelInvokeService; |
|||
import org.thingsboard.script.api.tbel.TbelInvokeService; |
|||
import org.thingsboard.server.actors.ActorSystemContext; |
|||
import org.thingsboard.server.common.data.AttributeScope; |
|||
import org.thingsboard.server.common.data.cf.CalculatedField; |
|||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|||
import org.thingsboard.server.common.data.cf.configuration.Argument; |
|||
import org.thingsboard.server.common.data.cf.configuration.ArgumentType; |
|||
import org.thingsboard.server.common.data.cf.configuration.CalculatedFieldConfiguration; |
|||
import org.thingsboard.server.common.data.cf.configuration.Output; |
|||
import org.thingsboard.server.common.data.cf.configuration.OutputType; |
|||
import org.thingsboard.server.common.data.cf.configuration.PropagationCalculatedFieldConfiguration; |
|||
import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.kv.DoubleDataEntry; |
|||
import org.thingsboard.server.common.data.relation.EntityRelation; |
|||
import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|||
import org.thingsboard.server.common.stats.DefaultStatsFactory; |
|||
import org.thingsboard.server.dao.usagerecord.ApiLimitService; |
|||
import org.thingsboard.server.service.cf.PropagationCalculatedFieldResult; |
|||
import org.thingsboard.server.service.cf.TelemetryCalculatedFieldResult; |
|||
import org.thingsboard.server.service.cf.ctx.state.propagation.PropagationArgumentEntry; |
|||
import org.thingsboard.server.service.cf.ctx.state.propagation.PropagationCalculatedFieldState; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.ExecutionException; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.Mockito.when; |
|||
import static org.thingsboard.server.common.data.cf.configuration.PropagationCalculatedFieldConfiguration.PROPAGATION_CONFIG_ARGUMENT; |
|||
|
|||
@SpringBootTest(classes = {SimpleMeterRegistry.class, DefaultStatsFactory.class, DefaultTbelInvokeService.class}) |
|||
public class PropagationCalculatedFieldStateTest { |
|||
|
|||
private static final String TEMPERATURE_ARGUMENT_NAME = "t"; |
|||
private static final String TEST_RESULT_EXPRESSION_KEY = "testResult"; |
|||
private static final double TEMPERATURE_VALUE = 12.5; |
|||
|
|||
private final TenantId TENANT_ID = TenantId.fromUUID(UUID.fromString("6c3513cb-85e7-4510-8746-1ba01859a8ce")); |
|||
private final DeviceId DEVICE_ID = new DeviceId(UUID.fromString("be960a50-c029-4698-b2ec-c56a543c561c")); |
|||
private final AssetId ASSET_ID_1 = new AssetId(UUID.fromString("d26f0e5b-7d7d-4a61-9f5e-08ab97b30734")); |
|||
private final AssetId ASSET_ID_2 = new AssetId(UUID.fromString("1933a317-4df5-4d36-9800-68aded74579b")); |
|||
|
|||
private final SingleValueArgumentEntry singleValueArgEntry = |
|||
new SingleValueArgumentEntry(System.currentTimeMillis(), new DoubleDataEntry("temperature", TEMPERATURE_VALUE), 99L); |
|||
|
|||
private final PropagationArgumentEntry propagationArgEntry = |
|||
new PropagationArgumentEntry(new ArrayList<>(List.of(ASSET_ID_2, ASSET_ID_1))); |
|||
|
|||
private PropagationCalculatedFieldState state; |
|||
private CalculatedFieldCtx ctx; |
|||
|
|||
@Autowired |
|||
private TbelInvokeService tbelInvokeService; |
|||
|
|||
@MockitoBean |
|||
private ApiLimitService apiLimitService; |
|||
|
|||
@MockitoBean |
|||
private ActorSystemContext actorSystemContext; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
when(actorSystemContext.getTbelInvokeService()).thenReturn(tbelInvokeService); |
|||
when(actorSystemContext.getApiLimitService()).thenReturn(apiLimitService); |
|||
when(apiLimitService.getLimit(any(), any())).thenReturn(1000L); |
|||
} |
|||
|
|||
void initCtxAndState(boolean applyExpressionToResolvedArguments) { |
|||
ctx = new CalculatedFieldCtx(getCalculatedField(applyExpressionToResolvedArguments), actorSystemContext); |
|||
ctx.init(); |
|||
|
|||
state = new PropagationCalculatedFieldState(ctx.getEntityId()); |
|||
state.setCtx(ctx, null); |
|||
state.init(); |
|||
} |
|||
|
|||
@Test |
|||
void testType() { |
|||
initCtxAndState(false); |
|||
assertThat(state.getType()).isEqualTo(CalculatedFieldType.PROPAGATION); |
|||
} |
|||
|
|||
@Test |
|||
void testInitAddsRequiredArgument() { |
|||
initCtxAndState(false); |
|||
assertThat(state.getRequiredArguments()).containsExactlyInAnyOrder(TEMPERATURE_ARGUMENT_NAME); |
|||
} |
|||
|
|||
@Test |
|||
void testIsReadyReturnFalseWhenNoArgumentsSet() { |
|||
initCtxAndState(false); |
|||
assertThat(state.isReady()).isFalse(); |
|||
} |
|||
|
|||
@Test |
|||
void testIsReadyWhenPropagationArgIsNull() { |
|||
initCtxAndState(false); |
|||
state.getArguments().put(TEMPERATURE_ARGUMENT_NAME, singleValueArgEntry); |
|||
assertThat(state.isReady()).isFalse(); |
|||
} |
|||
|
|||
@Test |
|||
void testIsReadyWhenPropagationArgIsEmpty() { |
|||
initCtxAndState(false); |
|||
state.getArguments().put(TEMPERATURE_ARGUMENT_NAME, singleValueArgEntry); |
|||
state.getArguments().put(PROPAGATION_CONFIG_ARGUMENT, new PropagationArgumentEntry(Collections.emptyList())); |
|||
assertThat(state.isReady()).isFalse(); |
|||
} |
|||
|
|||
@Test |
|||
void testIsReadyWhenPropagationArgHasEntities() { |
|||
initCtxAndState(false); |
|||
state.getArguments().put(TEMPERATURE_ARGUMENT_NAME, singleValueArgEntry); |
|||
state.getArguments().put(PROPAGATION_CONFIG_ARGUMENT, propagationArgEntry); |
|||
assertThat(state.isReady()).isTrue(); |
|||
} |
|||
|
|||
|
|||
@Test |
|||
void testPerformCalculationWithEmptyPropagationArg() throws Exception { |
|||
initCtxAndState(false); |
|||
state.getArguments().put(PROPAGATION_CONFIG_ARGUMENT, new PropagationArgumentEntry(Collections.emptyList())); |
|||
|
|||
PropagationCalculatedFieldResult result = performCalculation(); |
|||
|
|||
assertThat(result).isNotNull(); |
|||
assertThat(result.isEmpty()).isTrue(); |
|||
assertThat(result.getPropagationEntityIds()).isNullOrEmpty(); |
|||
} |
|||
|
|||
@Test |
|||
void testPerformCalculationWithArgumentsOnlyMode() throws Exception { |
|||
initCtxAndState(false); |
|||
state.getArguments().put(PROPAGATION_CONFIG_ARGUMENT, propagationArgEntry); |
|||
state.getArguments().put(TEMPERATURE_ARGUMENT_NAME, singleValueArgEntry); |
|||
|
|||
PropagationCalculatedFieldResult propagationResult = performCalculation(); |
|||
|
|||
assertThat(propagationResult).isNotNull(); |
|||
assertThat(propagationResult.isEmpty()).isFalse(); |
|||
assertThat(propagationResult.getPropagationEntityIds()).containsExactly(ASSET_ID_2, ASSET_ID_1); |
|||
|
|||
TelemetryCalculatedFieldResult result = propagationResult.getResult(); |
|||
assertThat(result).isNotNull(); |
|||
assertThat(result.getType()).isEqualTo(OutputType.ATTRIBUTES); |
|||
assertThat(result.getScope()).isEqualTo(AttributeScope.SERVER_SCOPE); |
|||
|
|||
ObjectNode expectedNode = JacksonUtil.newObjectNode(); |
|||
JacksonUtil.addKvEntry(expectedNode, singleValueArgEntry.getKvEntryValue(), TEMPERATURE_ARGUMENT_NAME); |
|||
|
|||
assertThat(result.getResult()).isEqualTo(expectedNode); |
|||
} |
|||
|
|||
@Test |
|||
void testPerformCalculationWithExpressionResultMode() throws Exception { |
|||
initCtxAndState(true); |
|||
state.getArguments().put(PROPAGATION_CONFIG_ARGUMENT, propagationArgEntry); |
|||
state.getArguments().put(TEMPERATURE_ARGUMENT_NAME, singleValueArgEntry); |
|||
|
|||
PropagationCalculatedFieldResult propagationResult = performCalculation(); |
|||
|
|||
assertThat(propagationResult).isNotNull(); |
|||
assertThat(propagationResult.isEmpty()).isFalse(); |
|||
assertThat(propagationResult.getPropagationEntityIds()).containsExactly(ASSET_ID_2, ASSET_ID_1); |
|||
|
|||
TelemetryCalculatedFieldResult result = propagationResult.getResult(); |
|||
assertThat(result).isNotNull(); |
|||
assertThat(result.getType()).isEqualTo(OutputType.ATTRIBUTES); |
|||
assertThat(result.getScope()).isEqualTo(AttributeScope.SERVER_SCOPE); |
|||
|
|||
ObjectNode expectedNode = JacksonUtil.newObjectNode(); |
|||
expectedNode.put(TEST_RESULT_EXPRESSION_KEY, TEMPERATURE_VALUE * 2); |
|||
|
|||
assertThat(result.getResult()).isEqualTo(expectedNode); |
|||
} |
|||
|
|||
private CalculatedField getCalculatedField(boolean applyExpressionToResolvedArguments) { |
|||
CalculatedField calculatedField = new CalculatedField(); |
|||
calculatedField.setTenantId(TENANT_ID); |
|||
calculatedField.setEntityId(DEVICE_ID); |
|||
calculatedField.setType(CalculatedFieldType.PROPAGATION); |
|||
calculatedField.setName("Test Propagation CF"); |
|||
calculatedField.setConfigurationVersion(1); |
|||
calculatedField.setConfiguration(getCalculatedFieldConfig(applyExpressionToResolvedArguments)); |
|||
calculatedField.setVersion(1L); |
|||
return calculatedField; |
|||
} |
|||
|
|||
private CalculatedFieldConfiguration getCalculatedFieldConfig(boolean applyExpressionToResolvedArguments) { |
|||
var config = new PropagationCalculatedFieldConfiguration(); |
|||
|
|||
config.setDirection(EntitySearchDirection.TO); |
|||
config.setRelationType(EntityRelation.CONTAINS_TYPE); |
|||
config.setApplyExpressionToResolvedArguments(applyExpressionToResolvedArguments); |
|||
|
|||
Argument temperatureArg = new Argument(); |
|||
ReferencedEntityKey tempKey = new ReferencedEntityKey("temperature", ArgumentType.TS_LATEST, null); |
|||
temperatureArg.setRefEntityKey(tempKey); |
|||
|
|||
config.setArguments(Map.of(TEMPERATURE_ARGUMENT_NAME, temperatureArg)); |
|||
config.setExpression("{" + TEST_RESULT_EXPRESSION_KEY + ": " + TEMPERATURE_ARGUMENT_NAME + " * 2}"); |
|||
|
|||
Output output = new Output(); |
|||
output.setType(OutputType.ATTRIBUTES); |
|||
output.setScope(AttributeScope.SERVER_SCOPE); |
|||
config.setOutput(output); |
|||
|
|||
return config; |
|||
} |
|||
|
|||
private PropagationCalculatedFieldResult performCalculation() throws ExecutionException, InterruptedException { |
|||
return (PropagationCalculatedFieldResult) state.performCalculation(Collections.emptyMap(), ctx).get(); |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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; |
|||
|
|||
public enum NameConflictPolicy { |
|||
|
|||
FAIL, |
|||
UNIQUIFY; |
|||
|
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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; |
|||
|
|||
import io.swagger.v3.oas.annotations.media.Schema; |
|||
|
|||
@Schema |
|||
public record NameConflictStrategy(NameConflictPolicy policy, String separator, UniquifyStrategy uniquifyStrategy) { |
|||
|
|||
public static final NameConflictStrategy DEFAULT = new NameConflictStrategy(NameConflictPolicy.FAIL, null, null); |
|||
|
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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; |
|||
|
|||
public enum UniquifyStrategy { |
|||
|
|||
RANDOM, |
|||
INCREMENTAL; |
|||
|
|||
} |
|||
@ -0,0 +1,96 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.cf.configuration; |
|||
|
|||
import jakarta.validation.constraints.NotBlank; |
|||
import jakarta.validation.constraints.NotNull; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|||
import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|||
import org.thingsboard.server.common.data.relation.RelationPathLevel; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
public class PropagationCalculatedFieldConfiguration extends BaseCalculatedFieldConfiguration { |
|||
|
|||
public static final String PROPAGATION_CONFIG_ARGUMENT = "propagationCtx"; |
|||
|
|||
@NotNull |
|||
private EntitySearchDirection direction; |
|||
@NotBlank |
|||
private String relationType; |
|||
|
|||
private boolean applyExpressionToResolvedArguments; |
|||
|
|||
@Override |
|||
public CalculatedFieldType getType() { |
|||
return CalculatedFieldType.PROPAGATION; |
|||
} |
|||
|
|||
@Override |
|||
public void validate() { |
|||
baseCalculatedFieldRestriction(); |
|||
propagationRestriction(); |
|||
if (!applyExpressionToResolvedArguments) { |
|||
arguments.forEach((name, argument) -> { |
|||
if (!currentEntitySource(argument)) { |
|||
throw new IllegalArgumentException("Arguments in 'Arguments only' propagation mode support only the 'Current entity' source entity type!"); |
|||
} |
|||
if (argument.getRefEntityKey() == null) { |
|||
throw new IllegalArgumentException("Argument: '" + name + "' doesn't have reference entity key configured!"); |
|||
} |
|||
if (argument.getRefEntityKey().getType() == ArgumentType.TS_ROLLING) { |
|||
throw new IllegalArgumentException("Argument type: 'Time series rolling' detected for argument: '" + name + "'. " + |
|||
"Only 'Attribute' or 'Latest telemetry' arguments are allowed for 'Arguments only' propagation mode!"); |
|||
} |
|||
}); |
|||
} else { |
|||
boolean noneMatchCurrentEntitySource = arguments.entrySet() |
|||
.stream() |
|||
.noneMatch(entry -> currentEntitySource(entry.getValue())); |
|||
if (noneMatchCurrentEntitySource) { |
|||
throw new IllegalArgumentException("At least one argument must be configured with the 'Current entity' " + |
|||
"source entity type for 'Expression result' propagation mode!"); |
|||
} |
|||
if (StringUtils.isBlank(expression)) { |
|||
throw new IllegalArgumentException("Expression must be specified for 'Expression result' propagation mode!"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public Argument toPropagationArgument() { |
|||
var refDynamicSourceConfiguration = new RelationPathQueryDynamicSourceConfiguration(); |
|||
refDynamicSourceConfiguration.setLevels(List.of(new RelationPathLevel(direction, relationType))); |
|||
var propagationArgument = new Argument(); |
|||
propagationArgument.setRefDynamicSourceConfiguration(refDynamicSourceConfiguration); |
|||
return propagationArgument; |
|||
} |
|||
|
|||
private void propagationRestriction() { |
|||
if (arguments.entrySet().stream().anyMatch(entry -> entry.getKey().equals(PROPAGATION_CONFIG_ARGUMENT))) { |
|||
throw new IllegalArgumentException("Argument name '" + PROPAGATION_CONFIG_ARGUMENT + "' is reserved and cannot be used."); |
|||
} |
|||
} |
|||
|
|||
private boolean currentEntitySource(Argument argument) { |
|||
return argument.getRefEntityId() == null && argument.getRefDynamicSourceConfiguration() == null; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,153 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.cf.configuration; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.extension.ExtendWith; |
|||
import org.mockito.junit.jupiter.MockitoExtension; |
|||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.relation.EntityRelation; |
|||
import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|||
|
|||
import java.util.Map; |
|||
import java.util.UUID; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|||
import static org.thingsboard.server.common.data.cf.configuration.PropagationCalculatedFieldConfiguration.PROPAGATION_CONFIG_ARGUMENT; |
|||
|
|||
@ExtendWith(MockitoExtension.class) |
|||
public class PropagationCalculatedFieldConfigurationTest { |
|||
|
|||
@Test |
|||
void typeShouldBePropagation() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
assertThat(cfg.getType()).isEqualTo(CalculatedFieldType.PROPAGATION); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenConfigurationDisallowArgumentsWithReferencedEntity() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
Argument argumentWithRefEntityIdSet = new Argument(); |
|||
argumentWithRefEntityIdSet.setRefEntityId(new DeviceId(UUID.fromString("bda14084-f40e-4acc-9b85-9d1dd209bb64"))); |
|||
cfg.setArguments(Map.of("argumentWithRefEntityIdSet", argumentWithRefEntityIdSet)); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Arguments in 'Arguments only' propagation mode support only the 'Current entity' source entity type!"); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenConfigurationDisallowArgumentsWithDynamicReferenceConfiguration() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
Argument argumentWithDynamicRefEntitySource = new Argument(); |
|||
argumentWithDynamicRefEntitySource.setRefDynamicSourceConfiguration(new CurrentOwnerDynamicSourceConfiguration()); |
|||
cfg.setArguments(Map.of("argumentWithDynamicRefEntitySource", argumentWithDynamicRefEntitySource)); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Arguments in 'Arguments only' propagation mode support only the 'Current entity' source entity type!"); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenConfigurationHasNoArgumentsWithCurrentEntitySource() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
Argument argumentWithRefEntityIdSet = new Argument(); |
|||
argumentWithRefEntityIdSet.setRefEntityId(new DeviceId(UUID.fromString("3703e895-3f9b-4b75-a715-b68f1ad51944"))); |
|||
cfg.setArguments(Map.of("argumentWithRefEntityIdSet", argumentWithRefEntityIdSet)); |
|||
cfg.setApplyExpressionToResolvedArguments(true); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("At least one argument must be configured with the 'Current entity' " + |
|||
"source entity type for 'Expression result' propagation mode!"); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenUsedReservedPropagationArgumentName() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
cfg.setArguments(Map.of(PROPAGATION_CONFIG_ARGUMENT, new Argument())); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Argument name '" + PROPAGATION_CONFIG_ARGUMENT + "' is reserved and cannot be used."); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenUsedReservedCtxArgumentName() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
cfg.setArguments(Map.of("ctx", new Argument())); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Argument name 'ctx' is reserved and cannot be used."); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenReferencedEntityKeyIsNotSet() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
Argument argument = new Argument(); |
|||
cfg.setArguments(Map.of("someArgumentName", argument)); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Argument: 'someArgumentName' doesn't have reference entity key configured!"); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenReferencedEntityKeyTypeIsTsRolling() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
ReferencedEntityKey referencedEntityKey = new ReferencedEntityKey("someKey", ArgumentType.TS_ROLLING, null); |
|||
Argument argument = new Argument(); |
|||
argument.setRefEntityKey(referencedEntityKey); |
|||
cfg.setArguments(Map.of("someArgumentName", argument)); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Argument type: 'Time series rolling' detected for argument: 'someArgumentName'. " + |
|||
"Only 'Attribute' or 'Latest telemetry' arguments are allowed for 'Arguments only' propagation mode!"); |
|||
} |
|||
|
|||
@Test |
|||
void validateShouldThrowWhenExpressionIsNotSet() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
cfg.setArguments(Map.of("someArgumentName", new Argument())); |
|||
cfg.setApplyExpressionToResolvedArguments(true); |
|||
assertThatThrownBy(cfg::validate) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessage("Expression must be specified for 'Expression result' propagation mode!"); |
|||
} |
|||
|
|||
@Test |
|||
void validateToPropagationArgumentMethodCallReturnCorrectArgument() { |
|||
var cfg = new PropagationCalculatedFieldConfiguration(); |
|||
cfg.setDirection(EntitySearchDirection.TO); |
|||
cfg.setRelationType(EntityRelation.CONTAINS_TYPE); |
|||
|
|||
Argument propagationArgument = cfg.toPropagationArgument(); |
|||
assertThat(propagationArgument).isNotNull(); |
|||
assertThat(propagationArgument.getRefEntityId()).isNull(); |
|||
assertThat(propagationArgument.getRefEntityKey()).isNull(); |
|||
assertThat(propagationArgument.getDefaultValue()).isNull(); |
|||
assertThat(propagationArgument.getTimeWindow()).isNull(); |
|||
assertThat(propagationArgument.getLimit()).isNull(); |
|||
|
|||
assertThat(propagationArgument.getRefDynamicSourceConfiguration()) |
|||
.isNotNull() |
|||
.isInstanceOf(RelationPathQueryDynamicSourceConfiguration.class); |
|||
var refDynamicSourceConfiguration = (RelationPathQueryDynamicSourceConfiguration) propagationArgument.getRefDynamicSourceConfiguration(); |
|||
assertThat(refDynamicSourceConfiguration.getLevels()).isNotEmpty().hasSize(1); |
|||
|
|||
var relationPathLevel = refDynamicSourceConfiguration.getLevels().get(0); |
|||
assertThat(relationPathLevel.direction()).isEqualTo(EntitySearchDirection.TO); |
|||
assertThat(relationPathLevel.relationType()).isEqualTo(EntityRelation.CONTAINS_TYPE); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.script.api.tbel; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonCreator; |
|||
import com.fasterxml.jackson.annotation.JsonProperty; |
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class TbelCfPropagationArg implements TbelCfArg { |
|||
|
|||
private final Object value; |
|||
|
|||
@JsonCreator |
|||
public TbelCfPropagationArg(@JsonProperty("value") Object value) { |
|||
this.value = value; |
|||
} |
|||
|
|||
@Override |
|||
public String getType() { |
|||
return "PROPAGATION_CF_ARGUMENT_VALUE"; |
|||
} |
|||
|
|||
@Override |
|||
public long memorySize() { |
|||
return OBJ_SIZE; |
|||
} |
|||
|
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue