Commit 8046f7ef45365a5bf0703dcc62d11f3687e614bd
Committed by
Andrew Shvayka
1 parent
6b55f330
Changed device provisioning response and added ability to provision devices without deviceName
Showing
6 changed files
with
57 additions
and
72 deletions
@@ -22,6 +22,7 @@ import com.google.common.util.concurrent.Futures; | @@ -22,6 +22,7 @@ import com.google.common.util.concurrent.Futures; | ||
22 | import com.google.common.util.concurrent.ListenableFuture; | 22 | import com.google.common.util.concurrent.ListenableFuture; |
23 | import com.google.common.util.concurrent.MoreExecutors; | 23 | import com.google.common.util.concurrent.MoreExecutors; |
24 | import lombok.extern.slf4j.Slf4j; | 24 | import lombok.extern.slf4j.Slf4j; |
25 | +import org.apache.commons.lang.RandomStringUtils; | ||
25 | import org.springframework.beans.factory.annotation.Autowired; | 26 | import org.springframework.beans.factory.annotation.Autowired; |
26 | import org.springframework.stereotype.Service; | 27 | import org.springframework.stereotype.Service; |
27 | import org.springframework.util.StringUtils; | 28 | import org.springframework.util.StringUtils; |
@@ -188,6 +189,11 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { | @@ -188,6 +189,11 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { | ||
188 | Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName()); | 189 | Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName()); |
189 | try { | 190 | try { |
190 | if (device == null) { | 191 | if (device == null) { |
192 | + if (StringUtils.isEmpty(provisionRequest.getDeviceName())) { | ||
193 | + String newDeviceName = RandomStringUtils.randomAlphanumeric(20); | ||
194 | + log.info("Device name not found in provision request. Generated name is: {}", newDeviceName); | ||
195 | + provisionRequest.setDeviceName(newDeviceName); | ||
196 | + } | ||
191 | Device savedDevice = deviceService.saveDevice(provisionRequest, profile); | 197 | Device savedDevice = deviceService.saveDevice(provisionRequest, profile); |
192 | 198 | ||
193 | deviceStateService.onDeviceAdded(savedDevice); | 199 | deviceStateService.onDeviceAdded(savedDevice); |
@@ -54,12 +54,15 @@ import org.thingsboard.server.dao.device.provision.ProvisionResponse; | @@ -54,12 +54,15 @@ import org.thingsboard.server.dao.device.provision.ProvisionResponse; | ||
54 | import org.thingsboard.server.dao.relation.RelationService; | 54 | import org.thingsboard.server.dao.relation.RelationService; |
55 | import org.thingsboard.server.dao.util.mapping.JacksonUtil; | 55 | import org.thingsboard.server.dao.util.mapping.JacksonUtil; |
56 | import org.thingsboard.server.gen.transport.TransportProtos; | 56 | import org.thingsboard.server.gen.transport.TransportProtos; |
57 | +import org.thingsboard.server.gen.transport.TransportProtos.DeviceCredentialsProto; | ||
57 | import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; | 58 | import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; |
58 | import org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayRequestMsg; | 59 | import org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayRequestMsg; |
59 | import org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayResponseMsg; | 60 | import org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayResponseMsg; |
60 | import org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileRequestMsg; | 61 | import org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileRequestMsg; |
61 | import org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileResponseMsg; | 62 | import org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileResponseMsg; |
62 | import org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceRequestMsg; | 63 | import org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceRequestMsg; |
64 | +import org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsg; | ||
65 | +import org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsgOrBuilder; | ||
63 | import org.thingsboard.server.gen.transport.TransportProtos.TransportApiRequestMsg; | 66 | import org.thingsboard.server.gen.transport.TransportProtos.TransportApiRequestMsg; |
64 | import org.thingsboard.server.gen.transport.TransportProtos.TransportApiResponseMsg; | 67 | import org.thingsboard.server.gen.transport.TransportProtos.TransportApiResponseMsg; |
65 | import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg; | 68 | import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg; |
@@ -292,30 +295,29 @@ public class DefaultTransportApiService implements TransportApiService { | @@ -292,30 +295,29 @@ public class DefaultTransportApiService implements TransportApiService { | ||
292 | requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceSecret())))); | 295 | requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceSecret())))); |
293 | } catch (ProvisionFailedException e) { | 296 | } catch (ProvisionFailedException e) { |
294 | return Futures.immediateFuture(getTransportApiResponseMsg( | 297 | return Futures.immediateFuture(getTransportApiResponseMsg( |
295 | - TransportProtos.DeviceCredentialsProto.getDefaultInstance(), | 298 | + new DeviceCredentials(), |
296 | TransportProtos.ProvisionResponseStatus.valueOf(e.getMessage()))); | 299 | TransportProtos.ProvisionResponseStatus.valueOf(e.getMessage()))); |
297 | } | 300 | } |
298 | - return Futures.transform(provisionResponseFuture, provisionResponse -> getTransportApiResponseMsg( | ||
299 | - getDeviceCredentials(provisionResponse.getDeviceCredentials()), TransportProtos.ProvisionResponseStatus.SUCCESS), | 301 | + return Futures.transform(provisionResponseFuture, provisionResponse -> getTransportApiResponseMsg(provisionResponse.getDeviceCredentials(), TransportProtos.ProvisionResponseStatus.SUCCESS), |
300 | dbCallbackExecutorService); | 302 | dbCallbackExecutorService); |
301 | } | 303 | } |
302 | 304 | ||
303 | - private TransportApiResponseMsg getTransportApiResponseMsg(TransportProtos.DeviceCredentialsProto deviceCredentials, TransportProtos.ProvisionResponseStatus status) { | ||
304 | - return TransportApiResponseMsg.newBuilder() | ||
305 | - .setProvisionDeviceResponseMsg(TransportProtos.ProvisionDeviceResponseMsg.newBuilder() | ||
306 | - .setDeviceCredentials(deviceCredentials) | ||
307 | - .setProvisionResponseStatus(status) | ||
308 | - .build()) | ||
309 | - .build(); | ||
310 | - } | 305 | + private TransportApiResponseMsg getTransportApiResponseMsg(DeviceCredentials deviceCredentials, TransportProtos.ProvisionResponseStatus status) { |
306 | + TransportProtos.ProvisionDeviceResponseMsg.Builder provisionResponse = TransportProtos.ProvisionDeviceResponseMsg.newBuilder() | ||
307 | + .setCredentialsType(TransportProtos.CredentialsType.valueOf(deviceCredentials.getCredentialsType().name())) | ||
308 | + .setStatus(status); | ||
309 | + switch (deviceCredentials.getCredentialsType()){ | ||
310 | + case ACCESS_TOKEN: | ||
311 | + provisionResponse.setCredentialsValue(deviceCredentials.getCredentialsId()); | ||
312 | + break; | ||
313 | + case MQTT_BASIC: | ||
314 | + case X509_CERTIFICATE: | ||
315 | + provisionResponse.setCredentialsValue(deviceCredentials.getCredentialsValue()); | ||
316 | + break; | ||
317 | + } | ||
311 | 318 | ||
312 | - private TransportProtos.DeviceCredentialsProto getDeviceCredentials(DeviceCredentials deviceCredentials) { | ||
313 | - return TransportProtos.DeviceCredentialsProto.newBuilder() | ||
314 | - .setDeviceIdMSB(deviceCredentials.getDeviceId().getId().getMostSignificantBits()) | ||
315 | - .setDeviceIdLSB(deviceCredentials.getDeviceId().getId().getLeastSignificantBits()) | ||
316 | - .setCredentialsType(TransportProtos.CredentialsType.valueOf(deviceCredentials.getCredentialsType().name())) | ||
317 | - .setCredentialsId(deviceCredentials.getCredentialsId()) | ||
318 | - .setCredentialsValue(deviceCredentials.getCredentialsValue() != null ? deviceCredentials.getCredentialsValue() : "") | 319 | + return TransportApiResponseMsg.newBuilder() |
320 | + .setProvisionDeviceResponseMsg(provisionResponse.build()) | ||
319 | .build(); | 321 | .build(); |
320 | } | 322 | } |
321 | 323 |
@@ -98,7 +98,7 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -98,7 +98,7 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
98 | byte[] result = createMqttClientAndPublish().getPayloadBytes(); | 98 | byte[] result = createMqttClientAndPublish().getPayloadBytes(); |
99 | JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); | 99 | JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); |
100 | Assert.assertEquals("Provision data was not found!", response.get("errorMsg").getAsString()); | 100 | Assert.assertEquals("Provision data was not found!", response.get("errorMsg").getAsString()); |
101 | - Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.get("provisionDeviceStatus").getAsString()); | 101 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.get("status").getAsString()); |
102 | } | 102 | } |
103 | 103 | ||
104 | 104 | ||
@@ -110,13 +110,11 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -110,13 +110,11 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
110 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 110 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
111 | 111 | ||
112 | Assert.assertNotNull(createdDevice); | 112 | Assert.assertNotNull(createdDevice); |
113 | - Assert.assertEquals(createdDevice.getId().toString(), response.get("deviceId").getAsString()); | ||
114 | 113 | ||
115 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 114 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
116 | 115 | ||
117 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); | 116 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); |
118 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | ||
119 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("provisionDeviceStatus").getAsString()); | 117 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("status").getAsString()); |
120 | } | 118 | } |
121 | 119 | ||
122 | 120 | ||
@@ -129,15 +127,13 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -129,15 +127,13 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
129 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 127 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
130 | 128 | ||
131 | Assert.assertNotNull(createdDevice); | 129 | Assert.assertNotNull(createdDevice); |
132 | - Assert.assertEquals(createdDevice.getId().toString(), response.get("deviceId").getAsString()); | ||
133 | 130 | ||
134 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 131 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
135 | 132 | ||
136 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); | 133 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); |
137 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | ||
138 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), "ACCESS_TOKEN"); | 134 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), "ACCESS_TOKEN"); |
139 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), "test_token"); | ||
140 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("provisionDeviceStatus").getAsString()); | 135 | + Assert.assertEquals(deviceCredentials.getCredentialsValue(), "test_token"); |
136 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("status").getAsString()); | ||
141 | } | 137 | } |
142 | 138 | ||
143 | 139 | ||
@@ -150,12 +146,10 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -150,12 +146,10 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
150 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 146 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
151 | 147 | ||
152 | Assert.assertNotNull(createdDevice); | 148 | Assert.assertNotNull(createdDevice); |
153 | - Assert.assertEquals(createdDevice.getId().toString(), response.get("deviceId").getAsString()); | ||
154 | 149 | ||
155 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 150 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
156 | 151 | ||
157 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); | 152 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); |
158 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | ||
159 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), "X509_CERTIFICATE"); | 153 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), "X509_CERTIFICATE"); |
160 | 154 | ||
161 | String cert = EncryptionUtil.trimNewLines(deviceCredentials.getCredentialsValue()); | 155 | String cert = EncryptionUtil.trimNewLines(deviceCredentials.getCredentialsValue()); |
@@ -164,7 +158,7 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -164,7 +158,7 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
164 | Assert.assertEquals(deviceCredentials.getCredentialsId(), sha3Hash); | 158 | Assert.assertEquals(deviceCredentials.getCredentialsId(), sha3Hash); |
165 | 159 | ||
166 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), "testHash"); | 160 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), "testHash"); |
167 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("provisionDeviceStatus").getAsString()); | 161 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("status").getAsString()); |
168 | } | 162 | } |
169 | 163 | ||
170 | 164 | ||
@@ -177,12 +171,10 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -177,12 +171,10 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
177 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 171 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
178 | 172 | ||
179 | Assert.assertNotNull(createdDevice); | 173 | Assert.assertNotNull(createdDevice); |
180 | - Assert.assertEquals(createdDevice.getId().toString(), response.get("deviceId").getAsString()); | ||
181 | 174 | ||
182 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 175 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
183 | 176 | ||
184 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); | 177 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); |
185 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | ||
186 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), "MQTT_BASIC"); | 178 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), "MQTT_BASIC"); |
187 | Assert.assertEquals(deviceCredentials.getCredentialsId(), EncryptionUtil.getSha3Hash("|", "test_clientId", "test_username")); | 179 | Assert.assertEquals(deviceCredentials.getCredentialsId(), EncryptionUtil.getSha3Hash("|", "test_clientId", "test_username")); |
188 | 180 | ||
@@ -192,21 +184,18 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -192,21 +184,18 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
192 | mqttCredentials.setPassword("test_password"); | 184 | mqttCredentials.setPassword("test_password"); |
193 | 185 | ||
194 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), JacksonUtil.toString(mqttCredentials)); | 186 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), JacksonUtil.toString(mqttCredentials)); |
195 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | ||
196 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("provisionDeviceStatus").getAsString()); | 187 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("status").getAsString()); |
197 | } | 188 | } |
198 | 189 | ||
199 | protected void processTestProvisioningCheckPreProvisionedDevice() throws Exception { | 190 | protected void processTestProvisioningCheckPreProvisionedDevice() throws Exception { |
200 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.JSON, null, null, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKey", "testProvisionSecret"); | 191 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.JSON, null, null, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKey", "testProvisionSecret"); |
201 | byte[] result = createMqttClientAndPublish().getPayloadBytes(); | 192 | byte[] result = createMqttClientAndPublish().getPayloadBytes(); |
202 | JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); | 193 | JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); |
203 | - Assert.assertEquals(savedDevice.getId().toString(), response.get("deviceId").getAsString()); | ||
204 | 194 | ||
205 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), savedDevice.getId()); | 195 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), savedDevice.getId()); |
206 | 196 | ||
207 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); | 197 | Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); |
208 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | ||
209 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("provisionDeviceStatus").getAsString()); | 198 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("status").getAsString()); |
210 | } | 199 | } |
211 | 200 | ||
212 | protected void processTestProvisioningWithBadKeyDevice() throws Exception { | 201 | protected void processTestProvisioningWithBadKeyDevice() throws Exception { |
@@ -214,7 +203,7 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | @@ -214,7 +203,7 @@ public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIn | ||
214 | byte[] result = createMqttClientAndPublish().getPayloadBytes(); | 203 | byte[] result = createMqttClientAndPublish().getPayloadBytes(); |
215 | JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); | 204 | JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); |
216 | Assert.assertEquals("Provision data was not found!", response.get("errorMsg").getAsString()); | 205 | Assert.assertEquals("Provision data was not found!", response.get("errorMsg").getAsString()); |
217 | - Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.get("provisionDeviceStatus").getAsString()); | 206 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.get("status").getAsString()); |
218 | } | 207 | } |
219 | 208 | ||
220 | protected TestMqttCallback createMqttClientAndPublish() throws Exception { | 209 | protected TestMqttCallback createMqttClientAndPublish() throws Exception { |
@@ -105,7 +105,7 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | @@ -105,7 +105,7 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | ||
105 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, null, null, DeviceProfileProvisionType.DISABLED, null, null); | 105 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, null, null, DeviceProfileProvisionType.DISABLED, null, null); |
106 | ProvisionDeviceResponseMsg result = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); | 106 | ProvisionDeviceResponseMsg result = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); |
107 | Assert.assertNotNull(result); | 107 | Assert.assertNotNull(result); |
108 | - Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), result.getProvisionResponseStatus().toString()); | 108 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), result.getStatus().toString()); |
109 | } | 109 | } |
110 | 110 | ||
111 | protected void processTestProvisioningCreateNewDeviceWithoutCredentials() throws Exception { | 111 | protected void processTestProvisioningCreateNewDeviceWithoutCredentials() throws Exception { |
@@ -115,13 +115,11 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | @@ -115,13 +115,11 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | ||
115 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 115 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
116 | 116 | ||
117 | Assert.assertNotNull(createdDevice); | 117 | Assert.assertNotNull(createdDevice); |
118 | - Assert.assertEquals(createdDevice.getId().getId(), new UUID(response.getDeviceCredentials().getDeviceIdMSB(), response.getDeviceCredentials().getDeviceIdLSB())); | ||
119 | 118 | ||
120 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 119 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
121 | 120 | ||
122 | - Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getDeviceCredentials().getCredentialsType().toString()); | ||
123 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | ||
124 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getProvisionResponseStatus().toString()); | 121 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getCredentialsType().toString()); |
122 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getStatus().toString()); | ||
125 | } | 123 | } |
126 | 124 | ||
127 | protected void processTestProvisioningCreateNewDeviceWithAccessToken() throws Exception { | 125 | protected void processTestProvisioningCreateNewDeviceWithAccessToken() throws Exception { |
@@ -133,15 +131,13 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | @@ -133,15 +131,13 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | ||
133 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 131 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
134 | 132 | ||
135 | Assert.assertNotNull(createdDevice); | 133 | Assert.assertNotNull(createdDevice); |
136 | - Assert.assertEquals(createdDevice.getId().getId(), new UUID(response.getDeviceCredentials().getDeviceIdMSB(), response.getDeviceCredentials().getDeviceIdLSB())); | ||
137 | 134 | ||
138 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 135 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
139 | 136 | ||
140 | - Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getDeviceCredentials().getCredentialsType().toString()); | ||
141 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | 137 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getCredentialsType().toString()); |
142 | Assert.assertEquals(deviceCredentials.getCredentialsType(), DeviceCredentialsType.ACCESS_TOKEN); | 138 | Assert.assertEquals(deviceCredentials.getCredentialsType(), DeviceCredentialsType.ACCESS_TOKEN); |
143 | Assert.assertEquals(deviceCredentials.getCredentialsId(), "test_token"); | 139 | Assert.assertEquals(deviceCredentials.getCredentialsId(), "test_token"); |
144 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getProvisionResponseStatus().toString()); | 140 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getStatus().toString()); |
145 | } | 141 | } |
146 | 142 | ||
147 | protected void processTestProvisioningCreateNewDeviceWithCert() throws Exception { | 143 | protected void processTestProvisioningCreateNewDeviceWithCert() throws Exception { |
@@ -153,12 +149,10 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | @@ -153,12 +149,10 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | ||
153 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 149 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
154 | 150 | ||
155 | Assert.assertNotNull(createdDevice); | 151 | Assert.assertNotNull(createdDevice); |
156 | - Assert.assertEquals(createdDevice.getId().getId(), new UUID(response.getDeviceCredentials().getDeviceIdMSB(), response.getDeviceCredentials().getDeviceIdLSB())); | ||
157 | 152 | ||
158 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 153 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
159 | 154 | ||
160 | - Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getDeviceCredentials().getCredentialsType().toString()); | ||
161 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | 155 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getCredentialsType().toString()); |
162 | Assert.assertEquals(deviceCredentials.getCredentialsType(), DeviceCredentialsType.X509_CERTIFICATE); | 156 | Assert.assertEquals(deviceCredentials.getCredentialsType(), DeviceCredentialsType.X509_CERTIFICATE); |
163 | 157 | ||
164 | String cert = EncryptionUtil.trimNewLines(deviceCredentials.getCredentialsValue()); | 158 | String cert = EncryptionUtil.trimNewLines(deviceCredentials.getCredentialsValue()); |
@@ -167,7 +161,7 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | @@ -167,7 +161,7 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | ||
167 | Assert.assertEquals(deviceCredentials.getCredentialsId(), sha3Hash); | 161 | Assert.assertEquals(deviceCredentials.getCredentialsId(), sha3Hash); |
168 | 162 | ||
169 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), "testHash"); | 163 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), "testHash"); |
170 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getProvisionResponseStatus().toString()); | 164 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getStatus().toString()); |
171 | } | 165 | } |
172 | 166 | ||
173 | protected void processTestProvisioningCreateNewDeviceWithMqttBasic() throws Exception { | 167 | protected void processTestProvisioningCreateNewDeviceWithMqttBasic() throws Exception { |
@@ -185,12 +179,10 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | @@ -185,12 +179,10 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | ||
185 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | 179 | Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); |
186 | 180 | ||
187 | Assert.assertNotNull(createdDevice); | 181 | Assert.assertNotNull(createdDevice); |
188 | - Assert.assertEquals(createdDevice.getId().getId(), new UUID(response.getDeviceCredentials().getDeviceIdMSB(), response.getDeviceCredentials().getDeviceIdLSB())); | ||
189 | 182 | ||
190 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | 183 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); |
191 | 184 | ||
192 | - Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getDeviceCredentials().getCredentialsType().toString()); | ||
193 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | 185 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getCredentialsType().toString()); |
194 | Assert.assertEquals(deviceCredentials.getCredentialsType(), DeviceCredentialsType.MQTT_BASIC); | 186 | Assert.assertEquals(deviceCredentials.getCredentialsType(), DeviceCredentialsType.MQTT_BASIC); |
195 | Assert.assertEquals(deviceCredentials.getCredentialsId(), EncryptionUtil.getSha3Hash("|", "test_clientId", "test_username")); | 187 | Assert.assertEquals(deviceCredentials.getCredentialsId(), EncryptionUtil.getSha3Hash("|", "test_clientId", "test_username")); |
196 | 188 | ||
@@ -200,26 +192,23 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | @@ -200,26 +192,23 @@ public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttI | ||
200 | mqttCredentials.setPassword("test_password"); | 192 | mqttCredentials.setPassword("test_password"); |
201 | 193 | ||
202 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), JacksonUtil.toString(mqttCredentials)); | 194 | Assert.assertEquals(deviceCredentials.getCredentialsValue(), JacksonUtil.toString(mqttCredentials)); |
203 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | ||
204 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getProvisionResponseStatus().toString()); | 195 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getStatus().toString()); |
205 | } | 196 | } |
206 | 197 | ||
207 | protected void processTestProvisioningCheckPreProvisionedDevice() throws Exception { | 198 | protected void processTestProvisioningCheckPreProvisionedDevice() throws Exception { |
208 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKey", "testProvisionSecret"); | 199 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKey", "testProvisionSecret"); |
209 | ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); | 200 | ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); |
210 | - Assert.assertEquals(savedDevice.getId().getId(), new UUID(response.getDeviceCredentials().getDeviceIdMSB(), response.getDeviceCredentials().getDeviceIdLSB())); | ||
211 | 201 | ||
212 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), savedDevice.getId()); | 202 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), savedDevice.getId()); |
213 | 203 | ||
214 | - Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getDeviceCredentials().getCredentialsType().toString()); | ||
215 | - Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | ||
216 | - Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getProvisionResponseStatus().toString()); | 204 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getCredentialsType().toString()); |
205 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getStatus().toString()); | ||
217 | } | 206 | } |
218 | 207 | ||
219 | protected void processTestProvisioningWithBadKeyDevice() throws Exception { | 208 | protected void processTestProvisioningWithBadKeyDevice() throws Exception { |
220 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKeyOrig", "testProvisionSecret"); | 209 | super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKeyOrig", "testProvisionSecret"); |
221 | ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); | 210 | ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); |
222 | - Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.getProvisionResponseStatus().toString()); | 211 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.getStatus().toString()); |
223 | } | 212 | } |
224 | 213 | ||
225 | protected TestMqttCallback createMqttClientAndPublish() throws Exception { | 214 | protected TestMqttCallback createMqttClientAndPublish() throws Exception { |
@@ -269,8 +269,9 @@ message ProvisionDeviceCredentialsMsg { | @@ -269,8 +269,9 @@ message ProvisionDeviceCredentialsMsg { | ||
269 | } | 269 | } |
270 | 270 | ||
271 | message ProvisionDeviceResponseMsg { | 271 | message ProvisionDeviceResponseMsg { |
272 | - DeviceCredentialsProto deviceCredentials = 1; | ||
273 | - ProvisionResponseStatus provisionResponseStatus = 2; | 272 | + ProvisionResponseStatus status = 1; |
273 | + CredentialsType credentialsType = 2; | ||
274 | + string credentialsValue = 3; | ||
274 | } | 275 | } |
275 | 276 | ||
276 | enum ProvisionResponseStatus { | 277 | enum ProvisionResponseStatus { |
@@ -408,22 +408,20 @@ public class JsonConverter { | @@ -408,22 +408,20 @@ public class JsonConverter { | ||
408 | 408 | ||
409 | private static JsonObject toJson(ProvisionDeviceResponseMsg payload, boolean toGateway, int requestId) { | 409 | private static JsonObject toJson(ProvisionDeviceResponseMsg payload, boolean toGateway, int requestId) { |
410 | JsonObject result = new JsonObject(); | 410 | JsonObject result = new JsonObject(); |
411 | - if (payload.getProvisionResponseStatus() == TransportProtos.ProvisionResponseStatus.NOT_FOUND) { | 411 | + if (payload.getStatus() == TransportProtos.ProvisionResponseStatus.NOT_FOUND) { |
412 | result.addProperty("errorMsg", "Provision data was not found!"); | 412 | result.addProperty("errorMsg", "Provision data was not found!"); |
413 | - result.addProperty("provisionDeviceStatus", ProvisionResponseStatus.NOT_FOUND.name()); | ||
414 | - } else if (payload.getProvisionResponseStatus() == TransportProtos.ProvisionResponseStatus.FAILURE) { | 413 | + result.addProperty("status", ProvisionResponseStatus.NOT_FOUND.name()); |
414 | + } else if (payload.getStatus() == TransportProtos.ProvisionResponseStatus.FAILURE) { | ||
415 | result.addProperty("errorMsg", "Failed to provision device!"); | 415 | result.addProperty("errorMsg", "Failed to provision device!"); |
416 | - result.addProperty("provisionDeviceStatus", ProvisionResponseStatus.FAILURE.name()); | 416 | + result.addProperty("status", ProvisionResponseStatus.FAILURE.name()); |
417 | } else { | 417 | } else { |
418 | if (toGateway) { | 418 | if (toGateway) { |
419 | result.addProperty("id", requestId); | 419 | result.addProperty("id", requestId); |
420 | } | 420 | } |
421 | - result.addProperty("deviceId", new UUID(payload.getDeviceCredentials().getDeviceIdMSB(), payload.getDeviceCredentials().getDeviceIdLSB()).toString()); | ||
422 | - result.addProperty("credentialsType", payload.getDeviceCredentials().getCredentialsType().name()); | ||
423 | - result.addProperty("credentialsId", payload.getDeviceCredentials().getCredentialsId()); | 421 | + result.addProperty("credentialsType", payload.getCredentialsType().name()); |
424 | result.addProperty("credentialsValue", | 422 | result.addProperty("credentialsValue", |
425 | - StringUtils.isEmpty(payload.getDeviceCredentials().getCredentialsValue()) ? null : payload.getDeviceCredentials().getCredentialsValue()); | ||
426 | - result.addProperty("provisionDeviceStatus", ProvisionResponseStatus.SUCCESS.name()); | 423 | + StringUtils.isEmpty(payload.getCredentialsValue()) ? null : payload.getCredentialsValue()); |
424 | + result.addProperty("status", ProvisionResponseStatus.SUCCESS.name()); | ||
427 | } | 425 | } |
428 | return result; | 426 | return result; |
429 | } | 427 | } |
@@ -551,7 +549,7 @@ public class JsonConverter { | @@ -551,7 +549,7 @@ public class JsonConverter { | ||
551 | 549 | ||
552 | private static TransportProtos.ProvisionDeviceRequestMsg buildProvisionRequestMsg(JsonObject jo) { | 550 | private static TransportProtos.ProvisionDeviceRequestMsg buildProvisionRequestMsg(JsonObject jo) { |
553 | return TransportProtos.ProvisionDeviceRequestMsg.newBuilder() | 551 | return TransportProtos.ProvisionDeviceRequestMsg.newBuilder() |
554 | - .setDeviceName(getStrValue(jo, DataConstants.DEVICE_NAME, true)) | 552 | + .setDeviceName(getStrValue(jo, DataConstants.DEVICE_NAME, false)) |
555 | .setCredentialsType(jo.get(DataConstants.CREDENTIALS_TYPE) != null ? TransportProtos.CredentialsType.valueOf(getStrValue(jo, DataConstants.CREDENTIALS_TYPE, false)) : CredentialsType.ACCESS_TOKEN) | 553 | .setCredentialsType(jo.get(DataConstants.CREDENTIALS_TYPE) != null ? TransportProtos.CredentialsType.valueOf(getStrValue(jo, DataConstants.CREDENTIALS_TYPE, false)) : CredentialsType.ACCESS_TOKEN) |
556 | .setCredentialsDataProto(TransportProtos.CredentialsDataProto.newBuilder() | 554 | .setCredentialsDataProto(TransportProtos.CredentialsDataProto.newBuilder() |
557 | .setValidateDeviceTokenRequestMsg(ValidateDeviceTokenRequestMsg.newBuilder().setToken(getStrValue(jo, DataConstants.TOKEN, false)).build()) | 555 | .setValidateDeviceTokenRequestMsg(ValidateDeviceTokenRequestMsg.newBuilder().setToken(getStrValue(jo, DataConstants.TOKEN, false)).build()) |