21 changed files with 315 additions and 147 deletions
@ -0,0 +1,57 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.rule.engine.transform; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
||||
|
import org.thingsboard.rule.engine.api.TbNode; |
||||
|
import org.thingsboard.rule.engine.api.TbNodeException; |
||||
|
import org.thingsboard.rule.engine.util.TbMsgSource; |
||||
|
import org.thingsboard.server.common.data.util.TbPair; |
||||
|
|
||||
|
public abstract class TbAbstractTransformNodeWithTbMsgSource implements TbNode { |
||||
|
|
||||
|
private static final String FROM_METADATA_PROPERTY = "fromMetadata"; |
||||
|
|
||||
|
protected abstract String getKeyToUpgradeFromVersionZero(); |
||||
|
|
||||
|
@Override |
||||
|
public TbPair<Boolean, JsonNode> upgrade(int fromVersion, JsonNode oldConfiguration) throws TbNodeException { |
||||
|
return fromVersion == 0 ? |
||||
|
upgradeToUseTbMsgSource((ObjectNode) oldConfiguration, getKeyToUpgradeFromVersionZero()) : |
||||
|
new TbPair<>(false, oldConfiguration); |
||||
|
} |
||||
|
|
||||
|
private TbPair<Boolean, JsonNode> upgradeToUseTbMsgSource(ObjectNode configToUpdate, String newProperty) throws TbNodeException { |
||||
|
if (!configToUpdate.has(FROM_METADATA_PROPERTY)) { |
||||
|
throw new TbNodeException("property to update: '" + FROM_METADATA_PROPERTY + "' doesn't exists in configuration!"); |
||||
|
} |
||||
|
var value = configToUpdate.get(FROM_METADATA_PROPERTY).asText(); |
||||
|
if ("true".equals(value)) { |
||||
|
configToUpdate.remove(FROM_METADATA_PROPERTY); |
||||
|
configToUpdate.put(newProperty, TbMsgSource.METADATA.name()); |
||||
|
return new TbPair<>(true, configToUpdate); |
||||
|
} |
||||
|
if ("false".equals(value)) { |
||||
|
configToUpdate.remove(FROM_METADATA_PROPERTY); |
||||
|
configToUpdate.put(newProperty, TbMsgSource.DATA.name()); |
||||
|
return new TbPair<>(true, configToUpdate); |
||||
|
} |
||||
|
throw new TbNodeException("property to update: '" + FROM_METADATA_PROPERTY + "' has unexpected value: " |
||||
|
+ value + ". Allowed values: true or false!"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue