69 changed files with 1121 additions and 237 deletions
@ -0,0 +1,37 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.install; |
|||
|
|||
import org.springframework.context.annotation.Profile; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.dao.util.NoSqlAnyDaoNonCloud; |
|||
|
|||
/* |
|||
* Create keyspace for Cassandra NoSQL database for non-cloud deployment. |
|||
* For cloud service like Astra DBaas admin have to create keyspace manually on cloud UI. |
|||
* Then create tokens with database admin role and put it on Thingsboard parameters. |
|||
* Without this service cloud DB will end up with exception like |
|||
* UnauthorizedException: Missing correct permission on thingsboard |
|||
* */ |
|||
@Service |
|||
@NoSqlAnyDaoNonCloud |
|||
@Profile("install") |
|||
public class CassandraKeyspaceService extends CassandraAbstractDatabaseSchemaService |
|||
implements NoSqlKeyspaceService { |
|||
public CassandraKeyspaceService() { |
|||
super("schema-keyspace.cql"); |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.install; |
|||
|
|||
import org.springframework.context.annotation.Profile; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
|||
|
|||
@Component |
|||
@Profile("install") |
|||
public class DbUpgradeExecutorService extends DbCallbackExecutorService { |
|||
|
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.install; |
|||
|
|||
public interface NoSqlKeyspaceService extends DatabaseSchemaService { |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.util; |
|||
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
|||
|
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
|
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@ConditionalOnExpression("('${database.ts.type}'=='cassandra' || '${database.ts_latest.type}'=='cassandra') " + |
|||
"&& ('${cassandra.cloud.secure_connect_bundle_path}' == null || '${cassandra.cloud.secure_connect_bundle_path}'.isBlank() )") |
|||
public @interface NoSqlAnyDaoNonCloud { |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.util; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
@AllArgsConstructor |
|||
public class TbPair<S, T> { |
|||
private S first; |
|||
private T second; |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
-- |
|||
-- Copyright © 2016-2022 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. |
|||
-- |
|||
|
|||
CREATE KEYSPACE IF NOT EXISTS thingsboard |
|||
WITH replication = { |
|||
'class' : 'SimpleStrategy', |
|||
'replication_factor' : 1 |
|||
}; |
|||
@ -0,0 +1,119 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.rpc; |
|||
|
|||
import com.google.common.util.concurrent.SettableFuture; |
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import org.junit.runner.RunWith; |
|||
import org.mockito.Mock; |
|||
import org.mockito.Mockito; |
|||
import org.mockito.junit.MockitoJUnitRunner; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.common.util.ListeningExecutor; |
|||
import org.thingsboard.rule.engine.api.RuleEngineRpcService; |
|||
import org.thingsboard.rule.engine.api.TbContext; |
|||
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|||
import org.thingsboard.rule.engine.api.TbNodeException; |
|||
import org.thingsboard.server.common.data.DataConstants; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbMsgDataType; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.common.msg.session.SessionMsgType; |
|||
import org.thingsboard.server.dao.edge.EdgeEventService; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.Mockito.never; |
|||
import static org.mockito.Mockito.verify; |
|||
|
|||
@RunWith(MockitoJUnitRunner.class) |
|||
public class TbSendRPCReplyNodeTest { |
|||
|
|||
private static final String DUMMY_SERVICE_ID = "testServiceId"; |
|||
private static final int DUMMY_REQUEST_ID = 0; |
|||
private static final UUID DUMMY_SESSION_ID = UUID.randomUUID(); |
|||
private static final String DUMMY_DATA = "{\"key\":\"value\"}"; |
|||
|
|||
TbSendRPCReplyNode node; |
|||
|
|||
private final TenantId tenantId = TenantId.fromUUID(UUID.randomUUID()); |
|||
private final DeviceId deviceId = new DeviceId(UUID.randomUUID()); |
|||
|
|||
@Mock |
|||
private TbContext ctx; |
|||
|
|||
@Mock |
|||
private RuleEngineRpcService rpcService; |
|||
|
|||
@Mock |
|||
private EdgeEventService edgeEventService; |
|||
|
|||
@Mock |
|||
private ListeningExecutor listeningExecutor; |
|||
|
|||
@Before |
|||
public void setUp() throws TbNodeException { |
|||
node = new TbSendRPCReplyNode(); |
|||
TbSendRpcReplyNodeConfiguration config = new TbSendRpcReplyNodeConfiguration().defaultConfiguration(); |
|||
node.init(ctx, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); |
|||
} |
|||
|
|||
@Test |
|||
public void sendReplyToTransport() { |
|||
Mockito.when(ctx.getRpcService()).thenReturn(rpcService); |
|||
|
|||
|
|||
TbMsg msg = TbMsg.newMsg(SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, getDefaultMetadata(), |
|||
TbMsgDataType.JSON, DUMMY_DATA, null, null); |
|||
|
|||
node.onMsg(ctx, msg); |
|||
|
|||
verify(rpcService).sendRpcReplyToDevice(DUMMY_SERVICE_ID, DUMMY_SESSION_ID, DUMMY_REQUEST_ID, DUMMY_DATA); |
|||
verify(edgeEventService, never()).saveAsync(any()); |
|||
} |
|||
|
|||
@Test |
|||
public void sendReplyToEdgeQueue() { |
|||
Mockito.when(ctx.getTenantId()).thenReturn(tenantId); |
|||
Mockito.when(ctx.getEdgeEventService()).thenReturn(edgeEventService); |
|||
Mockito.when(edgeEventService.saveAsync(any())).thenReturn(SettableFuture.create()); |
|||
Mockito.when(ctx.getDbCallbackExecutor()).thenReturn(listeningExecutor); |
|||
|
|||
TbMsgMetaData defaultMetadata = getDefaultMetadata(); |
|||
defaultMetadata.putValue(DataConstants.EDGE_ID, UUID.randomUUID().toString()); |
|||
defaultMetadata.putValue(DataConstants.DEVICE_ID, UUID.randomUUID().toString()); |
|||
TbMsg msg = TbMsg.newMsg(SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, defaultMetadata, |
|||
TbMsgDataType.JSON, DUMMY_DATA, null, null); |
|||
|
|||
node.onMsg(ctx, msg); |
|||
|
|||
verify(edgeEventService).saveAsync(any()); |
|||
verify(rpcService, never()).sendRpcReplyToDevice(DUMMY_SERVICE_ID, DUMMY_SESSION_ID, DUMMY_REQUEST_ID, DUMMY_DATA); |
|||
} |
|||
|
|||
private TbMsgMetaData getDefaultMetadata() { |
|||
TbSendRpcReplyNodeConfiguration config = new TbSendRpcReplyNodeConfiguration().defaultConfiguration(); |
|||
TbMsgMetaData metadata = new TbMsgMetaData(); |
|||
metadata.putValue(config.getServiceIdMetaDataAttribute(), DUMMY_SERVICE_ID); |
|||
metadata.putValue(config.getSessionIdMetaDataAttribute(), DUMMY_SESSION_ID.toString()); |
|||
metadata.putValue(config.getRequestIdMetaDataAttribute(), Integer.toString(DUMMY_REQUEST_ID)); |
|||
return metadata; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue