Skip to content

Commit

Permalink
FDP-1961: handle errors from device service
Browse files Browse the repository at this point in the history
Signed-off-by: Loes Immens <[email protected]>
  • Loading branch information
loesimmens committed Apr 2, 2024
1 parent 7565afe commit 85c8c84
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
import org.gxf.crestdevicesimulator.simulator.CborFactory
import org.gxf.crestdevicesimulator.simulator.coap.CoapClientService
import org.gxf.crestdevicesimulator.simulator.response.PskExtractor
import org.gxf.crestdevicesimulator.simulator.response.command.PskCommandHandler
import org.gxf.crestdevicesimulator.simulator.response.command.PskService
import org.springframework.stereotype.Service

@Service
class MessageHandler(
private val coapClientService: CoapClientService,
private val simulatorProperties: SimulatorProperties,
private val pskCommandHandler: PskCommandHandler,
private val pskService: PskService,
private val mapper: ObjectMapper
) {
private val logger = KotlinLogging.logger {}
Expand All @@ -47,7 +47,7 @@ class MessageHandler(
try {
coapClient = coapClientService.createCoapClient()
val response = coapClient.advanced(request)
logger.info { "Received Response: ${response.payload.decodeToString()}" }
logger.info { "Received Response: ${response.payload.decodeToString()} with status ${response.code}" }
handleResponse(response)
} catch (e: Exception) {
e.printStackTrace()
Expand All @@ -68,22 +68,34 @@ class MessageHandler(
}

private fun handleResponse(response: CoapResponse) {
val payload = String(response.payload)

if (PskExtractor.hasPskSetCommand(payload)) {
try {
logger.info { "Device ${simulatorProperties.pskIdentity} needs key change" }
pskCommandHandler.handlePskChange(payload)
sendSuccessMessage(payload)
pskCommandHandler.changeActiveKey()
} catch (e: Exception) {
logger.error(e) { "PSK change error, send failure message and set pending key status to invalid" }
sendFailureMessage(payload)
pskCommandHandler.setPendingKeyAsInvalid()
if (response.isSuccess) {
val payload = String(response.payload)
if (PskExtractor.hasPskSetCommand(payload)) {
handlePskSetCommand(payload)
} else if (pskService.isPendingKeyPresent()) {
pskService.changeActiveKey()
}
} else {
logger.error { "Received error response with ${response.code}" }
if (pskService.isPendingKeyPresent()) {
logger.error { "Error received. Set pending key to invalid" }
pskService.setPendingKeyAsInvalid()
}
}
}

private fun handlePskSetCommand(payload: String) {
try {
logger.info { "Device ${simulatorProperties.pskIdentity} needs key change" }
pskService.preparePendingKey(payload)
sendSuccessMessage(payload)
} catch (e: Exception) {
logger.error(e) { "PSK change error, send failure message and set pending key status to invalid" }
sendFailureMessage(payload)
pskService.setPendingKeyAsInvalid()
}
}

private fun sendSuccessMessage(pskCommand: String) {
logger.info { "Sending success message for command $pskCommand" }
val messageJsonNode =
Expand All @@ -110,8 +122,9 @@ class MessageHandler(
TextNode(urc),
ObjectNode(JsonNodeFactory.instance, mapOf(DL_FIELD to TextNode(receivedCommand)))
)
val array = mapper.valueToTree<ArrayNode>(urcList)
newMessage.replace(URC_FIELD, array)
val urcArray = mapper.valueToTree<ArrayNode>(urcList)
newMessage.replace(URC_FIELD, urcArray)
logger.debug { "Sending message with URC $urcArray" }
return newMessage
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import org.springframework.stereotype.Service

@Transactional
@Service
class PskCommandHandler(private val pskRepository: PskRepository,
private val simulatorProperties: SimulatorProperties,
private val pskStore: AdvancedSingleIdentityPskStore) {
class PskService(
private val pskRepository: PskRepository,
private val simulatorProperties: SimulatorProperties,
private val pskStore: AdvancedSingleIdentityPskStore
) {

private val logger = KotlinLogging.logger {}

fun handlePskChange(body: String): PreSharedKey {
fun preparePendingKey(body: String): PreSharedKey {
val newPsk = PskExtractor.extractKeyFromCommand(body)
val hash = PskExtractor.extractHashFromCommand(body)

Expand All @@ -48,6 +50,7 @@ class PskCommandHandler(private val pskRepository: PskRepository,

private fun setNewKeyForIdentity(previousPSK: PreSharedKey, newKey: String): PreSharedKey {
val newVersion = previousPSK.revision + 1
logger.debug { "Save new key for identity ${simulatorProperties.pskIdentity} with revision $newVersion and status PENDING" }
return pskRepository.save(
PreSharedKey(
previousPSK.identity,
Expand All @@ -59,6 +62,11 @@ class PskCommandHandler(private val pskRepository: PskRepository,
)
}

fun isPendingKeyPresent() = pskRepository.findFirstByIdentityAndStatusOrderByRevisionDesc(
simulatorProperties.pskIdentity,
PreSharedKeyStatus.PENDING
) != null

fun changeActiveKey() {
val identity = simulatorProperties.pskIdentity
val currentPsk =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.eclipse.californium.core.CoapClient
import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
import org.gxf.crestdevicesimulator.simulator.CborFactory
import org.gxf.crestdevicesimulator.simulator.coap.CoapClientService
import org.gxf.crestdevicesimulator.simulator.response.command.PskCommandHandler
import org.gxf.crestdevicesimulator.simulator.response.command.PskService
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
Expand All @@ -35,7 +35,7 @@ class MessageHandlerTests {
private lateinit var coapClientService: CoapClientService

@Mock
private lateinit var pskCommandHandler: PskCommandHandler
private lateinit var pskService: PskService

@InjectMocks
private lateinit var messageHandler: MessageHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@ExtendWith(MockitoExtension::class)
class PskCommandHandlerTest {
class PskServiceTest {

@Mock
private lateinit var pskRepository: PskRepository
Expand All @@ -39,7 +39,7 @@ class PskCommandHandlerTest {
private lateinit var pskStore: AdvancedSingleIdentityPskStore

@InjectMocks
private lateinit var pskCommandHandler: PskCommandHandler
private lateinit var pskService: PskService

private val newKey = "7654321987654321"

Expand Down Expand Up @@ -81,7 +81,7 @@ class PskCommandHandlerTest {
)
whenever(pskRepository.save(psk)).thenReturn(psk)

val result = pskCommandHandler.handlePskChange(pskCommand)
val result = pskService.preparePendingKey(pskCommand)

assertThat(result).isEqualTo(psk)
}
Expand All @@ -92,7 +92,7 @@ class PskCommandHandlerTest {
val pskCommand = "!PSK:$oldKey;PSK:$oldKey:${invalidHash}SET"

val thrownException = catchException {
pskCommandHandler.handlePskChange(pskCommand)
pskService.preparePendingKey(pskCommand)
}

assertThat(thrownException).isInstanceOf(InvalidPskException::class.java)
Expand All @@ -106,7 +106,7 @@ class PskCommandHandlerTest {
val pskCommand = "!PSK:$oldKey:$invalidHash;PSK:$oldKey:${invalidHash}SET"

val thrownException = catchException {
pskCommandHandler.handlePskChange(pskCommand)
pskService.preparePendingKey(pskCommand)
}

assertThat(thrownException).isInstanceOf(InvalidPskHashException::class.java)
Expand Down

0 comments on commit 85c8c84

Please sign in to comment.