629 changed files with 13285 additions and 4320 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,107 @@ |
|||||
|
/** |
||||
|
* 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.server.controller; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import io.swagger.annotations.ApiParam; |
||||
|
import io.swagger.annotations.ApiResponse; |
||||
|
import io.swagger.annotations.ApiResponses; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.http.HttpHeaders; |
||||
|
import org.springframework.http.MediaType; |
||||
|
import org.springframework.http.ResponseEntity; |
||||
|
import org.springframework.security.access.prepost.PreAuthorize; |
||||
|
import org.springframework.web.bind.annotation.PathVariable; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.thingsboard.server.common.data.Device; |
||||
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
||||
|
import org.thingsboard.server.common.data.id.DeviceId; |
||||
|
import org.thingsboard.server.dao.device.DeviceConnectivityService; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.security.permission.Operation; |
||||
|
import org.thingsboard.server.service.security.system.SystemSecurityService; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.io.IOException; |
||||
|
import java.net.URISyntaxException; |
||||
|
|
||||
|
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_ID; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_ID_PARAM_DESCRIPTION; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.PROTOCOL; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.PROTOCOL_PARAM_DESCRIPTION; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.PEM_CERT_FILE_NAME; |
||||
|
|
||||
|
@RestController |
||||
|
@TbCoreComponent |
||||
|
@RequestMapping("/api") |
||||
|
@RequiredArgsConstructor |
||||
|
@Slf4j |
||||
|
public class DeviceConnectivityController extends BaseController { |
||||
|
|
||||
|
private final DeviceConnectivityService deviceConnectivityService; |
||||
|
private final SystemSecurityService systemSecurityService; |
||||
|
|
||||
|
@ApiOperation(value = "Get commands to publish device telemetry (getDevicePublishTelemetryCommands)", |
||||
|
notes = "Fetch the list of commands to publish device telemetry based on device profile " + |
||||
|
"If the user has the authority of 'Tenant Administrator', the server checks that the device is owned by the same tenant. " + |
||||
|
"If the user has the authority of 'Customer User', the server checks that the device is assigned to the same customer. " + |
||||
|
TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH) |
||||
|
@ApiResponses(value = { |
||||
|
@ApiResponse(code = 200, message = "OK", |
||||
|
examples = @io.swagger.annotations.Example( |
||||
|
value = { |
||||
|
@io.swagger.annotations.ExampleProperty( |
||||
|
mediaType = "application/json", |
||||
|
value = "{\"http\":\"curl -v -X POST http://localhost:8080/api/v1/0ySs4FTOn5WU15XLmal8/telemetry --header Content-Type:application/json --data {temperature:25}\"," + |
||||
|
"\"mqtt\":\"mosquitto_pub -d -q 1 -h localhost -t v1/devices/me/telemetry -i myClient1 -u myUsername1 -P myPassword -m {temperature:25}\"," + |
||||
|
"\"coap\":\"coap-client -m POST coap://localhost:5683/api/v1/0ySs4FTOn5WU15XLmal8/telemetry -t json -e {temperature:25}\"}")}))}) |
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
||||
|
@RequestMapping(value = "/device-connectivity/{deviceId}", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public JsonNode getDevicePublishTelemetryCommands(@ApiParam(value = DEVICE_ID_PARAM_DESCRIPTION) |
||||
|
@PathVariable(DEVICE_ID) String strDeviceId, HttpServletRequest request) throws ThingsboardException, URISyntaxException { |
||||
|
checkParameter(DEVICE_ID, strDeviceId); |
||||
|
DeviceId deviceId = new DeviceId(toUUID(strDeviceId)); |
||||
|
Device device = checkDeviceId(deviceId, Operation.READ_CREDENTIALS); |
||||
|
|
||||
|
String baseUrl = systemSecurityService.getBaseUrl(getTenantId(), getCurrentUser().getCustomerId(), request); |
||||
|
return deviceConnectivityService.findDevicePublishTelemetryCommands(baseUrl, device); |
||||
|
} |
||||
|
|
||||
|
@ApiOperation(value = "Download server certificate using file path defined in device.connectivity properties (downloadServerCertificate)", notes = "Download server certificate.") |
||||
|
@RequestMapping(value = "/device-connectivity/{protocol}/certificate/download", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public ResponseEntity<org.springframework.core.io.Resource> downloadServerCertificate(@ApiParam(value = PROTOCOL_PARAM_DESCRIPTION) |
||||
|
@PathVariable(PROTOCOL) String protocol) throws ThingsboardException, IOException { |
||||
|
checkParameter(PROTOCOL, protocol); |
||||
|
var pemCert = |
||||
|
checkNotNull(deviceConnectivityService.getPemCertFile(protocol), protocol + " pem cert file is not found!"); |
||||
|
|
||||
|
return ResponseEntity.ok() |
||||
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + PEM_CERT_FILE_NAME) |
||||
|
.header("x-filename", PEM_CERT_FILE_NAME) |
||||
|
.contentLength(pemCert.contentLength()) |
||||
|
.contentType(MediaType.APPLICATION_OCTET_STREAM) |
||||
|
.body(pemCert); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,158 @@ |
|||||
|
/** |
||||
|
* 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.server.service.edge; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.transaction.event.TransactionalEventListener; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.cluster.TbClusterService; |
||||
|
import org.thingsboard.server.common.data.OtaPackageInfo; |
||||
|
import org.thingsboard.server.common.data.User; |
||||
|
import org.thingsboard.server.common.data.alarm.AlarmApiCallResult; |
||||
|
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
||||
|
import org.thingsboard.server.common.data.edge.EdgeEventType; |
||||
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
||||
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
||||
|
import org.thingsboard.server.common.data.rule.RuleChain; |
||||
|
import org.thingsboard.server.common.data.rule.RuleChainType; |
||||
|
import org.thingsboard.server.common.data.security.Authority; |
||||
|
import org.thingsboard.server.dao.edge.EdgeSynchronizationManager; |
||||
|
import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent; |
||||
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; |
||||
|
import org.thingsboard.server.dao.eventsourcing.RelationActionEvent; |
||||
|
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; |
||||
|
|
||||
|
import javax.annotation.PostConstruct; |
||||
|
|
||||
|
import static org.thingsboard.server.service.entitiy.DefaultTbNotificationEntityService.edgeTypeByActionType; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* This event listener does not support async event processing because relay on ThreadLocal |
||||
|
* Another possible approach is to implement a special annotation and a bunch of classes similar to TransactionalApplicationListener |
||||
|
* This class is the simplest approach to maintain edge synchronization within the single class. |
||||
|
* <p> |
||||
|
* For async event publishers, you have to decide whether publish event on creating async task in the same thread where dao method called |
||||
|
* @Autowired |
||||
|
* EdgeEventSynchronizationManager edgeSynchronizationManager |
||||
|
* ... |
||||
|
* //some async write action make future
|
||||
|
* if (!edgeSynchronizationManager.isSync()) { |
||||
|
* future.addCallback(eventPublisher.publishEvent(...)) |
||||
|
* } |
||||
|
* */ |
||||
|
@Component |
||||
|
@RequiredArgsConstructor |
||||
|
@Slf4j |
||||
|
public class EdgeEventSourcingListener { |
||||
|
|
||||
|
private final TbClusterService tbClusterService; |
||||
|
private final EdgeSynchronizationManager edgeSynchronizationManager; |
||||
|
|
||||
|
@PostConstruct |
||||
|
public void init() { |
||||
|
log.info("EdgeEventSourcingListener initiated"); |
||||
|
} |
||||
|
|
||||
|
@TransactionalEventListener(fallbackExecution = true) |
||||
|
public void handleEvent(SaveEntityEvent<?> event) { |
||||
|
if (edgeSynchronizationManager.isSync()) { |
||||
|
return; |
||||
|
} |
||||
|
try { |
||||
|
if (!isValidEdgeEventEntity(event.getEntity())) { |
||||
|
return; |
||||
|
} |
||||
|
log.trace("[{}] SaveEntityEvent called: {}", event.getTenantId(), event); |
||||
|
EdgeEventActionType action = Boolean.TRUE.equals(event.getAdded()) ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED; |
||||
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), null, event.getEntityId(), |
||||
|
null, null, action); |
||||
|
} catch (Exception e) { |
||||
|
log.error("[{}] failed to process SaveEntityEvent: {}", event.getTenantId(), event); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@TransactionalEventListener(fallbackExecution = true) |
||||
|
public void handleEvent(DeleteEntityEvent<?> event) { |
||||
|
if (edgeSynchronizationManager.isSync()) { |
||||
|
return; |
||||
|
} |
||||
|
try { |
||||
|
log.trace("[{}] DeleteEntityEvent called: {}", event.getTenantId(), event); |
||||
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), event.getEdgeId(), event.getEntityId(), |
||||
|
JacksonUtil.toString(event.getEntity()), null, EdgeEventActionType.DELETED); |
||||
|
} catch (Exception e) { |
||||
|
log.error("[{}] failed to process DeleteEntityEvent: {}", event.getTenantId(), event); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@TransactionalEventListener(fallbackExecution = true) |
||||
|
public void handleEvent(ActionEntityEvent event) { |
||||
|
if (edgeSynchronizationManager.isSync()) { |
||||
|
return; |
||||
|
} |
||||
|
try { |
||||
|
log.trace("[{}] ActionEntityEvent called: {}", event.getTenantId(), event); |
||||
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), event.getEdgeId(), event.getEntityId(), |
||||
|
event.getBody(), null, edgeTypeByActionType(event.getActionType())); |
||||
|
} catch (Exception e) { |
||||
|
log.error("[{}] failed to process ActionEntityEvent: {}", event.getTenantId(), event); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@TransactionalEventListener(fallbackExecution = true) |
||||
|
public void handleEvent(RelationActionEvent event) { |
||||
|
if (edgeSynchronizationManager.isSync()) { |
||||
|
return; |
||||
|
} |
||||
|
try { |
||||
|
EntityRelation relation = event.getRelation(); |
||||
|
if (relation == null) { |
||||
|
log.trace("[{}] skipping RelationActionEvent event in case relation is null: {}", event.getTenantId(), event); |
||||
|
return; |
||||
|
} |
||||
|
if (!RelationTypeGroup.COMMON.equals(relation.getTypeGroup())) { |
||||
|
log.trace("[{}] skipping RelationActionEvent event in case NOT COMMON relation type group: {}", event.getTenantId(), event); |
||||
|
return; |
||||
|
} |
||||
|
log.trace("[{}] RelationActionEvent called: {}", event.getTenantId(), event); |
||||
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), null, null, |
||||
|
JacksonUtil.toString(relation), EdgeEventType.RELATION, edgeTypeByActionType(event.getActionType())); |
||||
|
} catch (Exception e) { |
||||
|
log.error("[{}] failed to process RelationActionEvent: {}", event.getTenantId(), event); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private boolean isValidEdgeEventEntity(Object entity) { |
||||
|
if (entity instanceof OtaPackageInfo) { |
||||
|
OtaPackageInfo otaPackageInfo = (OtaPackageInfo) entity; |
||||
|
return otaPackageInfo.hasUrl() || otaPackageInfo.isHasData(); |
||||
|
} else if (entity instanceof RuleChain) { |
||||
|
RuleChain ruleChain = (RuleChain) entity; |
||||
|
return RuleChainType.EDGE.equals(ruleChain.getType()); |
||||
|
} else if (entity instanceof User) { |
||||
|
User user = (User) entity; |
||||
|
return !Authority.SYS_ADMIN.equals(user.getAuthority()); |
||||
|
} else if (entity instanceof AlarmApiCallResult) { |
||||
|
AlarmApiCallResult alarmApiCallResult = (AlarmApiCallResult) entity; |
||||
|
return alarmApiCallResult.isModified(); |
||||
|
} |
||||
|
// Default: If the entity doesn't match any of the conditions, consider it as valid.
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,340 @@ |
|||||
|
/** |
||||
|
* 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.server.controller; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import com.google.common.util.concurrent.ListeningExecutorService; |
||||
|
import com.google.common.util.concurrent.MoreExecutors; |
||||
|
import org.junit.After; |
||||
|
import org.junit.Assert; |
||||
|
import org.junit.Before; |
||||
|
import org.junit.Test; |
||||
|
import org.mockito.AdditionalAnswers; |
||||
|
import org.mockito.Mockito; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Primary; |
||||
|
import org.springframework.test.context.ContextConfiguration; |
||||
|
import org.springframework.test.context.TestPropertySource; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.common.util.ThingsBoardExecutors; |
||||
|
import org.thingsboard.server.common.data.Device; |
||||
|
import org.thingsboard.server.common.data.DeviceProfile; |
||||
|
import org.thingsboard.server.common.data.DeviceProfileType; |
||||
|
import org.thingsboard.server.common.data.DeviceTransportType; |
||||
|
import org.thingsboard.server.common.data.Tenant; |
||||
|
import org.thingsboard.server.common.data.User; |
||||
|
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials; |
||||
|
import org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration; |
||||
|
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; |
||||
|
import org.thingsboard.server.common.data.device.profile.DeviceProfileData; |
||||
|
import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; |
||||
|
import org.thingsboard.server.common.data.id.DeviceProfileId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.security.Authority; |
||||
|
import org.thingsboard.server.common.data.security.DeviceCredentials; |
||||
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
||||
|
import org.thingsboard.server.dao.device.DeviceDao; |
||||
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAP; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAPS; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.DOCKER; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTP; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTPS; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTT; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTTS; |
||||
|
|
||||
|
@TestPropertySource(properties = { |
||||
|
"device.connectivity.https.enabled=true", |
||||
|
"device.connectivity.mqtts.enabled=true", |
||||
|
"device.connectivity.coaps.enabled=true", |
||||
|
}) |
||||
|
@ContextConfiguration(classes = {DeviceConnectivityControllerTest.Config.class}) |
||||
|
@DaoSqlTest |
||||
|
public class DeviceConnectivityControllerTest extends AbstractControllerTest { |
||||
|
static final TypeReference<PageData<Device>> PAGE_DATA_DEVICE_TYPE_REF = new TypeReference<>() { |
||||
|
}; |
||||
|
|
||||
|
private static final String DEVICE_TELEMETRY_TOPIC = "v1/devices/customTopic"; |
||||
|
private static final String CHECK_DOCUMENTATION = "Check documentation"; |
||||
|
|
||||
|
ListeningExecutorService executor; |
||||
|
|
||||
|
private Tenant savedTenant; |
||||
|
private User tenantAdmin; |
||||
|
private DeviceProfileId mqttDeviceProfileId; |
||||
|
private DeviceProfileId coapDeviceProfileId; |
||||
|
|
||||
|
static class Config { |
||||
|
@Bean |
||||
|
@Primary |
||||
|
public DeviceDao deviceDao(DeviceDao deviceDao) { |
||||
|
return Mockito.mock(DeviceDao.class, AdditionalAnswers.delegatesTo(deviceDao)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Before |
||||
|
public void beforeTest() throws Exception { |
||||
|
executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(8, getClass())); |
||||
|
|
||||
|
loginSysAdmin(); |
||||
|
|
||||
|
Tenant tenant = new Tenant(); |
||||
|
tenant.setTitle("My tenant"); |
||||
|
savedTenant = doPost("/api/tenant", tenant, Tenant.class); |
||||
|
Assert.assertNotNull(savedTenant); |
||||
|
|
||||
|
tenantAdmin = new User(); |
||||
|
tenantAdmin.setAuthority(Authority.TENANT_ADMIN); |
||||
|
tenantAdmin.setTenantId(savedTenant.getId()); |
||||
|
tenantAdmin.setEmail("tenant2@thingsboard.org"); |
||||
|
tenantAdmin.setFirstName("Joe"); |
||||
|
tenantAdmin.setLastName("Downs"); |
||||
|
|
||||
|
tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1"); |
||||
|
|
||||
|
DeviceProfile mqttProfile = new DeviceProfile(); |
||||
|
mqttProfile.setName("Mqtt device profile"); |
||||
|
mqttProfile.setType(DeviceProfileType.DEFAULT); |
||||
|
mqttProfile.setTransportType(DeviceTransportType.MQTT); |
||||
|
DeviceProfileData deviceProfileData = new DeviceProfileData(); |
||||
|
deviceProfileData.setConfiguration(new DefaultDeviceProfileConfiguration()); |
||||
|
MqttDeviceProfileTransportConfiguration transportConfiguration = new MqttDeviceProfileTransportConfiguration(); |
||||
|
transportConfiguration.setDeviceTelemetryTopic(DEVICE_TELEMETRY_TOPIC); |
||||
|
deviceProfileData.setTransportConfiguration(transportConfiguration); |
||||
|
mqttProfile.setProfileData(deviceProfileData); |
||||
|
mqttProfile.setDefault(false); |
||||
|
mqttProfile.setDefaultRuleChainId(null); |
||||
|
|
||||
|
mqttDeviceProfileId = doPost("/api/deviceProfile", mqttProfile, DeviceProfile.class).getId(); |
||||
|
|
||||
|
DeviceProfile coapProfile = new DeviceProfile(); |
||||
|
coapProfile.setName("Coap device profile"); |
||||
|
coapProfile.setType(DeviceProfileType.DEFAULT); |
||||
|
coapProfile.setTransportType(DeviceTransportType.COAP); |
||||
|
DeviceProfileData deviceProfileData2 = new DeviceProfileData(); |
||||
|
deviceProfileData2.setConfiguration(new DefaultDeviceProfileConfiguration()); |
||||
|
deviceProfileData2.setTransportConfiguration(new CoapDeviceProfileTransportConfiguration()); |
||||
|
coapProfile.setProfileData(deviceProfileData); |
||||
|
coapProfile.setDefault(false); |
||||
|
coapProfile.setDefaultRuleChainId(null); |
||||
|
|
||||
|
coapDeviceProfileId = doPost("/api/deviceProfile", coapProfile, DeviceProfile.class).getId(); |
||||
|
} |
||||
|
|
||||
|
@After |
||||
|
public void afterTest() throws Exception { |
||||
|
executor.shutdownNow(); |
||||
|
|
||||
|
loginSysAdmin(); |
||||
|
|
||||
|
doDelete("/api/tenant/" + savedTenant.getId().getId()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFetchPublishTelemetryCommandsForDefaultDevice() throws Exception { |
||||
|
Device device = new Device(); |
||||
|
device.setName("My device"); |
||||
|
device.setType("default"); |
||||
|
Device savedDevice = doPost("/api/device", device, Device.class); |
||||
|
JsonNode commands = |
||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { |
||||
|
}); |
||||
|
|
||||
|
DeviceCredentials credentials = |
||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
||||
|
|
||||
|
assertThat(commands).hasSize(3); |
||||
|
JsonNode httpCommands = commands.get(HTTP); |
||||
|
assertThat(httpCommands.get(HTTP).asText()).isEqualTo(String.format("curl -v -X POST http://localhost:8080/api/v1/%s/telemetry " + |
||||
|
"--header Content-Type:application/json --data \"{temperature:25}\"", |
||||
|
credentials.getCredentialsId())); |
||||
|
assertThat(httpCommands.get(HTTPS).asText()).isEqualTo(String.format("curl -v -X POST https://localhost:443/api/v1/%s/telemetry " + |
||||
|
"--header Content-Type:application/json --data \"{temperature:25}\"", |
||||
|
credentials.getCredentialsId())); |
||||
|
|
||||
|
|
||||
|
JsonNode mqttCommands = commands.get(MQTT); |
||||
|
assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t v1/devices/me/telemetry " + |
||||
|
"-u %s -m \"{temperature:25}\"", |
||||
|
credentials.getCredentialsId())); |
||||
|
assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download"); |
||||
|
assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 " + |
||||
|
"-t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"", credentials.getCredentialsId())); |
||||
|
|
||||
|
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER); |
||||
|
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" + |
||||
|
" -p 1883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"", |
||||
|
credentials.getCredentialsId())); |
||||
|
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " + |
||||
|
"/bin/sh -c \"curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " + |
||||
|
"mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"\"", |
||||
|
credentials.getCredentialsId())); |
||||
|
|
||||
|
JsonNode linuxCoapCommands = commands.get(COAP); |
||||
|
assertThat(linuxCoapCommands.get(COAP).asText()).isEqualTo(String.format("coap-client -m POST coap://localhost:5683/api/v1/%s/telemetry " + |
||||
|
"-t json -e \"{temperature:25}\"", credentials.getCredentialsId())); |
||||
|
assertThat(linuxCoapCommands.get(COAPS).asText()).isEqualTo(String.format("coap-client-openssl -m POST coaps://localhost:5684/api/v1/%s/telemetry" + |
||||
|
" -t json -e \"{temperature:25}\"", credentials.getCredentialsId())); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFetchPublishTelemetryCommandsForMqttDeviceWithAccessToken() throws Exception { |
||||
|
Device device = new Device(); |
||||
|
device.setName("My device"); |
||||
|
device.setDeviceProfileId(mqttDeviceProfileId); |
||||
|
|
||||
|
Device savedDevice = doPost("/api/device", device, Device.class); |
||||
|
DeviceCredentials credentials = |
||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
||||
|
|
||||
|
JsonNode commands = |
||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { |
||||
|
}); |
||||
|
assertThat(commands).hasSize(1); |
||||
|
|
||||
|
JsonNode mqttCommands = commands.get(MQTT); |
||||
|
assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " + |
||||
|
"-u %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId())); |
||||
|
assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download"); |
||||
|
assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 " + |
||||
|
"-t %s -u %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId())); |
||||
|
|
||||
|
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER); |
||||
|
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" + |
||||
|
" -p 1883 -t %s -u %s -m \"{temperature:25}\"", |
||||
|
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId())); |
||||
|
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " + |
||||
|
"/bin/sh -c \"curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " + |
||||
|
"mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t %s -u %s -m \"{temperature:25}\"\"", |
||||
|
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId())); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFetchPublishTelemetryCommandsForDeviceWithMqttBasicCreds() throws Exception { |
||||
|
Device device = new Device(); |
||||
|
device.setName("My device"); |
||||
|
device.setDeviceProfileId(mqttDeviceProfileId); |
||||
|
|
||||
|
Device savedDevice = doPost("/api/device", device, Device.class); |
||||
|
DeviceCredentials credentials = |
||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
||||
|
credentials.setCredentialsId(null); |
||||
|
credentials.setCredentialsType(DeviceCredentialsType.MQTT_BASIC); |
||||
|
BasicMqttCredentials basicMqttCredentials = new BasicMqttCredentials(); |
||||
|
String clientId = "testClientId"; |
||||
|
String userName = "testUsername"; |
||||
|
String password = "testPassword"; |
||||
|
basicMqttCredentials.setClientId(clientId); |
||||
|
basicMqttCredentials.setUserName(userName); |
||||
|
basicMqttCredentials.setPassword(password); |
||||
|
credentials.setCredentialsValue(JacksonUtil.toString(basicMqttCredentials)); |
||||
|
doPost("/api/device/credentials", credentials) |
||||
|
.andExpect(status().isOk()); |
||||
|
|
||||
|
JsonNode commands = |
||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { |
||||
|
}); |
||||
|
assertThat(commands).hasSize(1); |
||||
|
|
||||
|
JsonNode mqttCommands = commands.get(MQTT); |
||||
|
assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " + |
||||
|
"-i %s -u %s -P %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, clientId, userName, password)); |
||||
|
assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download"); |
||||
|
assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 " + |
||||
|
"-t %s -i %s -u %s -P %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, clientId, userName, password)); |
||||
|
|
||||
|
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER); |
||||
|
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" + |
||||
|
" -p 1883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"", |
||||
|
DEVICE_TELEMETRY_TOPIC, clientId, userName, password)); |
||||
|
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " + |
||||
|
"/bin/sh -c \"curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " + |
||||
|
"mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"\"", |
||||
|
DEVICE_TELEMETRY_TOPIC, clientId, userName, password)); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFetchPublishTelemetryCommandsForDeviceWithX509Creds() throws Exception { |
||||
|
Device device = new Device(); |
||||
|
device.setName("My device"); |
||||
|
device.setDeviceProfileId(mqttDeviceProfileId); |
||||
|
|
||||
|
Device savedDevice = doPost("/api/device", device, Device.class); |
||||
|
DeviceCredentials credentials = |
||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
||||
|
credentials.setCredentialsId(null); |
||||
|
credentials.setCredentialsType(DeviceCredentialsType.X509_CERTIFICATE); |
||||
|
credentials.setCredentialsValue("testValue"); |
||||
|
doPost("/api/device/credentials", credentials) |
||||
|
.andExpect(status().isOk()); |
||||
|
|
||||
|
JsonNode commands = |
||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { |
||||
|
}); |
||||
|
assertThat(commands).hasSize(1); |
||||
|
assertThat(commands.get(MQTT).get(MQTTS).asText()).isEqualTo(CHECK_DOCUMENTATION); |
||||
|
assertThat(commands.get(MQTT).get(DOCKER)).isNull(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFetchPublishTelemetryCommandsForCoapDevice() throws Exception { |
||||
|
Device device = new Device(); |
||||
|
device.setName("My device"); |
||||
|
device.setDeviceProfileId(coapDeviceProfileId); |
||||
|
|
||||
|
Device savedDevice = doPost("/api/device", device, Device.class); |
||||
|
DeviceCredentials credentials = |
||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
||||
|
|
||||
|
JsonNode commands = |
||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { |
||||
|
}); |
||||
|
assertThat(commands).hasSize(1); |
||||
|
|
||||
|
JsonNode linuxCommands = commands.get(COAP); |
||||
|
assertThat(linuxCommands.get(COAP).asText()).isEqualTo(String.format("coap-client -m POST coap://localhost:5683/api/v1/%s/telemetry -t json -e \"{temperature:25}\"", |
||||
|
credentials.getCredentialsId())); |
||||
|
assertThat(linuxCommands.get(COAPS).asText()).isEqualTo(String.format("coap-client-openssl -m POST coaps://localhost:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"", |
||||
|
credentials.getCredentialsId())); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFetchPublishTelemetryCommandsForCoapDeviceWithX509Creds() throws Exception { |
||||
|
Device device = new Device(); |
||||
|
device.setName("My device"); |
||||
|
device.setDeviceProfileId(coapDeviceProfileId); |
||||
|
|
||||
|
Device savedDevice = doPost("/api/device", device, Device.class); |
||||
|
DeviceCredentials credentials = |
||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
||||
|
credentials.setCredentialsId(null); |
||||
|
credentials.setCredentialsType(DeviceCredentialsType.X509_CERTIFICATE); |
||||
|
credentials.setCredentialsValue("testValue"); |
||||
|
doPost("/api/device/credentials", credentials) |
||||
|
.andExpect(status().isOk()); |
||||
|
|
||||
|
JsonNode commands = |
||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { |
||||
|
}); |
||||
|
assertThat(commands).hasSize(1); |
||||
|
assertThat(commands.get(COAP).get(COAPS).asText()).isEqualTo(CHECK_DOCUMENTATION); |
||||
|
} |
||||
|
} |
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue