From ce4e9b58e869e68c8598e68e7bafaa335a1153d1 Mon Sep 17 00:00:00 2001 From: Loes Immens Date: Mon, 26 Aug 2024 16:08:51 +0200 Subject: [PATCH 1/3] FDP-2359: simulate device reboot Signed-off-by: Loes Immens --- .../configuration/SimulatorProperties.kt | 1 + .../simulator/message/MessageHandler.kt | 33 ++++++++---- .../simulator/response/CommandService.kt | 9 ++++ .../main/resources/application-test-data.yaml | 1 + .../messages/kod-reboot-success-message.json | 54 +++++++++++++++++++ 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt create mode 100644 application/src/main/resources/messages/kod-reboot-success-message.json diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt index 66233db..91cf0be 100644 --- a/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt @@ -19,5 +19,6 @@ class SimulatorProperties( val scheduledMessage: Resource, val successMessage: Resource, val failureMessage: Resource, + val rebootSuccessMessage: Resource, val produceValidCbor: Boolean, ) diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt index 5e74943..0c9b43f 100644 --- a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt @@ -18,6 +18,7 @@ import org.eclipse.californium.core.coap.Request 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.CommandService import org.gxf.crestdevicesimulator.simulator.response.PskExtractor import org.gxf.crestdevicesimulator.simulator.response.command.PskService import org.springframework.stereotype.Service @@ -27,7 +28,8 @@ class MessageHandler( private val coapClientService: CoapClientService, private val simulatorProperties: SimulatorProperties, private val pskService: PskService, - private val mapper: ObjectMapper + private val mapper: ObjectMapper, + private val commandService: CommandService ) { private val logger = KotlinLogging.logger {} @@ -70,10 +72,16 @@ class MessageHandler( private fun handleResponse(response: CoapResponse) { if (response.isSuccess) { val payload = String(response.payload) - if (PskExtractor.hasPskSetCommand(payload)) { - handlePskSetCommand(payload) - } else if (pskService.isPendingKeyPresent()) { - pskService.changeActiveKey() + when { + PskExtractor.hasPskSetCommand(payload) -> { + handlePskSetCommand(payload) + } + pskService.isPendingKeyPresent() -> { + pskService.changeActiveKey() + } + commandService.hasRebootCommand(payload) -> { + sendRebootSuccesMessage(payload) + } } } else { logger.error { "Received error response with ${response.code}" } @@ -88,15 +96,15 @@ class MessageHandler( try { logger.info { "Device ${simulatorProperties.pskIdentity} needs key change" } pskService.preparePendingKey(payload) - sendSuccessMessage(payload) + sendPskSetSuccessMessage(payload) } catch (e: Exception) { logger.error(e) { "PSK change error, send failure message and set pending key status to invalid" } - sendFailureMessage(payload) + sendPskSetFailureMessage(payload) pskService.setPendingKeyAsInvalid() } } - private fun sendSuccessMessage(pskCommand: String) { + private fun sendPskSetSuccessMessage(pskCommand: String) { logger.info { "Sending success message for command $pskCommand" } val messageJsonNode = mapper.readTree(simulatorProperties.successMessage.inputStream) @@ -104,7 +112,7 @@ class MessageHandler( sendMessage(message) } - private fun sendFailureMessage(pskCommand: String) { + private fun sendPskSetFailureMessage(pskCommand: String) { logger.warn { "Sending failure message for command $pskCommand" } val messageJsonNode = mapper.readTree(simulatorProperties.failureMessage.inputStream) @@ -112,6 +120,13 @@ class MessageHandler( sendMessage(message) } + private fun sendRebootSuccesMessage(command: String) { + logger.info { "Sending success message for command $command" } + val message = + mapper.readTree(simulatorProperties.rebootSuccessMessage.inputStream) + sendMessage(message) + } + private fun updatePskCommandInMessage( message: JsonNode, urc: String, diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt new file mode 100644 index 0000000..3642ba6 --- /dev/null +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt @@ -0,0 +1,9 @@ +package org.gxf.crestdevicesimulator.simulator.response + +import org.springframework.stereotype.Service + +@Service +class CommandService { + fun hasRebootCommand(command: String) = + command.contains("CMD:REBOOT") +} \ No newline at end of file diff --git a/application/src/main/resources/application-test-data.yaml b/application/src/main/resources/application-test-data.yaml index 00855d3..6c1af18 100644 --- a/application/src/main/resources/application-test-data.yaml +++ b/application/src/main/resources/application-test-data.yaml @@ -6,6 +6,7 @@ simulator: scheduled-message: "classpath:messages/kod-alarm-message.json" success-message: "classpath:messages/psk-change-success-message.json" failure-message: "classpath:messages/psk-change-failure-message.json" + reboot-success-message: "classpath:messages/kod-reboot-success-message.json" # Simulator will produce valid/invalid CBOR messages produce-valid-cbor: true # pre-shared key for coaps (dtls) diff --git a/application/src/main/resources/messages/kod-reboot-success-message.json b/application/src/main/resources/messages/kod-reboot-success-message.json new file mode 100644 index 0000000..e79c922 --- /dev/null +++ b/application/src/main/resources/messages/kod-reboot-success-message.json @@ -0,0 +1,54 @@ +{ + "ID": 867787050253370, + "MID": 1, + "IMSI": 460023210226023, + "EID": "89001012012341234012345678901224", + "ICCID": "89882280666074936745", + "TS": 1687522333, + "TSL": 1687522333, + "CON": "A", + "FW": 2100, + "TEL": 20416, + "cID": 7020155, + "PWR": 0, + "BAT": 3533, + "CSQ": 25, + "RSRQ": -210, + "RSRP": -99, + "SNR": 22, + "TRY": 1, + "MSI": 0, + "URC": [ + "INIT","WDR", + { + "DL": "!CMD:REBOOT" + } + ], + "A": [ + 3, + 1, + 0, + 0, + 0, + 0, + 0, + 0 + ], + "MEM": 7, + "UPT": 32, + "RLY": 0, + "T1": [ + 265 + ], + "H1": [ + 402 + ], + "D": 8, + "P1": [ + 12 + ], + "P2": [ + 12 + ], + "FMC": 0 +} From 267226c1a58fbe14fa28ee87c629e5d927e9f222 Mon Sep 17 00:00:00 2001 From: Loes Immens Date: Tue, 3 Sep 2024 16:38:57 +0200 Subject: [PATCH 2/3] FDP-2359: fix integration test Signed-off-by: Loes Immens --- .../resources/application.yaml | 1 + .../messages/kod-reboot-success-message.json | 54 +++++++++++++++++++ .../simulator/message/MessageHandlerTests.kt | 4 ++ 3 files changed, 59 insertions(+) create mode 100644 application/src/integrationTest/resources/messages/kod-reboot-success-message.json diff --git a/application/src/integrationTest/resources/application.yaml b/application/src/integrationTest/resources/application.yaml index 8afb919..4f479ec 100644 --- a/application/src/integrationTest/resources/application.yaml +++ b/application/src/integrationTest/resources/application.yaml @@ -19,6 +19,7 @@ simulator: scheduled-message: "classpath:messages/kod-message.json" success-message: "classpath:messages/psk-change-success-message.json" failure-message: "classpath:messages/psk-change-failure-message.json" + reboot-success-message: "classpath:messages/kod-reboot-success-message.json" # Simulator will produce valid/invalid CBOR messages produce-valid-cbor: true # Uri of the target coap server diff --git a/application/src/integrationTest/resources/messages/kod-reboot-success-message.json b/application/src/integrationTest/resources/messages/kod-reboot-success-message.json new file mode 100644 index 0000000..e79c922 --- /dev/null +++ b/application/src/integrationTest/resources/messages/kod-reboot-success-message.json @@ -0,0 +1,54 @@ +{ + "ID": 867787050253370, + "MID": 1, + "IMSI": 460023210226023, + "EID": "89001012012341234012345678901224", + "ICCID": "89882280666074936745", + "TS": 1687522333, + "TSL": 1687522333, + "CON": "A", + "FW": 2100, + "TEL": 20416, + "cID": 7020155, + "PWR": 0, + "BAT": 3533, + "CSQ": 25, + "RSRQ": -210, + "RSRP": -99, + "SNR": 22, + "TRY": 1, + "MSI": 0, + "URC": [ + "INIT","WDR", + { + "DL": "!CMD:REBOOT" + } + ], + "A": [ + 3, + 1, + 0, + 0, + 0, + 0, + 0, + 0 + ], + "MEM": 7, + "UPT": 32, + "RLY": 0, + "T1": [ + 265 + ], + "H1": [ + 402 + ], + "D": 8, + "P1": [ + 12 + ], + "P2": [ + 12 + ], + "FMC": 0 +} diff --git a/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt b/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt index 7e4d6a5..a4cae83 100644 --- a/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt +++ b/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt @@ -10,6 +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.CommandService import org.gxf.crestdevicesimulator.simulator.response.command.PskService import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith @@ -37,6 +38,9 @@ class MessageHandlerTests { @Mock private lateinit var pskService: PskService + @Mock + private lateinit var commandService: CommandService + @InjectMocks private lateinit var messageHandler: MessageHandler From 710e51fd04684f7aae546859d2da0f92e79f6a4f Mon Sep 17 00:00:00 2001 From: Loes Immens Date: Tue, 10 Sep 2024 16:51:18 +0200 Subject: [PATCH 3/3] FDP-2359: review comments Signed-off-by: Loes Immens --- .../simulator/response/CommandService.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt index 3642ba6..328d393 100644 --- a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: Contributors to the GXF project +// +// SPDX-License-Identifier: Apache-2.0 + package org.gxf.crestdevicesimulator.simulator.response import org.springframework.stereotype.Service @@ -6,4 +10,4 @@ import org.springframework.stereotype.Service class CommandService { fun hasRebootCommand(command: String) = command.contains("CMD:REBOOT") -} \ No newline at end of file +}